diff --git a/src/Ryujinx.Graphics.Metal/DepthStencilCache.cs b/src/Ryujinx.Graphics.Metal/DepthStencilCache.cs index 8456a9228..1964d093b 100644 --- a/src/Ryujinx.Graphics.Metal/DepthStencilCache.cs +++ b/src/Ryujinx.Graphics.Metal/DepthStencilCache.cs @@ -1,9 +1,4 @@ -using Ryujinx.Common.Logging; -using Ryujinx.Graphics.GAL; -using SharpMetal.Foundation; using SharpMetal.Metal; -using System; -using System.Collections.Generic; using System.Runtime.Versioning; namespace Ryujinx.Graphics.Metal @@ -31,37 +26,40 @@ namespace Ryujinx.Graphics.Metal { private readonly MTLDevice _device; - public DepthStencilCache(MTLDevice device) { + public DepthStencilCache(MTLDevice device) + { _device = device; } - protected override DepthStencilHash GetHash(MTLDepthStencilDescriptor descriptor) { - var hash = new DepthStencilHash(); - - // Front face - hash.FrontFace = new DepthStencilHash.StencilHash { - StencilFailureOperation = descriptor.FrontFaceStencil.StencilFailureOperation, - DepthFailureOperation = descriptor.FrontFaceStencil.DepthFailureOperation, - DepthStencilPassOperation = descriptor.FrontFaceStencil.DepthStencilPassOperation, - StencilCompareFunction = descriptor.FrontFaceStencil.StencilCompareFunction, - ReadMask = descriptor.FrontFaceStencil.ReadMask, - WriteMask = descriptor.FrontFaceStencil.WriteMask + protected override DepthStencilHash GetHash(MTLDepthStencilDescriptor descriptor) + { + var hash = new DepthStencilHash + { + // Front face + FrontFace = new DepthStencilHash.StencilHash + { + StencilFailureOperation = descriptor.FrontFaceStencil.StencilFailureOperation, + DepthFailureOperation = descriptor.FrontFaceStencil.DepthFailureOperation, + DepthStencilPassOperation = descriptor.FrontFaceStencil.DepthStencilPassOperation, + StencilCompareFunction = descriptor.FrontFaceStencil.StencilCompareFunction, + ReadMask = descriptor.FrontFaceStencil.ReadMask, + WriteMask = descriptor.FrontFaceStencil.WriteMask + }, + // Back face + BackFace = new DepthStencilHash.StencilHash + { + StencilFailureOperation = descriptor.BackFaceStencil.StencilFailureOperation, + DepthFailureOperation = descriptor.BackFaceStencil.DepthFailureOperation, + DepthStencilPassOperation = descriptor.BackFaceStencil.DepthStencilPassOperation, + StencilCompareFunction = descriptor.BackFaceStencil.StencilCompareFunction, + ReadMask = descriptor.BackFaceStencil.ReadMask, + WriteMask = descriptor.BackFaceStencil.WriteMask + }, + // Depth + DepthCompareFunction = descriptor.DepthCompareFunction, + DepthWriteEnabled = descriptor.DepthWriteEnabled }; - // Back face - hash.BackFace = new DepthStencilHash.StencilHash { - StencilFailureOperation = descriptor.BackFaceStencil.StencilFailureOperation, - DepthFailureOperation = descriptor.BackFaceStencil.DepthFailureOperation, - DepthStencilPassOperation = descriptor.BackFaceStencil.DepthStencilPassOperation, - StencilCompareFunction = descriptor.BackFaceStencil.StencilCompareFunction, - ReadMask = descriptor.BackFaceStencil.ReadMask, - WriteMask = descriptor.BackFaceStencil.WriteMask - }; - - // Depth - hash.DepthCompareFunction = descriptor.DepthCompareFunction; - hash.DepthWriteEnabled = descriptor.DepthWriteEnabled; - return hash; } diff --git a/src/Ryujinx.Graphics.Metal/EncoderState.cs b/src/Ryujinx.Graphics.Metal/EncoderState.cs index 93d84cf57..919677732 100644 --- a/src/Ryujinx.Graphics.Metal/EncoderState.cs +++ b/src/Ryujinx.Graphics.Metal/EncoderState.cs @@ -12,7 +12,8 @@ namespace Ryujinx.Graphics.Metal public DirtyFlags() { } - public void MarkAll() { + public void MarkAll() + { Pipeline = true; DepthStencil = true; } diff --git a/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs b/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs index fc8be20c1..a6a9a149d 100644 --- a/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs +++ b/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs @@ -12,11 +12,10 @@ namespace Ryujinx.Graphics.Metal [SupportedOSPlatform("macos")] struct EncoderStateManager { - private readonly MTLDevice _device; private readonly Pipeline _pipeline; - private readonly RenderPipelineCache RenderPipelineCache; - private readonly DepthStencilCache DepthStencilCache; + private readonly RenderPipelineCache _renderPipelineCache; + private readonly DepthStencilCache _depthStencilCache; private EncoderState _currentState = new(); private EncoderState _backState = new(); @@ -30,10 +29,9 @@ namespace Ryujinx.Graphics.Metal public EncoderStateManager(MTLDevice device, Pipeline pipeline) { - _device = device; _pipeline = pipeline; - RenderPipelineCache = new(device); - DepthStencilCache = new(device); + _renderPipelineCache = new(device); + _depthStencilCache = new(device); } public void SwapStates() @@ -139,7 +137,8 @@ namespace Ryujinx.Graphics.Metal _currentState.Dirty.Clear(); } - private void SetPipelineState(MTLRenderCommandEncoder renderCommandEncoder) { + private readonly void SetPipelineState(MTLRenderCommandEncoder renderCommandEncoder) + { var renderPipelineDescriptor = new MTLRenderPipelineDescriptor(); for (int i = 0; i < Constants.MaxColorAttachments; i++) @@ -209,7 +208,7 @@ namespace Ryujinx.Graphics.Metal renderPipelineDescriptor.FragmentFunction = _currentState.FragmentFunction.Value; } - var pipelineState = RenderPipelineCache.GetOrCreate(renderPipelineDescriptor); + var pipelineState = _renderPipelineCache.GetOrCreate(renderPipelineDescriptor); renderCommandEncoder.SetRenderPipelineState(pipelineState); @@ -330,7 +329,7 @@ namespace Ryujinx.Graphics.Metal descriptor.FrontFaceStencil = _currentState.FrontFaceStencil; } - _currentState.DepthStencilState = DepthStencilCache.GetOrCreate(descriptor); + _currentState.DepthStencilState = _depthStencilCache.GetOrCreate(descriptor); // Mark dirty _currentState.Dirty.DepthStencil = true; @@ -354,7 +353,7 @@ namespace Ryujinx.Graphics.Metal descriptor.FrontFaceStencil = _currentState.FrontFaceStencil; } - _currentState.DepthStencilState = DepthStencilCache.GetOrCreate(descriptor); + _currentState.DepthStencilState = _depthStencilCache.GetOrCreate(descriptor); // Mark dirty _currentState.Dirty.DepthStencil = true; @@ -556,7 +555,7 @@ namespace Ryujinx.Graphics.Metal } } - private void SetDepthStencilState(MTLRenderCommandEncoder renderCommandEncoder) + private readonly void SetDepthStencilState(MTLRenderCommandEncoder renderCommandEncoder) { if (_currentState.DepthStencilState != null) { @@ -564,7 +563,7 @@ namespace Ryujinx.Graphics.Metal } } - private void SetDepthClamp(MTLRenderCommandEncoder renderCommandEncoder) + private readonly void SetDepthClamp(MTLRenderCommandEncoder renderCommandEncoder) { renderCommandEncoder.SetDepthClipMode(_currentState.DepthClipMode); } @@ -591,7 +590,7 @@ namespace Ryujinx.Graphics.Metal } } - private MTLVertexDescriptor BuildVertexDescriptor(VertexBufferDescriptor[] bufferDescriptors, VertexAttribDescriptor[] attribDescriptors) + private readonly MTLVertexDescriptor BuildVertexDescriptor(VertexBufferDescriptor[] bufferDescriptors, VertexAttribDescriptor[] attribDescriptors) { var vertexDescriptor = new MTLVertexDescriptor(); uint indexMask = 0; @@ -635,7 +634,7 @@ namespace Ryujinx.Graphics.Metal SetBuffers(renderCommandEncoder, buffers); } - private void SetBuffers(MTLRenderCommandEncoder renderCommandEncoder, List buffers, bool fragment = false) + private readonly void SetBuffers(MTLRenderCommandEncoder renderCommandEncoder, List buffers, bool fragment = false) { foreach (var buffer in buffers) { @@ -648,17 +647,17 @@ namespace Ryujinx.Graphics.Metal } } - private void SetCullMode(MTLRenderCommandEncoder renderCommandEncoder) + private readonly void SetCullMode(MTLRenderCommandEncoder renderCommandEncoder) { renderCommandEncoder.SetCullMode(_currentState.CullMode); } - private void SetFrontFace(MTLRenderCommandEncoder renderCommandEncoder) + private readonly void SetFrontFace(MTLRenderCommandEncoder renderCommandEncoder) { renderCommandEncoder.SetFrontFacingWinding(_currentState.Winding); } - private void SetTextureAndSampler(MTLRenderCommandEncoder renderCommandEncoder, ShaderStage stage, Dictionary textures, Dictionary samplers) + private static void SetTextureAndSampler(MTLRenderCommandEncoder renderCommandEncoder, ShaderStage stage, Dictionary textures, Dictionary samplers) { foreach (var texture in textures) { diff --git a/src/Ryujinx.Graphics.Metal/EnumConversion.cs b/src/Ryujinx.Graphics.Metal/EnumConversion.cs index dbde36f5e..81a84f668 100644 --- a/src/Ryujinx.Graphics.Metal/EnumConversion.cs +++ b/src/Ryujinx.Graphics.Metal/EnumConversion.cs @@ -208,7 +208,7 @@ namespace Ryujinx.Graphics.Metal Format.R16G16B16A16Float => MTLVertexFormat.Half4, Format.R32Float => MTLVertexFormat.Float, Format.R32G32Float => MTLVertexFormat.Float2, - Format.R32G32B32Float=> MTLVertexFormat.Float3, + Format.R32G32B32Float => MTLVertexFormat.Float3, Format.R11G11B10Float => MTLVertexFormat.FloatRG11B10, Format.R32G32B32A32Float => MTLVertexFormat.Float4, Format.R8Uint => MTLVertexFormat.UChar, diff --git a/src/Ryujinx.Graphics.Metal/HelperShader.cs b/src/Ryujinx.Graphics.Metal/HelperShader.cs index a1dab8a5b..2537d6f36 100644 --- a/src/Ryujinx.Graphics.Metal/HelperShader.cs +++ b/src/Ryujinx.Graphics.Metal/HelperShader.cs @@ -10,13 +10,6 @@ using System.Runtime.Versioning; namespace Ryujinx.Graphics.Metal { - enum ComponentType - { - Float, - SignedInteger, - UnsignedInteger, - } - [SupportedOSPlatform("macos")] class HelperShader : IDisposable { diff --git a/src/Ryujinx.Graphics.Metal/Pipeline.cs b/src/Ryujinx.Graphics.Metal/Pipeline.cs index d19a774f8..5a0785299 100644 --- a/src/Ryujinx.Graphics.Metal/Pipeline.cs +++ b/src/Ryujinx.Graphics.Metal/Pipeline.cs @@ -5,7 +5,6 @@ using SharpMetal.Foundation; using SharpMetal.Metal; using SharpMetal.QuartzCore; using System; -using System.Drawing; using System.Runtime.CompilerServices; using System.Runtime.Versioning; diff --git a/src/Ryujinx.Graphics.Metal/RenderPipelineCache.cs b/src/Ryujinx.Graphics.Metal/RenderPipelineCache.cs index 1da672134..d564ef629 100644 --- a/src/Ryujinx.Graphics.Metal/RenderPipelineCache.cs +++ b/src/Ryujinx.Graphics.Metal/RenderPipelineCache.cs @@ -1,9 +1,7 @@ using Ryujinx.Common.Logging; -using Ryujinx.Graphics.GAL; using SharpMetal.Foundation; using SharpMetal.Metal; using System; -using System.Collections.Generic; using System.Runtime.Versioning; namespace Ryujinx.Graphics.Metal @@ -72,16 +70,24 @@ namespace Ryujinx.Graphics.Metal { private readonly MTLDevice _device; - public RenderPipelineCache(MTLDevice device) { + public RenderPipelineCache(MTLDevice device) + { _device = device; } - protected override RenderPipelineHash GetHash(MTLRenderPipelineDescriptor descriptor) { - var hash = new RenderPipelineHash(); - - // Functions - hash.VertexFunction = descriptor.VertexFunction; - hash.FragmentFunction = descriptor.FragmentFunction; + protected override RenderPipelineHash GetHash(MTLRenderPipelineDescriptor descriptor) + { + var hash = new RenderPipelineHash + { + // Functions + VertexFunction = descriptor.VertexFunction, + FragmentFunction = descriptor.FragmentFunction, + DepthStencilAttachment = new RenderPipelineHash.DepthStencilAttachmentHash + { + DepthPixelFormat = descriptor.DepthAttachmentPixelFormat, + StencilPixelFormat = descriptor.StencilAttachmentPixelFormat + }, + }; // Color Attachments for (int i = 0; i < Constants.MaxColorAttachments; i++) @@ -100,13 +106,6 @@ namespace Ryujinx.Graphics.Metal }; } - // Depth stencil attachment - hash.DepthStencilAttachment = new RenderPipelineHash.DepthStencilAttachmentHash - { - DepthPixelFormat = descriptor.DepthAttachmentPixelFormat, - StencilPixelFormat = descriptor.StencilAttachmentPixelFormat - }; - // Vertex descriptor hash.VertexDescriptor = new RenderPipelineHash.VertexDescriptorHash(); diff --git a/src/Ryujinx.Graphics.Metal/StateCache.cs b/src/Ryujinx.Graphics.Metal/StateCache.cs index 66fefcf7b..2abf5f528 100644 --- a/src/Ryujinx.Graphics.Metal/StateCache.cs +++ b/src/Ryujinx.Graphics.Metal/StateCache.cs @@ -1,33 +1,28 @@ -using Ryujinx.Common.Logging; -using Ryujinx.Graphics.GAL; -using SharpMetal.Foundation; -using SharpMetal.Metal; -using System; using System.Collections.Generic; using System.Runtime.Versioning; namespace Ryujinx.Graphics.Metal { [SupportedOSPlatform("macos")] - public abstract class StateCache + public abstract class StateCache { - private Dictionary Cache = new(); + private readonly Dictionary _cache = new(); - protected abstract HashT GetHash(DescriptorT descriptor); + protected abstract THash GetHash(TDescriptor descriptor); - protected abstract T CreateValue(DescriptorT descriptor); + protected abstract T CreateValue(TDescriptor descriptor); - public T GetOrCreate(DescriptorT descriptor) + public T GetOrCreate(TDescriptor descriptor) { var hash = GetHash(descriptor); - if (Cache.TryGetValue(hash, out T value)) + if (_cache.TryGetValue(hash, out T value)) { return value; } else { var newValue = CreateValue(descriptor); - Cache.Add(hash, newValue); + _cache.Add(hash, newValue); return newValue; } diff --git a/src/Ryujinx.Graphics.Metal/Window.cs b/src/Ryujinx.Graphics.Metal/Window.cs index a656ce26b..64410df6d 100644 --- a/src/Ryujinx.Graphics.Metal/Window.cs +++ b/src/Ryujinx.Graphics.Metal/Window.cs @@ -58,7 +58,7 @@ namespace Ryujinx.Graphics.Metal public void Dispose() { - + _metalLayer.Dispose(); } } } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenMemory.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenMemory.cs index b771ec12e..24c07e97c 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenMemory.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenMemory.cs @@ -246,12 +246,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions return resourceDefinitions.Textures[textOp.Binding].Name; } - // TODO: Verify that this is valid in MSL - private static string GetMask(int index) - { - return $".{"rgba".AsSpan(index, 1)}"; - } - private static string GetMaskMultiDest(int mask) { string swizzle = ".";