metalkitmetal-performance-shaders

Cast MTLTexture from depth32Float to bgra8UNorm


I would like to process information from depth buffer using Metal Performance Shaders e.g. using gauss or sobel shaders.

I run into problems when using MTLTexture with depth32Float pixel format. MPSImageGaussianBlur or any other performance shader isn't accepting it as source texture.

I tried to convert it using: depthBufferTexture.makeTextureView(pixelFormat: .bgra8Unorm) but got error saying:

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

Is there any way how to convert depth32Float to bgra8UNorm or any other pixel format?


Solution

  • Converting from depth32Float to bgra8UNorm, in my opinion, does not make much sense, they have different dimensions and number of channels. In your case, the best solution would be using MTLPixelFormatR32Float. To convert from depth32Float to MTLPixelFormatR32Float use MTLComputeCommandEncoder.