From 17cfcf07d276e1c12247e2552d47e6144d850ecb Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Thu, 20 Jun 2024 14:25:40 +0100 Subject: [PATCH] Helper shader cleanup --- src/Ryujinx.Graphics.Metal/HelperShader.cs | 56 ++++++++-------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/src/Ryujinx.Graphics.Metal/HelperShader.cs b/src/Ryujinx.Graphics.Metal/HelperShader.cs index f836756986..44e34aceb6 100644 --- a/src/Ryujinx.Graphics.Metal/HelperShader.cs +++ b/src/Ryujinx.Graphics.Metal/HelperShader.cs @@ -150,8 +150,13 @@ namespace Ryujinx.Graphics.Metal Extents2DF srcRegion, Extents2DF dstRegion) { + // Save current state + _pipeline.SaveState(); + const int RegionBufferSize = 16; + _pipeline.SetTextureAndSampler(ShaderStage.Fragment, 0, src, srcSampler); + Span region = stackalloc float[RegionBufferSize / sizeof(float)]; region[0] = srcRegion.X1 / src.Width; @@ -169,12 +174,11 @@ namespace Ryujinx.Graphics.Metal (region[2], region[3]) = (region[3], region[2]); } - // var bufferHandle = _renderer.BufferManager.CreateWithHandle(RegionBufferSize); - // _renderer.BufferManager.SetData(bufferHandle, 0, region); - // _pipeline.SetUniformBuffers([new BufferAssignment(0, new BufferRange(bufferHandle, 0, RegionBufferSize))]); + var bufferHandle = _renderer.BufferManager.CreateWithHandle(RegionBufferSize); + _renderer.BufferManager.SetData(bufferHandle, 0, region); + _pipeline.SetUniformBuffers([new BufferAssignment(0, new BufferRange(bufferHandle, 0, RegionBufferSize))]); Span viewports = stackalloc Viewport[1]; - Span> scissors = stackalloc Rectangle[1]; var rect = new Rectangle( MathF.Min(dstRegion.X1, dstRegion.X2), @@ -191,28 +195,13 @@ namespace Ryujinx.Graphics.Metal 0f, 1f); - scissors[0] = new Rectangle(0, 0, 0xFFFF, 0xFFFF); - - // Save current state - _pipeline.SaveState(); - _pipeline.SetProgram(_programColorBlit); _pipeline.SetViewports(viewports); - _pipeline.SetScissors(scissors); - _pipeline.SetTextureAndSampler(ShaderStage.Fragment, 0, src, srcSampler); _pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip); - _pipeline.SetFaceCulling(false, Face.FrontAndBack); - // For some reason this results in a SIGSEGV - // _pipeline.SetStencilTest(CreateStencilTestDescriptor(false)); - _pipeline.SetDepthTest(new DepthTestDescriptor(false, false, CompareOp.Always)); - - fixed (float* ptr = region) - { - _pipeline.GetOrCreateRenderEncoder(true).SetVertexBytes((IntPtr)ptr, RegionBufferSize, 0); - } - _pipeline.Draw(4, 1, 0, 0); + _renderer.BufferManager.Delete(bufferHandle); + // Restore previous state _pipeline.RestoreState(); } @@ -249,16 +238,13 @@ namespace Ryujinx.Graphics.Metal shaderParams[3] = srcOffset; using var buffer = _renderer.BufferManager.ReserveOrCreate(cbs, ParamsBufferSize); - buffer.Holder.SetDataUnchecked(buffer.Offset, shaderParams); - _pipeline.SetUniformBuffers([new BufferAssignment(0, buffer.Range)]); Span> sbRanges = new Auto[2]; sbRanges[0] = srcBuffer; sbRanges[1] = dstBuffer; - _pipeline.SetStorageBuffers(1, sbRanges); _pipeline.SetProgram(_programStrideChange); @@ -275,11 +261,17 @@ namespace Ryujinx.Graphics.Metal int dstWidth, int dstHeight) { - const int ClearColorBufferSize = 16; - // Save current state _pipeline.SaveState(); + const int ClearColorBufferSize = 16; + + // TODO: Flush + + using var buffer = _renderer.BufferManager.ReserveOrCreate(_pipeline.CurrentCommandBuffer, ClearColorBufferSize); + buffer.Holder.SetDataUnchecked(buffer.Offset, clearColor); + _pipeline.SetUniformBuffers([new BufferAssignment(0, buffer.Range)]); + Span viewports = stackalloc Viewport[1]; // TODO: Set exact viewport! @@ -293,18 +285,9 @@ namespace Ryujinx.Graphics.Metal 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.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip); _pipeline.SetViewports(viewports); - - fixed (float* ptr = clearColor) - { - _pipeline.GetOrCreateRenderEncoder(true).SetFragmentBytes((IntPtr)ptr, ClearColorBufferSize, 0); - } - + _pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip); _pipeline.Draw(4, 1, 0, 0); // Restore previous state @@ -343,6 +326,7 @@ namespace Ryujinx.Graphics.Metal _pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip); _pipeline.SetViewports(viewports); _pipeline.SetDepthTest(new DepthTestDescriptor(true, depthMask, CompareOp.Always)); + // TODO: Figure out why this causes a crash // _pipeline.SetStencilTest(CreateStencilTestDescriptor(stencilMask != 0, stencilValue, 0xFF, stencilMask)); _pipeline.GetOrCreateRenderEncoder(true).SetFragmentBytes(ptr, ClearDepthBufferSize, 0); _pipeline.Draw(4, 1, 0, 0);