Ryujinx/src/Ryujinx.Graphics.Metal/Texture.cs

373 lines
14 KiB
C#
Raw Normal View History

using Ryujinx.Graphics.GAL;
2024-05-14 14:36:01 +00:00
using SharpMetal.Foundation;
using SharpMetal.Metal;
using System;
2024-04-22 21:44:55 +00:00
using System.Buffers;
using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class Texture : TextureBase, ITexture
{
public Texture(MTLDevice device, MetalRenderer renderer, Pipeline pipeline, TextureCreateInfo info) : base(device, renderer, pipeline, info)
{
2024-03-20 03:12:28 +00:00
var descriptor = new MTLTextureDescriptor
{
PixelFormat = FormatTable.GetFormat(Info.Format),
2024-05-14 18:55:26 +00:00
Usage = MTLTextureUsage.Unknown,
2024-03-20 03:12:28 +00:00
SampleCount = (ulong)Info.Samples,
TextureType = Info.Target.Convert(),
Width = (ulong)Info.Width,
Height = (ulong)Info.Height,
MipmapLevelCount = (ulong)Info.Levels
};
2024-03-20 02:58:27 +00:00
if (info.Target == Target.Texture3D)
{
descriptor.Depth = (ulong)Info.Depth;
}
else if (info.Target != Target.Cubemap)
{
descriptor.ArrayLength = (ulong)Info.Depth;
}
2024-03-20 01:56:54 +00:00
2024-05-14 15:57:42 +00:00
descriptor.Swizzle = GetSwizzle(info, descriptor.PixelFormat);
_mtlTexture = _device.NewTexture(descriptor);
}
public Texture(MTLDevice device, MetalRenderer renderer, Pipeline pipeline, TextureCreateInfo info, MTLTexture sourceTexture, int firstLayer, int firstLevel) : base(device, renderer, pipeline, info)
2024-05-15 13:03:53 +00:00
{
2024-05-14 14:36:01 +00:00
var pixelFormat = FormatTable.GetFormat(Info.Format);
var textureType = Info.Target.Convert();
NSRange levels;
levels.location = (ulong)firstLevel;
levels.length = (ulong)Info.Levels;
NSRange slices;
slices.location = (ulong)firstLayer;
slices.length = 1;
if (info.Target != Target.Texture3D && info.Target != Target.Cubemap)
2024-05-14 14:36:01 +00:00
{
slices.length = (ulong)Info.Depth;
}
2024-05-14 15:57:42 +00:00
var swizzle = GetSwizzle(info, pixelFormat);
_mtlTexture = sourceTexture.NewTextureView(pixelFormat, textureType, levels, slices, swizzle);
2024-05-14 15:57:42 +00:00
}
2024-05-15 13:03:53 +00:00
private MTLTextureSwizzleChannels GetSwizzle(TextureCreateInfo info, MTLPixelFormat pixelFormat)
{
2024-05-14 14:36:01 +00:00
var swizzleR = Info.SwizzleR.Convert();
var swizzleG = Info.SwizzleG.Convert();
var swizzleB = Info.SwizzleB.Convert();
var swizzleA = Info.SwizzleA.Convert();
if (info.Format == Format.R5G5B5A1Unorm ||
info.Format == Format.R5G5B5X1Unorm ||
info.Format == Format.R5G6B5Unorm)
{
(swizzleB, swizzleR) = (swizzleR, swizzleB);
}
else if (pixelFormat == MTLPixelFormat.ABGR4Unorm || info.Format == Format.A1B5G5R5Unorm)
{
var tempB = swizzleB;
var tempA = swizzleA;
swizzleB = swizzleG;
swizzleA = swizzleR;
swizzleR = tempA;
swizzleG = tempB;
}
2024-05-14 15:57:42 +00:00
return new MTLTextureSwizzleChannels
2024-05-14 14:36:01 +00:00
{
red = swizzleR,
green = swizzleG,
blue = swizzleB,
alpha = swizzleA
};
}
public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
{
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
2023-07-28 20:23:13 +00:00
2023-07-28 02:54:24 +00:00
if (destination is Texture destinationTexture)
{
2024-05-30 12:20:37 +00:00
if (destinationTexture.Info.Target == Target.Texture3D)
{
blitCommandEncoder.CopyFromTexture(
_mtlTexture,
0,
(ulong)firstLevel,
new MTLOrigin { x = 0, y = 0, z = (ulong)firstLayer },
new MTLSize { width = (ulong)Math.Min(Info.Width, destinationTexture.Info.Width), height = (ulong)Math.Min(Info.Height, destinationTexture.Info.Height), depth = 1},
destinationTexture._mtlTexture,
0,
(ulong)firstLevel,
new MTLOrigin { x = 0, y = 0, z = (ulong)firstLayer });
}
else
{
blitCommandEncoder.CopyFromTexture(
_mtlTexture,
(ulong)firstLayer,
(ulong)firstLevel,
destinationTexture._mtlTexture,
(ulong)firstLayer,
(ulong)firstLevel,
_mtlTexture.ArrayLength,
_mtlTexture.MipmapLevelCount);
}
2023-07-28 02:54:24 +00:00
}
}
public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
{
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
2023-07-28 20:23:13 +00:00
2023-07-28 02:54:24 +00:00
if (destination is Texture destinationTexture)
{
2024-05-30 12:20:37 +00:00
if (destinationTexture.Info.Target == Target.Texture3D)
{
blitCommandEncoder.CopyFromTexture(
_mtlTexture,
0,
(ulong)srcLevel,
new MTLOrigin { x = 0, y = 0, z = (ulong)srcLayer },
new MTLSize { width = (ulong)Math.Min(Info.Width, destinationTexture.Info.Width), height = (ulong)Math.Min(Info.Height, destinationTexture.Info.Height), depth = 1},
destinationTexture._mtlTexture,
0,
(ulong)dstLevel,
new MTLOrigin { x = 0, y = 0, z = (ulong)dstLayer });
}
else
{
blitCommandEncoder.CopyFromTexture(
_mtlTexture,
(ulong)srcLayer,
(ulong)srcLevel,
destinationTexture._mtlTexture,
(ulong)dstLayer,
(ulong)dstLevel,
_mtlTexture.ArrayLength,
_mtlTexture.MipmapLevelCount);
}
2023-07-28 02:54:24 +00:00
}
}
public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
{
2024-05-27 14:38:00 +00:00
_pipeline.BlitColor(this, destination, srcRegion, dstRegion, linearFilter);
}
public void CopyTo(BufferRange range, int layer, int level, int stride)
{
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
var cbs = _pipeline.CurrentCommandBuffer;
int outSize = Info.GetMipSize(level);
2023-07-29 03:50:00 +00:00
ulong bytesPerRow = (ulong)Info.GetMipStride(level);
ulong bytesPerImage = 0;
if (_mtlTexture.TextureType == MTLTextureType.Type3D)
2023-07-29 03:50:00 +00:00
{
bytesPerImage = bytesPerRow * (ulong)Info.Height;
}
var autoBuffer = _renderer.BufferManager.GetBuffer(range.Handle, true);
var mtlBuffer = autoBuffer.Get(cbs, range.Offset, outSize).Value;
2023-07-29 03:50:00 +00:00
blitCommandEncoder.CopyFromTexture(
_mtlTexture,
2023-07-29 03:50:00 +00:00
(ulong)layer,
(ulong)level,
new MTLOrigin(),
new MTLSize { width = _mtlTexture.Width, height = _mtlTexture.Height, depth = _mtlTexture.Depth },
2023-07-29 03:50:00 +00:00
mtlBuffer,
(ulong)range.Offset,
bytesPerRow,
bytesPerImage);
}
public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
{
return new Texture(_device, _renderer, _pipeline, info, _mtlTexture, firstLayer, firstLevel);
}
public PinnedSpan<byte> GetData()
{
throw new NotImplementedException();
}
public PinnedSpan<byte> GetData(int layer, int level)
{
2024-05-25 09:03:45 +00:00
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
ulong bytesPerRow = (ulong)Info.GetMipStride(level);
ulong length = bytesPerRow * (ulong)Info.Height;
ulong bytesPerImage = 0;
if (_mtlTexture.TextureType == MTLTextureType.Type3D)
2024-05-25 09:03:45 +00:00
{
bytesPerImage = length;
}
unsafe
{
2024-05-25 09:03:45 +00:00
var mtlBuffer = _device.NewBuffer(length, MTLResourceOptions.ResourceStorageModeShared);
blitCommandEncoder.CopyFromTexture(
_mtlTexture,
2024-05-25 09:03:45 +00:00
(ulong)layer,
(ulong)level,
new MTLOrigin(),
new MTLSize { width = _mtlTexture.Width, height = _mtlTexture.Height, depth = _mtlTexture.Depth },
2024-05-25 09:03:45 +00:00
mtlBuffer,
0,
bytesPerRow,
bytesPerImage
);
2024-05-25 12:48:07 +00:00
return new PinnedSpan<byte>(mtlBuffer.Contents.ToPointer(), (int)length, () => mtlBuffer.Dispose());
2024-05-25 09:03:45 +00:00
}
}
2024-04-22 21:44:55 +00:00
public unsafe void SetData(IMemoryOwner<byte> data)
{
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
2023-08-02 19:57:57 +00:00
2024-04-22 21:44:55 +00:00
var dataSpan = data.Memory.Span;
2023-08-02 19:57:57 +00:00
var mtlBuffer = _device.NewBuffer((ulong)dataSpan.Length, MTLResourceOptions.ResourceStorageModeShared);
var bufferSpan = new Span<byte>(mtlBuffer.Contents.ToPointer(), dataSpan.Length);
dataSpan.CopyTo(bufferSpan);
int width = Info.Width;
int height = Info.Height;
int depth = Info.Depth;
int levels = Info.GetLevelsClamped();
2024-05-29 22:52:29 +00:00
int layers = Info.GetLayers();
2024-03-20 02:14:17 +00:00
bool is3D = Info.Target == Target.Texture3D;
2023-08-02 19:57:57 +00:00
int offset = 0;
for (int level = 0; level < levels; level++)
{
2024-05-29 22:52:29 +00:00
int mipSize = Info.GetMipSize2D(level);
2023-08-02 19:57:57 +00:00
int endOffset = offset + mipSize;
if ((uint)endOffset > (uint)dataSpan.Length)
{
return;
}
2024-05-29 22:52:29 +00:00
for (int layer = 0; layer < layers; layer++)
{
blitCommandEncoder.CopyFromBuffer(
mtlBuffer,
(ulong)offset,
(ulong)Info.GetMipStride(level),
(ulong)mipSize,
new MTLSize { width = (ulong)width, height = (ulong)height, depth = is3D ? (ulong)depth : 1 },
_mtlTexture,
(ulong)layer,
(ulong)level,
new MTLOrigin()
);
offset += mipSize;
}
2023-08-02 19:57:57 +00:00
width = Math.Max(1, width >> 1);
height = Math.Max(1, height >> 1);
2024-03-20 02:14:17 +00:00
if (is3D)
{
depth = Math.Max(1, depth >> 1);
}
2023-08-02 19:57:57 +00:00
}
2024-05-25 13:23:13 +00:00
// Cleanup
mtlBuffer.Dispose();
}
2024-04-22 21:44:55 +00:00
public void SetData(IMemoryOwner<byte> data, int layer, int level)
{
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
2023-07-29 03:35:55 +00:00
ulong bytesPerRow = (ulong)Info.GetMipStride(level);
ulong bytesPerImage = 0;
if (_mtlTexture.TextureType == MTLTextureType.Type3D)
2023-07-29 03:35:55 +00:00
{
bytesPerImage = bytesPerRow * (ulong)Info.Height;
}
unsafe
{
2024-04-22 21:44:55 +00:00
var dataSpan = data.Memory.Span;
2023-07-29 03:35:55 +00:00
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,
new MTLSize { width = _mtlTexture.Width, height = _mtlTexture.Height, depth = _mtlTexture.Depth },
_mtlTexture,
2023-07-29 03:35:55 +00:00
(ulong)layer,
(ulong)level,
new MTLOrigin()
);
2024-05-25 13:23:13 +00:00
// Cleanup
mtlBuffer.Dispose();
2023-07-29 03:35:55 +00:00
}
}
2024-04-22 21:44:55 +00:00
public void SetData(IMemoryOwner<byte> data, int layer, int level, Rectangle<int> region)
{
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
2023-07-29 03:33:09 +00:00
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)
2023-07-28 19:04:35 +00:00
{
bytesPerImage = bytesPerRow * (ulong)Info.Height;
}
unsafe
2023-07-28 15:55:52 +00:00
{
2024-04-22 21:44:55 +00:00
var dataSpan = data.Memory.Span;
2023-07-29 03:33:09 +00:00
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 },
_mtlTexture,
2023-07-29 03:33:09 +00:00
(ulong)layer,
(ulong)level,
new MTLOrigin { x = (ulong)region.X, y = (ulong)region.Y }
);
2024-05-25 13:23:13 +00:00
// Cleanup
mtlBuffer.Dispose();
2023-07-28 15:55:52 +00:00
}
}
public void SetStorage(BufferRange buffer)
{
throw new NotImplementedException();
}
}
2024-03-18 18:51:44 +00:00
}