将 MTLTexture 从 depth32Float 转换为 bgra8UNorm

Cast MTLTexture from depth32Float to bgra8UNorm

我想使用 Metal Performance Shaders 处理来自深度缓冲区的信息,例如使用高斯或索贝尔着色器。

我 运行 在使用 MTLTexturedepth32Float 像素格式时遇到问题。 MPSImageGaussianBlur 或任何其他性能着色器不接受它作为源纹理。

我尝试使用 depthBufferTexture.makeTextureView(pixelFormat: .bgra8Unorm) 进行转换,但出现错误提示:

validateArgumentsForTextureViewOnDevice:1406: failed assertion source texture pixelFormat (MTLPixelFormatDepth32Float) not castable.

有什么方法可以将 depth32Float 转换为 bgra8UNorm 或任何其他像素格式?

depth32Float 转换为 bgra8UNorm,在我看来,没有多大意义,它们具有不同的维度和通道数。在您的情况下,最好的解决方案是使用 MTLPixelFormatR32Float。 要从 depth32Float 转换为 MTLPixelFormatR32Float 使用 MTLComputeCommandEncoder.