Skip to content

Commit a38ddb5

Browse files
committed
fix: shadows
1 parent 5c246d5 commit a38ddb5

File tree

8 files changed

+91
-74
lines changed

8 files changed

+91
-74
lines changed

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

Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,7 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
21872187
}
21882188

21892189
auto texDim = shaderContext->shader->textureUnitDim[texInstruction->textureFetch.textureIndex];
2190+
bool isCompare = shaderContext->shader->textureUsesDepthCompare[texInstruction->textureFetch.textureIndex];
21902191

21912192
char tempBuffer0[32];
21922193
char tempBuffer1[32];
@@ -2212,6 +2213,7 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
22122213
}
22132214
// texture sampler opcode
22142215
uint32 texOpcode = texInstruction->opcode;
2216+
// TODO: is this needed?
22152217
if (shaderContext->shaderType == LatteConst::ShaderType::Vertex)
22162218
{
22172219
// vertex shader forces LOD to zero, but certain sampler types don't support textureLod(...) API
@@ -2275,7 +2277,10 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
22752277
}
22762278
else
22772279
{
2278-
src->addFmt("sample(samplr{}, ", texInstruction->textureFetch.textureIndex);
2280+
src->add("sample");
2281+
if (isCompare)
2282+
src->add("_compare");
2283+
src->addFmt("(samplr{}, ", texInstruction->textureFetch.textureIndex);
22792284
}
22802285

