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

Skip to content

Commit 75f648e

Browse files
committed
Fix maxSamplerAnisotropy limit not verified (fixes #216)
1 parent d22199c commit 75f648e

7 files changed

Lines changed: 39 additions & 9 deletions

File tree

code/24_sampler.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ class HelloTriangleApplication {
755755
}
756756

757757
void createTextureSampler() {
758+
VkPhysicalDeviceProperties properties{};
759+
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
760+
758761
VkSamplerCreateInfo samplerInfo{};
759762
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
760763
samplerInfo.magFilter = VK_FILTER_LINEAR;
@@ -763,7 +766,7 @@ class HelloTriangleApplication {
763766
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
764767
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
765768
samplerInfo.anisotropyEnable = VK_TRUE;
766-
samplerInfo.maxAnisotropy = 16.0f;
769+
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
767770
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
768771
samplerInfo.unnormalizedCoordinates = VK_FALSE;
769772
samplerInfo.compareEnable = VK_FALSE;

code/25_texture_mapping.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,9 @@ class HelloTriangleApplication {
769769
}
770770

771771
void createTextureSampler() {
772+
VkPhysicalDeviceProperties properties{};
773+
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
774+
772775
VkSamplerCreateInfo samplerInfo{};
773776
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
774777
samplerInfo.magFilter = VK_FILTER_LINEAR;
@@ -777,7 +780,7 @@ class HelloTriangleApplication {
777780
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
778781
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
779782
samplerInfo.anisotropyEnable = VK_TRUE;
780-
samplerInfo.maxAnisotropy = 16.0f;
783+
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
781784
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
782785
samplerInfo.unnormalizedCoordinates = VK_FALSE;
783786
samplerInfo.compareEnable = VK_FALSE;

code/26_depth_buffering.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,9 @@ class HelloTriangleApplication {
846846
}
847847

848848
void createTextureSampler() {
849+
VkPhysicalDeviceProperties properties{};
850+
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
851+
849852
VkSamplerCreateInfo samplerInfo{};
850853
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
851854
samplerInfo.magFilter = VK_FILTER_LINEAR;
@@ -854,7 +857,7 @@ class HelloTriangleApplication {
854857
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
855858
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
856859
samplerInfo.anisotropyEnable = VK_TRUE;
857-
samplerInfo.maxAnisotropy = 16.0f;
860+
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
858861
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
859862
samplerInfo.unnormalizedCoordinates = VK_FALSE;
860863
samplerInfo.compareEnable = VK_FALSE;

code/27_model_loading.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,9 @@ class HelloTriangleApplication {
853853
}
854854

855855
void createTextureSampler() {
856+
VkPhysicalDeviceProperties properties{};
857+
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
858+
856859
VkSamplerCreateInfo samplerInfo{};
857860
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
858861
samplerInfo.magFilter = VK_FILTER_LINEAR;
@@ -861,7 +864,7 @@ class HelloTriangleApplication {
861864
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
862865
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
863866
samplerInfo.anisotropyEnable = VK_TRUE;
864-
samplerInfo.maxAnisotropy = 16.0f;
867+
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
865868
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
866869
samplerInfo.unnormalizedCoordinates = VK_FALSE;
867870
samplerInfo.compareEnable = VK_FALSE;

code/28_mipmapping.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,9 @@ class HelloTriangleApplication {
944944
}
945945

946946
void createTextureSampler() {
947+
VkPhysicalDeviceProperties properties{};
948+
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
949+
947950
VkSamplerCreateInfo samplerInfo{};
948951
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
949952
samplerInfo.magFilter = VK_FILTER_LINEAR;
@@ -952,7 +955,7 @@ class HelloTriangleApplication {
952955
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
953956
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
954957
samplerInfo.anisotropyEnable = VK_TRUE;
955-
samplerInfo.maxAnisotropy = 16.0f;
958+
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
956959
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
957960
samplerInfo.unnormalizedCoordinates = VK_FALSE;
958961
samplerInfo.compareEnable = VK_FALSE;

code/29_multisampling.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,9 @@ VkSampleCountFlagBits getMaxUsableSampleCount() {
994994
}
995995

996996
void createTextureSampler() {
997+
VkPhysicalDeviceProperties properties{};
998+
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
999+
9971000
VkSamplerCreateInfo samplerInfo{};
9981001
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
9991002
samplerInfo.magFilter = VK_FILTER_LINEAR;
@@ -1002,7 +1005,7 @@ VkSampleCountFlagBits getMaxUsableSampleCount() {
10021005
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
10031006
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
10041007
samplerInfo.anisotropyEnable = VK_TRUE;
1005-
samplerInfo.maxAnisotropy = 16.0f;
1008+
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
10061009
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
10071010
samplerInfo.unnormalizedCoordinates = VK_FALSE;
10081011
samplerInfo.compareEnable = VK_FALSE;

en/06_Texture_mapping/01_Image_view_and_sampler.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,27 @@ floors and walls.
218218

219219
```c++
220220
samplerInfo.anisotropyEnable = VK_TRUE;
221-
samplerInfo.maxAnisotropy = 16.0f;
221+
samplerInfo.maxAnisotropy = ???;
222222
```
223223

224224
These two fields specify if anisotropic filtering should be used. There is no
225225
reason not to use this unless performance is a concern. The `maxAnisotropy`
226226
field limits the amount of texel samples that can be used to calculate the final
227227
color. A lower value results in better performance, but lower quality results.
228-
There is no graphics hardware available today that will use more than 16
229-
samples, because the difference is negligible beyond that point.
228+
To figure out which value we can use, we need to retrieve the properties of the physical device like so:
229+
230+
```c++
231+
VkPhysicalDeviceProperties properties{};
232+
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
233+
```
234+
235+
If you look at the documentation for the `VkPhysicalDeviceProperties` structure, you'll see that it contains a `VkPhysicalDeviceLimits` member named `limits`. This struct in turn has a member called `maxSamplerAnisotropy` and this is the maximum value we can specify for `maxAnisotropy`. If we want to go for maximum quality, we can simply use that value directly:
236+
237+
```c++
238+
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
239+
```
240+
241+
You can either query the properties at the beginning of your program and pass them around to the functions that need them, or query them in the `createTextureSampler` function itself.
230242

231243
```c++
232244
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;

0 commit comments

Comments
 (0)