Mercurial > games > semicongine
annotate static_utils.nim @ 1174:26e367076c15 compiletime-tests
de: test2
author | sam <sam@basx.dev> |
---|---|
date | Sat, 29 Jun 2024 11:12:48 +0700 |
parents | 2e0b527c2753 |
children | fafc2f14da0b |
rev | line source |
---|---|
1162 | 1 import std/os |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
2 import std/enumerate |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
3 import std/hashes |
1159 | 4 import std/macros |
1161 | 5 import std/strformat |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
6 import std/strutils |
1164 | 7 import std/sequtils |
1162 | 8 import std/typetraits as tt |
1159 | 9 |
1161 | 10 import semicongine/core/utils |
11 import semicongine/core/imagetypes | |
1159 | 12 import semicongine/core/vector |
13 import semicongine/core/matrix | |
14 import semicongine/core/vulkanapi | |
1161 | 15 import semicongine/vulkan/buffer |
1159 | 16 |
17 template VertexAttribute* {.pragma.} | |
18 template InstanceAttribute* {.pragma.} | |
1162 | 19 template Pass* {.pragma.} |
20 template PassFlat* {.pragma.} | |
21 template ShaderOutput* {.pragma.} | |
1159 | 22 |
1165 | 23 const INFLIGHTFRAMES = 2'u32 |
1159 | 24 type |
25 SupportedGPUType* = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64] | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
26 ShaderObject*[TShader] = object |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
27 vertexShader: VkShaderModule |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
28 fragmentShader: VkShaderModule |
1159 | 29 |
30 func VkType[T: SupportedGPUType](value: T): VkFormat = | |
31 when T is float32: VK_FORMAT_R32_SFLOAT | |
32 elif T is float64: VK_FORMAT_R64_SFLOAT | |
33 elif T is int8: VK_FORMAT_R8_SINT | |
34 elif T is int16: VK_FORMAT_R16_SINT | |
35 elif T is int32: VK_FORMAT_R32_SINT | |
36 elif T is int64: VK_FORMAT_R64_SINT | |
37 elif T is uint8: VK_FORMAT_R8_UINT | |
38 elif T is uint16: VK_FORMAT_R16_UINT | |
39 elif T is uint32: VK_FORMAT_R32_UINT | |
40 elif T is uint64: VK_FORMAT_R64_UINT | |
41 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT | |
42 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT | |
43 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT | |
44 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT | |
45 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT | |
46 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT | |
47 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT | |
48 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT | |
49 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT | |
50 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT | |
51 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT | |
52 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT | |
53 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT | |
54 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT | |
55 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
56 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
57 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
58 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
1162 | 59 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT |
60 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT | |
61 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
62 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
63 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT | |
64 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT | |
65 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
66 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
67 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
68 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
69 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
70 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
71 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
72 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
73 else: {.error: "Unsupported data type on GPU".} | |
74 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
75 func GlslType[T: SupportedGPUType|Texture](value: T): string = |
1162 | 76 when T is float32: "float" |
77 elif T is float64: "double" | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
78 elif T is int8 or T is int16 or T is int32 or T is int64: "int" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
79 elif T is uint8 or T is uint16 or T is uint32 or T is uint64: "uint" |
1162 | 80 elif T is TVec2[int32]: "ivec2" |
81 elif T is TVec2[int64]: "ivec2" | |
82 elif T is TVec3[int32]: "ivec3" | |
83 elif T is TVec3[int64]: "ivec3" | |
84 elif T is TVec4[int32]: "ivec4" | |
85 elif T is TVec4[int64]: "ivec4" | |
86 elif T is TVec2[uint32]: "uvec2" | |
87 elif T is TVec2[uint64]: "uvec2" | |
88 elif T is TVec3[uint32]: "uvec3" | |
89 elif T is TVec3[uint64]: "uvec3" | |
90 elif T is TVec4[uint32]: "uvec4" | |
91 elif T is TVec4[uint64]: "uvec4" | |
92 elif T is TVec2[float32]: "vec2" | |
93 elif T is TVec2[float64]: "dvec2" | |
94 elif T is TVec3[float32]: "vec3" | |
95 elif T is TVec3[float64]: "dvec3" | |
96 elif T is TVec4[float32]: "vec4" | |
97 elif T is TVec4[float64]: "dvec4" | |
98 elif T is TMat2[float32]: "mat2" | |
99 elif T is TMat2[float64]: "dmat2" | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
100 elif T is TMat23[float32]: "mat23" |
1162 | 101 elif T is TMat23[float64]: "dmat23" |
102 elif T is TMat32[float32]: "mat32" | |
103 elif T is TMat32[float64]: "dmat32" | |
104 elif T is TMat3[float32]: "mat3" | |
105 elif T is TMat3[float64]: "dmat3" | |
106 elif T is TMat34[float32]: "mat34" | |
107 elif T is TMat34[float64]: "dmat34" | |
108 elif T is TMat43[float32]: "mat43" | |
109 elif T is TMat43[float64]: "dmat43" | |
110 elif T is TMat4[float32]: "mat4" | |
111 elif T is TMat4[float64]: "dmat4" | |
112 elif T is Texture: "sampler2D" | |
1159 | 113 else: {.error: "Unsupported data type on GPU".} |
114 | |
1161 | 115 template ForVertexDataFields*(inputData: typed, fieldname, valuename, isinstancename, body: untyped): untyped = |
1159 | 116 for theFieldname, value in fieldPairs(inputData): |
1161 | 117 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
1159 | 118 when not typeof(value) is seq: |
119 {.error: "field '" & theFieldname & "' needs to be a seq".} | |
120 when not typeof(value) is SupportedGPUType: | |
121 {.error: "field '" & theFieldname & "' is not a supported GPU type".} | |
122 block: | |
123 let `fieldname` {.inject.} = theFieldname | |
1162 | 124 let `valuename` {.inject.} = value |
125 let `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute) | |
1159 | 126 body |
127 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
128 template ForDescriptorFields*(inputData: typed, typename, countname, body: untyped): untyped = |
1161 | 129 for theFieldname, value in fieldPairs(inputData): |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
130 when typeof(value) is Texture: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
131 block: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
132 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
133 let `countname` {.inject.} = 1'u32 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
134 body |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
135 elif typeof(value) is object: |
1161 | 136 block: |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
137 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
138 let `countname` {.inject.} = 1'u32 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
139 body |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
140 elif typeof(value) is array: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
141 when elementType(value) is Texture: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
142 block: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
143 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
144 let `countname` {.inject.} = uint32(typeof(value).len) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
145 body |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
146 elif elementType(value) is object: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
147 block: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
148 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
149 let `countname` {.inject.} = uint32(typeof(value).len) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
150 body |
1161 | 151 |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
152 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 = |
1159 | 153 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]: |
154 2 | |
155 elif T is TMat32[float32] or T is TMat32[float64] or T is TMat3[float32] or T is TMat3[float64] or T is TMat34[float32] or T is TMat34[float64]: | |
156 3 | |
157 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]: | |
158 4 | |
159 else: | |
160 1 | |
161 | |
1162 | 162 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 = |
1159 | 163 #[ |
164 single location: | |
1162 | 165 - any scalar |
166 - any 16-bit vector | |
167 - any 32-bit vector | |
168 - any 64-bit vector that has max. 2 components | |
1159 | 169 16-bit scalar and vector types, and |
170 32-bit scalar and vector types, and | |
171 64-bit scalar and 2-component vector types. | |
172 two locations | |
173 64-bit three- and four-component vectors | |
174 ]# | |
1162 | 175 when T is TVec3[int64] or |
176 T is TVec4[int64] or | |
177 T is TVec3[uint64] or | |
178 T is TVec4[uint64] or | |
179 T is TVec3[float64] or | |
180 T is TVec4[float64] or | |
181 T is TMat23[float64] or | |
182 T is TMat3[float64] or | |
183 T is TMat34[float64] or | |
184 T is TMat43[float64] or | |
185 T is TMat4[float64]: | |
1159 | 186 return 2 |
187 else: | |
188 return 1 | |
189 | |
190 type | |
1161 | 191 IndexType = enum |
192 None, UInt8, UInt16, UInt32 | |
193 RenderBuffers = object | |
194 deviceBuffers: seq[Buffer] # for fast reads | |
195 hostVisibleBuffers: seq[Buffer] # for fast writes | |
196 Renderable[TMesh, TInstance] = object | |
197 vertexBuffers: seq[VkBuffer] | |
198 bufferOffsets: seq[VkDeviceSize] | |
1159 | 199 instanceCount: uint32 |
1161 | 200 case indexType: IndexType |
201 of None: | |
1160 | 202 vertexCount: uint32 |
1161 | 203 else: |
1160 | 204 indexBuffer: VkBuffer |
205 indexCount: uint32 | |
206 indexBufferOffset: VkDeviceSize | |
1162 | 207 Pipeline[TShader] = object |
1159 | 208 pipeline: VkPipeline |
209 layout: VkPipelineLayout | |
1164 | 210 descriptorSets: array[INFLIGHTFRAMES, VkDescriptorSet] |
1162 | 211 |
1161 | 212 converter toVkIndexType(indexType: IndexType): VkIndexType = |
213 case indexType: | |
214 of None: VK_INDEX_TYPE_NONE_KHR | |
215 of UInt8: VK_INDEX_TYPE_UINT8_EXT | |
216 of UInt16: VK_INDEX_TYPE_UINT16 | |
217 of UInt32: VK_INDEX_TYPE_UINT32 | |
1159 | 218 |
1172 | 219 proc CreateRenderPass*( |
220 device: VkDevice, | |
221 format: VkFormat, | |
222 ): VkRenderPass = | |
223 | |
224 var | |
225 attachments = @[VkAttachmentDescription( | |
226 format: format, | |
227 samples: VK_SAMPLE_COUNT_1_BIT, | |
228 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
229 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
230 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
231 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
232 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
233 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
234 )] | |
235 dependencies = @[VkSubpassDependency( | |
236 srcSubpass: VK_SUBPASS_EXTERNAL, | |
237 dstSubpass: 0, | |
238 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], | |
239 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
240 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
241 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
242 )] | |
243 outputs = @[ | |
244 VkAttachmentReference( | |
245 attachment: 0, | |
246 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
247 ) | |
248 ] | |
249 | |
250 var subpassesList = [ | |
251 VkSubpassDescription( | |
252 flags: VkSubpassDescriptionFlags(0), | |
253 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
254 inputAttachmentCount: 0, | |
255 pInputAttachments: nil, | |
256 colorAttachmentCount: uint32(outputs.len), | |
257 pColorAttachments: outputs.ToCPointer, | |
258 pResolveAttachments: nil, | |
259 pDepthStencilAttachment: nil, | |
260 preserveAttachmentCount: 0, | |
261 pPreserveAttachments: nil, | |
262 ) | |
263 ] | |
264 | |
265 var createInfo = VkRenderPassCreateInfo( | |
266 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
267 attachmentCount: uint32(attachments.len), | |
268 pAttachments: attachments.ToCPointer, | |
269 subpassCount: uint32(subpassesList.len), | |
270 pSubpasses: subpassesList.ToCPointer, | |
271 dependencyCount: uint32(dependencies.len), | |
272 pDependencies: dependencies.ToCPointer, | |
273 ) | |
274 checkVkResult device.vkCreateRenderPass(addr(createInfo), nil, addr(result)) | |
275 | |
1162 | 276 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = |
277 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | |
278 case stage | |
279 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | |
280 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | |
281 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" | |
282 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" | |
283 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" | |
284 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" | |
285 else: "" | |
1161 | 286 |
1162 | 287 when defined(nimcheck): # will not run if nimcheck is running |
288 return result | |
289 | |
290 let | |
291 stagename = stage2string(stage) | |
292 shaderHash = hash(shaderSource) | |
293 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" | |
294 | |
295 if not shaderfile.fileExists: | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
296 echo "shader of type ", stage |
1162 | 297 for i, line in enumerate(shaderSource.splitlines()): |
298 echo " ", i + 1, " ", line | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
299 # var glslExe = currentSourcePath.parentDir.parentDir.parentDir / "tools" / "glslangValidator" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
300 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator" |
1162 | 301 when defined(windows): |
302 glslExe = glslExe & "." & ExeExt | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
303 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" |
1162 | 304 echo "run: ", command |
305 discard StaticExecChecked( | |
306 command = command, | |
307 input = shaderSource | |
308 ) | |
309 else: | |
310 echo &"shaderfile {shaderfile} is up-to-date" | |
311 | |
312 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up | |
313 let shaderbinary = staticRead shaderfile.replace("\\", "/") | |
314 else: | |
315 let shaderbinary = staticRead shaderfile | |
316 | |
317 var i = 0 | |
318 while i < shaderbinary.len: | |
319 result.add( | |
320 (uint32(shaderbinary[i + 0]) shl 0) or | |
321 (uint32(shaderbinary[i + 1]) shl 8) or | |
322 (uint32(shaderbinary[i + 2]) shl 16) or | |
323 (uint32(shaderbinary[i + 3]) shl 24) | |
324 ) | |
325 i += 4 | |
326 | |
327 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = | |
328 const GLSL_VERSION = "450" | |
329 var vsInput: seq[string] | |
330 var vsOutput: seq[string] | |
331 var fsInput: seq[string] | |
332 var fsOutput: seq[string] | |
333 var uniforms: seq[string] | |
334 var samplers: seq[string] | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
335 var vsInputLocation = 0'u32 |
1162 | 336 var passLocation = 0 |
337 var fsOutputLocation = 0 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
338 var descriptorBinding = 0 |
1162 | 339 |
340 for fieldname, value in fieldPairs(shader): | |
341 # vertex shader inputs | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
342 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
1162 | 343 assert typeof(value) is SupportedGPUType |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
344 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" |
1162 | 345 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): |
346 vsInputLocation += NLocationSlots(value) | |
347 # intermediate values, passed between shaders | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
348 elif hasCustomPragma(value, Pass) or hasCustomPragma(value, PassFlat): |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
349 let flat = if hasCustomPragma(value, PassFlat): "flat " else: "" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
350 vsOutput.add "layout(location = " & $passLocation & ") " & flat & "out " & GlslType(value) & " " & fieldname & ";" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
351 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" |
1162 | 352 passLocation.inc |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
353 elif hasCustomPragma(value, ShaderOutput): |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
354 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" |
1162 | 355 fsOutputLocation.inc |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
356 elif typeof(value) is Texture: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
357 samplers.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(value) & " " & fieldname & ";" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
358 descriptorBinding.inc |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
359 elif typeof(value) is object: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
360 # TODO |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
361 uniforms.add "" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
362 descriptorBinding.inc |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
363 elif typeof(value) is array: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
364 when elementType(value) is Texture: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
365 let arrayDecl = "[" & $typeof(value).len & "]" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
366 samplers.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(value))) & " " & fieldname & "" & arrayDecl & ";" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
367 descriptorBinding.inc |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
368 elif elementType(value) is object: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
369 # TODO |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
370 let arrayDecl = "[" & $typeof(value).len & "]" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
371 # uniforms.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(elementType(value)) & " " & fieldname & "" & arrayDecl & ";" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
372 descriptorBinding.inc |
1162 | 373 else: |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
374 {.error: "Unsupported shader field " & fieldname.} |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
375 elif fieldname in ["vertexCode", "fragmentCode"]: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
376 discard |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
377 else: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
378 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).} |
1162 | 379 |
380 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
381 vsInput & | |
382 uniforms & | |
383 samplers & | |
384 vsOutput & | |
385 @[shader.vertexCode]).join("\n") | |
386 | |
387 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
388 fsInput & | |
389 uniforms & | |
390 samplers & | |
391 fsOutput & | |
392 @[shader.fragmentCode]).join("\n") | |
393 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
394 # proc CompileShader[TShader](shader: static TShader): (seq[uint32], seq[uint32]) {.compileTime.}= |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
395 proc CompileShader[TShader](device: VkDevice, shader: static TShader): ShaderObject[TShader] = |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
396 const (vertexShaderSource, fragmentShaderSource) = generateShaderSource(shader) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
397 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
398 let vertexBinary = compileGlslToSPIRV(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderSource) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
399 let fragmentBinary = compileGlslToSPIRV(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderSource) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
400 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
401 var createInfoVertex = VkShaderModuleCreateInfo( |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
402 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
403 codeSize: csize_t(vertexBinary.len * sizeof(uint32)), |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
404 pCode: vertexBinary.ToCPointer, |
1162 | 405 ) |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
406 checkVkResult device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result.vertexShader)) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
407 var createInfoFragment = VkShaderModuleCreateInfo( |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
408 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
409 codeSize: csize_t(fragmentBinary.len * sizeof(uint32)), |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
410 pCode: fragmentBinary.ToCPointer, |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
411 ) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
412 checkVkResult device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader)) |
1162 | 413 |
414 | |
415 proc CreatePipeline*[TShader]( | |
1159 | 416 device: VkDevice, |
417 renderPass: VkRenderPass, | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
418 shader: ShaderObject[TShader], |
1159 | 419 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, |
420 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, | |
421 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT, | |
422 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, | |
1162 | 423 ): Pipeline[TShader] = |
1159 | 424 # assumptions/limitations: |
425 # - we are only using vertex and fragment shaders (2 stages) | |
426 # - we only support one subpass | |
1162 | 427 # = we only support one Uniform-Block |
1161 | 428 |
1164 | 429 # create pipeline |
1161 | 430 var layoutbindings: seq[VkDescriptorSetLayoutBinding] |
431 var descriptorBindingNumber = 0'u32 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
432 ForDescriptorFields(default(TShader), descriptorType, descriptorCount): |
1161 | 433 layoutbindings.add VkDescriptorSetLayoutBinding( |
434 binding: descriptorBindingNumber, | |
435 descriptorType: descriptorType, | |
436 descriptorCount: descriptorCount, | |
1162 | 437 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), |
1161 | 438 pImmutableSamplers: nil, |
439 ) | |
440 inc descriptorBindingNumber | |
441 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( | |
442 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
443 bindingCount: uint32(layoutbindings.len), | |
444 pBindings: layoutbindings.ToCPointer | |
445 ) | |
446 var descriptorSetLayout: VkDescriptorSetLayout | |
1162 | 447 checkVkResult vkCreateDescriptorSetLayout(device, addr(layoutCreateInfo), nil, addr(descriptorSetLayout)) |
1161 | 448 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( |
449 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
450 setLayoutCount: 1, | |
451 pSetLayouts: addr(descriptorSetLayout), | |
452 # pushConstantRangeCount: uint32(pushConstants.len), | |
453 # pPushConstantRanges: pushConstants.ToCPointer, | |
454 ) | |
455 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil, addr(result.layout)) | |
1159 | 456 |
457 let stages = [ | |
458 VkPipelineShaderStageCreateInfo( | |
459 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
460 stage: VK_SHADER_STAGE_VERTEX_BIT, | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
461 module: shader.vertexShader, |
1159 | 462 pName: "main", |
463 ), | |
464 VkPipelineShaderStageCreateInfo( | |
465 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
466 stage: VK_SHADER_STAGE_FRAGMENT_BIT, | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
467 module: shader.fragmentShader, |
1159 | 468 pName: "main", |
469 ), | |
470 ] | |
1162 | 471 var |
472 bindings: seq[VkVertexInputBindingDescription] | |
473 attributes: seq[VkVertexInputAttributeDescription] | |
1159 | 474 var inputBindingNumber = 0'u32 |
1162 | 475 var location = 0'u32 |
476 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): | |
1159 | 477 bindings.add VkVertexInputBindingDescription( |
478 binding: inputBindingNumber, | |
479 stride: sizeof(value).uint32, | |
480 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX, | |
481 ) | |
482 # allows to submit larger data structures like Mat44, for most other types will be 1 | |
483 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value) | |
484 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value): | |
485 attributes.add VkVertexInputAttributeDescription( | |
486 binding: inputBindingNumber, | |
1162 | 487 location: location, |
1159 | 488 format: VkType(value), |
489 offset: i * perDescriptorSize, | |
490 ) | |
1162 | 491 location += NLocationSlots(value) |
1159 | 492 inc inputBindingNumber |
493 | |
494 let | |
495 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
496 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
497 vertexBindingDescriptionCount: uint32(bindings.len), | |
498 pVertexBindingDescriptions: bindings.ToCPointer, | |
499 vertexAttributeDescriptionCount: uint32(attributes.len), | |
500 pVertexAttributeDescriptions: attributes.ToCPointer, | |
501 ) | |
502 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
503 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
504 topology: topology, | |
505 primitiveRestartEnable: false, | |
506 ) | |
507 viewportState = VkPipelineViewportStateCreateInfo( | |
508 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | |
509 viewportCount: 1, | |
510 scissorCount: 1, | |
511 ) | |
512 rasterizer = VkPipelineRasterizationStateCreateInfo( | |
513 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | |
514 depthClampEnable: VK_FALSE, | |
515 rasterizerDiscardEnable: VK_FALSE, | |
516 polygonMode: polygonMode, | |
517 lineWidth: 1.0, | |
518 cullMode: toBits [cullMode], | |
519 frontFace: frontFace, | |
520 depthBiasEnable: VK_FALSE, | |
521 depthBiasConstantFactor: 0.0, | |
522 depthBiasClamp: 0.0, | |
523 depthBiasSlopeFactor: 0.0, | |
524 ) | |
525 multisampling = VkPipelineMultisampleStateCreateInfo( | |
526 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | |
527 sampleShadingEnable: VK_FALSE, | |
528 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, | |
529 minSampleShading: 1.0, | |
530 pSampleMask: nil, | |
531 alphaToCoverageEnable: VK_FALSE, | |
532 alphaToOneEnable: VK_FALSE, | |
533 ) | |
534 colorBlendAttachment = VkPipelineColorBlendAttachmentState( | |
535 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT], | |
536 blendEnable: VK_TRUE, | |
537 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, | |
538 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | |
539 colorBlendOp: VK_BLEND_OP_ADD, | |
540 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, | |
541 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, | |
542 alphaBlendOp: VK_BLEND_OP_ADD, | |
543 ) | |
544 colorBlending = VkPipelineColorBlendStateCreateInfo( | |
545 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | |
546 logicOpEnable: false, | |
547 attachmentCount: 1, | |
548 pAttachments: addr(colorBlendAttachment), | |
549 ) | |
550 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
551 dynamicState = VkPipelineDynamicStateCreateInfo( | |
552 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
553 dynamicStateCount: dynamicStates.len.uint32, | |
554 pDynamicStates: dynamicStates.ToCPointer, | |
555 ) | |
556 let createInfo = VkGraphicsPipelineCreateInfo( | |
557 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | |
558 stageCount: 2, | |
1162 | 559 pStages: stages.ToCPointer, |
1159 | 560 pVertexInputState: addr(vertexInputInfo), |
561 pInputAssemblyState: addr(inputAssembly), | |
562 pViewportState: addr(viewportState), | |
563 pRasterizationState: addr(rasterizer), | |
564 pMultisampleState: addr(multisampling), | |
565 pDepthStencilState: nil, | |
566 pColorBlendState: addr(colorBlending), | |
567 pDynamicState: addr(dynamicState), | |
568 layout: result.layout, | |
569 renderPass: renderPass, | |
570 subpass: 0, | |
571 basePipelineHandle: VkPipeline(0), | |
572 basePipelineIndex: -1, | |
573 ) | |
574 checkVkResult vkCreateGraphicsPipelines( | |
575 device, | |
576 VkPipelineCache(0), | |
577 1, | |
578 addr(createInfo), | |
579 nil, | |
580 addr(result.pipeline) | |
581 ) | |
582 | |
1164 | 583 # create descriptors, one per frame-in-flight |
584 let nSamplers = 0'u32 | |
585 let nUniformBuffers = 0'u32 | |
586 | |
587 if nSamplers + nUniformBuffers > 0: | |
588 var poolSizes: seq[VkDescriptorPoolSize] | |
589 if nUniformBuffers > 0: | |
1165 | 590 poolSizes.add VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: nSamplers * INFLIGHTFRAMES) |
1164 | 591 if nSamplers > 0: |
1165 | 592 poolSizes.add VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: nUniformBuffers * INFLIGHTFRAMES) |
1164 | 593 var poolInfo = VkDescriptorPoolCreateInfo( |
594 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
595 poolSizeCount: uint32(poolSizes.len), | |
596 pPoolSizes: poolSizes.ToCPointer, | |
1165 | 597 maxSets: (nUniformBuffers + nSamplers) * INFLIGHTFRAMES * 2, # good formula? no idea... |
1164 | 598 ) |
599 var pool: VkDescriptorPool | |
600 checkVkResult vkCreateDescriptorPool(device, addr(poolInfo), nil, addr(pool)) | |
601 | |
602 var layouts = newSeqWith(result.descriptorSets.len, descriptorSetLayout) | |
603 var allocInfo = VkDescriptorSetAllocateInfo( | |
604 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
605 descriptorPool: pool, | |
606 descriptorSetCount: uint32(layouts.len), | |
607 pSetLayouts: layouts.ToCPointer, | |
608 ) | |
609 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), result.descriptorSets.ToCPointer) | |
610 | |
1165 | 611 # write descriptor sets |
612 # TODO | |
1172 | 613 #[ |
1165 | 614 var descriptorSetWrites: seq[VkWriteDescriptorSet] |
615 for XY in descriptors?: | |
616 | |
1172 | 617 bufferInfos.add VkDescriptorBufferInfo( |
618 buffer: descriptor.buffer.vk, | |
619 offset: descriptor.offset, | |
620 range: descriptor.size, | |
621 ) | |
622 descriptorSetWrites.add VkWriteDescriptorSet( | |
623 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
624 dstSet: descriptorSet.vk, | |
625 dstBinding: i, | |
626 dstArrayElement: 0, | |
627 descriptorType: descriptor.vkType, | |
628 descriptorCount: uint32(descriptor.count), | |
629 pBufferInfo: addr bufferInfos[^1], | |
630 ) | |
631 vkUpdateDescriptorSets(device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) | |
632 ]# | |
1165 | 633 |
634 | |
1161 | 635 proc CreateRenderable[TMesh, TInstance]( |
636 mesh: TMesh, | |
637 instance: TInstance, | |
638 buffers: RenderBuffers, | |
639 ): Renderable[TMesh, TInstance] = | |
640 result.indexType = None | |
1159 | 641 |
1172 | 642 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
643 let a = pipeline.descriptorSets | |
644 echo a[^currentFrameInFlight] | |
1159 | 645 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) |
1172 | 646 if a[currentFrameInFlight] != VkDescriptorSet(0): |
1164 | 647 commandBuffer.vkCmdBindDescriptorSets( |
648 VK_PIPELINE_BIND_POINT_GRAPHICS, | |
649 pipeline.layout, | |
650 0, | |
651 1, | |
652 addr pipeline.descriptorSets[currentFrameInFlight], | |
653 0, | |
654 nil, | |
655 ) | |
1161 | 656 |
1162 | 657 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals: typedesc) = |
658 # assert seq-fields of TMesh|TInstance == seq-fields of TShader | |
1161 | 659 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors |
1162 | 660 for inputName, inputValue in default(TShader).fieldPairs: |
1161 | 661 var foundField = false |
662 when hasCustomPragma(inputValue, VertexAttribute): | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
663 assert typeof(inputValue) is SupportedGPUType |
1161 | 664 for meshName, meshValue in default(TMesh).fieldPairs: |
665 when meshName == inputName: | |
1162 | 666 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
667 assert elementType(meshValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue)) & "'" |
1161 | 668 foundField = true |
1162 | 669 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "'" |
1161 | 670 elif hasCustomPragma(inputValue, InstanceAttribute): |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
671 assert typeof(inputValue) is SupportedGPUType |
1161 | 672 for instanceName, instanceValue in default(TInstance).fieldPairs: |
673 when instanceName == inputName: | |
1162 | 674 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
675 assert elementType(instanceValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but instance attribute is of type '" & tt.name(elementType(instanceValue)) & "'" |
1161 | 676 foundField = true |
1162 | 677 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TInstance) & "'" |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
678 elif typeof(inputValue) is Texture or typeof(inputValue) is object: |
1161 | 679 for meshName, meshValue in default(TMesh).fieldPairs: |
680 when meshName == inputName: | |
1162 | 681 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
682 assert typeof(meshValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue)) & "'" |
1161 | 683 foundField = true |
684 for globalName, globalValue in default(TGlobals).fieldPairs: | |
685 when globalName == inputName: | |
1162 | 686 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
687 assert typeof(globalValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue)) & "'" | |
1161 | 688 foundField = true |
1162 | 689 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
690 elif typeof(inputValue) is array: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
691 when (elementType(inputValue) is Texture or elementType(inputValue) is object): |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
692 for meshName, meshValue in default(TMesh).fieldPairs: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
693 when meshName == inputName: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
694 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
695 assert typeof(meshValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue)) & "'" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
696 foundField = true |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
697 for globalName, globalValue in default(TGlobals).fieldPairs: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
698 when globalName == inputName: |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
699 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
700 assert typeof(globalValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue)) & "'" |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
701 foundField = true |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
702 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
1161 | 703 |
704 | |
1162 | 705 proc Render[TShader, TMesh, TInstance, TGlobals]( |
706 pipeline: Pipeline[TShader], | |
1161 | 707 renderable: Renderable[TMesh, TInstance], |
708 globals: TGlobals, | |
709 commandBuffer: VkCommandBuffer, | |
710 ) = | |
1165 | 711 {.error: "Need to write descriptor sets".} |
1164 | 712 static: AssertCompatible(TShader, TMesh, TInstance, TGlobals) |
713 if renderable.vertexBuffers.len > 0: | |
714 commandBuffer.vkCmdBindVertexBuffers( | |
715 firstBinding = 0'u32, | |
716 bindingCount = uint32(renderable.vertexBuffers.len), | |
717 pBuffers = renderable.vertexBuffers.ToCPointer(), | |
718 pOffsets = renderable.bufferOffsets.ToCPointer() | |
719 ) | |
1161 | 720 if renderable.indexType != None: |
1159 | 721 commandBuffer.vkCmdBindIndexBuffer( |
722 renderable.indexBuffer, | |
723 renderable.indexBufferOffset, | |
1161 | 724 renderable.indexType, |
1159 | 725 ) |
726 commandBuffer.vkCmdDrawIndexed( | |
1161 | 727 indexCount = renderable.indexCount, |
728 instanceCount = renderable.instanceCount, | |
1159 | 729 firstIndex = 0, |
730 vertexOffset = 0, | |
731 firstInstance = 0 | |
732 ) | |
733 else: | |
734 commandBuffer.vkCmdDraw( | |
1161 | 735 vertexCount = renderable.vertexCount, |
736 instanceCount = renderable.instanceCount, | |
1159 | 737 firstVertex = 0, |
738 firstInstance = 0 | |
739 ) | |
1161 | 740 |
741 when isMainModule: | |
1162 | 742 import semicongine/platform/window |
743 import semicongine/vulkan/instance | |
744 import semicongine/vulkan/device | |
745 import semicongine/vulkan/physicaldevice | |
1172 | 746 # import semicongine/vulkan/renderpass |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
747 import semicongine/vulkan/commandbuffer |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
748 import std/options |
1162 | 749 |
1161 | 750 type |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
751 MaterialA = object |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
752 reflection: float32 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
753 baseColor: Vec3f |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
754 ShaderSettings = object |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
755 brightness: float32 |
1161 | 756 MeshA = object |
757 position: seq[Vec3f] | |
758 transparency: float | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
759 material: array[3, MaterialA] |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
760 materialTextures: array[3, Texture] |
1161 | 761 InstanceA = object |
762 transform: seq[Mat4] | |
763 position: seq[Vec3f] | |
764 Globals = object | |
1162 | 765 fontAtlas: Texture |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
766 settings: ShaderSettings |
1161 | 767 |
1162 | 768 ShaderA = object |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
769 # vertex input |
1161 | 770 position {.VertexAttribute.}: Vec3f |
771 transform {.InstanceAttribute.}: Mat4 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
772 # intermediate |
1162 | 773 test {.Pass.}: float32 |
774 test1 {.PassFlat.}: Vec3f | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
775 # output |
1162 | 776 color {.ShaderOutput.}: Vec4f |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
777 # uniforms |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
778 material: array[3, MaterialA] |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
779 settings: ShaderSettings |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
780 # textures |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
781 fontAtlas: Texture |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
782 materialTextures: array[3, Texture] |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
783 # code |
1162 | 784 vertexCode: string = "void main() {}" |
785 fragmentCode: string = "void main() {}" | |
1161 | 786 |
1162 | 787 let w = CreateWindow("test2") |
788 putEnv("VK_LAYER_ENABLES", "VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_AMD,VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_NVIDIA,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXTVK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT") | |
789 let i = w.CreateInstance( | |
790 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), | |
791 instanceExtensions = @[], | |
792 layers = @["VK_LAYER_KHRONOS_validation"], | |
793 ) | |
794 | |
795 | |
796 let selectedPhysicalDevice = i.GetPhysicalDevices().FilterBestGraphics() | |
797 let d = i.CreateDevice( | |
798 selectedPhysicalDevice, | |
799 enabledExtensions = @[], | |
800 selectedPhysicalDevice.FilterForGraphicsPresentationQueues() | |
801 ) | |
802 | |
1161 | 803 var r: Renderable[MeshA, InstanceA] |
804 var g: Globals | |
805 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
806 const shader = ShaderA() |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
807 let shaderObject = d.vk.CompileShader(shader) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
808 let rp = d.vk.CreateRenderPass(d.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
809 var p = CreatePipeline(d.vk, renderPass = rp, shaderObject) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
810 |
1172 | 811 let commandBufferPool = d.CreateCommandBufferPool(d.FirstGraphicsQueue().get().family, INFLIGHTFRAMES.int) |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
812 let cmd = commandBufferPool.buffers[0] |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
813 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
814 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
815 let beginInfo = VkCommandBufferBeginInfo( |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
816 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
817 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
818 ) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
819 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) |
1172 | 820 Bind(p, cmd, currentFrameInFlight = 0) |
821 Render(p, r, g, cmd) | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
822 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
823 checkVkResult cmd.vkEndCommandBuffer() |