Skip to content

Commit 6f45c00

Browse files
committed
Fix color-depth image copies and image copies using buffer with multiple layers.
1 parent a046e77 commit 6f45c00

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

MoltenVK/MoltenVK/Commands/MVKCmdTransfer.mm

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,30 @@ static inline MTLPixelFormat getDepthStencilAspectFormat(const MTLPixelFormat fo
146146

147147
uint8_t srcPlaneIndex = MVKImage::getPlaneFromVkImageAspectFlags(vkIC.srcSubresource.aspectMask);
148148
uint8_t dstPlaneIndex = MVKImage::getPlaneFromVkImageAspectFlags(vkIC.dstSubresource.aspectMask);
149-
149+
150+
uint32_t layerCount = vkIC.srcSubresource.layerCount == VK_REMAINING_ARRAY_LAYERS ?
151+
_srcImage->getLayerCount() - vkIC.srcSubresource.baseArrayLayer :
152+
vkIC.srcSubresource.layerCount;
153+
150154
MTLPixelFormat srcMTLPixFmt = _srcImage->getMTLPixelFormat(srcPlaneIndex);
151155
bool isSrcCompressed = _srcImage->getIsCompressed();
156+
bool isSrcColor = (vkIC.srcSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) != 0;
152157
bool isSrcCombinedDepthStencilAspect = getDepthStencilAspectFormat(srcMTLPixFmt, vkIC.srcSubresource.aspectMask) != srcMTLPixFmt;
153158
bool canReinterpretSrc = _srcImage->hasPixelFormatView(srcPlaneIndex);
154159

155160
MTLPixelFormat dstMTLPixFmt = _dstImage->getMTLPixelFormat(dstPlaneIndex);
156161
bool isDstCompressed = _dstImage->getIsCompressed();
162+
bool isDstColor = (vkIC.dstSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) != 0;
157163
bool isDstCombinedDepthStencilAspect = getDepthStencilAspectFormat(dstMTLPixFmt, vkIC.dstSubresource.aspectMask) != dstMTLPixFmt;
158164
bool canReinterpretDst = _dstImage->hasPixelFormatView(dstPlaneIndex);
159165

160166
bool isEitherCompressed = isSrcCompressed || isDstCompressed;
161-
bool isOneCombinedDepthStencilAspect = isSrcCombinedDepthStencilAspect != isDstCombinedDepthStencilAspect;
167+
bool isOnlyOneColor = isSrcColor != isDstColor;
168+
bool isOnlyOneCombinedDepthStencilAspect = isSrcCombinedDepthStencilAspect != isDstCombinedDepthStencilAspect;
162169
bool canReinterpret = canReinterpretSrc || canReinterpretDst;
163170

164171
// If source and destination can't be reinterpreted to matching formats use a temporary intermediary buffer
165-
bool useTempBuffer = (srcMTLPixFmt != dstMTLPixFmt) && (isEitherCompressed || isOneCombinedDepthStencilAspect || !canReinterpret);
172+
bool useTempBuffer = (srcMTLPixFmt != dstMTLPixFmt) && (isEitherCompressed || isOnlyOneColor || isOnlyOneCombinedDepthStencilAspect || !canReinterpret);
166173

167174
if (useTempBuffer) {
168175
// Add copy from source image to temp buffer.
@@ -201,7 +208,7 @@ static inline MTLPixelFormat getDepthStencilAspectFormat(const MTLPixelFormat fo
201208

202209
size_t bytesPerRow = pixFmts->getBytesPerRow(srcMTLPixFmt, vkIC.extent.width);
203210
size_t bytesPerRegion = pixFmts->getBytesPerLayer(srcMTLPixFmt, bytesPerRow, vkIC.extent.height);
204-
tmpBuffSize += bytesPerRegion * vkIC.extent.depth;
211+
tmpBuffSize += bytesPerRegion * vkIC.extent.depth * layerCount;
205212
} else {
206213
// Map the source pixel format to the dest pixel format through a texture view on the reinterpretable texture.
207214
// If the source and dest pixel formats are the same, this will simply degenerate to the texture itself.
@@ -215,9 +222,6 @@ static inline MTLPixelFormat getDepthStencilAspectFormat(const MTLPixelFormat fo
215222
// If copies can be performed using direct texture-texture copying, do so
216223
uint32_t srcLevel = vkIC.srcSubresource.mipLevel;
217224
uint32_t srcBaseLayer = vkIC.srcSubresource.baseArrayLayer;
218-
uint32_t srcLayerCount = vkIC.srcSubresource.layerCount == VK_REMAINING_ARRAY_LAYERS ?
219-
_srcImage->getLayerCount() - srcBaseLayer :
220-
vkIC.srcSubresource.layerCount;
221225
VkExtent3D srcExtent = _srcImage->getExtent3D(srcPlaneIndex, srcLevel);
222226
uint32_t dstLevel = vkIC.dstSubresource.mipLevel;
223227
uint32_t dstBaseLayer = vkIC.dstSubresource.baseArrayLayer;
@@ -233,7 +237,7 @@ static inline MTLPixelFormat getDepthStencilAspectFormat(const MTLPixelFormat fo
233237
toTexture: dstMTLTex
234238
destinationSlice: dstBaseLayer
235239
destinationLevel: dstLevel
236-
sliceCount: srcLayerCount
240+
sliceCount: layerCount
237241
levelCount: 1];
238242
} else {
239243
MTLOrigin srcOrigin = mvkMTLOriginFromVkOffset3D(vkIC.srcOffset);
@@ -248,7 +252,7 @@ static inline MTLPixelFormat getDepthStencilAspectFormat(const MTLPixelFormat fo
248252
mvkMTLSizeFromVkExtent3D(srcExtent));
249253
srcSize.depth = 1;
250254
} else {
251-
layCnt = srcLayerCount;
255+
layCnt = layerCount;
252256
srcSize = mvkClampMTLSize(mvkMTLSizeFromVkExtent3D(vkIC.extent),
253257
srcOrigin,
254258
mvkMTLSizeFromVkExtent3D(srcExtent));

0 commit comments

Comments
 (0)