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

Skip to content

Commit 24d6a2e

Browse files
committed
Revert "Fix all frames in flight using a single depth buffer"
This reverts commit 3272ef7.
1 parent 2ec77b0 commit 24d6a2e

5 files changed

Lines changed: 76 additions & 86 deletions

File tree

07_Depth_buffering.md

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ range of `0.0` to `1.0` using the `GLM_FORCE_DEPTH_ZERO_TO_ONE` definition.
124124
## Depth image and view
125125

126126
A depth attachment is based on an image, just like the color attachment. The
127-
difference is that the swap chain will not automatically create depth images for
128-
us. We need a depth image for every frame that can be in flight simultaneously. In practice it's easiest to just have one per swap chain image similar to the uniform buffers. The depth images will again require the trifecta of resources: image, memory and image view.
127+
difference is that the swap chain will not automatically create depth images for us. We only need a single depth image, because only one draw operation is
128+
running at once. The depth image will again require the trifecta of resources:
129+
image, memory and image view.
129130

130131
```c++
131-
std::vector<VkImage> depthImages;
132-
std::vector<VkDeviceMemory> depthImagesMemory;
133-
std::vector<VkImageView> depthImagesView;
132+
VkImage depthImage;
133+
VkDeviceMemory depthImageMemory;
134+
VkImageView depthImageView;
134135
```
135136

