Mercurial > games > semicongine
annotate static_utils.nim @ 1173:fafc2f14da0b compiletime-tests
more stuff
author | sam <sam@basx.dev> |
---|---|
date | Sat, 29 Jun 2024 17:34:22 +0700 |
parents | 2e0b527c2753 |
children | 511c9f7cd1da |
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 | |
1173 | 128 template ForDescriptorFields*(inputData: typed, typename, countname, bindingNumber, body: untyped): untyped = |
129 var `bindingNumber` {.inject.} = 1'u32 | |
1161 | 130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 body |
1173 | 136 `bindingNumber`.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
|
137 elif typeof(value) is object: |
1161 | 138 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
|
139 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
|
140 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
|
141 body |
1173 | 142 `bindingNumber`.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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 body |
1173 | 149 `bindingNumber`.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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 body |
1173 | 155 `bindingNumber`.inc |
1161 | 156 |
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
|
157 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 = |
1159 | 158 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]: |
159 2 | |
160 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]: | |
161 3 | |
162 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]: | |
163 4 | |
164 else: | |
165 1 | |
166 | |
1162 | 167 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 = |
1159 | 168 #[ |
169 single location: | |
1162 | 170 - any scalar |
171 - any 16-bit vector | |
172 - any 32-bit vector | |
173 - any 64-bit vector that has max. 2 components | |
1159 | 174 16-bit scalar and vector types, and |
175 32-bit scalar and vector types, and | |
176 64-bit scalar and 2-component vector types. | |
177 two locations | |
178 64-bit three- and four-component vectors | |
179 ]# | |
1162 | 180 when T is TVec3[int64] or |
181 T is TVec4[int64] or | |
182 T is TVec3[uint64] or | |
183 T is TVec4[uint64] or | |
184 T is TVec3[float64] or | |
185 T is TVec4[float64] or | |
186 T is TMat23[float64] or | |
187 T is TMat3[float64] or | |
188 T is TMat34[float64] or | |
189 T is TMat43[float64] or | |
190 T is TMat4[float64]: | |
1159 | 191 return 2 |
192 else: | |
193 return 1 | |
194 | |
195 type | |
1161 | 196 IndexType = enum |
197 None, UInt8, UInt16, UInt32 | |
198 RenderBuffers = object | |
199 deviceBuffers: seq[Buffer] # for fast reads | |
200 hostVisibleBuffers: seq[Buffer] # for fast writes | |
201 Renderable[TMesh, TInstance] = object | |
202 vertexBuffers: seq[VkBuffer] | |
203 bufferOffsets: seq[VkDeviceSize] | |
1159 | 204 instanceCount: uint32 |
1161 | 205 case indexType: IndexType |
206 of None: | |
1160 | 207 vertexCount: uint32 |
1161 | 208 else: |
1160 | 209 indexBuffer: VkBuffer |
210 indexCount: uint32 | |
211 indexBufferOffset: VkDeviceSize | |
1162 | 212 Pipeline[TShader] = object |
1159 | 213 pipeline: VkPipeline |
214 layout: VkPipelineLayout | |
1173 | 215 descriptorSets: array[INFLIGHTFRAMES.int, VkDescriptorSet] |
1162 | 216 |
1161 | 217 converter toVkIndexType(indexType: IndexType): VkIndexType = |
218 case indexType: | |
219 of None: VK_INDEX_TYPE_NONE_KHR | |
220 of UInt8: VK_INDEX_TYPE_UINT8_EXT | |
221 of UInt16: VK_INDEX_TYPE_UINT16 | |
222 of UInt32: VK_INDEX_TYPE_UINT32 | |
1159 | 223 |
1172 | 224 proc CreateRenderPass*( |
225 device: VkDevice, | |
226 format: VkFormat, | |
227 ): VkRenderPass = | |
228 | |
229 var | |
230 attachments = @[VkAttachmentDescription( | |
231 format: format, | |
232 samples: VK_SAMPLE_COUNT_1_BIT, | |
233 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
234 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
235 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
236 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
237 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
238 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
239 )] | |
240 dependencies = @[VkSubpassDependency( | |
241 srcSubpass: VK_SUBPASS_EXTERNAL, | |
242 dstSubpass: 0, | |
243 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], | |
244 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
245 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
246 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
247 )] | |
248 outputs = @[ | |
249 VkAttachmentReference( | |
250 attachment: 0, | |
251 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
252 ) | |
253 ] | |
254 | |
255 var subpassesList = [ | |
256 VkSubpassDescription( | |
257 flags: VkSubpassDescriptionFlags(0), | |
258 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
259 inputAttachmentCount: 0, | |
260 pInputAttachments: nil, | |
261 colorAttachmentCount: uint32(outputs.len), | |
262 pColorAttachments: outputs.ToCPointer, | |
263 pResolveAttachments: nil, | |
264 pDepthStencilAttachment: nil, | |
265 preserveAttachmentCount: 0, | |
266 pPreserveAttachments: nil, | |
267 ) | |
268 ] | |
269 | |
270 var createInfo = VkRenderPassCreateInfo( | |
271 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
272 attachmentCount: uint32(attachments.len), | |
273 pAttachments: attachments.ToCPointer, | |
274 subpassCount: uint32(subpassesList.len), | |
275 pSubpasses: subpassesList.ToCPointer, | |
276 dependencyCount: uint32(dependencies.len), | |
277 pDependencies: dependencies.ToCPointer, | |
278 ) | |
279 checkVkResult device.vkCreateRenderPass(addr(createInfo), nil, addr(result)) | |
280 | |
1162 | 281 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = |
282 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | |
283 case stage | |
284 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | |
285 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | |
286 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" | |
287 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" | |
288 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" | |
289 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" | |
290 else: "" | |
1161 | 291 |
1162 | 292 when defined(nimcheck): # will not run if nimcheck is running |
293 return result | |
294 | |
295 let | |
296 stagename = stage2string(stage) | |
297 shaderHash = hash(shaderSource) | |
298 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" | |
299 | |
300 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
|
301 echo "shader of type ", stage |
1162 | 302 for i, line in enumerate(shaderSource.splitlines()): |
303 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
|
304 # 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
|
305 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator" |
1162 | 306 when defined(windows): |
307 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
|
308 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" |
1162 | 309 echo "run: ", command |
310 discard StaticExecChecked( | |
311 command = command, | |
312 input = shaderSource | |
313 ) | |
314 else: | |
315 echo &"shaderfile {shaderfile} is up-to-date" | |
316 | |
317 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up | |
318 let shaderbinary = staticRead shaderfile.replace("\\", "/") | |
319 else: | |
320 let shaderbinary = staticRead shaderfile | |
321 | |
322 var i = 0 | |
323 while i < shaderbinary.len: | |
324 result.add( | |
325 (uint32(shaderbinary[i + 0]) shl 0) or | |
326 (uint32(shaderbinary[i + 1]) shl 8) or | |
327 (uint32(shaderbinary[i + 2]) shl 16) or | |
328 (uint32(shaderbinary[i + 3]) shl 24) | |
329 ) | |
330 i += 4 | |
331 | |
332 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = | |
333 const GLSL_VERSION = "450" | |
334 var vsInput: seq[string] | |
335 var vsOutput: seq[string] | |
336 var fsInput: seq[string] | |
337 var fsOutput: seq[string] | |
338 var uniforms: seq[string] | |
339 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
|
340 var vsInputLocation = 0'u32 |
1162 | 341 var passLocation = 0 |
342 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
|
343 var descriptorBinding = 0 |
1162 | 344 |
345 for fieldname, value in fieldPairs(shader): | |
346 # 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
|
347 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
1162 | 348 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
|
349 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" |
1162 | 350 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): |
351 vsInputLocation += NLocationSlots(value) | |
352 # 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
|
353 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
|
354 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
|
355 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
|
356 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" |
1162 | 357 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
|
358 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
|
359 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" |
1162 | 360 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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 # 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
|
366 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
|
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 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
|
369 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
|
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 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
|
372 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
|
373 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
|
374 # 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
|
375 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
|
376 # 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
|
377 descriptorBinding.inc |
1162 | 378 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
|
379 {.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
|
380 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
|
381 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
|
382 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
|
383 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).} |
1162 | 384 |
385 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
386 vsInput & | |
387 uniforms & | |
388 samplers & | |
389 vsOutput & | |
390 @[shader.vertexCode]).join("\n") | |
391 | |
392 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
393 fsInput & | |
394 uniforms & | |
395 samplers & | |
396 fsOutput & | |
397 @[shader.fragmentCode]).join("\n") | |
398 | |
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
|
399 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
|
400 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
|
401 |
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 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
|
403 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
|
404 |
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
|
405 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
|
406 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
|
407 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
|
408 pCode: vertexBinary.ToCPointer, |
1162 | 409 ) |
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
|
410 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
|
411 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
|
412 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
|
413 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
|
414 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
|
415 ) |
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
|
416 checkVkResult device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader)) |
1162 | 417 |
418 | |
419 proc CreatePipeline*[TShader]( | |
1159 | 420 device: VkDevice, |
421 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
|
422 shader: ShaderObject[TShader], |
1159 | 423 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, |
424 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, | |
425 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT, | |
426 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, | |
1162 | 427 ): Pipeline[TShader] = |
1159 | 428 # assumptions/limitations: |
429 # - we are only using vertex and fragment shaders (2 stages) | |
430 # - we only support one subpass | |
1162 | 431 # = we only support one Uniform-Block |
1161 | 432 |
1164 | 433 # create pipeline |
1161 | 434 var layoutbindings: seq[VkDescriptorSetLayoutBinding] |
1173 | 435 ForDescriptorFields(default(TShader), descriptorType, descriptorCount, descriptorBindingNumber): |
1161 | 436 layoutbindings.add VkDescriptorSetLayoutBinding( |
437 binding: descriptorBindingNumber, | |
438 descriptorType: descriptorType, | |
439 descriptorCount: descriptorCount, | |
1162 | 440 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), |
1161 | 441 pImmutableSamplers: nil, |
442 ) | |
443 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( | |
444 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
445 bindingCount: uint32(layoutbindings.len), | |
446 pBindings: layoutbindings.ToCPointer | |
447 ) | |
448 var descriptorSetLayout: VkDescriptorSetLayout | |
1162 | 449 checkVkResult vkCreateDescriptorSetLayout(device, addr(layoutCreateInfo), nil, addr(descriptorSetLayout)) |
1161 | 450 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( |
451 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
452 setLayoutCount: 1, | |
453 pSetLayouts: addr(descriptorSetLayout), | |
454 # pushConstantRangeCount: uint32(pushConstants.len), | |
455 # pPushConstantRanges: pushConstants.ToCPointer, | |
456 ) | |
457 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil, addr(result.layout)) | |
1159 | 458 |
459 let stages = [ | |
460 VkPipelineShaderStageCreateInfo( | |
461 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
462 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
|
463 module: shader.vertexShader, |
1159 | 464 pName: "main", |
465 ), | |
466 VkPipelineShaderStageCreateInfo( | |
467 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
468 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
|
469 module: shader.fragmentShader, |
1159 | 470 pName: "main", |
471 ), | |
472 ] | |
1162 | 473 var |
474 bindings: seq[VkVertexInputBindingDescription] | |
475 attributes: seq[VkVertexInputAttributeDescription] | |
1159 | 476 var inputBindingNumber = 0'u32 |
1162 | 477 var location = 0'u32 |
478 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): | |
1159 | 479 bindings.add VkVertexInputBindingDescription( |
480 binding: inputBindingNumber, | |
481 stride: sizeof(value).uint32, | |
482 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX, | |
483 ) | |
484 # allows to submit larger data structures like Mat44, for most other types will be 1 | |
485 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value) | |
486 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value): | |
487 attributes.add VkVertexInputAttributeDescription( | |
488 binding: inputBindingNumber, | |
1162 | 489 location: location, |
1159 | 490 format: VkType(value), |
491 offset: i * perDescriptorSize, | |
492 ) | |
1162 | 493 location += NLocationSlots(value) |
1159 | 494 inc inputBindingNumber |
495 | |
496 let | |
497 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
498 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
499 vertexBindingDescriptionCount: uint32(bindings.len), | |
500 pVertexBindingDescriptions: bindings.ToCPointer, | |
501 vertexAttributeDescriptionCount: uint32(attributes.len), | |
502 pVertexAttributeDescriptions: attributes.ToCPointer, | |
503 ) | |
504 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
505 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
506 topology: topology, | |
507 primitiveRestartEnable: false, | |
508 ) | |
509 viewportState = VkPipelineViewportStateCreateInfo( | |
510 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | |
511 viewportCount: 1, | |
512 scissorCount: 1, | |
513 ) | |
514 rasterizer = VkPipelineRasterizationStateCreateInfo( | |
515 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | |
516 depthClampEnable: VK_FALSE, | |
517 rasterizerDiscardEnable: VK_FALSE, | |
518 polygonMode: polygonMode, | |
519 lineWidth: 1.0, | |
520 cullMode: toBits [cullMode], | |
521 frontFace: frontFace, | |
522 depthBiasEnable: VK_FALSE, | |
523 depthBiasConstantFactor: 0.0, | |
524 depthBiasClamp: 0.0, | |
525 depthBiasSlopeFactor: 0.0, | |
526 ) | |
527 multisampling = VkPipelineMultisampleStateCreateInfo( | |
528 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | |
529 sampleShadingEnable: VK_FALSE, | |
530 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, | |
531 minSampleShading: 1.0, | |
532 pSampleMask: nil, | |
533 alphaToCoverageEnable: VK_FALSE, | |
534 alphaToOneEnable: VK_FALSE, | |
535 ) | |
536 colorBlendAttachment = VkPipelineColorBlendAttachmentState( | |
537 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT], | |
538 blendEnable: VK_TRUE, | |
539 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, | |
540 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | |
541 colorBlendOp: VK_BLEND_OP_ADD, | |
542 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, | |
543 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, | |
544 alphaBlendOp: VK_BLEND_OP_ADD, | |
545 ) | |
546 colorBlending = VkPipelineColorBlendStateCreateInfo( | |
547 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | |
548 logicOpEnable: false, | |
549 attachmentCount: 1, | |
550 pAttachments: addr(colorBlendAttachment), | |
551 ) | |
552 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
553 dynamicState = VkPipelineDynamicStateCreateInfo( | |
554 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
555 dynamicStateCount: dynamicStates.len.uint32, | |
556 pDynamicStates: dynamicStates.ToCPointer, | |
557 ) | |
558 let createInfo = VkGraphicsPipelineCreateInfo( | |
559 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | |
560 stageCount: 2, | |
1162 | 561 pStages: stages.ToCPointer, |
1159 | 562 pVertexInputState: addr(vertexInputInfo), |
563 pInputAssemblyState: addr(inputAssembly), | |
564 pViewportState: addr(viewportState), | |
565 pRasterizationState: addr(rasterizer), | |
566 pMultisampleState: addr(multisampling), | |
567 pDepthStencilState: nil, | |
568 pColorBlendState: addr(colorBlending), | |
569 pDynamicState: addr(dynamicState), | |
570 layout: result.layout, | |
571 renderPass: renderPass, | |
572 subpass: 0, | |
573 basePipelineHandle: VkPipeline(0), | |
574 basePipelineIndex: -1, | |
575 ) | |
576 checkVkResult vkCreateGraphicsPipelines( | |
577 device, | |
578 VkPipelineCache(0), | |
579 1, | |
580 addr(createInfo), | |
581 nil, | |
582 addr(result.pipeline) | |
583 ) | |
584 | |
1164 | 585 # create descriptors, one per frame-in-flight |
586 let nSamplers = 0'u32 | |
587 let nUniformBuffers = 0'u32 | |
588 | |
589 if nSamplers + nUniformBuffers > 0: | |
590 var poolSizes: seq[VkDescriptorPoolSize] | |
591 if nUniformBuffers > 0: | |
1165 | 592 poolSizes.add VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: nSamplers * INFLIGHTFRAMES) |
1164 | 593 if nSamplers > 0: |
1165 | 594 poolSizes.add VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: nUniformBuffers * INFLIGHTFRAMES) |
1164 | 595 var poolInfo = VkDescriptorPoolCreateInfo( |
596 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
597 poolSizeCount: uint32(poolSizes.len), | |
598 pPoolSizes: poolSizes.ToCPointer, | |
1165 | 599 maxSets: (nUniformBuffers + nSamplers) * INFLIGHTFRAMES * 2, # good formula? no idea... |
1164 | 600 ) |
601 var pool: VkDescriptorPool | |
602 checkVkResult vkCreateDescriptorPool(device, addr(poolInfo), nil, addr(pool)) | |
603 | |
604 var layouts = newSeqWith(result.descriptorSets.len, descriptorSetLayout) | |
605 var allocInfo = VkDescriptorSetAllocateInfo( | |
606 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
607 descriptorPool: pool, | |
608 descriptorSetCount: uint32(layouts.len), | |
609 pSetLayouts: layouts.ToCPointer, | |
610 ) | |
611 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), result.descriptorSets.ToCPointer) | |
612 | |
1173 | 613 proc WriteDescriptors[TShader](device: VkDevice, pipeline: Pipeline[TShader]) = |
1165 | 614 var descriptorSetWrites: seq[VkWriteDescriptorSet] |
1173 | 615 ForDescriptorFields(default(TShader), descriptorType, descriptorCount, descriptorBindingNumber): |
616 for frameInFlight in 0 ..< pipeline.descriptorSets.len: | |
617 if descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: | |
618 # TODO | |
619 let bufferInfo = VkDescriptorBufferInfo( | |
620 buffer: VkBuffer(0), | |
621 offset: 0, | |
622 range: 1, | |
623 ) | |
624 descriptorSetWrites.add VkWriteDescriptorSet( | |
625 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
626 dstSet: pipeline.descriptorSets[frameInFlight], | |
627 dstBinding: descriptorBindingNumber, | |
628 dstArrayElement: uint32(0), | |
629 descriptorType: descriptorType, | |
630 descriptorCount: descriptorCount, | |
631 pImageInfo: nil, | |
632 pBufferInfo: addr(bufferInfo), | |
633 ) | |
634 elif descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: | |
635 # TODO | |
636 let imageInfo = VkDescriptorImageInfo( | |
637 sampler: VkSampler(0), | |
638 imageView: VkImageView(0), | |
639 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, | |
640 ) | |
641 descriptorSetWrites.add VkWriteDescriptorSet( | |
642 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
643 dstSet: pipeline.descriptorSets[frameInFlight], | |
644 dstBinding: descriptorBindingNumber, | |
645 dstArrayElement: uint32(0), | |
646 descriptorType: descriptorType, | |
647 descriptorCount: descriptorCount, | |
648 pImageInfo: addr(imageInfo), | |
649 pBufferInfo: nil, | |
650 ) | |
651 vkUpdateDescriptorSets(device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) | |
1165 | 652 |
1161 | 653 proc CreateRenderable[TMesh, TInstance]( |
654 mesh: TMesh, | |
655 instance: TInstance, | |
656 buffers: RenderBuffers, | |
657 ): Renderable[TMesh, TInstance] = | |
658 result.indexType = None | |
1159 | 659 |
1172 | 660 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
661 let a = pipeline.descriptorSets | |
1159 | 662 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) |
1172 | 663 if a[currentFrameInFlight] != VkDescriptorSet(0): |
1164 | 664 commandBuffer.vkCmdBindDescriptorSets( |
665 VK_PIPELINE_BIND_POINT_GRAPHICS, | |
666 pipeline.layout, | |
667 0, | |
668 1, | |
669 addr pipeline.descriptorSets[currentFrameInFlight], | |
670 0, | |
671 nil, | |
672 ) | |
1161 | 673 |
1162 | 674 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals: typedesc) = |
675 # assert seq-fields of TMesh|TInstance == seq-fields of TShader | |
1161 | 676 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors |
1162 | 677 for inputName, inputValue in default(TShader).fieldPairs: |
1161 | 678 var foundField = false |
679 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
|
680 assert typeof(inputValue) is SupportedGPUType |
1161 | 681 for meshName, meshValue in default(TMesh).fieldPairs: |
682 when meshName == inputName: | |
1162 | 683 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
|
684 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 | 685 foundField = true |
1162 | 686 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "'" |
1161 | 687 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
|
688 assert typeof(inputValue) is SupportedGPUType |
1161 | 689 for instanceName, instanceValue in default(TInstance).fieldPairs: |
690 when instanceName == inputName: | |
1162 | 691 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
|
692 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 | 693 foundField = true |
1162 | 694 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
|
695 elif typeof(inputValue) is Texture or typeof(inputValue) is object: |
1161 | 696 for meshName, meshValue in default(TMesh).fieldPairs: |
697 when meshName == inputName: | |
1162 | 698 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
|
699 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 | 700 foundField = true |
701 for globalName, globalValue in default(TGlobals).fieldPairs: | |
702 when globalName == inputName: | |
1162 | 703 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
704 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 | 705 foundField = true |
1162 | 706 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
|
707 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
|
708 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
|
709 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
|
710 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
|
711 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
|
712 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
|
713 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
|
714 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
|
715 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
|
716 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
|
717 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
|
718 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
|
719 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
1161 | 720 |
721 | |
1162 | 722 proc Render[TShader, TMesh, TInstance, TGlobals]( |
723 pipeline: Pipeline[TShader], | |
1161 | 724 renderable: Renderable[TMesh, TInstance], |
725 globals: TGlobals, | |
726 commandBuffer: VkCommandBuffer, | |
727 ) = | |
1164 | 728 static: AssertCompatible(TShader, TMesh, TInstance, TGlobals) |
729 if renderable.vertexBuffers.len > 0: | |
730 commandBuffer.vkCmdBindVertexBuffers( | |
731 firstBinding = 0'u32, | |
732 bindingCount = uint32(renderable.vertexBuffers.len), | |
733 pBuffers = renderable.vertexBuffers.ToCPointer(), | |
734 pOffsets = renderable.bufferOffsets.ToCPointer() | |
735 ) | |
1161 | 736 if renderable.indexType != None: |
1159 | 737 commandBuffer.vkCmdBindIndexBuffer( |
738 renderable.indexBuffer, | |
739 renderable.indexBufferOffset, | |
1161 | 740 renderable.indexType, |
1159 | 741 ) |
742 commandBuffer.vkCmdDrawIndexed( | |
1161 | 743 indexCount = renderable.indexCount, |
744 instanceCount = renderable.instanceCount, | |
1159 | 745 firstIndex = 0, |
746 vertexOffset = 0, | |
747 firstInstance = 0 | |
748 ) | |
749 else: | |
750 commandBuffer.vkCmdDraw( | |
1161 | 751 vertexCount = renderable.vertexCount, |
752 instanceCount = renderable.instanceCount, | |
1159 | 753 firstVertex = 0, |
754 firstInstance = 0 | |
755 ) | |
1161 | 756 |
757 when isMainModule: | |
1162 | 758 import semicongine/platform/window |
759 import semicongine/vulkan/instance | |
760 import semicongine/vulkan/device | |
761 import semicongine/vulkan/physicaldevice | |
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
|
762 import std/options |
1162 | 763 |
1161 | 764 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
|
765 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
|
766 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
|
767 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
|
768 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
|
769 brightness: float32 |
1161 | 770 MeshA = object |
771 position: seq[Vec3f] | |
772 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
|
773 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
|
774 materialTextures: array[3, Texture] |
1161 | 775 InstanceA = object |
776 transform: seq[Mat4] | |
777 position: seq[Vec3f] | |
778 Globals = object | |
1162 | 779 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
|
780 settings: ShaderSettings |
1161 | 781 |
1162 | 782 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
|
783 # vertex input |
1161 | 784 position {.VertexAttribute.}: Vec3f |
785 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
|
786 # intermediate |
1162 | 787 test {.Pass.}: float32 |
788 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
|
789 # output |
1162 | 790 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
|
791 # 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
|
792 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
|
793 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
|
794 # 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
|
795 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
|
796 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
|
797 # code |
1162 | 798 vertexCode: string = "void main() {}" |
799 fragmentCode: string = "void main() {}" | |
1161 | 800 |
1162 | 801 let w = CreateWindow("test2") |
802 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") | |
803 let i = w.CreateInstance( | |
804 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), | |
805 instanceExtensions = @[], | |
806 layers = @["VK_LAYER_KHRONOS_validation"], | |
807 ) | |
808 | |
809 | |
810 let selectedPhysicalDevice = i.GetPhysicalDevices().FilterBestGraphics() | |
1173 | 811 let dev = i.CreateDevice( |
1162 | 812 selectedPhysicalDevice, |
813 enabledExtensions = @[], | |
814 selectedPhysicalDevice.FilterForGraphicsPresentationQueues() | |
815 ) | |
1173 | 816 let frameWidth = 100'u32 |
817 let frameHeight = 100'u32 | |
1162 | 818 |
1173 | 819 var myRenderable: Renderable[MeshA, InstanceA] |
820 var myGlobals: Globals | |
821 | |
822 # setup for rendering (TODO: swapchain & framebuffers) | |
823 | |
824 # renderpass | |
825 let renderpass = dev.vk.CreateRenderPass(dev.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format) | |
826 | |
827 # shaders | |
828 const shader = ShaderA() | |
829 let shaderObject = dev.vk.CompileShader(shader) | |
830 var pipeline1 = CreatePipeline(dev.vk, renderPass = renderpass, shaderObject) | |
831 | |
832 # TODO: probably here: allocate renderables, uniform buffers & textures | |
1161 | 833 |
1173 | 834 # descriptors |
835 WriteDescriptors(dev.vk, pipeline1) | |
836 | |
837 # command buffer | |
838 var | |
839 commandBufferPool: VkCommandPool | |
840 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] | |
841 createInfo = VkCommandPoolCreateInfo( | |
842 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
843 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
844 queueFamilyIndex: dev.FirstGraphicsQueue().get().family.index, | |
845 ) | |
846 checkVkResult vkCreateCommandPool(dev.vk, addr createInfo, nil, addr commandBufferPool) | |
847 var allocInfo = VkCommandBufferAllocateInfo( | |
848 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
849 commandPool: commandBufferPool, | |
850 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
851 commandBufferCount: INFLIGHTFRAMES, | |
852 ) | |
853 checkVkResult vkAllocateCommandBuffers(dev.vk, addr allocInfo, cmdBuffers.ToCPointer) | |
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
|
854 |
1173 | 855 # start command buffer |
856 block: | |
857 let | |
858 currentFramebuffer = VkFramebuffer(0) # TODO | |
859 currentFrameInFlight = 1 | |
860 cmd = cmdBuffers[currentFrameInFlight] | |
861 beginInfo = VkCommandBufferBeginInfo( | |
862 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
863 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | |
864 ) | |
865 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) | |
866 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) | |
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
|
867 |
1173 | 868 # start renderpass |
869 block: | |
870 var | |
871 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
872 renderPassInfo = VkRenderPassBeginInfo( | |
873 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
874 renderPass: renderpass, | |
875 framebuffer: currentFramebuffer, | |
876 renderArea: VkRect2D( | |
877 offset: VkOffset2D(x: 0, y: 0), | |
878 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
879 ), | |
880 clearValueCount: uint32(clearColors.len), | |
881 pClearValues: clearColors.ToCPointer(), | |
882 ) | |
883 viewport = VkViewport( | |
884 x: 0.0, | |
885 y: 0.0, | |
886 width: frameWidth.float32, | |
887 height: frameHeight.float32, | |
888 minDepth: 0.0, | |
889 maxDepth: 1.0, | |
890 ) | |
891 scissor = VkRect2D( | |
892 offset: VkOffset2D(x: 0, y: 0), | |
893 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
894 ) | |
895 vkCmdBeginRenderPass(cmd, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE) | |
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
|
896 |
1173 | 897 # setup viewport |
898 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport)) | |
899 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
900 | |
901 # bind pipeline, will be loop | |
902 block: | |
903 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) | |
904 | |
905 # render object, will be loop | |
906 block: | |
907 Render(pipeline1, myRenderable, myGlobals, cmd) | |
908 | |
909 vkCmdEndRenderPass(cmd) | |
910 checkVkResult cmd.vkEndCommandBuffer() |