From 8da7c42cf84c7baeae4e4a8cfe8d171f1c5b5139 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Fri, 28 Jul 2023 11:55:52 -0400 Subject: [PATCH] Delete and Get Data from Buffer --- src/Ryujinx.Graphics.Metal/MetalRenderer.cs | 25 +++++---------------- src/Ryujinx.Graphics.Metal/Texture.cs | 20 ++++++++++++++--- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/Ryujinx.Graphics.Metal/MetalRenderer.cs b/src/Ryujinx.Graphics.Metal/MetalRenderer.cs index e0074351e..1c4db1a01 100644 --- a/src/Ryujinx.Graphics.Metal/MetalRenderer.cs +++ b/src/Ryujinx.Graphics.Metal/MetalRenderer.cs @@ -89,22 +89,7 @@ namespace Ryujinx.Graphics.Metal public ITexture CreateTexture(TextureCreateInfo info) { - MTLTextureDescriptor descriptor = new() - { - PixelFormat = FormatCapabilities.ConvertToMTLFormat(info.Format), - TextureType = info.Target.Convert(), - Width = (ulong)info.Width, - Height = (ulong)info.Height, - MipmapLevelCount = (ulong)info.Levels, - SampleCount = (ulong)info.Samples, - }; - - return CreateTextureView(info); - } - - internal Texture CreateTextureView(TextureCreateInfo info) - { - throw new NotImplementedException(); + return new Texture(_device, _pipeline, info); } public bool PrepareHostMapping(IntPtr address, ulong size) @@ -120,12 +105,14 @@ namespace Ryujinx.Graphics.Metal public void DeleteBuffer(BufferHandle buffer) { - throw new NotImplementedException(); + MTLBuffer mtlBuffer = new(Unsafe.As(ref buffer)); + mtlBuffer.SetPurgeableState(MTLPurgeableState.Empty); } - public PinnedSpan GetBufferData(BufferHandle buffer, int offset, int size) + public unsafe PinnedSpan GetBufferData(BufferHandle buffer, int offset, int size) { - throw new NotImplementedException(); + MTLBuffer mtlBuffer = new(Unsafe.As(ref buffer)); + return new PinnedSpan(IntPtr.Add(mtlBuffer.Contents, offset).ToPointer(), size); } public Capabilities GetCapabilities() diff --git a/src/Ryujinx.Graphics.Metal/Texture.cs b/src/Ryujinx.Graphics.Metal/Texture.cs index 9d7e5e0d0..b03fb26c8 100644 --- a/src/Ryujinx.Graphics.Metal/Texture.cs +++ b/src/Ryujinx.Graphics.Metal/Texture.cs @@ -18,7 +18,7 @@ namespace Ryujinx.Graphics.Metal public int Width => Info.Width; public int Height => Info.Height; - public Texture(MTLDevice device, Pipeline pipeline, TextureCreateInfo info, int firstLayer, int firstLevel) + public Texture(MTLDevice device, Pipeline pipeline, TextureCreateInfo info) { _device = device; _pipeline = pipeline; @@ -114,9 +114,23 @@ namespace Ryujinx.Graphics.Metal throw new NotImplementedException(); } - public void SetData(SpanOrArray data, int layer, int level, Rectangle region) + public unsafe void SetData(SpanOrArray data, int layer, int level, Rectangle region) { - throw new NotImplementedException(); + // TODO: Figure out bytesPerRow + // For an ordinary or packed pixel format, the stride, in bytes, between rows of source data. + // For a compressed pixel format, the stride is the number of bytes from the beginning of one row of blocks to the beginning of the next. + if (MTLTexture.IsSparse) + ulong bytesPerRow = 0; + var mtlRegion = new MTLRegion + { + origin = new MTLOrigin { x = (ulong)region.X, y = (ulong)region.Y }, + size = new MTLSize { width = (ulong)region.Width, height = (ulong)region.Height }, + }; + + fixed (byte* pData = data.Span) + { + MTLTexture.ReplaceRegion(mtlRegion, (ulong)level, (ulong)layer, new IntPtr(pData), bytesPerRow, 0); + } } public void SetStorage(BufferRange buffer)