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