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

Skip to content

Commit f769482

Browse files
committed
Use multiple depth buffers for MSAA.
1 parent f6a9d88 commit f769482

2 files changed

Lines changed: 87 additions & 52 deletions

File tree

10_Multisampling.md

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void createImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCo
7272
For now, update all calls to this function using `VK_SAMPLE_COUNT_1_BIT` - we will be replacing this with proper values as we progress with implementation:
7373
7474
```c++
75-
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, depthImage, depthImageMemory);
75+
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]);
7676
...
7777
createImage(texWidth, texHeight, mipLevels, VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, textureImage, textureImageMemory);
7878
```
@@ -83,13 +83,13 @@ In MSAA, each pixel is sampled in an offscreen buffer which is then rendered to
8383

8484
```c++
8585
...
86-
VkImage colorImage;
87-
VkDeviceMemory colorImageMemory;
88-
VkImageView colorImageView;
86+
std::vector<VkImage> colorImages;
87+
std::vector<VkDeviceMemory> colorImagesMemory;
88+
std::vector<VkImageView> colorImagesView;
8989

90-
VkImage depthMsaaImage;
91-
VkDeviceMemory depthMsaaImageMemory;
92-
VkImageView depthMsaaImageView;
90+
std::vector<VkImage> depthMsaaImages;
91+
std::vector<VkDeviceMemory> depthMsaaImagesMemory;
92+
std::vector<VkImageView> depthMsaaImagesView;
9393
...
9494
```
9595

