Skip to content

Commit f11526a

Browse files
committed
fix: texture bindings
1 parent 5030a2e commit f11526a

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
21932193
src->add(".");
21942194
const char* resultElemTable[4] = {"x","y","z","w"};
21952195
sint32 numWrittenElements = 0;
2196-
for(sint32 f=0; f<4; f++)
2196+
for(sint32 f = 0; f < 4; f++)
21972197
{
21982198
if( texInstruction->dstSel[f] < 4 )
21992199
{
@@ -2493,18 +2493,21 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
24932493
// lod bias
24942494
if( texOpcode == GPU7_TEX_INST_SAMPLE_C || texOpcode == GPU7_TEX_INST_SAMPLE_C_LZ )
24952495
{
2496-
src->add(")");
2496+
src->add(").");
24972497

24982498
if (numWrittenElements > 1)
24992499
{
25002500
// result is copied into multiple channels
2501-
src->add(".");
25022501
for (sint32 f = 0; f < numWrittenElements; f++)
25032502
{
25042503
cemu_assert_debug(texInstruction->dstSel[f] == 0); // only x component is defined
25052504
src->add("x");
25062505
}
25072506
}
2507+
else
2508+
{
2509+
src->add("x");
2510+
}
25082511
}
25092512
else
25102513
{

src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSLHeader.hpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace LatteDecompiler
66
{
77
auto src = decompilerContext->shaderSource;
88

9-
LatteDecompilerShaderResourceMapping& resourceMapping = decompilerContext->output->resourceMappingGL;
109
auto& uniformOffsets = decompilerContext->output->uniformOffsetsVK;
1110

1211
src->add("struct SupportBuffer {" _CRLF);
@@ -129,11 +128,8 @@ namespace LatteDecompiler
129128
if (!decompilerContext->analyzer.uniformBufferAccessTracker[i].HasAccess())
130129
continue;
131130

132-
cemu_assert_debug(decompilerContext->output->resourceMappingGL.uniformBuffersBindingPoint[i] >= 0);
133131
cemu_assert_debug(decompilerContext->output->resourceMappingVK.uniformBuffersBindingPoint[i] >= 0);
134132

135-
//shaderSrc->addFmt("UNIFORM_BUFFER_LAYOUT({}, {}, {}) ", (sint32)decompilerContext->output->resourceMappingGL.uniformBuffersBindingPoint[i], (sint32)decompilerContext->output->resourceMappingVK.setIndex, (sint32)decompilerContext->output->resourceMappingVK.uniformBuffersBindingPoint[i]);
136-
137133
shaderSrc->addFmt("struct UBuff{} {{" _CRLF, i);
138134
shaderSrc->addFmt("float4 d[{}];" _CRLF, decompilerContext->analyzer.uniformBufferAccessTracker[i].DetermineSize(decompilerContext->shaderBaseHash, LATTE_GLSL_DYNAMIC_UNIFORM_BLOCK_SIZE));
139135
shaderSrc->add("};" _CRLF _CRLF);
@@ -169,9 +165,7 @@ namespace LatteDecompiler
169165
{
170166
if (decompilerContext->analyzer.inputAttributSemanticMask[i])
171167
{
172-
cemu_assert_debug(decompilerContext->output->resourceMappingGL.attributeMapping[i] >= 0);
173168
cemu_assert_debug(decompilerContext->output->resourceMappingVK.attributeMapping[i] >= 0);
174-
cemu_assert_debug(decompilerContext->output->resourceMappingGL.attributeMapping[i] == decompilerContext->output->resourceMappingVK.attributeMapping[i]);
175169

176170
src->addFmt("uint4 attrDataSem{} [[attribute({})]];" _CRLF, i, (sint32)decompilerContext->output->resourceMappingVK.attributeMapping[i]);
177171
}
@@ -304,10 +298,9 @@ namespace LatteDecompiler
304298
if (!decompilerContext->analyzer.uniformBufferAccessTracker[i].HasAccess())
305299
continue;
306300

307-
cemu_assert_debug(decompilerContext->output->resourceMappingGL.uniformBuffersBindingPoint[i] >= 0);
308301
cemu_assert_debug(decompilerContext->output->resourceMappingVK.uniformBuffersBindingPoint[i] >= 0);
309302

310-
src->addFmt(", constant UBuff{}& ubuff{} [[buffer({})]]", i, i, (sint32)decompilerContext->output->resourceMappingGL.uniformBuffersBindingPoint[i]);
303+
src->addFmt(", constant UBuff{}& ubuff{} [[buffer({})]]", i, i, (sint32)decompilerContext->output->resourceMappingVK.uniformBuffersBindingPoint[i]);
311304
}
312305
}
313306
}
@@ -348,11 +341,11 @@ namespace LatteDecompiler
348341
cemu_assert_unimplemented();
349342
}
350343

351-
// HACK
352-
uint32 textureBinding = shaderContext->output->resourceMappingGL.textureUnitToBindingPoint[i] % 31;
353-
uint32 samplerBinding = textureBinding % 16;
354-
src->addFmt(" tex{} [[texture({})]]", i, textureBinding);
355-
src->addFmt(", sampler samplr{} [[sampler({})]]", i, samplerBinding);
344+
uint32 binding = shaderContext->output->resourceMappingVK.textureUnitToBindingPoint[i];
345+
//uint32 textureBinding = shaderContext->output->resourceMappingVK.textureUnitToBindingPoint[i] % 31;
346+
//uint32 samplerBinding = textureBinding % 16;
347+
src->addFmt(" tex{} [[texture({})]]", i, binding);
348+
src->addFmt(", sampler samplr{} [[sampler({})]]", i, binding);
356349
}
357350
}
358351

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ void MetalRenderer::Shutdown()
107107
CommitCommandBuffer();
108108
}
109109

