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