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

Skip to content

Commit 7bd0389

Browse files
committed
Rewrite Sample explanation
1 parent b509fef commit 7bd0389

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

09_Generating_Mipmaps.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,49 @@ Our texture image's mipmaps are now completely filled.
233233

234234
## Sampler
235235

236-
One last thing we need to do before we see the results is modify `textureSampler`.
236+
While the `VkImage` holds the mipmap data, `VkSampler` controls how that data is read while rendering. Vulkan allows us to specify `minLod`, `maxLod`, `mipLodBias`, and `mipmapMode`. When a texture is sampled, the sampler selects a mip level according to the following pseudocode:
237+
238+
```c++
239+
lod = getLodLevelFromScreenSize(); //smaller when the object is close, may be negative
240+
lod = clamp(lod + mipLodBias, minLod, maxLod);
241+
242+
level = clamp(floor(lod), 0, texture.mipLevels - 1); //clamped to the number of mip levels in the texture
243+
244+
if (mipmapMode == VK_SAMPLER_MIPMAP_MODE_NEAREST) {
245+
color = sample(level);
246+
} else {
247+
color = blend(sample(level), sample(level + 1));
248+
}
249+
```
250+
251+
If `samplerInfo.mipmapMode` is `VK_SAMPLER_MIPMAP_MODE_NEAREST`, `lod` selects the mip level to sample from. If the mipmap mode is `VK_SAMPLER_MIPMAP_MODE_LINEAR`, `lod` is used to select two mip levels to be sampled. Those levels are sampled and the results are linearly blended.
252+
253+
The sample operation is also affected by `lod`:
254+
255+
```c++
256+
if (lod <= 0) {
257+
color = readTexture(uv, magFilter);
258+
} else {
259+
color = readTexture(uv, minFilter);
260+
}
261+
```
262+
263+
If the object is close to the camera, `magFilter` is used as the filter. If the object is further from the camera, `minFilter` is used. Normally, `lod` is non-negative, and is only 0 when close the camera. `mipLodBias` lets us force Vulkan to use lower mip levels than it normally would.
264+
265+
To see the results of this chapter, we need to choose values for our `textureSampler`. We've already set the `minFilter` and `magFilter` to use `VK_FILTER_LINEAR`. We just need to choose values for `minLod`, `maxLod`, `mipLodBias`, and `mipmapMode`.
237266

238267
```c++
239268
void createTextureSampler() {
240269
...
270+
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
271+
samplerInfo.minLod = 0; // Optional
241272
samplerInfo.maxLod = static_cast<float>(mipLevels);
273+
samplerInfo.mipLodBias = 0; // Optional
242274
...
243275
}
244276
```
245277
246-
This configures the sampler to sample from all mip levels up to `mipLevels`.
278+
To allow the full range of mip levels to be used, we set `minLod` to 0, and `maxLod` to the number of mip levels. We have no reason to change the `lod` value , so we set `mipLodBias` to 0.
247279
248280
Now run your program and you should see the following:
249281

code/28_mipmapping.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,9 @@ class HelloTriangleApplication {
918918
samplerInfo.compareEnable = VK_FALSE;
919919
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
920920
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
921+
samplerInfo.minLod = 0;
921922
samplerInfo.maxLod = static_cast<float>(mipLevels);
923+
samplerInfo.mipLodBias = 0;
922924

923925
if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
924926
throw std::runtime_error("failed to create texture sampler!");

0 commit comments

Comments
 (0)