110+
// TODO: what should this do?
110111
bool MetalRenderer::IsPadWindowActive()
111112
{
112-
debug_printf("MetalRenderer::IsPadWindowActive not implemented\n");
113+
//debug_printf("MetalRenderer::IsPadWindowActive not implemented\n");
113114

114115
return false;
115116
}
@@ -805,6 +806,23 @@ void MetalRenderer::BindStageResources(MTL::RenderCommandEncoder* renderCommandE
805806
auto hostTextureUnit = relative_textureUnit;
806807
auto textureDim = shader->textureUnitDim[relative_textureUnit];
807808
auto texUnitRegIndex = hostTextureUnit * 7;
809+
switch (shader->shaderType)
810+
{
811+
case LatteConst::ShaderType::Vertex:
812+
hostTextureUnit += LATTE_CEMU_VS_TEX_UNIT_BASE;
813+
texUnitRegIndex += Latte::REGADDR::SQ_TEX_RESOURCE_WORD0_N_VS;
814+
break;
815+
case LatteConst::ShaderType::Pixel:
816+
hostTextureUnit += LATTE_CEMU_PS_TEX_UNIT_BASE;
817+
texUnitRegIndex += Latte::REGADDR::SQ_TEX_RESOURCE_WORD0_N_PS;
818+
break;
819+
case LatteConst::ShaderType::Geometry:
820+
hostTextureUnit += LATTE_CEMU_GS_TEX_UNIT_BASE;
821+
texUnitRegIndex += Latte::REGADDR::SQ_TEX_RESOURCE_WORD0_N_GS;
822+
break;
823+
default:
824+
UNREACHABLE;
825+
}
808826

809827
auto textureView = m_state.textures[hostTextureUnit];
810828
if (!textureView)
@@ -821,9 +839,15 @@ void MetalRenderer::BindStageResources(MTL::RenderCommandEncoder* renderCommandE
821839
//auto imageViewObj = textureView->GetSamplerView(word4);
822840
//info.imageView = imageViewObj->m_textureImageView;
823841

824-
// HACK
825-
uint32 textureBinding = (shader->resourceMapping.getTextureBaseBindingPoint() + i) % MAX_MTL_TEXTURES;
826-
uint32 samplerBinding = textureBinding % MAX_MTL_SAMPLERS;
842+
// TODO: uncomment
843+
uint32 binding = shader->resourceMapping.getTextureBaseBindingPoint() + i;//shader->resourceMapping.textureUnitToBindingPoint[hostTextureUnit];
844+
//uint32 textureBinding = binding % MAX_MTL_TEXTURES;
845+
//uint32 samplerBinding = binding % MAX_MTL_SAMPLERS;
846+
if (binding >= MAX_MTL_TEXTURES)
847+
{
848+
debug_printf("invalid texture binding %u\n", binding);
849+
continue;
850+
}
827851

828852
uint32 stageSamplerIndex = shader->textureUnitSamplerAssignment[relative_textureUnit];
829853
if (stageSamplerIndex != LATTE_DECOMPILER_SAMPLER_NONE)
@@ -934,12 +958,12 @@ void MetalRenderer::BindStageResources(MTL::RenderCommandEncoder* renderCommandE
934958
{
935959
case LatteConst::ShaderType::Vertex:
936960
{
937-
renderCommandEncoder->setVertexSamplerState(sampler, samplerBinding);
961+
renderCommandEncoder->setVertexSamplerState(sampler, binding);
938962
break;
939963
}
940964
case LatteConst::ShaderType::Pixel:
941965
{
942-
renderCommandEncoder->setFragmentSamplerState(sampler, samplerBinding);
966+
renderCommandEncoder->setFragmentSamplerState(sampler, binding);
943967
break;
944968
}
945969
default:
@@ -952,12 +976,12 @@ void MetalRenderer::BindStageResources(MTL::RenderCommandEncoder* renderCommandE
952976
{
953977
case LatteConst::ShaderType::Vertex:
954978
{
955-
renderCommandEncoder->setVertexTexture(textureView->GetTexture(), textureBinding);
979+
renderCommandEncoder->setVertexTexture(textureView->GetTexture(), binding);
956980
break;
957981
}
958982
case LatteConst::ShaderType::Pixel:
959983
{
960-
renderCommandEncoder->setFragmentTexture(textureView->GetTexture(), textureBinding);
984+
renderCommandEncoder->setFragmentTexture(textureView->GetTexture(), binding);
961985
break;
962986
}
963987
default:

0 commit comments

Comments
 (0)