Skip to content

Commit

Permalink
support max anisotropy overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
SamoZ256 committed Jan 28, 2025
1 parent 2f9ef59 commit 05518c0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
17 changes: 16 additions & 1 deletion src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
26 changes: 5 additions & 21 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand All @@ -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<uint64>(hash, 17);
hash += (uint64)samplerWords->WORD0.getRawValue();
Expand Down
4 changes: 2 additions & 2 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalSamplerCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class MetalSamplerCache
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<uint64, MTL::SamplerState*> 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);
};

0 comments on commit 05518c0

Please sign in to comment.