Mercurial > games > semicongine
annotate src/zamikongine/shader.nim @ 24:71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 11 Jan 2023 11:15:02 +0700 |
parents | b45a5d338cd0 |
children | 8f290112718a |
rev | line source |
---|---|
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
1 import std/tempfiles |
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
2 import std/strformat |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
3 import std/strutils |
17 | 4 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
|
5 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
|
6 |
17 | 7 |
8 import ./vulkan_helpers | |
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 |
17 | 11 |
12 type | |
13 ShaderProgram* = object | |
14 entryPoint*: string | |
15 programType*: VkShaderStageFlagBits | |
16 shader*: VkPipelineShaderStageCreateInfo | |
17 | |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
18 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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
29 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
|
30 let stagename = stage2string(stage) |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
31 let (tmpfile, tmpfilename) = createTempFile("shader", stage2string(stage)) |
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
32 tmpfile.close() |
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
33 let (output, exitCode) = gorgeEx(command=fmt"{querySetting(projectPath)}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {tmpfilename}", input=shaderSource) |
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
34 if exitCode != 0: |
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
35 raise newException(Exception, output) |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
36 let shaderbinary = staticRead tmpfilename |
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 (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
|
41 (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
|
42 (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
|
43 (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
|
44 ) |
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
45 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
|
46 |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
47 proc initShaderProgram*(device: VkDevice, programType: static VkShaderStageFlagBits, shader: static string, entryPoint: static string="main"): ShaderProgram = |
17 | 48 result.entryPoint = entryPoint |
49 result.programType = programType | |
50 | |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
51 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
|
52 var code = constcode |
17 | 53 var createInfo = VkShaderModuleCreateInfo( |
54 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, | |
55 codeSize: uint(code.len * sizeof(uint32)), | |
56 pCode: if code.len > 0: addr(code[0]) else: nil, | |
57 ) | |
58 var shaderModule: VkShaderModule | |
59 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule)) | |
60 | |
61 result.shader = VkPipelineShaderStageCreateInfo( | |
62 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
63 stage: programType, | |
64 module: shaderModule, | |
65 pName: cstring(result.entryPoint), # entry point for shader | |
66 ) | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
67 |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
68 func generateVertexShaderCode*[T](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
|
69 var lines: seq[string] |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
70 lines.add "#version 450" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
71 lines.add generateGLSLDeclarations[T]() |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
72 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
|
73 lines.add "void " & entryPoint & "() {" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
74 |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
75 for name, value in T().fieldPairs: |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 lines.add " fragColor = " & name & ";" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
80 lines.add "}" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
81 return lines.join("\n") |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
82 |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
83 func generateFragmentShaderCode*[T](entryPoint: static string): string {.compileTime.} = |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
84 var lines: seq[string] |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
85 lines.add "#version 450" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
86 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
|
87 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
|
88 lines.add "void " & entryPoint & "() {" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
89 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
|
90 lines.add "}" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
91 |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
92 return lines.join("\n") |