Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 770aa49

Browse files
committed
A multisample resolve attachment for depth is completely redundant.
1 parent 6a0ec4d commit 770aa49

2 files changed

Lines changed: 17 additions & 71 deletions

File tree

10_Multisampling.md

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ In MSAA, each pixel is sampled in an offscreen buffer which is then rendered to
6969
std::vector<VkImage> colorImages;
7070
std::vector<VkDeviceMemory> colorImagesMemory;
7171
std::vector<VkImageView> colorImagesView;
72-
73-
std::vector<VkImage> depthMsaaImages;
74-
std::vector<VkDeviceMemory> depthMsaaImagesMemory;
75-
std::vector<VkImageView> depthMsaaImagesView;
7672
...
7773
```
7874

@@ -141,23 +137,13 @@ void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayo
141137
}
142138
```
143139
144-
Now that we have a multisampled color buffer in place it's time to take care of depth. Modify `createDepthResources` and add creation steps for a multisampled depth buffer:
140+
Now that we have a multisampled color buffer in place it's time to take care of depth. Modify `createDepthResources` and update the number of samples used by the depth buffer:
145141
146142
```c++
147143
void createDepthResources() {
148-
...
149-
depthMsaaImages.resize(swapChainImages.size());
150-
depthMsaaImagesMemory.resize(swapChainImages.size());
151-
depthMsaaImagesView.resize(swapChainImages.size());
152-
...
153-
154-
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
155144
...
156-
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthMsaaImages[i], depthMsaaImagesMemory[i]);
157-
depthMsaaImagesView[i] = createImageView(depthMsaaImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
158-
159-
transitionImageLayout(depthMsaaImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
160-
}
145+
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImagesMemory[i]);
146+
...
161147
}
162148
```
163149

