Mercurial > games > semicongine
comparison static_utils.nim @ 1182:e9a212e9cdf7 compiletime-tests
sync from bedroom to office
| author | sam <sam@basx.dev> |
|---|---|
| date | Wed, 03 Jul 2024 00:08:19 +0700 |
| parents | 6b66e6c837bc |
| children | 850450bfe2a2 |
comparison
equal
deleted
inserted
replaced
| 1181:6b66e6c837bc | 1182:e9a212e9cdf7 |
|---|---|
| 17 template InstanceAttribute {.pragma.} | 17 template InstanceAttribute {.pragma.} |
| 18 template Pass {.pragma.} | 18 template Pass {.pragma.} |
| 19 template PassFlat {.pragma.} | 19 template PassFlat {.pragma.} |
| 20 template ShaderOutput {.pragma.} | 20 template ShaderOutput {.pragma.} |
| 21 template VertexIndices {.pragma.} | 21 template VertexIndices {.pragma.} |
| 22 template DescriptorSet {.pragma.} | 22 |
| 23 const INFLIGHTFRAMES = 2'u32 | |
| 24 const MEMORY_ALIGNMENT = 65536'u64 # Align buffers inside memory along this alignment | |
| 25 const BUFFER_ALIGNMENT = 64'u64 # align offsets inside buffers along this alignment | |
| 23 | 26 |
| 24 # some globals that will (likely?) never change during the life time of the engine | 27 # some globals that will (likely?) never change during the life time of the engine |
| 25 type | 28 type |
| 26 DescriptorSetType = enum | 29 SupportedGPUType = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64] |
| 27 GlobalSet | 30 |
| 28 MaterialSet | 31 ShaderObject[TShader] = object |
| 32 vertexShader: VkShaderModule | |
| 33 fragmentShader: VkShaderModule | |
| 34 | |
| 29 VulkanGlobals = object | 35 VulkanGlobals = object |
| 30 instance: VkInstance | 36 instance: VkInstance |
| 31 device: VkDevice | 37 device: VkDevice |
| 32 physicalDevice: VkPhysicalDevice | 38 physicalDevice: VkPhysicalDevice |
| 33 queueFamilyIndex: uint32 | 39 queueFamilyIndex: uint32 |
| 34 queue: VkQueue | 40 queue: VkQueue |
| 35 | 41 |
| 36 const INFLIGHTFRAMES = 2'u32 | 42 IndexType = enum |
| 37 const MAX_DESCRIPTORSETS = tt.enumLen(DescriptorSetType) | 43 None, UInt8, UInt16, UInt32 |
| 38 const MEMORY_ALIGNMENT = 65536'u64 # Align buffers inside memory along this alignment | 44 |
| 39 const BUFFER_ALIGNMENT = 64'u64 # align offsets inside buffers along this alignment | 45 IndirectGPUMemory = object |
| 46 vk: VkDeviceMemory | |
| 47 size: uint64 | |
| 48 needsTransfer: bool # usually true | |
| 49 DirectGPUMemory = object | |
| 50 vk: VkDeviceMemory | |
| 51 size: uint64 | |
| 52 data: pointer | |
| 53 needsFlush: bool # usually true | |
| 54 GPUMemory = IndirectGPUMemory | DirectGPUMemory | |
| 55 | |
| 56 Buffer[TMemory: GPUMemory] = object | |
| 57 memory: TMemory | |
| 58 vk: VkBuffer | |
| 59 offset: uint64 | |
| 60 size: uint64 | |
| 61 | |
| 62 GPUArray[T: SupportedGPUType, TMemory: GPUMemory] = object | |
| 63 data: seq[T] | |
| 64 buffer: Buffer[TMemory] | |
| 65 offset: uint64 | |
| 66 GPUValue[T: object|array, TMemory: GPUMemory] = object | |
| 67 data: T | |
| 68 buffer: Buffer[TMemory] | |
| 69 offset: uint64 | |
| 70 GPUData = GPUArray | GPUValue | |
| 71 | |
| 72 DescriptorSetType = enum | |
| 73 GlobalSet | |
| 74 MaterialSet | |
| 75 DescriptorSet[T: object, sType: static DescriptorSetType] = object | |
| 76 data: T | |
| 77 vk: array[INFLIGHTFRAMES, VkDescriptorSet] | |
| 78 | |
| 79 Pipeline[TShader] = object | |
| 80 vk: VkPipeline | |
| 81 layout: VkPipelineLayout | |
| 82 descriptorSetLayouts: array[DescriptorSetType, VkDescriptorSetLayout] | |
| 83 BufferType = enum | |
| 84 VertexBuffer, IndexBuffer, UniformBuffer | |
| 85 RenderData = object | |
| 86 descriptorPool: VkDescriptorPool | |
| 87 # tuple is memory and offset to next free allocation in that memory | |
| 88 indirectMemory: seq[tuple[memory: IndirectGPUMemory, usedOffset: uint64]] | |
| 89 directMemory: seq[tuple[memory: DirectGPUMemory, usedOffset: uint64]] | |
| 90 indirectBuffers: seq[tuple[buffer: Buffer[IndirectGPUMemory], btype: BufferType, usedOffset: uint64]] | |
| 91 directBuffers: seq[tuple[buffer: Buffer[DirectGPUMemory], btype: BufferType, usedOffset: uint64]] | |
| 40 | 92 |
| 41 var vulkan: VulkanGlobals | 93 var vulkan: VulkanGlobals |
| 42 | |
| 43 type | |
| 44 SupportedGPUType = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64] | |
| 45 ShaderObject[TShader] = object | |
| 46 vertexShader: VkShaderModule | |
| 47 fragmentShader: VkShaderModule | |
| 48 | 94 |
| 49 func alignedTo[T: SomeInteger](value: T, alignment: T): T = | 95 func alignedTo[T: SomeInteger](value: T, alignment: T): T = |
| 50 let remainder = value mod alignment | 96 let remainder = value mod alignment |
| 51 if remainder == 0: | 97 if remainder == 0: |
| 52 return value | 98 return value |
| 149 const `fieldname` {.inject.} = theFieldname | 195 const `fieldname` {.inject.} = theFieldname |
| 150 let `valuename` {.inject.} = value | 196 let `valuename` {.inject.} = value |
| 151 const `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute) | 197 const `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute) |
| 152 body | 198 body |
| 153 | 199 |
| 154 template ForDescriptorSets(shader: typed, setNumber, descriptorSet, body: untyped): untyped = | |
| 155 var n = DescriptorSetType.low | |
| 156 for theFieldname, value in fieldPairs(shader): | |
| 157 when value.hasCustomPragma(DescriptorSet): | |
| 158 block: | |
| 159 let `setNumber` {.inject.} = n | |
| 160 let `descriptorSet` {.inject.} = value | |
| 161 body | |
| 162 if n < DescriptorSetType.high: | |
| 163 n.inc | |
| 164 | |
| 165 template ForDescriptorFields(shader: typed, fieldname, typename, countname, bindingNumber, body: untyped): untyped = | 200 template ForDescriptorFields(shader: typed, fieldname, typename, countname, bindingNumber, body: untyped): untyped = |
| 166 var `bindingNumber` {.inject.} = 1'u32 | 201 var `bindingNumber` {.inject.} = 1'u32 |
| 167 for theFieldname, value in fieldPairs(shader): | 202 for theFieldname, value in fieldPairs(shader): |
| 168 when typeof(value) is Texture: | 203 when typeof(value) is Texture: |
| 169 block: | 204 block: |
| 231 T is TMat4[float64]: | 266 T is TMat4[float64]: |
| 232 return 2 | 267 return 2 |
| 233 else: | 268 else: |
| 234 return 1 | 269 return 1 |
| 235 | 270 |
| 236 type | 271 template sType(descriptorSet: DescriptorSet): untyped = |
| 237 IndexType = enum | 272 get(genericParams(typeof(gpuData)), 1) |
| 238 None, UInt8, UInt16, UInt32 | |
| 239 | |
| 240 IndirectGPUMemory = object | |
| 241 vk: VkDeviceMemory | |
| 242 size: uint64 | |
| 243 needsTransfer: bool # usually true | |
| 244 DirectGPUMemory = object | |
| 245 vk: VkDeviceMemory | |
| 246 size: uint64 | |
| 247 data: pointer | |
| 248 needsFlush: bool # usually true | |
| 249 GPUMemory = IndirectGPUMemory | DirectGPUMemory | |
| 250 | |
| 251 Buffer[TMemory: GPUMemory] = object | |
| 252 memory: TMemory | |
| 253 vk: VkBuffer | |
| 254 offset: uint64 | |
| 255 size: uint64 | |
| 256 | |
| 257 GPUArray[T: SupportedGPUType, TMemory: GPUMemory] = object | |
| 258 data: seq[T] | |
| 259 buffer: Buffer[TMemory] | |
| 260 offset: uint64 | |
| 261 GPUValue[T: object|array, TMemory: GPUMemory] = object | |
| 262 data: T | |
| 263 buffer: Buffer[TMemory] | |
| 264 offset: uint64 | |
| 265 GPUData = GPUArray | GPUValue | |
| 266 | |
| 267 Pipeline[TShader] = object | |
| 268 pipeline: VkPipeline | |
| 269 layout: VkPipelineLayout | |
| 270 descriptorSetLayouts: array[MAX_DESCRIPTORSETS, VkDescriptorSetLayout] | |
| 271 BufferType = enum | |
| 272 VertexBuffer, IndexBuffer, UniformBuffer | |
| 273 RenderData = object | |
| 274 descriptorPool: VkDescriptorPool | |
| 275 # tuple is memory and offset to next free allocation in that memory | |
| 276 indirectMemory: seq[tuple[memory: IndirectGPUMemory, usedOffset: uint64]] | |
| 277 directMemory: seq[tuple[memory: DirectGPUMemory, usedOffset: uint64]] | |
| 278 indirectBuffers: seq[tuple[buffer: Buffer[IndirectGPUMemory], btype: BufferType, usedOffset: uint64]] | |
| 279 directBuffers: seq[tuple[buffer: Buffer[DirectGPUMemory], btype: BufferType, usedOffset: uint64]] | |
| 280 | 273 |
| 281 template UsesIndirectMemory(gpuData: GPUData): untyped = | 274 template UsesIndirectMemory(gpuData: GPUData): untyped = |
| 282 get(genericParams(typeof(gpuData)), 1) is IndirectGPUMemory | 275 get(genericParams(typeof(gpuData)), 1) is IndirectGPUMemory |
| 283 template UsesDirectMemory(gpuData: GPUData): untyped = | 276 template UsesDirectMemory(gpuData: GPUData): untyped = |
| 284 get(genericParams(typeof(gpuData)), 1) is DirectGPUMemory | 277 get(genericParams(typeof(gpuData)), 1) is DirectGPUMemory |
| 469 proc UpdateAllGPUBuffers[T](value: T) = | 462 proc UpdateAllGPUBuffers[T](value: T) = |
| 470 for name, fieldvalue in value.fieldPairs(): | 463 for name, fieldvalue in value.fieldPairs(): |
| 471 when typeof(fieldvalue) is GPUData: | 464 when typeof(fieldvalue) is GPUData: |
| 472 UpdateGPUBuffer(fieldvalue) | 465 UpdateGPUBuffer(fieldvalue) |
| 473 | 466 |
| 474 proc AssertCompatible(TShader, TDescriptorSet: typedesc, descriptorSetType: static DescriptorSetType) = | 467 proc InitDescriptorSet( |
| 475 ForDescriptorSets(default(TShader), setNumber, descriptorSet): | |
| 476 if setNumber == descriptorSetType: | |
| 477 assert typeof(descriptorSet) is TDescriptorSet | |
| 478 | |
| 479 proc CreateDescriptorSet[T, TShader]( | |
| 480 renderData: RenderData, | 468 renderData: RenderData, |
| 481 pipeline: Pipeline[TShader], | 469 layout: VkDescriptorSetLayout, |
| 482 value: T, | 470 descriptorSet: var DescriptorSet, |
| 483 descriptorSetType: static DescriptorSetType | 471 ) = |
| 484 ): array[INFLIGHTFRAMES.int, VkDescriptorSet] = | 472 for name, value in descriptorSet.data.fieldPairs: |
| 485 | 473 when typeof(value) is GPUValue: |
| 486 static: AssertCompatible(TShader, T, descriptorSetType) | 474 assert value.buffer.vk.valid |
| 487 | 475 # TODO: |
| 488 var layouts = newSeqWith(result.len, pipeline.descriptorSetLayouts[descriptorSetType.int]) | 476 # when typeof(value) is Texture: |
| 477 # assert value.texture.vk.valid | |
| 478 | |
| 479 # allocate | |
| 480 var layouts = newSeqWith(descriptorSet.vk.len, layout) | |
| 489 var allocInfo = VkDescriptorSetAllocateInfo( | 481 var allocInfo = VkDescriptorSetAllocateInfo( |
| 490 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | 482 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, |
| 491 descriptorPool: renderData.descriptorPool, | 483 descriptorPool: renderData.descriptorPool, |
| 492 descriptorSetCount: uint32(layouts.len), | 484 descriptorSetCount: uint32(layouts.len), |
| 493 pSetLayouts: layouts.ToCPointer, | 485 pSetLayouts: layouts.ToCPointer, |
| 494 ) | 486 ) |
| 495 checkVkResult vkAllocateDescriptorSets(vulkan.device, addr(allocInfo), result.ToCPointer) | 487 checkVkResult vkAllocateDescriptorSets(vulkan.device, addr(allocInfo), descriptorSet.vk.ToCPointer) |
| 496 | 488 |
| 497 converter toVkIndexType(indexType: IndexType): VkIndexType = | 489 # write |
| 498 case indexType: | 490 var descriptorSetWrites: newSeq[VkWriteDescriptorSet](descriptorSet.vk.len) |
| 499 of None: VK_INDEX_TYPE_NONE_KHR | 491 for i in 0 ..< descriptorSet.vk.len: |
| 500 of UInt8: VK_INDEX_TYPE_UINT8_EXT | 492 descriptorSetWrites.add |
| 501 of UInt16: VK_INDEX_TYPE_UINT16 | 493 |
| 502 of UInt32: VK_INDEX_TYPE_UINT32 | 494 |
| 503 | 495 vkUpdateDescriptorSets(vulkan.device, descriptorSetWrites.len.uint32, descriptorSetWrites.ToCPointer, 0, nil) |
| 504 proc CreateRenderPass(format: VkFormat): VkRenderPass = | 496 |
| 505 var | 497 #[ |
| 506 attachments = @[VkAttachmentDescription( | |
| 507 format: format, | |
| 508 samples: VK_SAMPLE_COUNT_1_BIT, | |
| 509 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
| 510 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
| 511 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
| 512 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
| 513 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
| 514 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
| 515 )] | |
| 516 dependencies = @[VkSubpassDependency( | |
| 517 srcSubpass: VK_SUBPASS_EXTERNAL, | |
| 518 dstSubpass: 0, | |
| 519 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], | |
| 520 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
| 521 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
| 522 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
| 523 )] | |
| 524 outputs = @[ | |
| 525 VkAttachmentReference( | |
| 526 attachment: 0, | |
| 527 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
| 528 ) | |
| 529 ] | |
| 530 | |
| 531 var subpassesList = [ | |
| 532 VkSubpassDescription( | |
| 533 flags: VkSubpassDescriptionFlags(0), | |
| 534 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
| 535 inputAttachmentCount: 0, | |
| 536 pInputAttachments: nil, | |
| 537 colorAttachmentCount: uint32(outputs.len), | |
| 538 pColorAttachments: outputs.ToCPointer, | |
| 539 pResolveAttachments: nil, | |
| 540 pDepthStencilAttachment: nil, | |
| 541 preserveAttachmentCount: 0, | |
| 542 pPreserveAttachments: nil, | |
| 543 ) | |
| 544 ] | |
| 545 | |
| 546 var createInfo = VkRenderPassCreateInfo( | |
| 547 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
| 548 attachmentCount: uint32(attachments.len), | |
| 549 pAttachments: attachments.ToCPointer, | |
| 550 subpassCount: uint32(subpassesList.len), | |
| 551 pSubpasses: subpassesList.ToCPointer, | |
| 552 dependencyCount: uint32(dependencies.len), | |
| 553 pDependencies: dependencies.ToCPointer, | |
| 554 ) | |
| 555 checkVkResult vulkan.device.vkCreateRenderPass(addr(createInfo), nil, addr(result)) | |
| 556 | |
| 557 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = | |
| 558 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | |
| 559 case stage | |
| 560 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | |
| 561 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | |
| 562 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" | |
| 563 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" | |
| 564 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" | |
| 565 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" | |
| 566 else: "" | |
| 567 | |
| 568 when defined(nimcheck): # will not run if nimcheck is running | |
| 569 return result | |
| 570 | |
| 571 let | |
| 572 stagename = stage2string(stage) | |
| 573 shaderHash = hash(shaderSource) | |
| 574 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" | |
| 575 | |
| 576 if not shaderfile.fileExists: | |
| 577 echo "shader of type ", stage | |
| 578 for i, line in enumerate(shaderSource.splitlines()): | |
| 579 echo " ", i + 1, " ", line | |
| 580 # var glslExe = currentSourcePath.parentDir.parentDir.parentDir / "tools" / "glslangValidator" | |
| 581 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator" | |
| 582 when defined(windows): | |
| 583 glslExe = glslExe & "." & ExeExt | |
| 584 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" | |
| 585 echo "run: ", command | |
| 586 discard StaticExecChecked( | |
| 587 command = command, | |
| 588 input = shaderSource | |
| 589 ) | |
| 590 else: | |
| 591 echo &"shaderfile {shaderfile} is up-to-date" | |
| 592 | |
| 593 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up | |
| 594 let shaderbinary = staticRead shaderfile.replace("\\", "/") | |
| 595 else: | |
| 596 let shaderbinary = staticRead shaderfile | |
| 597 | |
| 598 var i = 0 | |
| 599 while i < shaderbinary.len: | |
| 600 result.add( | |
| 601 (uint32(shaderbinary[i + 0]) shl 0) or | |
| 602 (uint32(shaderbinary[i + 1]) shl 8) or | |
| 603 (uint32(shaderbinary[i + 2]) shl 16) or | |
| 604 (uint32(shaderbinary[i + 3]) shl 24) | |
| 605 ) | |
| 606 i += 4 | |
| 607 | |
| 608 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = | |
| 609 const GLSL_VERSION = "450" | |
| 610 var vsInput: seq[string] | |
| 611 var vsOutput: seq[string] | |
| 612 var fsInput: seq[string] | |
| 613 var fsOutput: seq[string] | |
| 614 var uniforms: seq[string] | |
| 615 var samplers: seq[string] | |
| 616 var vsInputLocation = 0'u32 | |
| 617 var passLocation = 0 | |
| 618 var fsOutputLocation = 0 | |
| 619 | |
| 620 var descriptorSetCount = 0 | |
| 621 for fieldname, value in fieldPairs(shader): | |
| 622 # vertex shader inputs | |
| 623 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): | |
| 624 assert typeof(value) is SupportedGPUType | |
| 625 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" | |
| 626 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): | |
| 627 vsInputLocation += NLocationSlots(value) | |
| 628 | |
| 629 # intermediate values, passed between shaders | |
| 630 elif hasCustomPragma(value, Pass) or hasCustomPragma(value, PassFlat): | |
| 631 let flat = if hasCustomPragma(value, PassFlat): "flat " else: "" | |
| 632 vsOutput.add "layout(location = " & $passLocation & ") " & flat & "out " & GlslType(value) & " " & fieldname & ";" | |
| 633 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" | |
| 634 passLocation.inc | |
| 635 | |
| 636 # fragment shader output | |
| 637 elif hasCustomPragma(value, ShaderOutput): | |
| 638 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" | |
| 639 fsOutputLocation.inc | |
| 640 | |
| 641 # descriptor sets | |
| 642 # need to consider 4 cases: uniform block, texture, uniform block array, texture array | |
| 643 elif hasCustomPragma(value, DescriptorSet): | |
| 644 assert descriptorSetCount < MAX_DESCRIPTORSETS, &"{tt.name(TShader)}: maximum {MAX_DESCRIPTORSETS} allowed" | |
| 645 | |
| 646 var descriptorBinding = 0 | |
| 647 for descriptorName, descriptorValue in fieldPairs(value): | |
| 648 | |
| 649 when typeof(descriptorValue) is Texture: | |
| 650 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(descriptorValue) & " " & descriptorName & ";" | |
| 651 descriptorBinding.inc | |
| 652 | |
| 653 elif typeof(descriptorValue) is GPUValue: | |
| 654 uniforms.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform T" & descriptorName & " {" | |
| 655 when typeof(descriptorValue.data) is object: | |
| 656 for blockFieldName, blockFieldValue in descriptorValue.data.fieldPairs(): | |
| 657 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType" | |
| 658 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";" | |
| 659 uniforms.add "} " & descriptorName & ";" | |
| 660 elif typeof(descriptorValue.data) is array: | |
| 661 for blockFieldName, blockFieldValue in default(elementType(descriptorValue.data)).fieldPairs(): | |
| 662 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType" | |
| 663 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";" | |
| 664 uniforms.add "} " & descriptorName & "[" & $descriptorValue.data.len & "];" | |
| 665 descriptorBinding.inc | |
| 666 elif typeof(descriptorValue) is array: | |
| 667 when elementType(descriptorValue) is Texture: | |
| 668 let arrayDecl = "[" & $typeof(descriptorValue).len & "]" | |
| 669 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(descriptorValue))) & " " & descriptorName & "" & arrayDecl & ";" | |
| 670 descriptorBinding.inc | |
| 671 else: | |
| 672 {.error: "Unsupported shader descriptor field " & descriptorName.} | |
| 673 descriptorSetCount.inc | |
| 674 elif fieldname in ["vertexCode", "fragmentCode"]: | |
| 675 discard | |
| 676 else: | |
| 677 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).} | |
| 678 | |
| 679 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
| 680 vsInput & | |
| 681 uniforms & | |
| 682 samplers & | |
| 683 vsOutput & | |
| 684 @[shader.vertexCode]).join("\n") | |
| 685 | |
| 686 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
| 687 fsInput & | |
| 688 uniforms & | |
| 689 samplers & | |
| 690 fsOutput & | |
| 691 @[shader.fragmentCode]).join("\n") | |
| 692 | |
| 693 proc CompileShader[TShader](shader: static TShader): ShaderObject[TShader] = | |
| 694 const (vertexShaderSource, fragmentShaderSource) = generateShaderSource(shader) | |
| 695 | |
| 696 let vertexBinary = compileGlslToSPIRV(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderSource) | |
| 697 let fragmentBinary = compileGlslToSPIRV(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderSource) | |
| 698 | |
| 699 var createInfoVertex = VkShaderModuleCreateInfo( | |
| 700 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, | |
| 701 codeSize: csize_t(vertexBinary.len * sizeof(uint32)), | |
| 702 pCode: vertexBinary.ToCPointer, | |
| 703 ) | |
| 704 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result.vertexShader)) | |
| 705 var createInfoFragment = VkShaderModuleCreateInfo( | |
| 706 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, | |
| 707 codeSize: csize_t(fragmentBinary.len * sizeof(uint32)), | |
| 708 pCode: fragmentBinary.ToCPointer, | |
| 709 ) | |
| 710 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader)) | |
| 711 | |
| 712 | |
| 713 proc CreatePipeline[TShader]( | |
| 714 renderPass: VkRenderPass, | |
| 715 shader: ShaderObject[TShader], | |
| 716 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, | |
| 717 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, | |
| 718 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT, | |
| 719 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, | |
| 720 descriptorPoolLimit = 1024 | |
| 721 ): Pipeline[TShader] = | |
| 722 # create pipeline | |
| 723 | |
| 724 ForDescriptorSets(default(TShader), setNumber, descriptorSet): | |
| 725 var layoutbindings: seq[VkDescriptorSetLayoutBinding] | |
| 726 ForDescriptorFields(descriptorSet, fieldName, descriptorType, descriptorCount, descriptorBindingNumber): | |
| 727 layoutbindings.add VkDescriptorSetLayoutBinding( | |
| 728 binding: descriptorBindingNumber, | |
| 729 descriptorType: descriptorType, | |
| 730 descriptorCount: descriptorCount, | |
| 731 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), | |
| 732 pImmutableSamplers: nil, | |
| 733 ) | |
| 734 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( | |
| 735 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
| 736 bindingCount: layoutbindings.len.uint32, | |
| 737 pBindings: layoutbindings.ToCPointer | |
| 738 ) | |
| 739 checkVkResult vkCreateDescriptorSetLayout( | |
| 740 vulkan.device, | |
| 741 addr(layoutCreateInfo), | |
| 742 nil, | |
| 743 addr(result.descriptorSetLayouts[setNumber.int]) | |
| 744 ) | |
| 745 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( | |
| 746 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
| 747 setLayoutCount: result.descriptorSetLayouts.len.uint32, | |
| 748 pSetLayouts: result.descriptorSetLayouts.ToCPointer, | |
| 749 # pushConstantRangeCount: uint32(pushConstants.len), | |
| 750 # pPushConstantRanges: pushConstants.ToCPointer, | |
| 751 ) | |
| 752 checkVkResult vkCreatePipelineLayout(vulkan.device, addr(pipelineLayoutInfo), nil, addr(result.layout)) | |
| 753 | |
| 754 let stages = [ | |
| 755 VkPipelineShaderStageCreateInfo( | |
| 756 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 757 stage: VK_SHADER_STAGE_VERTEX_BIT, | |
| 758 module: shader.vertexShader, | |
| 759 pName: "main", | |
| 760 ), | |
| 761 VkPipelineShaderStageCreateInfo( | |
| 762 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 763 stage: VK_SHADER_STAGE_FRAGMENT_BIT, | |
| 764 module: shader.fragmentShader, | |
| 765 pName: "main", | |
| 766 ), | |
| 767 ] | |
| 768 var | |
| 769 bindings: seq[VkVertexInputBindingDescription] | |
| 770 attributes: seq[VkVertexInputAttributeDescription] | |
| 771 var inputBindingNumber = 0'u32 | |
| 772 var location = 0'u32 | |
| 773 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): | |
| 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, | |
| 784 location: location, | |
| 785 format: VkType(value), | |
| 786 offset: i * perDescriptorSize, | |
| 787 ) | |
| 788 location += NLocationSlots(value) | |
| 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, | |
| 856 pStages: stages.ToCPointer, | |
| 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( | |
| 872 vulkan.device, | |
| 873 VkPipelineCache(0), | |
| 874 1, | |
| 875 addr(createInfo), | |
| 876 nil, | |
| 877 addr(result.pipeline) | |
| 878 ) | |
| 879 | |
| 880 proc AllocateIndirectMemory(size: uint64): IndirectGPUMemory = | |
| 881 # chooses biggest memory type that has NOT VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | |
| 882 result.size = size | |
| 883 result.needsTransfer = true | |
| 884 | |
| 885 # find a good memory type | |
| 886 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
| 887 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties) | |
| 888 | |
| 889 var biggestHeap: uint64 = 0 | |
| 890 var memoryTypeIndex = high(uint32) | |
| 891 # try to find non-host-visible type | |
| 892 for i in 0'u32 ..< physicalProperties.memoryTypeCount: | |
| 893 if not (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in toEnums(physicalProperties.memoryTypes[i].propertyFlags)): | |
| 894 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 895 if size > biggestHeap: | |
| 896 biggestHeap = size | |
| 897 memoryTypeIndex = i | |
| 898 | |
| 899 # If we did not found a device-only memory type, let's just take the biggest overall | |
| 900 if memoryTypeIndex == high(uint32): | |
| 901 result.needsTransfer = false | |
| 902 for i in 0'u32 ..< physicalProperties.memoryTypeCount: | |
| 903 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 904 if size > biggestHeap: | |
| 905 biggestHeap = size | |
| 906 memoryTypeIndex = i | |
| 907 | |
| 908 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type" | |
| 909 var allocationInfo = VkMemoryAllocateInfo( | |
| 910 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
| 911 allocationSize: result.size, | |
| 912 memoryTypeIndex: memoryTypeIndex, | |
| 913 ) | |
| 914 checkVkResult vkAllocateMemory( | |
| 915 vulkan.device, | |
| 916 addr allocationInfo, | |
| 917 nil, | |
| 918 addr result.vk | |
| 919 ) | |
| 920 | |
| 921 proc AllocateDirectMemory(size: uint64): DirectGPUMemory = | |
| 922 result.size = size | |
| 923 result.needsFlush = true | |
| 924 | |
| 925 # find a good memory type | |
| 926 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
| 927 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties) | |
| 928 | |
| 929 var biggestHeap: uint64 = 0 | |
| 930 var memoryTypeIndex = high(uint32) | |
| 931 # try to find host-visible type | |
| 932 for i in 0 ..< physicalProperties.memoryTypeCount: | |
| 933 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags) | |
| 934 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags: | |
| 935 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 936 if size > biggestHeap: | |
| 937 biggestHeap = size | |
| 938 memoryTypeIndex = i | |
| 939 result.needsFlush = not (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT in flags) | |
| 940 | |
| 941 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type" | |
| 942 var allocationInfo = VkMemoryAllocateInfo( | |
| 943 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
| 944 allocationSize: result.size, | |
| 945 memoryTypeIndex: GetDirectMemoryTypeIndex(), | |
| 946 ) | |
| 947 checkVkResult vkAllocateMemory( | |
| 948 vulkan.device, | |
| 949 addr allocationInfo, | |
| 950 nil, | |
| 951 addr result.vk | |
| 952 ) | |
| 953 checkVkResult vkMapMemory( | |
| 954 device = vulkan.device, | |
| 955 memory = result.vk, | |
| 956 offset = 0'u64, | |
| 957 size = result.size, | |
| 958 flags = VkMemoryMapFlags(0), | |
| 959 ppData = addr(result.data) | |
| 960 ) | |
| 961 | |
| 962 proc AllocateIndirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) = | |
| 963 if size == 0: | |
| 964 return | |
| 965 var buffer = Buffer[IndirectGPUMemory](size: size) | |
| 966 | |
| 967 let usageFlags = case btype: | |
| 968 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 969 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 970 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 971 | |
| 972 # iterate through memory areas to find big enough free space | |
| 973 # TODO: dynamically expand memory allocations | |
| 974 for (memory, usedOffset) in renderData.indirectMemory.mitems: | |
| 975 if memory.size - usedOffset >= size: | |
| 976 buffer.offset = usedOffset | |
| 977 # create buffer | |
| 978 var createInfo = VkBufferCreateInfo( | |
| 979 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
| 980 flags: VkBufferCreateFlags(0), | |
| 981 size: buffer.size, | |
| 982 usage: toBits(usageFlags), | |
| 983 sharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
| 984 ) | |
| 985 checkVkResult vkCreateBuffer( | |
| 986 device = vulkan.device, | |
| 987 pCreateInfo = addr createInfo, | |
| 988 pAllocator = nil, | |
| 989 pBuffer = addr(buffer.vk) | |
| 990 ) | |
| 991 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset) | |
| 992 renderData.indirectBuffers.add (buffer, btype, 0'u64) | |
| 993 # update memory area offset | |
| 994 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT) | |
| 995 return | |
| 996 | |
| 997 assert false, "Did not find allocated memory region with enough space" | |
| 998 | |
| 999 proc AllocateDirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) = | |
| 1000 if size == 0: | |
| 1001 return | |
| 1002 | |
| 1003 var buffer = Buffer[DirectGPUMemory](size: size) | |
| 1004 | |
| 1005 let usageFlags = case btype: | |
| 1006 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1007 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1008 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1009 | |
| 1010 # iterate through memory areas to find big enough free space | |
| 1011 # TODO: dynamically expand memory allocations | |
| 1012 for (memory, usedOffset) in renderData.directMemory.mitems: | |
| 1013 if memory.size - usedOffset >= size: | |
| 1014 buffer.offset = usedOffset | |
| 1015 # create buffer | |
| 1016 var createInfo = VkBufferCreateInfo( | |
| 1017 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
| 1018 flags: VkBufferCreateFlags(0), | |
| 1019 size: buffer.size, | |
| 1020 usage: toBits(usageFlags), | |
| 1021 sharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
| 1022 ) | |
| 1023 checkVkResult vkCreateBuffer( | |
| 1024 device = vulkan.device, | |
| 1025 pCreateInfo = addr createInfo, | |
| 1026 pAllocator = nil, | |
| 1027 pBuffer = addr(buffer.vk) | |
| 1028 ) | |
| 1029 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset) | |
| 1030 renderData.directBuffers.add (buffer, btype, 0'u64) | |
| 1031 # update memory area offset | |
| 1032 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT) | |
| 1033 return | |
| 1034 | |
| 1035 assert false, "Did not find allocated memory region with enough space" | |
| 1036 | |
| 1037 proc InitRenderData(descriptorPoolLimit = 1024'u32): RenderData = | |
| 1038 # allocate descriptor pools | |
| 1039 var poolSizes = [ | |
| 1040 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit), | |
| 1041 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit), | |
| 1042 ] | |
| 1043 var poolInfo = VkDescriptorPoolCreateInfo( | |
| 1044 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
| 1045 poolSizeCount: poolSizes.len.uint32, | |
| 1046 pPoolSizes: poolSizes.ToCPointer, | |
| 1047 maxSets: descriptorPoolLimit, | |
| 1048 ) | |
| 1049 checkVkResult vkCreateDescriptorPool(vulkan.device, addr(poolInfo), nil, addr(result.descriptorPool)) | |
| 1050 | |
| 1051 # allocate some memory | |
| 1052 var initialAllocationSize = 1_000_000_000'u64 # TODO: make this more dynamic or something? | |
| 1053 result.indirectMemory = @[(memory: AllocateIndirectMemory(size = initialAllocationSize), usedOffset: 0'u64)] | |
| 1054 result.directMemory = @[(memory: AllocateDirectMemory(size = initialAllocationSize), usedOffset: 0'u64)] | |
| 1055 | |
| 1056 proc FlushDirectMemory(renderData: RenderData) = | |
| 1057 var flushRegions = newSeqOfCap[VkMappedMemoryRange](renderData.directMemory.len) | |
| 1058 for entry in renderData.directMemory: | |
| 1059 if entry.usedOffset > 0: | |
| 1060 flushRegions.add VkMappedMemoryRange( | |
| 1061 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, | |
| 1062 memory: entry.memory.vk, | |
| 1063 size: entry.usedOffset, | |
| 1064 ) | |
| 1065 if flushRegions.len > 0: | |
| 1066 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, flushRegions.len.uint32, flushRegions.ToCPointer()) | |
| 1067 | |
| 1068 # For the Get*BufferSize: | |
| 1069 # BUFFER_ALIGNMENT is just added for a rough estimate, to ensure we have enough space to align when binding | |
| 1070 proc GetIndirectBufferSizes[T](data: T): uint64 = | |
| 1071 for name, value in fieldPairs(data): | |
| 1072 when not hasCustomPragma(value, VertexIndices): | |
| 1073 when typeof(value) is GPUData: | |
| 1074 when UsesIndirectMemory(value): | |
| 1075 result += value.size + BUFFER_ALIGNMENT | |
| 1076 proc GetDirectBufferSizes[T](data: T): uint64 = | |
| 1077 for name, value in fieldPairs(data): | |
| 1078 when not hasCustomPragma(value, VertexIndices): | |
| 1079 when typeof(value) is GPUData: | |
| 1080 when UsesDirectMemory(value): | |
| 1081 result += value.size + BUFFER_ALIGNMENT | |
| 1082 proc GetIndirectIndexBufferSizes[T](data: T): uint64 = | |
| 1083 for name, value in fieldPairs(data): | |
| 1084 when hasCustomPragma(value, VertexIndices): | |
| 1085 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray" | |
| 1086 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32 | |
| 1087 when UsesIndirectMemory(value): | |
| 1088 result += value.size + BUFFER_ALIGNMENT | |
| 1089 proc GetDirectIndexBufferSizes[T](data: T): uint64 = | |
| 1090 for name, value in fieldPairs(data): | |
| 1091 when hasCustomPragma(value, VertexIndices): | |
| 1092 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray" | |
| 1093 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32 | |
| 1094 when UsesDirectMemory(value): | |
| 1095 result += value.size + BUFFER_ALIGNMENT | |
| 1096 | |
| 1097 proc AssignIndirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) = | |
| 1098 for name, value in fieldPairs(data): | |
| 1099 when typeof(value) is GPUData: | |
| 1100 when UsesIndirectMemory(value): | |
| 1101 # find next buffer of correct type with enough free space | |
| 1102 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices): | |
| 1103 var foundBuffer = false | |
| 1104 for (buffer, bt, offset) in renderData.indirectBuffers.mitems: | |
| 1105 if bt == btype and buffer.size - offset >= value.size: | |
| 1106 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned" | |
| 1107 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created" | |
| 1108 value.buffer = buffer | |
| 1109 value.offset = offset | |
| 1110 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT) | |
| 1111 foundBuffer = true | |
| 1112 break | |
| 1113 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'" | |
| 1114 proc AssignDirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) = | |
| 1115 for name, value in fieldPairs(data): | |
| 1116 when typeof(value) is GPUData: | |
| 1117 when UsesDirectMemory(value): | |
| 1118 # find next buffer of correct type with enough free space | |
| 1119 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices): | |
| 1120 var foundBuffer = false | |
| 1121 for (buffer, bt, offset) in renderData.directBuffers.mitems: | |
| 1122 if bt == btype and buffer.size - offset >= value.size: | |
| 1123 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned" | |
| 1124 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created" | |
| 1125 value.buffer = buffer | |
| 1126 value.offset = offset | |
| 1127 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT) | |
| 1128 foundBuffer = true | |
| 1129 break | |
| 1130 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'" | |
| 1131 | |
| 1132 proc HasGPUValueField[T](name: static string): bool {.compileTime.} = | |
| 1133 for fieldname, value in default(T).fieldPairs(): | |
| 1134 when typeof(value) is GPUValue and fieldname == name: return true | |
| 1135 return false | |
| 1136 | |
| 1137 template WithGPUValueField(obj: object, name: static string, fieldvalue, body: untyped): untyped = | |
| 1138 # HasGPUValueField MUST be used to check if this is supported | |
| 1139 for fieldname, value in obj.fieldPairs(): | |
| 1140 when fieldname == name: | |
| 1141 block: | |
| 1142 let `fieldvalue` {.inject.} = value | |
| 1143 body | |
| 1144 | |
| 1145 proc WriteDescriptors[TShader, TUniforms, TGlobals](renderData: RenderData, uniforms: TUniforms, globals: TGlobals) = | 498 proc WriteDescriptors[TShader, TUniforms, TGlobals](renderData: RenderData, uniforms: TUniforms, globals: TGlobals) = |
| 1146 var descriptorSetWrites: seq[VkWriteDescriptorSet] | 499 var descriptorSetWrites: seq[VkWriteDescriptorSet] |
| 1147 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber): | 500 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber): |
| 1148 for frameInFlight in 0 ..< renderData.descriptorSets.len: | 501 for frameInFlight in 0 ..< renderData.descriptorSets.len: |
| 1149 when descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: | 502 when descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 1200 pImageInfo: addr(imageInfo), | 553 pImageInfo: addr(imageInfo), |
| 1201 pBufferInfo: nil, | 554 pBufferInfo: nil, |
| 1202 ) | 555 ) |
| 1203 else: | 556 else: |
| 1204 assert false, "Unsupported descriptor type" | 557 assert false, "Unsupported descriptor type" |
| 1205 echo descriptorSetWrites | |
| 1206 vkUpdateDescriptorSets(vulkan.device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) | 558 vkUpdateDescriptorSets(vulkan.device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) |
| 559 ]# | |
| 560 | |
| 561 | |
| 562 converter toVkIndexType(indexType: IndexType): VkIndexType = | |
| 563 case indexType: | |
| 564 of None: VK_INDEX_TYPE_NONE_KHR | |
| 565 of UInt8: VK_INDEX_TYPE_UINT8_EXT | |
| 566 of UInt16: VK_INDEX_TYPE_UINT16 | |
| 567 of UInt32: VK_INDEX_TYPE_UINT32 | |
| 568 | |
| 569 proc CreateRenderPass(format: VkFormat): VkRenderPass = | |
| 570 var | |
| 571 attachments = @[VkAttachmentDescription( | |
| 572 format: format, | |
| 573 samples: VK_SAMPLE_COUNT_1_BIT, | |
| 574 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
| 575 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
| 576 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
| 577 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
| 578 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
| 579 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
| 580 )] | |
| 581 dependencies = @[VkSubpassDependency( | |
| 582 srcSubpass: VK_SUBPASS_EXTERNAL, | |
| 583 dstSubpass: 0, | |
| 584 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], | |
| 585 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
| 586 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
| 587 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
| 588 )] | |
| 589 outputs = @[ | |
| 590 VkAttachmentReference( | |
| 591 attachment: 0, | |
| 592 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
| 593 ) | |
| 594 ] | |
| 595 | |
| 596 var subpassesList = [ | |
| 597 VkSubpassDescription( | |
| 598 flags: VkSubpassDescriptionFlags(0), | |
| 599 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
| 600 inputAttachmentCount: 0, | |
| 601 pInputAttachments: nil, | |
| 602 colorAttachmentCount: uint32(outputs.len), | |
| 603 pColorAttachments: outputs.ToCPointer, | |
| 604 pResolveAttachments: nil, | |
| 605 pDepthStencilAttachment: nil, | |
| 606 preserveAttachmentCount: 0, | |
| 607 pPreserveAttachments: nil, | |
| 608 ) | |
| 609 ] | |
| 610 | |
| 611 var createInfo = VkRenderPassCreateInfo( | |
| 612 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
| 613 attachmentCount: uint32(attachments.len), | |
| 614 pAttachments: attachments.ToCPointer, | |
| 615 subpassCount: uint32(subpassesList.len), | |
| 616 pSubpasses: subpassesList.ToCPointer, | |
| 617 dependencyCount: uint32(dependencies.len), | |
| 618 pDependencies: dependencies.ToCPointer, | |
| 619 ) | |
| 620 checkVkResult vulkan.device.vkCreateRenderPass(addr(createInfo), nil, addr(result)) | |
| 621 | |
| 622 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = | |
| 623 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | |
| 624 case stage | |
| 625 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | |
| 626 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | |
| 627 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" | |
| 628 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" | |
| 629 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" | |
| 630 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" | |
| 631 else: "" | |
| 632 | |
| 633 when defined(nimcheck): # will not run if nimcheck is running | |
| 634 return result | |
| 635 | |
| 636 let | |
| 637 stagename = stage2string(stage) | |
| 638 shaderHash = hash(shaderSource) | |
| 639 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" | |
| 640 | |
| 641 if not shaderfile.fileExists: | |
| 642 echo "shader of type ", stage | |
| 643 for i, line in enumerate(shaderSource.splitlines()): | |
| 644 echo " ", i + 1, " ", line | |
| 645 # var glslExe = currentSourcePath.parentDir.parentDir.parentDir / "tools" / "glslangValidator" | |
| 646 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator" | |
| 647 when defined(windows): | |
| 648 glslExe = glslExe & "." & ExeExt | |
| 649 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" | |
| 650 echo "run: ", command | |
| 651 discard StaticExecChecked( | |
| 652 command = command, | |
| 653 input = shaderSource | |
| 654 ) | |
| 655 else: | |
| 656 echo &"shaderfile {shaderfile} is up-to-date" | |
| 657 | |
| 658 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up | |
| 659 let shaderbinary = staticRead shaderfile.replace("\\", "/") | |
| 660 else: | |
| 661 let shaderbinary = staticRead shaderfile | |
| 662 | |
| 663 var i = 0 | |
| 664 while i < shaderbinary.len: | |
| 665 result.add( | |
| 666 (uint32(shaderbinary[i + 0]) shl 0) or | |
| 667 (uint32(shaderbinary[i + 1]) shl 8) or | |
| 668 (uint32(shaderbinary[i + 2]) shl 16) or | |
| 669 (uint32(shaderbinary[i + 3]) shl 24) | |
| 670 ) | |
| 671 i += 4 | |
| 672 | |
| 673 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = | |
| 674 const GLSL_VERSION = "450" | |
| 675 var vsInput: seq[string] | |
| 676 var vsOutput: seq[string] | |
| 677 var fsInput: seq[string] | |
| 678 var fsOutput: seq[string] | |
| 679 var uniforms: seq[string] | |
| 680 var samplers: seq[string] | |
| 681 var vsInputLocation = 0'u32 | |
| 682 var passLocation = 0 | |
| 683 var fsOutputLocation = 0 | |
| 684 | |
| 685 var descriptorSetCount = 0 | |
| 686 for fieldname, value in fieldPairs(shader): | |
| 687 # vertex shader inputs | |
| 688 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): | |
| 689 assert typeof(value) is SupportedGPUType | |
| 690 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" | |
| 691 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): | |
| 692 vsInputLocation += NLocationSlots(value) | |
| 693 | |
| 694 # intermediate values, passed between shaders | |
| 695 elif hasCustomPragma(value, Pass) or hasCustomPragma(value, PassFlat): | |
| 696 let flat = if hasCustomPragma(value, PassFlat): "flat " else: "" | |
| 697 vsOutput.add "layout(location = " & $passLocation & ") " & flat & "out " & GlslType(value) & " " & fieldname & ";" | |
| 698 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" | |
| 699 passLocation.inc | |
| 700 | |
| 701 # fragment shader output | |
| 702 elif hasCustomPragma(value, ShaderOutput): | |
| 703 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" | |
| 704 fsOutputLocation.inc | |
| 705 | |
| 706 # descriptor sets | |
| 707 # need to consider 4 cases: uniform block, texture, uniform block array, texture array | |
| 708 elif typeof(value) is DescriptorSet: | |
| 709 assert descriptorSetCount <= DescriptorSetType.high.int, &"{tt.name(TShader)}: maximum {DescriptorSetType.high} allowed" | |
| 710 | |
| 711 var descriptorBinding = 0 | |
| 712 for descriptorName, descriptorValue in fieldPairs(value.data): | |
| 713 | |
| 714 when typeof(descriptorValue) is Texture: | |
| 715 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(descriptorValue) & " " & descriptorName & ";" | |
| 716 descriptorBinding.inc | |
| 717 | |
| 718 elif typeof(descriptorValue) is GPUValue: | |
| 719 uniforms.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform T" & descriptorName & " {" | |
| 720 when typeof(descriptorValue.data) is object: | |
| 721 for blockFieldName, blockFieldValue in descriptorValue.data.fieldPairs(): | |
| 722 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType" | |
| 723 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";" | |
| 724 uniforms.add "} " & descriptorName & ";" | |
| 725 elif typeof(descriptorValue.data) is array: | |
| 726 for blockFieldName, blockFieldValue in default(elementType(descriptorValue.data)).fieldPairs(): | |
| 727 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType" | |
| 728 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";" | |
| 729 uniforms.add "} " & descriptorName & "[" & $descriptorValue.data.len & "];" | |
| 730 descriptorBinding.inc | |
| 731 elif typeof(descriptorValue) is array: | |
| 732 when elementType(descriptorValue) is Texture: | |
| 733 let arrayDecl = "[" & $typeof(descriptorValue).len & "]" | |
| 734 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(descriptorValue))) & " " & descriptorName & "" & arrayDecl & ";" | |
| 735 descriptorBinding.inc | |
| 736 else: | |
| 737 {.error: "Unsupported shader descriptor field " & descriptorName.} | |
| 738 descriptorSetCount.inc | |
| 739 elif fieldname in ["vertexCode", "fragmentCode"]: | |
| 740 discard | |
| 741 else: | |
| 742 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).} | |
| 743 | |
| 744 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
| 745 vsInput & | |
| 746 uniforms & | |
| 747 samplers & | |
| 748 vsOutput & | |
| 749 @[shader.vertexCode]).join("\n") | |
| 750 | |
| 751 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
| 752 fsInput & | |
| 753 uniforms & | |
| 754 samplers & | |
| 755 fsOutput & | |
| 756 @[shader.fragmentCode]).join("\n") | |
| 757 | |
| 758 proc CompileShader[TShader](shader: static TShader): ShaderObject[TShader] = | |
| 759 const (vertexShaderSource, fragmentShaderSource) = generateShaderSource(shader) | |
| 760 | |
| 761 let vertexBinary = compileGlslToSPIRV(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderSource) | |
| 762 let fragmentBinary = compileGlslToSPIRV(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderSource) | |
| 763 | |
| 764 var createInfoVertex = VkShaderModuleCreateInfo( | |
| 765 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, | |
| 766 codeSize: csize_t(vertexBinary.len * sizeof(uint32)), | |
| 767 pCode: vertexBinary.ToCPointer, | |
| 768 ) | |
| 769 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result.vertexShader)) | |
| 770 var createInfoFragment = VkShaderModuleCreateInfo( | |
| 771 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, | |
| 772 codeSize: csize_t(fragmentBinary.len * sizeof(uint32)), | |
| 773 pCode: fragmentBinary.ToCPointer, | |
| 774 ) | |
| 775 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader)) | |
| 776 | |
| 777 | |
| 778 proc CreatePipeline[TShader]( | |
| 779 renderPass: VkRenderPass, | |
| 780 shader: ShaderObject[TShader], | |
| 781 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, | |
| 782 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, | |
| 783 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT, | |
| 784 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, | |
| 785 descriptorPoolLimit = 1024 | |
| 786 ): Pipeline[TShader] = | |
| 787 # create pipeline | |
| 788 | |
| 789 for theFieldname, value in fieldPairs(default(TShader)): | |
| 790 when typeof(value) is DescriptorSet: | |
| 791 var layoutbindings: seq[VkDescriptorSetLayoutBinding] | |
| 792 ForDescriptorFields(value.data, fieldName, descriptorType, descriptorCount, descriptorBindingNumber): | |
| 793 layoutbindings.add VkDescriptorSetLayoutBinding( | |
| 794 binding: descriptorBindingNumber, | |
| 795 descriptorType: descriptorType, | |
| 796 descriptorCount: descriptorCount, | |
| 797 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), | |
| 798 pImmutableSamplers: nil, | |
| 799 ) | |
| 800 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( | |
| 801 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
| 802 bindingCount: layoutbindings.len.uint32, | |
| 803 pBindings: layoutbindings.ToCPointer | |
| 804 ) | |
| 805 checkVkResult vkCreateDescriptorSetLayout( | |
| 806 vulkan.device, | |
| 807 addr(layoutCreateInfo), | |
| 808 nil, | |
| 809 addr(result.descriptorSetLayouts[value.sType]) | |
| 810 ) | |
| 811 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( | |
| 812 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
| 813 setLayoutCount: result.descriptorSetLayouts.len.uint32, | |
| 814 pSetLayouts: result.descriptorSetLayouts.ToCPointer, | |
| 815 # pushConstantRangeCount: uint32(pushConstants.len), | |
| 816 # pPushConstantRanges: pushConstants.ToCPointer, | |
| 817 ) | |
| 818 checkVkResult vkCreatePipelineLayout(vulkan.device, addr(pipelineLayoutInfo), nil, addr(result.layout)) | |
| 819 | |
| 820 let stages = [ | |
| 821 VkPipelineShaderStageCreateInfo( | |
| 822 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 823 stage: VK_SHADER_STAGE_VERTEX_BIT, | |
| 824 module: shader.vertexShader, | |
| 825 pName: "main", | |
| 826 ), | |
| 827 VkPipelineShaderStageCreateInfo( | |
| 828 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 829 stage: VK_SHADER_STAGE_FRAGMENT_BIT, | |
| 830 module: shader.fragmentShader, | |
| 831 pName: "main", | |
| 832 ), | |
| 833 ] | |
| 834 var | |
| 835 bindings: seq[VkVertexInputBindingDescription] | |
| 836 attributes: seq[VkVertexInputAttributeDescription] | |
| 837 var inputBindingNumber = 0'u32 | |
| 838 var location = 0'u32 | |
| 839 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): | |
| 840 bindings.add VkVertexInputBindingDescription( | |
| 841 binding: inputBindingNumber, | |
| 842 stride: sizeof(value).uint32, | |
| 843 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX, | |
| 844 ) | |
| 845 # allows to submit larger data structures like Mat44, for most other types will be 1 | |
| 846 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value) | |
| 847 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value): | |
| 848 attributes.add VkVertexInputAttributeDescription( | |
| 849 binding: inputBindingNumber, | |
| 850 location: location, | |
| 851 format: VkType(value), | |
| 852 offset: i * perDescriptorSize, | |
| 853 ) | |
| 854 location += NLocationSlots(value) | |
| 855 inc inputBindingNumber | |
| 856 | |
| 857 let | |
| 858 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
| 859 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
| 860 vertexBindingDescriptionCount: uint32(bindings.len), | |
| 861 pVertexBindingDescriptions: bindings.ToCPointer, | |
| 862 vertexAttributeDescriptionCount: uint32(attributes.len), | |
| 863 pVertexAttributeDescriptions: attributes.ToCPointer, | |
| 864 ) | |
| 865 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
| 866 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
| 867 topology: topology, | |
| 868 primitiveRestartEnable: false, | |
| 869 ) | |
| 870 viewportState = VkPipelineViewportStateCreateInfo( | |
| 871 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | |
| 872 viewportCount: 1, | |
| 873 scissorCount: 1, | |
| 874 ) | |
| 875 rasterizer = VkPipelineRasterizationStateCreateInfo( | |
| 876 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | |
| 877 depthClampEnable: VK_FALSE, | |
| 878 rasterizerDiscardEnable: VK_FALSE, | |
| 879 polygonMode: polygonMode, | |
| 880 lineWidth: 1.0, | |
| 881 cullMode: toBits [cullMode], | |
| 882 frontFace: frontFace, | |
| 883 depthBiasEnable: VK_FALSE, | |
| 884 depthBiasConstantFactor: 0.0, | |
| 885 depthBiasClamp: 0.0, | |
| 886 depthBiasSlopeFactor: 0.0, | |
| 887 ) | |
| 888 multisampling = VkPipelineMultisampleStateCreateInfo( | |
| 889 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | |
| 890 sampleShadingEnable: VK_FALSE, | |
| 891 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, | |
| 892 minSampleShading: 1.0, | |
| 893 pSampleMask: nil, | |
| 894 alphaToCoverageEnable: VK_FALSE, | |
| 895 alphaToOneEnable: VK_FALSE, | |
| 896 ) | |
| 897 colorBlendAttachment = VkPipelineColorBlendAttachmentState( | |
| 898 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT], | |
| 899 blendEnable: VK_TRUE, | |
| 900 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, | |
| 901 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | |
| 902 colorBlendOp: VK_BLEND_OP_ADD, | |
| 903 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, | |
| 904 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, | |
| 905 alphaBlendOp: VK_BLEND_OP_ADD, | |
| 906 ) | |
| 907 colorBlending = VkPipelineColorBlendStateCreateInfo( | |
| 908 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | |
| 909 logicOpEnable: false, | |
| 910 attachmentCount: 1, | |
| 911 pAttachments: addr(colorBlendAttachment), | |
| 912 ) | |
| 913 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
| 914 dynamicState = VkPipelineDynamicStateCreateInfo( | |
| 915 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
| 916 dynamicStateCount: dynamicStates.len.uint32, | |
| 917 pDynamicStates: dynamicStates.ToCPointer, | |
| 918 ) | |
| 919 let createInfo = VkGraphicsPipelineCreateInfo( | |
| 920 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | |
| 921 stageCount: 2, | |
| 922 pStages: stages.ToCPointer, | |
| 923 pVertexInputState: addr(vertexInputInfo), | |
| 924 pInputAssemblyState: addr(inputAssembly), | |
| 925 pViewportState: addr(viewportState), | |
| 926 pRasterizationState: addr(rasterizer), | |
| 927 pMultisampleState: addr(multisampling), | |
| 928 pDepthStencilState: nil, | |
| 929 pColorBlendState: addr(colorBlending), | |
| 930 pDynamicState: addr(dynamicState), | |
| 931 layout: result.layout, | |
| 932 renderPass: renderPass, | |
| 933 subpass: 0, | |
| 934 basePipelineHandle: VkPipeline(0), | |
| 935 basePipelineIndex: -1, | |
| 936 ) | |
| 937 checkVkResult vkCreateGraphicsPipelines( | |
| 938 vulkan.device, | |
| 939 VkPipelineCache(0), | |
| 940 1, | |
| 941 addr(createInfo), | |
| 942 nil, | |
| 943 addr(result.vk) | |
| 944 ) | |
| 945 | |
| 946 proc AllocateIndirectMemory(size: uint64): IndirectGPUMemory = | |
| 947 # chooses biggest memory type that has NOT VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | |
| 948 result.size = size | |
| 949 result.needsTransfer = true | |
| 950 | |
| 951 # find a good memory type | |
| 952 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
| 953 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties) | |
| 954 | |
| 955 var biggestHeap: uint64 = 0 | |
| 956 var memoryTypeIndex = high(uint32) | |
| 957 # try to find non-host-visible type | |
| 958 for i in 0'u32 ..< physicalProperties.memoryTypeCount: | |
| 959 if not (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in toEnums(physicalProperties.memoryTypes[i].propertyFlags)): | |
| 960 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 961 if size > biggestHeap: | |
| 962 biggestHeap = size | |
| 963 memoryTypeIndex = i | |
| 964 | |
| 965 # If we did not found a device-only memory type, let's just take the biggest overall | |
| 966 if memoryTypeIndex == high(uint32): | |
| 967 result.needsTransfer = false | |
| 968 for i in 0'u32 ..< physicalProperties.memoryTypeCount: | |
| 969 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 970 if size > biggestHeap: | |
| 971 biggestHeap = size | |
| 972 memoryTypeIndex = i | |
| 973 | |
| 974 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type" | |
| 975 var allocationInfo = VkMemoryAllocateInfo( | |
| 976 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
| 977 allocationSize: result.size, | |
| 978 memoryTypeIndex: memoryTypeIndex, | |
| 979 ) | |
| 980 checkVkResult vkAllocateMemory( | |
| 981 vulkan.device, | |
| 982 addr allocationInfo, | |
| 983 nil, | |
| 984 addr result.vk | |
| 985 ) | |
| 986 | |
| 987 proc AllocateDirectMemory(size: uint64): DirectGPUMemory = | |
| 988 result.size = size | |
| 989 result.needsFlush = true | |
| 990 | |
| 991 # find a good memory type | |
| 992 var physicalProperties: VkPhysicalDeviceMemoryProperties | |
| 993 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties) | |
| 994 | |
| 995 var biggestHeap: uint64 = 0 | |
| 996 var memoryTypeIndex = high(uint32) | |
| 997 # try to find host-visible type | |
| 998 for i in 0 ..< physicalProperties.memoryTypeCount: | |
| 999 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags) | |
| 1000 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags: | |
| 1001 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size | |
| 1002 if size > biggestHeap: | |
| 1003 biggestHeap = size | |
| 1004 memoryTypeIndex = i | |
| 1005 result.needsFlush = not (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT in flags) | |
| 1006 | |
| 1007 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type" | |
| 1008 var allocationInfo = VkMemoryAllocateInfo( | |
| 1009 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | |
| 1010 allocationSize: result.size, | |
| 1011 memoryTypeIndex: GetDirectMemoryTypeIndex(), | |
| 1012 ) | |
| 1013 checkVkResult vkAllocateMemory( | |
| 1014 vulkan.device, | |
| 1015 addr allocationInfo, | |
| 1016 nil, | |
| 1017 addr result.vk | |
| 1018 ) | |
| 1019 checkVkResult vkMapMemory( | |
| 1020 device = vulkan.device, | |
| 1021 memory = result.vk, | |
| 1022 offset = 0'u64, | |
| 1023 size = result.size, | |
| 1024 flags = VkMemoryMapFlags(0), | |
| 1025 ppData = addr(result.data) | |
| 1026 ) | |
| 1027 | |
| 1028 proc AllocateIndirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) = | |
| 1029 if size == 0: | |
| 1030 return | |
| 1031 var buffer = Buffer[IndirectGPUMemory](size: size) | |
| 1032 | |
| 1033 let usageFlags = case btype: | |
| 1034 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1035 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1036 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1037 | |
| 1038 # iterate through memory areas to find big enough free space | |
| 1039 # TODO: dynamically expand memory allocations | |
| 1040 for (memory, usedOffset) in renderData.indirectMemory.mitems: | |
| 1041 if memory.size - usedOffset >= size: | |
| 1042 buffer.offset = usedOffset | |
| 1043 # create buffer | |
| 1044 var createInfo = VkBufferCreateInfo( | |
| 1045 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
| 1046 flags: VkBufferCreateFlags(0), | |
| 1047 size: buffer.size, | |
| 1048 usage: toBits(usageFlags), | |
| 1049 sharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
| 1050 ) | |
| 1051 checkVkResult vkCreateBuffer( | |
| 1052 device = vulkan.device, | |
| 1053 pCreateInfo = addr createInfo, | |
| 1054 pAllocator = nil, | |
| 1055 pBuffer = addr(buffer.vk) | |
| 1056 ) | |
| 1057 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset) | |
| 1058 renderData.indirectBuffers.add (buffer, btype, 0'u64) | |
| 1059 # update memory area offset | |
| 1060 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT) | |
| 1061 return | |
| 1062 | |
| 1063 assert false, "Did not find allocated memory region with enough space" | |
| 1064 | |
| 1065 proc AllocateDirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) = | |
| 1066 if size == 0: | |
| 1067 return | |
| 1068 | |
| 1069 var buffer = Buffer[DirectGPUMemory](size: size) | |
| 1070 | |
| 1071 let usageFlags = case btype: | |
| 1072 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1073 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1074 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] | |
| 1075 | |
| 1076 # iterate through memory areas to find big enough free space | |
| 1077 # TODO: dynamically expand memory allocations | |
| 1078 for (memory, usedOffset) in renderData.directMemory.mitems: | |
| 1079 if memory.size - usedOffset >= size: | |
| 1080 buffer.offset = usedOffset | |
| 1081 # create buffer | |
| 1082 var createInfo = VkBufferCreateInfo( | |
| 1083 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | |
| 1084 flags: VkBufferCreateFlags(0), | |
| 1085 size: buffer.size, | |
| 1086 usage: toBits(usageFlags), | |
| 1087 sharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
| 1088 ) | |
| 1089 checkVkResult vkCreateBuffer( | |
| 1090 device = vulkan.device, | |
| 1091 pCreateInfo = addr createInfo, | |
| 1092 pAllocator = nil, | |
| 1093 pBuffer = addr(buffer.vk) | |
| 1094 ) | |
| 1095 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset) | |
| 1096 renderData.directBuffers.add (buffer, btype, 0'u64) | |
| 1097 # update memory area offset | |
| 1098 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT) | |
| 1099 return | |
| 1100 | |
| 1101 assert false, "Did not find allocated memory region with enough space" | |
| 1102 | |
| 1103 proc InitRenderData(descriptorPoolLimit = 1024'u32): RenderData = | |
| 1104 # allocate descriptor pools | |
| 1105 var poolSizes = [ | |
| 1106 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit), | |
| 1107 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit), | |
| 1108 ] | |
| 1109 var poolInfo = VkDescriptorPoolCreateInfo( | |
| 1110 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
| 1111 poolSizeCount: poolSizes.len.uint32, | |
| 1112 pPoolSizes: poolSizes.ToCPointer, | |
| 1113 maxSets: descriptorPoolLimit, | |
| 1114 ) | |
| 1115 checkVkResult vkCreateDescriptorPool(vulkan.device, addr(poolInfo), nil, addr(result.descriptorPool)) | |
| 1116 | |
| 1117 # allocate some memory | |
| 1118 var initialAllocationSize = 1_000_000_000'u64 # TODO: make this more dynamic or something? | |
| 1119 result.indirectMemory = @[(memory: AllocateIndirectMemory(size = initialAllocationSize), usedOffset: 0'u64)] | |
| 1120 result.directMemory = @[(memory: AllocateDirectMemory(size = initialAllocationSize), usedOffset: 0'u64)] | |
| 1121 | |
| 1122 proc FlushDirectMemory(renderData: RenderData) = | |
| 1123 var flushRegions = newSeqOfCap[VkMappedMemoryRange](renderData.directMemory.len) | |
| 1124 for entry in renderData.directMemory: | |
| 1125 if entry.usedOffset > 0: | |
| 1126 flushRegions.add VkMappedMemoryRange( | |
| 1127 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, | |
| 1128 memory: entry.memory.vk, | |
| 1129 size: entry.usedOffset, | |
| 1130 ) | |
| 1131 if flushRegions.len > 0: | |
| 1132 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, flushRegions.len.uint32, flushRegions.ToCPointer()) | |
| 1133 | |
| 1134 # For the Get*BufferSize: | |
| 1135 # BUFFER_ALIGNMENT is just added for a rough estimate, to ensure we have enough space to align when binding | |
| 1136 proc GetIndirectBufferSizes[T](data: T): uint64 = | |
| 1137 for name, value in fieldPairs(data): | |
| 1138 when not hasCustomPragma(value, VertexIndices): | |
| 1139 when typeof(value) is GPUData: | |
| 1140 when UsesIndirectMemory(value): | |
| 1141 result += value.size + BUFFER_ALIGNMENT | |
| 1142 proc GetIndirectBufferSizes(data: DescriptorSet): uint64 = | |
| 1143 GetIndirectBufferSizes(data.data) | |
| 1144 proc GetDirectBufferSizes[T](data: T): uint64 = | |
| 1145 for name, value in fieldPairs(data): | |
| 1146 when not hasCustomPragma(value, VertexIndices): | |
| 1147 when typeof(value) is GPUData: | |
| 1148 when UsesDirectMemory(value): | |
| 1149 result += value.size + BUFFER_ALIGNMENT | |
| 1150 proc GetDirectBufferSizes(data: DescriptorSet): uint64 = | |
| 1151 GetDirectBufferSizes(data.data) | |
| 1152 proc GetIndirectIndexBufferSizes[T](data: T): uint64 = | |
| 1153 for name, value in fieldPairs(data): | |
| 1154 when hasCustomPragma(value, VertexIndices): | |
| 1155 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray" | |
| 1156 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32 | |
| 1157 when UsesIndirectMemory(value): | |
| 1158 result += value.size + BUFFER_ALIGNMENT | |
| 1159 proc GetDirectIndexBufferSizes[T](data: T): uint64 = | |
| 1160 for name, value in fieldPairs(data): | |
| 1161 when hasCustomPragma(value, VertexIndices): | |
| 1162 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray" | |
| 1163 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32 | |
| 1164 when UsesDirectMemory(value): | |
| 1165 result += value.size + BUFFER_ALIGNMENT | |
| 1166 | |
| 1167 proc AssignIndirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) = | |
| 1168 for name, value in fieldPairs(data): | |
| 1169 when typeof(value) is GPUData: | |
| 1170 when UsesIndirectMemory(value): | |
| 1171 # find next buffer of correct type with enough free space | |
| 1172 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices): | |
| 1173 var foundBuffer = false | |
| 1174 for (buffer, bt, offset) in renderData.indirectBuffers.mitems: | |
| 1175 if bt == btype and buffer.size - offset >= value.size: | |
| 1176 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned" | |
| 1177 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created" | |
| 1178 value.buffer = buffer | |
| 1179 value.offset = offset | |
| 1180 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT) | |
| 1181 foundBuffer = true | |
| 1182 break | |
| 1183 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'" | |
| 1184 proc AssignIndirectBuffers(renderdata: var RenderData, btype: BufferType, data: var DescriptorSet) = | |
| 1185 AssignIndirectBuffers(renderdata, btype, data.data) | |
| 1186 proc AssignDirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) = | |
| 1187 for name, value in fieldPairs(data): | |
| 1188 when typeof(value) is GPUData: | |
| 1189 when UsesDirectMemory(value): | |
| 1190 # find next buffer of correct type with enough free space | |
| 1191 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices): | |
| 1192 var foundBuffer = false | |
| 1193 for (buffer, bt, offset) in renderData.directBuffers.mitems: | |
| 1194 if bt == btype and buffer.size - offset >= value.size: | |
| 1195 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned" | |
| 1196 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created" | |
| 1197 value.buffer = buffer | |
| 1198 value.offset = offset | |
| 1199 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT) | |
| 1200 foundBuffer = true | |
| 1201 break | |
| 1202 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'" | |
| 1203 proc AssignDirectBuffers(renderdata: var RenderData, btype: BufferType, data: var DescriptorSet) = | |
| 1204 AssignDirectBuffers(renderdata, btype, data.data) | |
| 1205 | |
| 1206 proc HasGPUValueField[T](name: static string): bool {.compileTime.} = | |
| 1207 for fieldname, value in default(T).fieldPairs(): | |
| 1208 when typeof(value) is GPUValue and fieldname == name: return true | |
| 1209 return false | |
| 1210 | |
| 1211 template WithGPUValueField(obj: object, name: static string, fieldvalue, body: untyped): untyped = | |
| 1212 # HasGPUValueField MUST be used to check if this is supported | |
| 1213 for fieldname, value in obj.fieldPairs(): | |
| 1214 when fieldname == name: | |
| 1215 block: | |
| 1216 let `fieldvalue` {.inject.} = value | |
| 1217 body | |
| 1207 | 1218 |
| 1208 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = | 1219 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
| 1209 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) | 1220 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.vk) |
| 1210 #[ | 1221 #[ |
| 1211 commandBuffer.vkCmdBindDescriptorSets( | 1222 commandBuffer.vkCmdBindDescriptorSets( |
| 1212 VK_PIPELINE_BIND_POINT_GRAPHICS, | 1223 VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1213 pipeline.layout, | 1224 pipeline.layout, |
| 1214 0, | 1225 0, |
| 1217 0, | 1228 0, |
| 1218 nil, | 1229 nil, |
| 1219 ) | 1230 ) |
| 1220 ]# | 1231 ]# |
| 1221 | 1232 |
| 1222 proc AssertCompatible(TShader, TMesh, TInstance, TUniforms, TGlobals: typedesc) = | 1233 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals, TMaterial: typedesc) = |
| 1223 var descriptorSetCount = 0 | 1234 var descriptorSetCount = 0 |
| 1224 | 1235 |
| 1225 for inputName, inputValue in default(TShader).fieldPairs: | 1236 for shaderAttributeName, shaderAttribute in default(TShader).fieldPairs: |
| 1226 var foundField = false | 1237 var foundField = false |
| 1227 | 1238 |
| 1228 # Vertex input data | 1239 # Vertex input data |
| 1229 when hasCustomPragma(inputValue, VertexAttribute): | 1240 when hasCustomPragma(shaderAttribute, VertexAttribute): |
| 1230 assert typeof(inputValue) is SupportedGPUType | 1241 assert typeof(shaderAttribute) is SupportedGPUType |
| 1231 for meshName, meshValue in default(TMesh).fieldPairs: | 1242 for meshName, meshValue in default(TMesh).fieldPairs: |
| 1232 when meshName == inputName: | 1243 when meshName == shaderAttributeName: |
| 1233 assert meshValue is GPUArray, "Mesh attribute '" & meshName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(meshValue)) | 1244 assert meshValue is GPUArray, "Mesh attribute '" & meshName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(meshValue)) |
| 1234 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | 1245 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & shaderAttributeName & "' has been found more than once" |
| 1235 assert elementType(meshValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue.data)) & "'" | 1246 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)) & "'" |
| 1236 foundField = true | 1247 foundField = true |
| 1237 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "'" | 1248 assert foundField, "Shader input '" & tt.name(TShader) & "." & shaderAttributeName & ": " & tt.name(typeof(shaderAttribute)) & "' not found in '" & tt.name(TMesh) & "'" |
| 1238 | 1249 |
| 1239 # Instance input data | 1250 # Instance input data |
| 1240 elif hasCustomPragma(inputValue, InstanceAttribute): | 1251 elif hasCustomPragma(shaderAttribute, InstanceAttribute): |
| 1241 assert typeof(inputValue) is SupportedGPUType | 1252 assert typeof(shaderAttribute) is SupportedGPUType |
| 1242 for instanceName, instanceValue in default(TInstance).fieldPairs: | 1253 for instanceName, instanceValue in default(TInstance).fieldPairs: |
| 1243 when instanceName == inputName: | 1254 when instanceName == shaderAttributeName: |
| 1244 assert instanceValue is GPUArray, "Instance attribute '" & instanceName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(instanceName)) | 1255 assert instanceValue is GPUArray, "Instance attribute '" & instanceName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(instanceName)) |
| 1245 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | 1256 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & shaderAttributeName & "' has been found more than once" |
| 1246 assert elementType(instanceValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but instance attribute is of type '" & tt.name(elementType(instanceValue.data)) & "'" | 1257 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)) & "'" |
| 1247 foundField = true | 1258 foundField = true |
| 1248 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TInstance) & "'" | 1259 assert foundField, "Shader input '" & tt.name(TShader) & "." & shaderAttributeName & ": " & tt.name(typeof(shaderAttribute)) & "' not found in '" & tt.name(TInstance) & "'" |
| 1249 | 1260 |
| 1250 | 1261 # descriptors |
| 1251 elif hasCustomPragma(inputValue, DescriptorSet): | 1262 elif typeof(shaderAttribute) is DescriptorSet: |
| 1252 assert descriptorSetCount < MAX_DESCRIPTORSETS, &"{tt.name(TShader)}: maximum {MAX_DESCRIPTORSETS} allowed" | 1263 assert descriptorSetCount <= DescriptorSetType.high.int, &"{tt.name(TShader)}: maximum {DescriptorSetType.high} allowed" |
| 1253 descriptorSetCount.inc | 1264 descriptorSetCount.inc |
| 1254 echo "DescriptorSet: ", inputName | 1265 |
| 1255 | 1266 |
| 1256 for descriptorName, descriptorValue in inputValue.fieldPairs(): | 1267 when shaderAttribute.sType == GlobalSet: |
| 1257 when typeof(descriptorValue) is Texture: | 1268 assert shaderAttribute.sType == default(TGlobals).sType, "Shader has global descriptor set of type '" & $shaderAttribute.sType & "' but matching provided type is '" & $default(TGlobals).sType & "'" |
| 1258 echo " Texture: ", descriptorName | 1269 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) |
| 1259 elif typeof(descriptorValue) is GPUValue: | 1270 elif shaderAttribute.sType == MaterialSet: |
| 1260 echo " Uniform block: ", descriptorName | 1271 assert shaderAttribute.sType == default(TMaterial).sType, "Shader has material descriptor set of type '" & $shaderAttribute.sType & "' but matching provided type is '" & $default(TMaterial).sType & "'" |
| 1261 | 1272 assert typeof(shaderAttribute) is TMaterial, "Shader has materialdescriptor type '" & tt.name(get(genericParams(typeof(shaderAttribute)), 0)) & "' but provided type is " & tt.name(TMaterial) |
| 1262 #[ | 1273 |
| 1263 # Texture | 1274 |
| 1264 elif typeof(inputValue) is Texture: | 1275 proc Render[TShader, TGlobals, TMaterial, TMesh, TInstance]( |
| 1265 for uniformName, uniformValue in default(TUniforms).fieldPairs: | |
| 1266 when uniformName == inputName: | |
| 1267 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 1268 assert typeof(uniformValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue)) & "'" | |
| 1269 foundField = true | |
| 1270 for globalName, globalValue in default(TGlobals).fieldPairs: | |
| 1271 when globalName == inputName: | |
| 1272 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 1273 assert typeof(globalValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue)) & "'" | |
| 1274 foundField = true | |
| 1275 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
| 1276 | |
| 1277 # Uniform block | |
| 1278 elif typeof(inputValue) is object: | |
| 1279 for uniformName, uniformValue in default(TUniforms).fieldPairs: | |
| 1280 when uniformName == inputName: | |
| 1281 assert uniformValue is GPUValue, "global attribute '" & uniformName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(uniformValue)) | |
| 1282 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 1283 assert typeof(uniformValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue.data)) & "'" | |
| 1284 foundField = true | |
| 1285 for globalName, globalValue in default(TGlobals).fieldPairs: | |
| 1286 when globalName == inputName: | |
| 1287 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue)) | |
| 1288 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 1289 assert typeof(globalValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue.data)) & "'" | |
| 1290 foundField = true | |
| 1291 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
| 1292 | |
| 1293 # array | |
| 1294 elif typeof(inputValue) is array: | |
| 1295 | |
| 1296 # texture-array | |
| 1297 when elementType(inputValue) is Texture: | |
| 1298 for uniformName, uniformValue in default(TUniforms).fieldPairs: | |
| 1299 when uniformName == inputName: | |
| 1300 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 1301 assert typeof(uniformValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue)) & "'" | |
| 1302 foundField = true | |
| 1303 for globalName, globalValue in default(TGlobals).fieldPairs: | |
| 1304 when globalName == inputName: | |
| 1305 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 1306 assert typeof(globalValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue)) & "'" | |
| 1307 foundField = true | |
| 1308 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
| 1309 | |
| 1310 # uniform-block array | |
| 1311 elif elementType(inputValue) is object: | |
| 1312 for uniformName, uniformValue in default(TUniforms).fieldPairs: | |
| 1313 when uniformName == inputName: | |
| 1314 assert uniformValue is GPUValue, "global attribute '" & uniformName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(uniformValue)) | |
| 1315 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 1316 assert typeof(uniformValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue.data)) & "'" | |
| 1317 foundField = true | |
| 1318 for globalName, globalValue in default(TGlobals).fieldPairs: | |
| 1319 when globalName == inputName: | |
| 1320 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue)) | |
| 1321 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" | |
| 1322 assert typeof(globalValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue.data)) & "'" | |
| 1323 foundField = true | |
| 1324 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" | |
| 1325 ]# | |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 proc Render[TShader, TUniforms, TGlobals, TMesh, TInstance]( | |
| 1333 commandBuffer: VkCommandBuffer, | 1276 commandBuffer: VkCommandBuffer, |
| 1334 pipeline: Pipeline[TShader], | 1277 pipeline: Pipeline[TShader], |
| 1335 uniforms: TUniforms, | 1278 globalSet: TGlobals, |
| 1336 globals: TGlobals, | 1279 materialSet: TMaterial, |
| 1337 mesh: TMesh, | 1280 mesh: TMesh, |
| 1338 instances: TInstance, | 1281 instances: TInstance, |
| 1339 ) = | 1282 ) = |
| 1340 static: AssertCompatible(TShader, TMesh, TInstance, TUniforms, TGlobals) | 1283 static: AssertCompatible(TShader, TMesh, TInstance, TGlobals, TMaterial) |
| 1341 #[ | 1284 #[ |
| 1342 if renderable.vertexBuffers.len > 0: | 1285 if renderable.vertexBuffers.len > 0: |
| 1343 commandBuffer.vkCmdBindVertexBuffers( | 1286 commandBuffer.vkCmdBindVertexBuffers( |
| 1344 firstBinding = 0'u32, | 1287 firstBinding = 0'u32, |
| 1345 bindingCount = uint32(renderable.vertexBuffers.len), | 1288 bindingCount = uint32(renderable.vertexBuffers.len), |
| 1405 test {.Pass.}: float32 | 1348 test {.Pass.}: float32 |
| 1406 test1 {.PassFlat.}: Vec3f | 1349 test1 {.PassFlat.}: Vec3f |
| 1407 # output | 1350 # output |
| 1408 color {.ShaderOutput.}: Vec4f | 1351 color {.ShaderOutput.}: Vec4f |
| 1409 # descriptor sets | 1352 # descriptor sets |
| 1410 globals {.DescriptorSet.}: GlobalsA | 1353 globals: DescriptorSet[GlobalsA, GlobalSet] |
| 1411 uniforms {.DescriptorSet.}: UniformsA | 1354 uniforms: DescriptorSet[UniformsA, MaterialSet] |
| 1412 # code | 1355 # code |
| 1413 vertexCode: string = "void main() {}" | 1356 vertexCode: string = "void main() {}" |
| 1414 fragmentCode: string = "void main() {}" | 1357 fragmentCode: string = "void main() {}" |
| 1415 | 1358 |
| 1416 let w = CreateWindow("test2") | 1359 let w = CreateWindow("test2") |
| 1443 ) | 1386 ) |
| 1444 | 1387 |
| 1445 var myMesh1 = MeshA( | 1388 var myMesh1 = MeshA( |
| 1446 position: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | 1389 position: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), |
| 1447 ) | 1390 ) |
| 1448 var uniforms1 = UniformsA( | 1391 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( |
| 1449 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory](data: [ | 1392 data: UniformsA( |
| 1450 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | 1393 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory](data: [ |
| 1451 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | 1394 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), |
| 1452 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | 1395 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), |
| 1396 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
| 1453 ]), | 1397 ]), |
| 1454 materialTextures: [ | 1398 materialTextures: [ |
| 1455 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[255'u8, 0'u8, 0'u8, 255'u8]])), | 1399 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[255'u8, 0'u8, 0'u8, 255'u8]])), |
| 1456 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 255'u8, 0'u8, 255'u8]])), | 1400 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 255'u8, 0'u8, 255'u8]])), |
| 1457 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 0'u8, 255'u8, 255'u8]])), | 1401 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 0'u8, 255'u8, 255'u8]])), |
| 1458 ] | 1402 ] |
| 1459 ) | 1403 ) |
| 1404 ) | |
| 1460 var instances1 = InstanceA( | 1405 var instances1 = InstanceA( |
| 1461 rotation: GPUArray[Vec4f, IndirectGPUMemory](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | 1406 rotation: GPUArray[Vec4f, IndirectGPUMemory](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), |
| 1462 objPosition: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | 1407 objPosition: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), |
| 1463 ) | 1408 ) |
| 1464 var myGlobals: GlobalsA | 1409 var myGlobals = DescriptorSet[GlobalsA, GlobalSet]() |
| 1465 | 1410 |
| 1466 # setup for rendering (TODO: swapchain & framebuffers) | 1411 # setup for rendering (TODO: swapchain & framebuffers) |
| 1467 let renderpass = CreateRenderPass(GetSurfaceFormat()) | 1412 let renderpass = CreateRenderPass(GetSurfaceFormat()) |
| 1468 | 1413 |
| 1469 # shaders | 1414 # shaders |
| 1543 renderdata.FlushDirectMemory() | 1488 renderdata.FlushDirectMemory() |
| 1544 | 1489 |
| 1545 | 1490 |
| 1546 # descriptors | 1491 # descriptors |
| 1547 # TODO: I think we can write and assign descriptors directly after creation | 1492 # TODO: I think we can write and assign descriptors directly after creation |
| 1548 var s1 = CreateDescriptorSet(renderdata, pipeline1, myGlobals, GlobalSet) | 1493 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[GlobalSet], myGlobals) |
| 1549 var s2 = CreateDescriptorSet(renderdata, pipeline1, uniforms1, MaterialSet) | 1494 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[MaterialSet], uniforms1) |
| 1550 # WriteDescriptors[ShaderA, UniformsA, GlobalsA](renderdata, uniforms1, myGlobals) | 1495 # WriteDescriptors[ShaderA, UniformsA, GlobalsA](renderdata, uniforms1, myGlobals) |
| 1551 | |
| 1552 | 1496 |
| 1553 | 1497 |
| 1554 # command buffer | 1498 # command buffer |
| 1555 var | 1499 var |
| 1556 commandBufferPool: VkCommandPool | 1500 commandBufferPool: VkCommandPool |
| 1620 block: | 1564 block: |
| 1621 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) | 1565 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) |
| 1622 | 1566 |
| 1623 # render object, will be loop | 1567 # render object, will be loop |
| 1624 block: | 1568 block: |
| 1625 Render(cmd, pipeline1, uniforms1, myGlobals, myMesh1, instances1) | 1569 Render(cmd, pipeline1, myGlobals, uniforms1, myMesh1, instances1) |
| 1626 | 1570 |
| 1627 vkCmdEndRenderPass(cmd) | 1571 vkCmdEndRenderPass(cmd) |
| 1628 checkVkResult cmd.vkEndCommandBuffer() | 1572 checkVkResult cmd.vkEndCommandBuffer() |
