From 05518c01fb578ae68e00c315996a339181ef68fc Mon Sep 17 00:00:00 2001 From: Samuliak Date: Tue, 28 Jan 2025 07:12:10 +0100 Subject: [PATCH] support max anisotropy overwrite --- .../HW/Latte/Renderer/Metal/MetalRenderer.cpp | 17 +++++++++++- .../Renderer/Metal/MetalSamplerCache.cpp | 26 ++++--------------- .../Latte/Renderer/Metal/MetalSamplerCache.h | 4 +-- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp index a3bf1e32..fb284b80 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp @@ -2068,7 +2068,22 @@ void MetalRenderer::BindStageResources(MTL::RenderCommandEncoder* renderCommandE MTL::SamplerState* sampler; if (stageSamplerIndex != LATTE_DECOMPILER_SAMPLER_NONE) { - sampler = m_samplerCache->GetSamplerState(LatteGPUState.contextNew, shader->shaderType, stageSamplerIndex); + uint32 samplerIndex = stageSamplerIndex + LatteDecompiler_getTextureSamplerBaseIndex(shader->shaderType); + _LatteRegisterSetSampler* samplerWords = LatteGPUState.contextNew.SQ_TEX_SAMPLER + samplerIndex; + + // Overwriting + + // Lod bias + //if (baseTexture->overwriteInfo.hasLodBias) + // samplerWords->WORD1.set_LOD_BIAS(baseTexture->overwriteInfo.lodBias); + //else if (baseTexture->overwriteInfo.hasRelativeLodBias) + // samplerWords->WORD1.set_LOD_BIAS(samplerWords->WORD1.get_LOD_BIAS() + baseTexture->overwriteInfo.relativeLodBias); + + // Max anisotropy + if (baseTexture->overwriteInfo.anisotropicLevel >= 0) + samplerWords->WORD0.set_MAX_ANISO_RATIO(baseTexture->overwriteInfo.anisotropicLevel); + + sampler = m_samplerCache->GetSamplerState(LatteGPUState.contextNew, shader->shaderType, stageSamplerIndex, samplerWords); } else { diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.cpp index 8dab80b6..3a1371a5 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.cpp @@ -86,32 +86,22 @@ MetalSamplerCache::~MetalSamplerCache() m_samplerCache.clear(); } -MTL::SamplerState* MetalSamplerCache::GetSamplerState(const LatteContextRegister& lcr, LatteConst::ShaderType shaderType, uint32 stageSamplerIndex) +MTL::SamplerState* MetalSamplerCache::GetSamplerState(const LatteContextRegister& lcr, LatteConst::ShaderType shaderType, uint32 stageSamplerIndex, const _LatteRegisterSetSampler* samplerWords) { - uint32 samplerIndex = stageSamplerIndex + LatteDecompiler_getTextureSamplerBaseIndex(shaderType); - - uint64 stateHash = CalculateSamplerHash(lcr, shaderType, stageSamplerIndex, samplerIndex); + uint64 stateHash = CalculateSamplerHash(lcr, shaderType, stageSamplerIndex, samplerWords); auto& samplerState = m_samplerCache[stateHash]; if (samplerState) return samplerState; // Sampler state - const _LatteRegisterSetSampler* samplerWords = lcr.SQ_TEX_SAMPLER + samplerIndex; + NS_STACK_SCOPED MTL::SamplerDescriptor* samplerDescriptor = MTL::SamplerDescriptor::alloc()->init(); // lod uint32 iMinLOD = samplerWords->WORD1.get_MIN_LOD(); uint32 iMaxLOD = samplerWords->WORD1.get_MAX_LOD(); - sint32 iLodBias = samplerWords->WORD1.get_LOD_BIAS(); - - // TODO: uncomment - // apply relative lod bias from graphic pack - //if (baseTexture->overwriteInfo.hasRelativeLodBias) - // iLodBias += baseTexture->overwriteInfo.relativeLodBias; - // apply absolute lod bias from graphic pack - //if (baseTexture->overwriteInfo.hasLodBias) - // iLodBias = baseTexture->overwriteInfo.lodBias; + //sint32 iLodBias = samplerWords->WORD1.get_LOD_BIAS(); auto filterMip = samplerWords->WORD0.get_MIP_FILTER(); if (filterMip == Latte::LATTE_SQ_TEX_SAMPLER_WORD0_0::E_Z_FILTER::NONE) @@ -160,10 +150,6 @@ MTL::SamplerState* MetalSamplerCache::GetSamplerState(const LatteContextRegister auto maxAniso = samplerWords->WORD0.get_MAX_ANISO_RATIO(); - // TODO: uncomment - //if (baseTexture->overwriteInfo.anisotropicLevel >= 0) - // maxAniso = baseTexture->overwriteInfo.anisotropicLevel; - if (maxAniso > 0) samplerDescriptor->setMaxAnisotropy(1 << maxAniso); @@ -184,10 +170,8 @@ MTL::SamplerState* MetalSamplerCache::GetSamplerState(const LatteContextRegister return samplerState; } -uint64 MetalSamplerCache::CalculateSamplerHash(const LatteContextRegister& lcr, LatteConst::ShaderType shaderType, uint32 stageSamplerIndex, uint32 samplerIndex) +uint64 MetalSamplerCache::CalculateSamplerHash(const LatteContextRegister& lcr, LatteConst::ShaderType shaderType, uint32 stageSamplerIndex, const _LatteRegisterSetSampler* samplerWords) { - const _LatteRegisterSetSampler* samplerWords = lcr.SQ_TEX_SAMPLER + samplerIndex; - uint64 hash = 0; hash = std::rotl(hash, 17); hash += (uint64)samplerWords->WORD0.getRawValue(); diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.h index 17857f0e..cbb02cf3 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.h @@ -11,12 +11,12 @@ public: MetalSamplerCache(class MetalRenderer* metalRenderer) : m_mtlr{metalRenderer} {} ~MetalSamplerCache(); - MTL::SamplerState* GetSamplerState(const LatteContextRegister& lcr, LatteConst::ShaderType shaderType, uint32 stageSamplerIndex); + MTL::SamplerState* GetSamplerState(const LatteContextRegister& lcr, LatteConst::ShaderType shaderType, uint32 stageSamplerIndex, const _LatteRegisterSetSampler* samplerWords); private: class MetalRenderer* m_mtlr; std::map m_samplerCache; - uint64 CalculateSamplerHash(const LatteContextRegister& lcr, LatteConst::ShaderType shaderType, uint32 stageSamplerIndex, uint32 samplerIndex); + uint64 CalculateSamplerHash(const LatteContextRegister& lcr, LatteConst::ShaderType shaderType, uint32 stageSamplerIndex, const _LatteRegisterSetSampler* samplerWords); };