From 5fc45407db8f5f159fe231e6033cf03fd76a9ccb Mon Sep 17 00:00:00 2001 From: Samuliak Date: Tue, 6 Aug 2024 13:42:06 +0200 Subject: [PATCH] fix: texture swizzle --- .../LatteDecompilerEmitMSL.cpp | 5 +- .../Renderer/Metal/LatteTextureViewMtl.cpp | 61 ++++++++++--------- .../Renderer/Metal/LatteTextureViewMtl.h | 1 - 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp index 91558c3b..e743f31c 100644 --- a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp +++ b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp @@ -2334,7 +2334,7 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex // shadow sampler if (texDim == Latte::E_DIM::DIM_2D_ARRAY) { - // 3 coords + compare value (as float4) + // 3 coords + compare value src->add("float3("); _emitTEXSampleCoordInputComponent(shaderContext, texInstruction, 0, LATTE_DECOMPILER_DTYPE_FLOAT); src->add(", "); @@ -2442,7 +2442,8 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex } else if( texOpcode == GPU7_TEX_INST_SAMPLE_LZ || texOpcode == GPU7_TEX_INST_SAMPLE_C_LZ ) { - src->add(",0.0"); + // TODO: correct? + src->add(", level(0.0)"); } } // gradient parameters diff --git a/src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.cpp b/src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.cpp index ce2fec62..b9d00119 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.cpp @@ -27,43 +27,42 @@ MTL::Texture* LatteTextureViewMtl::GetSwizzledView(uint32 gpuSamplerSwizzle) // Mask out gpuSamplerSwizzle &= 0x0FFF0000; + // RGBA swizzle == no swizzle if (gpuSamplerSwizzle == RGBA_SWIZZLE) { return m_baseTexture->GetTexture(); } - else + + // First, try to find a view in the cache + + // Fast cache + sint32 freeIndex = -1; + for (sint32 i = 0; i < std::size(m_viewCache); i++) { - // First, try to find a view in the cache - - // Fast cache - sint32 freeIndex = -1; - for (sint32 i = 0; i < std::size(m_viewCache); i++) + if (m_viewCache[i].key == gpuSamplerSwizzle) { - if (m_viewCache[i].key == gpuSamplerSwizzle) - { - return m_viewCache[i].texture; - } - else if (m_viewCache[i].key == INVALID_SWIZZLE && freeIndex == -1) - { - freeIndex = i; - } + return m_viewCache[i].texture; } - - // Fallback cache - auto it = m_fallbackViewCache.find(gpuSamplerSwizzle); - if (it != m_fallbackViewCache.end()) + else if (m_viewCache[i].key == INVALID_SWIZZLE && freeIndex == -1) { - return it->second; + freeIndex = i; } - - MTL::Texture* texture = CreateSwizzledView(gpuSamplerSwizzle); - if (freeIndex != -1) - m_viewCache[freeIndex] = {gpuSamplerSwizzle, texture}; - else - it->second = texture; - - return texture; } + + // Fallback cache + auto it = m_fallbackViewCache.find(gpuSamplerSwizzle); + if (it != m_fallbackViewCache.end()) + { + return it->second; + } + + MTL::Texture* texture = CreateSwizzledView(gpuSamplerSwizzle); + if (freeIndex != -1) + m_viewCache[freeIndex] = {gpuSamplerSwizzle, texture}; + else + it->second = texture; + + return texture; } MTL::Texture* LatteTextureViewMtl::CreateSwizzledView(uint32 gpuSamplerSwizzle) @@ -117,10 +116,14 @@ MTL::Texture* LatteTextureViewMtl::CreateSwizzledView(uint32 gpuSamplerSwizzle) layerCount = this->numSlice; } - // TODO: swizzle + MTL::TextureSwizzleChannels swizzle; + swizzle.red = GetMtlTextureSwizzle(compSelR); + swizzle.green = GetMtlTextureSwizzle(compSelG); + swizzle.blue = GetMtlTextureSwizzle(compSelB); + swizzle.alpha = GetMtlTextureSwizzle(compSelA); auto formatInfo = GetMtlPixelFormatInfo(format, m_baseTexture->IsDepth()); - MTL::Texture* texture = m_baseTexture->GetTexture()->newTextureView(formatInfo.pixelFormat, textureType, NS::Range::Make(baseLevel, levelCount), NS::Range::Make(baseLayer, layerCount)); + MTL::Texture* texture = m_baseTexture->GetTexture()->newTextureView(formatInfo.pixelFormat, textureType, NS::Range::Make(baseLevel, levelCount), NS::Range::Make(baseLayer, layerCount), swizzle); return texture; } diff --git a/src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.h b/src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.h index eb224180..7a5a9dfa 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.h @@ -8,7 +8,6 @@ #define RGBA_SWIZZLE 0x06880000 #define INVALID_SWIZZLE 0xFFFFFFFF -// TODO: test the swizzle class LatteTextureViewMtl : public LatteTextureView { public: