From 68f6c082872ab93ae088f03bf5f2e5d8678b10b5 Mon Sep 17 00:00:00 2001 From: Samuliak Date: Sat, 25 May 2024 11:03:45 +0200 Subject: [PATCH] implement texture get data --- src/Ryujinx.Graphics.Metal/Texture.cs | 30 ++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Ryujinx.Graphics.Metal/Texture.cs b/src/Ryujinx.Graphics.Metal/Texture.cs index 524cd6cf9..89b965ba8 100644 --- a/src/Ryujinx.Graphics.Metal/Texture.cs +++ b/src/Ryujinx.Graphics.Metal/Texture.cs @@ -196,7 +196,35 @@ namespace Ryujinx.Graphics.Metal public PinnedSpan GetData(int layer, int level) { - throw new NotImplementedException(); + var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder(); + + ulong bytesPerRow = (ulong)Info.GetMipStride(level); + ulong length = bytesPerRow * (ulong)Info.Height; + ulong bytesPerImage = 0; + if (MTLTexture.TextureType == MTLTextureType.Type3D) + { + bytesPerImage = length; + } + + unsafe + { + var mtlBuffer = _device.NewBuffer(length, MTLResourceOptions.ResourceStorageModeShared); + + blitCommandEncoder.CopyFromTexture( + MTLTexture, + (ulong)layer, + (ulong)level, + new MTLOrigin(), + new MTLSize { width = MTLTexture.Width, height = MTLTexture.Height, depth = MTLTexture.Depth }, + mtlBuffer, + 0, + bytesPerRow, + bytesPerImage + ); + + // TODO: Dispose the buffer + return new PinnedSpan(mtlBuffer.Contents.ToPointer(), (int)length); + } } // TODO: Handle array formats