Skip to content

Commit 5fc4540

Browse files
committed
fix: texture swizzle
1 parent 0a7f30c commit 5fc4540

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,7 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
23342334
// shadow sampler
23352335
if (texDim == Latte::E_DIM::DIM_2D_ARRAY)
23362336
{
2337-
// 3 coords + compare value (as float4)
2337+
// 3 coords + compare value
23382338
src->add("float3(");
23392339
_emitTEXSampleCoordInputComponent(shaderContext, texInstruction, 0, LATTE_DECOMPILER_DTYPE_FLOAT);
23402340
src->add(", ");
@@ -2442,7 +2442,8 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
24422442
}
24432443
else if( texOpcode == GPU7_TEX_INST_SAMPLE_LZ || texOpcode == GPU7_TEX_INST_SAMPLE_C_LZ )
24442444
{
2445-
src->add(",0.0");
2445+
// TODO: correct?
2446+
src->add(", level(0.0)");
24462447
}
24472448
}
24482449
// gradient parameters

src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,42 @@ MTL::Texture* LatteTextureViewMtl::GetSwizzledView(uint32 gpuSamplerSwizzle)
2727
// Mask out
2828
gpuSamplerSwizzle &= 0x0FFF0000;
2929

30+
// RGBA swizzle == no swizzle
3031
if (gpuSamplerSwizzle == RGBA_SWIZZLE)
3132
{
3233
return m_baseTexture->GetTexture();
3334
}
34-
else
35-
{
36-
// First, try to find a view in the cache
3735

38-
// Fast cache
39-
sint32 freeIndex = -1;
40-
for (sint32 i = 0; i < std::size(m_viewCache); i++)
36+
// First, try to find a view in the cache
37+
38+
// Fast cache
39+
sint32 freeIndex = -1;
40+
for (sint32 i = 0; i < std::size(m_viewCache); i++)
41+
{
42+
if (m_viewCache[i].key == gpuSamplerSwizzle)
4143
{
42-
if (m_viewCache[i].key == gpuSamplerSwizzle)
43-
{
44-
return m_viewCache[i].texture;
45-
}
46-
else if (m_viewCache[i].key == INVALID_SWIZZLE && freeIndex == -1)
47-
{
48-
freeIndex = i;
49-
}
44+
return m_viewCache[i].texture;
5045
}
51-
52-
// Fallback cache
53-
auto it = m_fallbackViewCache.find(gpuSamplerSwizzle);
54-
if (it != m_fallbackViewCache.end())
46+
else if (m_viewCache[i].key == INVALID_SWIZZLE && freeIndex == -1)
5547
{
56-
return it->second;
48+
freeIndex = i;
5749
}
50+
}
5851

59-
MTL::Texture* texture = CreateSwizzledView(gpuSamplerSwizzle);
60-
if (freeIndex != -1)
61-
m_viewCache[freeIndex] = {gpuSamplerSwizzle, texture};
62-
else
63-
it->second = texture;
64-
65-
return texture;
52+
// Fallback cache
53+
auto it = m_fallbackViewCache.find(gpuSamplerSwizzle);
54+
if (it != m_fallbackViewCache.end())
55+
{
56+
return it->second;
6657
}
58+
59+
MTL::Texture* texture = CreateSwizzledView(gpuSamplerSwizzle);
60+
if (freeIndex != -1)
61+
m_viewCache[freeIndex] = {gpuSamplerSwizzle, texture};
62+
else
63+
it->second = texture;
64+
65+
return texture;
6766
}
6867

6968
MTL::Texture* LatteTextureViewMtl::CreateSwizzledView(uint32 gpuSamplerSwizzle)
@@ -117,10 +116,14 @@ MTL::Texture* LatteTextureViewMtl::CreateSwizzledView(uint32 gpuSamplerSwizzle)
117116
layerCount = this->numSlice;
118117
}
119118

120-
// TODO: swizzle
119+
MTL::TextureSwizzleChannels swizzle;
120+
swizzle.red = GetMtlTextureSwizzle(compSelR);
121+
swizzle.green = GetMtlTextureSwizzle(compSelG);
122+
swizzle.blue = GetMtlTextureSwizzle(compSelB);
123+
swizzle.alpha = GetMtlTextureSwizzle(compSelA);
121124

122125
auto formatInfo = GetMtlPixelFormatInfo(format, m_baseTexture->IsDepth());
123-
MTL::Texture* texture = m_baseTexture->GetTexture()->newTextureView(formatInfo.pixelFormat, textureType, NS::Range::Make(baseLevel, levelCount), NS::Range::Make(baseLayer, layerCount));
126+
MTL::Texture* texture = m_baseTexture->GetTexture()->newTextureView(formatInfo.pixelFormat, textureType, NS::Range::Make(baseLevel, levelCount), NS::Range::Make(baseLayer, layerCount), swizzle);
124127

125128
return texture;
126129
}

src/Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#define RGBA_SWIZZLE 0x06880000
99
#define INVALID_SWIZZLE 0xFFFFFFFF
1010

11-
// TODO: test the swizzle
1211
class LatteTextureViewMtl : public LatteTextureView
1312
{
1413
public:

0 commit comments

Comments
 (0)