I'd like to save a depth buffer to a texture in metal, but nothing I've tried seems to work.
_renderPassDesc.colorAttachments[1].clearColor = MTLClearColorMake(0.f, 0.f, 0.f, 1.f);
[self createTextureFor:_renderPassDesc.colorAttachments[1]
size:screenSize
withDevice:_device
format:MTLPixelFormatRGBA16Float];
_renderPassDesc.depthAttachment.loadAction = MTLLoadActionClear;
_renderPassDesc.depthAttachment.storeAction = MTLStoreActionStore;
_renderPassDesc.depthAttachment.texture = self.depthTexture;
_renderPassDesc.depthAttachment.clearDepth = 1.0;
When I pass depthTexture
into my shader (which works fine with data from my other textures), all I get is red pixels.
As I change clearDepth
to values closer to zero, I get darker shades of red. Perhaps I'm somehow not sampling the texture correctly in my shader?
fragment float4 cubeFrag(ColorInOut in [[stage_in]],
texture2d<float> albedo [[ texture(0) ]],
texture2d<float> normals [[ texture(1) ]],
texture2d<float> albedo2 [[ texture(2) ]],
texture2d<float> normals2 [[ texture(3) ]],
texture2d<float> lightData [[ texture(4) ]],
texture2d<float> depth [[ texture(5) ]])
{
constexpr sampler texSampler(min_filter::linear, mag_filter::linear);
return depth.sample(texSampler, in.texCoord).rgba;
}