Mercurial > games > semicongine
comparison static_utils.nim @ 1164:7b4d4d85d9f5 compiletime-tests
add: descriptor sets allocation
author | sam <sam@basx.dev> |
---|---|
date | Sat, 22 Jun 2024 02:26:16 +0700 |
parents | 438d32d8b14f |
children | 58694b30b9cb |
comparison
equal
deleted
inserted
replaced
1163:438d32d8b14f | 1164:7b4d4d85d9f5 |
---|---|
2 import std/enumerate | 2 import std/enumerate |
3 import std/hashes | 3 import std/hashes |
4 import std/macros | 4 import std/macros |
5 import std/strformat | 5 import std/strformat |
6 import std/strutils | 6 import std/strutils |
7 import std/sequtils | |
7 import std/typetraits as tt | 8 import std/typetraits as tt |
8 | 9 |
9 import semicongine/core/utils | 10 import semicongine/core/utils |
10 import semicongine/core/imagetypes | 11 import semicongine/core/imagetypes |
11 import semicongine/core/vector | 12 import semicongine/core/vector |
204 indexCount: uint32 | 205 indexCount: uint32 |
205 indexBufferOffset: VkDeviceSize | 206 indexBufferOffset: VkDeviceSize |
206 Pipeline[TShader] = object | 207 Pipeline[TShader] = object |
207 pipeline: VkPipeline | 208 pipeline: VkPipeline |
208 layout: VkPipelineLayout | 209 layout: VkPipelineLayout |
209 descriptorSets: array[INFLIGHTFRAMES, seq[VkDescriptorSet]] | 210 descriptorSets: array[INFLIGHTFRAMES, VkDescriptorSet] |
210 | 211 |
211 converter toVkIndexType(indexType: IndexType): VkIndexType = | 212 converter toVkIndexType(indexType: IndexType): VkIndexType = |
212 case indexType: | 213 case indexType: |
213 of None: VK_INDEX_TYPE_NONE_KHR | 214 of None: VK_INDEX_TYPE_NONE_KHR |
214 of UInt8: VK_INDEX_TYPE_UINT8_EXT | 215 of UInt8: VK_INDEX_TYPE_UINT8_EXT |
366 # assumptions/limitations: | 367 # assumptions/limitations: |
367 # - we are only using vertex and fragment shaders (2 stages) | 368 # - we are only using vertex and fragment shaders (2 stages) |
368 # - we only support one subpass | 369 # - we only support one subpass |
369 # = we only support one Uniform-Block | 370 # = we only support one Uniform-Block |
370 | 371 |
372 # create pipeline | |
371 var layoutbindings: seq[VkDescriptorSetLayoutBinding] | 373 var layoutbindings: seq[VkDescriptorSetLayoutBinding] |
372 var descriptorBindingNumber = 0'u32 | 374 var descriptorBindingNumber = 0'u32 |
373 ForDescriptorFields(default(TShader), descriptorType, descriptorCount): | 375 ForDescriptorFields(default(TShader), descriptorType, descriptorCount): |
374 layoutbindings.add VkDescriptorSetLayoutBinding( | 376 layoutbindings.add VkDescriptorSetLayoutBinding( |
375 binding: descriptorBindingNumber, | 377 binding: descriptorBindingNumber, |
519 addr(createInfo), | 521 addr(createInfo), |
520 nil, | 522 nil, |
521 addr(result.pipeline) | 523 addr(result.pipeline) |
522 ) | 524 ) |
523 | 525 |
526 # create descriptors, one per frame-in-flight | |
527 let nSamplers = 0'u32 | |
528 let nUniformBuffers = 0'u32 | |
529 | |
530 if nSamplers + nUniformBuffers > 0: | |
531 var poolSizes: seq[VkDescriptorPoolSize] | |
532 if nUniformBuffers > 0: | |
533 poolSizes.add VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: nSamplers * INFLIGHTFRAMES.uint32) | |
534 if nSamplers > 0: | |
535 poolSizes.add VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: nUniformBuffers * INFLIGHTFRAMES.uint32) | |
536 var poolInfo = VkDescriptorPoolCreateInfo( | |
537 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
538 poolSizeCount: uint32(poolSizes.len), | |
539 pPoolSizes: poolSizes.ToCPointer, | |
540 maxSets: (nUniformBuffers + nSamplers) * INFLIGHTFRAMES.uint32 * 2, # good formula? no idea... | |
541 ) | |
542 var pool: VkDescriptorPool | |
543 checkVkResult vkCreateDescriptorPool(device, addr(poolInfo), nil, addr(pool)) | |
544 | |
545 var layouts = newSeqWith(result.descriptorSets.len, descriptorSetLayout) | |
546 var allocInfo = VkDescriptorSetAllocateInfo( | |
547 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
548 descriptorPool: pool, | |
549 descriptorSetCount: uint32(layouts.len), | |
550 pSetLayouts: layouts.ToCPointer, | |
551 ) | |
552 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), result.descriptorSets.ToCPointer) | |
553 | |
524 proc CreateRenderable[TMesh, TInstance]( | 554 proc CreateRenderable[TMesh, TInstance]( |
525 mesh: TMesh, | 555 mesh: TMesh, |
526 instance: TInstance, | 556 instance: TInstance, |
527 buffers: RenderBuffers, | 557 buffers: RenderBuffers, |
528 ): Renderable[TMesh, TInstance] = | 558 ): Renderable[TMesh, TInstance] = |
529 result.indexType = None | 559 result.indexType = None |
530 | 560 |
531 proc Bind(pipeline: Pipeline, commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = | 561 proc Bind(pipeline: Pipeline, commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
532 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) | 562 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) |
533 commandBuffer.vkCmdBindDescriptorSets( | 563 if pipeline.descriptorSets[currentFrameInFlight] != VkDescriptorSet(0): |
534 VK_PIPELINE_BIND_POINT_GRAPHICS, | 564 commandBuffer.vkCmdBindDescriptorSets( |
535 pipeline.layout, | 565 VK_PIPELINE_BIND_POINT_GRAPHICS, |
536 0, | 566 pipeline.layout, |
537 pipeline.descriptorSets[currentFrameInFlight].len.uint32, | 567 0, |
538 pipeline.descriptorSets[currentFrameInFlight].ToCPointer, | 568 1, |
539 0, | 569 addr pipeline.descriptorSets[currentFrameInFlight], |
540 nil, | 570 0, |
541 ) | 571 nil, |
572 ) | |
542 | 573 |
543 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals: typedesc) = | 574 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals: typedesc) = |
544 # assert seq-fields of TMesh|TInstance == seq-fields of TShader | 575 # assert seq-fields of TMesh|TInstance == seq-fields of TShader |
545 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors | 576 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors |
546 for inputName, inputValue in default(TShader).fieldPairs: | 577 for inputName, inputValue in default(TShader).fieldPairs: |
592 pipeline: Pipeline[TShader], | 623 pipeline: Pipeline[TShader], |
593 renderable: Renderable[TMesh, TInstance], | 624 renderable: Renderable[TMesh, TInstance], |
594 globals: TGlobals, | 625 globals: TGlobals, |
595 commandBuffer: VkCommandBuffer, | 626 commandBuffer: VkCommandBuffer, |
596 ) = | 627 ) = |
597 static: | 628 static: AssertCompatible(TShader, TMesh, TInstance, TGlobals) |
598 AssertCompatible(TShader, TMesh, TInstance, TGlobals) | 629 if renderable.vertexBuffers.len > 0: |
599 commandBuffer.vkCmdBindVertexBuffers( | 630 commandBuffer.vkCmdBindVertexBuffers( |
600 firstBinding = 0'u32, | 631 firstBinding = 0'u32, |
601 bindingCount = uint32(renderable.vertexBuffers.len), | 632 bindingCount = uint32(renderable.vertexBuffers.len), |
602 pBuffers = renderable.vertexBuffers.ToCPointer(), | 633 pBuffers = renderable.vertexBuffers.ToCPointer(), |
603 pOffsets = renderable.bufferOffsets.ToCPointer() | 634 pOffsets = renderable.bufferOffsets.ToCPointer() |
604 ) | 635 ) |
605 if renderable.indexType != None: | 636 if renderable.indexType != None: |
606 commandBuffer.vkCmdBindIndexBuffer( | 637 commandBuffer.vkCmdBindIndexBuffer( |
607 renderable.indexBuffer, | 638 renderable.indexBuffer, |
608 renderable.indexBufferOffset, | 639 renderable.indexBufferOffset, |
609 renderable.indexType, | 640 renderable.indexType, |