Mercurial > games > semicongine
comparison static_utils.nim @ 1172:2e0b527c2753 compiletime-tests
merge?
author | sam <sam@basx.dev> |
---|---|
date | Thu, 27 Jun 2024 20:52:50 +0700 |
parents | 58694b30b9cb |
children | fafc2f14da0b |
comparison
equal
deleted
inserted
replaced
1171:dc6e1660831d | 1172:2e0b527c2753 |
---|---|
214 of None: VK_INDEX_TYPE_NONE_KHR | 214 of None: VK_INDEX_TYPE_NONE_KHR |
215 of UInt8: VK_INDEX_TYPE_UINT8_EXT | 215 of UInt8: VK_INDEX_TYPE_UINT8_EXT |
216 of UInt16: VK_INDEX_TYPE_UINT16 | 216 of UInt16: VK_INDEX_TYPE_UINT16 |
217 of UInt32: VK_INDEX_TYPE_UINT32 | 217 of UInt32: VK_INDEX_TYPE_UINT32 |
218 | 218 |
219 proc CreateRenderPass*( | |
220 device: VkDevice, | |
221 format: VkFormat, | |
222 ): VkRenderPass = | |
223 | |
224 var | |
225 attachments = @[VkAttachmentDescription( | |
226 format: format, | |
227 samples: VK_SAMPLE_COUNT_1_BIT, | |
228 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
229 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
230 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
231 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
232 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
233 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
234 )] | |
235 dependencies = @[VkSubpassDependency( | |
236 srcSubpass: VK_SUBPASS_EXTERNAL, | |
237 dstSubpass: 0, | |
238 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], | |
239 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
240 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
241 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
242 )] | |
243 outputs = @[ | |
244 VkAttachmentReference( | |
245 attachment: 0, | |
246 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
247 ) | |
248 ] | |
249 | |
250 var subpassesList = [ | |
251 VkSubpassDescription( | |
252 flags: VkSubpassDescriptionFlags(0), | |
253 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
254 inputAttachmentCount: 0, | |
255 pInputAttachments: nil, | |
256 colorAttachmentCount: uint32(outputs.len), | |
257 pColorAttachments: outputs.ToCPointer, | |
258 pResolveAttachments: nil, | |
259 pDepthStencilAttachment: nil, | |
260 preserveAttachmentCount: 0, | |
261 pPreserveAttachments: nil, | |
262 ) | |
263 ] | |
264 | |
265 var createInfo = VkRenderPassCreateInfo( | |
266 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
267 attachmentCount: uint32(attachments.len), | |
268 pAttachments: attachments.ToCPointer, | |
269 subpassCount: uint32(subpassesList.len), | |
270 pSubpasses: subpassesList.ToCPointer, | |
271 dependencyCount: uint32(dependencies.len), | |
272 pDependencies: dependencies.ToCPointer, | |
273 ) | |
274 checkVkResult device.vkCreateRenderPass(addr(createInfo), nil, addr(result)) | |
275 | |
219 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = | 276 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = |
220 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | 277 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = |
221 case stage | 278 case stage |
222 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | 279 of VK_SHADER_STAGE_VERTEX_BIT: "vert" |
223 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | 280 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" |
551 ) | 608 ) |
552 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), result.descriptorSets.ToCPointer) | 609 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), result.descriptorSets.ToCPointer) |
553 | 610 |
554 # write descriptor sets | 611 # write descriptor sets |
555 # TODO | 612 # TODO |
613 #[ | |
556 var descriptorSetWrites: seq[VkWriteDescriptorSet] | 614 var descriptorSetWrites: seq[VkWriteDescriptorSet] |
557 for XY in descriptors?: | 615 for XY in descriptors?: |
558 | 616 |
559 bufferInfos.add VkDescriptorBufferInfo( | 617 bufferInfos.add VkDescriptorBufferInfo( |
560 buffer: descriptor.buffer.vk, | 618 buffer: descriptor.buffer.vk, |
561 offset: descriptor.offset, | 619 offset: descriptor.offset, |
562 range: descriptor.size, | 620 range: descriptor.size, |
563 ) | 621 ) |
564 descriptorSetWrites.add VkWriteDescriptorSet( | 622 descriptorSetWrites.add VkWriteDescriptorSet( |
565 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | 623 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, |
566 dstSet: descriptorSet.vk, | 624 dstSet: descriptorSet.vk, |
567 dstBinding: i, | 625 dstBinding: i, |
568 dstArrayElement: 0, | 626 dstArrayElement: 0, |
569 descriptorType: descriptor.vkType, | 627 descriptorType: descriptor.vkType, |
570 descriptorCount: uint32(descriptor.count), | 628 descriptorCount: uint32(descriptor.count), |
571 pBufferInfo: addr bufferInfos[^1], | 629 pBufferInfo: addr bufferInfos[^1], |
572 ) | 630 ) |
573 | 631 vkUpdateDescriptorSets(device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) |
574 vkUpdateDescriptorSets(device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil) | 632 ]# |
633 | |
575 | 634 |
576 proc CreateRenderable[TMesh, TInstance]( | 635 proc CreateRenderable[TMesh, TInstance]( |
577 mesh: TMesh, | 636 mesh: TMesh, |
578 instance: TInstance, | 637 instance: TInstance, |
579 buffers: RenderBuffers, | 638 buffers: RenderBuffers, |
580 ): Renderable[TMesh, TInstance] = | 639 ): Renderable[TMesh, TInstance] = |
581 result.indexType = None | 640 result.indexType = None |
582 | 641 |
583 proc Bind(pipeline: Pipeline, commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = | 642 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
643 let a = pipeline.descriptorSets | |
644 echo a[^currentFrameInFlight] | |
584 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) | 645 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) |
585 if pipeline.descriptorSets[currentFrameInFlight] != VkDescriptorSet(0): | 646 if a[currentFrameInFlight] != VkDescriptorSet(0): |
586 commandBuffer.vkCmdBindDescriptorSets( | 647 commandBuffer.vkCmdBindDescriptorSets( |
587 VK_PIPELINE_BIND_POINT_GRAPHICS, | 648 VK_PIPELINE_BIND_POINT_GRAPHICS, |
588 pipeline.layout, | 649 pipeline.layout, |
589 0, | 650 0, |
590 1, | 651 1, |
680 when isMainModule: | 741 when isMainModule: |
681 import semicongine/platform/window | 742 import semicongine/platform/window |
682 import semicongine/vulkan/instance | 743 import semicongine/vulkan/instance |
683 import semicongine/vulkan/device | 744 import semicongine/vulkan/device |
684 import semicongine/vulkan/physicaldevice | 745 import semicongine/vulkan/physicaldevice |
685 import semicongine/vulkan/renderpass | 746 # import semicongine/vulkan/renderpass |
686 import semicongine/vulkan/commandbuffer | 747 import semicongine/vulkan/commandbuffer |
687 import std/options | 748 import std/options |
688 | 749 |
689 type | 750 type |
690 MaterialA = object | 751 MaterialA = object |
745 const shader = ShaderA() | 806 const shader = ShaderA() |
746 let shaderObject = d.vk.CompileShader(shader) | 807 let shaderObject = d.vk.CompileShader(shader) |
747 let rp = d.vk.CreateRenderPass(d.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format) | 808 let rp = d.vk.CreateRenderPass(d.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format) |
748 var p = CreatePipeline(d.vk, renderPass = rp, shaderObject) | 809 var p = CreatePipeline(d.vk, renderPass = rp, shaderObject) |
749 | 810 |
750 let commandBufferPool = d.CreateCommandBufferPool(d.FirstGraphicsQueue().get().family, INFLIGHTFRAMES) | 811 let commandBufferPool = d.CreateCommandBufferPool(d.FirstGraphicsQueue().get().family, INFLIGHTFRAMES.int) |
751 let cmd = commandBufferPool.buffers[0] | 812 let cmd = commandBufferPool.buffers[0] |
752 | 813 |
753 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) | 814 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) |
754 let beginInfo = VkCommandBufferBeginInfo( | 815 let beginInfo = VkCommandBufferBeginInfo( |
755 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | 816 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
756 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | 817 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), |
757 ) | 818 ) |
758 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) | 819 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) |
759 p.Bind(cmd, currentFrameInFlight = 0) | 820 Bind(p, cmd, currentFrameInFlight = 0) |
760 p.Render(r, g, cmd) | 821 Render(p, r, g, cmd) |
761 | 822 |
762 checkVkResult cmd.vkEndCommandBuffer() | 823 checkVkResult cmd.vkEndCommandBuffer() |