@@ -99,10 +99,16 @@ We will now create a multisampled color buffer. Add a `createColorResources` fun
9999
void createColorResources() {
100100
VkFormat colorFormat = swapChainImageFormat;
101101

102-
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, colorFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, colorImage, colorImageMemory);
103-
colorImageView = createImageView(colorImage, colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
102+
colorImages.resize(swapChainImages.size());
103+
colorImagesMemory.resize(swapChainImages.size());
104+
colorImageViews.resize(swapChainImages.size());
104105

105-
transitionImageLayout(colorImage, colorFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1);
106+
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
107+
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, colorFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, colorImages[i], colorImagesMemory[i]);
108+
colorImageViews[i] = createImageView(colorImages[i], colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
109+
110+
transitionImageLayout(colorImages[i], colorFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1);
111+
}
106112
}
107113
```
108114

@@ -140,24 +146,36 @@ Now that we have multisampled color buffer in place it's time to take care of de
140146
```c++
141147
void createDepthResources() {
142148
...
143-
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, depthMsaaImage, depthMsaaImageMemory);
144-
depthMsaaImageView = createImageView(depthMsaaImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
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++) {
155+
...
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);
145158
146-
transitionImageLayout(depthMsaaImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
159+
transitionImageLayout(depthMsaaImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
160+
}
147161
}
148162
```
149163

150164
We have now created a couple of new Vulkan resources, so let's not forget to release them when necessary:
151165

152166
```c++
153167
void cleanupSwapChain() {
154-
vkDestroyImageView(device, colorImageView, nullptr);
155-
vkDestroyImage(device, colorImage, nullptr);
156-
vkFreeMemory(device, colorImageMemory, nullptr);
157-
vkDestroyImageView(device, depthMsaaImageView, nullptr);
158-
vkDestroyImage(device, depthMsaaImage, nullptr);
159-
vkFreeMemory(device, depthMsaaImageMemory, nullptr);
168+
for (size_t i = 0; i < swapChainImages.size(); i++) {
169+
vkDestroyImageView(device, colorImageViews[i], nullptr);
170+
vkDestroyImage(device, colorImages[i], nullptr);
171+
vkFreeMemory(device, colorImagesMemory[i], nullptr);
172+
vkDestroyImageView(device, depthMsaaImagesView[i], nullptr);
173+
vkDestroyImage(device, depthMsaaImages[i], nullptr);
174+
vkFreeMemory(device, depthMsaaImagesMemory[i], nullptr);
175+
...
176+
}
160177
...
178+
}
161179
```
162180

163181
With only a few simple steps we created additional buffers and image views necessary for multsampling - it's now time to put it all together and see the results!
@@ -234,10 +252,10 @@ With render pass in place, modify `createFrameBuffers` and add new image views t
234252
void createFrameBuffers() {
235253
...
236254
std::array<VkImageView, 4> attachments = {
237-
colorImageView,
238-
depthMsaaImageView,
255+
colorImageViews[i],
256+
depthMsaaImagesView[i],
239257
swapChainImageViews[i],
240-
depthImageView
258+
depthImagesView[i]
241259
};
242260
...
243261
}

code/29_multisampling.cpp

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,17 @@ class HelloTriangleApplication {
170170

171171
VkCommandPool commandPool;
172172

173-
VkImage colorImage;
174-
VkDeviceMemory colorImageMemory;
175-
VkImageView colorImageView;
173+
std::vector<VkImage> colorImages;
174+
std::vector<VkDeviceMemory> colorImagesMemory;
175+
std::vector<VkImageView> colorImageViews;
176176

177-
VkImage depthMsaaImage;
178-
VkDeviceMemory depthMsaaImageMemory;
179-
VkImageView depthMsaaImageView;
177+
std::vector<VkImage> depthMsaaImages;
178+
std::vector<VkDeviceMemory> depthMsaaImagesMemory;
179+
std::vector<VkImageView> depthMsaaImagesView;
180180

181-
VkImage depthImage;
182-
VkDeviceMemory depthImageMemory;
183-
VkImageView depthImageView;
181+
std::vector<VkImage> depthImages;
182+
std::vector<VkDeviceMemory> depthImagesMemory;
183+
std::vector<VkImageView> depthImagesView;
184184

185185
uint32_t mipLevels;
186186
VkImage textureImage;
@@ -263,15 +263,17 @@ class HelloTriangleApplication {
263263
}
264264

265265
void cleanupSwapChain() {
266-
vkDestroyImageView(device, colorImageView, nullptr);
267-
vkDestroyImage(device, colorImage, nullptr);
268-
vkFreeMemory(device, colorImageMemory, nullptr);
269-
vkDestroyImageView(device, depthMsaaImageView, nullptr);
270-
vkDestroyImage(device, depthMsaaImage, nullptr);
271-
vkFreeMemory(device, depthMsaaImageMemory, nullptr);
272-
vkDestroyImageView(device, depthImageView, nullptr);
273-
vkDestroyImage(device, depthImage, nullptr);
274-
vkFreeMemory(device, depthImageMemory, nullptr);
266+
for (size_t i = 0; i < swapChainImages.size(); i++) {
267+
vkDestroyImageView(device, colorImageViews[i], nullptr);
268+
vkDestroyImage(device, colorImages[i], nullptr);
269+
vkFreeMemory(device, colorImagesMemory[i], nullptr);
270+
vkDestroyImageView(device, depthMsaaImagesView[i], nullptr);
271+
vkDestroyImage(device, depthMsaaImages[i], nullptr);
272+
vkFreeMemory(device, depthMsaaImagesMemory[i], nullptr);
273+
vkDestroyImageView(device, depthImagesView[i], nullptr);
274+
vkDestroyImage(device, depthImages[i], nullptr);
275+
vkFreeMemory(device, depthImagesMemory[i], nullptr);
276+
}
275277

276278
for (auto framebuffer : swapChainFramebuffers) {
277279
vkDestroyFramebuffer(device, framebuffer, nullptr);
@@ -778,10 +780,10 @@ class HelloTriangleApplication {
778780

779781
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
780782
std::array<VkImageView, 4> attachments = {
781-
colorImageView,
782-
depthMsaaImageView,
783+
colorImageViews[i],
784+
depthMsaaImagesView[i],
783785
swapChainImageViews[i],
784-
depthImageView
786+
depthImagesView[i]
785787
};
786788

787789
VkFramebufferCreateInfo framebufferInfo = {};
@@ -814,24 +816,39 @@ class HelloTriangleApplication {
814816
void createColorResources() {
815817
VkFormat colorFormat = swapChainImageFormat;
816818

817-
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, colorFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, colorImage, colorImageMemory);
818-
colorImageView = createImageView(colorImage, colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
819+
colorImages.resize(swapChainImages.size());
820+
colorImagesMemory.resize(swapChainImages.size());
821+
colorImageViews.resize(swapChainImages.size());
822+
823+
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
824+
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, colorFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, colorImages[i], colorImagesMemory[i]);
825+
colorImageViews[i] = createImageView(colorImages[i], colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
819826

820-
transitionImageLayout(colorImage, colorFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1);
827+
transitionImageLayout(colorImages[i], colorFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1);
828+
}
821829
}
822830

823831
void createDepthResources() {
824832
VkFormat depthFormat = findDepthFormat();
825833

826-
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, depthImage, depthImageMemory);
827-
depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
834+
depthImages.resize(swapChainImages.size());
835+
depthImagesMemory.resize(swapChainImages.size());
836+
depthImagesView.resize(swapChainImages.size());
837+
depthMsaaImages.resize(swapChainImages.size());
838+
depthMsaaImagesMemory.resize(swapChainImages.size());
839+
depthMsaaImagesView.resize(swapChainImages.size());
840+
841+
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]);
843+
depthImagesView[i] = createImageView(depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
828844

829-
transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
845+
transitionImageLayout(depthImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
830846

831-
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, depthMsaaImage, depthMsaaImageMemory);
832-
depthMsaaImageView = createImageView(depthMsaaImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
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);
833849

834-
transitionImageLayout(depthMsaaImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
850+
transitionImageLayout(depthMsaaImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
851+
}
835852
}
836853

837854
VkFormat findSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features) {

0 commit comments

Comments
 (0)