Mercurial > games > semicongine
annotate static_utils.nim @ 1177:4ef959278451 compiletime-tests
sync from bedroom to office
author | sam <sam@basx.dev> |
---|---|
date | Sun, 30 Jun 2024 06:40:33 +0700 |
parents | 511c9f7cd1da |
children | ec4ff70299f2 |
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.} | |
1177 | 22 template VertexIndices*{.pragma.} |
1159 | 23 |
1165 | 24 const INFLIGHTFRAMES = 2'u32 |
1177 | 25 const MEMORY_ALIGNMENT = 65536'u64 # Align buffers inside memory along this alignment |
26 const BUFFERALIGNMENT = 64'u64 # align offsets inside buffers along this alignment | |
1176 | 27 |
1159 | 28 type |
29 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
|
30 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
|
31 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
|
32 fragmentShader: VkShaderModule |
1159 | 33 |
34 func VkType[T: SupportedGPUType](value: T): VkFormat = | |
35 when T is float32: VK_FORMAT_R32_SFLOAT | |
36 elif T is float64: VK_FORMAT_R64_SFLOAT | |
37 elif T is int8: VK_FORMAT_R8_SINT | |
38 elif T is int16: VK_FORMAT_R16_SINT | |
39 elif T is int32: VK_FORMAT_R32_SINT | |
40 elif T is int64: VK_FORMAT_R64_SINT | |
41 elif T is uint8: VK_FORMAT_R8_UINT | |
42 elif T is uint16: VK_FORMAT_R16_UINT | |
43 elif T is uint32: VK_FORMAT_R32_UINT | |
44 elif T is uint64: VK_FORMAT_R64_UINT | |
45 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT | |
46 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT | |
47 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT | |
48 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT | |
49 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT | |
50 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT | |
51 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT | |
52 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT | |
53 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT | |
54 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT | |
55 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT | |
56 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT | |
57 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT | |
58 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT | |
59 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
60 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
61 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
62 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
1162 | 63 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT |
64 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT | |
65 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
66 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
67 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT | |
68 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT | |
69 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
70 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
71 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
72 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
73 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
74 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
75 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
76 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
77 else: {.error: "Unsupported data type on GPU".} | |
78 | |
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
|
79 func GlslType[T: SupportedGPUType|Texture](value: T): string = |
1162 | 80 when T is float32: "float" |
81 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
|
82 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
|
83 elif T is uint8 or T is uint16 or T is uint32 or T is uint64: "uint" |
1162 | 84 elif T is TVec2[int32]: "ivec2" |
85 elif T is TVec2[int64]: "ivec2" | |
86 elif T is TVec3[int32]: "ivec3" | |
87 elif T is TVec3[int64]: "ivec3" | |
88 elif T is TVec4[int32]: "ivec4" | |
89 elif T is TVec4[int64]: "ivec4" | |
90 elif T is TVec2[uint32]: "uvec2" | |
91 elif T is TVec2[uint64]: "uvec2" | |
92 elif T is TVec3[uint32]: "uvec3" | |
93 elif T is TVec3[uint64]: "uvec3" | |
94 elif T is TVec4[uint32]: "uvec4" | |
95 elif T is TVec4[uint64]: "uvec4" | |
96 elif T is TVec2[float32]: "vec2" | |
97 elif T is TVec2[float64]: "dvec2" | |
98 elif T is TVec3[float32]: "vec3" | |
99 elif T is TVec3[float64]: "dvec3" | |
100 elif T is TVec4[float32]: "vec4" | |
101 elif T is TVec4[float64]: "dvec4" | |
102 elif T is TMat2[float32]: "mat2" | |
103 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
|
104 elif T is TMat23[float32]: "mat23" |
1162 | 105 elif T is TMat23[float64]: "dmat23" |
106 elif T is TMat32[float32]: "mat32" | |
107 elif T is TMat32[float64]: "dmat32" | |
108 elif T is TMat3[float32]: "mat3" | |
109 elif T is TMat3[float64]: "dmat3" | |
110 elif T is TMat34[float32]: "mat34" | |
111 elif T is TMat34[float64]: "dmat34" | |
112 elif T is TMat43[float32]: "mat43" | |
113 elif T is TMat43[float64]: "dmat43" | |
114 elif T is TMat4[float32]: "mat4" | |
115 elif T is TMat4[float64]: "dmat4" | |
116 elif T is Texture: "sampler2D" | |
1159 | 117 else: {.error: "Unsupported data type on GPU".} |
118 | |
1161 | 119 template ForVertexDataFields*(inputData: typed, fieldname, valuename, isinstancename, body: untyped): untyped = |
1159 | 120 for theFieldname, value in fieldPairs(inputData): |
1161 | 121 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
1159 | 122 when not typeof(value) is seq: |
123 {.error: "field '" & theFieldname & "' needs to be a seq".} | |
124 when not typeof(value) is SupportedGPUType: | |
125 {.error: "field '" & theFieldname & "' is not a supported GPU type".} | |
126 block: | |
127 let `fieldname` {.inject.} = theFieldname | |
1162 | 128 let `valuename` {.inject.} = value |
129 let `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute) | |
1159 | 130 body |
131 | |
1176 | 132 template ForDescriptorFields*(inputData: typed, fieldname, typename, countname, bindingNumber, body: untyped): untyped = |
1173 | 133 var `bindingNumber` {.inject.} = 1'u32 |
1161 | 134 for theFieldname, value in fieldPairs(inputData): |
1176 | 135 let `fieldname` {.inject.} = theFieldname |
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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 body |
1173 | 141 `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
|
142 elif typeof(value) is object: |
1161 | 143 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
|
144 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
|
145 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
|
146 body |
1173 | 147 `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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 body |
1173 | 154 `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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 body |
1173 | 160 `bindingNumber`.inc |
1161 | 161 |
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
|
162 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 = |
1159 | 163 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]: |
164 2 | |
165 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]: | |
166 3 | |
167 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]: | |
168 4 | |
169 else: | |
170 1 | |
171 | |
1162 | 172 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 = |
1159 | 173 #[ |
174 single location: | |
1162 | 175 - any scalar |
176 - any 16-bit vector | |
177 - any 32-bit vector | |
178 - any 64-bit vector that has max. 2 components | |
1159 | 179 16-bit scalar and vector types, and |
180 32-bit scalar and vector types, and | |
181 64-bit scalar and 2-component vector types. | |
182 two locations | |
183 64-bit three- and four-component vectors | |
184 ]# | |
1162 | 185 when T is TVec3[int64] or |
186 T is TVec4[int64] or | |
187 T is TVec3[uint64] or | |
188 T is TVec4[uint64] or | |
189 T is TVec3[float64] or | |
190 T is TVec4[float64] or | |
191 T is TMat23[float64] or | |
192 T is TMat3[float64] or | |
193 T is TMat34[float64] or | |
194 T is TMat43[float64] or | |
195 T is TMat4[float64]: | |
1159 | 196 return 2 |
197 else: | |
198 return 1 | |
199 | |
200 type | |
1161 | 201 IndexType = enum |
202 None, UInt8, UInt16, UInt32 | |
1176 | 203 |
204 IndirectGPUMemory = object | |
205 vk: VkDeviceMemory | |
206 size: uint64 | |
1177 | 207 needsTransfer: bool # usually true |
1176 | 208 DirectGPUMemory = object |
209 vk: VkDeviceMemory | |
210 size: uint64 | |
211 data: pointer | |
1177 | 212 needsFlush: bool # usually true |
1176 | 213 GPUMemory = IndirectGPUMemory | DirectGPUMemory |
214 | |
215 Buffer[TMemory: GPUMemory] = object | |
216 vk: VkBuffer | |
217 offset: uint64 | |
218 size: uint64 | |
219 | |
220 GPUArray[T: SupportedGPUType, TMemory: GPUMemory] = object | |
221 data: seq[T] | |
222 buffer: Buffer[TMemory] | |
223 offset: uint64 | |
224 GPUValue[T: object|array, TMemory: GPUMemory] = object | |
225 data: T | |
226 buffer: Buffer[TMemory] | |
227 offset: uint64 | |
228 | |
1161 | 229 Renderable[TMesh, TInstance] = object |
230 vertexBuffers: seq[VkBuffer] | |
231 bufferOffsets: seq[VkDeviceSize] | |
1159 | 232 instanceCount: uint32 |
1161 | 233 case indexType: IndexType |
234 of None: | |
1160 | 235 vertexCount: uint32 |
1161 | 236 else: |
1160 | 237 indexBuffer: VkBuffer |
238 indexCount: uint32 | |
239 indexBufferOffset: VkDeviceSize | |
1176 | 240 |
1162 | 241 Pipeline[TShader] = object |
1159 | 242 pipeline: VkPipeline |
243 layout: VkPipelineLayout | |
1176 | 244 descriptorSetLayout: VkDescriptorSetLayout |
245 RenderData = object | |
246 descriptorPool: VkDescriptorPool | |
1177 | 247 # tuple is memory and offset to next free allocation in that memory |
248 indirectMemory: seq[tuple[memory: IndirectGPUMemory, nextFree: uint64]] | |
249 directMemory: seq[tuple[memory: DirectGPUMemory, nextFree: uint64]] | |
1176 | 250 indirectBuffers: seq[Buffer[IndirectGPUMemory]] |
251 directBuffers: seq[Buffer[DirectGPUMemory]] | |
1162 | 252 |
1177 | 253 template IsDirectMemory(gpuArray: GPUArray): untyped = |
254 get(genericParams(typeof(gpuArray)), 1) is DirectGPUMemory | |
255 template IsDirectMemory(gpuValue: GPUValue): untyped = | |
256 get(genericParams(typeof(gpuValue)), 1) is DirectGPUMemory | |
257 | |
1161 | 258 converter toVkIndexType(indexType: IndexType): VkIndexType = |
259 case indexType: | |
260 of None: VK_INDEX_TYPE_NONE_KHR | |
261 of UInt8: VK_INDEX_TYPE_UINT8_EXT | |
262 of UInt16: VK_INDEX_TYPE_UINT16 | |
263 of UInt32: VK_INDEX_TYPE_UINT32 | |
1159 | 264 |
1172 | 265 proc CreateRenderPass*( |
266 device: VkDevice, | |
267 format: VkFormat, | |
268 ): VkRenderPass = | |
269 | |
270 var | |
271 attachments = @[VkAttachmentDescription( | |
272 format: format, | |
273 samples: VK_SAMPLE_COUNT_1_BIT, | |
274 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
275 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
276 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
277 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
278 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
279 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
280 )] | |
281 dependencies = @[VkSubpassDependency( | |
282 srcSubpass: VK_SUBPASS_EXTERNAL, | |
283 dstSubpass: 0, | |
284 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], | |
285 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
286 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
287 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
288 )] | |
289 outputs = @[ | |
290 VkAttachmentReference( | |
291 attachment: 0, | |
292 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
293 ) | |
294 ] | |
295 | |
296 var subpassesList = [ | |
297 VkSubpassDescription( | |
298 flags: VkSubpassDescriptionFlags(0), | |
299 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
300 inputAttachmentCount: 0, | |
301 pInputAttachments: nil, | |
302 colorAttachmentCount: uint32(outputs.len), | |
303 pColorAttachments: outputs.ToCPointer, | |
304 pResolveAttachments: nil, | |
305 pDepthStencilAttachment: nil, | |
306 preserveAttachmentCount: 0, | |
307 pPreserveAttachments: nil, | |
308 ) | |
309 ] | |
310 | |
311 var createInfo = VkRenderPassCreateInfo( | |
312 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
313 attachmentCount: uint32(attachments.len), | |
314 pAttachments: attachments.ToCPointer, | |
315 subpassCount: uint32(subpassesList.len), | |
316 pSubpasses: subpassesList.ToCPointer, | |
317 dependencyCount: uint32(dependencies.len), | |
318 pDependencies: dependencies.ToCPointer, | |
319 ) | |
320 checkVkResult device.vkCreateRenderPass(addr(createInfo), nil, addr(result)) | |
321 | |
1162 | 322 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = |
323 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | |
324 case stage | |
325 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | |
326 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | |
327 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" | |
328 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" | |
329 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" | |
330 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" | |
331 else: "" | |
1161 | 332 |
1162 | 333 when defined(nimcheck): # will not run if nimcheck is running |
334 return result | |
335 | |
336 let | |
337 stagename = stage2string(stage) | |
338 shaderHash = hash(shaderSource) | |
339 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" | |
340 | |
341 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
|
342 echo "shader of type ", stage |
1162 | 343 for i, line in enumerate(shaderSource.splitlines()): |
344 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
|
345 # 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
|
346 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator" |
1162 | 347 when defined(windows): |
348 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
|
349 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" |
1162 | 350 echo "run: ", command |
351 discard StaticExecChecked( | |
352 command = command, | |
353 input = shaderSource | |
354 ) | |
355 else: | |
356 echo &"shaderfile {shaderfile} is up-to-date" | |
357 | |
358 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up | |
359 let shaderbinary = staticRead shaderfile.replace("\\", "/") | |
360 else: | |
361 let shaderbinary = staticRead shaderfile | |
362 | |
363 var i = 0 | |
364 while i < shaderbinary.len: | |
365 result.add( | |
366 (uint32(shaderbinary[i + 0]) shl 0) or | |
367 (uint32(shaderbinary[i + 1]) shl 8) or | |
368 (uint32(shaderbinary[i + 2]) shl 16) or | |
369 (uint32(shaderbinary[i + 3]) shl 24) | |
370 ) | |
371 i += 4 | |
372 | |
373 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = | |
374 const GLSL_VERSION = "450" | |
375 var vsInput: seq[string] | |
376 var vsOutput: seq[string] | |
377 var fsInput: seq[string] | |
378 var fsOutput: seq[string] | |
379 var uniforms: seq[string] | |
380 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
|
381 var vsInputLocation = 0'u32 |
1162 | 382 var passLocation = 0 |
383 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
|
384 var descriptorBinding = 0 |
1162 | 385 |
386 for fieldname, value in fieldPairs(shader): | |
387 # 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
|
388 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
1162 | 389 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
|
390 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" |
1162 | 391 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): |
392 vsInputLocation += NLocationSlots(value) | |
393 # 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
|
394 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
|
395 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
|
396 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
|
397 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" |
1162 | 398 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
|
399 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
|
400 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" |
1162 | 401 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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 # 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
|
407 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
|
408 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
|
409 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
|
410 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
|
411 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
|
412 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
|
413 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
|
414 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
|
415 # 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
|
416 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
|
417 # 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
|
418 descriptorBinding.inc |
1162 | 419 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
|
420 {.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
|
421 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
|
422 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
|
423 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
|
424 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).} |
1162 | 425 |
426 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
427 vsInput & | |
428 uniforms & | |
429 samplers & | |
430 vsOutput & | |
431 @[shader.vertexCode]).join("\n") | |
432 | |
433 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
434 fsInput & | |
435 uniforms & | |
436 samplers & | |
437 fsOutput & | |
438 @[shader.fragmentCode]).join("\n") | |
439 | |
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
|
440 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
|
441 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
|
442 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
443 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
|
444 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
|
445 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
446 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
|
447 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
|
448 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
|
449 pCode: vertexBinary.ToCPointer, |
1162 | 450 ) |
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
|
451 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
|
452 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
|
453 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
|
454 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
|
455 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
|
456 ) |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
457 checkVkResult device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader)) |
1162 | 458 |
459 | |
460 proc CreatePipeline*[TShader]( | |
1159 | 461 device: VkDevice, |
462 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
|
463 shader: ShaderObject[TShader], |
1159 | 464 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, |
465 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, | |
466 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT, | |
467 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, | |
1176 | 468 descriptorPoolLimit = 1024 |
1162 | 469 ): Pipeline[TShader] = |
1164 | 470 # create pipeline |
1161 | 471 var layoutbindings: seq[VkDescriptorSetLayoutBinding] |
1176 | 472 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber): |
1161 | 473 layoutbindings.add VkDescriptorSetLayoutBinding( |
474 binding: descriptorBindingNumber, | |
475 descriptorType: descriptorType, | |
476 descriptorCount: descriptorCount, | |
1162 | 477 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), |
1161 | 478 pImmutableSamplers: nil, |
479 ) | |
480 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( | |
481 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
482 bindingCount: uint32(layoutbindings.len), | |
483 pBindings: layoutbindings.ToCPointer | |
484 ) | |
1176 | 485 checkVkResult vkCreateDescriptorSetLayout(device, addr(layoutCreateInfo), nil, addr(result.descriptorSetLayout)) |
1161 | 486 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( |
487 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
488 setLayoutCount: 1, | |
1176 | 489 pSetLayouts: addr(result.descriptorSetLayout), |
1161 | 490 # pushConstantRangeCount: uint32(pushConstants.len), |
491 # pPushConstantRanges: pushConstants.ToCPointer, | |
492 ) | |
493 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil, addr(result.layout)) | |
1159 | 494 |
495 let stages = [ | |
496 VkPipelineShaderStageCreateInfo( | |
497 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
498 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
|
499 module: shader.vertexShader, |
1159 | 500 pName: "main", |
501 ), | |
502 VkPipelineShaderStageCreateInfo( | |
503 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
504 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
|
505 module: shader.fragmentShader, |
1159 | 506 pName: "main", |
507 ), | |
508 ] | |
1162 | 509 var |
510 bindings: seq[VkVertexInputBindingDescription] | |
511 attributes: seq[VkVertexInputAttributeDescription] | |
1159 | 512 var inputBindingNumber = 0'u32 |
1162 | 513 var location = 0'u32 |
514 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): | |
1159 | 515 bindings.add VkVertexInputBindingDescription( |
516 binding: inputBindingNumber, | |
517 stride: sizeof(value).uint32, | |
518 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX, | |
519 ) | |
520 # allows to submit larger data structures like Mat44, for most other types will be 1 | |
521 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value) | |
522 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value): | |
523 attributes.add VkVertexInputAttributeDescription( | |
524 binding: inputBindingNumber, | |
1162 | 525 location: location, |
1159 | 526 format: VkType(value), |
527 offset: i * perDescriptorSize, | |
528 ) | |
1162 | 529 location += NLocationSlots(value) |
1159 | 530 inc inputBindingNumber |
531 | |
532 let | |
533 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
534 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
535 vertexBindingDescriptionCount: uint32(bindings.len), | |
536 pVertexBindingDescriptions: bindings.ToCPointer, | |
537 vertexAttributeDescriptionCount: uint32(attributes.len), | |
538 pVertexAttributeDescriptions: attributes.ToCPointer, | |
539 ) | |
540 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
541 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
542 topology: topology, | |
543 primitiveRestartEnable: false, | |
544 ) | |
545 viewportState = VkPipelineViewportStateCreateInfo( | |
546 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | |
547 viewportCount: 1, | |
548 scissorCount: 1, | |
549 ) | |
550 rasterizer = VkPipelineRasterizationStateCreateInfo( | |
551 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | |
552 depthClampEnable: VK_FALSE, | |
553 rasterizerDiscardEnable: VK_FALSE, | |
554 polygonMode: polygonMode, | |
555 lineWidth: 1.0, | |
556 cullMode: toBits [cullMode], | |
557 frontFace: frontFace, | |
558 depthBiasEnable: VK_FALSE, | |
559 depthBiasConstantFactor: 0.0, | |
560 depthBiasClamp: 0.0, | |
561 depthBiasSlopeFactor: 0.0, | |
562 ) | |
563 multisampling = VkPipelineMultisampleStateCreateInfo( | |
564 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | |
565 sampleShadingEnable: VK_FALSE, | |
566 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, | |
567 minSampleShading: 1.0, | |
568 pSampleMask: nil, | |
569 alphaToCoverageEnable: VK_FALSE, | |
570 alphaToOneEnable: VK_FALSE, | |
571 ) | |
572 colorBlendAttachment = VkPipelineColorBlendAttachmentState( | |
573 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT], | |
574 blendEnable: VK_TRUE, | |
575 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, | |
576 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | |
577 colorBlendOp: VK_BLEND_OP_ADD, | |
578 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, | |
579 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, | |
580 alphaBlendOp: VK_BLEND_OP_ADD, | |
581 ) | |
582 colorBlending = VkPipelineColorBlendStateCreateInfo( | |
583 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | |
584 logicOpEnable: false, | |
585 attachmentCount: 1, | |
586 pAttachments: addr(colorBlendAttachment), | |
587 ) | |
588 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
589 dynamicState = VkPipelineDynamicStateCreateInfo( | |
590 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
591 dynamicStateCount: dynamicStates.len.uint32, | |
592 pDynamicStates: dynamicStates.ToCPointer, | |
593 ) | |
594 let createInfo = VkGraphicsPipelineCreateInfo( | |
595 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | |
596 stageCount: 2, | |
1162 | 597 pStages: stages.ToCPointer, |
1159 | 598 pVertexInputState: addr(vertexInputInfo), |
599 pInputAssemblyState: addr(inputAssembly), | |
600 pViewportState: addr(viewportState), | |
601 pRasterizationState: addr(rasterizer), | |
602 pMultisampleState: addr(multisampling), | |
603 pDepthStencilState: nil, | |
604 pColorBlendState: addr(colorBlending), | |
605 pDynamicState: addr(dynamicState), | |
606 layout: result.layout, | |
607 renderPass: renderPass, | |
608 subpass: 0, | |
609 basePipelineHandle: VkPipeline(0), | |
610 basePipelineIndex: -1, | |
611 ) | |
612 checkVkResult vkCreateGraphicsPipelines( | |
613 device, | |
614 VkPipelineCache(0), | |
615 1, | |
616 addr(createInfo), | |
617 nil, | |
618 addr(result.pipeline) | |
619 ) | |
620 | |
1177 | 621 proc AllocateIndirectMemory(device: VkDevice, pDevice: VkPhysicalDevice, size: uint64): IndirectGPUMemory = |
1176 | 622 # chooses biggest memory type that has NOT VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1177 | 623 result.size = size |
624 result.needsTransfer = true | |
1176 | 625 |
626 # find a good memory type | |
627 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
628 vkGetPhysicalDeviceMemoryProperties(pDevice, addr physicalProperties) | |
629 | |
1177 | 630 var biggestHeap: uint64 = 0 |
631 var memoryTypeIndex = high(uint32) | |
1176 | 632 # try to find non-host-visible type |
1177 | 633 for i in 0'u32 ..< physicalProperties.memoryTypeCount: |
634 if not (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in toEnums(physicalProperties.memoryTypes[i].propertyFlags)): | |
1176 | 635 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size |
636 if size > biggestHeap: | |
1177 | 637 biggestHeap = size |
1176 | 638 memoryTypeIndex = i |
639 | |
640 # If we did not found a device-only memory type, let's just take the biggest overall | |
1177 | 641 if memoryTypeIndex == high(uint32): |
642 result.needsTransfer = false | |
643 for i in 0'u32 ..< physicalProperties.memoryTypeCount: | |
644 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
645 if size > biggestHeap: | |
646 biggestHeap = size | |
647 memoryTypeIndex = i | |
1176 | 648 |
1177 | 649 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type" |
1176 | 650 var allocationInfo = VkMemoryAllocateInfo( |
651 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
1177 | 652 allocationSize: result.size, |
1176 | 653 memoryTypeIndex: memoryTypeIndex, |
654 ) | |
655 checkVkResult vkAllocateMemory( | |
656 device, | |
657 addr allocationInfo, | |
658 nil, | |
659 addr result.vk | |
660 ) | |
661 | |
1177 | 662 proc AllocateDirectMemory(device: VkDevice, pDevice: VkPhysicalDevice, size: uint64): DirectGPUMemory = |
663 result.size = size | |
664 result.needsFlush = true | |
1176 | 665 |
666 # find a good memory type | |
667 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
668 vkGetPhysicalDeviceMemoryProperties(pDevice, addr physicalProperties) | |
1164 | 669 |
1177 | 670 var biggestHeap: uint64 = 0 |
671 var memoryTypeIndex = high(uint32) | |
1176 | 672 # try to find host-visible type |
673 for i in 0 ..< physicalProperties.memoryTypeCount: | |
1177 | 674 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags) |
675 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags: | |
1176 | 676 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size |
677 if size > biggestHeap: | |
1177 | 678 biggestHeap = size |
1176 | 679 memoryTypeIndex = i |
1177 | 680 result.needsFlush = not (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT in flags) |
1164 | 681 |
1177 | 682 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type" |
1176 | 683 var allocationInfo = VkMemoryAllocateInfo( |
684 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
1177 | 685 allocationSize: result.size, |
1176 | 686 memoryTypeIndex: memoryTypeIndex, |
687 ) | |
688 checkVkResult vkAllocateMemory( | |
689 device, | |
690 addr allocationInfo, | |
691 nil, | |
692 addr result.vk | |
693 ) | |
1177 | 694 checkVkResult vkMapMemory( |
695 device = device, | |
1176 | 696 memory = result.vk, |
697 offset = 0'u64, | |
698 size = result.size, | |
699 flags = VkMemoryMapFlags(0), | |
700 ppData = addr(result.data) | |
701 ) | |
702 | |
1177 | 703 proc AllocateIndirectBuffer(device: VkDevice, renderData: var RenderData, size: uint64, usage: openArray[VkBufferUsageFlagBits]) = |
704 assert size > 0, "Buffer sizes must be larger than 0" | |
705 var buffer = Buffer[IndirectGPUMemory](size: size) | |
706 | |
707 # iterate through memory areas to find big enough free space | |
708 for (memory, offset) in renderData.indirectMemory.mitems: | |
709 if memory.size - offset >= size: | |
710 buffer.offset = offset | |
711 # create buffer | |
712 var createInfo = VkBufferCreateInfo( | |
713 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
714 flags: VkBufferCreateFlags(0), | |
715 size: buffer.size, | |
716 usage: toBits(@usage & @[VK_BUFFER_USAGE_TRANSFER_DST_BIT]), # ensure we can transfer to this buffer | |
717 sharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
718 ) | |
719 checkVkResult vkCreateBuffer( | |
720 device = device, | |
721 pCreateInfo = addr createInfo, | |
722 pAllocator = nil, | |
723 pBuffer = addr(buffer.vk) | |
724 ) | |
725 checkVkResult vkBindBufferMemory(device, buffer.vk, memory.vk, buffer.offset) | |
726 renderData.indirectBuffers.add buffer | |
727 # update memory area offset | |
728 offset = offset + size | |
729 if offset mod MEMORY_ALIGNMENT != 0: | |
730 offset = offset + MEMORY_ALIGNMENT - (offset mod MEMORY_ALIGNMENT) | |
731 return | |
732 | |
733 assert false, "Did not find allocated memory region with enough space" | |
734 | |
735 proc AllocateDirectBuffer(device: VkDevice, renderData: var RenderData, size: uint64, usage: openArray[VkBufferUsageFlagBits]) = | |
736 assert size > 0, "Buffer sizes must be larger than 0" | |
737 var buffer = Buffer[DirectGPUMemory](size: size) | |
738 | |
739 # iterate through memory areas to find big enough free space | |
740 for (memory, offset) in renderData.directMemory.mitems: | |
741 if memory.size - offset >= size: | |
742 buffer.offset = offset | |
743 # create buffer | |
744 var createInfo = VkBufferCreateInfo( | |
745 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
746 flags: VkBufferCreateFlags(0), | |
747 size: buffer.size, | |
748 usage: toBits(usage), | |
749 sharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
750 ) | |
751 checkVkResult vkCreateBuffer( | |
752 device = device, | |
753 pCreateInfo = addr createInfo, | |
754 pAllocator = nil, | |
755 pBuffer = addr(buffer.vk) | |
756 ) | |
757 checkVkResult vkBindBufferMemory(device, buffer.vk, memory.vk, buffer.offset) | |
758 renderData.directBuffers.add buffer | |
759 # update memory area offset | |
760 offset = offset + size | |
761 if offset mod MEMORY_ALIGNMENT != 0: | |
762 offset = offset + MEMORY_ALIGNMENT - (offset mod MEMORY_ALIGNMENT) | |
763 return | |
764 | |
765 assert false, "Did not find allocated memory region with enough space" | |
766 | |
767 proc InitRenderData(device: VkDevice, pDevice: VkPhysicalDevice, descriptorPoolLimit = 1024'u32): RenderData = | |
1176 | 768 # allocate descriptor pools |
769 var poolSizes = [ | |
1177 | 770 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit), |
771 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit), | |
1176 | 772 ] |
773 var poolInfo = VkDescriptorPoolCreateInfo( | |
774 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
775 poolSizeCount: poolSizes.len.uint32, | |
776 pPoolSizes: poolSizes.ToCPointer, | |
777 maxSets: descriptorPoolLimit, | |
778 ) | |
779 checkVkResult vkCreateDescriptorPool(device, addr(poolInfo), nil, addr(result.descriptorPool)) | |
780 | |
781 # allocate some memory | |
1177 | 782 var initialAllocationSize = 1_000_000_000'u64 # TODO: make this more dynamic or something |
783 result.indirectMemory = @[(AllocateIndirectMemory(device, pDevice, size = initialAllocationSize), 0'u64)] | |
784 result.directMemory = @[(AllocateDirectMemory(device, pDevice, size = initialAllocationSize), 0'u64)] | |
1164 | 785 |
1177 | 786 proc GetIndirectBufferSizes[T](data: T): uint64 = |
787 # return buffer sizes for direct and indirect buffers | |
788 # BUFFER_ALIGNMENT is just added for a rough estimate, to ensure we have enough space to align when binding | |
789 for name, value in fieldPairs(data): | |
790 when not hasCustomPragma(value, VertexIndices) | |
791 when typeof(value) is GPUArray: | |
792 if not IsDirectMemory(value): | |
793 result += (value.data.len * sizeof(elementType(value.data))).uint64 + BUFFER_ALIGNMENT | |
794 when typeof(value) is GPUValue: | |
795 if not IsDirectMemory(value): | |
796 result += sizeof(value.data).uint64 + BUFFER_ALIGNMENT | |
797 proc GetDirectBufferSizes[T](data: T): uint64 = | |
798 # return buffer sizes for direct and indirect buffers | |
799 # BUFFER_ALIGNMENT is just added for a rough estimate, to ensure we have enough space to align when binding | |
800 for name, value in fieldPairs(data): | |
801 when not hasCustomPragma(value, VertexIndices) | |
802 when typeof(value) is GPUArray: | |
803 if IsDirectMemory(value): | |
804 result += (value.data.len * sizeof(elementType(value.data))).uint64 + BUFFER_ALIGNMENT | |
805 when typeof(value) is GPUValue: | |
806 if IsDirectMemory(value): | |
807 result += sizeof(value.data).uint64 + BUFFER_ALIGNMENT | |
808 | |
809 proc GetIndirectIndexBufferSizes[T](data: T): uint64 = | |
810 for name, value in fieldPairs(data): | |
811 when hasCustomPragma(value, VertexIndices): | |
812 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray" | |
813 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32 | |
814 if not IsDirectMemory(value): | |
815 result += (value.data.len * sizeof(elementType(value.data))).uint64 + BUFFER_ALIGNMENT | |
816 | |
817 proc GetDirectIndexBufferSizes[T](data: T): uint64 = | |
818 for name, value in fieldPairs(data): | |
819 when hasCustomPragma(value, VertexIndices): | |
820 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray" | |
821 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32 | |
822 if IsDirectMemory(value): | |
823 result += (value.data.len * sizeof(elementType(value.data))).uint64 + BUFFER_ALIGNMENT | |
824 | |
825 | |
826 proc WriteDescriptors[TShader](device: VkDevice, descriptorSets: array[INFLIGHTFRAMES.int, VkDescriptorSet]) = | |
1165 | 827 var descriptorSetWrites: seq[VkWriteDescriptorSet] |
1176 | 828 # map (buffer + offset + range) to descriptor |
829 # map (texture) to descriptor | |
830 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber): | |
1177 | 831 for frameInFlight in 0 ..< descriptorSets.len: |
832 when descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: | |
1173 | 833 # TODO |
834 let bufferInfo = VkDescriptorBufferInfo( | |
835 buffer: VkBuffer(0), | |
836 offset: 0, | |
837 range: 1, | |
838 ) | |
839 descriptorSetWrites.add VkWriteDescriptorSet( | |
840 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
1177 | 841 dstSet: descriptorSets[frameInFlight], |
1173 | 842 dstBinding: descriptorBindingNumber, |
843 dstArrayElement: uint32(0), | |
844 descriptorType: descriptorType, | |
845 descriptorCount: descriptorCount, | |
846 pImageInfo: nil, | |
847 pBufferInfo: addr(bufferInfo), | |
848 ) | |
849 elif descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: | |
850 # TODO | |
851 let imageInfo = VkDescriptorImageInfo( | |
852 sampler: VkSampler(0), | |
853 imageView: VkImageView(0), | |
854 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, | |
855 ) | |
856 descriptorSetWrites.add VkWriteDescriptorSet( | |
857 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
1177 | 858 dstSet: descriptorSets[frameInFlight], |
1173 | 859 dstBinding: descriptorBindingNumber, |
860 dstArrayElement: uint32(0), | |
861 descriptorType: descriptorType, | |
862 descriptorCount: descriptorCount, | |
863 pImageInfo: addr(imageInfo), | |
864 pBufferInfo: nil, | |
865 ) | |
866 vkUpdateDescriptorSets(device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) | |
1165 | 867 |
1172 | 868 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
1159 | 869 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) |
1177 | 870 #[ |
871 commandBuffer.vkCmdBindDescriptorSets( | |
872 VK_PIPELINE_BIND_POINT_GRAPHICS, | |
873 pipeline.layout, | |
874 0, | |
875 1, | |
876 addr pipeline.descriptorSets[currentFrameInFlight], | |
877 0, | |
878 nil, | |
879 ) | |
880 ]# | |
1161 | 881 |
1177 | 882 proc AssertCompatible(TShader, TMesh, TInstance, TUniforms, TGlobals: typedesc) = |
1162 | 883 # assert seq-fields of TMesh|TInstance == seq-fields of TShader |
1161 | 884 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors |
1162 | 885 for inputName, inputValue in default(TShader).fieldPairs: |
1161 | 886 var foundField = false |
1176 | 887 |
888 # Vertex input data | |
1161 | 889 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
|
890 assert typeof(inputValue) is SupportedGPUType |
1161 | 891 for meshName, meshValue in default(TMesh).fieldPairs: |
892 when meshName == inputName: | |
1176 | 893 assert meshValue is GPUArray, "Mesh attribute '" & meshName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(meshValue)) |
1162 | 894 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1176 | 895 assert elementType(meshValue.data) 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.data)) & "'" |
1161 | 896 foundField = true |
1162 | 897 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "'" |
1176 | 898 |
899 # Instance input data | |
1161 | 900 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
|
901 assert typeof(inputValue) is SupportedGPUType |
1161 | 902 for instanceName, instanceValue in default(TInstance).fieldPairs: |
903 when instanceName == inputName: | |
1176 | 904 assert instanceValue is GPUArray, "Instance attribute '" & instanceName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(instanceName)) |
1162 | 905 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1176 | 906 assert elementType(instanceValue.data) 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.data)) & "'" |
1161 | 907 foundField = true |
1162 | 908 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TInstance) & "'" |
1176 | 909 |
910 # Texture | |
911 elif typeof(inputValue) is Texture: | |
1177 | 912 for uniformName, uniformValue in default(TUniforms).fieldPairs: |
913 when uniformName == inputName: | |
1162 | 914 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1177 | 915 assert typeof(uniformValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue)) & "'" |
1161 | 916 foundField = true |
917 for globalName, globalValue in default(TGlobals).fieldPairs: | |
918 when globalName == inputName: | |
1162 | 919 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
920 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 | 921 foundField = true |
1162 | 922 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
1176 | 923 |
924 # Uniform block | |
925 elif typeof(inputValue) is object: | |
1177 | 926 for uniformName, uniformValue in default(TUniforms).fieldPairs: |
927 when uniformName == inputName: | |
928 assert uniformValue is GPUValue, "global attribute '" & uniformName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(uniformValue)) | |
1176 | 929 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1177 | 930 assert typeof(uniformValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue.data)) & "'" |
1176 | 931 foundField = true |
932 for globalName, globalValue in default(TGlobals).fieldPairs: | |
933 when globalName == inputName: | |
934 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue)) | |
935 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
936 assert typeof(globalValue.data) 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.data)) & "'" | |
937 foundField = true | |
938 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
939 | |
940 # array | |
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
|
941 elif typeof(inputValue) is array: |
1176 | 942 |
943 # texture-array | |
944 when elementType(inputValue) is Texture: | |
1177 | 945 for uniformName, uniformValue in default(TUniforms).fieldPairs: |
946 when uniformName == inputName: | |
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
|
947 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1177 | 948 assert typeof(uniformValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue)) & "'" |
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
|
949 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
|
950 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
|
951 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
|
952 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
|
953 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
|
954 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
|
955 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
1161 | 956 |
1176 | 957 # uniform-block array |
958 elif elementType(inputValue) is object: | |
1177 | 959 for uniformName, uniformValue in default(TUniforms).fieldPairs: |
960 when uniformName == inputName: | |
961 assert uniformValue is GPUValue, "global attribute '" & uniformName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(uniformValue)) | |
1176 | 962 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1177 | 963 assert typeof(uniformValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue.data)) & "'" |
1176 | 964 foundField = true |
965 for globalName, globalValue in default(TGlobals).fieldPairs: | |
966 when globalName == inputName: | |
967 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue)) | |
968 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
969 assert typeof(globalValue.data) 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.data)) & "'" | |
970 foundField = true | |
971 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
972 | |
1161 | 973 |
1177 | 974 proc Render[TShader, TMesh, TInstance, TUniforms, TGlobals]( |
1162 | 975 pipeline: Pipeline[TShader], |
1161 | 976 renderable: Renderable[TMesh, TInstance], |
1177 | 977 uniforms: TUniforms, |
1161 | 978 globals: TGlobals, |
979 commandBuffer: VkCommandBuffer, | |
980 ) = | |
1177 | 981 static: AssertCompatible(TShader, TMesh, TInstance, TUniforms, TGlobals) |
1164 | 982 if renderable.vertexBuffers.len > 0: |
983 commandBuffer.vkCmdBindVertexBuffers( | |
984 firstBinding = 0'u32, | |
985 bindingCount = uint32(renderable.vertexBuffers.len), | |
986 pBuffers = renderable.vertexBuffers.ToCPointer(), | |
987 pOffsets = renderable.bufferOffsets.ToCPointer() | |
988 ) | |
1161 | 989 if renderable.indexType != None: |
1159 | 990 commandBuffer.vkCmdBindIndexBuffer( |
991 renderable.indexBuffer, | |
992 renderable.indexBufferOffset, | |
1161 | 993 renderable.indexType, |
1159 | 994 ) |
995 commandBuffer.vkCmdDrawIndexed( | |
1161 | 996 indexCount = renderable.indexCount, |
997 instanceCount = renderable.instanceCount, | |
1159 | 998 firstIndex = 0, |
999 vertexOffset = 0, | |
1000 firstInstance = 0 | |
1001 ) | |
1002 else: | |
1003 commandBuffer.vkCmdDraw( | |
1161 | 1004 vertexCount = renderable.vertexCount, |
1005 instanceCount = renderable.instanceCount, | |
1159 | 1006 firstVertex = 0, |
1007 firstInstance = 0 | |
1008 ) | |
1161 | 1009 |
1010 when isMainModule: | |
1162 | 1011 import semicongine/platform/window |
1012 import semicongine/vulkan/instance | |
1013 import semicongine/vulkan/device | |
1014 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
|
1015 import std/options |
1162 | 1016 |
1161 | 1017 type |
1177 | 1018 MeshA = object |
1019 position: GPUArray[Vec3f, IndirectGPUMemory] | |
1020 indices {.VertexIndices.}: GPUArray[uint16, IndirectGPUMemory] | |
1021 InstanceA = object | |
1022 rotation: GPUArray[Vec4f, IndirectGPUMemory] | |
1023 objPosition: GPUArray[Vec3f, IndirectGPUMemory] | |
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
|
1024 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
|
1025 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
|
1026 baseColor: Vec3f |
1176 | 1027 UniformsA = object |
1028 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory] | |
1029 materialTextures: array[3, Texture] | |
1177 | 1030 ShaderSettings = object |
1031 brightness: float32 | |
1176 | 1032 GlobalsA = object |
1162 | 1033 fontAtlas: Texture |
1176 | 1034 settings: GPUValue[ShaderSettings, IndirectGPUMemory] |
1161 | 1035 |
1162 | 1036 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
|
1037 # vertex input |
1161 | 1038 position {.VertexAttribute.}: Vec3f |
1176 | 1039 objPosition {.InstanceAttribute.}: Vec3f |
1040 rotation {.InstanceAttribute.}: 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
|
1041 # intermediate |
1162 | 1042 test {.Pass.}: float32 |
1043 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
|
1044 # output |
1162 | 1045 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
|
1046 # uniforms |
1176 | 1047 materials: array[3, MaterialA] |
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
|
1048 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
|
1049 # 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
|
1050 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
|
1051 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
|
1052 # code |
1162 | 1053 vertexCode: string = "void main() {}" |
1054 fragmentCode: string = "void main() {}" | |
1161 | 1055 |
1162 | 1056 let w = CreateWindow("test2") |
1057 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") | |
1058 let i = w.CreateInstance( | |
1059 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), | |
1060 instanceExtensions = @[], | |
1061 layers = @["VK_LAYER_KHRONOS_validation"], | |
1062 ) | |
1063 | |
1064 let selectedPhysicalDevice = i.GetPhysicalDevices().FilterBestGraphics() | |
1173 | 1065 let dev = i.CreateDevice( |
1162 | 1066 selectedPhysicalDevice, |
1067 enabledExtensions = @[], | |
1068 selectedPhysicalDevice.FilterForGraphicsPresentationQueues() | |
1069 ) | |
1173 | 1070 let frameWidth = 100'u32 |
1071 let frameHeight = 100'u32 | |
1162 | 1072 |
1176 | 1073 var myMesh1 = MeshA( |
1074 position: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | |
1075 ) | |
1076 var uniforms1 = UniformsA( | |
1077 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory](data: [ | |
1078 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
1079 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
1080 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
1081 ]), | |
1082 materialTextures: [ | |
1083 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[255'u8, 0'u8, 0'u8, 255'u8]])), | |
1084 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 255'u8, 0'u8, 255'u8]])), | |
1085 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 0'u8, 255'u8, 255'u8]])), | |
1086 ] | |
1087 ) | |
1088 var instances1 = InstanceA( | |
1089 rotation: GPUArray[Vec4f, IndirectGPUMemory](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
1090 objPosition: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
1091 ) | |
1092 var myGlobals: GlobalsA | |
1173 | 1093 |
1094 # setup for rendering (TODO: swapchain & framebuffers) | |
1095 | |
1096 # renderpass | |
1097 let renderpass = dev.vk.CreateRenderPass(dev.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format) | |
1098 | |
1099 # shaders | |
1100 const shader = ShaderA() | |
1101 let shaderObject = dev.vk.CompileShader(shader) | |
1102 var pipeline1 = CreatePipeline(dev.vk, renderPass = renderpass, shaderObject) | |
1103 | |
1177 | 1104 var renderdata = InitRenderData(dev.vk, dev.physicalDevice.vk) |
1176 | 1105 |
1106 # create descriptor sets | |
1177 | 1107 #[ |
1176 | 1108 var descriptorSets: array[INFLIGHTFRAMES.int, VkDescriptorSet] |
1109 var layouts = newSeqWith(descriptorSets.len, pipeline.descriptorSetLayout) | |
1110 var allocInfo = VkDescriptorSetAllocateInfo( | |
1111 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
1112 descriptorPool: pool, | |
1113 descriptorSetCount: uint32(layouts.len), | |
1114 pSetLayouts: layouts.ToCPointer, | |
1115 ) | |
1116 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), descriptorSets.ToCPointer) | |
1177 | 1117 ]# |
1176 | 1118 |
1119 #[ | |
1177 | 1120 # TODO: |
1121 # | |
1122 # assign indirect buffers to vertex data, can happen through the GPUArray/GPUValue-wrappers, they know buffers | |
1123 # assign direct buffers to vertex data | |
1124 # assign indirect buffers to uniform data | |
1125 # assign direct buffers to uniform data | |
1126 # | |
1127 # upload all textures | |
1128 # write descriptors for textures and uniform buffers | |
1129 # | |
1176 | 1130 ]# |
1131 var myRenderable: Renderable[MeshA, InstanceA] | |
1161 | 1132 |
1177 | 1133 var |
1134 indirectVertexSizes = 0'u64 | |
1135 directVertexSizes = 0'u64 | |
1136 indirectIndexSizes = 0'u64 | |
1137 directIndexSizes = 0'u64 | |
1138 indirectUniformSizes = 0'u64 | |
1139 directUniformSizes = 0'u64 | |
1140 | |
1141 indirectVertexSizes += GetIndirectBufferSizes(myMesh1) | |
1142 indirectVertexSizes += GetIndirectBufferSizes(instances1) | |
1143 if indirectVertexSizes > 0: | |
1144 AllocateIndirectBuffer(dev.vk, renderdata, indirectVertexSizes, [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT]) | |
1145 | |
1146 directVertexSizes += GetDirectBufferSizes(myMesh1) | |
1147 directVertexSizes += GetDirectBufferSizes(instances1) | |
1148 if directVertexSizes > 0: | |
1149 AllocateDirectBuffer(dev.vk, renderdata, directVertexSizes, [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT]) | |
1150 | |
1151 indirectIndexSizes += GetIndirectIndexBufferSizes(myMesh1) | |
1152 if indirectIndexSizes > 0: | |
1153 AllocateIndirectBuffer(dev.vk, renderdata, indirectIndexSizes, [VK_BUFFER_USAGE_INDEX_BUFFER_BIT]) | |
1154 | |
1155 directIndexSizes += GetDirectIndexBufferSizes(myMesh1) | |
1156 if directIndexSizes > 0: | |
1157 AllocateIndirectBuffer(dev.vk, renderdata, directIndexSizes, [VK_BUFFER_USAGE_INDEX_BUFFER_BIT]) | |
1158 | |
1159 indirectUniformSizes += GetIndirectBufferSizes(uniforms1) | |
1160 indirectUniformSizes += GetIndirectBufferSizes(myGlobals) | |
1161 if indirectUniformSizes > 0: | |
1162 AllocateIndirectBuffer(dev.vk, renderdata, indirectUniformSizes, [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT]) | |
1163 | |
1164 directUniformSizes += GetDirectBufferSizes(uniforms1) | |
1165 directUniformSizes += GetDirectBufferSizes(myGlobals) | |
1166 if directUniformSizes > 0: | |
1167 AllocateDirectBuffer(dev.vk, renderdata, directUniformSizes, [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT]) | |
1168 | |
1173 | 1169 # descriptors |
1177 | 1170 # WriteDescriptors(dev.vk, pipeline1) |
1173 | 1171 |
1172 # command buffer | |
1173 var | |
1174 commandBufferPool: VkCommandPool | |
1175 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] | |
1176 createInfo = VkCommandPoolCreateInfo( | |
1177 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
1178 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
1179 queueFamilyIndex: dev.FirstGraphicsQueue().get().family.index, | |
1180 ) | |
1181 checkVkResult vkCreateCommandPool(dev.vk, addr createInfo, nil, addr commandBufferPool) | |
1182 var allocInfo = VkCommandBufferAllocateInfo( | |
1183 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
1184 commandPool: commandBufferPool, | |
1185 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
1186 commandBufferCount: INFLIGHTFRAMES, | |
1187 ) | |
1188 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
|
1189 |
1173 | 1190 # start command buffer |
1191 block: | |
1192 let | |
1193 currentFramebuffer = VkFramebuffer(0) # TODO | |
1194 currentFrameInFlight = 1 | |
1195 cmd = cmdBuffers[currentFrameInFlight] | |
1196 beginInfo = VkCommandBufferBeginInfo( | |
1197 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
1198 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | |
1199 ) | |
1200 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) | |
1201 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
|
1202 |
1173 | 1203 # start renderpass |
1204 block: | |
1205 var | |
1206 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
1207 renderPassInfo = VkRenderPassBeginInfo( | |
1208 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
1209 renderPass: renderpass, | |
1210 framebuffer: currentFramebuffer, | |
1211 renderArea: VkRect2D( | |
1212 offset: VkOffset2D(x: 0, y: 0), | |
1213 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
1214 ), | |
1215 clearValueCount: uint32(clearColors.len), | |
1216 pClearValues: clearColors.ToCPointer(), | |
1217 ) | |
1218 viewport = VkViewport( | |
1219 x: 0.0, | |
1220 y: 0.0, | |
1221 width: frameWidth.float32, | |
1222 height: frameHeight.float32, | |
1223 minDepth: 0.0, | |
1224 maxDepth: 1.0, | |
1225 ) | |
1226 scissor = VkRect2D( | |
1227 offset: VkOffset2D(x: 0, y: 0), | |
1228 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
1229 ) | |
1230 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
|
1231 |
1173 | 1232 # setup viewport |
1233 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport)) | |
1234 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
1235 | |
1236 # bind pipeline, will be loop | |
1237 block: | |
1238 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) | |
1239 | |
1240 # render object, will be loop | |
1241 block: | |
1177 | 1242 Render(pipeline1, myRenderable, uniforms1, myGlobals, cmd) |
1173 | 1243 |
1244 vkCmdEndRenderPass(cmd) | |
1245 checkVkResult cmd.vkEndCommandBuffer() |