Как сохранить буфер глубины в текстуру с помощью Metal?

Я хотел бы сохранить буфер глубины для текстуры в металле, но ничего из того, что я пробовал, похоже, не работает.

_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;   

Когда я передаю depthTexture в свой шейдер (который отлично работает с данными из других моих текстур), все, что я получаю, - это красные пиксели.

Когда я меняю clearDepth на значения, близкие к нулю, я получаю более темные оттенки красного. Возможно, я как-то неправильно сэмплирую текстуру в своем шейдере?

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;
}

person Max Harris    schedule 27.05.2016    source источник


Ответы (2)


Используйте depth2d<float> вместо texture2d<float> в качестве типа аргумента и считайте float из текстуры глубины float val = depth.sample(texSampler, in.texCoord);

person tmpvar    schedule 27.05.2016

Хорошо, оказалось, что мне просто нужно было использовать depth2d вместо texture2d:

depth2d<float> depth [[ texture(5) ]])
person Max Harris    schedule 27.05.2016