fix: color write mask and unpackHalf2x16

This commit is contained in:
Samuliak 2024-08-11 18:32:50 +02:00
parent c6ab45a098
commit eb573fcaca
6 changed files with 30 additions and 11 deletions

View File

@ -287,7 +287,7 @@ void LatteIndices_generateAutoLineLoopIndices(void* indexDataOutput, uint32 coun
template<typename T> template<typename T>
void LatteIndices_unpackTriangleFanAndConvert(const void* indexDataInput, void* indexDataOutput, uint32 count, uint32& indexMin, uint32& indexMax) void LatteIndices_unpackTriangleFanAndConvert(const void* indexDataInput, void* indexDataOutput, uint32 count, uint32& indexMin, uint32& indexMax)
{ {
debug_printf("TRIANGLE FAN UNPACK\n"); debug_printf("TRIANGLE FAN UNPACK %u\n", rand());
const betype<T>* src = (betype<T>*)indexDataInput; const betype<T>* src = (betype<T>*)indexDataInput;
T* dst = (T*)indexDataOutput; T* dst = (T*)indexDataOutput;
// TODO: check this // TODO: check this
@ -308,7 +308,7 @@ void LatteIndices_unpackTriangleFanAndConvert(const void* indexDataInput, void*
template<typename T> template<typename T>
void LatteIndices_generateAutoTriangleFanIndices(const void* indexDataInput, void* indexDataOutput, uint32 count, uint32& indexMin, uint32& indexMax) void LatteIndices_generateAutoTriangleFanIndices(const void* indexDataInput, void* indexDataOutput, uint32 count, uint32& indexMin, uint32& indexMax)
{ {
debug_printf("TRIANGLE FAN AUTO\n"); debug_printf("TRIANGLE FAN AUTO %u\n", rand());
const betype<T>* src = (betype<T>*)indexDataInput; const betype<T>* src = (betype<T>*)indexDataInput;
T* dst = (T*)indexDataOutput; T* dst = (T*)indexDataOutput;
for (sint32 i = 0; i < count; i++) for (sint32 i = 0; i < count; i++)

View File

@ -3734,8 +3734,9 @@ void LatteDecompiler_emitHelperFunctions(LatteDecompilerShaderContext* shaderCon
// unpackHalf2x16 // unpackHalf2x16
fCStr_shaderSource->add("" fCStr_shaderSource->add(""
"float2 unpackHalf2x16(float x) {\r\n" "template<typename T>\r\n"
"return float2(as_type<half>(ushort(as_type<uint>(x) & 0x00FF)), as_type<half>(ushort((as_type<uint>(x) & 0xFF00) >> 16)));\r\n" "float2 unpackHalf2x16(T x) {\r\n"
"return float2(as_type<half2>(x));\r\n"
"}\r\n"); "}\r\n");
// Bit cast // Bit cast

View File

@ -2,6 +2,7 @@
#include "Common/precompiled.h" #include "Common/precompiled.h"
#include "Metal/MTLDepthStencil.hpp" #include "Metal/MTLDepthStencil.hpp"
#include "Metal/MTLRenderCommandEncoder.hpp" #include "Metal/MTLRenderCommandEncoder.hpp"
#include "Metal/MTLRenderPipeline.hpp"
#include "Metal/MTLSampler.hpp" #include "Metal/MTLSampler.hpp"
std::map<Latte::E_GX2SURFFMT, MetalPixelFormatInfo> MTL_COLOR_FORMAT_TABLE = { std::map<Latte::E_GX2SURFFMT, MetalPixelFormatInfo> MTL_COLOR_FORMAT_TABLE = {
@ -469,3 +470,14 @@ MTL::StencilOperation GetMtlStencilOp(Latte::LATTE_DB_DEPTH_CONTROL::E_STENCILAC
cemu_assert_debug((uint32)action < std::size(MTL_STENCIL_OPERATIONS)); cemu_assert_debug((uint32)action < std::size(MTL_STENCIL_OPERATIONS));
return MTL_STENCIL_OPERATIONS[(uint32)action]; return MTL_STENCIL_OPERATIONS[(uint32)action];
} }
MTL::ColorWriteMask GetMtlColorWriteMask(uint8 mask)
{
MTL::ColorWriteMask mtlMask = MTL::ColorWriteMaskNone;
if (mask & 0x1) mtlMask |= MTL::ColorWriteMaskRed;
if (mask & 0x2) mtlMask |= MTL::ColorWriteMaskGreen;
if (mask & 0x4) mtlMask |= MTL::ColorWriteMaskBlue;
if (mask & 0x8) mtlMask |= MTL::ColorWriteMaskAlpha;
return mtlMask;
}

View File

@ -7,6 +7,7 @@
//#include "Cafe/HW/Latte/Core/FetchShader.h" //#include "Cafe/HW/Latte/Core/FetchShader.h"
#include "Cafe/HW/Latte/Renderer/Renderer.h" #include "Cafe/HW/Latte/Renderer/Renderer.h"
#include "Metal/MTLDepthStencil.hpp" #include "Metal/MTLDepthStencil.hpp"
#include "Metal/MTLRenderPipeline.hpp"
#include "Metal/MTLSampler.hpp" #include "Metal/MTLSampler.hpp"
#include "Metal/MTLTexture.hpp" #include "Metal/MTLTexture.hpp"
@ -55,3 +56,5 @@ MTL::SamplerAddressMode GetMtlSamplerAddressMode(Latte::LATTE_SQ_TEX_SAMPLER_WOR
MTL::TextureSwizzle GetMtlTextureSwizzle(uint32 swizzle); MTL::TextureSwizzle GetMtlTextureSwizzle(uint32 swizzle);
MTL::StencilOperation GetMtlStencilOp(Latte::LATTE_DB_DEPTH_CONTROL::E_STENCILACTION action); MTL::StencilOperation GetMtlStencilOp(Latte::LATTE_DB_DEPTH_CONTROL::E_STENCILACTION action);
MTL::ColorWriteMask GetMtlColorWriteMask(uint8 mask);

View File

@ -92,6 +92,11 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
desc->setFragmentFunction(mtlPixelShader->GetFunction()); desc->setFragmentFunction(mtlPixelShader->GetFunction());
// TODO: don't always set the vertex descriptor? // TODO: don't always set the vertex descriptor?
desc->setVertexDescriptor(vertexDescriptor); desc->setVertexDescriptor(vertexDescriptor);
// Color attachments
const Latte::LATTE_CB_COLOR_CONTROL& colorControlReg = LatteGPUState.contextNew.CB_COLOR_CONTROL;
uint32 blendEnableMask = colorControlReg.get_BLEND_MASK();
uint32 renderTargetMask = LatteGPUState.contextNew.CB_TARGET_MASK.get_MASK();
for (uint8 i = 0; i < 8; i++) for (uint8 i = 0; i < 8; i++)
{ {
const auto& colorBuffer = activeFBO->colorBuffer[i]; const auto& colorBuffer = activeFBO->colorBuffer[i];
@ -102,12 +107,9 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
} }
auto colorAttachment = desc->colorAttachments()->object(i); auto colorAttachment = desc->colorAttachments()->object(i);
colorAttachment->setPixelFormat(texture->GetRGBAView()->pixelFormat()); colorAttachment->setPixelFormat(texture->GetRGBAView()->pixelFormat());
colorAttachment->setWriteMask(GetMtlColorWriteMask((renderTargetMask >> (i * 4)) & 0xF));
// Blending // Blending
const Latte::LATTE_CB_COLOR_CONTROL& colorControlReg = LatteGPUState.contextNew.CB_COLOR_CONTROL;
uint32 blendEnableMask = colorControlReg.get_BLEND_MASK();
uint32 renderTargetMask = LatteGPUState.contextNew.CB_TARGET_MASK.get_MASK();
bool blendEnabled = ((blendEnableMask & (1 << i))) != 0; bool blendEnabled = ((blendEnableMask & (1 << i))) != 0;
// Only float data type is blendable // Only float data type is blendable
if (blendEnabled && GetMtlPixelFormatInfo(texture->format, false).dataType == MetalDataType::FLOAT) if (blendEnabled && GetMtlPixelFormatInfo(texture->format, false).dataType == MetalDataType::FLOAT)
@ -120,7 +122,6 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
auto srcRgbBlendFactor = GetMtlBlendFactor(blendControlReg.get_COLOR_SRCBLEND()); auto srcRgbBlendFactor = GetMtlBlendFactor(blendControlReg.get_COLOR_SRCBLEND());
auto dstRgbBlendFactor = GetMtlBlendFactor(blendControlReg.get_COLOR_DSTBLEND()); auto dstRgbBlendFactor = GetMtlBlendFactor(blendControlReg.get_COLOR_DSTBLEND());
colorAttachment->setWriteMask((renderTargetMask >> (i * 4)) & 0xF);
colorAttachment->setRgbBlendOperation(rgbBlendOp); colorAttachment->setRgbBlendOperation(rgbBlendOp);
colorAttachment->setSourceRGBBlendFactor(srcRgbBlendFactor); colorAttachment->setSourceRGBBlendFactor(srcRgbBlendFactor);
colorAttachment->setDestinationRGBBlendFactor(dstRgbBlendFactor); colorAttachment->setDestinationRGBBlendFactor(dstRgbBlendFactor);
@ -138,6 +139,8 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
} }
} }
} }
// Depth stencil attachment
if (activeFBO->depthBuffer.texture) if (activeFBO->depthBuffer.texture)
{ {
auto texture = static_cast<LatteTextureViewMtl*>(activeFBO->depthBuffer.texture); auto texture = static_cast<LatteTextureViewMtl*>(activeFBO->depthBuffer.texture);

View File

@ -79,7 +79,7 @@ void MetalRenderer::InitializeLayer(const Vector2i& size, bool mainWindow)
m_metalLayer->setDevice(m_device); m_metalLayer->setDevice(m_device);
// TODO: don't always force sRGB // TODO: don't always force sRGB
// TODO: shouldn't this be handled differently? // TODO: shouldn't this be handled differently?
m_metalLayer->setPixelFormat(MTL::PixelFormatRGBA8Unorm_sRGB); m_metalLayer->setPixelFormat(MTL::PixelFormatRGBA8Unorm/*_sRGB*/);
// Present pipeline // Present pipeline
NS::Error* error = nullptr; NS::Error* error = nullptr;