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