annotate src/zamikongine/shader.nim @ 19:b55d6ecde79d

did: introduce scene graph, meshs and generic vertex buffers
author Sam <sam@basx.dev>
date Mon, 09 Jan 2023 11:04:19 +0700
parents src/shader.nim@b40466fa446a
children b45a5d338cd0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
1 import std/strutils
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/tables
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
3
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
4 import ./vulkan_helpers
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
5 import ./vulkan
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
6 import ./vertex
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
7 import ./glslang/glslang
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
8
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
9 type
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
10 ShaderProgram* = object
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
11 entryPoint*: string
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
12 programType*: VkShaderStageFlagBits
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
13 shader*: VkPipelineShaderStageCreateInfo
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
14
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
15 proc initShaderProgram*(device: VkDevice, programType: VkShaderStageFlagBits, shader: string, entryPoint: string="main"): ShaderProgram =
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
16 result.entryPoint = entryPoint
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
17 result.programType = programType
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
18
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
19 const VK_GLSL_MAP = {
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
20 VK_SHADER_STAGE_VERTEX_BIT: GLSLANG_STAGE_VERTEX,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
21 VK_SHADER_STAGE_FRAGMENT_BIT: GLSLANG_STAGE_FRAGMENT,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
22 }.toTable()
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
23 var code = compileGLSLToSPIRV(VK_GLSL_MAP[result.programType], shader, "<memory-shader>")
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
24 var createInfo = VkShaderModuleCreateInfo(
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
25 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
26 codeSize: uint(code.len * sizeof(uint32)),
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
27 pCode: if code.len > 0: addr(code[0]) else: nil,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
28 )
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
29 var shaderModule: VkShaderModule
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
30 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule))
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
31
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
32 result.shader = VkPipelineShaderStageCreateInfo(
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
33 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
34 stage: programType,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
35 module: shaderModule,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
36 pName: cstring(result.entryPoint), # entry point for shader
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
37 )
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
38
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
39 func generateVertexShaderCode*[T](entryPoint, positionAttrName, colorAttrName: static string): string =
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
40 var lines: seq[string]
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
41 lines.add "#version 450"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
42 lines.add generateGLSLDeclarations[T]()
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
43 lines.add "layout(location = 0) out vec3 fragColor;"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
44 lines.add "void " & entryPoint & "() {"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
45
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
46 for name, value in T().fieldPairs:
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
47 when typeof(value) is VertexAttribute and name == positionAttrName:
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
48 lines.add " gl_Position = vec4(" & name & ", 0.0, 1.0);"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
49 when typeof(value) is VertexAttribute and name == colorAttrName:
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
50 lines.add " fragColor = " & name & ";"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
51 lines.add "}"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
52 return lines.join("\n")
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
53
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
54 func generateFragmentShaderCode*[T](entryPoint: static string): string =
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
55 var lines: seq[string]
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
56 lines.add "#version 450"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
57 lines.add "layout(location = 0) in vec3 fragColor;"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
58 lines.add "layout(location = 0) out vec4 outColor;"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
59 lines.add "void " & entryPoint & "() {"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
60 lines.add " outColor = vec4(fragColor, 1.0);"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
61 lines.add "}"
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
62
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
63 return lines.join("\n")