You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 09_Generating_Mipmaps.md
+34-2Lines changed: 34 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -233,17 +233,49 @@ Our texture image's mipmaps are now completely filled.
233
233
234
234
## Sampler
235
235
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`.
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.
247
279
248
280
Now run your program and you should see the following:
0 commit comments