@@ -169,16 +155,13 @@ void cleanupSwapChain() {
169155
vkDestroyImageView(device, colorImageViews[i], nullptr);
170156
vkDestroyImage(device, colorImages[i], nullptr);
171157
vkFreeMemory(device, colorImagesMemory[i], nullptr);
172-
vkDestroyImageView(device, depthMsaaImagesView[i], nullptr);
173-
vkDestroyImage(device, depthMsaaImages[i], nullptr);
174-
vkFreeMemory(device, depthMsaaImagesMemory[i], nullptr);
175158
...
176159
}
177160
...
178161
}
179162
```
180163

181-
We made it past the initial MSAA configuration, now we need to start using these new resources in our graphics pipeline, framebuffer, render pass and and see the results!
164+
We made it past the initial MSAA setup, now we need to start using these new resources in our graphics pipeline, framebuffer, render pass and see the results!
182165

183166
## Adding new attachments
184167

@@ -194,7 +177,7 @@ void createRenderPass() {
194177
...
195178
```
196179
197-
You'll notice that we have changed the finalLayout from `VK_IMAGE_LAYOUT_PRESENT_SRC_KHR` to `VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL`. That's because multisampled images cannot be presented directly. We first need to resolve them to a regular image. The same requirement applies to the depth buffer. Therefore we will have two new attachments that are so-called resolve attachments:
180+
You'll notice that we have changed the finalLayout from `VK_IMAGE_LAYOUT_PRESENT_SRC_KHR` to `VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL`. That's because multisampled images cannot be presented directly. We first need to resolve them to a regular image. This requirement does not apply to the depth buffer, since it won't be presented at any point. Therefore we will have to add only one new attachment for color which is a so-called resolve attachment:
198181
199182
```c++
200183
...
@@ -207,20 +190,10 @@ You'll notice that we have changed the finalLayout from `VK_IMAGE_LAYOUT_PRESENT
207190
colorAttachmentResolve.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
208191
colorAttachmentResolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
209192
colorAttachmentResolve.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
210-
211-
VkAttachmentDescription depthAttachmentResolve = {};
212-
depthAttachmentResolve.format = findDepthFormat();
213-
depthAttachmentResolve.samples = VK_SAMPLE_COUNT_1_BIT;
214-
depthAttachmentResolve.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
215-
depthAttachmentResolve.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
216-
depthAttachmentResolve.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
217-
depthAttachmentResolve.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
218-
depthAttachmentResolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
219-
depthAttachmentResolve.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
220193
...
221194
```
222195

223-
The render pass now has to be instructed to resolve multisampled images into these regular attachments. Create a new attachment reference that will point to the color buffer which will serve as the resolve target:
196+
The render pass now has to be instructed to resolve multisampled color image into regular attachment. Create a new attachment reference that will point to the color buffer which will serve as the resolve target:
224197

225198
```c++
226199
...
@@ -238,24 +211,23 @@ Set the `pResolveAttachments` subpass struct member to point to the newly create
238211
...
239212
```
240213

241-
Now update render pass info struct with new attachments:
214+
Now update render pass info struct with new color attachment:
242215

243216
```c++
244217
...
245-
std::array<VkAttachmentDescription, 4> attachments = {colorAttachment, depthAttachment, colorAttachmentResolve, depthAttachmentResolve };
218+
std::array<VkAttachmentDescription, 3> attachments = {colorAttachment, depthAttachment, colorAttachmentResolve };
246219
...
247220
```
248221
249-
With render pass in place, modify `createFrameBuffers` and add new image views to the list:
222+
With render pass in place, modify `createFrameBuffers` and add new image view to the list:
250223
251224
```c++
252225
void createFrameBuffers() {
253226
...
254-
std::array<VkImageView, 4> attachments = {
227+
std::array<VkImageView, 3> attachments = {
255228
colorImageViews[i],
256-
depthMsaaImagesView[i],
257-
swapChainImageViews[i],
258-
depthImagesView[i]
229+
depthImagesView[i],
230+
swapChainImageViews[i]
259231
};
260232
...
261233
}

code/29_multisampling.cpp

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,6 @@ class HelloTriangleApplication {
174174
std::vector<VkDeviceMemory> colorImagesMemory;
175175
std::vector<VkImageView> colorImageViews;
176176

177-
std::vector<VkImage> depthMsaaImages;
178-
std::vector<VkDeviceMemory> depthMsaaImagesMemory;
179-
std::vector<VkImageView> depthMsaaImagesView;
180-
181177
std::vector<VkImage> depthImages;
182178
std::vector<VkDeviceMemory> depthImagesMemory;
183179
std::vector<VkImageView> depthImagesView;
@@ -267,9 +263,6 @@ class HelloTriangleApplication {
267263
vkDestroyImageView(device, colorImageViews[i], nullptr);
268264
vkDestroyImage(device, colorImages[i], nullptr);
269265
vkFreeMemory(device, colorImagesMemory[i], nullptr);
270-
vkDestroyImageView(device, depthMsaaImagesView[i], nullptr);
271-
vkDestroyImage(device, depthMsaaImages[i], nullptr);
272-
vkFreeMemory(device, depthMsaaImagesMemory[i], nullptr);
273266
vkDestroyImageView(device, depthImagesView[i], nullptr);
274267
vkDestroyImage(device, depthImages[i], nullptr);
275268
vkFreeMemory(device, depthImagesMemory[i], nullptr);
@@ -570,16 +563,6 @@ class HelloTriangleApplication {
570563
colorAttachmentResolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
571564
colorAttachmentResolve.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
572565

573-
VkAttachmentDescription depthAttachmentResolve = {};
574-
depthAttachmentResolve.format = findDepthFormat();
575-
depthAttachmentResolve.samples = VK_SAMPLE_COUNT_1_BIT;
576-
depthAttachmentResolve.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
577-
depthAttachmentResolve.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
578-
depthAttachmentResolve.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
579-
depthAttachmentResolve.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
580-
depthAttachmentResolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
581-
depthAttachmentResolve.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
582-
583566
VkAttachmentReference colorAttachmentRef = {};
584567
colorAttachmentRef.attachment = 0;
585568
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
@@ -607,7 +590,7 @@ class HelloTriangleApplication {
607590
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
608591
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
609592

610-
std::array<VkAttachmentDescription, 4> attachments = {colorAttachment, depthAttachment, colorAttachmentResolve, depthAttachmentResolve };
593+
std::array<VkAttachmentDescription, 3> attachments = {colorAttachment, depthAttachment, colorAttachmentResolve };
611594
VkRenderPassCreateInfo renderPassInfo = {};
612595
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
613596
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
@@ -779,11 +762,10 @@ class HelloTriangleApplication {
779762
swapChainFramebuffers.resize(swapChainImageViews.size());
780763

781764
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
782-
std::array<VkImageView, 4> attachments = {
765+
std::array<VkImageView, 3> attachments = {
783766
colorImageViews[i],
784-
depthMsaaImagesView[i],
785-
swapChainImageViews[i],
786-
depthImagesView[i]
767+
depthImagesView[i],
768+
swapChainImageViews[i]
787769
};
788770

789771
VkFramebufferCreateInfo framebufferInfo = {};
@@ -834,20 +816,12 @@ class HelloTriangleApplication {
834816
depthImages.resize(swapChainImages.size());
835817
depthImagesMemory.resize(swapChainImages.size());
836818
depthImagesView.resize(swapChainImages.size());
837-
depthMsaaImages.resize(swapChainImages.size());
838-
depthMsaaImagesMemory.resize(swapChainImages.size());
839-
depthMsaaImagesView.resize(swapChainImages.size());
840819

841820
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
842-
createImage(swapChainExtent.width, swapChainExtent.height, 1, VK_SAMPLE_COUNT_1_BIT, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImagesMemory[i]);
821+
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImagesMemory[i]);
843822
depthImagesView[i] = createImageView(depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
844823

845824
transitionImageLayout(depthImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
846-
847-
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthMsaaImages[i], depthMsaaImagesMemory[i]);
848-
depthMsaaImagesView[i] = createImageView(depthMsaaImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
849-
850-
transitionImageLayout(depthMsaaImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
851825
}
852826
}
853827

0 commit comments

Comments
 (0)