Mercurial > games > semicongine
annotate static_utils.nim @ 1180:d554f8185815 compiletime-tests
add: tons of work on descriptors
author | sam <sam@basx.dev> |
---|---|
date | Tue, 02 Jul 2024 06:42:42 +0700 |
parents | e1830f9b8af4 |
children | 6b66e6c837bc |
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 | |
15 | |
1179 | 16 template VertexAttribute {.pragma.} |
17 template InstanceAttribute {.pragma.} | |
18 template Pass {.pragma.} | |
19 template PassFlat {.pragma.} | |
20 template ShaderOutput {.pragma.} | |
1180 | 21 template VertexIndices {.pragma.} |
22 template DescriptorSet {.pragma.} | |
1159 | 23 |
1165 | 24 const INFLIGHTFRAMES = 2'u32 |
1180 | 25 const MAX_DESCRIPTORSETS = 2 |
1177 | 26 const MEMORY_ALIGNMENT = 65536'u64 # Align buffers inside memory along this alignment |
1178 | 27 const BUFFER_ALIGNMENT = 64'u64 # align offsets inside buffers along this alignment |
1176 | 28 |
1179 | 29 # some globals that will (likely?) never change during the life time of the engine |
1159 | 30 type |
1179 | 31 VulkanGlobals = object |
32 instance: VkInstance | |
33 device: VkDevice | |
34 physicalDevice: VkPhysicalDevice | |
35 queueFamilyIndex: uint32 | |
36 queue: VkQueue | |
37 var vulkan: VulkanGlobals | |
38 | |
39 type | |
40 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] | |
41 ShaderObject[TShader] = 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
|
42 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
|
43 fragmentShader: VkShaderModule |
1159 | 44 |
1179 | 45 func alignedTo[T: SomeInteger](value: T, alignment: T): T = |
1178 | 46 let remainder = value mod alignment |
47 if remainder == 0: | |
48 return value | |
49 else: | |
50 return value + alignment - remainder | |
51 | |
1159 | 52 func VkType[T: SupportedGPUType](value: T): VkFormat = |
53 when T is float32: VK_FORMAT_R32_SFLOAT | |
54 elif T is float64: VK_FORMAT_R64_SFLOAT | |
55 elif T is int8: VK_FORMAT_R8_SINT | |
56 elif T is int16: VK_FORMAT_R16_SINT | |
57 elif T is int32: VK_FORMAT_R32_SINT | |
58 elif T is int64: VK_FORMAT_R64_SINT | |
59 elif T is uint8: VK_FORMAT_R8_UINT | |
60 elif T is uint16: VK_FORMAT_R16_UINT | |
61 elif T is uint32: VK_FORMAT_R32_UINT | |
62 elif T is uint64: VK_FORMAT_R64_UINT | |
63 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT | |
64 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT | |
65 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT | |
66 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT | |
67 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT | |
68 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT | |
69 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT | |
70 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT | |
71 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT | |
72 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT | |
73 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT | |
74 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT | |
75 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT | |
76 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT | |
77 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
78 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
79 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
80 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
1162 | 81 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT |
82 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT | |
83 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
84 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
85 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT | |
86 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT | |
87 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
88 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
89 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
90 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
91 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
92 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
93 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
94 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
95 else: {.error: "Unsupported data type on GPU".} | |
96 | |
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
|
97 func GlslType[T: SupportedGPUType|Texture](value: T): string = |
1162 | 98 when T is float32: "float" |
99 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
|
100 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
|
101 elif T is uint8 or T is uint16 or T is uint32 or T is uint64: "uint" |
1162 | 102 elif T is TVec2[int32]: "ivec2" |
103 elif T is TVec2[int64]: "ivec2" | |
104 elif T is TVec3[int32]: "ivec3" | |
105 elif T is TVec3[int64]: "ivec3" | |
106 elif T is TVec4[int32]: "ivec4" | |
107 elif T is TVec4[int64]: "ivec4" | |
108 elif T is TVec2[uint32]: "uvec2" | |
109 elif T is TVec2[uint64]: "uvec2" | |
110 elif T is TVec3[uint32]: "uvec3" | |
111 elif T is TVec3[uint64]: "uvec3" | |
112 elif T is TVec4[uint32]: "uvec4" | |
113 elif T is TVec4[uint64]: "uvec4" | |
114 elif T is TVec2[float32]: "vec2" | |
115 elif T is TVec2[float64]: "dvec2" | |
116 elif T is TVec3[float32]: "vec3" | |
117 elif T is TVec3[float64]: "dvec3" | |
118 elif T is TVec4[float32]: "vec4" | |
119 elif T is TVec4[float64]: "dvec4" | |
120 elif T is TMat2[float32]: "mat2" | |
121 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
|
122 elif T is TMat23[float32]: "mat23" |
1162 | 123 elif T is TMat23[float64]: "dmat23" |
124 elif T is TMat32[float32]: "mat32" | |
125 elif T is TMat32[float64]: "dmat32" | |
126 elif T is TMat3[float32]: "mat3" | |
127 elif T is TMat3[float64]: "dmat3" | |
128 elif T is TMat34[float32]: "mat34" | |
129 elif T is TMat34[float64]: "dmat34" | |
130 elif T is TMat43[float32]: "mat43" | |
131 elif T is TMat43[float64]: "dmat43" | |
132 elif T is TMat4[float32]: "mat4" | |
133 elif T is TMat4[float64]: "dmat4" | |
134 elif T is Texture: "sampler2D" | |
1159 | 135 else: {.error: "Unsupported data type on GPU".} |
136 | |
1179 | 137 template ForVertexDataFields(shader: typed, fieldname, valuename, isinstancename, body: untyped): untyped = |
138 for theFieldname, value in fieldPairs(shader): | |
1161 | 139 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
1159 | 140 when not typeof(value) is seq: |
141 {.error: "field '" & theFieldname & "' needs to be a seq".} | |
142 when not typeof(value) is SupportedGPUType: | |
143 {.error: "field '" & theFieldname & "' is not a supported GPU type".} | |
144 block: | |
1179 | 145 const `fieldname` {.inject.} = theFieldname |
1162 | 146 let `valuename` {.inject.} = value |
1179 | 147 const `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute) |
1159 | 148 body |
149 | |
1180 | 150 template ForDescriptorSets(shader: typed, setNumber, descriptorSet, body: untyped): untyped = |
151 var n = 0 | |
152 for theFieldname, value in fieldPairs(shader): | |
153 when value.hasCustomPragma(DescriptorSet): | |
154 block: | |
155 let `setNumber` {.inject.} = n | |
156 let `descriptorSet` {.inject.} = value | |
157 body | |
158 n.inc | |
159 | |
1179 | 160 template ForDescriptorFields(shader: typed, fieldname, typename, countname, bindingNumber, body: untyped): untyped = |
1173 | 161 var `bindingNumber` {.inject.} = 1'u32 |
1179 | 162 for theFieldname, value in fieldPairs(shader): |
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
|
163 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
|
164 block: |
1180 | 165 const `fieldname` {.inject.} = theFieldname |
1179 | 166 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER |
167 const `countname` {.inject.} = 1'u32 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
168 body |
1173 | 169 `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
|
170 elif typeof(value) is object: |
1161 | 171 block: |
1180 | 172 const `fieldname` {.inject.} = theFieldname |
1179 | 173 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER |
174 const `countname` {.inject.} = 1'u32 | |
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
175 body |
1173 | 176 `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
|
177 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
|
178 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
|
179 block: |
1180 | 180 const `fieldname` {.inject.} = theFieldname |
1179 | 181 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER |
182 const `countname` {.inject.} = uint32(typeof(value).len) | |
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
|
183 body |
1173 | 184 `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
|
185 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
|
186 block: |
1180 | 187 const `fieldname` {.inject.} = theFieldname |
1179 | 188 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER |
189 const `countname` {.inject.} = uint32(typeof(value).len) | |
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
|
190 body |
1173 | 191 `bindingNumber`.inc |
1161 | 192 |
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
|
193 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 = |
1159 | 194 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]: |
195 2 | |
196 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]: | |
197 3 | |
198 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]: | |
199 4 | |
200 else: | |
201 1 | |
202 | |
1162 | 203 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 = |
1159 | 204 #[ |
205 single location: | |
1162 | 206 - any scalar |
207 - any 16-bit vector | |
208 - any 32-bit vector | |
209 - any 64-bit vector that has max. 2 components | |
1159 | 210 16-bit scalar and vector types, and |
211 32-bit scalar and vector types, and | |
212 64-bit scalar and 2-component vector types. | |
213 two locations | |
214 64-bit three- and four-component vectors | |
215 ]# | |
1162 | 216 when T is TVec3[int64] or |
217 T is TVec4[int64] or | |
218 T is TVec3[uint64] or | |
219 T is TVec4[uint64] or | |
220 T is TVec3[float64] or | |
221 T is TVec4[float64] or | |
222 T is TMat23[float64] or | |
223 T is TMat3[float64] or | |
224 T is TMat34[float64] or | |
225 T is TMat43[float64] or | |
226 T is TMat4[float64]: | |
1159 | 227 return 2 |
228 else: | |
229 return 1 | |
230 | |
231 type | |
1161 | 232 IndexType = enum |
233 None, UInt8, UInt16, UInt32 | |
1176 | 234 |
235 IndirectGPUMemory = object | |
236 vk: VkDeviceMemory | |
237 size: uint64 | |
1177 | 238 needsTransfer: bool # usually true |
1176 | 239 DirectGPUMemory = object |
240 vk: VkDeviceMemory | |
241 size: uint64 | |
242 data: pointer | |
1177 | 243 needsFlush: bool # usually true |
1176 | 244 GPUMemory = IndirectGPUMemory | DirectGPUMemory |
245 | |
246 Buffer[TMemory: GPUMemory] = object | |
1178 | 247 memory: TMemory |
1176 | 248 vk: VkBuffer |
249 offset: uint64 | |
250 size: uint64 | |
251 | |
252 GPUArray[T: SupportedGPUType, TMemory: GPUMemory] = object | |
253 data: seq[T] | |
254 buffer: Buffer[TMemory] | |
255 offset: uint64 | |
256 GPUValue[T: object|array, TMemory: GPUMemory] = object | |
257 data: T | |
258 buffer: Buffer[TMemory] | |
259 offset: uint64 | |
1178 | 260 GPUData = GPUArray | GPUValue |
1176 | 261 |
1162 | 262 Pipeline[TShader] = object |
1159 | 263 pipeline: VkPipeline |
264 layout: VkPipelineLayout | |
1180 | 265 descriptorSetLayouts: array[MAX_DESCRIPTORSETS, VkDescriptorSetLayout] |
1178 | 266 BufferType = enum |
267 VertexBuffer, IndexBuffer, UniformBuffer | |
1176 | 268 RenderData = object |
269 descriptorPool: VkDescriptorPool | |
1177 | 270 # tuple is memory and offset to next free allocation in that memory |
1179 | 271 indirectMemory: seq[tuple[memory: IndirectGPUMemory, usedOffset: uint64]] |
272 directMemory: seq[tuple[memory: DirectGPUMemory, usedOffset: uint64]] | |
273 indirectBuffers: seq[tuple[buffer: Buffer[IndirectGPUMemory], btype: BufferType, usedOffset: uint64]] | |
274 directBuffers: seq[tuple[buffer: Buffer[DirectGPUMemory], btype: BufferType, usedOffset: uint64]] | |
1178 | 275 |
276 template UsesIndirectMemory(gpuData: GPUData): untyped = | |
277 get(genericParams(typeof(gpuData)), 1) is IndirectGPUMemory | |
278 template UsesDirectMemory(gpuData: GPUData): untyped = | |
279 get(genericParams(typeof(gpuData)), 1) is DirectGPUMemory | |
280 | |
281 template size(gpuArray: GPUArray): uint64 = | |
1179 | 282 (gpuArray.data.len * sizeof(elementType(gpuArray.data))).uint64 |
1178 | 283 template size(gpuValue: GPUValue): uint64 = |
1179 | 284 sizeof(gpuValue.data).uint64 |
285 | |
286 template datapointer(gpuArray: GPUArray): pointer = | |
287 addr(gpuArray.data[0]) | |
288 template datapointer(gpuValue: GPUValue): pointer = | |
289 addr(gpuValue.data) | |
1178 | 290 |
1179 | 291 proc AllocationSize(buffer: Buffer): uint64 = |
292 var req: VkMemoryRequirements | |
293 vkGetBufferMemoryRequirements(vulkan.device, buffer.vk, addr(req)) | |
294 return req.size | |
295 | |
296 proc GetPhysicalDevice(instance: VkInstance): VkPhysicalDevice = | |
1178 | 297 var nDevices: uint32 |
1179 | 298 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), nil) |
1178 | 299 var devices = newSeq[VkPhysicalDevice](nDevices) |
1179 | 300 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), devices.ToCPointer) |
1178 | 301 |
1179 | 302 var score = 0'u32 |
1178 | 303 for pDevice in devices: |
304 var props: VkPhysicalDeviceProperties | |
305 vkGetPhysicalDeviceProperties(pDevice, addr(props)) | |
1179 | 306 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU and props.limits.maxImageDimension2D > score: |
307 score = props.limits.maxImageDimension2D | |
1178 | 308 result = pDevice |
309 | |
1179 | 310 if score == 0: |
1178 | 311 for pDevice in devices: |
312 var props: VkPhysicalDeviceProperties | |
313 vkGetPhysicalDeviceProperties(pDevice, addr(props)) | |
1179 | 314 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU and props.limits.maxImageDimension2D > score: |
315 score = props.limits.maxImageDimension2D | |
1178 | 316 result = pDevice |
317 | |
318 assert score > 0, "Unable to find integrated or discrete GPU" | |
319 | |
320 | |
1179 | 321 proc GetDirectMemoryTypeIndex(): uint32 = |
1178 | 322 var physicalProperties: VkPhysicalDeviceMemoryProperties |
1179 | 323 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr(physicalProperties)) |
1178 | 324 |
325 var biggestHeap: uint64 = 0 | |
326 result = high(uint32) | |
327 # try to find host-visible type | |
1179 | 328 for i in 0'u32 ..< physicalProperties.memoryTypeCount: |
1178 | 329 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags) |
330 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags: | |
331 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
332 if size > biggestHeap: | |
333 biggestHeap = size | |
334 result = i | |
335 assert result != high(uint32), "There is not host visible memory. This is likely a driver bug." | |
336 | |
1179 | 337 proc GetQueueFamily(pDevice: VkPhysicalDevice, qType: VkQueueFlagBits): uint32 = |
1178 | 338 var nQueuefamilies: uint32 |
1179 | 339 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, nil) |
1178 | 340 var queuFamilies = newSeq[VkQueueFamilyProperties](nQueuefamilies) |
1179 | 341 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, queuFamilies.ToCPointer) |
342 for i in 0'u32 ..< nQueuefamilies: | |
1178 | 343 if qType in toEnums(queuFamilies[i].queueFlags): |
344 return i | |
345 assert false, &"Queue of type {qType} not found" | |
346 | |
1179 | 347 proc GetQueue(device: VkDevice, queueFamilyIndex: uint32, qType: VkQueueFlagBits): VkQueue = |
348 vkGetDeviceQueue( | |
1178 | 349 device, |
1179 | 350 queueFamilyIndex, |
1178 | 351 0, |
352 addr(result), | |
353 ) | |
354 | |
1179 | 355 proc GetSurfaceFormat(): VkFormat = |
356 # EVERY windows driver and almost every linux driver should support this | |
357 VK_FORMAT_B8G8R8A8_SRGB | |
1162 | 358 |
1179 | 359 template WithSingleUseCommandBuffer(device: VkDevice, cmd, body: untyped): untyped = |
1178 | 360 block: |
1179 | 361 var |
362 commandBufferPool: VkCommandPool | |
363 createInfo = VkCommandPoolCreateInfo( | |
1178 | 364 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, |
365 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
1179 | 366 queueFamilyIndex: vulkan.queueFamilyIndex, |
1178 | 367 ) |
368 checkVkResult vkCreateCommandPool(device, addr createInfo, nil, addr(commandBufferPool)) | |
369 var | |
370 `cmd` {.inject.}: VkCommandBuffer | |
371 allocInfo = VkCommandBufferAllocateInfo( | |
372 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
373 commandPool: commandBufferPool, | |
374 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
375 commandBufferCount: 1, | |
376 ) | |
1179 | 377 checkVkResult device.vkAllocateCommandBuffers(addr allocInfo, addr(`cmd`)) |
378 var beginInfo = VkCommandBufferBeginInfo( | |
1178 | 379 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
380 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | |
381 ) | |
382 checkVkResult `cmd`.vkBeginCommandBuffer(addr beginInfo) | |
383 | |
384 body | |
385 | |
386 checkVkResult `cmd`.vkEndCommandBuffer() | |
387 var submitInfo = VkSubmitInfo( | |
388 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO, | |
389 commandBufferCount: 1, | |
390 pCommandBuffers: addr(`cmd`), | |
391 ) | |
1179 | 392 |
393 var | |
394 fence: VkFence | |
395 fenceInfo = VkFenceCreateInfo( | |
396 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, | |
397 # flags: toBits [VK_FENCE_CREATE_SIGNALED_BIT] | |
398 ) | |
399 checkVkResult device.vkCreateFence(addr(fenceInfo), nil, addr(fence)) | |
400 checkVkResult vkQueueSubmit(vulkan.queue, 1, addr(submitInfo), fence) | |
401 checkVkResult vkWaitForFences(device, 1, addr fence, false, high(uint64)) | |
1178 | 402 vkDestroyCommandPool(device, commandBufferPool, nil) |
403 | |
404 | |
1179 | 405 proc UpdateGPUBuffer(gpuData: GPUData) = |
406 if gpuData.size == 0: | |
407 return | |
1178 | 408 when UsesDirectMemory(gpuData): |
1179 | 409 copyMem(cast[pointer](cast[uint64](gpuData.buffer.memory.data) + gpuData.buffer.offset + gpuData.offset), gpuData.datapointer, gpuData.size) |
1178 | 410 else: |
411 var | |
412 stagingBuffer: VkBuffer | |
413 createInfo = VkBufferCreateInfo( | |
414 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
415 flags: VkBufferCreateFlags(0), | |
416 size: gpuData.size, | |
417 usage: toBits([VK_BUFFER_USAGE_TRANSFER_SRC_BIT]), | |
418 sharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
419 ) | |
420 checkVkResult vkCreateBuffer( | |
1179 | 421 device = vulkan.device, |
1178 | 422 pCreateInfo = addr(createInfo), |
423 pAllocator = nil, | |
424 pBuffer = addr(stagingBuffer), | |
425 ) | |
426 var | |
427 stagingMemory: VkDeviceMemory | |
428 stagingPtr: pointer | |
429 memoryAllocationInfo = VkMemoryAllocateInfo( | |
430 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
1179 | 431 allocationSize: gpuData.buffer.AllocationSize(), |
1178 | 432 memoryTypeIndex: GetDirectMemoryTypeIndex(), |
433 ) | |
434 checkVkResult vkAllocateMemory( | |
1179 | 435 vulkan.device, |
1178 | 436 addr(memoryAllocationInfo), |
437 nil, | |
438 addr(stagingMemory), | |
439 ) | |
1179 | 440 checkVkResult vkBindBufferMemory(vulkan.device, stagingBuffer, stagingMemory, 0) |
1178 | 441 checkVkResult vkMapMemory( |
1179 | 442 device = vulkan.device, |
1178 | 443 memory = stagingMemory, |
444 offset = 0'u64, | |
445 size = VK_WHOLE_SIZE, | |
446 flags = VkMemoryMapFlags(0), | |
1179 | 447 ppData = addr(stagingPtr) |
1178 | 448 ) |
1179 | 449 copyMem(stagingPtr, gpuData.datapointer, gpuData.size) |
1178 | 450 var stagingRange = VkMappedMemoryRange( |
451 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, | |
452 memory: stagingMemory, | |
453 size: VK_WHOLE_SIZE, | |
454 ) | |
1179 | 455 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, 1, addr(stagingRange)) |
1178 | 456 |
1179 | 457 WithSingleUseCommandBuffer(vulkan.device, commandBuffer): |
1178 | 458 var copyRegion = VkBufferCopy(size: gpuData.size) |
459 vkCmdCopyBuffer(commandBuffer, stagingBuffer, gpuData.buffer.vk, 1, addr(copyRegion)) | |
460 | |
1179 | 461 vkDestroyBuffer(vulkan.device, stagingBuffer, nil) |
462 vkFreeMemory(vulkan.device, stagingMemory, nil) | |
463 | |
464 proc UpdateAllGPUBuffers[T](value: T) = | |
465 for name, fieldvalue in value.fieldPairs(): | |
466 when typeof(fieldvalue) is GPUData: | |
467 UpdateGPUBuffer(fieldvalue) | |
1177 | 468 |
1180 | 469 proc AssertCompatible(TShader, TDescriptorSet: typedesc, DescriptorSetIndex: static int) = |
470 ForDescriptorSets(default(TShader), setNumber, descriptorSet): | |
471 if setNumber == DescriptorSetIndex: | |
472 assert typeof(descriptorSet) is TDescriptorSet | |
473 | |
474 proc CreateDescriptorSet[T, TShader]( | |
475 renderData: RenderData, | |
476 pipeline: Pipeline[TShader], | |
477 value: T, | |
478 setNumber: static int | |
479 ): array[INFLIGHTFRAMES.int, VkDescriptorSet] = | |
480 | |
481 static: AssertCompatible(TShader, T, setNumber) | |
482 | |
483 var layouts = newSeqWith(result.len, pipeline.descriptorSetLayouts[setNumber]) | |
484 var allocInfo = VkDescriptorSetAllocateInfo( | |
485 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
486 descriptorPool: renderData.descriptorPool, | |
487 descriptorSetCount: uint32(layouts.len), | |
488 pSetLayouts: layouts.ToCPointer, | |
489 ) | |
490 checkVkResult vkAllocateDescriptorSets(vulkan.device, addr(allocInfo), result.ToCPointer) | |
491 | |
1161 | 492 converter toVkIndexType(indexType: IndexType): VkIndexType = |
493 case indexType: | |
494 of None: VK_INDEX_TYPE_NONE_KHR | |
495 of UInt8: VK_INDEX_TYPE_UINT8_EXT | |
496 of UInt16: VK_INDEX_TYPE_UINT16 | |
497 of UInt32: VK_INDEX_TYPE_UINT32 | |
1159 | 498 |
1179 | 499 proc CreateRenderPass(format: VkFormat): VkRenderPass = |
1172 | 500 var |
501 attachments = @[VkAttachmentDescription( | |
502 format: format, | |
503 samples: VK_SAMPLE_COUNT_1_BIT, | |
504 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
505 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
506 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
507 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
508 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
509 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
510 )] | |
511 dependencies = @[VkSubpassDependency( | |
512 srcSubpass: VK_SUBPASS_EXTERNAL, | |
513 dstSubpass: 0, | |
514 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], | |
515 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
516 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
517 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
518 )] | |
519 outputs = @[ | |
520 VkAttachmentReference( | |
521 attachment: 0, | |
522 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
523 ) | |
524 ] | |
525 | |
526 var subpassesList = [ | |
527 VkSubpassDescription( | |
528 flags: VkSubpassDescriptionFlags(0), | |
529 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
530 inputAttachmentCount: 0, | |
531 pInputAttachments: nil, | |
532 colorAttachmentCount: uint32(outputs.len), | |
533 pColorAttachments: outputs.ToCPointer, | |
534 pResolveAttachments: nil, | |
535 pDepthStencilAttachment: nil, | |
536 preserveAttachmentCount: 0, | |
537 pPreserveAttachments: nil, | |
538 ) | |
539 ] | |
540 | |
541 var createInfo = VkRenderPassCreateInfo( | |
542 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
543 attachmentCount: uint32(attachments.len), | |
544 pAttachments: attachments.ToCPointer, | |
545 subpassCount: uint32(subpassesList.len), | |
546 pSubpasses: subpassesList.ToCPointer, | |
547 dependencyCount: uint32(dependencies.len), | |
548 pDependencies: dependencies.ToCPointer, | |
549 ) | |
1179 | 550 checkVkResult vulkan.device.vkCreateRenderPass(addr(createInfo), nil, addr(result)) |
1172 | 551 |
1162 | 552 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = |
553 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | |
554 case stage | |
555 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | |
556 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | |
557 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" | |
558 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" | |
559 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" | |
560 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" | |
561 else: "" | |
1161 | 562 |
1162 | 563 when defined(nimcheck): # will not run if nimcheck is running |
564 return result | |
565 | |
566 let | |
567 stagename = stage2string(stage) | |
568 shaderHash = hash(shaderSource) | |
569 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" | |
570 | |
571 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
|
572 echo "shader of type ", stage |
1162 | 573 for i, line in enumerate(shaderSource.splitlines()): |
574 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
|
575 # 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
|
576 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator" |
1162 | 577 when defined(windows): |
578 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
|
579 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" |
1162 | 580 echo "run: ", command |
581 discard StaticExecChecked( | |
582 command = command, | |
583 input = shaderSource | |
584 ) | |
585 else: | |
586 echo &"shaderfile {shaderfile} is up-to-date" | |
587 | |
588 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up | |
589 let shaderbinary = staticRead shaderfile.replace("\\", "/") | |
590 else: | |
591 let shaderbinary = staticRead shaderfile | |
592 | |
593 var i = 0 | |
594 while i < shaderbinary.len: | |
595 result.add( | |
596 (uint32(shaderbinary[i + 0]) shl 0) or | |
597 (uint32(shaderbinary[i + 1]) shl 8) or | |
598 (uint32(shaderbinary[i + 2]) shl 16) or | |
599 (uint32(shaderbinary[i + 3]) shl 24) | |
600 ) | |
601 i += 4 | |
602 | |
603 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = | |
604 const GLSL_VERSION = "450" | |
605 var vsInput: seq[string] | |
606 var vsOutput: seq[string] | |
607 var fsInput: seq[string] | |
608 var fsOutput: seq[string] | |
609 var uniforms: seq[string] | |
610 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
|
611 var vsInputLocation = 0'u32 |
1162 | 612 var passLocation = 0 |
613 var fsOutputLocation = 0 | |
614 | |
1180 | 615 var descriptorSetCount = 0 |
1162 | 616 for fieldname, value in fieldPairs(shader): |
617 # 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
|
618 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
1162 | 619 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
|
620 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" |
1162 | 621 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): |
622 vsInputLocation += NLocationSlots(value) | |
1180 | 623 |
1162 | 624 # 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
|
625 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
|
626 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
|
627 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
|
628 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" |
1162 | 629 passLocation.inc |
1180 | 630 |
631 # fragment shader output | |
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
|
632 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
|
633 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" |
1162 | 634 fsOutputLocation.inc |
1180 | 635 |
636 # descriptor sets | |
637 # need to consider 4 cases: uniform block, texture, uniform block array, texture array | |
638 elif hasCustomPragma(value, DescriptorSet): | |
639 assert descriptorSetCount < MAX_DESCRIPTORSETS, &"{tt.name(TShader)}: maximum {MAX_DESCRIPTORSETS} allowed" | |
640 | |
641 var descriptorBinding = 0 | |
642 for descriptorName, descriptorValue in fieldPairs(value): | |
643 | |
644 when typeof(descriptorValue) is Texture: | |
645 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(descriptorValue) & " " & descriptorName & ";" | |
646 descriptorBinding.inc | |
647 | |
648 elif typeof(descriptorValue) is GPUValue: | |
649 uniforms.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform T" & descriptorName & " {" | |
650 when typeof(descriptorValue.data) is object: | |
651 for blockFieldName, blockFieldValue in descriptorValue.data.fieldPairs(): | |
652 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType" | |
653 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";" | |
654 uniforms.add "} " & descriptorName & ";" | |
655 elif typeof(descriptorValue.data) is array: | |
656 for blockFieldName, blockFieldValue in default(elementType(descriptorValue.data)).fieldPairs(): | |
657 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType" | |
658 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";" | |
659 uniforms.add "} " & descriptorName & "[" & $descriptorValue.data.len & "];" | |
660 descriptorBinding.inc | |
661 elif typeof(descriptorValue) is array: | |
662 when elementType(descriptorValue) is Texture: | |
663 let arrayDecl = "[" & $typeof(descriptorValue).len & "]" | |
664 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(descriptorValue))) & " " & descriptorName & "" & arrayDecl & ";" | |
665 descriptorBinding.inc | |
666 else: | |
667 {.error: "Unsupported shader descriptor field " & descriptorName.} | |
668 descriptorSetCount.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
|
669 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
|
670 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
|
671 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
|
672 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).} |
1162 | 673 |
674 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
675 vsInput & | |
676 uniforms & | |
677 samplers & | |
678 vsOutput & | |
679 @[shader.vertexCode]).join("\n") | |
680 | |
681 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
682 fsInput & | |
683 uniforms & | |
684 samplers & | |
685 fsOutput & | |
686 @[shader.fragmentCode]).join("\n") | |
687 | |
1179 | 688 proc CompileShader[TShader](shader: static TShader): ShaderObject[TShader] = |
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
|
689 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
|
690 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
691 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
|
692 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
|
693 |
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
694 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
|
695 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
|
696 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
|
697 pCode: vertexBinary.ToCPointer, |
1162 | 698 ) |
1179 | 699 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result.vertexShader)) |
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
|
700 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
|
701 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
|
702 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
|
703 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
|
704 ) |
1179 | 705 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader)) |
1162 | 706 |
707 | |
1179 | 708 proc CreatePipeline[TShader]( |
1159 | 709 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
|
710 shader: ShaderObject[TShader], |
1159 | 711 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, |
712 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, | |
713 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT, | |
714 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, | |
1176 | 715 descriptorPoolLimit = 1024 |
1162 | 716 ): Pipeline[TShader] = |
1164 | 717 # create pipeline |
1180 | 718 |
719 ForDescriptorSets(default(TShader), setNumber, descriptorSet): | |
720 var layoutbindings: seq[VkDescriptorSetLayoutBinding] | |
721 ForDescriptorFields(descriptorSet, fieldName, descriptorType, descriptorCount, descriptorBindingNumber): | |
722 layoutbindings.add VkDescriptorSetLayoutBinding( | |
723 binding: descriptorBindingNumber, | |
724 descriptorType: descriptorType, | |
725 descriptorCount: descriptorCount, | |
726 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), | |
727 pImmutableSamplers: nil, | |
728 ) | |
729 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( | |
730 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
731 bindingCount: layoutbindings.len.uint32, | |
732 pBindings: layoutbindings.ToCPointer | |
1161 | 733 ) |
1180 | 734 checkVkResult vkCreateDescriptorSetLayout( |
735 vulkan.device, | |
736 addr(layoutCreateInfo), | |
737 nil, | |
738 addr(result.descriptorSetLayouts[setNumber]) | |
739 ) | |
1161 | 740 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( |
741 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
1179 | 742 setLayoutCount: result.descriptorSetLayouts.len.uint32, |
1180 | 743 pSetLayouts: result.descriptorSetLayouts.ToCPointer, |
1161 | 744 # pushConstantRangeCount: uint32(pushConstants.len), |
745 # pPushConstantRanges: pushConstants.ToCPointer, | |
746 ) | |
1179 | 747 checkVkResult vkCreatePipelineLayout(vulkan.device, addr(pipelineLayoutInfo), nil, addr(result.layout)) |
1159 | 748 |
749 let stages = [ | |
750 VkPipelineShaderStageCreateInfo( | |
751 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
752 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
|
753 module: shader.vertexShader, |
1159 | 754 pName: "main", |
755 ), | |
756 VkPipelineShaderStageCreateInfo( | |
757 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
758 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
|
759 module: shader.fragmentShader, |
1159 | 760 pName: "main", |
761 ), | |
762 ] | |
1162 | 763 var |
764 bindings: seq[VkVertexInputBindingDescription] | |
765 attributes: seq[VkVertexInputAttributeDescription] | |
1159 | 766 var inputBindingNumber = 0'u32 |
1162 | 767 var location = 0'u32 |
768 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): | |
1159 | 769 bindings.add VkVertexInputBindingDescription( |
770 binding: inputBindingNumber, | |
771 stride: sizeof(value).uint32, | |
772 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX, | |
773 ) | |
774 # allows to submit larger data structures like Mat44, for most other types will be 1 | |
775 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value) | |
776 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value): | |
777 attributes.add VkVertexInputAttributeDescription( | |
778 binding: inputBindingNumber, | |
1162 | 779 location: location, |
1159 | 780 format: VkType(value), |
781 offset: i * perDescriptorSize, | |
782 ) | |
1162 | 783 location += NLocationSlots(value) |
1159 | 784 inc inputBindingNumber |
785 | |
786 let | |
787 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
788 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
789 vertexBindingDescriptionCount: uint32(bindings.len), | |
790 pVertexBindingDescriptions: bindings.ToCPointer, | |
791 vertexAttributeDescriptionCount: uint32(attributes.len), | |
792 pVertexAttributeDescriptions: attributes.ToCPointer, | |
793 ) | |
794 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
795 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
796 topology: topology, | |
797 primitiveRestartEnable: false, | |
798 ) | |
799 viewportState = VkPipelineViewportStateCreateInfo( | |
800 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | |
801 viewportCount: 1, | |
802 scissorCount: 1, | |
803 ) | |
804 rasterizer = VkPipelineRasterizationStateCreateInfo( | |
805 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | |
806 depthClampEnable: VK_FALSE, | |
807 rasterizerDiscardEnable: VK_FALSE, | |
808 polygonMode: polygonMode, | |
809 lineWidth: 1.0, | |
810 cullMode: toBits [cullMode], | |
811 frontFace: frontFace, | |
812 depthBiasEnable: VK_FALSE, | |
813 depthBiasConstantFactor: 0.0, | |
814 depthBiasClamp: 0.0, | |
815 depthBiasSlopeFactor: 0.0, | |
816 ) | |
817 multisampling = VkPipelineMultisampleStateCreateInfo( | |
818 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | |
819 sampleShadingEnable: VK_FALSE, | |
820 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, | |
821 minSampleShading: 1.0, | |
822 pSampleMask: nil, | |
823 alphaToCoverageEnable: VK_FALSE, | |
824 alphaToOneEnable: VK_FALSE, | |
825 ) | |
826 colorBlendAttachment = VkPipelineColorBlendAttachmentState( | |
827 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT], | |
828 blendEnable: VK_TRUE, | |
829 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, | |
830 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | |
831 colorBlendOp: VK_BLEND_OP_ADD, | |
832 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, | |
833 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, | |
834 alphaBlendOp: VK_BLEND_OP_ADD, | |
835 ) | |
836 colorBlending = VkPipelineColorBlendStateCreateInfo( | |
837 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | |
838 logicOpEnable: false, | |
839 attachmentCount: 1, | |
840 pAttachments: addr(colorBlendAttachment), | |
841 ) | |
842 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
843 dynamicState = VkPipelineDynamicStateCreateInfo( | |
844 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
845 dynamicStateCount: dynamicStates.len.uint32, | |
846 pDynamicStates: dynamicStates.ToCPointer, | |
847 ) | |
848 let createInfo = VkGraphicsPipelineCreateInfo( | |
849 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | |
850 stageCount: 2, | |
1162 | 851 pStages: stages.ToCPointer, |
1159 | 852 pVertexInputState: addr(vertexInputInfo), |
853 pInputAssemblyState: addr(inputAssembly), | |
854 pViewportState: addr(viewportState), | |
855 pRasterizationState: addr(rasterizer), | |
856 pMultisampleState: addr(multisampling), | |
857 pDepthStencilState: nil, | |
858 pColorBlendState: addr(colorBlending), | |
859 pDynamicState: addr(dynamicState), | |
860 layout: result.layout, | |
861 renderPass: renderPass, | |
862 subpass: 0, | |
863 basePipelineHandle: VkPipeline(0), | |
864 basePipelineIndex: -1, | |
865 ) | |
866 checkVkResult vkCreateGraphicsPipelines( | |
1179 | 867 vulkan.device, |
1159 | 868 VkPipelineCache(0), |
869 1, | |
870 addr(createInfo), | |
871 nil, | |
872 addr(result.pipeline) | |
873 ) | |
874 | |
1179 | 875 proc AllocateIndirectMemory(size: uint64): IndirectGPUMemory = |
1176 | 876 # chooses biggest memory type that has NOT VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1177 | 877 result.size = size |
878 result.needsTransfer = true | |
1176 | 879 |
880 # find a good memory type | |
881 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
1179 | 882 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties) |
1176 | 883 |
1177 | 884 var biggestHeap: uint64 = 0 |
885 var memoryTypeIndex = high(uint32) | |
1176 | 886 # try to find non-host-visible type |
1177 | 887 for i in 0'u32 ..< physicalProperties.memoryTypeCount: |
888 if not (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in toEnums(physicalProperties.memoryTypes[i].propertyFlags)): | |
1176 | 889 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size |
890 if size > biggestHeap: | |
1177 | 891 biggestHeap = size |
1176 | 892 memoryTypeIndex = i |
893 | |
894 # If we did not found a device-only memory type, let's just take the biggest overall | |
1177 | 895 if memoryTypeIndex == high(uint32): |
896 result.needsTransfer = false | |
897 for i in 0'u32 ..< physicalProperties.memoryTypeCount: | |
898 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
899 if size > biggestHeap: | |
900 biggestHeap = size | |
901 memoryTypeIndex = i | |
1176 | 902 |
1177 | 903 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type" |
1176 | 904 var allocationInfo = VkMemoryAllocateInfo( |
905 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
1177 | 906 allocationSize: result.size, |
1176 | 907 memoryTypeIndex: memoryTypeIndex, |
908 ) | |
909 checkVkResult vkAllocateMemory( | |
1179 | 910 vulkan.device, |
1176 | 911 addr allocationInfo, |
912 nil, | |
913 addr result.vk | |
914 ) | |
915 | |
1179 | 916 proc AllocateDirectMemory(size: uint64): DirectGPUMemory = |
1177 | 917 result.size = size |
918 result.needsFlush = true | |
1176 | 919 |
920 # find a good memory type | |
921 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
1179 | 922 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties) |
1164 | 923 |
1177 | 924 var biggestHeap: uint64 = 0 |
925 var memoryTypeIndex = high(uint32) | |
1176 | 926 # try to find host-visible type |
927 for i in 0 ..< physicalProperties.memoryTypeCount: | |
1177 | 928 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags) |
929 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags: | |
1176 | 930 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size |
931 if size > biggestHeap: | |
1177 | 932 biggestHeap = size |
1176 | 933 memoryTypeIndex = i |
1177 | 934 result.needsFlush = not (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT in flags) |
1164 | 935 |
1177 | 936 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type" |
1176 | 937 var allocationInfo = VkMemoryAllocateInfo( |
938 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
1177 | 939 allocationSize: result.size, |
1179 | 940 memoryTypeIndex: GetDirectMemoryTypeIndex(), |
1176 | 941 ) |
942 checkVkResult vkAllocateMemory( | |
1179 | 943 vulkan.device, |
1176 | 944 addr allocationInfo, |
945 nil, | |
946 addr result.vk | |
947 ) | |
1177 | 948 checkVkResult vkMapMemory( |
1179 | 949 device = vulkan.device, |
1176 | 950 memory = result.vk, |
951 offset = 0'u64, | |
952 size = result.size, | |
953 flags = VkMemoryMapFlags(0), | |
954 ppData = addr(result.data) | |
955 ) | |
956 | |
1179 | 957 proc AllocateIndirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) = |
958 if size == 0: | |
959 return | |
1177 | 960 var buffer = Buffer[IndirectGPUMemory](size: size) |
961 | |
1178 | 962 let usageFlags = case btype: |
963 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
964 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
965 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
966 | |
1177 | 967 # iterate through memory areas to find big enough free space |
1179 | 968 # TODO: dynamically expand memory allocations |
969 for (memory, usedOffset) in renderData.indirectMemory.mitems: | |
970 if memory.size - usedOffset >= size: | |
971 buffer.offset = usedOffset | |
1177 | 972 # create buffer |
973 var createInfo = VkBufferCreateInfo( | |
974 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
975 flags: VkBufferCreateFlags(0), | |
976 size: buffer.size, | |
1178 | 977 usage: toBits(usageFlags), |
1177 | 978 sharingMode: VK_SHARING_MODE_EXCLUSIVE, |
979 ) | |
980 checkVkResult vkCreateBuffer( | |
1179 | 981 device = vulkan.device, |
1177 | 982 pCreateInfo = addr createInfo, |
983 pAllocator = nil, | |
984 pBuffer = addr(buffer.vk) | |
985 ) | |
1179 | 986 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset) |
1178 | 987 renderData.indirectBuffers.add (buffer, btype, 0'u64) |
1177 | 988 # update memory area offset |
1179 | 989 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT) |
1177 | 990 return |
991 | |
992 assert false, "Did not find allocated memory region with enough space" | |
993 | |
1179 | 994 proc AllocateDirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) = |
995 if size == 0: | |
996 return | |
997 | |
1177 | 998 var buffer = Buffer[DirectGPUMemory](size: size) |
999 | |
1178 | 1000 let usageFlags = case btype: |
1001 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
1002 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
1003 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
1004 | |
1177 | 1005 # iterate through memory areas to find big enough free space |
1179 | 1006 # TODO: dynamically expand memory allocations |
1007 for (memory, usedOffset) in renderData.directMemory.mitems: | |
1008 if memory.size - usedOffset >= size: | |
1009 buffer.offset = usedOffset | |
1177 | 1010 # create buffer |
1011 var createInfo = VkBufferCreateInfo( | |
1012 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
1013 flags: VkBufferCreateFlags(0), | |
1014 size: buffer.size, | |
1178 | 1015 usage: toBits(usageFlags), |
1177 | 1016 sharingMode: VK_SHARING_MODE_EXCLUSIVE, |
1017 ) | |
1018 checkVkResult vkCreateBuffer( | |
1179 | 1019 device = vulkan.device, |
1177 | 1020 pCreateInfo = addr createInfo, |
1021 pAllocator = nil, | |
1022 pBuffer = addr(buffer.vk) | |
1023 ) | |
1179 | 1024 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset) |
1178 | 1025 renderData.directBuffers.add (buffer, btype, 0'u64) |
1177 | 1026 # update memory area offset |
1179 | 1027 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT) |
1177 | 1028 return |
1029 | |
1030 assert false, "Did not find allocated memory region with enough space" | |
1031 | |
1179 | 1032 proc InitRenderData(descriptorPoolLimit = 1024'u32): RenderData = |
1176 | 1033 # allocate descriptor pools |
1034 var poolSizes = [ | |
1177 | 1035 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit), |
1036 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit), | |
1176 | 1037 ] |
1038 var poolInfo = VkDescriptorPoolCreateInfo( | |
1039 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
1040 poolSizeCount: poolSizes.len.uint32, | |
1041 pPoolSizes: poolSizes.ToCPointer, | |
1042 maxSets: descriptorPoolLimit, | |
1043 ) | |
1179 | 1044 checkVkResult vkCreateDescriptorPool(vulkan.device, addr(poolInfo), nil, addr(result.descriptorPool)) |
1176 | 1045 |
1046 # allocate some memory | |
1177 | 1047 var initialAllocationSize = 1_000_000_000'u64 # TODO: make this more dynamic or something |
1179 | 1048 result.indirectMemory = @[(memory: AllocateIndirectMemory(size = initialAllocationSize), usedOffset: 0'u64)] |
1049 result.directMemory = @[(memory: AllocateDirectMemory(size = initialAllocationSize), usedOffset: 0'u64)] | |
1050 | |
1051 proc FlushDirectMemory(renderData: RenderData) = | |
1052 var flushRegions = newSeqOfCap[VkMappedMemoryRange](renderData.directMemory.len) | |
1053 for entry in renderData.directMemory: | |
1054 if entry.usedOffset > 0: | |
1055 flushRegions.add VkMappedMemoryRange( | |
1056 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, | |
1057 memory: entry.memory.vk, | |
1058 size: entry.usedOffset, | |
1059 ) | |
1060 if flushRegions.len > 0: | |
1061 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, flushRegions.len.uint32, flushRegions.ToCPointer()) | |
1164 | 1062 |
1178 | 1063 # For the Get*BufferSize: |
1064 # BUFFER_ALIGNMENT is just added for a rough estimate, to ensure we have enough space to align when binding | |
1177 | 1065 proc GetIndirectBufferSizes[T](data: T): uint64 = |
1066 for name, value in fieldPairs(data): | |
1178 | 1067 when not hasCustomPragma(value, VertexIndices): |
1068 when typeof(value) is GPUData: | |
1069 when UsesIndirectMemory(value): | |
1070 result += value.size + BUFFER_ALIGNMENT | |
1177 | 1071 proc GetDirectBufferSizes[T](data: T): uint64 = |
1072 for name, value in fieldPairs(data): | |
1178 | 1073 when not hasCustomPragma(value, VertexIndices): |
1074 when typeof(value) is GPUData: | |
1075 when UsesDirectMemory(value): | |
1076 result += value.size + BUFFER_ALIGNMENT | |
1177 | 1077 proc GetIndirectIndexBufferSizes[T](data: T): uint64 = |
1078 for name, value in fieldPairs(data): | |
1079 when hasCustomPragma(value, VertexIndices): | |
1080 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray" | |
1081 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32 | |
1178 | 1082 when UsesIndirectMemory(value): |
1083 result += value.size + BUFFER_ALIGNMENT | |
1177 | 1084 proc GetDirectIndexBufferSizes[T](data: T): uint64 = |
1085 for name, value in fieldPairs(data): | |
1086 when hasCustomPragma(value, VertexIndices): | |
1087 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray" | |
1088 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32 | |
1178 | 1089 when UsesDirectMemory(value): |
1090 result += value.size + BUFFER_ALIGNMENT | |
1177 | 1091 |
1179 | 1092 proc AssignIndirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) = |
1178 | 1093 for name, value in fieldPairs(data): |
1094 when typeof(value) is GPUData: | |
1095 when UsesIndirectMemory(value): | |
1096 # find next buffer of correct type with enough free space | |
1179 | 1097 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices): |
1098 var foundBuffer = false | |
1099 for (buffer, bt, offset) in renderData.indirectBuffers.mitems: | |
1100 if bt == btype and buffer.size - offset >= value.size: | |
1101 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned" | |
1102 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created" | |
1103 value.buffer = buffer | |
1104 value.offset = offset | |
1105 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT) | |
1106 foundBuffer = true | |
1107 break | |
1108 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'" | |
1109 proc AssignDirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) = | |
1178 | 1110 for name, value in fieldPairs(data): |
1111 when typeof(value) is GPUData: | |
1112 when UsesDirectMemory(value): | |
1113 # find next buffer of correct type with enough free space | |
1179 | 1114 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices): |
1115 var foundBuffer = false | |
1116 for (buffer, bt, offset) in renderData.directBuffers.mitems: | |
1117 if bt == btype and buffer.size - offset >= value.size: | |
1118 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned" | |
1119 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created" | |
1120 value.buffer = buffer | |
1121 value.offset = offset | |
1122 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT) | |
1123 foundBuffer = true | |
1124 break | |
1125 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'" | |
1177 | 1126 |
1179 | 1127 proc HasGPUValueField[T](name: static string): bool {.compileTime.} = |
1128 for fieldname, value in default(T).fieldPairs(): | |
1129 when typeof(value) is GPUValue and fieldname == name: return true | |
1130 return false | |
1131 | |
1132 template WithGPUValueField(obj: object, name: static string, fieldvalue, body: untyped): untyped = | |
1133 # HasGPUValueField MUST be used to check if this is supported | |
1134 for fieldname, value in obj.fieldPairs(): | |
1135 when fieldname == name: | |
1136 block: | |
1137 let `fieldvalue` {.inject.} = value | |
1138 body | |
1139 | |
1140 proc WriteDescriptors[TShader, TUniforms, TGlobals](renderData: RenderData, uniforms: TUniforms, globals: TGlobals) = | |
1165 | 1141 var descriptorSetWrites: seq[VkWriteDescriptorSet] |
1176 | 1142 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber): |
1179 | 1143 for frameInFlight in 0 ..< renderData.descriptorSets.len: |
1177 | 1144 when descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
1179 | 1145 when HasGPUValueField[TUniforms](fieldName): |
1146 WithGPUValueField(uniforms, fieldName, gpuValue): | |
1147 let bufferInfo = VkDescriptorBufferInfo( | |
1148 buffer: gpuValue.buffer.vk, | |
1149 offset: gpuValue.buffer.offset, | |
1150 range: gpuValue.buffer.size, | |
1151 ) | |
1152 descriptorSetWrites.add VkWriteDescriptorSet( | |
1153 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
1154 dstSet: renderData.descriptorSets[frameInFlight], | |
1155 dstBinding: descriptorBindingNumber, | |
1156 dstArrayElement: uint32(0), | |
1157 descriptorType: descriptorType, | |
1158 descriptorCount: descriptorCount, | |
1159 pImageInfo: nil, | |
1160 pBufferInfo: addr(bufferInfo), | |
1161 ) | |
1162 elif HasGPUValueField[TGlobals](fieldName): | |
1163 WithGPUValueField(globals, fieldName, theValue): | |
1164 let bufferInfo = VkDescriptorBufferInfo( | |
1165 buffer: theValue.buffer.vk, | |
1166 offset: theValue.buffer.offset, | |
1167 range: theValue.buffer.size, | |
1168 ) | |
1169 descriptorSetWrites.add VkWriteDescriptorSet( | |
1170 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
1171 dstSet: renderData.descriptorSets[frameInFlight], | |
1172 dstBinding: descriptorBindingNumber, | |
1173 dstArrayElement: uint32(0), | |
1174 descriptorType: descriptorType, | |
1175 descriptorCount: descriptorCount, | |
1176 pImageInfo: nil, | |
1177 pBufferInfo: addr(bufferInfo), | |
1178 ) | |
1179 else: | |
1180 {.error: "Unable to find field '" & fieldName & "' in uniforms or globals".} | |
1173 | 1181 elif descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
1182 # TODO | |
1183 let imageInfo = VkDescriptorImageInfo( | |
1184 sampler: VkSampler(0), | |
1185 imageView: VkImageView(0), | |
1186 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, | |
1187 ) | |
1188 descriptorSetWrites.add VkWriteDescriptorSet( | |
1189 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
1179 | 1190 dstSet: renderData.descriptorSets[frameInFlight], |
1173 | 1191 dstBinding: descriptorBindingNumber, |
1179 | 1192 dstArrayElement: 0'u32, |
1173 | 1193 descriptorType: descriptorType, |
1194 descriptorCount: descriptorCount, | |
1195 pImageInfo: addr(imageInfo), | |
1196 pBufferInfo: nil, | |
1197 ) | |
1179 | 1198 else: |
1199 assert false, "Unsupported descriptor type" | |
1200 echo descriptorSetWrites | |
1201 vkUpdateDescriptorSets(vulkan.device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) | |
1165 | 1202 |
1172 | 1203 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
1159 | 1204 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) |
1177 | 1205 #[ |
1206 commandBuffer.vkCmdBindDescriptorSets( | |
1207 VK_PIPELINE_BIND_POINT_GRAPHICS, | |
1208 pipeline.layout, | |
1209 0, | |
1210 1, | |
1211 addr pipeline.descriptorSets[currentFrameInFlight], | |
1212 0, | |
1213 nil, | |
1214 ) | |
1215 ]# | |
1161 | 1216 |
1177 | 1217 proc AssertCompatible(TShader, TMesh, TInstance, TUniforms, TGlobals: typedesc) = |
1180 | 1218 # TODO: overhaul this |
1162 | 1219 for inputName, inputValue in default(TShader).fieldPairs: |
1161 | 1220 var foundField = false |
1176 | 1221 |
1222 # Vertex input data | |
1161 | 1223 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
|
1224 assert typeof(inputValue) is SupportedGPUType |
1161 | 1225 for meshName, meshValue in default(TMesh).fieldPairs: |
1226 when meshName == inputName: | |
1176 | 1227 assert meshValue is GPUArray, "Mesh attribute '" & meshName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(meshValue)) |
1162 | 1228 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1176 | 1229 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 | 1230 foundField = true |
1162 | 1231 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "'" |
1176 | 1232 |
1233 # Instance input data | |
1161 | 1234 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
|
1235 assert typeof(inputValue) is SupportedGPUType |
1161 | 1236 for instanceName, instanceValue in default(TInstance).fieldPairs: |
1237 when instanceName == inputName: | |
1176 | 1238 assert instanceValue is GPUArray, "Instance attribute '" & instanceName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(instanceName)) |
1162 | 1239 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1176 | 1240 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 | 1241 foundField = true |
1162 | 1242 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TInstance) & "'" |
1176 | 1243 |
1244 # Texture | |
1245 elif typeof(inputValue) is Texture: | |
1177 | 1246 for uniformName, uniformValue in default(TUniforms).fieldPairs: |
1247 when uniformName == inputName: | |
1162 | 1248 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1177 | 1249 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 | 1250 foundField = true |
1251 for globalName, globalValue in default(TGlobals).fieldPairs: | |
1252 when globalName == inputName: | |
1162 | 1253 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1254 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 | 1255 foundField = true |
1162 | 1256 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
1176 | 1257 |
1258 # Uniform block | |
1259 elif typeof(inputValue) is object: | |
1177 | 1260 for uniformName, uniformValue in default(TUniforms).fieldPairs: |
1261 when uniformName == inputName: | |
1262 assert uniformValue is GPUValue, "global attribute '" & uniformName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(uniformValue)) | |
1176 | 1263 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1177 | 1264 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 | 1265 foundField = true |
1266 for globalName, globalValue in default(TGlobals).fieldPairs: | |
1267 when globalName == inputName: | |
1268 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue)) | |
1269 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
1270 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)) & "'" | |
1271 foundField = true | |
1272 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
1273 | |
1274 # 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
|
1275 elif typeof(inputValue) is array: |
1176 | 1276 |
1277 # texture-array | |
1278 when elementType(inputValue) is Texture: | |
1177 | 1279 for uniformName, uniformValue in default(TUniforms).fieldPairs: |
1280 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
|
1281 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1177 | 1282 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
|
1283 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
|
1284 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
|
1285 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
|
1286 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
|
1287 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
|
1288 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
|
1289 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
1161 | 1290 |
1176 | 1291 # uniform-block array |
1292 elif elementType(inputValue) is object: | |
1177 | 1293 for uniformName, uniformValue in default(TUniforms).fieldPairs: |
1294 when uniformName == inputName: | |
1295 assert uniformValue is GPUValue, "global attribute '" & uniformName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(uniformValue)) | |
1176 | 1296 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
1177 | 1297 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 | 1298 foundField = true |
1299 for globalName, globalValue in default(TGlobals).fieldPairs: | |
1300 when globalName == inputName: | |
1301 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue)) | |
1302 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
1303 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)) & "'" | |
1304 foundField = true | |
1305 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
1306 | |
1161 | 1307 |
1178 | 1308 proc Render[TShader, TUniforms, TGlobals, TMesh, TInstance]( |
1309 commandBuffer: VkCommandBuffer, | |
1162 | 1310 pipeline: Pipeline[TShader], |
1177 | 1311 uniforms: TUniforms, |
1161 | 1312 globals: TGlobals, |
1178 | 1313 mesh: TMesh, |
1314 instances: TInstance, | |
1161 | 1315 ) = |
1180 | 1316 discard |
1317 # static: AssertCompatible(TShader, TMesh, TInstance, TUniforms, TGlobals) | |
1178 | 1318 #[ |
1164 | 1319 if renderable.vertexBuffers.len > 0: |
1320 commandBuffer.vkCmdBindVertexBuffers( | |
1321 firstBinding = 0'u32, | |
1322 bindingCount = uint32(renderable.vertexBuffers.len), | |
1323 pBuffers = renderable.vertexBuffers.ToCPointer(), | |
1324 pOffsets = renderable.bufferOffsets.ToCPointer() | |
1325 ) | |
1161 | 1326 if renderable.indexType != None: |
1159 | 1327 commandBuffer.vkCmdBindIndexBuffer( |
1328 renderable.indexBuffer, | |
1329 renderable.indexBufferOffset, | |
1161 | 1330 renderable.indexType, |
1159 | 1331 ) |
1332 commandBuffer.vkCmdDrawIndexed( | |
1161 | 1333 indexCount = renderable.indexCount, |
1334 instanceCount = renderable.instanceCount, | |
1159 | 1335 firstIndex = 0, |
1336 vertexOffset = 0, | |
1337 firstInstance = 0 | |
1338 ) | |
1339 else: | |
1340 commandBuffer.vkCmdDraw( | |
1161 | 1341 vertexCount = renderable.vertexCount, |
1342 instanceCount = renderable.instanceCount, | |
1159 | 1343 firstVertex = 0, |
1344 firstInstance = 0 | |
1345 ) | |
1178 | 1346 ]# |
1161 | 1347 |
1348 when isMainModule: | |
1162 | 1349 import semicongine/platform/window |
1350 import semicongine/vulkan/instance | |
1351 import semicongine/vulkan/device | |
1352 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
|
1353 import std/options |
1162 | 1354 |
1161 | 1355 type |
1177 | 1356 MeshA = object |
1357 position: GPUArray[Vec3f, IndirectGPUMemory] | |
1358 indices {.VertexIndices.}: GPUArray[uint16, IndirectGPUMemory] | |
1359 InstanceA = object | |
1360 rotation: GPUArray[Vec4f, IndirectGPUMemory] | |
1361 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
|
1362 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
|
1363 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
|
1364 baseColor: Vec3f |
1176 | 1365 UniformsA = object |
1180 | 1366 defaultTexture: Texture |
1367 defaultMaterial: GPUValue[MaterialA, IndirectGPUMemory] | |
1176 | 1368 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory] |
1369 materialTextures: array[3, Texture] | |
1177 | 1370 ShaderSettings = object |
1180 | 1371 gamma: float32 |
1176 | 1372 GlobalsA = object |
1162 | 1373 fontAtlas: Texture |
1176 | 1374 settings: GPUValue[ShaderSettings, IndirectGPUMemory] |
1161 | 1375 |
1162 | 1376 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
|
1377 # vertex input |
1161 | 1378 position {.VertexAttribute.}: Vec3f |
1176 | 1379 objPosition {.InstanceAttribute.}: Vec3f |
1380 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
|
1381 # intermediate |
1162 | 1382 test {.Pass.}: float32 |
1383 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
|
1384 # output |
1162 | 1385 color {.ShaderOutput.}: Vec4f |
1180 | 1386 # descriptor sets |
1387 globals {.DescriptorSet.}: GlobalsA | |
1388 uniforms {.DescriptorSet.}: UniformsA | |
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
|
1389 # code |
1162 | 1390 vertexCode: string = "void main() {}" |
1391 fragmentCode: string = "void main() {}" | |
1161 | 1392 |
1162 | 1393 let w = CreateWindow("test2") |
1394 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") | |
1179 | 1395 |
1396 # TODO: remove those ugly wrappers | |
1397 let theInstance = w.CreateInstance( | |
1162 | 1398 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), |
1399 instanceExtensions = @[], | |
1400 layers = @["VK_LAYER_KHRONOS_validation"], | |
1401 ) | |
1402 | |
1179 | 1403 let dev = theInstance.CreateDevice( |
1404 theInstance.GetPhysicalDevices().FilterBestGraphics(), | |
1162 | 1405 enabledExtensions = @[], |
1179 | 1406 theInstance.GetPhysicalDevices().FilterBestGraphics().FilterForGraphicsPresentationQueues() |
1407 ).vk | |
1173 | 1408 let frameWidth = 100'u32 |
1409 let frameHeight = 100'u32 | |
1162 | 1410 |
1179 | 1411 # TODO: pack this stuff into a setup method and condense everything a bit |
1412 let pDevice = theInstance.vk.GetPhysicalDevice() | |
1413 let qfi = pDevice.GetQueueFamily(VK_QUEUE_GRAPHICS_BIT) | |
1414 vulkan = VulkanGlobals( | |
1415 instance: theInstance.vk, | |
1416 device: dev, | |
1417 physicalDevice: pDevice, | |
1418 queueFamilyIndex: qfi, | |
1419 queue: dev.GetQueue(qfi, VK_QUEUE_GRAPHICS_BIT) | |
1420 ) | |
1421 | |
1176 | 1422 var myMesh1 = MeshA( |
1423 position: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | |
1424 ) | |
1425 var uniforms1 = UniformsA( | |
1426 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory](data: [ | |
1427 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
1428 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
1429 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
1430 ]), | |
1431 materialTextures: [ | |
1432 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[255'u8, 0'u8, 0'u8, 255'u8]])), | |
1433 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 255'u8, 0'u8, 255'u8]])), | |
1434 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 0'u8, 255'u8, 255'u8]])), | |
1435 ] | |
1436 ) | |
1437 var instances1 = InstanceA( | |
1438 rotation: GPUArray[Vec4f, IndirectGPUMemory](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
1439 objPosition: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
1440 ) | |
1441 var myGlobals: GlobalsA | |
1173 | 1442 |
1443 # setup for rendering (TODO: swapchain & framebuffers) | |
1179 | 1444 let renderpass = CreateRenderPass(GetSurfaceFormat()) |
1173 | 1445 |
1446 # shaders | |
1447 const shader = ShaderA() | |
1179 | 1448 let shaderObject = CompileShader(shader) |
1449 var pipeline1 = CreatePipeline(renderPass = renderpass, shader = shaderObject) | |
1176 | 1450 |
1179 | 1451 var renderdata = InitRenderData() |
1176 | 1452 |
1179 | 1453 # TODO: Textures |
1177 | 1454 # upload all textures |
1455 # write descriptors for textures and uniform buffers | |
1161 | 1456 |
1178 | 1457 # buffer allocation |
1177 | 1458 var |
1459 indirectVertexSizes = 0'u64 | |
1460 directVertexSizes = 0'u64 | |
1461 indirectIndexSizes = 0'u64 | |
1462 directIndexSizes = 0'u64 | |
1463 indirectUniformSizes = 0'u64 | |
1464 directUniformSizes = 0'u64 | |
1465 | |
1179 | 1466 # buffer allocation |
1467 | |
1468 echo "Allocating GPU buffers" | |
1177 | 1469 indirectVertexSizes += GetIndirectBufferSizes(myMesh1) |
1470 indirectVertexSizes += GetIndirectBufferSizes(instances1) | |
1179 | 1471 AllocateIndirectBuffer(renderdata, indirectVertexSizes, VertexBuffer) |
1177 | 1472 |
1473 directVertexSizes += GetDirectBufferSizes(myMesh1) | |
1474 directVertexSizes += GetDirectBufferSizes(instances1) | |
1179 | 1475 AllocateDirectBuffer(renderdata, directVertexSizes, VertexBuffer) |
1177 | 1476 |
1477 indirectIndexSizes += GetIndirectIndexBufferSizes(myMesh1) | |
1179 | 1478 AllocateIndirectBuffer(renderdata, indirectIndexSizes, IndexBuffer) |
1177 | 1479 |
1480 directIndexSizes += GetDirectIndexBufferSizes(myMesh1) | |
1179 | 1481 AllocateDirectBuffer(renderdata, directIndexSizes, IndexBuffer) |
1177 | 1482 |
1483 indirectUniformSizes += GetIndirectBufferSizes(uniforms1) | |
1484 indirectUniformSizes += GetIndirectBufferSizes(myGlobals) | |
1179 | 1485 AllocateIndirectBuffer(renderdata, indirectUniformSizes, UniformBuffer) |
1177 | 1486 |
1487 directUniformSizes += GetDirectBufferSizes(uniforms1) | |
1488 directUniformSizes += GetDirectBufferSizes(myGlobals) | |
1179 | 1489 AllocateDirectBuffer(renderdata, directUniformSizes, UniformBuffer) |
1178 | 1490 |
1491 # buffer assignment | |
1179 | 1492 # |
1493 echo "Assigning buffers to GPUData fields" | |
1178 | 1494 |
1179 | 1495 # for meshes we do: |
1496 renderdata.AssignIndirectBuffers(VertexBuffer, myMesh1) | |
1497 renderdata.AssignDirectBuffers(VertexBuffer, myMesh1) | |
1498 renderdata.AssignIndirectBuffers(IndexBuffer, myMesh1) | |
1499 renderdata.AssignDirectBuffers(IndexBuffer, myMesh1) | |
1500 | |
1501 # for instances we do: | |
1502 renderdata.AssignIndirectBuffers(VertexBuffer, instances1) | |
1503 renderdata.AssignDirectBuffers(VertexBuffer, instances1) | |
1178 | 1504 |
1179 | 1505 # for uniforms/globals we do: |
1506 renderdata.AssignIndirectBuffers(UniformBuffer, uniforms1) | |
1507 renderdata.AssignDirectBuffers(UniformBuffer, uniforms1) | |
1508 renderdata.AssignIndirectBuffers(UniformBuffer, myGlobals) | |
1509 renderdata.AssignDirectBuffers(UniformBuffer, myGlobals) | |
1510 | |
1511 # buffer filling | |
1178 | 1512 |
1179 | 1513 echo "Copying all data to GPU memory" |
1514 | |
1515 # copy everything to GPU | |
1516 UpdateAllGPUBuffers(myMesh1) | |
1517 UpdateAllGPUBuffers(instances1) | |
1518 UpdateAllGPUBuffers(uniforms1) | |
1519 UpdateAllGPUBuffers(myGlobals) | |
1520 renderdata.FlushDirectMemory() | |
1177 | 1521 |
1180 | 1522 var s1 = CreateDescriptorSet(renderdata, pipeline1, myGlobals, 0) |
1523 var s2 = CreateDescriptorSet(renderdata, pipeline1, uniforms1, 1) | |
1524 | |
1173 | 1525 # descriptors |
1180 | 1526 # WriteDescriptors[ShaderA, UniformsA, GlobalsA](renderdata, uniforms1, myGlobals) |
1179 | 1527 |
1528 # create descriptor sets | |
1529 #[ | |
1530 var descriptorSets: array[INFLIGHTFRAMES.int, VkDescriptorSet] | |
1531 var layouts = newSeqWith(descriptorSets.len, pipeline.descriptorSetLayouts) | |
1532 var allocInfo = VkDescriptorSetAllocateInfo( | |
1533 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
1534 descriptorPool: pool, | |
1535 descriptorSetCount: uint32(layouts.len), | |
1536 pSetLayouts: layouts.ToCPointer, | |
1537 ) | |
1538 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), descriptorSets.ToCPointer) | |
1539 ]# | |
1540 | |
1541 | |
1173 | 1542 |
1543 # command buffer | |
1544 var | |
1545 commandBufferPool: VkCommandPool | |
1546 createInfo = VkCommandPoolCreateInfo( | |
1547 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
1548 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
1179 | 1549 queueFamilyIndex: vulkan.queueFamilyIndex, |
1173 | 1550 ) |
1179 | 1551 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool) |
1178 | 1552 var |
1553 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] | |
1554 allocInfo = VkCommandBufferAllocateInfo( | |
1555 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
1556 commandPool: commandBufferPool, | |
1557 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
1558 commandBufferCount: INFLIGHTFRAMES, | |
1559 ) | |
1179 | 1560 checkVkResult vkAllocateCommandBuffers(vulkan.device, 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
|
1561 |
1173 | 1562 # start command buffer |
1563 block: | |
1564 let | |
1565 currentFramebuffer = VkFramebuffer(0) # TODO | |
1566 currentFrameInFlight = 1 | |
1567 cmd = cmdBuffers[currentFrameInFlight] | |
1568 beginInfo = VkCommandBufferBeginInfo( | |
1569 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
1570 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | |
1571 ) | |
1572 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) | |
1573 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
|
1574 |
1173 | 1575 # start renderpass |
1576 block: | |
1577 var | |
1578 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
1579 renderPassInfo = VkRenderPassBeginInfo( | |
1580 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
1581 renderPass: renderpass, | |
1178 | 1582 framebuffer: currentFramebuffer, # TODO |
1173 | 1583 renderArea: VkRect2D( |
1584 offset: VkOffset2D(x: 0, y: 0), | |
1585 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
1586 ), | |
1587 clearValueCount: uint32(clearColors.len), | |
1588 pClearValues: clearColors.ToCPointer(), | |
1589 ) | |
1590 viewport = VkViewport( | |
1591 x: 0.0, | |
1592 y: 0.0, | |
1593 width: frameWidth.float32, | |
1594 height: frameHeight.float32, | |
1595 minDepth: 0.0, | |
1596 maxDepth: 1.0, | |
1597 ) | |
1598 scissor = VkRect2D( | |
1599 offset: VkOffset2D(x: 0, y: 0), | |
1600 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
1601 ) | |
1179 | 1602 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
|
1603 |
1173 | 1604 # setup viewport |
1605 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport)) | |
1606 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
1607 | |
1608 # bind pipeline, will be loop | |
1609 block: | |
1610 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) | |
1611 | |
1612 # render object, will be loop | |
1613 block: | |
1178 | 1614 Render(cmd, pipeline1, uniforms1, myGlobals, myMesh1, instances1) |
1173 | 1615 |
1616 vkCmdEndRenderPass(cmd) | |
1617 checkVkResult cmd.vkEndCommandBuffer() |