comparison tests/test_vulkan_wrapper.nim @ 106:9c3e3dcb0328

add: render main loop structure
author Sam <sam@basx.dev>
date Sun, 26 Mar 2023 01:56:19 +0700
parents 4059aa0d689b
children 2d0351a68a4e
comparison
equal deleted inserted replaced
105:4059aa0d689b 106:9c3e3dcb0328
65 ) 65 )
66 66
67 # setup render pipeline 67 # setup render pipeline
68 var surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat() 68 var surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
69 var renderpass = device.simpleForwardRenderPass(surfaceFormat.format) 69 var renderpass = device.simpleForwardRenderPass(surfaceFormat.format)
70 var (swapchain, res) = device.createSwapchain(renderpass, surfaceFormat) 70 var (swapchain, res) = device.createSwapchain(renderpass, surfaceFormat, device.firstGraphicsQueue().get().family)
71 if res != VK_SUCCESS: 71 if res != VK_SUCCESS:
72 raise newException(Exception, "Unable to create swapchain") 72 raise newException(Exception, "Unable to create swapchain")
73 # var framebuffers: seq[Framebuffer]
74 # for imageview in swapchain.imageviews:
75 # framebuffers.add device.createFramebuffer(renderpass, [imageview], swapchain.dimension)
76 73
77 # todo: could be create inside "device", but it would be nice to have nim v2 with support for circular dependencies first 74 # todo: could be create inside "device", but it would be nice to have nim v2 with support for circular dependencies first
78 var
79 commandPool = device.createCommandPool(family=device.firstGraphicsQueue().get().family, nBuffers=1)
80 imageAvailable = device.createSemaphore()
81 renderFinished = device.createSemaphore()
82 inflight = device.createFence()
83
84 const vertexBinary = shaderCode[Vertex, Uniforms, FragmentInput](stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;") 75 const vertexBinary = shaderCode[Vertex, Uniforms, FragmentInput](stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;")
85 const fragmentBinary = shaderCode[FragmentInput, void, Pixel](stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);") 76 const fragmentBinary = shaderCode[FragmentInput, void, Pixel](stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);")
86 var vertexshader = createShader[Vertex, Uniforms, FragmentInput](device, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary) 77 var
87 var fragmentshader = createShader[FragmentInput, void, Pixel](device, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary) 78 vertexshader = createShader[Vertex, Uniforms, FragmentInput](device, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary)
88 79 fragmentshader = createShader[FragmentInput, void, Pixel](device, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary)
89 var pipeline = renderpass.createPipeline(vertexshader, fragmentshader) 80 pipeline = renderpass.createPipeline(vertexshader, fragmentshader)
90 var descriptorPool = device.createDescriptorSetPool(@[(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1'u32)]) 81 descriptorPool = device.createDescriptorSetPool(@[(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1'u32)])
91 var descriptorSet = descriptorPool.allocateDescriptorSet(pipeline.descriptorSetLayout, 1) 82 descriptorSet = descriptorPool.allocateDescriptorSet(pipeline.descriptorSetLayout, 1)
92 83
93 echo "All successfull" 84 echo "All successfull"
85 echo swapchain.drawNextFrame(pipeline)
94 echo "Start cleanup" 86 echo "Start cleanup"
95 87
96 # cleanup 88 # cleanup
97 checkVkResult device.vk.vkDeviceWaitIdle() 89 checkVkResult device.vk.vkDeviceWaitIdle()
98 descriptorPool.destroy() 90 descriptorPool.destroy()
99 vertexshader.destroy() 91 vertexshader.destroy()
100 fragmentshader.destroy() 92 fragmentshader.destroy()
101 pipeline.destroy() 93 pipeline.destroy()
102 inflight.destroy()
103 imageAvailable.destroy()
104 renderFinished.destroy()
105 commandPool.destroy()
106 renderpass.destroy() 94 renderpass.destroy()
107 swapchain.destroy() 95 swapchain.destroy()
108 device.destroy() 96 device.destroy()
109 97
110 debugger.destroy() 98 debugger.destroy()