Mercurial > games > semicongine
annotate src/zamikongine/shader.nim @ 32:9edca5dc4e93
add: working implementation of uniforms
| author | Sam <sam@basx.dev> |
|---|---|
| date | Sat, 14 Jan 2023 23:34:50 +0700 |
| parents | b1b05d4efb52 |
| children | 94c38e4b5782 |
| rev | line source |
|---|---|
|
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
1 import std/strformat |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
2 import std/strutils |
| 17 | 3 import std/tables |
|
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
4 import std/compilesettings |
|
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
5 |
| 17 | 6 |
| 7 import ./vulkan_helpers | |
| 32 | 8 import ./glsl_helpers |
| 17 | 9 import ./vulkan |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
10 import ./vertex |
| 32 | 11 import ./descriptor |
| 28 | 12 import ./math/vector |
| 17 | 13 |
| 14 type | |
| 28 | 15 AllowedUniformType = SomeNumber|Vec |
| 16 UniformSlot *[T:AllowedUniformType] = object | |
| 32 | 17 ShaderProgram*[VertexType, Uniforms] = object |
| 17 | 18 entryPoint*: string |
| 19 programType*: VkShaderStageFlagBits | |
| 20 shader*: VkPipelineShaderStageCreateInfo | |
| 28 | 21 uniforms*: Uniforms |
| 17 | 22 |
|
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
23 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = |
|
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
24 case stage |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
25 of VK_SHADER_STAGE_VERTEX_BIT: "vert" |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
26 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
27 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
28 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
29 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
30 of VK_SHADER_STAGE_ALL_GRAPHICS: "" |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
31 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
32 of VK_SHADER_STAGE_ALL: "" |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
33 |
|
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
34 proc compileGLSLToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string, entrypoint: string): seq[uint32] {.compileTime.} = |
|
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
35 let stagename = stage2string(stage) |
|
25
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
36 |
| 28 | 37 # TODO: compiles only on linux for now (because we don't have compile-time functionality in std/tempfile) |
| 38 if not defined(linux): | |
| 39 raise newException(Exception, "Compilation is currently only supported on linux (need mktemp command), sorry!") | |
|
25
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
40 let (tmpfile, exitCode) = gorgeEx(command=fmt"mktemp --tmpdir shader_XXXXXXX.{stagename}") |
|
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
41 if exitCode != 0: |
|
25
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
42 raise newException(Exception, tmpfile) |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
43 |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
44 let (output, exitCode_glsl) = gorgeEx(command=fmt"{querySetting(projectPath)}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {tmpfile}", input=shaderSource) |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
45 if exitCode_glsl != 0: |
|
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
46 raise newException(Exception, output) |
|
25
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
47 let shaderbinary = staticRead tmpfile |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
48 |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
49 let (output_rm, exitCode_rm) = gorgeEx(command=fmt"rm {tmpfile}") |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
50 if exitCode_rm != 0: |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
51 raise newException(Exception, output_rm) |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
52 |
|
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
53 var i = 0 |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
54 while i < shaderbinary.len: |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
55 result.add( |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
56 (uint32(shaderbinary[i + 0]) shl 0) or |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
57 (uint32(shaderbinary[i + 1]) shl 8) or |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
58 (uint32(shaderbinary[i + 2]) shl 16) or |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
59 (uint32(shaderbinary[i + 3]) shl 24) |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
60 ) |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
61 i += 4 |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
62 |
| 32 | 63 proc initShaderProgram*[VertexType, Uniforms](device: VkDevice, programType: static VkShaderStageFlagBits, shader: static string, entryPoint: static string="main"): ShaderProgram[VertexType, Uniforms] = |
| 17 | 64 result.entryPoint = entryPoint |
| 65 result.programType = programType | |
| 66 | |
|
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
67 const constcode = compileGLSLToSPIRV(programType, shader, entryPoint) |
|
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
68 var code = constcode |
| 17 | 69 var createInfo = VkShaderModuleCreateInfo( |
| 70 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, | |
| 71 codeSize: uint(code.len * sizeof(uint32)), | |
| 72 pCode: if code.len > 0: addr(code[0]) else: nil, | |
| 73 ) | |
| 74 var shaderModule: VkShaderModule | |
| 75 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule)) | |
| 76 | |
| 77 result.shader = VkPipelineShaderStageCreateInfo( | |
| 78 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 79 stage: programType, | |
| 80 module: shaderModule, | |
| 81 pName: cstring(result.entryPoint), # entry point for shader | |
| 82 ) | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
83 |
| 32 | 84 func generateVertexShaderCode*[VertexType, Uniforms]( |
| 85 shaderBody: static string = "", | |
| 86 entryPoint: static string = "main", | |
| 87 glslVersion: static string = "450" | |
| 88 ): string {.compileTime.} = | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
89 var lines: seq[string] |
| 32 | 90 lines.add "#version " & glslVersion |
| 91 lines.add "layout(row_major) uniform;" | |
| 92 lines.add generateGLSLUniformDeclarations[Uniforms]() | |
| 93 lines.add generateGLSLVertexDeclarations[VertexType]() | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
94 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
|
95 lines.add "void " & entryPoint & "() {" |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
96 |
| 32 | 97 var hasPosition = 0 |
| 98 var hasColor = 0 | |
| 28 | 99 for name, value in VertexType().fieldPairs: |
| 32 | 100 when typeof(value) is PositionAttribute: |
| 101 let glsltype = getGLSLType[getAttributeType(value)]() | |
| 102 lines.add &" {glsltype} in_position = " & name & ";" | |
| 103 if getAttributeType(value) is Vec2: | |
| 104 lines.add " vec4 out_position = vec4(in_position, 0.0, 1.0);" | |
| 105 elif getAttributeType(value) is Vec3: | |
| 106 lines.add " vec4 out_position = vec4(in_position, 1.0);" | |
| 107 elif getAttributeType(value) is Vec4: | |
| 108 lines.add " vec4 out_position = in_position;" | |
| 109 hasPosition += 1 | |
| 110 when typeof(value) is ColorAttribute: | |
| 111 let glsltype = getGLSLType[getAttributeType(value)]() | |
| 112 lines.add &" {glsltype} in_color = " & name & ";" | |
| 113 lines.add &" {glsltype} out_color = in_color;"; | |
| 114 hasColor += 1 | |
| 115 | |
| 116 lines.add shaderBody | |
| 117 lines.add " gl_Position = out_position;" | |
| 118 lines.add " fragColor = out_color;" | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
119 lines.add "}" |
| 32 | 120 if hasPosition != 1: |
| 121 raise newException(Exception, fmt"VertexType needs to have exactly one attribute of type PositionAttribute (has {hasPosition})") | |
| 122 if hasColor != 1: | |
| 123 raise newException(Exception, fmt"VertexType needs to have exactly one attribute of type ColorAttribute (has {hasColor})") | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
124 return lines.join("\n") |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
125 |
| 32 | 126 func generateFragmentShaderCode*[VertexType]( |
| 127 shaderBody: static string = "", | |
| 128 entryPoint: static string = "main", | |
| 129 glslVersion: static string = "450" | |
| 130 ): string {.compileTime.} = | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
131 var lines: seq[string] |
| 32 | 132 lines.add "#version " & glslVersion |
| 133 lines.add "layout(row_major) uniform;" | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
134 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
|
135 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
|
136 lines.add "void " & entryPoint & "() {" |
| 32 | 137 lines.add " vec3 in_color = fragColor;" |
| 138 lines.add " vec3 out_color = in_color;" | |
| 139 lines.add shaderBody | |
| 140 lines.add " outColor = vec4(out_color, 1.0);" | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
141 lines.add "}" |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
142 |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
143 return lines.join("\n") |
