2024-08-31 20:42:56 +00:00
|
|
|
using Ryujinx.Common.Memory;
|
|
|
|
using Ryujinx.Graphics.GAL;
|
2023-07-28 01:51:20 +00:00
|
|
|
using SharpMetal.Metal;
|
2024-08-31 20:42:56 +00:00
|
|
|
using System;
|
2023-07-29 03:50:00 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2023-07-28 01:51:20 +00:00
|
|
|
using System.Runtime.Versioning;
|
2024-08-31 20:42:56 +00:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Metal
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
[SupportedOSPlatform("macos")]
|
|
|
|
class Texture : ITexture, IDisposable
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
private readonly TextureCreateInfo _info;
|
2023-07-28 02:54:24 +00:00
|
|
|
private readonly Pipeline _pipeline;
|
2023-07-28 12:20:15 +00:00
|
|
|
private readonly MTLDevice _device;
|
2023-07-28 01:51:20 +00:00
|
|
|
|
|
|
|
public MTLTexture MTLTexture;
|
2023-07-28 19:04:35 +00:00
|
|
|
public TextureCreateInfo Info => _info;
|
2023-07-28 01:51:20 +00:00
|
|
|
public int Width => Info.Width;
|
|
|
|
public int Height => Info.Height;
|
2023-07-29 04:14:21 +00:00
|
|
|
public int Depth => Info.Depth;
|
2023-07-28 01:51:20 +00:00
|
|
|
|
2023-07-28 15:55:52 +00:00
|
|
|
public Texture(MTLDevice device, Pipeline pipeline, TextureCreateInfo info)
|
2023-07-28 01:51:20 +00:00
|
|
|
{
|
2023-07-28 12:20:15 +00:00
|
|
|
_device = device;
|
|
|
|
_pipeline = pipeline;
|
2023-07-28 01:51:20 +00:00
|
|
|
_info = info;
|
|
|
|
|
|
|
|
var descriptor = new MTLTextureDescriptor();
|
|
|
|
descriptor.PixelFormat = FormatTable.GetFormat(Info.Format);
|
2023-07-29 04:07:54 +00:00
|
|
|
descriptor.Usage = MTLTextureUsage.ShaderRead | MTLTextureUsage.ShaderWrite | MTLTextureUsage.RenderTarget;
|
2023-07-28 01:51:20 +00:00
|
|
|
descriptor.Width = (ulong)Width;
|
|
|
|
descriptor.Height = (ulong)Height;
|
2023-07-29 04:14:21 +00:00
|
|
|
descriptor.Depth = (ulong)Depth;
|
2023-07-28 01:51:20 +00:00
|
|
|
descriptor.Depth = (ulong)Info.Depth;
|
|
|
|
descriptor.SampleCount = (ulong)Info.Samples;
|
2023-07-28 02:54:24 +00:00
|
|
|
descriptor.MipmapLevelCount = (ulong)Info.Levels;
|
2023-07-28 01:51:20 +00:00
|
|
|
descriptor.TextureType = Info.Target.Convert();
|
|
|
|
descriptor.Swizzle = new MTLTextureSwizzleChannels
|
|
|
|
{
|
|
|
|
red = Info.SwizzleR.Convert(),
|
|
|
|
green = Info.SwizzleG.Convert(),
|
|
|
|
blue = Info.SwizzleB.Convert(),
|
|
|
|
alpha = Info.SwizzleA.Convert()
|
|
|
|
};
|
|
|
|
|
2023-07-28 12:20:15 +00:00
|
|
|
MTLTexture = _device.NewTexture(descriptor);
|
2023-07-28 01:51:20 +00:00
|
|
|
}
|
|
|
|
|
2024-08-31 20:42:56 +00:00
|
|
|
public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
MTLBlitCommandEncoder blitCommandEncoder;
|
|
|
|
|
|
|
|
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
blitCommandEncoder = encoder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blitCommandEncoder = _pipeline.BeginBlitPass();
|
|
|
|
}
|
|
|
|
|
2023-07-28 02:54:24 +00:00
|
|
|
if (destination is Texture destinationTexture)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
blitCommandEncoder.CopyFromTexture(
|
2023-07-28 02:54:24 +00:00
|
|
|
MTLTexture,
|
|
|
|
(ulong)firstLayer,
|
|
|
|
(ulong)firstLevel,
|
|
|
|
destinationTexture.MTLTexture,
|
|
|
|
(ulong)firstLayer,
|
|
|
|
(ulong)firstLevel,
|
|
|
|
MTLTexture.ArrayLength,
|
|
|
|
MTLTexture.MipmapLevelCount);
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
MTLBlitCommandEncoder blitCommandEncoder;
|
|
|
|
|
|
|
|
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
blitCommandEncoder = encoder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blitCommandEncoder = _pipeline.BeginBlitPass();
|
|
|
|
}
|
|
|
|
|
2023-07-28 02:54:24 +00:00
|
|
|
if (destination is Texture destinationTexture)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
blitCommandEncoder.CopyFromTexture(
|
2023-07-28 02:54:24 +00:00
|
|
|
MTLTexture,
|
|
|
|
(ulong)srcLayer,
|
|
|
|
(ulong)srcLevel,
|
|
|
|
destinationTexture.MTLTexture,
|
|
|
|
(ulong)dstLayer,
|
|
|
|
(ulong)dstLevel,
|
|
|
|
MTLTexture.ArrayLength,
|
|
|
|
MTLTexture.MipmapLevelCount);
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
|
|
|
|
{
|
2023-07-29 03:50:00 +00:00
|
|
|
throw new NotImplementedException();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(BufferRange range, int layer, int level, int stride)
|
|
|
|
{
|
2023-07-29 03:50:00 +00:00
|
|
|
MTLBlitCommandEncoder blitCommandEncoder;
|
|
|
|
|
|
|
|
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
blitCommandEncoder = encoder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blitCommandEncoder = _pipeline.BeginBlitPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong bytesPerRow = (ulong)Info.GetMipStride(level);
|
|
|
|
ulong bytesPerImage = 0;
|
|
|
|
if (MTLTexture.TextureType == MTLTextureType.Type3D)
|
|
|
|
{
|
|
|
|
bytesPerImage = bytesPerRow * (ulong)Info.Height;
|
|
|
|
}
|
|
|
|
|
|
|
|
var handle = range.Handle;
|
|
|
|
MTLBuffer mtlBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref handle));
|
|
|
|
|
|
|
|
blitCommandEncoder.CopyFromTexture(
|
|
|
|
MTLTexture,
|
|
|
|
(ulong)layer,
|
|
|
|
(ulong)level,
|
|
|
|
new MTLOrigin(),
|
|
|
|
new MTLSize { width = MTLTexture.Width, height = MTLTexture.Height, depth = MTLTexture.Depth },
|
|
|
|
mtlBuffer,
|
|
|
|
(ulong)range.Offset,
|
|
|
|
bytesPerRow,
|
|
|
|
bytesPerImage);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public PinnedSpan<byte> GetData()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public PinnedSpan<byte> GetData(int layer, int level)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetData(SpanOrArray<byte> data)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetData(SpanOrArray<byte> data, int layer, int level)
|
|
|
|
{
|
2023-07-29 03:35:55 +00:00
|
|
|
MTLBlitCommandEncoder blitCommandEncoder;
|
|
|
|
|
|
|
|
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
blitCommandEncoder = encoder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blitCommandEncoder = _pipeline.BeginBlitPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong bytesPerRow = (ulong)Info.GetMipStride(level);
|
|
|
|
ulong bytesPerImage = 0;
|
|
|
|
if (MTLTexture.TextureType == MTLTextureType.Type3D)
|
|
|
|
{
|
|
|
|
bytesPerImage = bytesPerRow * (ulong)Info.Height;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
var dataSpan = data.Span;
|
|
|
|
var mtlBuffer = _device.NewBuffer((ulong)dataSpan.Length, MTLResourceOptions.ResourceStorageModeShared);
|
|
|
|
var bufferSpan = new Span<byte>(mtlBuffer.Contents.ToPointer(), dataSpan.Length);
|
|
|
|
dataSpan.CopyTo(bufferSpan);
|
|
|
|
|
|
|
|
blitCommandEncoder.CopyFromBuffer(
|
|
|
|
mtlBuffer,
|
|
|
|
0,
|
|
|
|
bytesPerRow,
|
|
|
|
bytesPerImage,
|
2023-07-29 03:50:00 +00:00
|
|
|
new MTLSize { width = MTLTexture.Width, height = MTLTexture.Height, depth = MTLTexture.Depth},
|
2023-07-29 03:35:55 +00:00
|
|
|
MTLTexture,
|
|
|
|
(ulong)layer,
|
|
|
|
(ulong)level,
|
|
|
|
new MTLOrigin()
|
|
|
|
);
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 19:04:35 +00:00
|
|
|
public void SetData(SpanOrArray<byte> data, int layer, int level, Rectangle<int> region)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-07-29 03:33:09 +00:00
|
|
|
MTLBlitCommandEncoder blitCommandEncoder;
|
|
|
|
|
|
|
|
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
blitCommandEncoder = encoder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blitCommandEncoder = _pipeline.BeginBlitPass();
|
|
|
|
}
|
|
|
|
|
2023-07-28 19:09:40 +00:00
|
|
|
ulong bytesPerRow = (ulong)Info.GetMipStride(level);
|
2023-07-28 19:04:35 +00:00
|
|
|
ulong bytesPerImage = 0;
|
|
|
|
if (MTLTexture.TextureType == MTLTextureType.Type3D)
|
|
|
|
{
|
|
|
|
bytesPerImage = bytesPerRow * (ulong)Info.Height;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe
|
2023-07-28 15:55:52 +00:00
|
|
|
{
|
2023-07-29 03:33:09 +00:00
|
|
|
var dataSpan = data.Span;
|
|
|
|
var mtlBuffer = _device.NewBuffer((ulong)dataSpan.Length, MTLResourceOptions.ResourceStorageModeShared);
|
|
|
|
var bufferSpan = new Span<byte>(mtlBuffer.Contents.ToPointer(), dataSpan.Length);
|
|
|
|
dataSpan.CopyTo(bufferSpan);
|
|
|
|
|
|
|
|
blitCommandEncoder.CopyFromBuffer(
|
|
|
|
mtlBuffer,
|
|
|
|
0,
|
|
|
|
bytesPerRow,
|
|
|
|
bytesPerImage,
|
2023-07-29 03:50:00 +00:00
|
|
|
new MTLSize { width = (ulong)region.Width, height = (ulong)region.Height, depth = 1 },
|
2023-07-29 03:33:09 +00:00
|
|
|
MTLTexture,
|
|
|
|
(ulong)layer,
|
|
|
|
(ulong)level,
|
|
|
|
new MTLOrigin { x = (ulong)region.X, y = (ulong)region.Y }
|
|
|
|
);
|
2023-07-28 15:55:52 +00:00
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetStorage(BufferRange buffer)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Release()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|