Mercurial > games > semicongine
annotate static_utils.nim @ 1176:511c9f7cd1da compiletime-tests
sync to notebook in bedroom
| author | sam <sam@basx.dev> |
|---|---|
| date | Sat, 29 Jun 2024 21:04:04 +0700 |
| parents | fafc2f14da0b |
| children | 4ef959278451 |
| 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.} | |
| 1159 | 22 |
| 1165 | 23 const INFLIGHTFRAMES = 2'u32 |
| 1176 | 24 |
| 1159 | 25 type |
| 26 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
|
27 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
|
28 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
|
29 fragmentShader: VkShaderModule |
| 1159 | 30 |
| 31 func VkType[T: SupportedGPUType](value: T): VkFormat = | |
| 32 when T is float32: VK_FORMAT_R32_SFLOAT | |
| 33 elif T is float64: VK_FORMAT_R64_SFLOAT | |
| 34 elif T is int8: VK_FORMAT_R8_SINT | |
| 35 elif T is int16: VK_FORMAT_R16_SINT | |
| 36 elif T is int32: VK_FORMAT_R32_SINT | |
| 37 elif T is int64: VK_FORMAT_R64_SINT | |
| 38 elif T is uint8: VK_FORMAT_R8_UINT | |
| 39 elif T is uint16: VK_FORMAT_R16_UINT | |
| 40 elif T is uint32: VK_FORMAT_R32_UINT | |
| 41 elif T is uint64: VK_FORMAT_R64_UINT | |
| 42 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT | |
| 43 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT | |
| 44 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT | |
| 45 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT | |
| 46 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT | |
| 47 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT | |
| 48 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT | |
| 49 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT | |
| 50 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT | |
| 51 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT | |
| 52 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT | |
| 53 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT | |
| 54 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT | |
| 55 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 56 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 57 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 58 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 59 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 1162 | 60 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT |
| 61 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 62 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 63 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 64 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT | |
| 65 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 66 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 67 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 68 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 69 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 70 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 71 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 72 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 73 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 74 else: {.error: "Unsupported data type on GPU".} | |
| 75 | |
|
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
|
76 func GlslType[T: SupportedGPUType|Texture](value: T): string = |
| 1162 | 77 when T is float32: "float" |
| 78 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
|
79 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
|
80 elif T is uint8 or T is uint16 or T is uint32 or T is uint64: "uint" |
| 1162 | 81 elif T is TVec2[int32]: "ivec2" |
| 82 elif T is TVec2[int64]: "ivec2" | |
| 83 elif T is TVec3[int32]: "ivec3" | |
| 84 elif T is TVec3[int64]: "ivec3" | |
| 85 elif T is TVec4[int32]: "ivec4" | |
| 86 elif T is TVec4[int64]: "ivec4" | |
| 87 elif T is TVec2[uint32]: "uvec2" | |
| 88 elif T is TVec2[uint64]: "uvec2" | |
| 89 elif T is TVec3[uint32]: "uvec3" | |
| 90 elif T is TVec3[uint64]: "uvec3" | |
| 91 elif T is TVec4[uint32]: "uvec4" | |
| 92 elif T is TVec4[uint64]: "uvec4" | |
| 93 elif T is TVec2[float32]: "vec2" | |
| 94 elif T is TVec2[float64]: "dvec2" | |
| 95 elif T is TVec3[float32]: "vec3" | |
| 96 elif T is TVec3[float64]: "dvec3" | |
| 97 elif T is TVec4[float32]: "vec4" | |
| 98 elif T is TVec4[float64]: "dvec4" | |
| 99 elif T is TMat2[float32]: "mat2" | |
| 100 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
|
101 elif T is TMat23[float32]: "mat23" |
| 1162 | 102 elif T is TMat23[float64]: "dmat23" |
| 103 elif T is TMat32[float32]: "mat32" | |
| 104 elif T is TMat32[float64]: "dmat32" | |
| 105 elif T is TMat3[float32]: "mat3" | |
| 106 elif T is TMat3[float64]: "dmat3" | |
| 107 elif T is TMat34[float32]: "mat34" | |
| 108 elif T is TMat34[float64]: "dmat34" | |
| 109 elif T is TMat43[float32]: "mat43" | |
| 110 elif T is TMat43[float64]: "dmat43" | |
| 111 elif T is TMat4[float32]: "mat4" | |
| 112 elif T is TMat4[float64]: "dmat4" | |
| 113 elif T is Texture: "sampler2D" | |
| 1159 | 114 else: {.error: "Unsupported data type on GPU".} |
| 115 | |
| 1161 | 116 template ForVertexDataFields*(inputData: typed, fieldname, valuename, isinstancename, body: untyped): untyped = |
| 1159 | 117 for theFieldname, value in fieldPairs(inputData): |
| 1161 | 118 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
| 1159 | 119 when not typeof(value) is seq: |
| 120 {.error: "field '" & theFieldname & "' needs to be a seq".} | |
| 121 when not typeof(value) is SupportedGPUType: | |
| 122 {.error: "field '" & theFieldname & "' is not a supported GPU type".} | |
| 123 block: | |
| 124 let `fieldname` {.inject.} = theFieldname | |
| 1162 | 125 let `valuename` {.inject.} = value |
| 126 let `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute) | |
| 1159 | 127 body |
| 128 | |
| 1176 | 129 template ForDescriptorFields*(inputData: typed, fieldname, typename, countname, bindingNumber, body: untyped): untyped = |
| 1173 | 130 var `bindingNumber` {.inject.} = 1'u32 |
| 1161 | 131 for theFieldname, value in fieldPairs(inputData): |
| 1176 | 132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 body |
| 1173 | 138 `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
|
139 elif typeof(value) is object: |
| 1161 | 140 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
|
141 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
|
142 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
|
143 body |
| 1173 | 144 `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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 body |
| 1173 | 151 `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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 body |
| 1173 | 157 `bindingNumber`.inc |
| 1161 | 158 |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
159 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 = |
| 1159 | 160 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]: |
| 161 2 | |
| 162 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]: | |
| 163 3 | |
| 164 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]: | |
| 165 4 | |
| 166 else: | |
| 167 1 | |
| 168 | |
| 1162 | 169 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 = |
| 1159 | 170 #[ |
| 171 single location: | |
| 1162 | 172 - any scalar |
| 173 - any 16-bit vector | |
| 174 - any 32-bit vector | |
| 175 - any 64-bit vector that has max. 2 components | |
| 1159 | 176 16-bit scalar and vector types, and |
| 177 32-bit scalar and vector types, and | |
| 178 64-bit scalar and 2-component vector types. | |
| 179 two locations | |
| 180 64-bit three- and four-component vectors | |
| 181 ]# | |
| 1162 | 182 when T is TVec3[int64] or |
| 183 T is TVec4[int64] or | |
| 184 T is TVec3[uint64] or | |
| 185 T is TVec4[uint64] or | |
| 186 T is TVec3[float64] or | |
| 187 T is TVec4[float64] or | |
| 188 T is TMat23[float64] or | |
| 189 T is TMat3[float64] or | |
| 190 T is TMat34[float64] or | |
| 191 T is TMat43[float64] or | |
| 192 T is TMat4[float64]: | |
| 1159 | 193 return 2 |
| 194 else: | |
| 195 return 1 | |
| 196 | |
| 197 type | |
| 1161 | 198 IndexType = enum |
| 199 None, UInt8, UInt16, UInt32 | |
| 1176 | 200 |
| 201 IndirectGPUMemory = object | |
| 202 vk: VkDeviceMemory | |
| 203 size: uint64 | |
| 204 DirectGPUMemory = object | |
| 205 vk: VkDeviceMemory | |
| 206 size: uint64 | |
| 207 data: pointer | |
| 208 GPUMemory = IndirectGPUMemory | DirectGPUMemory | |
| 209 | |
| 210 Buffer[TMemory: GPUMemory] = object | |
| 211 vk: VkBuffer | |
| 212 memory*: TMemory | |
| 213 offset: uint64 | |
| 214 size: uint64 | |
| 215 | |
| 216 GPUArray[T: SupportedGPUType, TMemory: GPUMemory] = object | |
| 217 data: seq[T] | |
| 218 buffer: Buffer[TMemory] | |
| 219 offset: uint64 | |
| 220 GPUValue[T: object|array, TMemory: GPUMemory] = object | |
| 221 data: T | |
| 222 buffer: Buffer[TMemory] | |
| 223 offset: uint64 | |
| 224 | |
| 1161 | 225 Renderable[TMesh, TInstance] = object |
| 226 vertexBuffers: seq[VkBuffer] | |
| 227 bufferOffsets: seq[VkDeviceSize] | |
| 1159 | 228 instanceCount: uint32 |
| 1161 | 229 case indexType: IndexType |
| 230 of None: | |
| 1160 | 231 vertexCount: uint32 |
| 1161 | 232 else: |
| 1160 | 233 indexBuffer: VkBuffer |
| 234 indexCount: uint32 | |
| 235 indexBufferOffset: VkDeviceSize | |
| 1176 | 236 |
| 1162 | 237 Pipeline[TShader] = object |
| 1159 | 238 pipeline: VkPipeline |
| 239 layout: VkPipelineLayout | |
| 1176 | 240 descriptorSetLayout: VkDescriptorSetLayout |
| 241 RenderData = object | |
| 242 descriptorPool: VkDescriptorPool | |
| 243 indirectMemory: seq[IndirectGPUMemory] | |
| 244 nextFreeIndirectMemoryOffset: seq[uint64] | |
| 245 indirectBuffers: seq[Buffer[IndirectGPUMemory]] | |
| 246 directMemory: seq[DirectGPUMemory] | |
| 247 nextFreeDirectMemoryOffset: seq[uint64] | |
| 248 directBuffers: seq[Buffer[DirectGPUMemory]] | |
| 1162 | 249 |
| 1161 | 250 converter toVkIndexType(indexType: IndexType): VkIndexType = |
| 251 case indexType: | |
| 252 of None: VK_INDEX_TYPE_NONE_KHR | |
| 253 of UInt8: VK_INDEX_TYPE_UINT8_EXT | |
| 254 of UInt16: VK_INDEX_TYPE_UINT16 | |
| 255 of UInt32: VK_INDEX_TYPE_UINT32 | |
| 1159 | 256 |
| 1172 | 257 proc CreateRenderPass*( |
| 258 device: VkDevice, | |
| 259 format: VkFormat, | |
| 260 ): VkRenderPass = | |
| 261 | |
| 262 var | |
| 263 attachments = @[VkAttachmentDescription( | |
| 264 format: format, | |
| 265 samples: VK_SAMPLE_COUNT_1_BIT, | |
| 266 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
| 267 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
| 268 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
| 269 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
| 270 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
| 271 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
| 272 )] | |
| 273 dependencies = @[VkSubpassDependency( | |
| 274 srcSubpass: VK_SUBPASS_EXTERNAL, | |
| 275 dstSubpass: 0, | |
| 276 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], | |
| 277 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
| 278 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
| 279 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
| 280 )] | |
| 281 outputs = @[ | |
| 282 VkAttachmentReference( | |
| 283 attachment: 0, | |
| 284 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
| 285 ) | |
| 286 ] | |
| 287 | |
| 288 var subpassesList = [ | |
| 289 VkSubpassDescription( | |
| 290 flags: VkSubpassDescriptionFlags(0), | |
| 291 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
| 292 inputAttachmentCount: 0, | |
| 293 pInputAttachments: nil, | |
| 294 colorAttachmentCount: uint32(outputs.len), | |
| 295 pColorAttachments: outputs.ToCPointer, | |
| 296 pResolveAttachments: nil, | |
| 297 pDepthStencilAttachment: nil, | |
| 298 preserveAttachmentCount: 0, | |
| 299 pPreserveAttachments: nil, | |
| 300 ) | |
| 301 ] | |
| 302 | |
| 303 var createInfo = VkRenderPassCreateInfo( | |
| 304 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
| 305 attachmentCount: uint32(attachments.len), | |
| 306 pAttachments: attachments.ToCPointer, | |
| 307 subpassCount: uint32(subpassesList.len), | |
| 308 pSubpasses: subpassesList.ToCPointer, | |
| 309 dependencyCount: uint32(dependencies.len), | |
| 310 pDependencies: dependencies.ToCPointer, | |
| 311 ) | |
| 312 checkVkResult device.vkCreateRenderPass(addr(createInfo), nil, addr(result)) | |
| 313 | |
| 1162 | 314 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = |
| 315 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | |
| 316 case stage | |
| 317 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | |
| 318 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | |
| 319 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" | |
| 320 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" | |
| 321 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" | |
| 322 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" | |
| 323 else: "" | |
| 1161 | 324 |
| 1162 | 325 when defined(nimcheck): # will not run if nimcheck is running |
| 326 return result | |
| 327 | |
| 328 let | |
| 329 stagename = stage2string(stage) | |
| 330 shaderHash = hash(shaderSource) | |
| 331 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" | |
| 332 | |
| 333 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
|
334 echo "shader of type ", stage |
| 1162 | 335 for i, line in enumerate(shaderSource.splitlines()): |
| 336 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
|
337 # 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
|
338 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator" |
| 1162 | 339 when defined(windows): |
| 340 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
|
341 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" |
| 1162 | 342 echo "run: ", command |
| 343 discard StaticExecChecked( | |
| 344 command = command, | |
| 345 input = shaderSource | |
| 346 ) | |
| 347 else: | |
| 348 echo &"shaderfile {shaderfile} is up-to-date" | |
| 349 | |
| 350 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up | |
| 351 let shaderbinary = staticRead shaderfile.replace("\\", "/") | |
| 352 else: | |
| 353 let shaderbinary = staticRead shaderfile | |
| 354 | |
| 355 var i = 0 | |
| 356 while i < shaderbinary.len: | |
| 357 result.add( | |
| 358 (uint32(shaderbinary[i + 0]) shl 0) or | |
| 359 (uint32(shaderbinary[i + 1]) shl 8) or | |
| 360 (uint32(shaderbinary[i + 2]) shl 16) or | |
| 361 (uint32(shaderbinary[i + 3]) shl 24) | |
| 362 ) | |
| 363 i += 4 | |
| 364 | |
| 365 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = | |
| 366 const GLSL_VERSION = "450" | |
| 367 var vsInput: seq[string] | |
| 368 var vsOutput: seq[string] | |
| 369 var fsInput: seq[string] | |
| 370 var fsOutput: seq[string] | |
| 371 var uniforms: seq[string] | |
| 372 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
|
373 var vsInputLocation = 0'u32 |
| 1162 | 374 var passLocation = 0 |
| 375 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
|
376 var descriptorBinding = 0 |
| 1162 | 377 |
| 378 for fieldname, value in fieldPairs(shader): | |
| 379 # 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
|
380 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
| 1162 | 381 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
|
382 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" |
| 1162 | 383 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): |
| 384 vsInputLocation += NLocationSlots(value) | |
| 385 # 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
|
386 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
|
387 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
|
388 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
|
389 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" |
| 1162 | 390 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
|
391 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
|
392 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" |
| 1162 | 393 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
|
394 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
|
395 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
|
396 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
|
397 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
|
398 # 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
|
399 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
|
400 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
|
401 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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 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
|
407 # 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
|
408 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
|
409 # 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
|
410 descriptorBinding.inc |
| 1162 | 411 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
|
412 {.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
|
413 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
|
414 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
|
415 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
|
416 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).} |
| 1162 | 417 |
| 418 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
| 419 vsInput & | |
| 420 uniforms & | |
| 421 samplers & | |
| 422 vsOutput & | |
| 423 @[shader.vertexCode]).join("\n") | |
| 424 | |
| 425 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
| 426 fsInput & | |
| 427 uniforms & | |
| 428 samplers & | |
| 429 fsOutput & | |
| 430 @[shader.fragmentCode]).join("\n") | |
| 431 | |
|
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
|
432 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
|
433 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
|
434 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
435 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
|
436 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
|
437 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
438 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
|
439 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
|
440 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
|
441 pCode: vertexBinary.ToCPointer, |
| 1162 | 442 ) |
|
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
|
443 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
|
444 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
|
445 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
|
446 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
|
447 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
|
448 ) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
449 checkVkResult device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader)) |
| 1162 | 450 |
| 451 | |
| 452 proc CreatePipeline*[TShader]( | |
| 1159 | 453 device: VkDevice, |
| 454 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
|
455 shader: ShaderObject[TShader], |
| 1159 | 456 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, |
| 457 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, | |
| 458 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT, | |
| 459 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, | |
| 1176 | 460 descriptorPoolLimit = 1024 |
| 1162 | 461 ): Pipeline[TShader] = |
| 1164 | 462 # create pipeline |
| 1161 | 463 var layoutbindings: seq[VkDescriptorSetLayoutBinding] |
| 1176 | 464 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber): |
| 1161 | 465 layoutbindings.add VkDescriptorSetLayoutBinding( |
| 466 binding: descriptorBindingNumber, | |
| 467 descriptorType: descriptorType, | |
| 468 descriptorCount: descriptorCount, | |
| 1162 | 469 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), |
| 1161 | 470 pImmutableSamplers: nil, |
| 471 ) | |
| 472 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( | |
| 473 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
| 474 bindingCount: uint32(layoutbindings.len), | |
| 475 pBindings: layoutbindings.ToCPointer | |
| 476 ) | |
| 1176 | 477 result.descriptorSetLayout: VkDescriptorSetLayout |
| 478 checkVkResult vkCreateDescriptorSetLayout(device, addr(layoutCreateInfo), nil, addr(result.descriptorSetLayout)) | |
| 1161 | 479 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( |
| 480 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
| 481 setLayoutCount: 1, | |
| 1176 | 482 pSetLayouts: addr(result.descriptorSetLayout), |
| 1161 | 483 # pushConstantRangeCount: uint32(pushConstants.len), |
| 484 # pPushConstantRanges: pushConstants.ToCPointer, | |
| 485 ) | |
| 486 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil, addr(result.layout)) | |
| 1159 | 487 |
| 488 let stages = [ | |
| 489 VkPipelineShaderStageCreateInfo( | |
| 490 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 491 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
|
492 module: shader.vertexShader, |
| 1159 | 493 pName: "main", |
| 494 ), | |
| 495 VkPipelineShaderStageCreateInfo( | |
| 496 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 497 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
|
498 module: shader.fragmentShader, |
| 1159 | 499 pName: "main", |
| 500 ), | |
| 501 ] | |
| 1162 | 502 var |
| 503 bindings: seq[VkVertexInputBindingDescription] | |
| 504 attributes: seq[VkVertexInputAttributeDescription] | |
| 1159 | 505 var inputBindingNumber = 0'u32 |
| 1162 | 506 var location = 0'u32 |
| 507 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): | |
| 1159 | 508 bindings.add VkVertexInputBindingDescription( |
| 509 binding: inputBindingNumber, | |
| 510 stride: sizeof(value).uint32, | |
| 511 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX, | |
| 512 ) | |
| 513 # allows to submit larger data structures like Mat44, for most other types will be 1 | |
| 514 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value) | |
| 515 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value): | |
| 516 attributes.add VkVertexInputAttributeDescription( | |
| 517 binding: inputBindingNumber, | |
| 1162 | 518 location: location, |
| 1159 | 519 format: VkType(value), |
| 520 offset: i * perDescriptorSize, | |
| 521 ) | |
| 1162 | 522 location += NLocationSlots(value) |
| 1159 | 523 inc inputBindingNumber |
| 524 | |
| 525 let | |
| 526 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
| 527 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
| 528 vertexBindingDescriptionCount: uint32(bindings.len), | |
| 529 pVertexBindingDescriptions: bindings.ToCPointer, | |
| 530 vertexAttributeDescriptionCount: uint32(attributes.len), | |
| 531 pVertexAttributeDescriptions: attributes.ToCPointer, | |
| 532 ) | |
| 533 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
| 534 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
| 535 topology: topology, | |
| 536 primitiveRestartEnable: false, | |
| 537 ) | |
| 538 viewportState = VkPipelineViewportStateCreateInfo( | |
| 539 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | |
| 540 viewportCount: 1, | |
| 541 scissorCount: 1, | |
| 542 ) | |
| 543 rasterizer = VkPipelineRasterizationStateCreateInfo( | |
| 544 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | |
| 545 depthClampEnable: VK_FALSE, | |
| 546 rasterizerDiscardEnable: VK_FALSE, | |
| 547 polygonMode: polygonMode, | |
| 548 lineWidth: 1.0, | |
| 549 cullMode: toBits [cullMode], | |
| 550 frontFace: frontFace, | |
| 551 depthBiasEnable: VK_FALSE, | |
| 552 depthBiasConstantFactor: 0.0, | |
| 553 depthBiasClamp: 0.0, | |
| 554 depthBiasSlopeFactor: 0.0, | |
| 555 ) | |
| 556 multisampling = VkPipelineMultisampleStateCreateInfo( | |
| 557 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | |
| 558 sampleShadingEnable: VK_FALSE, | |
| 559 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, | |
| 560 minSampleShading: 1.0, | |
| 561 pSampleMask: nil, | |
| 562 alphaToCoverageEnable: VK_FALSE, | |
| 563 alphaToOneEnable: VK_FALSE, | |
| 564 ) | |
| 565 colorBlendAttachment = VkPipelineColorBlendAttachmentState( | |
| 566 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT], | |
| 567 blendEnable: VK_TRUE, | |
| 568 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, | |
| 569 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | |
| 570 colorBlendOp: VK_BLEND_OP_ADD, | |
| 571 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, | |
| 572 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, | |
| 573 alphaBlendOp: VK_BLEND_OP_ADD, | |
| 574 ) | |
| 575 colorBlending = VkPipelineColorBlendStateCreateInfo( | |
| 576 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | |
| 577 logicOpEnable: false, | |
| 578 attachmentCount: 1, | |
| 579 pAttachments: addr(colorBlendAttachment), | |
| 580 ) | |
| 581 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
| 582 dynamicState = VkPipelineDynamicStateCreateInfo( | |
| 583 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
| 584 dynamicStateCount: dynamicStates.len.uint32, | |
| 585 pDynamicStates: dynamicStates.ToCPointer, | |
| 586 ) | |
| 587 let createInfo = VkGraphicsPipelineCreateInfo( | |
| 588 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | |
| 589 stageCount: 2, | |
| 1162 | 590 pStages: stages.ToCPointer, |
| 1159 | 591 pVertexInputState: addr(vertexInputInfo), |
| 592 pInputAssemblyState: addr(inputAssembly), | |
| 593 pViewportState: addr(viewportState), | |
| 594 pRasterizationState: addr(rasterizer), | |
| 595 pMultisampleState: addr(multisampling), | |
| 596 pDepthStencilState: nil, | |
| 597 pColorBlendState: addr(colorBlending), | |
| 598 pDynamicState: addr(dynamicState), | |
| 599 layout: result.layout, | |
| 600 renderPass: renderPass, | |
| 601 subpass: 0, | |
| 602 basePipelineHandle: VkPipeline(0), | |
| 603 basePipelineIndex: -1, | |
| 604 ) | |
| 605 checkVkResult vkCreateGraphicsPipelines( | |
| 606 device, | |
| 607 VkPipelineCache(0), | |
| 608 1, | |
| 609 addr(createInfo), | |
| 610 nil, | |
| 611 addr(result.pipeline) | |
| 612 ) | |
| 613 | |
| 1176 | 614 proc AllocateIndirectMemory(device: VkDevice, pDevice: VkPhysicalDevice, allocationSize: uint64): IndirectGPUMemory = |
| 615 # chooses biggest memory type that has NOT VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | |
| 616 result.size = allocationSize | |
| 617 | |
| 618 # find a good memory type | |
| 619 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
| 620 vkGetPhysicalDeviceMemoryProperties(pDevice, addr physicalProperties) | |
| 621 | |
| 622 var biggestHeap: uint64 = -1 | |
| 623 var memoryTypeIndex = -1 | |
| 624 # try to find non-host-visible type | |
| 625 for i in 0 ..< physicalProperties.memoryTypeCount: | |
| 626 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT not in toEnums(physicalProperties.memoryTypes[i].propertyFlags) | |
| 627 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 628 if size > biggestHeap: | |
| 629 biggest = size | |
| 630 memoryTypeIndex = i | |
| 631 | |
| 632 # If we did not found a device-only memory type, let's just take the biggest overall | |
| 633 if memoryTypeIndex < 0: | |
| 634 for i in 0 ..< physicalProperties.memoryTypeCount: | |
| 635 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 636 if size > biggestHeap: | |
| 637 biggest = size | |
| 638 memoryTypeIndex = i | |
| 639 | |
| 640 assert memoryTypeIndex >= 0, "Unable to find indirect memory type" | |
| 641 var allocationInfo = VkMemoryAllocateInfo( | |
| 642 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
| 643 allocationSize: initialAllocationSize, | |
| 644 memoryTypeIndex: memoryTypeIndex, | |
| 645 ) | |
| 646 checkVkResult vkAllocateMemory( | |
| 647 device, | |
| 648 addr allocationInfo, | |
| 649 nil, | |
| 650 addr result.vk | |
| 651 ) | |
| 652 | |
| 653 proc AllocateDirectMemory(device: VkDevice, allocationSize: uint64): DirectGPUMemory = | |
| 654 result.size = allocationSize | |
| 655 | |
| 656 # find a good memory type | |
| 657 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
| 658 vkGetPhysicalDeviceMemoryProperties(pDevice, addr physicalProperties) | |
| 1164 | 659 |
| 1176 | 660 var biggestHeap: uint64 = -1 |
| 661 var memoryTypeIndex = -1 | |
| 662 # try to find host-visible type | |
| 663 for i in 0 ..< physicalProperties.memoryTypeCount: | |
| 664 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in toEnums(physicalProperties.memoryTypes[i].propertyFlags) | |
| 665 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 666 if size > biggestHeap: | |
| 667 biggest = size | |
| 668 memoryTypeIndex = i | |
| 1164 | 669 |
| 1176 | 670 assert memoryTypeIndex >= 0, "Unable to find direct (aka host-visible) memory type" |
| 671 var allocationInfo = VkMemoryAllocateInfo( | |
| 672 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
| 673 allocationSize: initialAllocationSize, | |
| 674 memoryTypeIndex: memoryTypeIndex, | |
| 675 ) | |
| 676 checkVkResult vkAllocateMemory( | |
| 677 device, | |
| 678 addr allocationInfo, | |
| 679 nil, | |
| 680 addr result.vk | |
| 681 ) | |
| 682 checkVkResult result.device.vk.vkMapMemory( | |
| 683 memory = result.vk, | |
| 684 offset = 0'u64, | |
| 685 size = result.size, | |
| 686 flags = VkMemoryMapFlags(0), | |
| 687 ppData = addr(result.data) | |
| 688 ) | |
| 689 | |
| 690 proc InitRenderData(device: VkDevice, descriptorPoolLimit = 1024): RenderData = | |
| 691 # allocate descriptor pools | |
| 692 var poolSizes = [ | |
| 693 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit) | |
| 694 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit) | |
| 695 ] | |
| 696 var poolInfo = VkDescriptorPoolCreateInfo( | |
| 697 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
| 698 poolSizeCount: poolSizes.len.uint32, | |
| 699 pPoolSizes: poolSizes.ToCPointer, | |
| 700 maxSets: descriptorPoolLimit, | |
| 701 ) | |
| 702 checkVkResult vkCreateDescriptorPool(device, addr(poolInfo), nil, addr(result.descriptorPool)) | |
| 703 | |
| 704 # allocate some memory | |
| 705 var initialAllocationSize: 1_000_000_000 # TODO: make this more dynamic or something | |
| 706 result.indirectMemory = @[AllocateIndirectMemory(device, size=initialAllocationSize)] | |
| 707 result.nextFreeIndirectMemoryOffset = @[0'u64] | |
| 708 result.directMemory = @[AllocateDirectMemory(device, size=initialAllocationSize)] | |
| 709 result.nextFreeDirectMemoryOffset = @[0'u64] | |
| 1164 | 710 |
| 1173 | 711 proc WriteDescriptors[TShader](device: VkDevice, pipeline: Pipeline[TShader]) = |
| 1165 | 712 var descriptorSetWrites: seq[VkWriteDescriptorSet] |
| 1176 | 713 # map (buffer + offset + range) to descriptor |
| 714 # map (texture) to descriptor | |
| 715 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber): | |
| 1173 | 716 for frameInFlight in 0 ..< pipeline.descriptorSets.len: |
| 717 if descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: | |
| 718 # TODO | |
| 719 let bufferInfo = VkDescriptorBufferInfo( | |
| 720 buffer: VkBuffer(0), | |
| 721 offset: 0, | |
| 722 range: 1, | |
| 723 ) | |
| 724 descriptorSetWrites.add VkWriteDescriptorSet( | |
| 725 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
| 726 dstSet: pipeline.descriptorSets[frameInFlight], | |
| 727 dstBinding: descriptorBindingNumber, | |
| 728 dstArrayElement: uint32(0), | |
| 729 descriptorType: descriptorType, | |
| 730 descriptorCount: descriptorCount, | |
| 731 pImageInfo: nil, | |
| 732 pBufferInfo: addr(bufferInfo), | |
| 733 ) | |
| 734 elif descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: | |
| 735 # TODO | |
| 736 let imageInfo = VkDescriptorImageInfo( | |
| 737 sampler: VkSampler(0), | |
| 738 imageView: VkImageView(0), | |
| 739 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, | |
| 740 ) | |
| 741 descriptorSetWrites.add VkWriteDescriptorSet( | |
| 742 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | |
| 743 dstSet: pipeline.descriptorSets[frameInFlight], | |
| 744 dstBinding: descriptorBindingNumber, | |
| 745 dstArrayElement: uint32(0), | |
| 746 descriptorType: descriptorType, | |
| 747 descriptorCount: descriptorCount, | |
| 748 pImageInfo: addr(imageInfo), | |
| 749 pBufferInfo: nil, | |
| 750 ) | |
| 751 vkUpdateDescriptorSets(device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) | |
| 1165 | 752 |
| 1161 | 753 proc CreateRenderable[TMesh, TInstance]( |
| 754 mesh: TMesh, | |
| 755 instance: TInstance, | |
| 756 ): Renderable[TMesh, TInstance] = | |
| 757 result.indexType = None | |
| 1159 | 758 |
| 1172 | 759 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
| 760 let a = pipeline.descriptorSets | |
| 1159 | 761 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) |
| 1172 | 762 if a[currentFrameInFlight] != VkDescriptorSet(0): |
| 1164 | 763 commandBuffer.vkCmdBindDescriptorSets( |
| 764 VK_PIPELINE_BIND_POINT_GRAPHICS, | |
| 765 pipeline.layout, | |
| 766 0, | |
| 767 1, | |
| 768 addr pipeline.descriptorSets[currentFrameInFlight], | |
| 769 0, | |
| 770 nil, | |
| 771 ) | |
| 1161 | 772 |
| 1162 | 773 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals: typedesc) = |
| 774 # assert seq-fields of TMesh|TInstance == seq-fields of TShader | |
| 1161 | 775 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors |
| 1162 | 776 for inputName, inputValue in default(TShader).fieldPairs: |
| 1161 | 777 var foundField = false |
| 1176 | 778 |
| 779 # Vertex input data | |
| 1161 | 780 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
|
781 assert typeof(inputValue) is SupportedGPUType |
| 1161 | 782 for meshName, meshValue in default(TMesh).fieldPairs: |
| 783 when meshName == inputName: | |
| 1176 | 784 assert meshValue is GPUArray, "Mesh attribute '" & meshName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(meshValue)) |
| 1162 | 785 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
| 1176 | 786 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 | 787 foundField = true |
| 1162 | 788 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "'" |
| 1176 | 789 |
| 790 # Instance input data | |
| 1161 | 791 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
|
792 assert typeof(inputValue) is SupportedGPUType |
| 1161 | 793 for instanceName, instanceValue in default(TInstance).fieldPairs: |
| 794 when instanceName == inputName: | |
| 1176 | 795 assert instanceValue is GPUArray, "Instance attribute '" & instanceName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(instanceName)) |
| 1162 | 796 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
| 1176 | 797 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 | 798 foundField = true |
| 1162 | 799 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TInstance) & "'" |
| 1176 | 800 |
| 801 # Texture | |
| 802 elif typeof(inputValue) is Texture: | |
| 1161 | 803 for meshName, meshValue in default(TMesh).fieldPairs: |
| 804 when meshName == inputName: | |
| 1162 | 805 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
|
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
|
806 assert typeof(meshValue) 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)) & "'" |
| 1161 | 807 foundField = true |
| 808 for globalName, globalValue in default(TGlobals).fieldPairs: | |
| 809 when globalName == inputName: | |
| 1162 | 810 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
| 811 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 | 812 foundField = true |
| 1162 | 813 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
| 1176 | 814 |
| 815 # Uniform block | |
| 816 elif typeof(inputValue) is object: | |
| 817 for meshName, meshValue in default(TMesh).fieldPairs: | |
| 818 when meshName == inputName: | |
| 819 assert meshValue is GPUValue, "Mesh attribute '" & meshName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(meshValue)) | |
| 820 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 821 assert typeof(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)) & "'" | |
| 822 foundField = true | |
| 823 for globalName, globalValue in default(TGlobals).fieldPairs: | |
| 824 when globalName == inputName: | |
| 825 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue)) | |
| 826 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 827 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)) & "'" | |
| 828 foundField = true | |
| 829 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
| 830 | |
| 831 # 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
|
832 elif typeof(inputValue) is array: |
| 1176 | 833 |
| 834 # texture-array | |
| 835 when elementType(inputValue) is Texture: | |
|
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
|
836 for meshName, meshValue in default(TMesh).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
|
837 when meshName == 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
|
838 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
|
839 assert typeof(meshValue) 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)) & "'" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
840 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
|
841 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
|
842 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
|
843 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
|
844 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
|
845 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
|
846 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
| 1161 | 847 |
| 1176 | 848 # uniform-block array |
| 849 elif elementType(inputValue) is object: | |
| 850 for meshName, meshValue in default(TMesh).fieldPairs: | |
| 851 when meshName == inputName: | |
| 852 assert meshValue is GPUValue, "Mesh attribute '" & meshName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(meshValue)) | |
| 853 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 854 assert typeof(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)) & "'" | |
| 855 foundField = true | |
| 856 for globalName, globalValue in default(TGlobals).fieldPairs: | |
| 857 when globalName == inputName: | |
| 858 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue)) | |
| 859 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 860 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)) & "'" | |
| 861 foundField = true | |
| 862 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
| 863 | |
| 1161 | 864 |
| 1162 | 865 proc Render[TShader, TMesh, TInstance, TGlobals]( |
| 866 pipeline: Pipeline[TShader], | |
| 1161 | 867 renderable: Renderable[TMesh, TInstance], |
| 868 globals: TGlobals, | |
| 869 commandBuffer: VkCommandBuffer, | |
| 870 ) = | |
| 1164 | 871 static: AssertCompatible(TShader, TMesh, TInstance, TGlobals) |
| 872 if renderable.vertexBuffers.len > 0: | |
| 873 commandBuffer.vkCmdBindVertexBuffers( | |
| 874 firstBinding = 0'u32, | |
| 875 bindingCount = uint32(renderable.vertexBuffers.len), | |
| 876 pBuffers = renderable.vertexBuffers.ToCPointer(), | |
| 877 pOffsets = renderable.bufferOffsets.ToCPointer() | |
| 878 ) | |
| 1161 | 879 if renderable.indexType != None: |
| 1159 | 880 commandBuffer.vkCmdBindIndexBuffer( |
| 881 renderable.indexBuffer, | |
| 882 renderable.indexBufferOffset, | |
| 1161 | 883 renderable.indexType, |
| 1159 | 884 ) |
| 885 commandBuffer.vkCmdDrawIndexed( | |
| 1161 | 886 indexCount = renderable.indexCount, |
| 887 instanceCount = renderable.instanceCount, | |
| 1159 | 888 firstIndex = 0, |
| 889 vertexOffset = 0, | |
| 890 firstInstance = 0 | |
| 891 ) | |
| 892 else: | |
| 893 commandBuffer.vkCmdDraw( | |
| 1161 | 894 vertexCount = renderable.vertexCount, |
| 895 instanceCount = renderable.instanceCount, | |
| 1159 | 896 firstVertex = 0, |
| 897 firstInstance = 0 | |
| 898 ) | |
| 1161 | 899 |
| 900 when isMainModule: | |
| 1162 | 901 import semicongine/platform/window |
| 902 import semicongine/vulkan/instance | |
| 903 import semicongine/vulkan/device | |
| 904 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
|
905 import std/options |
| 1162 | 906 |
| 1161 | 907 type |
|
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
|
908 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
|
909 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
|
910 baseColor: Vec3f |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
911 ShaderSettings = 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
|
912 brightness: float32 |
| 1161 | 913 MeshA = object |
| 1176 | 914 position: GPUArray[Vec3f, IndirectGPUMemory] |
| 1161 | 915 InstanceA = object |
| 1176 | 916 rotation: GPUArray[Vec4f, IndirectGPUMemory] |
| 917 objPosition: GPUArray[Vec3f, IndirectGPUMemory] | |
| 918 UniformsA = object | |
| 919 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory] | |
| 920 materialTextures: array[3, Texture] | |
| 921 GlobalsA = object | |
| 1162 | 922 fontAtlas: Texture |
| 1176 | 923 settings: GPUValue[ShaderSettings, IndirectGPUMemory] |
| 1161 | 924 |
| 1162 | 925 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
|
926 # vertex input |
| 1161 | 927 position {.VertexAttribute.}: Vec3f |
| 1176 | 928 objPosition {.InstanceAttribute.}: Vec3f |
| 929 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
|
930 # intermediate |
| 1162 | 931 test {.Pass.}: float32 |
| 932 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
|
933 # output |
| 1162 | 934 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
|
935 # uniforms |
| 1176 | 936 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
|
937 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
|
938 # 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
|
939 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
|
940 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
|
941 # code |
| 1162 | 942 vertexCode: string = "void main() {}" |
| 943 fragmentCode: string = "void main() {}" | |
| 1161 | 944 |
| 1162 | 945 let w = CreateWindow("test2") |
| 946 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") | |
| 947 let i = w.CreateInstance( | |
| 948 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), | |
| 949 instanceExtensions = @[], | |
| 950 layers = @["VK_LAYER_KHRONOS_validation"], | |
| 951 ) | |
| 952 | |
| 953 let selectedPhysicalDevice = i.GetPhysicalDevices().FilterBestGraphics() | |
| 1173 | 954 let dev = i.CreateDevice( |
| 1162 | 955 selectedPhysicalDevice, |
| 956 enabledExtensions = @[], | |
| 957 selectedPhysicalDevice.FilterForGraphicsPresentationQueues() | |
| 958 ) | |
| 1173 | 959 let frameWidth = 100'u32 |
| 960 let frameHeight = 100'u32 | |
| 1162 | 961 |
| 1176 | 962 var myMesh1 = MeshA( |
| 963 position: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | |
| 964 ) | |
| 965 var uniforms1 = UniformsA( | |
| 966 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory](data: [ | |
| 967 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
| 968 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
| 969 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
| 970 ]), | |
| 971 materialTextures: [ | |
| 972 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[255'u8, 0'u8, 0'u8, 255'u8]])), | |
| 973 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 255'u8, 0'u8, 255'u8]])), | |
| 974 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 0'u8, 255'u8, 255'u8]])), | |
| 975 ] | |
| 976 ) | |
| 977 var instances1 = InstanceA( | |
| 978 rotation: GPUArray[Vec4f, IndirectGPUMemory](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
| 979 objPosition: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
| 980 ) | |
| 981 var myGlobals: GlobalsA | |
| 1173 | 982 |
| 983 # setup for rendering (TODO: swapchain & framebuffers) | |
| 984 | |
| 985 # renderpass | |
| 986 let renderpass = dev.vk.CreateRenderPass(dev.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format) | |
| 987 | |
| 988 # shaders | |
| 989 const shader = ShaderA() | |
| 990 let shaderObject = dev.vk.CompileShader(shader) | |
| 991 var pipeline1 = CreatePipeline(dev.vk, renderPass = renderpass, shaderObject) | |
| 992 | |
| 1176 | 993 var renderdata = InitRenderData(dev.vk) |
| 994 | |
| 995 # create descriptor sets | |
| 996 var descriptorSets: array[INFLIGHTFRAMES.int, VkDescriptorSet] | |
| 997 var layouts = newSeqWith(descriptorSets.len, pipeline.descriptorSetLayout) | |
| 998 var allocInfo = VkDescriptorSetAllocateInfo( | |
| 999 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
| 1000 descriptorPool: pool, | |
| 1001 descriptorSetCount: uint32(layouts.len), | |
| 1002 pSetLayouts: layouts.ToCPointer, | |
| 1003 ) | |
| 1004 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), descriptorSets.ToCPointer) | |
| 1005 | |
| 1006 | |
| 1007 #[ | |
| 1173 | 1008 # TODO: probably here: allocate renderables, uniform buffers & textures |
| 1176 | 1009 let meshBuffers: seq[(bool, uint64)] = GetBufferSizes[MeshA](item = myMesh1) |
| 1010 let instanceBuffers: seq[(bool, uint64)] = GetBufferSizes[InstanceA](item = instances1) | |
| 1011 let globalBuffers: seq[(bool, uint64)] = GetBufferSizes[Globals](item = myGlobals) | |
| 1012 var myRenderable = CreateRenderable() | |
| 1013 UploadTextures[MeshA]() | |
| 1014 UploadTextures[Globals]() | |
| 1015 ]# | |
| 1016 var myRenderable: Renderable[MeshA, InstanceA] | |
| 1161 | 1017 |
| 1173 | 1018 # descriptors |
| 1019 WriteDescriptors(dev.vk, pipeline1) | |
| 1020 | |
| 1021 # command buffer | |
| 1022 var | |
| 1023 commandBufferPool: VkCommandPool | |
| 1024 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] | |
| 1025 createInfo = VkCommandPoolCreateInfo( | |
| 1026 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
| 1027 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
| 1028 queueFamilyIndex: dev.FirstGraphicsQueue().get().family.index, | |
| 1029 ) | |
| 1030 checkVkResult vkCreateCommandPool(dev.vk, addr createInfo, nil, addr commandBufferPool) | |
| 1031 var allocInfo = VkCommandBufferAllocateInfo( | |
| 1032 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
| 1033 commandPool: commandBufferPool, | |
| 1034 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
| 1035 commandBufferCount: INFLIGHTFRAMES, | |
| 1036 ) | |
| 1037 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
|
1038 |
| 1173 | 1039 # start command buffer |
| 1040 block: | |
| 1041 let | |
| 1042 currentFramebuffer = VkFramebuffer(0) # TODO | |
| 1043 currentFrameInFlight = 1 | |
| 1044 cmd = cmdBuffers[currentFrameInFlight] | |
| 1045 beginInfo = VkCommandBufferBeginInfo( | |
| 1046 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
| 1047 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | |
| 1048 ) | |
| 1049 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) | |
| 1050 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
|
1051 |
| 1173 | 1052 # start renderpass |
| 1053 block: | |
| 1054 var | |
| 1055 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
| 1056 renderPassInfo = VkRenderPassBeginInfo( | |
| 1057 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
| 1058 renderPass: renderpass, | |
| 1059 framebuffer: currentFramebuffer, | |
| 1060 renderArea: VkRect2D( | |
| 1061 offset: VkOffset2D(x: 0, y: 0), | |
| 1062 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
| 1063 ), | |
| 1064 clearValueCount: uint32(clearColors.len), | |
| 1065 pClearValues: clearColors.ToCPointer(), | |
| 1066 ) | |
| 1067 viewport = VkViewport( | |
| 1068 x: 0.0, | |
| 1069 y: 0.0, | |
| 1070 width: frameWidth.float32, | |
| 1071 height: frameHeight.float32, | |
| 1072 minDepth: 0.0, | |
| 1073 maxDepth: 1.0, | |
| 1074 ) | |
| 1075 scissor = VkRect2D( | |
| 1076 offset: VkOffset2D(x: 0, y: 0), | |
| 1077 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
| 1078 ) | |
| 1079 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
|
1080 |
| 1173 | 1081 # setup viewport |
| 1082 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport)) | |
| 1083 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
| 1084 | |
| 1085 # bind pipeline, will be loop | |
| 1086 block: | |
| 1087 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) | |
| 1088 | |
| 1089 # render object, will be loop | |
| 1090 block: | |
| 1091 Render(pipeline1, myRenderable, myGlobals, cmd) | |
| 1092 | |
| 1093 vkCmdEndRenderPass(cmd) | |
| 1094 checkVkResult cmd.vkEndCommandBuffer() |
