Mercurial > games > semicongine
annotate src/semicongine/vulkan/swapchain.nim @ 590:1edf3e16144e
add: input handling, small refactoring for renderer
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 18 Apr 2023 03:04:37 +0700 |
parents | b434feaf2b67 |
children | 11666d28e04d |
rev | line source |
---|---|
567 | 1 import std/options |
574
bbeec60e25ca
did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents:
573
diff
changeset
|
2 import std/logging |
567 | 3 |
557 | 4 import ./api |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
5 import ./utils |
557 | 6 import ./device |
7 import ./physicaldevice | |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
8 import ./image |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
9 import ./renderpass |
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
10 import ./framebuffer |
567 | 11 import ./commandbuffer |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
12 import ./syncing |
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
13 |
559 | 14 import ../math |
557 | 15 |
16 type | |
588 | 17 Swapchain* = object |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
18 device*: Device |
557 | 19 vk*: VkSwapchainKHR |
559 | 20 format*: VkFormat |
21 dimension*: TVec2[uint32] | |
567 | 22 renderPass*: RenderPass |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
23 nImages*: uint32 |
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
24 imageviews*: seq[ImageView] |
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
25 framebuffers*: seq[Framebuffer] |
569 | 26 currentInFlight*: int |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
27 currentFramebufferIndex: uint32 |
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
28 framesRendered*: uint64 |
567 | 29 queueFinishedFence: seq[Fence] |
30 imageAvailableSemaphore*: seq[Semaphore] | |
31 renderFinishedSemaphore*: seq[Semaphore] | |
32 commandBufferPool: CommandBufferPool | |
588 | 33 inFlightFrames: int |
557 | 34 |
35 | |
559 | 36 proc createSwapchain*( |
37 device: Device, | |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
38 renderPass: RenderPass, |
559 | 39 surfaceFormat: VkSurfaceFormatKHR, |
567 | 40 queueFamily: QueueFamily, |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
41 desiredNumberOfImages=3'u32, |
588 | 42 presentationMode: VkPresentModeKHR=VK_PRESENT_MODE_MAILBOX_KHR, |
43 inFlightFrames=2 | |
559 | 44 ): (Swapchain, VkResult) = |
557 | 45 assert device.vk.valid |
46 assert device.physicalDevice.vk.valid | |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
47 assert renderPass.vk.valid |
588 | 48 assert inFlightFrames > 0 |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
49 |
557 | 50 var capabilities = device.physicalDevice.getSurfaceCapabilities() |
51 | |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
52 var imageCount = desiredNumberOfImages |
557 | 53 # following is according to vulkan specs |
54 if presentationMode in [VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR, VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR]: | |
55 imageCount = 1 | |
56 else: | |
57 imageCount = max(imageCount, capabilities.minImageCount) | |
58 if capabilities.maxImageCount != 0: | |
59 imageCount = min(imageCount, capabilities.maxImageCount) | |
60 | |
61 var createInfo = VkSwapchainCreateInfoKHR( | |
62 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, | |
63 surface: device.physicalDevice.surface, | |
64 minImageCount: imageCount, | |
65 imageFormat: surfaceFormat.format, | |
66 imageColorSpace: surfaceFormat.colorSpace, | |
67 imageExtent: capabilities.currentExtent, | |
68 imageArrayLayers: 1, | |
69 imageUsage: toBits [VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT], | |
70 # VK_SHARING_MODE_CONCURRENT no supported currently | |
71 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
72 preTransform: capabilities.currentTransform, | |
73 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, # only used for blending with other windows, can be opaque | |
74 presentMode: presentationMode, | |
75 clipped: true, | |
76 ) | |
77 var | |
559 | 78 swapchain = Swapchain( |
79 device: device, | |
80 format: surfaceFormat.format, | |
567 | 81 dimension: TVec2[uint32]([capabilities.currentExtent.width, capabilities.currentExtent.height]), |
82 renderPass: renderPass, | |
588 | 83 inFlightFrames: inFlightFrames |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
84 ) |
557 | 85 createResult = device.vk.vkCreateSwapchainKHR(addr(createInfo), nil, addr(swapchain.vk)) |
86 | |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
87 if createResult == VK_SUCCESS: |
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
88 var nImages: uint32 |
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
89 checkVkResult device.vk.vkGetSwapchainImagesKHR(swapChain.vk, addr(nImages), nil) |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
90 swapchain.nImages = nImages |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
91 var images = newSeq[VkImage](nImages) |
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
92 checkVkResult device.vk.vkGetSwapchainImagesKHR(swapChain.vk, addr(nImages), images.toCPointer) |
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
93 for vkimage in images: |
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
94 let image = Image(vk: vkimage, format: surfaceFormat.format, device: device) |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
95 let imageview = image.createImageView() |
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
96 swapChain.imageviews.add imageview |
588 | 97 swapChain.framebuffers.add swapchain.device.createFramebuffer(renderPass.vk, [imageview], swapchain.dimension) |
98 for i in 0 ..< swapchain.inFlightFrames: | |
567 | 99 swapchain.queueFinishedFence.add device.createFence() |
100 swapchain.imageAvailableSemaphore.add device.createSemaphore() | |
101 swapchain.renderFinishedSemaphore.add device.createSemaphore() | |
588 | 102 swapchain.commandBufferPool = device.createCommandBufferPool(queueFamily, swapchain.inFlightFrames) |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
103 |
557 | 104 return (swapchain, createResult) |
105 | |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
106 proc currentFramebuffer*(swapchain: Swapchain): Framebuffer = |
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
107 swapchain.framebuffers[swapchain.currentFramebufferIndex] |
572
9f1de117aaca
did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
571
diff
changeset
|
108 |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
109 proc nextFrame*(swapchain: var Swapchain): VkCommandBuffer = |
567 | 110 assert swapchain.device.vk.valid |
111 assert swapchain.vk.valid | |
112 assert swapchain.device.firstGraphicsQueue().isSome | |
113 assert swapchain.device.firstPresentationQueue().isSome | |
114 | |
588 | 115 swapchain.currentInFlight = (swapchain.currentInFlight + 1) mod swapchain.inFlightFrames |
567 | 116 swapchain.queueFinishedFence[swapchain.currentInFlight].wait() |
117 | |
118 let nextImageResult = swapchain.device.vk.vkAcquireNextImageKHR( | |
119 swapchain.vk, | |
120 high(uint64), | |
121 swapchain.imageAvailableSemaphore[swapchain.currentInFlight].vk, | |
122 VkFence(0), | |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
123 addr(swapchain.currentFramebufferIndex) |
567 | 124 ) |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
589
diff
changeset
|
125 # TODO: resize swapchain on VK_SUBOPTIMAL_KHR |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
589
diff
changeset
|
126 echo "HIIIIIIIIIIII next image result", nextImageResult |
567 | 127 if not (nextImageResult in [VK_SUCCESS, VK_TIMEOUT, VK_NOT_READY, VK_SUBOPTIMAL_KHR]): |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
128 return |
567 | 129 |
130 swapchain.queueFinishedFence[swapchain.currentInFlight].reset() | |
131 | |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
132 return swapchain.commandBufferPool.buffers[swapchain.currentInFlight] |
567 | 133 |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
134 proc swap*(swapchain: var Swapchain): bool = |
567 | 135 var |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
136 commandBuffer = swapchain.commandBufferPool.buffers[swapchain.currentInFlight] |
567 | 137 waitSemaphores = [swapchain.imageAvailableSemaphore[swapchain.currentInFlight].vk] |
138 waitStages = [VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)] | |
139 submitInfo = VkSubmitInfo( | |
140 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO, | |
141 waitSemaphoreCount: 1, | |
142 pWaitSemaphores: addr(waitSemaphores[0]), | |
143 pWaitDstStageMask: addr(waitStages[0]), | |
144 commandBufferCount: 1, | |
569 | 145 pCommandBuffers: addr(commandBuffer), |
567 | 146 signalSemaphoreCount: 1, |
147 pSignalSemaphores: addr(swapchain.renderFinishedSemaphore[swapchain.currentInFlight].vk), | |
148 ) | |
149 checkVkResult vkQueueSubmit( | |
150 swapchain.device.firstGraphicsQueue().get.vk, | |
151 1, | |
152 addr(submitInfo), | |
153 swapchain.queueFinishedFence[swapchain.currentInFlight].vk | |
154 ) | |
155 | |
156 var presentInfo = VkPresentInfoKHR( | |
157 sType: VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, | |
158 waitSemaphoreCount: 1, | |
159 pWaitSemaphores: addr(swapchain.renderFinishedSemaphore[swapchain.currentInFlight].vk), | |
160 swapchainCount: 1, | |
161 pSwapchains: addr(swapchain.vk), | |
589
b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
162 pImageIndices: addr(swapchain.currentFramebufferIndex), |
567 | 163 pResults: nil, |
164 ) | |
165 let presentResult = vkQueuePresentKHR(swapchain.device.firstPresentationQueue().get().vk, addr(presentInfo)) | |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
589
diff
changeset
|
166 # TODO: resize swapchain on VK_SUBOPTIMAL_KHR |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
589
diff
changeset
|
167 echo "HIIIIIIIIIIII Present Result:", presentResult |
567 | 168 if not (presentResult in [VK_SUCCESS, VK_SUBOPTIMAL_KHR]): |
169 return false | |
170 | |
569 | 171 inc swapchain.framesRendered |
567 | 172 return true |
173 | |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
174 |
557 | 175 proc destroy*(swapchain: var Swapchain) = |
176 assert swapchain.vk.valid | |
567 | 177 assert swapchain.commandBufferPool.vk.valid |
178 | |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
179 for imageview in swapchain.imageviews.mitems: |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
180 assert imageview.vk.valid |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
181 imageview.destroy() |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
182 for framebuffer in swapchain.framebuffers.mitems: |
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
183 assert framebuffer.vk.valid |
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
184 framebuffer.destroy() |
567 | 185 swapchain.commandBufferPool.destroy() |
588 | 186 for i in 0 ..< swapchain.inFlightFrames: |
567 | 187 assert swapchain.queueFinishedFence[i].vk.valid |
188 assert swapchain.imageAvailableSemaphore[i].vk.valid | |
189 assert swapchain.renderFinishedSemaphore[i].vk.valid | |
190 swapchain.queueFinishedFence[i].destroy() | |
191 swapchain.imageAvailableSemaphore[i].destroy() | |
192 swapchain.renderFinishedSemaphore[i].destroy() | |
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
581
diff
changeset
|
193 |
557 | 194 swapchain.device.vk.vkDestroySwapchainKHR(swapchain.vk, nil) |
558
affa6571a2c9
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
557
diff
changeset
|
195 swapchain.vk.reset() |