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