Repository navigation
Support texture readback through copyTextureRegion - #114
Open
IsaacMarovitz wants to merge 1 commit into
Open
IsaacMarovitz wants to merge 1 commit into
IsaacMarovitz wants to merge 1 commit into
Conversation
squidbus
reviewed
Sep 20, 2026
squidbus
left a comment
Contributor
There was a problem hiding this comment.
I can't vouch for the D3D12 but the others look okay to me.
dcvz
requested changes
Oct 12, 2026
dcvz
left a comment
Contributor
There was a problem hiding this comment.
One thing I noticed is that it ignores dstX/Y/Z and sizes the copy from the footprint instead of srcBox which is not how D3D12 does it.
| // Calculate block size based on source texture format | ||
| const uint32_t blockWidth = RenderFormatBlockWidth(srcTexture->desc.format); | ||
|
|
||
| const MTL::Size size = { dstLocation.placedFootprint.width, dstLocation.placedFootprint.height, dstLocation.placedFootprint.depth }; |
Contributor
There was a problem hiding this comment.
This takes the copy size from the footprint, D3D12 uses srcBox or the whole subresource when there's no box:
Suggested change
| const MTL::Size size = { dstLocation.placedFootprint.width, dstLocation.placedFootprint.height, dstLocation.placedFootprint.depth }; | |
| const uint32_t mipLevel = srcLocation.subresource.mipLevel; | |
| const MTL::Size size = (srcBox != nullptr) | |
| ? MTL::Size { NS::UInteger(srcBox->right - srcBox->left), NS::UInteger(srcBox->bottom - srcBox->top), NS::UInteger(srcBox->back - srcBox->front) } | |
| : MTL::Size { std::max(srcTexture->desc.width >> mipLevel, 1U), std::max(srcTexture->desc.height >> mipLevel, 1U), std::max(srcTexture->desc.depth >> mipLevel, 1U) }; |
| const uint32_t horizontalBlocks = (dstLocation.placedFootprint.rowWidth + blockWidth - 1) / blockWidth; | ||
| const uint32_t verticalBlocks = (dstLocation.placedFootprint.height + blockWidth - 1) / blockWidth; | ||
| const uint32_t bytesPerRow = horizontalBlocks * RenderFormatSize(srcTexture->desc.format); | ||
| const uint32_t bytesPerImage = bytesPerRow * verticalBlocks; |
Contributor
There was a problem hiding this comment.
dstX/Y/Z place the region inside the footprint, so they need to go into the buffer offset:
Suggested change
| const uint32_t bytesPerImage = bytesPerRow * verticalBlocks; | |
| const uint32_t bytesPerImage = bytesPerRow * verticalBlocks; | |
| const uint64_t dstOffset = dstLocation.placedFootprint.offset + dstZ * bytesPerImage + (dstY / blockWidth) * bytesPerRow + (dstX / blockWidth) * RenderFormatSize(srcTexture->desc.format); |
| srcOrigin, | ||
| size, | ||
| dstBuffer->mtl, | ||
| dstLocation.placedFootprint.offset, |
Contributor
There was a problem hiding this comment.
Suggested change
| dstLocation.placedFootprint.offset, | |
| dstOffset, |
Comment on lines
+3237
to
+3238
| VkBufferImageCopy imageCopy = {}; | ||
| imageCopy.bufferOffset = dstLocation.placedFootprint.offset; |
Contributor
There was a problem hiding this comment.
Same two things on Vulkan, the offset:
Suggested change
| VkBufferImageCopy imageCopy = {}; | |
| imageCopy.bufferOffset = dstLocation.placedFootprint.offset; | |
| const uint32_t blockSize = RenderFormatSize(srcTexture->desc.format); | |
| const uint32_t bytesPerRow = ((dstLocation.placedFootprint.rowWidth + blockWidth - 1) / blockWidth) * blockSize; | |
| const uint32_t bytesPerImage = bytesPerRow * ((dstLocation.placedFootprint.height + blockWidth - 1) / blockWidth); | |
| const uint32_t mipLevel = srcLocation.subresource.mipLevel; | |
| VkBufferImageCopy imageCopy = {}; | |
| imageCopy.bufferOffset = dstLocation.placedFootprint.offset + dstZ * bytesPerImage + (dstY / blockWidth) * bytesPerRow + (dstX / blockWidth) * blockSize; |
Comment on lines
+3248
to
+3250
| imageCopy.imageExtent.width = dstLocation.placedFootprint.width; | ||
| imageCopy.imageExtent.height = dstLocation.placedFootprint.height; | ||
| imageCopy.imageExtent.depth = dstLocation.placedFootprint.depth; |
Contributor
There was a problem hiding this comment.
and the size:
Suggested change
| imageCopy.imageExtent.width = dstLocation.placedFootprint.width; | |
| imageCopy.imageExtent.height = dstLocation.placedFootprint.height; | |
| imageCopy.imageExtent.depth = dstLocation.placedFootprint.depth; | |
| imageCopy.imageExtent.width = srcBox != nullptr ? uint32_t(srcBox->right - srcBox->left) : std::max(srcTexture->desc.width >> mipLevel, 1U); | |
| imageCopy.imageExtent.height = srcBox != nullptr ? uint32_t(srcBox->bottom - srcBox->top) : std::max(srcTexture->desc.height >> mipLevel, 1U); | |
| imageCopy.imageExtent.depth = srcBox != nullptr ? uint32_t(srcBox->back - srcBox->front) : std::max(srcTexture->desc.depth >> mipLevel, 1U); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explicitly adds support for copyTextureRegion calls in the other direction.