From 26a2e488acbf1f770d5b16156f246a1bb30b04bb Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Fri, 30 Jul 2021 12:14:54 -0700 Subject: [PATCH] Enable anisotropic filtering when the game requests it --- Source/Core/VideoBackends/D3D/D3DState.cpp | 4 ++-- .../Core/VideoBackends/D3D12/DescriptorHeapManager.cpp | 4 ++-- Source/Core/VideoBackends/OGL/SamplerCache.cpp | 4 ++-- Source/Core/VideoBackends/Vulkan/ObjectCache.cpp | 4 ++-- Source/Core/VideoCommon/RenderState.cpp | 9 +++++++-- Source/Core/VideoCommon/RenderState.h | 2 +- Source/Core/VideoCommon/TextureCacheBase.cpp | 9 +-------- 7 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/D3DState.cpp b/Source/Core/VideoBackends/D3D/D3DState.cpp index 7beec54611..acfa1bc2e7 100644 --- a/Source/Core/VideoBackends/D3D/D3DState.cpp +++ b/Source/Core/VideoBackends/D3D/D3DState.cpp @@ -336,10 +336,10 @@ ID3D11SamplerState* StateCache::Get(SamplerState state) sampdc.MinLOD = state.tm1.min_lod / 16.f; sampdc.MipLODBias = state.tm0.lod_bias / 256.f; - if (state.tm0.anisotropic_filtering) + if (state.tm0.anisotropic_filtering != 0) { sampdc.Filter = D3D11_FILTER_ANISOTROPIC; - sampdc.MaxAnisotropy = 1u << g_ActiveConfig.iMaxAnisotropy; + sampdc.MaxAnisotropy = 1u << state.tm0.anisotropic_filtering; } ComPtr res; diff --git a/Source/Core/VideoBackends/D3D12/DescriptorHeapManager.cpp b/Source/Core/VideoBackends/D3D12/DescriptorHeapManager.cpp index 9827057626..a8fb30e210 100644 --- a/Source/Core/VideoBackends/D3D12/DescriptorHeapManager.cpp +++ b/Source/Core/VideoBackends/D3D12/DescriptorHeapManager.cpp @@ -128,10 +128,10 @@ static void GetD3DSamplerDesc(D3D12_SAMPLER_DESC* desc, const SamplerState& stat desc->MipLODBias = static_cast(state.tm0.lod_bias) / 256.f; desc->ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER; - if (state.tm0.anisotropic_filtering) + if (state.tm0.anisotropic_filtering != 0) { desc->Filter = D3D12_FILTER_ANISOTROPIC; - desc->MaxAnisotropy = 1u << g_ActiveConfig.iMaxAnisotropy; + desc->MaxAnisotropy = 1u << state.tm0.anisotropic_filtering; } } diff --git a/Source/Core/VideoBackends/OGL/SamplerCache.cpp b/Source/Core/VideoBackends/OGL/SamplerCache.cpp index 2673fd2ddd..0c344f4719 100644 --- a/Source/Core/VideoBackends/OGL/SamplerCache.cpp +++ b/Source/Core/VideoBackends/OGL/SamplerCache.cpp @@ -102,10 +102,10 @@ void SamplerCache::SetParameters(GLuint sampler_id, const SamplerState& params) glSamplerParameterf(sampler_id, GL_TEXTURE_LOD_BIAS, params.tm0.lod_bias / 256.f); } - if (params.tm0.anisotropic_filtering && g_ogl_config.bSupportsAniso) + if (params.tm0.anisotropic_filtering != 0 && g_ogl_config.bSupportsAniso) { glSamplerParameterf(sampler_id, GL_TEXTURE_MAX_ANISOTROPY_EXT, - static_cast(1 << g_ActiveConfig.iMaxAnisotropy)); + static_cast(1 << params.tm0.anisotropic_filtering)); } } diff --git a/Source/Core/VideoBackends/Vulkan/ObjectCache.cpp b/Source/Core/VideoBackends/Vulkan/ObjectCache.cpp index 68683790e7..f1c8e0e9b5 100644 --- a/Source/Core/VideoBackends/Vulkan/ObjectCache.cpp +++ b/Source/Core/VideoBackends/Vulkan/ObjectCache.cpp @@ -369,11 +369,11 @@ VkSampler ObjectCache::GetSampler(const SamplerState& info) }; // Can we use anisotropic filtering with this sampler? - if (info.tm0.anisotropic_filtering && g_vulkan_context->SupportsAnisotropicFiltering()) + if (info.tm0.anisotropic_filtering != 0 && g_vulkan_context->SupportsAnisotropicFiltering()) { // Cap anisotropy to device limits. create_info.anisotropyEnable = VK_TRUE; - create_info.maxAnisotropy = std::min(static_cast(1 << g_ActiveConfig.iMaxAnisotropy), + create_info.maxAnisotropy = std::min(static_cast(1 << info.tm0.anisotropic_filtering), g_vulkan_context->GetMaxSamplerAnisotropy()); } diff --git a/Source/Core/VideoCommon/RenderState.cpp b/Source/Core/VideoCommon/RenderState.cpp index 0860488b8a..6df108ebf2 100644 --- a/Source/Core/VideoCommon/RenderState.cpp +++ b/Source/Core/VideoCommon/RenderState.cpp @@ -311,10 +311,15 @@ void SamplerState::Generate(const BPMemory& bp, u32 index) }; tm0.wrap_u = filter_invalid_wrap(bp_tm0.wrap_s); tm0.wrap_v = filter_invalid_wrap(bp_tm0.wrap_t); + if (bp_tm0.max_aniso == MaxAniso::Two) + tm0.anisotropic_filtering = 1; + else if (bp_tm0.max_aniso == MaxAniso::Four) + tm0.anisotropic_filtering = 2; + else + tm0.anisotropic_filtering = 0; tm0.diag_lod = bp_tm0.diag_lod; - tm0.anisotropic_filtering = false; // TODO: Respect BP anisotropic filtering mode - tm0.lod_clamp = bp_tm0.lod_clamp; // TODO: What does this do? + tm0.lod_clamp = bp_tm0.lod_clamp; // TODO: What does this do? } namespace RenderState diff --git a/Source/Core/VideoCommon/RenderState.h b/Source/Core/VideoCommon/RenderState.h index b52a6d2851..b389db1f80 100644 --- a/Source/Core/VideoCommon/RenderState.h +++ b/Source/Core/VideoCommon/RenderState.h @@ -199,7 +199,7 @@ struct SamplerState BitField<7, 1, LODType> diag_lod; BitField<8, 16, s32> lod_bias; // multiplied by 256, higher precision than normal BitField<24, 1, bool, u32> lod_clamp; // TODO: This isn't currently implemented - BitField<25, 1, bool, u32> anisotropic_filtering; // TODO: This doesn't use the BP one yet + BitField<25, 4, u32> anisotropic_filtering; u32 hex = 0; }; union TM1 diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp index 6d138d5912..4e3d3014d1 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -1078,11 +1078,7 @@ static void SetSamplerState(u32 index, float custom_tex_scale, bool custom_tex, state.tm0.mag_filter = FilterMode::Linear; if (tm0.mipmap_filter != MipMode::None) state.tm0.mipmap_filter = FilterMode::Linear; - state.tm0.anisotropic_filtering = true; - } - else - { - state.tm0.anisotropic_filtering = false; + state.tm0.anisotropic_filtering = g_ActiveConfig.iMaxAnisotropy; } if (has_arbitrary_mips && tm0.mipmap_filter != MipMode::None) @@ -1093,9 +1089,6 @@ static void SetSamplerState(u32 index, float custom_tex_scale, bool custom_tex, // Correct this with the upscaling factor of custom textures. s32 lod_offset = std::log2(g_framebuffer_manager->GetEFBScale() / custom_tex_scale) * 256.f; state.tm0.lod_bias = std::clamp(state.tm0.lod_bias + lod_offset, -32768, 32767); - - // Anisotropic also pushes mips farther away so it cannot be used either - state.tm0.anisotropic_filtering = false; } g_gfx->SetSamplerState(index, state);