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

Skip to content

Commit 937e6ed

Browse files
committed
Make generateMipmaps more generic
1 parent 7c2cc66 commit 937e6ed

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

09_Generating_Mipmaps.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ This will leave each level of the texture image in `VK_IMAGE_LAYOUT_TRANSFER_DST
9999
We're now going to write the function generates the mipmaps:
100100
101101
```c++
102-
void generateMipmaps(int32_t texWidth, int32_t texHeight) {
102+
void generateMipmaps(VkImage image, int32_t texWidth, int32_t texHeight, uint32_t mipLevels) {
103103
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
104104
...
105105
endSingleTimeCommands(commandBuffer);
@@ -108,11 +108,10 @@ We're now going to write the function generates the mipmaps:
108108

109109
Since we can't use `transitionImageLayout`, we need to record and submit another `VkCommandBuffer`.
110110

111-
112111
```c++
113112
VkImageMemoryBarrier barrier = {};
114113
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
115-
barrier.image = textureImage;
114+
barrier.image = image;
116115
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
117116
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
118117
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
@@ -170,8 +169,8 @@ Next, we specify the regions that will be used in the blit operation. The source
170169
171170
```c++
172171
vkCmdBlitImage(commandBuffer,
173-
textureImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
174-
textureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
172+
image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
173+
image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
175174
1, &blit,
176175
VK_FILTER_LINEAR);
177176
```
@@ -227,7 +226,11 @@ Before we end the command buffer, we insert one more pipeline barrier. This barr
227226
Finally, add the call to `generateMipmaps` in `createTextureImage`:
228227
229228
```c++
230-
generateMipmaps(texWidth, texHeight);
229+
transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, mipLevels);
230+
copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
231+
//transitioned to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL while generating mipmaps
232+
...
233+
generateMipmaps(textureImage, texWidth, texHeight, mipLevels);
231234
```
232235

233236
Our texture image's mipmaps are now completely filled.

code/28_mipmapping.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -817,15 +817,15 @@ class HelloTriangleApplication {
817817
vkDestroyBuffer(device, stagingBuffer, nullptr);
818818
vkFreeMemory(device, stagingBufferMemory, nullptr);
819819

820-
generateMipmaps(texWidth, texHeight);
820+
generateMipmaps(textureImage, texWidth, texHeight, mipLevels);
821821
}
822822

823-
void generateMipmaps(int32_t texWidth, int32_t texHeight) {
823+
void generateMipmaps(VkImage image, int32_t texWidth, int32_t texHeight, uint32_t mipLevels) {
824824
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
825825

826826
VkImageMemoryBarrier barrier = {};
827827
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
828-
barrier.image = textureImage;
828+
barrier.image = image;
829829
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
830830
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
831831
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
@@ -864,8 +864,8 @@ class HelloTriangleApplication {
864864
blit.dstSubresource.layerCount = 1;
865865

866866
vkCmdBlitImage(commandBuffer,
867-
textureImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
868-
textureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
867+
image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
868+
image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
869869
1, &blit,
870870
VK_FILTER_LINEAR);
871871

0 commit comments

Comments
 (0)