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

Skip to content

Add chapter on mipmapping#72

Merged
Overv merged 24 commits intoOverv:masterfrom
vazgriz:master
Mar 30, 2018
Merged

Add chapter on mipmapping#72
Overv merged 24 commits intoOverv:masterfrom
vazgriz:master

Conversation

@vazgriz
Copy link
Copy Markdown

@vazgriz vazgriz commented Mar 25, 2018

I've written a chapter that explains how to generate mip maps. I figured this was a basic effect that fell under the scope of vulkan-tutorial.com. This chapter comes after Model Loading.

Comment thread 09_Generating_Mipmaps.md
```c++
barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

srcAccessMask should probably be VK_ACCESS_TRANSFER_READ_BIT here since the (LAYOUT_TRANSFER_SRC_OPTIMAL) image was read, not written.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thank you.

@Overv Overv self-assigned this Mar 26, 2018
@Overv
Copy link
Copy Markdown
Owner

Overv commented Mar 26, 2018

Thank you very much for writing this! I'll review it and get back to you.

Comment thread 09_Generating_Mipmaps.md Outdated
mipLevels = 1;
int32_t mipWidth = texWidth;
int32_t mipHeight = texHeight;
while (mipWidth > 1 && mipHeight > 1) {
Copy link
Copy Markdown
Owner

@Overv Overv Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace this with a logarithm calculation. The loop in the generateMipmaps function is fine.

Copy link
Copy Markdown
Contributor

@SaschaWillems SaschaWillems Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how I calculate number of mips in my runtime mip-map generation example:

texture.mipLevels = floor(log2(std::max(texture.width, texture.height))) + 1;

This matches the Vulkan/GL specs on how number of mip levels is calculated.

Comment thread 09_Generating_Mipmaps.md Outdated
```
This will leave each level of the texture image in `VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL`.

We're now going to write the function generates the mip maps:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that generates the mipmaps:

Comment thread 09_Generating_Mipmaps.md Outdated
1, &blit, VK_FILTER_LINEAR);
```

Now, we record the blit command. Note that `textureImage` is used for both the `srcImage` and `dstImage` parameter. The source mip level was just transitioned to `VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL` and the destination level is still in `VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL` from `createTextureImage`. The last parameter says to use a linear filter when scaling the data.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention VkFilter and that you have the same interpolation options as with texture sampling in general

Comment thread 09_Generating_Mipmaps.md Outdated
We're now going to write the function generates the mip maps:

```c++
void generateMipmaps(int32_t texWidth, int32_t texHeight) {
Copy link
Copy Markdown
Owner

@Overv Overv Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it a bit more generic by making the image and miplevels parameters.

Comment thread 09_Generating_Mipmaps.md Outdated

![](/images/mipmaps_comparison.png)

The left image is our new program. The right image is our program without mipmaps. The most noticeable difference is the writing on the signs. With mipmaps, the writing has been blurred. Without mipmaps, the writing has harsh edges and gaps from Moiré artifacts.
Copy link
Copy Markdown
Owner

@Overv Overv Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps change "blurred" to "smoothed" since this is arguably a desirable change

Comment thread 09_Generating_Mipmaps.md

![](/images/highmipmaps.png)

This is how higher mip levels will be used when objects are further away from the camera.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice demonstration 👍

@SaschaWillems
Copy link
Copy Markdown
Contributor

I think it would be good idea to add a chapter on anisotropic filtering. If you're going to use mip maps you're most of the time also going to use anisotropic filtering.

Comment thread 09_Generating_Mipmaps.md Outdated
}
```

This configures the sampler to sample from all mip levels up to `mipLevels`.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add some explanation why minLod and maxLoad are floats and what mipLodBias does. You can direct readers who want to learn all the advanced details to https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#textures-level-of-detail-operation

@Overv
Copy link
Copy Markdown
Owner

Overv commented Mar 27, 2018

@SaschaWillems The chapter about image views and samplers already mentions anisotropic filtering, but it may be nice to mention it again in the mipmapping chapter.

@vazgriz
Copy link
Copy Markdown
Author

vazgriz commented Mar 28, 2018

Thanks for the feedback. I've added most of changes suggested. I'm not sure what to write regarding anisotropic filtering, since the specification doesn't specify how mipmapping affects it. How did you get the image used for anisotropic filtering in the "Image view and sampler" chapter? Did you make it yourself, or did you find it somewhere?

@Overv
Copy link
Copy Markdown
Owner

Overv commented Mar 28, 2018

@vazgriz Yeah, I made that image in Blender :) I guess we can skip anisotropic filtering for now. I'll do a final review tomorrow and if it looks good, then I'll merge and publish it.

Comment thread 09_Generating_Mipmaps.md Outdated
You can play around with the sampler settings to see how they affect mipmapping. For example, by changing `minLod`, you can force the sampler to not use the lowest mip levels:

```c++
samplerInfo.minLod = static_cast<float>(mipLevels / 2);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the indentation here

Comment thread 09_Generating_Mipmaps.md Outdated

Now, we record the blit command. Note that `textureImage` is used for both the `srcImage` and `dstImage` parameter. This is because we're blitting between different levels of the same image. The source mip level was just transitioned to `VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL` and the destination level is still in `VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL` from `createTextureImage`.

The last parameter allows us to specify a `VkFilter` to use in the blit. We have the same filtering options here that we had when making the `VkSampler`. We use the `VK_FILTER_LINEAR` to enable filtering.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to enable interpolation

@Overv
Copy link
Copy Markdown
Owner

Overv commented Mar 29, 2018

If you could just fix the remaining comments (the two just now and the We're now going to write the function generates the mip maps sentence), I'll approve it. You can see what it's going to look like here:

https://dev.vulkan-tutorial.com/Generating_Mipmaps

@vazgriz
Copy link
Copy Markdown
Author

vazgriz commented Mar 29, 2018

Done

@Overv Overv merged commit babc3a0 into Overv:master Mar 30, 2018
@Overv
Copy link
Copy Markdown
Owner

Overv commented Apr 3, 2018

Could you update the text to reflect your change to transitionImageLayout to have the mipmap parameter?

@vazgriz
Copy link
Copy Markdown
Author

vazgriz commented Apr 4, 2018

Ok. I think this has to be done a new pull request. See #74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants