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