From e2c397355c05a2efe29a1cd7e00cc963a0031bd8 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 1 Jan 2022 15:48:43 -0400 Subject: [PATCH] metal: implement copyBufferToTexture and copyTextureToBuffer. --- src/modules/graphics/metal/Texture.mm | 62 ++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/src/modules/graphics/metal/Texture.mm b/src/modules/graphics/metal/Texture.mm index 8b4c96571..cbc2b0043 100644 --- a/src/modules/graphics/metal/Texture.mm +++ b/src/modules/graphics/metal/Texture.mm @@ -259,14 +259,64 @@ void Texture::readbackImageData(love::image::ImageData *imagedata, int slice, in }} void Texture::copyFromBuffer(love::graphics::Buffer *source, size_t sourceoffset, int sourcewidth, size_t size, int slice, int mipmap, const Rect &rect) -{ - // TODO -} +{ @autoreleasepool { + id encoder = Graphics::getInstance()->useBlitEncoder(); + id buffer = (__bridge id)(void *) source->getHandle(); + + size_t rowSize = 0; + if (isCompressed()) + rowSize = getPixelFormatCompressedBlockRowSize(format, sourcewidth); + else + rowSize = getPixelFormatUncompressedRowSize(format, sourcewidth); + + int z = texType == TEXTURE_VOLUME ? slice : 0; + + MTLBlitOption options = MTLBlitOptionNone; + + if (isPixelFormatDepthStencil(format)) + options = MTLBlitOptionDepthFromDepthStencil; + + [encoder copyFromBuffer:buffer + sourceOffset:sourceoffset + sourceBytesPerRow:rowSize + sourceBytesPerImage:size + sourceSize:MTLSizeMake(rect.w, rect.h, 1) + toTexture:texture + destinationSlice:texType == TEXTURE_VOLUME ? 0 : slice + destinationLevel:mipmap + destinationOrigin:MTLOriginMake(rect.x, rect.y, z) + options:options]; +}} void Texture::copyToBuffer(love::graphics::Buffer *dest, int slice, int mipmap, const Rect &rect, size_t destoffset, int destwidth, size_t size) -{ - // TODO -} +{ @autoreleasepool { + id encoder = Graphics::getInstance()->useBlitEncoder(); + id buffer = (__bridge id)(void *) dest->getHandle(); + + size_t rowSize = 0; + if (isCompressed()) + rowSize = getPixelFormatCompressedBlockRowSize(format, destwidth); + else + rowSize = getPixelFormatUncompressedRowSize(format, destwidth); + + int z = texType == TEXTURE_VOLUME ? slice : 0; + + MTLBlitOption options = MTLBlitOptionNone; + + if (isPixelFormatDepthStencil(format)) + options = MTLBlitOptionDepthFromDepthStencil; + + [encoder copyFromTexture:texture + sourceSlice:texType == TEXTURE_VOLUME ? 0 : slice + sourceLevel:mipmap + sourceOrigin:MTLOriginMake(rect.x, rect.y, z) + sourceSize:MTLSizeMake(rect.w, rect.h, 1) + toBuffer:buffer + destinationOffset:destoffset + destinationBytesPerRow:rowSize + destinationBytesPerImage:size + options:options]; +}} void Texture::setSamplerState(const SamplerState &s) { @autoreleasepool {