Skip to content

Commit eb573fc

Browse files
committed
fix: color write mask and unpackHalf2x16
1 parent c6ab45a commit eb573fc

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

src/Cafe/HW/Latte/Core/LatteIndices.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,15 @@ void LatteIndices_generateAutoLineLoopIndices(void* indexDataOutput, uint32 coun
287287
template<typename T>
288288
void LatteIndices_unpackTriangleFanAndConvert(const void* indexDataInput, void* indexDataOutput, uint32 count, uint32& indexMin, uint32& indexMax)
289289
{
290-
debug_printf("TRIANGLE FAN UNPACK\n");
290+
debug_printf("TRIANGLE FAN UNPACK %u\n", rand());
291291
const betype<T>* src = (betype<T>*)indexDataInput;
292292
T* dst = (T*)indexDataOutput;
293293
// TODO: check this
294294
for (sint32 i = 0; i < count; i++)
295295
{
296296
uint32 i0;
297297
if (i % 2 == 0)
298-
i0 = i / 2;
298+
i0 = i / 2;
299299
else
300300
i0 = count - 1 - i / 2;
301301
T idx = src[i0];
@@ -308,7 +308,7 @@ void LatteIndices_unpackTriangleFanAndConvert(const void* indexDataInput, void*
308308
template<typename T>
309309
void LatteIndices_generateAutoTriangleFanIndices(const void* indexDataInput, void* indexDataOutput, uint32 count, uint32& indexMin, uint32& indexMax)
310310
{
311-
debug_printf("TRIANGLE FAN AUTO\n");
311+
debug_printf("TRIANGLE FAN AUTO %u\n", rand());
312312
const betype<T>* src = (betype<T>*)indexDataInput;
313313
T* dst = (T*)indexDataOutput;
314314
for (sint32 i = 0; i < count; i++)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,8 +3734,9 @@ void LatteDecompiler_emitHelperFunctions(LatteDecompilerShaderContext* shaderCon
37343734

37353735
// unpackHalf2x16
37363736
fCStr_shaderSource->add(""
3737-
"float2 unpackHalf2x16(float x) {\r\n"
3738-
"return float2(as_type<half>(ushort(as_type<uint>(x) & 0x00FF)), as_type<half>(ushort((as_type<uint>(x) & 0xFF00) >> 16)));\r\n"
3737+
"template<typename T>\r\n"
3738+
"float2 unpackHalf2x16(T x) {\r\n"
3739+
"return float2(as_type<half2>(x));\r\n"
37393740
"}\r\n");
37403741

37413742
// Bit cast

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Common/precompiled.h"
33
#include "Metal/MTLDepthStencil.hpp"
44
#include "Metal/MTLRenderCommandEncoder.hpp"
5+
#include "Metal/MTLRenderPipeline.hpp"
56
#include "Metal/MTLSampler.hpp"
67

78
std::map<Latte::E_GX2SURFFMT, MetalPixelFormatInfo> MTL_COLOR_FORMAT_TABLE = {
@@ -469,3 +470,14 @@ MTL::StencilOperation GetMtlStencilOp(Latte::LATTE_DB_DEPTH_CONTROL::E_STENCILAC
469470
cemu_assert_debug((uint32)action < std::size(MTL_STENCIL_OPERATIONS));
470471
return MTL_STENCIL_OPERATIONS[(uint32)action];
471472
}
473+
474+
MTL::ColorWriteMask GetMtlColorWriteMask(uint8 mask)
475+
{
476+
MTL::ColorWriteMask mtlMask = MTL::ColorWriteMaskNone;
477+
if (mask & 0x1) mtlMask |= MTL::ColorWriteMaskRed;
478+
if (mask & 0x2) mtlMask |= MTL::ColorWriteMaskGreen;
479+
if (mask & 0x4) mtlMask |= MTL::ColorWriteMaskBlue;
480+
if (mask & 0x8) mtlMask |= MTL::ColorWriteMaskAlpha;
481+
482+
return mtlMask;
483+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//#include "Cafe/HW/Latte/Core/FetchShader.h"
88
#include "Cafe/HW/Latte/Renderer/Renderer.h"
99
#include "Metal/MTLDepthStencil.hpp"
10+
#include "Metal/MTLRenderPipeline.hpp"
1011
#include "Metal/MTLSampler.hpp"
1112
#include "Metal/MTLTexture.hpp"
1213

@@ -55,3 +56,5 @@ MTL::SamplerAddressMode GetMtlSamplerAddressMode(Latte::LATTE_SQ_TEX_SAMPLER_WOR
5556
MTL::TextureSwizzle GetMtlTextureSwizzle(uint32 swizzle);
5657

5758
MTL::StencilOperation GetMtlStencilOp(Latte::LATTE_DB_DEPTH_CONTROL::E_STENCILACTION action);
59+
60+
MTL::ColorWriteMask GetMtlColorWriteMask(uint8 mask);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
9292
desc->setFragmentFunction(mtlPixelShader->GetFunction());
9393
// TODO: don't always set the vertex descriptor?
9494
desc->setVertexDescriptor(vertexDescriptor);
95+
96+
// Color attachments
97+
const Latte::LATTE_CB_COLOR_CONTROL& colorControlReg = LatteGPUState.contextNew.CB_COLOR_CONTROL;
98+
uint32 blendEnableMask = colorControlReg.get_BLEND_MASK();
99+
uint32 renderTargetMask = LatteGPUState.contextNew.CB_TARGET_MASK.get_MASK();
95100
for (uint8 i = 0; i < 8; i++)
96101
{
97102
const auto& colorBuffer = activeFBO->colorBuffer[i];
@@ -102,12 +107,9 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
102107
}
103108
auto colorAttachment = desc->colorAttachments()->object(i);
104109
colorAttachment->setPixelFormat(texture->GetRGBAView()->pixelFormat());
110+
colorAttachment->setWriteMask(GetMtlColorWriteMask((renderTargetMask >> (i * 4)) & 0xF));
105111

106112
// Blending
107-
const Latte::LATTE_CB_COLOR_CONTROL& colorControlReg = LatteGPUState.contextNew.CB_COLOR_CONTROL;
108-
uint32 blendEnableMask = colorControlReg.get_BLEND_MASK();
109-
uint32 renderTargetMask = LatteGPUState.contextNew.CB_TARGET_MASK.get_MASK();
110-
111113
bool blendEnabled = ((blendEnableMask & (1 << i))) != 0;
112114
// Only float data type is blendable
113115
if (blendEnabled && GetMtlPixelFormatInfo(texture->format, false).dataType == MetalDataType::FLOAT)
@@ -120,7 +122,6 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
120122
auto srcRgbBlendFactor = GetMtlBlendFactor(blendControlReg.get_COLOR_SRCBLEND());
121123
auto dstRgbBlendFactor = GetMtlBlendFactor(blendControlReg.get_COLOR_DSTBLEND());
122124

123-
colorAttachment->setWriteMask((renderTargetMask >> (i * 4)) & 0xF);
124125
colorAttachment->setRgbBlendOperation(rgbBlendOp);
125126
colorAttachment->setSourceRGBBlendFactor(srcRgbBlendFactor);
126127
colorAttachment->setDestinationRGBBlendFactor(dstRgbBlendFactor);
@@ -138,6 +139,8 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
138139
}
139140
}
140141
}
142+
143+
// Depth stencil attachment
141144
if (activeFBO->depthBuffer.texture)
142145
{
143146
auto texture = static_cast<LatteTextureViewMtl*>(activeFBO->depthBuffer.texture);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void MetalRenderer::InitializeLayer(const Vector2i& size, bool mainWindow)
7979
m_metalLayer->setDevice(m_device);
8080
// TODO: don't always force sRGB
8181
// TODO: shouldn't this be handled differently?
82-
m_metalLayer->setPixelFormat(MTL::PixelFormatRGBA8Unorm_sRGB);
82+
m_metalLayer->setPixelFormat(MTL::PixelFormatRGBA8Unorm/*_sRGB*/);
8383

8484
// Present pipeline
8585
NS::Error* error = nullptr;

0 commit comments

Comments
 (0)