Mercurial > games > semicongine
annotate semicongine/rendering/swapchain.nim @ 1204:e2901100a596 compiletime-tests
add: tests, some fixes, some helpers
author | sam <sam@basx.dev> |
---|---|
date | Mon, 15 Jul 2024 23:51:17 +0700 |
parents | d4a206b5e5b0 |
children |
rev | line source |
---|---|
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
1 const N_FRAMEBUFFERS = 3'u32 |
1194 | 2 |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
3 proc InitSwapchain*( |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
4 renderPass: VkRenderPass, |
1196 | 5 vSync: bool = false, |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
6 samples = VK_SAMPLE_COUNT_1_BIT, |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
7 oldSwapchain: ref Swapchain = nil, |
1196 | 8 ): Option[Swapchain] = |
1204 | 9 assert vulkan.instance.Valid, "Vulkan not initialized" |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
10 |
1194 | 11 var capabilities: VkSurfaceCapabilitiesKHR |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
12 checkVkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkan.physicalDevice, vulkan.surface, addr(capabilities)) |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
13 let |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
14 format = DefaultSurfaceFormat() |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
15 width = capabilities.currentExtent.width |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
16 height = capabilities.currentExtent.height |
1194 | 17 |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
18 if width == 0 or height == 0: |
1196 | 19 return none(Swapchain) |
1194 | 20 |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
21 # following "count" is established according to vulkan specs |
1194 | 22 var minFramebufferCount = N_FRAMEBUFFERS |
23 minFramebufferCount = max(minFramebufferCount, capabilities.minImageCount) | |
24 if capabilities.maxImageCount != 0: | |
25 minFramebufferCount = min(minFramebufferCount, capabilities.maxImageCount) | |
26 | |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
27 # create swapchain |
1194 | 28 let hasTripleBuffering = VK_PRESENT_MODE_MAILBOX_KHR in svkGetPhysicalDeviceSurfacePresentModesKHR() |
1198 | 29 var swapchainCreateInfo = VkSwapchainCreateInfoKHR( |
1194 | 30 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
31 surface: vulkan.surface, |
1194 | 32 minImageCount: minFramebufferCount, |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
33 imageFormat: format, |
1194 | 34 imageColorSpace: VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, # only one supported without special extensions |
35 imageExtent: capabilities.currentExtent, | |
36 imageArrayLayers: 1, | |
37 imageUsage: toBits [VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT], | |
38 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
39 preTransform: capabilities.currentTransform, | |
40 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, # only used for blending with other windows, can be opaque | |
41 presentMode: if (vSync or not hasTripleBuffering): VK_PRESENT_MODE_FIFO_KHR else: VK_PRESENT_MODE_MAILBOX_KHR, | |
42 clipped: true, | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
43 oldSwapchain: if oldSwapchain != nil: oldSwapchain.vk else: VkSwapchainKHR(0), |
1194 | 44 ) |
1196 | 45 var swapchain: Swapchain |
1198 | 46 if vkCreateSwapchainKHR(vulkan.device, addr(swapchainCreateInfo), nil, addr(swapchain.vk)) != VK_SUCCESS: |
1196 | 47 return none(Swapchain) |
48 | |
1199 | 49 swapchain.width = width |
50 swapchain.height = height | |
1196 | 51 swapchain.renderPass = renderPass |
52 swapchain.vSync = vSync | |
53 swapchain.samples = samples | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
54 swapchain.oldSwapchain = oldSwapchain |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
55 if swapchain.oldSwapchain != nil: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
56 swapchain.oldSwapchainCounter = INFLIGHTFRAMES.int * 2 |
1194 | 57 |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
58 # create msaa image+view if desired |
1194 | 59 if samples != VK_SAMPLE_COUNT_1_BIT: |
1196 | 60 swapchain.msaaImage = svkCreate2DImage( |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
61 width = width, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
62 height = height, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
63 format = format, |
1194 | 64 usage = [VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT], |
1204 | 65 samples = samples, |
1194 | 66 ) |
1196 | 67 let requirements = svkGetImageMemoryRequirements(swapchain.msaaImage) |
68 swapchain.msaaMemory = svkAllocateMemory( | |
69 requirements.size, | |
70 BestMemory(mappable = false, filter = requirements.memoryTypes) | |
71 ) | |
1194 | 72 checkVkResult vkBindImageMemory( |
73 vulkan.device, | |
1196 | 74 swapchain.msaaImage, |
75 swapchain.msaaMemory, | |
1194 | 76 0, |
77 ) | |
1196 | 78 swapchain.msaaImageView = svkCreate2DImageView(swapchain.msaaImage, format) |
1194 | 79 |
1196 | 80 # create framebuffers |
81 var actualNFramebuffers: uint32 | |
82 checkVkResult vkGetSwapchainImagesKHR(vulkan.device, swapchain.vk, addr(actualNFramebuffers), nil) | |
83 var framebuffers = newSeq[VkImage](actualNFramebuffers) | |
84 checkVkResult vkGetSwapchainImagesKHR(vulkan.device, swapchain.vk, addr(actualNFramebuffers), framebuffers.ToCPointer) | |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
85 |
1196 | 86 for framebuffer in framebuffers: |
87 swapchain.framebufferViews.add svkCreate2DImageView(framebuffer, format) | |
88 if samples == VK_SAMPLE_COUNT_1_BIT: | |
1197 | 89 swapchain.framebuffers.add svkCreateFramebuffer(renderPass, width, height, [swapchain.framebufferViews[^1]]) |
1196 | 90 else: |
1197 | 91 swapchain.framebuffers.add svkCreateFramebuffer(renderPass, width, height, [swapchain.msaaImageView, swapchain.framebufferViews[^1]]) |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
92 |
1196 | 93 # create sync primitives |
94 for i in 0 ..< INFLIGHTFRAMES: | |
95 swapchain.queueFinishedFence[i] = svkCreateFence(signaled = true) | |
96 swapchain.imageAvailableSemaphore[i] = svkCreateSemaphore() | |
97 swapchain.renderFinishedSemaphore[i] = svkCreateSemaphore() | |
1198 | 98 |
99 # command buffers | |
100 var commandPoolCreateInfo = VkCommandPoolCreateInfo( | |
101 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
102 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
103 queueFamilyIndex: vulkan.graphicsQueueFamily, | |
104 ) | |
105 checkVkResult vkCreateCommandPool(vulkan.device, addr(commandPoolCreateInfo), nil, addr(swapchain.commandBufferPool)) | |
106 var allocInfo = VkCommandBufferAllocateInfo( | |
107 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
108 commandPool: swapchain.commandBufferPool, | |
109 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
110 commandBufferCount: INFLIGHTFRAMES, | |
111 ) | |
112 checkVkResult vkAllocateCommandBuffers(vulkan.device, addr(allocInfo), swapchain.commandBuffers.ToCPointer) | |
113 | |
1196 | 114 return some(swapchain) |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
115 |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
116 proc DestroySwapchain*(swapchain: Swapchain) = |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
117 |
1204 | 118 if swapchain.samples != VK_SAMPLE_COUNT_1_BIT: |
119 vkDestroyImageView(vulkan.device, swapchain.msaaImageView, nil) | |
120 vkDestroyImage(vulkan.device, swapchain.msaaImage, nil) | |
121 vkFreeMemory(vulkan.device, swapchain.msaaMemory, nil) | |
122 | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
123 for fence in swapchain.queueFinishedFence: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
124 vkDestroyFence(vulkan.device, fence, nil) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
125 |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
126 for semaphore in swapchain.imageAvailableSemaphore: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
127 vkDestroySemaphore(vulkan.device, semaphore, nil) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
128 |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
129 for semaphore in swapchain.renderFinishedSemaphore: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
130 vkDestroySemaphore(vulkan.device, semaphore, nil) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
131 |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
132 for imageView in swapchain.framebufferViews: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
133 vkDestroyImageView(vulkan.device, imageView, nil) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
134 |
1201 | 135 for framebuffer in swapchain.framebuffers: |
136 vkDestroyFramebuffer(vulkan.device, framebuffer, nil) | |
137 | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
138 vkDestroyCommandPool(vulkan.device, swapchain.commandBufferPool, nil) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
139 |
1201 | 140 vkDestroySwapchainKHR(vulkan.device, swapchain.vk, nil) |
141 | |
1198 | 142 proc TryAcquireNextImage(swapchain: var Swapchain): Option[VkFramebuffer] = |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
143 if not swapchain.queueFinishedFence[swapchain.currentFiF].Await(100_000_000): |
1199 | 144 return none(VkFramebuffer) |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
145 |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
146 let nextImageResult = vkAcquireNextImageKHR( |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
147 vulkan.device, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
148 swapchain.vk, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
149 high(uint64), |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
150 swapchain.imageAvailableSemaphore[swapchain.currentFiF], |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
151 VkFence(0), |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
152 addr(swapchain.currentFramebufferIndex), |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
153 ) |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
154 |
1199 | 155 swapchain.queueFinishedFence[swapchain.currentFiF].svkResetFences() |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
156 |
1196 | 157 if nextImageResult != VK_SUCCESS: |
158 return none(VkFramebuffer) | |
159 return some(swapchain.framebuffers[swapchain.currentFramebufferIndex]) | |
1194 | 160 |
1198 | 161 proc Swap(swapchain: var Swapchain, commandBuffer: VkCommandBuffer): bool = |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
162 var |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
163 waitStage = VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT) |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
164 submitInfo = VkSubmitInfo( |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
165 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
166 waitSemaphoreCount: 1, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
167 pWaitSemaphores: addr(swapchain.imageAvailableSemaphore[swapchain.currentFiF]), |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
168 pWaitDstStageMask: addr(waitStage), |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
169 commandBufferCount: 1, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
170 pCommandBuffers: addr(commandBuffer), |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
171 signalSemaphoreCount: 1, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
172 pSignalSemaphores: addr(swapchain.renderFinishedSemaphore[swapchain.currentFiF]), |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
173 ) |
1197 | 174 checkVkResult vkQueueSubmit( |
1198 | 175 queue = vulkan.graphicsQueue, |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
176 submitCount = 1, |
1197 | 177 pSubmits = addr(submitInfo), |
178 fence = swapchain.queueFinishedFence[swapchain.currentFiF] | |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
179 ) |
1194 | 180 |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
181 var presentInfo = VkPresentInfoKHR( |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
182 sType: VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
183 waitSemaphoreCount: 1, |
1197 | 184 pWaitSemaphores: addr(swapchain.renderFinishedSemaphore[swapchain.currentFiF]), |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
185 swapchainCount: 1, |
1197 | 186 pSwapchains: addr(swapchain.vk), |
187 pImageIndices: addr(swapchain.currentFramebufferIndex), | |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
188 pResults: nil, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
189 ) |
1197 | 190 let presentResult = vkQueuePresentKHR(vulkan.graphicsQueue, addr(presentInfo)) |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
191 |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
192 if swapchain.oldSwapchain != nil: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
193 dec swapchain.oldSwapchainCounter |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
194 if swapchain.oldSwapchainCounter <= 0: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
195 DestroySwapchain(swapchain.oldSwapchain[]) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
196 swapchain.oldSwapchain = nil |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
197 |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
198 if presentResult != VK_SUCCESS: |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
199 return false |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
200 |
1198 | 201 swapchain.currentFiF = (uint32(swapchain.currentFiF) + 1) mod INFLIGHTFRAMES |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
202 return true |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
203 |
1198 | 204 proc Recreate(swapchain: Swapchain): Option[Swapchain] = |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
205 var oldSwapchain = new Swapchain |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
206 oldSwapchain[] = swapchain |
1197 | 207 InitSwapchain( |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
208 renderPass = swapchain.renderPass, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
209 vSync = swapchain.vSync, |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
210 samples = swapchain.samples, |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
211 oldSwapchain = oldSwapchain, |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
212 ) |
1198 | 213 |
1199 | 214 template WithNextFrame*(swapchain: var Swapchain, framebufferName, commandBufferName, body: untyped): untyped = |
1198 | 215 var maybeFramebuffer = TryAcquireNextImage(swapchain) |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
216 if maybeFramebuffer.isSome: |
1198 | 217 block: |
218 let `framebufferName` {.inject.} = maybeFramebuffer.get | |
219 let `commandBufferName` {.inject.} = swapchain.commandBuffers[swapchain.currentFiF] | |
220 let beginInfo = VkCommandBufferBeginInfo( | |
221 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
222 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | |
223 ) | |
224 checkVkResult vkResetCommandBuffer(`commandBufferName`, VkCommandBufferResetFlags(0)) | |
225 checkVkResult vkBeginCommandBuffer(`commandBufferName`, addr(beginInfo)) | |
226 | |
227 body | |
228 | |
229 checkVkResult vkEndCommandBuffer(`commandBufferName`) | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
230 discard Swap(swapchain = swapchain, commandBuffer = `commandBufferName`) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
231 else: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
232 let maybeNewSwapchain = Recreate(swapchain) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
233 if maybeNewSwapchain.isSome: |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
234 swapchain = maybeNewSwapchain.get |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
235 |