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