From f601ffddfc8bf0582de0bc86b987f332d0ca24c7 Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Date: Sun, 21 Mar 2021 00:15:12 +0100 Subject: [PATCH] Use the real anisotropic filtering value in Vulkan When creating the Vulkan texture samplers, make them have the real anisotropic filtering value selected by the user. This has two side effects: * We no longer need two sets of texture samplers in Vulkan (one with and another one without anisotropic filtering). * The anisotropic filter value in Vulkan is no longer an on/off switch and we use the value as chosen by the user. --- doc/040_cvarlist.md | 2 -- src/client/refresh/vk/header/local.h | 7 ----- src/client/refresh/vk/header/qvk.h | 8 ++---- src/client/refresh/vk/vk_common.c | 40 +++++++--------------------- src/client/refresh/vk/vk_image.c | 2 -- src/common/header/shared.h | 7 +++++ 6 files changed, 19 insertions(+), 47 deletions(-) diff --git a/doc/040_cvarlist.md b/doc/040_cvarlist.md index 7879eaf6..050a2dfb 100644 --- a/doc/040_cvarlist.md +++ b/doc/040_cvarlist.md @@ -245,8 +245,6 @@ it's `+set busywait 0` (setting the `busywait` cvar) and `-portable` dependent on the GPU driver, most of them support `1`, `2`, `4`, `8` and `16`. Anisotropic filtering gives a huge improvement to texture quality by a negligible performance impact. - If vulkan render have used, flag is only toggle anisotropic filtering - without use specific level. * **r_msaa_samples**: Full scene anti aliasing samples. The number of samples depends on the GPU driver, most drivers support at least `2`, diff --git a/src/client/refresh/vk/header/local.h b/src/client/refresh/vk/header/local.h index b1ffc7d9..9c46d91c 100644 --- a/src/client/refresh/vk/header/local.h +++ b/src/client/refresh/vk/header/local.h @@ -50,13 +50,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // fall over #define ROLL 2 -#ifndef min -#define min(a, b) (((a) < (b)) ? (a) : (b)) -#endif -#ifndef max -#define max(a, b) (((a) > (b)) ? (a) : (b)) -#endif - extern viddef_t vid; typedef struct image_s diff --git a/src/client/refresh/vk/header/qvk.h b/src/client/refresh/vk/header/qvk.h index ba08bb5b..2e9738f4 100644 --- a/src/client/refresh/vk/header/qvk.h +++ b/src/client/refresh/vk/header/qvk.h @@ -68,12 +68,8 @@ typedef enum S_LINEAR = 1, S_MIPMAP_NEAREST = 2, S_MIPMAP_LINEAR = 3, - S_ANISO_NEAREST = 4, - S_ANISO_LINEAR = 5, - S_ANISO_MIPMAP_NEAREST = 6, - S_ANISO_MIPMAP_LINEAR = 7, - S_NEAREST_UNNORMALIZED = 8, - S_SAMPLER_CNT = 9 + S_NEAREST_UNNORMALIZED = 4, + S_SAMPLER_CNT = 5 } qvksampler_t; #define NUM_SAMPLERS (S_SAMPLER_CNT * 2) diff --git a/src/client/refresh/vk/vk_common.c b/src/client/refresh/vk/vk_common.c index 08ffb225..653906fa 100644 --- a/src/client/refresh/vk/vk_common.c +++ b/src/client/refresh/vk/vk_common.c @@ -861,6 +861,14 @@ static void CreateSamplersHelper(VkSampler *samplers, VkSamplerAddressMode addre .unnormalizedCoordinates = VK_FALSE }; + assert((vk_device.properties.limits.maxSamplerAnisotropy > 1.f) && "maxSamplerAnisotropy is 1"); + if (vk_device.features.samplerAnisotropy && vk_aniso->value > 0.f) + { + const float maxAniso = min(max(vk_aniso->value, 1.f), vk_device.properties.limits.maxSamplerAnisotropy); + samplerInfo.anisotropyEnable = VK_TRUE; + samplerInfo.maxAnisotropy = maxAniso; + } + VK_VERIFY(vkCreateSampler(vk_device.logical, &samplerInfo, NULL, &samplers[S_NEAREST])); QVk_DebugSetObjectName((uint64_t)samplers[S_NEAREST], VK_OBJECT_TYPE_SAMPLER, "Sampler: S_NEAREST"); @@ -874,6 +882,8 @@ static void CreateSamplersHelper(VkSampler *samplers, VkSamplerAddressMode addre nuSamplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; nuSamplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; nuSamplerInfo.unnormalizedCoordinates = VK_TRUE; + nuSamplerInfo.anisotropyEnable = VK_FALSE; + nuSamplerInfo.maxAnisotropy = 1.f; VK_VERIFY(vkCreateSampler(vk_device.logical, &nuSamplerInfo, NULL, &samplers[S_NEAREST_UNNORMALIZED])); QVk_DebugSetObjectName((uint64_t)samplers[S_NEAREST_UNNORMALIZED], VK_OBJECT_TYPE_SAMPLER, "Sampler: S_NEAREST_UNNORMALIZED"); @@ -892,36 +902,6 @@ static void CreateSamplersHelper(VkSampler *samplers, VkSamplerAddressMode addre samplerInfo.maxLod = 1.f; VK_VERIFY(vkCreateSampler(vk_device.logical, &samplerInfo, NULL, &samplers[S_LINEAR])); QVk_DebugSetObjectName((uint64_t)samplers[S_LINEAR], VK_OBJECT_TYPE_SAMPLER, "Sampler: S_LINEAR"); - - // aniso samplers - assert((vk_device.properties.limits.maxSamplerAnisotropy > 1.f) && "maxSamplerAnisotropy is 1"); - - samplerInfo.magFilter = VK_FILTER_NEAREST; - samplerInfo.minFilter = VK_FILTER_NEAREST; - samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; - if (vk_device.features.samplerAnisotropy) - { - samplerInfo.anisotropyEnable = VK_TRUE; - samplerInfo.maxAnisotropy = vk_device.properties.limits.maxSamplerAnisotropy; - } - samplerInfo.maxLod = 1.f; - - VK_VERIFY(vkCreateSampler(vk_device.logical, &samplerInfo, NULL, &samplers[S_ANISO_NEAREST])); - QVk_DebugSetObjectName((uint64_t)samplers[S_ANISO_NEAREST], VK_OBJECT_TYPE_SAMPLER, "Sampler: S_ANISO_NEAREST"); - - samplerInfo.maxLod = FLT_MAX; - VK_VERIFY(vkCreateSampler(vk_device.logical, &samplerInfo, NULL, &samplers[S_ANISO_MIPMAP_NEAREST])); - QVk_DebugSetObjectName((uint64_t)samplers[S_ANISO_MIPMAP_NEAREST], VK_OBJECT_TYPE_SAMPLER, "Sampler: S_ANISO_MIPMAP_NEAREST"); - - samplerInfo.magFilter = VK_FILTER_LINEAR; - samplerInfo.minFilter = VK_FILTER_LINEAR; - samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; - VK_VERIFY(vkCreateSampler(vk_device.logical, &samplerInfo, NULL, &samplers[S_ANISO_MIPMAP_LINEAR])); - QVk_DebugSetObjectName((uint64_t)samplers[S_ANISO_MIPMAP_LINEAR], VK_OBJECT_TYPE_SAMPLER, "Sampler: S_ANISO_MIPMAP_LINEAR"); - - samplerInfo.maxLod = 1.f; - VK_VERIFY(vkCreateSampler(vk_device.logical, &samplerInfo, NULL, &samplers[S_ANISO_LINEAR])); - QVk_DebugSetObjectName((uint64_t)samplers[S_ANISO_LINEAR], VK_OBJECT_TYPE_SAMPLER, "Sampler: S_ANISO_LINEAR"); } // internal helper diff --git a/src/client/refresh/vk/vk_image.c b/src/client/refresh/vk/vk_image.c index fe833690..4dc555a6 100644 --- a/src/client/refresh/vk/vk_image.c +++ b/src/client/refresh/vk/vk_image.c @@ -666,7 +666,6 @@ void Vk_TextureMode( char *string ) memcpy(prev_mode, string, strlen(string)); prev_mode[strlen(string)] = '\0'; - i += vk_aniso->value > 0 ? NUM_VK_MODES : 0; vk_current_sampler = i; vkDeviceWaitIdle(vk_device.logical); @@ -707,7 +706,6 @@ void Vk_LmapTextureMode( char *string ) memcpy(prev_mode, string, strlen(string)); prev_mode[strlen(string)] = '\0'; - i += vk_aniso->value > 0 ? NUM_VK_MODES : 0; vk_current_lmap_sampler = i; vkDeviceWaitIdle(vk_device.logical); diff --git a/src/common/header/shared.h b/src/common/header/shared.h index e5643b18..74b5088a 100644 --- a/src/common/header/shared.h +++ b/src/common/header/shared.h @@ -90,6 +90,13 @@ typedef unsigned char byte; #define YAW 1 /* left / right */ #define ROLL 2 /* fall over */ +#ifndef min +#define min(a, b) (((a) < (b)) ? (a) : (b)) +#endif +#ifndef max +#define max(a, b) (((a) > (b)) ? (a) : (b)) +#endif + #define MAX_STRING_CHARS 2048 /* max length of a string passed to Cmd_TokenizeString */ #define MAX_STRING_TOKENS 80 /* max tokens resulting from Cmd_TokenizeString */ #define MAX_TOKEN_CHARS 1024 /* max length of an individual token */