summaryrefslogtreecommitdiff
path: root/tools/gfx/metal/metal-device.cpp
diff options
context:
space:
mode:
authorskallweitNV <64953474+skallweitNV@users.noreply.github.com>2024-06-11 21:33:02 +0200
committerGitHub <noreply@github.com>2024-06-11 12:33:02 -0700
commit7e796692065060dea34b9e5b7eb224be444f5dee (patch)
treec858c13c64789bf263234c15ae6cc61f47fde937 /tools/gfx/metal/metal-device.cpp
parent6909d65c77bb4e7c9cfb281bd1684a58d5f8b94d (diff)
[gfx] Metal improvements (#4337)
* fix binding resources for render pass * compute vertex buffer binding offset based on root layout * fix result code * cleanup * implement readTextureResource * more getNativeHandle --------- Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tools/gfx/metal/metal-device.cpp')
-rw-r--r--tools/gfx/metal/metal-device.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/tools/gfx/metal/metal-device.cpp b/tools/gfx/metal/metal-device.cpp
index 1be9d1019..4a1c02480 100644
--- a/tools/gfx/metal/metal-device.cpp
+++ b/tools/gfx/metal/metal-device.cpp
@@ -189,7 +189,53 @@ SlangResult DeviceImpl::readTextureResource(
{
AUTORELEASEPOOL
+ TextureResourceImpl* textureImpl = static_cast<TextureResourceImpl*>(texture);
+
+ if (textureImpl->getDesc()->sampleDesc.numSamples > 1)
+ {
return SLANG_E_NOT_IMPLEMENTED;
+ }
+
+ NS::SharedPtr<MTL::Texture> srcTexture = textureImpl->m_texture;
+
+ const ITextureResource::Desc& desc = *textureImpl->getDesc();
+ Count width = Math::Max(desc.size.width, 1);
+ Count height = Math::Max(desc.size.height, 1);
+ Count depth = Math::Max(desc.size.depth, 1);
+ FormatInfo formatInfo;
+ gfxGetFormatInfo(desc.format, &formatInfo);
+ Size bytesPerPixel = formatInfo.blockSizeInBytes / formatInfo.pixelsPerBlock;
+ Size bytesPerRow = Size(width) * bytesPerPixel;
+ Size bytesPerSlice = Size(height) * bytesPerRow;
+ Size bufferSize = Size(depth) * bytesPerSlice;
+ if (outRowPitch)
+ *outRowPitch = bytesPerRow;
+ if (outPixelSize)
+ *outPixelSize = bytesPerPixel;
+
+ // create staging buffer
+ NS::SharedPtr<MTL::Buffer> stagingBuffer = NS::TransferPtr(m_device->newBuffer(bufferSize, MTL::StorageModeShared));
+ if (!stagingBuffer)
+ {
+ return SLANG_FAIL;
+ }
+
+ MTL::CommandBuffer* commandBuffer = m_commandQueue->commandBuffer();
+ MTL::BlitCommandEncoder* encoder = commandBuffer->blitCommandEncoder();
+ encoder->copyFromTexture(
+ srcTexture.get(), 0, 0, MTL::Origin(0, 0, 0), MTL::Size(width, height, depth),
+ stagingBuffer.get(), 0, bytesPerRow, bytesPerSlice);
+ encoder->endEncoding();
+ commandBuffer->commit();
+ commandBuffer->waitUntilCompleted();
+
+ List<uint8_t> blobData;
+ blobData.setCount(bufferSize);
+ ::memcpy(blobData.getBuffer(), stagingBuffer->contents(), bufferSize);
+ auto blob = ListBlob::moveCreate(blobData);
+
+ returnComPtr(outBlob, blob);
+ return SLANG_OK;
}
SlangResult DeviceImpl::readBufferResource(