2024-06-30 16:23:53 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2024-08-31 20:42:56 +00:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2024-05-14 14:36:01 +00:00
|
|
|
using SharpMetal.Foundation;
|
2023-07-28 01:51:20 +00:00
|
|
|
using SharpMetal.Metal;
|
2024-08-31 20:42:56 +00:00
|
|
|
using System;
|
2024-04-22 21:44:55 +00:00
|
|
|
using System.Buffers;
|
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")]
|
2024-06-20 23:21:06 +00:00
|
|
|
class Texture : TextureBase, ITexture
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2024-06-19 22:13:55 +00:00
|
|
|
public Texture(MTLDevice device, MetalRenderer renderer, Pipeline pipeline, TextureCreateInfo info) : base(device, renderer, pipeline, info)
|
2023-07-28 01:51:20 +00:00
|
|
|
{
|
2024-06-28 20:14:53 +00:00
|
|
|
MTLPixelFormat pixelFormat = FormatTable.GetFormat(Info.Format);
|
|
|
|
|
2024-03-20 03:12:28 +00:00
|
|
|
var descriptor = new MTLTextureDescriptor
|
|
|
|
{
|
2024-06-28 20:14:53 +00:00
|
|
|
PixelFormat = pixelFormat,
|
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);
|
2023-07-28 01:51:20 +00:00
|
|
|
|
2024-05-29 15:21:59 +00:00
|
|
|
_mtlTexture = _device.NewTexture(descriptor);
|
2024-07-02 16:52:53 +00:00
|
|
|
|
2024-06-28 20:14:53 +00:00
|
|
|
MtlFormat = pixelFormat;
|
2024-06-29 18:07:07 +00:00
|
|
|
descriptor.Dispose();
|
2023-07-28 01:51:20 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 22:13:55 +00:00
|
|
|
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;
|
2024-06-25 13:25:31 +00:00
|
|
|
slices.length = textureType == MTLTextureType.Type3D ? 1 : (ulong)info.GetDepthOrLayers();
|
2024-05-14 14:36:01 +00:00
|
|
|
|
2024-05-14 15:57:42 +00:00
|
|
|
var swizzle = GetSwizzle(info, pixelFormat);
|
|
|
|
|
2024-05-29 15:21:59 +00:00
|
|
|
_mtlTexture = sourceTexture.NewTextureView(pixelFormat, textureType, levels, slices, swizzle);
|
2024-06-28 20:14:53 +00:00
|
|
|
MtlFormat = pixelFormat;
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-08-31 20:42:56 +00:00
|
|
|
public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
|
|
|
|
{
|
2024-06-30 16:23:53 +00:00
|
|
|
if (!_renderer.CommandBufferPool.OwnedByCurrentThread)
|
|
|
|
{
|
|
|
|
Logger.Warning?.PrintMsg(LogClass.Gpu, "Metal doesn't currently support scaled blit on background thread.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-03 20:47:10 +00:00
|
|
|
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 },
|
2024-07-02 16:52:53 +00:00
|
|
|
new MTLSize { width = (ulong)Math.Min(Info.Width, destinationTexture.Info.Width), height = (ulong)Math.Min(Info.Height, destinationTexture.Info.Height), depth = 1 },
|
2024-05-30 12:20:37 +00:00
|
|
|
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
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
|
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
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 },
|
2024-07-02 16:52:53 +00:00
|
|
|
new MTLSize { width = (ulong)Math.Min(Info.Width, destinationTexture.Info.Width), height = (ulong)Math.Min(Info.Height, destinationTexture.Info.Height), depth = 1 },
|
2024-05-30 12:20:37 +00:00
|
|
|
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
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
|
|
|
|
{
|
2024-06-29 21:54:28 +00:00
|
|
|
var dst = (Texture)destination;
|
|
|
|
bool isDepthOrStencil = dst.Info.Format.IsDepthOrStencil();
|
|
|
|
|
|
|
|
_pipeline.Blit(this, destination, srcRegion, dstRegion, isDepthOrStencil, linearFilter);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(BufferRange range, int layer, int level, int stride)
|
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
|
2024-06-20 23:21:06 +00:00
|
|
|
var cbs = _pipeline.Cbs;
|
2024-06-19 22:13:55 +00:00
|
|
|
|
|
|
|
int outSize = Info.GetMipSize(level);
|
2023-07-29 03:50:00 +00:00
|
|
|
|
|
|
|
ulong bytesPerRow = (ulong)Info.GetMipStride(level);
|
|
|
|
ulong bytesPerImage = 0;
|
2024-05-29 15:21:59 +00:00
|
|
|
if (_mtlTexture.TextureType == MTLTextureType.Type3D)
|
2023-07-29 03:50:00 +00:00
|
|
|
{
|
|
|
|
bytesPerImage = bytesPerRow * (ulong)Info.Height;
|
|
|
|
}
|
|
|
|
|
2024-06-19 22:13:55 +00:00
|
|
|
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(
|
2024-05-29 15:21:59 +00:00
|
|
|
_mtlTexture,
|
2023-07-29 03:50:00 +00:00
|
|
|
(ulong)layer,
|
|
|
|
(ulong)level,
|
|
|
|
new MTLOrigin(),
|
2024-05-29 15:21:59 +00:00
|
|
|
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);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
|
|
|
|
{
|
2024-06-19 22:13:55 +00:00
|
|
|
return new Texture(_device, _renderer, _pipeline, info, _mtlTexture, firstLayer, firstLevel);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
private int GetBufferDataLength(int size)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2024-06-30 16:23:53 +00:00
|
|
|
// TODO: D32S8 conversion
|
2024-06-21 15:32:31 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
return size;
|
|
|
|
}
|
2024-06-21 15:32:31 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
private ReadOnlySpan<byte> GetDataFromBuffer(ReadOnlySpan<byte> storage, int size, Span<byte> output)
|
|
|
|
{
|
|
|
|
// TODO: D32S8 conversion
|
|
|
|
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyFromOrToBuffer(
|
|
|
|
CommandBufferScoped cbs,
|
|
|
|
MTLBuffer buffer,
|
|
|
|
MTLTexture image,
|
|
|
|
int size,
|
|
|
|
bool to,
|
|
|
|
int dstLayer,
|
|
|
|
int dstLevel,
|
|
|
|
int dstLayers,
|
|
|
|
int dstLevels,
|
|
|
|
bool singleSlice,
|
|
|
|
int offset = 0,
|
|
|
|
int stride = 0)
|
|
|
|
{
|
|
|
|
MTLBlitCommandEncoder blitCommandEncoder = cbs.Encoders.EnsureBlitEncoder();
|
2024-06-21 15:32:31 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
bool is3D = Info.Target == Target.Texture3D;
|
|
|
|
int width = Math.Max(1, Info.Width >> dstLevel);
|
|
|
|
int height = Math.Max(1, Info.Height >> dstLevel);
|
|
|
|
int depth = is3D && !singleSlice ? Math.Max(1, Info.Depth >> dstLevel) : 1;
|
|
|
|
int layers = dstLayers;
|
|
|
|
int levels = dstLevels;
|
|
|
|
|
|
|
|
for (int oLevel = 0; oLevel < levels; oLevel++)
|
2024-06-21 15:32:31 +00:00
|
|
|
{
|
2024-06-30 16:23:53 +00:00
|
|
|
int level = oLevel + dstLevel;
|
|
|
|
int mipSize = Info.GetMipSize2D(level);
|
|
|
|
|
|
|
|
int mipSizeLevel = GetBufferDataLength(is3D && !singleSlice
|
|
|
|
? Info.GetMipSize(level)
|
|
|
|
: mipSize * dstLayers);
|
2024-06-21 15:32:31 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
int endOffset = offset + mipSizeLevel;
|
2024-06-21 15:32:31 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
if ((uint)endOffset > (uint)size)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2024-06-21 15:32:31 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
for (int oLayer = 0; oLayer < layers; oLayer++)
|
2024-06-21 15:32:31 +00:00
|
|
|
{
|
2024-06-30 16:23:53 +00:00
|
|
|
int layer = !is3D ? dstLayer + oLayer : 0;
|
|
|
|
int z = is3D ? dstLayer + oLayer : 0;
|
2024-06-21 15:32:31 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
if (to)
|
2024-06-21 15:32:31 +00:00
|
|
|
{
|
|
|
|
blitCommandEncoder.CopyFromTexture(
|
2024-06-30 16:23:53 +00:00
|
|
|
image,
|
2024-06-21 15:32:31 +00:00
|
|
|
(ulong)layer,
|
|
|
|
(ulong)level,
|
2024-06-30 16:23:53 +00:00
|
|
|
new MTLOrigin { z = (ulong)z },
|
|
|
|
new MTLSize { width = (ulong)width, height = (ulong)height, depth = 1 },
|
|
|
|
buffer,
|
2024-06-21 15:32:31 +00:00
|
|
|
(ulong)offset,
|
|
|
|
(ulong)Info.GetMipStride(level),
|
|
|
|
(ulong)mipSize
|
|
|
|
);
|
|
|
|
}
|
2024-06-30 16:23:53 +00:00
|
|
|
else
|
2024-06-21 15:32:31 +00:00
|
|
|
{
|
2024-06-30 16:23:53 +00:00
|
|
|
blitCommandEncoder.CopyFromBuffer(
|
|
|
|
buffer,
|
|
|
|
(ulong)offset,
|
|
|
|
(ulong)Info.GetMipStride(level),
|
|
|
|
(ulong)mipSize,
|
|
|
|
new MTLSize { width = (ulong)width, height = (ulong)height, depth = 1 },
|
|
|
|
image,
|
|
|
|
(ulong)(layer + oLayer),
|
|
|
|
(ulong)level,
|
|
|
|
new MTLOrigin { z = (ulong)z }
|
|
|
|
);
|
2024-06-21 15:32:31 +00:00
|
|
|
}
|
2024-06-30 16:23:53 +00:00
|
|
|
|
|
|
|
offset += mipSize;
|
2024-06-21 15:32:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
width = Math.Max(1, width >> 1);
|
|
|
|
height = Math.Max(1, height >> 1);
|
2024-06-21 15:32:31 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
if (Info.Target == Target.Texture3D)
|
|
|
|
{
|
|
|
|
depth = Math.Max(1, depth >> 1);
|
|
|
|
}
|
2024-06-21 15:32:31 +00:00
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
private ReadOnlySpan<byte> GetData(CommandBufferPool cbp, PersistentFlushBuffer flushBuffer)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2024-06-30 16:23:53 +00:00
|
|
|
int size = 0;
|
2024-05-25 09:03:45 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
for (int level = 0; level < Info.Levels; level++)
|
|
|
|
{
|
|
|
|
size += Info.GetMipSize(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
size = GetBufferDataLength(size);
|
|
|
|
|
|
|
|
Span<byte> result = flushBuffer.GetTextureData(cbp, this, size);
|
|
|
|
|
|
|
|
return GetDataFromBuffer(result, size, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ReadOnlySpan<byte> GetData(CommandBufferPool cbp, PersistentFlushBuffer flushBuffer, int layer, int level)
|
|
|
|
{
|
|
|
|
int size = GetBufferDataLength(Info.GetMipSize(level));
|
|
|
|
|
|
|
|
Span<byte> result = flushBuffer.GetTextureData(cbp, this, size, layer, level);
|
|
|
|
|
|
|
|
return GetDataFromBuffer(result, size, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PinnedSpan<byte> GetData()
|
|
|
|
{
|
|
|
|
BackgroundResource resources = _renderer.BackgroundResources.Get();
|
|
|
|
|
|
|
|
if (_renderer.CommandBufferPool.OwnedByCurrentThread)
|
2024-05-25 09:03:45 +00:00
|
|
|
{
|
2024-06-30 16:23:53 +00:00
|
|
|
_renderer.FlushAllCommands();
|
|
|
|
|
|
|
|
return PinnedSpan<byte>.UnsafeFromSpan(GetData(_renderer.CommandBufferPool, resources.GetFlushBuffer()));
|
2024-05-25 09:03:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
return PinnedSpan<byte>.UnsafeFromSpan(GetData(resources.GetPool(), resources.GetFlushBuffer()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public PinnedSpan<byte> GetData(int layer, int level)
|
|
|
|
{
|
|
|
|
BackgroundResource resources = _renderer.BackgroundResources.Get();
|
|
|
|
|
|
|
|
if (_renderer.CommandBufferPool.OwnedByCurrentThread)
|
2024-05-25 09:03:45 +00:00
|
|
|
{
|
2024-06-30 16:23:53 +00:00
|
|
|
_renderer.FlushAllCommands();
|
2024-06-19 22:13:55 +00:00
|
|
|
|
2024-06-30 16:23:53 +00:00
|
|
|
return PinnedSpan<byte>.UnsafeFromSpan(GetData(_renderer.CommandBufferPool, resources.GetFlushBuffer(), layer, level));
|
2024-05-25 09:03:45 +00:00
|
|
|
}
|
2024-06-30 16:23:53 +00:00
|
|
|
|
|
|
|
return PinnedSpan<byte>.UnsafeFromSpan(GetData(resources.GetPool(), resources.GetFlushBuffer(), layer, level));
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 13:25:31 +00:00
|
|
|
public void SetData(IMemoryOwner<byte> data)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
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;
|
2024-06-25 13:25:31 +00:00
|
|
|
|
|
|
|
var buffer = _renderer.BufferManager.Create(dataSpan.Length);
|
|
|
|
buffer.SetDataUnchecked(0, dataSpan);
|
|
|
|
var mtlBuffer = buffer.GetBuffer(false).Get(_pipeline.Cbs).Value;
|
2023-08-02 19:57:57 +00:00
|
|
|
|
|
|
|
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
|
2024-06-25 13:25:31 +00:00
|
|
|
buffer.Dispose();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 21:44:55 +00:00
|
|
|
public void SetData(IMemoryOwner<byte> data, int layer, int level)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
|
2023-07-29 03:35:55 +00:00
|
|
|
|
|
|
|
ulong bytesPerRow = (ulong)Info.GetMipStride(level);
|
|
|
|
ulong bytesPerImage = 0;
|
2024-05-29 15:21:59 +00:00
|
|
|
if (_mtlTexture.TextureType == MTLTextureType.Type3D)
|
2023-07-29 03:35:55 +00:00
|
|
|
{
|
|
|
|
bytesPerImage = bytesPerRow * (ulong)Info.Height;
|
|
|
|
}
|
|
|
|
|
2024-06-25 13:25:31 +00:00
|
|
|
var dataSpan = data.Memory.Span;
|
2023-07-29 03:35:55 +00:00
|
|
|
|
2024-06-25 13:25:31 +00:00
|
|
|
var buffer = _renderer.BufferManager.Create(dataSpan.Length);
|
|
|
|
buffer.SetDataUnchecked(0, dataSpan);
|
|
|
|
var mtlBuffer = buffer.GetBuffer(false).Get(_pipeline.Cbs).Value;
|
2024-05-25 13:23:13 +00:00
|
|
|
|
2024-06-25 13:25:31 +00:00
|
|
|
blitCommandEncoder.CopyFromBuffer(
|
|
|
|
mtlBuffer,
|
|
|
|
0,
|
|
|
|
bytesPerRow,
|
|
|
|
bytesPerImage,
|
|
|
|
new MTLSize { width = _mtlTexture.Width, height = _mtlTexture.Height, depth = _mtlTexture.Depth },
|
|
|
|
_mtlTexture,
|
|
|
|
(ulong)layer,
|
|
|
|
(ulong)level,
|
|
|
|
new MTLOrigin()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
buffer.Dispose();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 21:44:55 +00:00
|
|
|
public void SetData(IMemoryOwner<byte> data, int layer, int level, Rectangle<int> region)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
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;
|
2024-05-29 15:21:59 +00:00
|
|
|
if (_mtlTexture.TextureType == MTLTextureType.Type3D)
|
2023-07-28 19:04:35 +00:00
|
|
|
{
|
|
|
|
bytesPerImage = bytesPerRow * (ulong)Info.Height;
|
|
|
|
}
|
|
|
|
|
2024-06-25 13:25:31 +00:00
|
|
|
var dataSpan = data.Memory.Span;
|
2023-07-29 03:33:09 +00:00
|
|
|
|
2024-06-25 13:25:31 +00:00
|
|
|
var buffer = _renderer.BufferManager.Create(dataSpan.Length);
|
|
|
|
buffer.SetDataUnchecked(0, dataSpan);
|
|
|
|
var mtlBuffer = buffer.GetBuffer(false).Get(_pipeline.Cbs).Value;
|
2024-05-25 13:23:13 +00:00
|
|
|
|
2024-06-25 13:25:31 +00:00
|
|
|
blitCommandEncoder.CopyFromBuffer(
|
|
|
|
mtlBuffer,
|
|
|
|
0,
|
|
|
|
bytesPerRow,
|
|
|
|
bytesPerImage,
|
|
|
|
new MTLSize { width = (ulong)region.Width, height = (ulong)region.Height, depth = 1 },
|
|
|
|
_mtlTexture,
|
|
|
|
(ulong)layer,
|
|
|
|
(ulong)level,
|
|
|
|
new MTLOrigin { x = (ulong)region.X, y = (ulong)region.Y }
|
|
|
|
);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
buffer.Dispose();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetStorage(BufferRange buffer)
|
|
|
|
{
|
2024-05-29 15:21:59 +00:00
|
|
|
throw new NotImplementedException();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-18 18:51:44 +00:00
|
|
|
}
|