如何在 Metal 中裁剪/调整纹理数组的大小

s1d*_*dok 2 metal

假设我有一个MPSImage基于MTLTexture.

如何从中裁剪一个区域,复制所有 N 个通道,但更改“像素大小”?

war*_*enm 5

我将只解决裁剪案例,因为调整大小案例涉及重新采样并且稍微复杂一些。让我知道你是否真的需要那个。

假设您的源MPSImage是一个 128x128 像素的 12 个特征通道(3 个切片)图像,您的目标图像是一个 64x64 像素的 8 个特征通道图像(2 个切片),并且您想要复制右下角的 64x64 区域源的最后两个切片到目标。

我知道没有 API 允许您一次从/向数组纹理的多个切片复制,因此您需要发出多个 blit 命令来覆盖所有切片:

let sourceRegion = MTLRegionMake3D(64, 64, 0, 64, 64, 1)
let destOrigin = MTLOrigin(x: 0, y: 0, z: 0)
let firstSlice = 1
let lastSlice = 2 // inclusive

let commandBuffer = commandQueue.makeCommandBuffer()
let blitEncoder = commandBuffer.makeBlitCommandEncoder()

for slice in firstSlice...lastSlice {
    blitEncoder.copy(from: sourceImage.texture,
                     sourceSlice: slice,
                     sourceLevel: 0,
                     sourceOrigin: sourceRegion.origin,
                     sourceSize: sourceRegion.size,
                     to: destImage.texture,
                     destinationSlice: slice - firstSlice,
                     destinationLevel: 0,
                     destinationOrigin: destOrigin)
}
    
blitEncoder.endEncoding()
commandBuffer.commit()
Run Code Online (Sandbox Code Playgroud)