From 64005ba9eead9ca8bcb0e1d8baff0e24f889c31f Mon Sep 17 00:00:00 2001 From: Samuliak Date: Tue, 14 May 2024 20:51:53 +0200 Subject: [PATCH] don't hardcode render pipeline attachments --- src/Ryujinx.Graphics.Metal/Pipeline.cs | 4 ++-- src/Ryujinx.Graphics.Metal/Program.cs | 2 ++ .../RenderEncoderState.cs | 24 ++++++++++++------- src/Ryujinx.Graphics.Metal/Sampler.cs | 3 ++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Ryujinx.Graphics.Metal/Pipeline.cs b/src/Ryujinx.Graphics.Metal/Pipeline.cs index 94d0bc19e2..dd2e3bf99b 100644 --- a/src/Ryujinx.Graphics.Metal/Pipeline.cs +++ b/src/Ryujinx.Graphics.Metal/Pipeline.cs @@ -137,7 +137,7 @@ namespace Ryujinx.Graphics.Metal } var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor); - _renderEncoderState.SetEncoderState(renderCommandEncoder, _vertexDescriptor); + _renderEncoderState.SetEncoderState(renderCommandEncoder, descriptor, _vertexDescriptor); RebindBuffers(renderCommandEncoder); @@ -193,7 +193,7 @@ namespace Ryujinx.Graphics.Metal _helperShaders.BlitShader.VertexFunction, _helperShaders.BlitShader.FragmentFunction, _device); - _renderEncoderState.SetEncoderState(renderCommandEncoder, _vertexDescriptor); + _renderEncoderState.SetEncoderState(renderCommandEncoder, descriptor, _vertexDescriptor); var sampler = _device.NewSamplerState(new MTLSamplerDescriptor { diff --git a/src/Ryujinx.Graphics.Metal/Program.cs b/src/Ryujinx.Graphics.Metal/Program.cs index 764bcf126d..255a7316bc 100644 --- a/src/Ryujinx.Graphics.Metal/Program.cs +++ b/src/Ryujinx.Graphics.Metal/Program.cs @@ -28,6 +28,8 @@ namespace Ryujinx.Graphics.Metal { Logger.Warning?.Print(LogClass.Gpu, $"Shader linking failed: \n{StringHelper.String(libraryError.LocalizedDescription)}"); _status = ProgramLinkStatus.Failure; + //Console.WriteLine($"SHADER {index}: {shader.Code}"); + //throw new NotImplementedException(); return; } diff --git a/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs b/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs index 4a045c96b6..832e0ba36c 100644 --- a/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs +++ b/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs @@ -36,7 +36,7 @@ namespace Ryujinx.Graphics.Metal _device = device; } - public unsafe readonly void SetEncoderState(MTLRenderCommandEncoder renderCommandEncoder, MTLVertexDescriptor vertexDescriptor) + public unsafe readonly void SetEncoderState(MTLRenderCommandEncoder renderCommandEncoder, MTLRenderPassDescriptor descriptor, MTLVertexDescriptor vertexDescriptor) { var renderPipelineDescriptor = new MTLRenderPipelineDescriptor { @@ -53,13 +53,21 @@ namespace Ryujinx.Graphics.Metal renderPipelineDescriptor.FragmentFunction = _fragmentFunction.Value; } - var attachment = renderPipelineDescriptor.ColorAttachments.Object(0); - attachment.SetBlendingEnabled(true); - attachment.PixelFormat = MTLPixelFormat.BGRA8Unorm; - attachment.SourceAlphaBlendFactor = MTLBlendFactor.SourceAlpha; - attachment.DestinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha; - attachment.SourceRGBBlendFactor = MTLBlendFactor.SourceAlpha; - attachment.DestinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha; + const int maxColorAttachments = 8; + for (int i = 0; i < maxColorAttachments; i++) + { + var renderAttachment = descriptor.ColorAttachments.Object((ulong)i); + if (renderAttachment.Texture != null) + { + var attachment = renderPipelineDescriptor.ColorAttachments.Object((ulong)i); + attachment.SetBlendingEnabled(true); + attachment.PixelFormat = renderAttachment.Texture.PixelFormat; + attachment.SourceAlphaBlendFactor = MTLBlendFactor.SourceAlpha; + attachment.DestinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha; + attachment.SourceRGBBlendFactor = MTLBlendFactor.SourceAlpha; + attachment.DestinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha; + } + } var error = new NSError(IntPtr.Zero); var pipelineState = _device.NewRenderPipelineState(renderPipelineDescriptor, ref error); diff --git a/src/Ryujinx.Graphics.Metal/Sampler.cs b/src/Ryujinx.Graphics.Metal/Sampler.cs index f4ffecc027..00570b8a98 100644 --- a/src/Ryujinx.Graphics.Metal/Sampler.cs +++ b/src/Ryujinx.Graphics.Metal/Sampler.cs @@ -1,6 +1,7 @@ using Ryujinx.Graphics.GAL; using SharpMetal.Metal; using System.Runtime.Versioning; +using System; namespace Ryujinx.Graphics.Metal { @@ -23,7 +24,7 @@ namespace Ryujinx.Graphics.Metal LodMinClamp = info.MinLod, LodMaxClamp = info.MaxLod, LodAverage = false, - MaxAnisotropy = (uint)info.MaxAnisotropy, + MaxAnisotropy = Math.Max((uint)info.MaxAnisotropy, 1), SAddressMode = info.AddressU.Convert(), TAddressMode = info.AddressV.Convert(), RAddressMode = info.AddressP.Convert()