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