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