From 8b214470184549e1804ededda92d939e883f4be3 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Fri, 28 Jul 2023 15:04:35 -0400 Subject: [PATCH] Finish SetData /w region --- src/Ryujinx.Graphics.Metal/Texture.cs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Ryujinx.Graphics.Metal/Texture.cs b/src/Ryujinx.Graphics.Metal/Texture.cs index b03fb26c8..23a248622 100644 --- a/src/Ryujinx.Graphics.Metal/Texture.cs +++ b/src/Ryujinx.Graphics.Metal/Texture.cs @@ -14,7 +14,7 @@ namespace Ryujinx.Graphics.Metal private readonly MTLDevice _device; public MTLTexture MTLTexture; - public TextureCreateInfo Info => Info; + public TextureCreateInfo Info => _info; public int Width => Info.Width; public int Height => Info.Height; @@ -114,22 +114,28 @@ namespace Ryujinx.Graphics.Metal throw new NotImplementedException(); } - public unsafe void SetData(SpanOrArray data, int layer, int level, Rectangle region) + public void SetData(SpanOrArray data, int layer, int level, Rectangle region) { - // 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; + ulong bytesPerRow = (ulong)(Info.Width * Info.BytesPerPixel); + ulong bytesPerImage = 0; + + if (MTLTexture.TextureType == MTLTextureType.Type3D) + { + bytesPerImage = bytesPerRow * (ulong)Info.Height; + } + 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) + unsafe { - MTLTexture.ReplaceRegion(mtlRegion, (ulong)level, (ulong)layer, new IntPtr(pData), bytesPerRow, 0); + fixed (byte* pData = data.Span) + { + MTLTexture.ReplaceRegion(mtlRegion, (ulong)level, (ulong)layer, new IntPtr(pData), bytesPerRow, bytesPerImage); + } } }