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

Skip to content

Commit 21779c1

Browse files
committed
Rewrite section to calculate mip levels
1 parent 889c9fd commit 21779c1

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

09_Generating_Mipmaps.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@ Mipmaps are precalculated, downscaled versions of an image. Each new image is ha
77

88
## Image creation
99

10-
In Vulkan, each of the mip images is stored in different *mip levels* of a `VkImage`. Mip level 0 is the original image, and each mip level after that is half the size of previous level. The number of mip levels is specified when the `VkImage` is created. Up until now, we have always set this value to one. In `createTextureImage`, we need to calculate the number of mip levels from the dimensions of the image. First, add a class member to store this number:
10+
In Vulkan, each of the mip images is stored in different *mip levels* of a `VkImage`. Mip level 0 is the original image, and each mip level after that is half the size of previous level. The mip levels after level 0 are commonly referred to as the *mip chain.*
11+
12+
The number of mip levels is specified when the `VkImage` is created. Up until now, we have always set this value to one. We need to calculate the number of mip levels from the dimensions of the image. First, add a class member to store this number:
1113

1214
```c++
1315
...
1416
uint32_t mipLevels;
1517
VkImage textureImage;
1618
...
1719
```
18-
The value for `mipLevels` can be found with a simple loop in `createTextureImage`:
20+
21+
The value for `mipLevels` can be found once we've loaded the texture in `createTextureImage`:
22+
23+
```c++
24+
int texWidth, texHeight, texChannels;
25+
stbi_uc* pixels = stbi_load(TEXTURE_PATH.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
26+
...
27+
mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(texWidth, texHeight)))) + 1;
28+
29+
```
30+
31+
The `max` function selects the largest dimension. The `log2` function calculates how many times that dimension can be divided by 2. The `floor` function handles cases where the largest dimension is not a power of 2. This value is the number of levels in the mip chain. `1` is added so that the original image has a mip level.
32+
33+
To use this value, we need to change the `createImage` and `createImageView` functions to allow us to specify the number of mip levels. Add a `mipLevels` parameter to the functions:
1934

2035
```c++
2136
int texWidth, texHeight, texChannels;

code/28_mipmapping.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -791,20 +791,12 @@ class HelloTriangleApplication {
791791
int texWidth, texHeight, texChannels;
792792
stbi_uc* pixels = stbi_load(TEXTURE_PATH.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
793793
VkDeviceSize imageSize = texWidth * texHeight * 4;
794+
mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(texWidth, texHeight)))) + 1;
794795

795796
if (!pixels) {
796797
throw std::runtime_error("failed to load texture image!");
797798
}
798799

799-
mipLevels = 1;
800-
int32_t mipWidth = texWidth;
801-
int32_t mipHeight = texHeight;
802-
while (mipWidth > 1 && mipHeight > 1) {
803-
mipLevels++;
804-
mipWidth /= 2;
805-
mipHeight /= 2;
806-
}
807-
808800
VkBuffer stagingBuffer;
809801
VkDeviceMemory stagingBufferMemory;
810802
createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);

0 commit comments

Comments
 (0)