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