22812286
// for textureGather() add shift (todo: depends on rounding mode set in sampler registers?)
@@ -2493,61 +2498,68 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
24932498
src->addFmt(",int3({},{},{})", texInstruction->textureFetch.offsetX/2, texInstruction->textureFetch.offsetY/2, texInstruction->textureFetch.offsetZ/2);
24942499
}
24952500
}
2496-
// lod bias
2497-
if( texOpcode == GPU7_TEX_INST_SAMPLE_C || texOpcode == GPU7_TEX_INST_SAMPLE_C_LZ )
2498-
{
2499-
src->add(").");
25002501

2501-
if (numWrittenElements > 1)
2502-
{
2503-
// result is copied into multiple channels
2504-
for (sint32 f = 0; f < numWrittenElements; f++)
2505-
{
2506-
cemu_assert_debug(texInstruction->dstSel[f] == 0); // only x component is defined
2507-
src->add("x");
2508-
}
2509-
}
2510-
else
2511-
{
2512-
src->add("x");
2513-
}
2514-
}
2515-
else
2516-
{
2517-
src->add(").");
2518-
for (sint32 f = 0; f < 4; f++)
2519-
{
2520-
if( texInstruction->dstSel[f] < 4 )
2521-
{
2522-
uint8 elemIndex = texInstruction->dstSel[f];
2523-
if (texOpcode == GPU7_TEX_INST_FETCH4)
2524-
{
2525-
// 's textureGather() and GPU7's FETCH4 instruction have a different order of elements
2526-
// xyzw: top-left, top-right, bottom-right, bottom-left
2527-
// textureGather xyzw
2528-
// fetch4 yzxw
2529-
// translate index from fetch4 to textureGather order
2530-
static uint8 fetchToGather[4] =
2531-
{
2532-
2, // x -> z
2533-
0, // y -> x
2534-
1, // z -> y
2535-
3, // w -> w
2536-
};
2537-
elemIndex = fetchToGather[elemIndex];
2538-
}
2539-
src->add(resultElemTable[elemIndex]);
2540-
numWrittenElements++;
2541-
}
2542-
else if( texInstruction->dstSel[f] == 7 )
2543-
{
2544-
// masked and not written
2545-
}
2546-
else
2547-
{
2548-
cemu_assert_unimplemented();
2549-
}
2550-
}
2502+
// lod bias (TODO: wht?)
2503+
2504+
src->add(")");
2505+
// sample_compare doesn't return a float
2506+
if (!isCompare)
2507+
{
2508+
if( texOpcode == GPU7_TEX_INST_SAMPLE_C || texOpcode == GPU7_TEX_INST_SAMPLE_C_LZ )
2509+
{
2510+
src->add(".");
2511+
2512+
if (numWrittenElements > 1)
2513+
{
2514+
// result is copied into multiple channels
2515+
for (sint32 f = 0; f < numWrittenElements; f++)
2516+
{
2517+
cemu_assert_debug(texInstruction->dstSel[f] == 0); // only x component is defined
2518+
src->add("x");
2519+
}
2520+
}
2521+
else
2522+
{
2523+
src->add("x");
2524+
}
2525+
}
2526+
else
2527+
{
2528+
src->add(".");
2529+
for (sint32 f = 0; f < 4; f++)
2530+
{
2531+
if( texInstruction->dstSel[f] < 4 )
2532+
{
2533+
uint8 elemIndex = texInstruction->dstSel[f];
2534+
if (texOpcode == GPU7_TEX_INST_FETCH4)
2535+
{
2536+
// 's textureGather() and GPU7's FETCH4 instruction have a different order of elements
2537+
// xyzw: top-left, top-right, bottom-right, bottom-left
2538+
// textureGather xyzw
2539+
// fetch4 yzxw
2540+
// translate index from fetch4 to textureGather order
2541+
static uint8 fetchToGather[4] =
2542+
{
2543+
2, // x -> z
2544+
0, // y -> x
2545+
1, // z -> y
2546+
3, // w -> w
2547+
};
2548+
elemIndex = fetchToGather[elemIndex];
2549+
}
2550+
src->add(resultElemTable[elemIndex]);
2551+
numWrittenElements++;
2552+
}
2553+
else if( texInstruction->dstSel[f] == 7 )
2554+
{
2555+
// masked and not written
2556+
}
2557+
else
2558+
{
2559+
cemu_assert_unimplemented();
2560+
}
2561+
}
2562+
}
25512563
}
25522564
src->add(");");
25532565

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ namespace LatteDecompiler
224224
src->add("#define GET_FRAGCOORD() vec4(in.position.xy * supportBuffer.fragCoordScale.xy, in.position.z, 1.0 / in.position.w)" _CRLF);
225225

226226
src->add("struct FragmentIn {" _CRLF);
227+
src->add("float4 position [[position]];" _CRLF);
227228

228229
LatteShaderPSInputTable* psInputTable = LatteSHRC_GetPSInputTable();
229230
for (sint32 i = 0; i < psInputTable->count; i++)
@@ -271,7 +272,7 @@ namespace LatteDecompiler
271272
// generate depth output for pixel shader
272273
if (decompilerContext->shader->depthWritten)
273274
{
274-
src->add("float passDepth [[depth]];" _CRLF);
275+
src->add("float passDepth [[depth(any)]];" _CRLF);
275276
}
276277

277278
src->add("};" _CRLF _CRLF);
@@ -323,26 +324,31 @@ namespace LatteDecompiler
323324

324325
src->add(", ");
325326

327+
if (shaderContext->shader->textureUsesDepthCompare[i])
328+
src->add("depth");
329+
else
330+
src->add("texture");
331+
326332
if (shaderContext->shader->textureIsIntegerFormat[i])
327333
{
328334
// integer samplers
329335
if (shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_1D)
330-
src->add("texture1d<uint>");
336+
src->add("1d<uint>");
331337
else if (shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_2D || shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_2D_MSAA)
332-
src->add("texture2d<uint>");
338+
src->add("2d<uint>");
333339
else
334340
cemu_assert_unimplemented();
335341
}
336342
else if (shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_2D || shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_2D_MSAA)
337-
src->add("texture2d<float>");
343+
src->add("2d<float>");
338344
else if (shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_1D)
339-
src->add("texture1d<float>");
345+
src->add("1d<float>");
340346
else if (shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_2D_ARRAY)
341-
src->add("texture2d_array<float>");
347+
src->add("2d_array<float>");
342348
else if (shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_CUBEMAP)
343-
src->add("texturecube_array<float>");
349+
src->add("cube_array<float>");
344350
else if (shaderContext->shader->textureUnitDim[i] == Latte::E_DIM::DIM_3D)
345-
src->add("texture3d<float>");
351+
src->add("3d<float>");
346352
else
347353
{
348354
cemu_assert_unimplemented();

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ MTL::Texture* LatteTextureViewMtl::GetSwizzledView(uint32 gpuSamplerSwizzle)
3939
sint32 freeIndex = -1;
4040
for (sint32 i = 0; i < std::size(m_viewCache); i++)
4141
{
42-
if (m_viewCache[i].key == gpuSamplerSwizzle)
42+
const auto& entry = m_viewCache[i];
43+
if (entry.key == gpuSamplerSwizzle)
4344
{
44-
return m_viewCache[i].texture;
45+
return entry.texture;
4546
}
46-
else if (m_viewCache[i].key == INVALID_SWIZZLE && freeIndex == -1)
47+
else if (entry.key == INVALID_SWIZZLE && freeIndex == -1)
4748
{
4849
freeIndex = i;
4950
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class LatteTextureViewMtl : public LatteTextureView
2929
struct {
3030
uint32 key;
3131
MTL::Texture* texture;
32-
} m_viewCache[4] = {{INVALID_SWIZZLE, nullptr}};
32+
} m_viewCache[4] = {{INVALID_SWIZZLE, nullptr}, {INVALID_SWIZZLE, nullptr}, {INVALID_SWIZZLE, nullptr}, {INVALID_SWIZZLE, nullptr}};
3333
std::unordered_map<uint32, MTL::Texture*> m_fallbackViewCache;
3434

3535
MTL::Texture* CreateSwizzledView(uint32 gpuSamplerSwizzle);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ MTL::DepthStencilState* MetalDepthStencilCache::GetDepthStencilState(const Latte
3030
MTL::DepthStencilDescriptor* desc = MTL::DepthStencilDescriptor::alloc()->init();
3131
desc->setDepthWriteEnabled(depthWriteEnable);
3232

33-
auto depthCompareFunc = GetMtlCompareFunc(depthFunc);
34-
if (!depthEnable)
33+
if (depthEnable)
3534
{
36-
depthCompareFunc = MTL::CompareFunctionAlways;
35+
desc->setDepthCompareFunction(GetMtlCompareFunc(depthFunc));
3736
}
38-
desc->setDepthCompareFunction(depthCompareFunc);
3937

4038
// Stencil state
4139
bool stencilEnable = LatteGPUState.contextNew.DB_DEPTH_CONTROL.get_STENCIL_ENABLE();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
105105
{
106106
memcpy(newPtr + elem * newStride, oldPtr + elem * stride, stride);
107107
}
108+
debug_printf("Restrided vertex buffer (old stride: %zu, new stride: %zu, old size: %zu, new size: %zu)\n", stride, newStride, vertexBufferRange->size, newSize);
108109

109110
restrideInfo.memoryInvalidated = false;
110111
restrideInfo.lastStride = newStride;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
4343

4444
auto attribute = vertexDescriptor->attributes()->object(semanticId);
4545
attribute->setOffset(attr.offset);
46-
// Bind from the end to not conflict with uniform buffers
4746
attribute->setBufferIndex(GET_MTL_VERTEX_BUFFER_INDEX(attr.attributeBufferIndex));
4847
attribute->setFormat(GetMtlVertexFormat(attr.format));
4948

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "Metal/MTLResource.hpp"
2020
#include "Metal/MTLTypes.hpp"
2121
#include "gui/guiWrapper.h"
22+
#include <stdexcept>
2223

2324
extern bool hasValidFramebufferAttached;
2425

@@ -596,8 +597,7 @@ void MetalRenderer::draw_execute(uint32 baseVertex, uint32 baseInstance, uint32
596597
debug_printf("no vertex function, skipping draw\n");
597598
return;
598599
}
599-
600-
auto fetchShader = vertexShader->compatibleFetchShader;
600+
const auto fetchShader = LatteSHRC_GetActiveFetchShader();
601601

602602
// Render pipeline state
603603
MTL::RenderPipelineState* renderPipelineState = m_pipelineCache->GetPipelineState(fetchShader, vertexShader, pixelShader, m_state.activeFBO, LatteGPUState.contextNew);

0 commit comments

Comments
 (0)