From 3c188718c4f6dc0fd4179e531f539bd5b3b699fb Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Thu, 30 May 2024 13:20:16 +0100 Subject: [PATCH] Fix Clear Viewport --- .../EncoderStateManager.cs | 2 ++ src/Ryujinx.Graphics.Metal/HelperShader.cs | 36 +++++++++++++++++-- src/Ryujinx.Graphics.Metal/Pipeline.cs | 7 ++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs b/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs index eef8fd341..0ca7dbd2d 100644 --- a/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs +++ b/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs @@ -25,6 +25,8 @@ namespace Ryujinx.Graphics.Metal public readonly MTLIndexType IndexType => _currentState.IndexType; public readonly ulong IndexBufferOffset => _currentState.IndexBufferOffset; public readonly PrimitiveTopology Topology => _currentState.Topology; + public readonly Texture RenderTarget => _currentState.RenderTargets[0]; + public readonly Texture DepthStencil => _currentState.DepthStencil; // RGBA32F is the biggest format private const int ZeroBufferSize = 4 * 4; diff --git a/src/Ryujinx.Graphics.Metal/HelperShader.cs b/src/Ryujinx.Graphics.Metal/HelperShader.cs index 5adc336f0..5066b0244 100644 --- a/src/Ryujinx.Graphics.Metal/HelperShader.cs +++ b/src/Ryujinx.Graphics.Metal/HelperShader.cs @@ -202,19 +202,35 @@ namespace Ryujinx.Graphics.Metal public unsafe void ClearColor( int index, - ReadOnlySpan clearColor) + ReadOnlySpan clearColor, + uint componentMask, + int dstWidth, + int dstHeight) { const int ClearColorBufferSize = 16; // Save current state _pipeline.SaveState(); + Span viewports = stackalloc Viewport[1]; + + // TODO: Set exact viewport! + viewports[0] = new Viewport( + new Rectangle(0, 0, dstWidth, dstHeight), + ViewportSwizzle.PositiveX, + ViewportSwizzle.PositiveY, + ViewportSwizzle.PositiveZ, + ViewportSwizzle.PositiveW, + 0f, + 1f); + _pipeline.SetProgram(_programsColorClear[index]); _pipeline.SetBlendState(index, new BlendDescriptor(false, new ColorF(0f, 0f, 0f, 1f), BlendOp.Add, BlendFactor.One, BlendFactor.Zero, BlendOp.Add, BlendFactor.One, BlendFactor.Zero)); _pipeline.SetFaceCulling(false, Face.Front); _pipeline.SetDepthTest(new DepthTestDescriptor(false, false, CompareOp.Always)); - // _pipeline.SetRenderTargetColorMasks([componentMask]); + _pipeline.SetRenderTargetColorMasks([componentMask]); _pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip); + _pipeline.SetViewports(viewports); fixed (float* ptr = clearColor) { @@ -231,7 +247,9 @@ namespace Ryujinx.Graphics.Metal float depthValue, bool depthMask, int stencilValue, - int stencilMask) + int stencilMask, + int dstWidth, + int dstHeight) { const int ClearDepthBufferSize = 4; @@ -240,10 +258,22 @@ namespace Ryujinx.Graphics.Metal // Save current state _pipeline.SaveState(); + Span viewports = stackalloc Viewport[1]; + + viewports[0] = new Viewport( + new Rectangle(0, 0, dstWidth, dstHeight), + ViewportSwizzle.PositiveX, + ViewportSwizzle.PositiveY, + ViewportSwizzle.PositiveZ, + ViewportSwizzle.PositiveW, + 0f, + 1f); + _pipeline.SetProgram(_programDepthStencilClear); _pipeline.SetFaceCulling(false, Face.Front); _pipeline.SetDepthTest(new DepthTestDescriptor(false, false, CompareOp.Always)); _pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip); + _pipeline.SetViewports(viewports); _pipeline.SetDepthTest(new DepthTestDescriptor(true, depthMask, CompareOp.Always)); // _pipeline.SetStencilTest(CreateStencilTestDescriptor(stencilMask != 0, stencilValue, 0xFF, stencilMask)); _pipeline.GetOrCreateRenderEncoder().SetFragmentBytes(ptr, ClearDepthBufferSize, 0); diff --git a/src/Ryujinx.Graphics.Metal/Pipeline.cs b/src/Ryujinx.Graphics.Metal/Pipeline.cs index 9d6aab7a6..912a28ac0 100644 --- a/src/Ryujinx.Graphics.Metal/Pipeline.cs +++ b/src/Ryujinx.Graphics.Metal/Pipeline.cs @@ -250,13 +250,16 @@ namespace Ryujinx.Graphics.Metal public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color) { float[] colors = [color.Red, color.Green, color.Blue, color.Alpha]; + var dst = _encoderStateManager.RenderTarget; - _helperShader.ClearColor(index, colors); + _helperShader.ClearColor(index, colors, componentMask, dst.Width, dst.Height); } public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue, int stencilMask) { - _helperShader.ClearDepthStencil(depthValue, depthMask, stencilValue, stencilMask); + var depthStencil = _encoderStateManager.DepthStencil; + + _helperShader.ClearDepthStencil(depthValue, depthMask, stencilValue, stencilMask, depthStencil.Width, depthStencil.Height); } public void CommandBufferBarrier()