Mercurial > games > semicongine
annotate src/zamikongine/shader.nim @ 29:da922b506570
add: corret make dependencies
| author | Sam <sam@basx.dev> |
|---|---|
| date | Sat, 14 Jan 2023 14:08:23 +0700 |
| parents | b1b05d4efb52 |
| children | 9edca5dc4e93 |
| 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 | |
| 8 import ./vulkan | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
9 import ./vertex |
| 28 | 10 import ./math/vector |
| 17 | 11 |
| 12 type | |
| 28 | 13 AllowedUniformType = SomeNumber|Vec |
| 14 UniformSlot *[T:AllowedUniformType] = object | |
| 15 ShaderProgram*[Uniforms] = object | |
| 17 | 16 entryPoint*: string |
| 17 programType*: VkShaderStageFlagBits | |
| 18 shader*: VkPipelineShaderStageCreateInfo | |
| 28 | 19 uniforms*: Uniforms |
| 17 | 20 |
|
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 |
|
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
32 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
|
33 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
|
34 |
| 28 | 35 # TODO: compiles only on linux for now (because we don't have compile-time functionality in std/tempfile) |
| 36 if not defined(linux): | |
| 37 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
|
38 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
|
39 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
|
40 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
|
41 |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
42 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
|
43 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
|
44 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
|
45 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
|
46 |
|
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 |
|
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 (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
|
55 (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
|
56 (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
|
57 (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
|
58 ) |
|
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
59 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
|
60 |
| 28 | 61 proc initShaderProgram*[T](device: VkDevice, programType: static VkShaderStageFlagBits, shader: static string, entryPoint: static string="main"): ShaderProgram[T] = |
| 17 | 62 result.entryPoint = entryPoint |
| 63 result.programType = programType | |
| 64 | |
|
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
65 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
|
66 var code = constcode |
| 17 | 67 var createInfo = VkShaderModuleCreateInfo( |
| 68 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, | |
| 69 codeSize: uint(code.len * sizeof(uint32)), | |
| 70 pCode: if code.len > 0: addr(code[0]) else: nil, | |
| 71 ) | |
| 72 var shaderModule: VkShaderModule | |
| 73 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule)) | |
| 74 | |
| 75 result.shader = VkPipelineShaderStageCreateInfo( | |
| 76 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 77 stage: programType, | |
| 78 module: shaderModule, | |
| 79 pName: cstring(result.entryPoint), # entry point for shader | |
| 80 ) | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
81 |
| 28 | 82 func generateVertexShaderCode*[VertexType](entryPoint, positionAttrName, colorAttrName: static string): string {.compileTime.} = |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
83 var lines: seq[string] |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
84 lines.add "#version 450" |
| 28 | 85 # lines.add "layout(binding = 0) uniform UniformBufferObject { float dt; } ubo;" |
| 86 lines.add generateGLSLDeclarations[VertexType]() | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
87 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
|
88 lines.add "void " & entryPoint & "() {" |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
89 |
| 28 | 90 for name, value in VertexType().fieldPairs: |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
91 when typeof(value) is VertexAttribute and name == positionAttrName: |
| 28 | 92 # lines.add " vec2 tmp = " & name & " * ubo.dt;" |
| 93 lines.add " vec2 tmp = " & name & ";" | |
| 94 lines.add " gl_Position = vec4(tmp, 0.0, 1.0);" | |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
95 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
|
96 lines.add " fragColor = " & name & ";" |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
97 lines.add "}" |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
98 return lines.join("\n") |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
99 |
| 28 | 100 func generateFragmentShaderCode*[VertexType](entryPoint: static string): string {.compileTime.} = |
|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
101 var lines: seq[string] |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
102 lines.add "#version 450" |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
103 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
|
104 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
|
105 lines.add "void " & entryPoint & "() {" |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
106 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
|
107 lines.add "}" |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
108 |
|
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
109 return lines.join("\n") |
