Skip to content

Commit 1bcdade

Browse files
committed
set stencil state
1 parent d3249dc commit 1bcdade

File tree

8 files changed

+79
-44
lines changed

8 files changed

+79
-44
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3215,7 +3215,7 @@ static void _emitExportCode(LatteDecompilerShaderContext* shaderContext, LatteDe
32153215
cemu_assert_unimplemented(); // ukn
32163216
}
32173217

3218-
src->add("gl_FragDepth = ");
3218+
src->add("out.depth = ");
32193219
_emitExportGPRReadCode(shaderContext, cfInstruction, LATTE_DECOMPILER_DTYPE_FLOAT, 0);
32203220
src->add(".x");
32213221
src->add(";" _CRLF);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ namespace LatteDecompiler
271271
}
272272
}
273273

274+
// generate depth output for pixel shader
275+
if (decompilerContext->shader->pixelDepthOutputMask)
276+
{
277+
src->add("float passDepth [[depth(any)]];" _CRLF);
278+
}
279+
274280
src->add("};" _CRLF _CRLF);
275281
}
276282
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ void CachedFBOMtl::CreateRenderPass()
2828
depthAttachment->setTexture(textureView->GetRGBAView());
2929
depthAttachment->setLoadAction(MTL::LoadActionLoad);
3030
depthAttachment->setStoreAction(MTL::StoreActionStore);
31+
32+
// setup stencil attachment
33+
if (depthBuffer.hasStencil)
34+
{
35+
auto stencilAttachment = m_renderPassDescriptor->stencilAttachment();
36+
stencilAttachment->setTexture(textureView->GetRGBAView());
37+
stencilAttachment->setLoadAction(MTL::LoadActionLoad);
38+
stencilAttachment->setStoreAction(MTL::StoreActionStore);
39+
}
3140
}
3241
}
3342

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,20 @@ MTL::TextureSwizzle GetMtlTextureSwizzle(uint32 swizzle)
452452
cemu_assert_debug(swizzle < std::size(MTL_TEXTURE_SWIZZLES));
453453
return MTL_TEXTURE_SWIZZLES[swizzle];
454454
}
455+
456+
const MTL::StencilOperation MTL_STENCIL_OPERATIONS[8] = {
457+
MTL::StencilOperationKeep,
458+
MTL::StencilOperationZero,
459+
MTL::StencilOperationReplace,
460+
MTL::StencilOperationIncrementClamp,
461+
MTL::StencilOperationDecrementClamp,
462+
MTL::StencilOperationInvert,
463+
MTL::StencilOperationIncrementWrap,
464+
MTL::StencilOperationDecrementWrap
465+
};
466+
467+
MTL::StencilOperation GetMtlStencilOp(Latte::LATTE_DB_DEPTH_CONTROL::E_STENCILACTION action)
468+
{
469+
cemu_assert_debug((uint32)action < std::size(MTL_STENCIL_OPERATIONS));
470+
return MTL_STENCIL_OPERATIONS[(uint32)action];
471+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ MTL::CompareFunction GetMtlCompareFunc(Latte::E_COMPAREFUNC func);
5353
MTL::SamplerAddressMode GetMtlSamplerAddressMode(Latte::LATTE_SQ_TEX_SAMPLER_WORD0_0::E_CLAMP clamp);
5454

5555
MTL::TextureSwizzle GetMtlTextureSwizzle(uint32 swizzle);
56+
57+
MTL::StencilOperation GetMtlStencilOp(Latte::LATTE_DB_DEPTH_CONTROL::E_STENCILACTION action);

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

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ MTL::DepthStencilState* MetalDepthStencilCache::GetDepthStencilState(const Latte
3737
}
3838
desc->setDepthCompareFunction(depthCompareFunc);
3939

40-
// TODO: stencil state
41-
/*
4240
// get stencil control parameters
4341
bool stencilEnable = LatteGPUState.contextNew.DB_DEPTH_CONTROL.get_STENCIL_ENABLE();
4442
bool backStencilEnable = LatteGPUState.contextNew.DB_DEPTH_CONTROL.get_BACK_STENCIL_ENABLE();
@@ -58,48 +56,47 @@ MTL::DepthStencilState* MetalDepthStencilCache::GetDepthStencilState(const Latte
5856
uint32 stencilWriteMaskBack = LatteGPUState.contextNew.DB_STENCILREFMASK_BF.get_STENCILWRITEMASK_B();
5957
uint32 stencilRefBack = LatteGPUState.contextNew.DB_STENCILREFMASK_BF.get_STENCILREF_B();
6058

61-
static const VkStencilOp stencilOpTable[8] = {
62-
VK_STENCIL_OP_KEEP,
63-
VK_STENCIL_OP_ZERO,
64-
VK_STENCIL_OP_REPLACE,
65-
VK_STENCIL_OP_INCREMENT_AND_CLAMP,
66-
VK_STENCIL_OP_DECREMENT_AND_CLAMP,
67-
VK_STENCIL_OP_INVERT,
68-
VK_STENCIL_OP_INCREMENT_AND_WRAP,
69-
VK_STENCIL_OP_DECREMENT_AND_WRAP
70-
};
71-
72-
depthStencilState.stencilTestEnable = stencilEnable ? VK_TRUE : VK_FALSE;
73-
74-
depthStencilState.front.reference = stencilRefFront;
75-
depthStencilState.front.compareMask = stencilCompareMaskFront;
76-
depthStencilState.front.writeMask = stencilWriteMaskBack;
77-
depthStencilState.front.compareOp = vkDepthCompareTable[(size_t)frontStencilFunc];
78-
depthStencilState.front.depthFailOp = stencilOpTable[(size_t)frontStencilZFail];
79-
depthStencilState.front.failOp = stencilOpTable[(size_t)frontStencilFail];
80-
depthStencilState.front.passOp = stencilOpTable[(size_t)frontStencilZPass];
81-
82-
if (backStencilEnable)
83-
{
84-
depthStencilState.back.reference = stencilRefBack;
85-
depthStencilState.back.compareMask = stencilCompareMaskBack;
86-
depthStencilState.back.writeMask = stencilWriteMaskBack;
87-
depthStencilState.back.compareOp = vkDepthCompareTable[(size_t)backStencilFunc];
88-
depthStencilState.back.depthFailOp = stencilOpTable[(size_t)backStencilZFail];
89-
depthStencilState.back.failOp = stencilOpTable[(size_t)backStencilFail];
90-
depthStencilState.back.passOp = stencilOpTable[(size_t)backStencilZPass];
91-
}
92-
else
59+
if (stencilEnable)
9360
{
94-
depthStencilState.back.reference = stencilRefFront;
95-
depthStencilState.back.compareMask = stencilCompareMaskFront;
96-
depthStencilState.back.writeMask = stencilWriteMaskFront;
97-
depthStencilState.back.compareOp = vkDepthCompareTable[(size_t)frontStencilFunc];
98-
depthStencilState.back.depthFailOp = stencilOpTable[(size_t)frontStencilZFail];
99-
depthStencilState.back.failOp = stencilOpTable[(size_t)frontStencilFail];
100-
depthStencilState.back.passOp = stencilOpTable[(size_t)frontStencilZPass];
61+
MTL::StencilDescriptor* frontStencil = MTL::StencilDescriptor::alloc()->init();
62+
// TODO: set reference
63+
//depthStencilState.front.reference = stencilRefFront;
64+
frontStencil->setReadMask(stencilCompareMaskFront);
65+
frontStencil->setWriteMask(stencilWriteMaskFront);
66+
frontStencil->setStencilCompareFunction(GetMtlCompareFunc(frontStencilFunc));
67+
frontStencil->setDepthFailureOperation(GetMtlStencilOp(frontStencilZFail));
68+
frontStencil->setStencilFailureOperation(GetMtlStencilOp(frontStencilFail));
69+
frontStencil->setDepthStencilPassOperation(GetMtlStencilOp(frontStencilZPass));
70+
desc->setFrontFaceStencil(frontStencil);
71+
72+
MTL::StencilDescriptor* backStencil = MTL::StencilDescriptor::alloc()->init();
73+
if (backStencilEnable)
74+
{
75+
// TODO: set reference
76+
//depthStencilState.back.reference = stencilRefBack;
77+
backStencil->setReadMask(stencilCompareMaskBack);
78+
backStencil->setWriteMask(stencilWriteMaskBack);
79+
backStencil->setStencilCompareFunction(GetMtlCompareFunc(backStencilFunc));
80+
backStencil->setDepthFailureOperation(GetMtlStencilOp(backStencilZFail));
81+
backStencil->setStencilFailureOperation(GetMtlStencilOp(backStencilFail));
82+
backStencil->setDepthStencilPassOperation(GetMtlStencilOp(backStencilZPass));
83+
}
84+
else
85+
{
86+
// TODO: set reference
87+
//depthStencilState.back.reference = stencilRefFront;
88+
backStencil->setReadMask(stencilCompareMaskFront);
89+
backStencil->setWriteMask(stencilWriteMaskFront);
90+
backStencil->setStencilCompareFunction(GetMtlCompareFunc(frontStencilFunc));
91+
backStencil->setDepthFailureOperation(GetMtlStencilOp(frontStencilZFail));
92+
backStencil->setStencilFailureOperation(GetMtlStencilOp(frontStencilFail));
93+
backStencil->setDepthStencilPassOperation(GetMtlStencilOp(frontStencilZPass));
94+
}
95+
desc->setBackFaceStencil(backStencil);
96+
97+
frontStencil->release();
98+
backStencil->release();
10199
}
102-
*/
103100

104101
depthStencilState = m_mtlr->GetDevice()->newDepthStencilState(desc);
105102
desc->release();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
143143
{
144144
auto texture = static_cast<LatteTextureViewMtl*>(activeFBO->depthBuffer.texture);
145145
desc->setDepthAttachmentPixelFormat(texture->GetRGBAView()->pixelFormat());
146-
// TODO: stencil pixel format
146+
if (activeFBO->depthBuffer.hasStencil)
147+
{
148+
desc->setStencilAttachmentPixelFormat(texture->GetRGBAView()->pixelFormat());
149+
}
147150
}
148151

149152
NS::Error* error = nullptr;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum class MetalEncoderType
4949
Blit,
5050
};
5151

52+
// HACK: Dummy occlusion query object for Metal
5253
class LatteQueryObjectMtl : public LatteQueryObject
5354
{
5455
public:

0 commit comments

Comments
 (0)