136137
Create a new function `createDepthResources` to set up these resources:
@@ -262,27 +263,15 @@ bool hasStencilComponent(VkFormat format) {
262263
Call the function to find a depth format from `createDepthResources`:
263264
264265
```c++
265-
void createDepthResources() {
266-
VkFormat depthFormat = findDepthFormat();
267-
}
266+
VkFormat depthFormat = findDepthFormat();
268267
```
269268

270269
We now have all the required information to invoke our `createImage` and
271270
`createImageView` helper functions:
272271

273272
```c++
274-
void createDepthResources() {
275-
VkFormat depthFormat = findDepthFormat();
276-
277-
depthImages.resize(swapChainImages.size());
278-
depthImagesMemory.resize(swapChainImages.size());
279-
depthImagesView.resize(swapChainImages.size());
280-
281-
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
282-
createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImagesMemory[i]);
283-
depthImagesView[i] = createImageView(depthImages[i], depthFormat);
284-
}
285-
}
273+
createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
274+
depthImageView = createImageView(depthImage, depthFormat);
286275
```
287276
288277
However, the `createImageView` function currently assumes that the subresource
@@ -302,7 +291,7 @@ Update all calls to this function to use the right aspect:
302291
```c++
303292
swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
304293
...
305-
depthImagesView[i] = createImageView(depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
294+
depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
306295
...
307296
textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
308297
```
@@ -315,7 +304,7 @@ render pass like the color attachment, but here I've chosen to use a pipeline
315304
barrier because the transition only needs to happen once:
316305

317306
```c++
318-
transitionImageLayout(depthImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
307+
transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
319308
```
320309
321310
The undefined layout can be used as initial layout, because there are no
@@ -431,12 +420,13 @@ attachments.
431420
## Framebuffer
432421
433422
The next step is to modify the framebuffer creation to bind the depth image to
434-
the depth attachment. Go to `createFramebuffers` and specify the right depth image view as second attachment:
423+
the depth attachment. Go to `createFramebuffers` and specify the depth image
424+
view as second attachment:
435425
436426
```c++
437427
std::array<VkImageView, 2> attachments = {
438428
swapChainImageViews[i],
439-
depthImagesView[i]
429+
depthImageView
440430
};
441431
442432
VkFramebufferCreateInfo framebufferInfo = {};
@@ -449,6 +439,10 @@ framebufferInfo.height = swapChainExtent.height;
449439
framebufferInfo.layers = 1;
450440
```
451441

442+
The color attachment differs for every swap chain image, but the same depth
443+
image can be used by all of them because only a single subpass is running at the
444+
same time due to our semaphores.
445+
452446
You'll also need to move the call to `createFramebuffers` to make sure that it
453447
is called after the depth image view has actually been created:
454448

@@ -570,11 +564,9 @@ The cleanup operations should happen in the swap chain cleanup function:
570564

571565
```c++
572566
void cleanupSwapChain() {
573-
for (size_t i = 0; i < swapChainImages.size(); i++) {
574-
vkDestroyImageView(device, depthImagesView[i], nullptr);
575-
vkDestroyImage(device, depthImages[i], nullptr);
576-
vkFreeMemory(device, depthImagesMemory[i], nullptr);
577-
}
567+
vkDestroyImageView(device, depthImageView, nullptr);
568+
vkDestroyImage(device, depthImage, nullptr);
569+
vkFreeMemory(device, depthImageMemory, nullptr);
578570

579571
...
580572
}

09_Generating_Mipmaps.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayo
5555
Update all calls to these functions to use the right values:
5656
5757
```c++
58-
createImage(swapChainExtent.width, swapChainExtent.height, 1, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImagesMemory[i]);
58+
createImage(swapChainExtent.width, swapChainExtent.height, 1, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
5959
...
6060
createImage(texWidth, texHeight, mipLevels, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, textureImage, textureImageMemory);
6161
```
6262
```c++
6363
swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
6464
...
65-
depthImagesView[i] = createImageView(depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
65+
depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
6666
...
6767
textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT, mipLevels);
6868
```
6969
```c++
70-
transitionImageLayout(depthImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
70+
transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
7171
...
7272
transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, mipLevels);
7373
```
@@ -346,6 +346,28 @@ These settings will produce this image:
346346

347347
This is how higher mip levels will be used when objects are further away from the camera.
348348

349+
350+
## Conclusion
351+
352+
It has taken a lot of work to get to this point, but now you finally have a good
353+
base for a Vulkan program. The knowledge of the basic principles of Vulkan that
354+
you now possess should be sufficient to start exploring more of the features,
355+
like:
356+
357+
* Push constants
358+
* Instanced rendering
359+
* Dynamic uniforms
360+
* Separate images and sampler descriptors
361+
* Pipeline cache
362+
* Multi-threaded command buffer generation
363+
* Multiple subpasses
364+
* Compute shaders
365+
366+
The current program can be extended in many ways, like adding Blinn-Phong
367+
lighting, post-processing effects and shadow mapping. You should be able to
368+
learn how these effects work from tutorials for other APIs, because despite
369+
Vulkan's explicitness, many concepts still work the same.
370+
349371
[C++ code](/code/28_mipmapping.cpp) /
350372
[Vertex shader](/code/26_shader_depth.vert) /
351373
[Fragment shader](/code/26_shader_depth.frag)

code/26_depth_buffering.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ class HelloTriangleApplication {
165165

166166
VkCommandPool commandPool;
167167

168-
std::vector<VkImage> depthImages;
169-
std::vector<VkDeviceMemory> depthImagesMemory;
170-
std::vector<VkImageView> depthImagesView;
168+
VkImage depthImage;
169+
VkDeviceMemory depthImageMemory;
170+
VkImageView depthImageView;
171171

172172
VkImage textureImage;
173173
VkDeviceMemory textureImageMemory;
@@ -245,11 +245,9 @@ class HelloTriangleApplication {
245245
}
246246

247247
void cleanupSwapChain() {
248-
for (size_t i = 0; i < swapChainImages.size(); i++) {
249-
vkDestroyImageView(device, depthImagesView[i], nullptr);
250-
vkDestroyImage(device, depthImages[i], nullptr);
251-
vkFreeMemory(device, depthImagesMemory[i], nullptr);
252-
}
248+
vkDestroyImageView(device, depthImageView, nullptr);
249+
vkDestroyImage(device, depthImage, nullptr);
250+
vkFreeMemory(device, depthImageMemory, nullptr);
253251

254252
for (auto framebuffer : swapChainFramebuffers) {
255253
vkDestroyFramebuffer(device, framebuffer, nullptr);
@@ -731,7 +729,7 @@ class HelloTriangleApplication {
731729
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
732730
std::array<VkImageView, 2> attachments = {
733731
swapChainImageViews[i],
734-
depthImagesView[i]
732+
depthImageView
735733
};
736734

737735
VkFramebufferCreateInfo framebufferInfo = {};
@@ -764,16 +762,10 @@ class HelloTriangleApplication {
764762
void createDepthResources() {
765763
VkFormat depthFormat = findDepthFormat();
766764

767-
depthImages.resize(swapChainImages.size());
768-
depthImagesMemory.resize(swapChainImages.size());
769-
depthImagesView.resize(swapChainImages.size());
765+
createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
766+
depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
770767

771-
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
772-
createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImagesMemory[i]);
773-
depthImagesView[i] = createImageView(depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
774-
775-
transitionImageLayout(depthImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
776-
}
768+
transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
777769
}
778770

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

code/27_model_loading.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ class HelloTriangleApplication {
169169

170170
VkCommandPool commandPool;
171171

172-
std::vector<VkImage> depthImages;
173-
std::vector<VkDeviceMemory> depthImagesMemory;
174-
std::vector<VkImageView> depthImagesView;
172+
VkImage depthImage;
173+
VkDeviceMemory depthImageMemory;
174+
VkImageView depthImageView;
175175

176176
VkImage textureImage;
177177
VkDeviceMemory textureImageMemory;
@@ -252,11 +252,9 @@ class HelloTriangleApplication {
252252
}
253253

254254
void cleanupSwapChain() {
255-
for (size_t i = 0; i < swapChainImages.size(); i++) {
256-
vkDestroyImageView(device, depthImagesView[i], nullptr);
257-
vkDestroyImage(device, depthImages[i], nullptr);
258-
vkFreeMemory(device, depthImagesMemory[i], nullptr);
259-
}
255+
vkDestroyImageView(device, depthImageView, nullptr);
256+
vkDestroyImage(device, depthImage, nullptr);
257+
vkFreeMemory(device, depthImageMemory, nullptr);
260258

261259
for (auto framebuffer : swapChainFramebuffers) {
262260
vkDestroyFramebuffer(device, framebuffer, nullptr);
@@ -738,7 +736,7 @@ class HelloTriangleApplication {
738736
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
739737
std::array<VkImageView, 2> attachments = {
740738
swapChainImageViews[i],
741-
depthImagesView[i]
739+
depthImageView
742740
};
743741

744742
VkFramebufferCreateInfo framebufferInfo = {};
@@ -771,16 +769,10 @@ class HelloTriangleApplication {
771769
void createDepthResources() {
772770
VkFormat depthFormat = findDepthFormat();
773771

774-
depthImages.resize(swapChainImages.size());
775-
depthImagesMemory.resize(swapChainImages.size());
776-
depthImagesView.resize(swapChainImages.size());
772+
createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
773+
depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
777774

778-
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
779-
createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImagesMemory[i]);
780-
depthImagesView[i] = createImageView(depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
781-
782-
transitionImageLayout(depthImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
783-
}
775+
transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
784776
}
785777

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

code/28_mipmapping.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ class HelloTriangleApplication {
169169

170170
VkCommandPool commandPool;
171171

172-
std::vector<VkImage> depthImages;
173-
std::vector<VkDeviceMemory> depthImagesMemory;
174-
std::vector<VkImageView> depthImagesView;
172+
VkImage depthImage;
173+
VkDeviceMemory depthImageMemory;
174+
VkImageView depthImageView;
175175

176176
uint32_t mipLevels;
177177
VkImage textureImage;
@@ -253,11 +253,9 @@ class HelloTriangleApplication {
253253
}
254254

255255
void cleanupSwapChain() {
256-
for (size_t i = 0; i < swapChainImages.size(); i++) {
257-
vkDestroyImageView(device, depthImagesView[i], nullptr);
258-
vkDestroyImage(device, depthImages[i], nullptr);
259-
vkFreeMemory(device, depthImagesMemory[i], nullptr);
260-
}
256+
vkDestroyImageView(device, depthImageView, nullptr);
257+
vkDestroyImage(device, depthImage, nullptr);
258+
vkFreeMemory(device, depthImageMemory, nullptr);
261259

262260
for (auto framebuffer : swapChainFramebuffers) {
263261
vkDestroyFramebuffer(device, framebuffer, nullptr);
@@ -739,7 +737,7 @@ class HelloTriangleApplication {
739737
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
740738
std::array<VkImageView, 2> attachments = {
741739
swapChainImageViews[i],
742-
depthImagesView[i]
740+
depthImageView
743741
};
744742

745743
VkFramebufferCreateInfo framebufferInfo = {};
@@ -772,16 +770,10 @@ class HelloTriangleApplication {
772770
void createDepthResources() {
773771
VkFormat depthFormat = findDepthFormat();
774772

775-
depthImages.resize(swapChainImages.size());
776-
depthImagesMemory.resize(swapChainImages.size());
777-
depthImagesView.resize(swapChainImages.size());
773+
createImage(swapChainExtent.width, swapChainExtent.height, 1, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
774+
depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
778775

779-
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
780-
createImage(swapChainExtent.width, swapChainExtent.height, 1, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImagesMemory[i]);
781-
depthImagesView[i] = createImageView(depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
782-
783-
transitionImageLayout(depthImages[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
784-
}
776+
transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
785777
}
786778

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

0 commit comments

Comments
 (0)