Mercurial > games > semicongine
annotate src/zamikongine/shader.nim @ 38:c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 18 Jan 2023 09:52:03 +0700 |
parents | 94c38e4b5782 |
children |
rev | line source |
---|---|
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
1 import std/os |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
2 import std/hashes |
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
3 import std/strformat |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
4 import std/strutils |
17 | 5 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
|
6 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
|
7 |
17 | 8 import ./vulkan_helpers |
32 | 9 import ./glsl_helpers |
17 | 10 import ./vulkan |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
11 import ./vertex |
32 | 12 import ./descriptor |
28 | 13 import ./math/vector |
17 | 14 |
15 type | |
28 | 16 AllowedUniformType = SomeNumber|Vec |
17 UniformSlot *[T:AllowedUniformType] = object | |
32 | 18 ShaderProgram*[VertexType, Uniforms] = object |
17 | 19 entryPoint*: string |
20 programType*: VkShaderStageFlagBits | |
21 shader*: VkPipelineShaderStageCreateInfo | |
28 | 22 uniforms*: Uniforms |
17 | 23 |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
22
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
35 proc compileGLSLToSPIRV(stage: static VkShaderStageFlagBits, shaderSource: static string, entrypoint: string): seq[uint32] {.compileTime.} = |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
36 const |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
37 stagename = stage2string(stage) |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
38 shaderHash = hash(shaderSource) |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
39 # cross compilation for windows workaround, sorry computer |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
40 shaderout = getTempDir().replace("\\", "/") & "/" & fmt"shader_{shaderHash}.{stagename}" |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
41 projectPath = querySetting(projectPath) |
25
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
42 |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
43 let (output, exitCode_glsl) = gorgeEx(command=fmt"{projectPath}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {shaderout}", input=shaderSource) |
25
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
44 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
|
45 raise newException(Exception, output) |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
46 if output == "": # this happens when the nim was invoked with "check" instead of "compile/c", as it prevents the gorgeEx command to really run. However, there is hope, see https://github.com/nim-lang/RFCs/issues/430 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
47 return result |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
48 let shaderbinary = staticRead shaderout |
25
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
49 |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
33
diff
changeset
|
50 let (output_rm, exitCode_rm) = gorgeEx(command=fmt"rm {shaderout}") |
25
8f290112718a
fix: build from scratch not working, remove temp shader files from compilation
Sam <sam@basx.dev>
parents:
24
diff
changeset
|
51 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
|
52 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
|
53 |
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 (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
|
58 (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
|
59 (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
|
60 (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
|
61 ) |
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
62 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
|
63 |
32 | 64 proc initShaderProgram*[VertexType, Uniforms](device: VkDevice, programType: static VkShaderStageFlagBits, shader: static string, entryPoint: static string="main"): ShaderProgram[VertexType, Uniforms] = |
17 | 65 result.entryPoint = entryPoint |
66 result.programType = programType | |
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 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
|
69 var code = constcode |
17 | 70 var createInfo = VkShaderModuleCreateInfo( |
71 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, | |
72 codeSize: uint(code.len * sizeof(uint32)), | |
73 pCode: if code.len > 0: addr(code[0]) else: nil, | |
74 ) | |
75 var shaderModule: VkShaderModule | |
76 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule)) | |
77 | |
78 result.shader = VkPipelineShaderStageCreateInfo( | |
79 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
80 stage: programType, | |
81 module: shaderModule, | |
82 pName: cstring(result.entryPoint), # entry point for shader | |
83 ) | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
84 |
32 | 85 func generateVertexShaderCode*[VertexType, Uniforms]( |
86 shaderBody: static string = "", | |
87 entryPoint: static string = "main", | |
88 glslVersion: static string = "450" | |
89 ): string {.compileTime.} = | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
90 var lines: seq[string] |
32 | 91 lines.add "#version " & glslVersion |
92 lines.add "layout(row_major) uniform;" | |
93 lines.add generateGLSLUniformDeclarations[Uniforms]() | |
94 lines.add generateGLSLVertexDeclarations[VertexType]() | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
95 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
|
96 lines.add "void " & entryPoint & "() {" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
97 |
32 | 98 var hasPosition = 0 |
99 var hasColor = 0 | |
28 | 100 for name, value in VertexType().fieldPairs: |
32 | 101 when typeof(value) is PositionAttribute: |
102 let glsltype = getGLSLType[getAttributeType(value)]() | |
103 lines.add &" {glsltype} in_position = " & name & ";" | |
104 if getAttributeType(value) is Vec2: | |
105 lines.add " vec4 out_position = vec4(in_position, 0.0, 1.0);" | |
106 elif getAttributeType(value) is Vec3: | |
107 lines.add " vec4 out_position = vec4(in_position, 1.0);" | |
108 elif getAttributeType(value) is Vec4: | |
109 lines.add " vec4 out_position = in_position;" | |
110 hasPosition += 1 | |
111 when typeof(value) is ColorAttribute: | |
112 let glsltype = getGLSLType[getAttributeType(value)]() | |
113 lines.add &" {glsltype} in_color = " & name & ";" | |
114 lines.add &" {glsltype} out_color = in_color;"; | |
115 hasColor += 1 | |
116 | |
117 lines.add shaderBody | |
118 lines.add " gl_Position = out_position;" | |
119 lines.add " fragColor = out_color;" | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
120 lines.add "}" |
32 | 121 if hasPosition != 1: |
122 raise newException(Exception, fmt"VertexType needs to have exactly one attribute of type PositionAttribute (has {hasPosition})") | |
123 if hasColor != 1: | |
124 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
|
125 return lines.join("\n") |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
126 |
32 | 127 func generateFragmentShaderCode*[VertexType]( |
128 shaderBody: static string = "", | |
129 entryPoint: static string = "main", | |
130 glslVersion: static string = "450" | |
131 ): string {.compileTime.} = | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
132 var lines: seq[string] |
32 | 133 lines.add "#version " & glslVersion |
134 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
|
135 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
|
136 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
|
137 lines.add "void " & entryPoint & "() {" |
32 | 138 lines.add " vec3 in_color = fragColor;" |
139 lines.add " vec3 out_color = in_color;" | |
140 lines.add shaderBody | |
141 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
|
142 lines.add "}" |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
143 |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
17
diff
changeset
|
144 return lines.join("\n") |