From 45b533b23b02b9cd88f4c7afddc47cd2c14defcf Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Sat, 18 May 2024 20:27:27 -0400 Subject: [PATCH] Fix present --- src/Ryujinx.Graphics.Metal/EncoderState.cs | 4 +- .../EncoderStateManager.cs | 66 ++++++++++--------- src/Ryujinx.Graphics.Metal/MetalRenderer.cs | 1 + src/Ryujinx.Graphics.Metal/Pipeline.cs | 6 +- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/Ryujinx.Graphics.Metal/EncoderState.cs b/src/Ryujinx.Graphics.Metal/EncoderState.cs index ed512125b1..35b726e258 100644 --- a/src/Ryujinx.Graphics.Metal/EncoderState.cs +++ b/src/Ryujinx.Graphics.Metal/EncoderState.cs @@ -8,6 +8,8 @@ namespace Ryujinx.Graphics.Metal [SupportedOSPlatform("macos")] public struct EncoderState { + public const int MaxColorAttachments = 8; + public MTLFunction? VertexFunction = null; public MTLFunction? FragmentFunction = null; @@ -43,7 +45,7 @@ namespace Ryujinx.Graphics.Metal // Changes to attachments take recreation! public MTLTexture DepthStencil = default; - public MTLTexture[] RenderTargets = []; + public MTLTexture[] RenderTargets = new MTLTexture[MaxColorAttachments]; public MTLVertexDescriptor VertexDescriptor = new(); public EncoderState() { } diff --git a/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs b/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs index 49c3b1ee8d..3414903544 100644 --- a/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs +++ b/src/Ryujinx.Graphics.Metal/EncoderStateManager.cs @@ -48,8 +48,7 @@ namespace Ryujinx.Graphics.Metal var renderPassDescriptor = new MTLRenderPassDescriptor(); var renderPipelineDescriptor = new MTLRenderPipelineDescriptor(); - const int MaxColorAttachments = 8; - for (int i = 0; i < MaxColorAttachments; i++) + for (int i = 0; i < EncoderState.MaxColorAttachments; i++) { if (_currentState.RenderTargets[i] != IntPtr.Zero) { @@ -70,40 +69,43 @@ namespace Ryujinx.Graphics.Metal var depthAttachment = renderPassDescriptor.DepthAttachment; var stencilAttachment = renderPassDescriptor.StencilAttachment; - switch (_currentState.DepthStencil.PixelFormat) + if (_currentState.DepthStencil != IntPtr.Zero) { - // Depth Only Attachment - case MTLPixelFormat.Depth16Unorm: - case MTLPixelFormat.Depth32Float: - depthAttachment.Texture = _currentState.DepthStencil; - depthAttachment.LoadAction = MTLLoadAction.Load; - renderPipelineDescriptor.DepthAttachmentPixelFormat = _currentState.DepthStencil.PixelFormat; - break; + switch (_currentState.DepthStencil.PixelFormat) + { + // Depth Only Attachment + case MTLPixelFormat.Depth16Unorm: + case MTLPixelFormat.Depth32Float: + depthAttachment.Texture = _currentState.DepthStencil; + depthAttachment.LoadAction = MTLLoadAction.Load; + renderPipelineDescriptor.DepthAttachmentPixelFormat = _currentState.DepthStencil.PixelFormat; + break; - // Stencil Only Attachment - case MTLPixelFormat.Stencil8: - stencilAttachment.Texture = _currentState.DepthStencil; - stencilAttachment.LoadAction = MTLLoadAction.Load; - renderPipelineDescriptor.StencilAttachmentPixelFormat = _currentState.DepthStencil.PixelFormat; - break; + // Stencil Only Attachment + case MTLPixelFormat.Stencil8: + stencilAttachment.Texture = _currentState.DepthStencil; + stencilAttachment.LoadAction = MTLLoadAction.Load; + renderPipelineDescriptor.StencilAttachmentPixelFormat = _currentState.DepthStencil.PixelFormat; + break; - // Combined Attachment - case MTLPixelFormat.Depth24UnormStencil8: - case MTLPixelFormat.Depth32FloatStencil8: - depthAttachment.Texture = _currentState.DepthStencil; - depthAttachment.LoadAction = MTLLoadAction.Load; + // Combined Attachment + case MTLPixelFormat.Depth24UnormStencil8: + case MTLPixelFormat.Depth32FloatStencil8: + depthAttachment.Texture = _currentState.DepthStencil; + depthAttachment.LoadAction = MTLLoadAction.Load; - var unpackedFormat = FormatTable.PackedStencilToXFormat(_currentState.DepthStencil.PixelFormat); - var stencilView = _currentState.DepthStencil.NewTextureView(unpackedFormat); - stencilAttachment.Texture = stencilView; - stencilAttachment.LoadAction = MTLLoadAction.Load; + var unpackedFormat = FormatTable.PackedStencilToXFormat(_currentState.DepthStencil.PixelFormat); + var stencilView = _currentState.DepthStencil.NewTextureView(unpackedFormat); + stencilAttachment.Texture = stencilView; + stencilAttachment.LoadAction = MTLLoadAction.Load; - renderPipelineDescriptor.DepthAttachmentPixelFormat = _currentState.DepthStencil.PixelFormat; - renderPipelineDescriptor.StencilAttachmentPixelFormat = _currentState.DepthStencil.PixelFormat; - break; - default: - Logger.Error?.PrintMsg(LogClass.Gpu, $"Unsupported Depth/Stencil Format: {_currentState.DepthStencil.PixelFormat}!"); - break; + renderPipelineDescriptor.DepthAttachmentPixelFormat = _currentState.DepthStencil.PixelFormat; + renderPipelineDescriptor.StencilAttachmentPixelFormat = _currentState.DepthStencil.PixelFormat; + break; + default: + Logger.Error?.PrintMsg(LogClass.Gpu, $"Unsupported Depth/Stencil Format: {_currentState.DepthStencil.PixelFormat}!"); + break; + } } renderPipelineDescriptor.VertexDescriptor = _currentState.VertexDescriptor; @@ -187,7 +189,7 @@ namespace Ryujinx.Graphics.Metal public void UpdateRenderTargets(ITexture[] colors, ITexture depthStencil) { - _currentState.RenderTargets = new MTLTexture[colors.Length]; + _currentState.RenderTargets = new MTLTexture[EncoderState.MaxColorAttachments]; for (int i = 0; i < colors.Length; i++) { diff --git a/src/Ryujinx.Graphics.Metal/MetalRenderer.cs b/src/Ryujinx.Graphics.Metal/MetalRenderer.cs index 7342a363ba..4cd230dbc5 100644 --- a/src/Ryujinx.Graphics.Metal/MetalRenderer.cs +++ b/src/Ryujinx.Graphics.Metal/MetalRenderer.cs @@ -43,6 +43,7 @@ namespace Ryujinx.Graphics.Metal { var layer = _getMetalLayer(); layer.Device = _device; + layer.FramebufferOnly = false; _window = new Window(this, layer); _pipeline = new Pipeline(_device, _queue); diff --git a/src/Ryujinx.Graphics.Metal/Pipeline.cs b/src/Ryujinx.Graphics.Metal/Pipeline.cs index 1b29a85621..7ec0384b4c 100644 --- a/src/Ryujinx.Graphics.Metal/Pipeline.cs +++ b/src/Ryujinx.Graphics.Metal/Pipeline.cs @@ -158,7 +158,11 @@ namespace Ryujinx.Graphics.Metal _encoderStateManager.SwapStates(); - // _helperShader.BlitColor(tex, drawable.Texture); + // TODO: Clean this up + var textureInfo = new TextureCreateInfo((int)drawable.Texture.Width, (int)drawable.Texture.Height, (int)drawable.Texture.Depth, (int)drawable.Texture.MipmapLevelCount, (int)drawable.Texture.SampleCount, 0, 0, 0, Format.B8G8R8A8Unorm, 0, Target.Texture2D, SwizzleComponent.Red, SwizzleComponent.Green, SwizzleComponent.Blue, SwizzleComponent.Alpha); + var dest = new Texture(_device, this, textureInfo, drawable.Texture, 0, 0); + + _helperShader.BlitColor(tex, dest); _commandBuffer.PresentDrawable(drawable); _commandBuffer.Commit();