2

我想从深度缓冲区中读取。在 OS XI 上的 GL 中可以执行以下操作:

float depth[2][2]; // get 2x2 for bilinear interpolation
glReadPixels(s.x, s.y, /*width*/2, /*height*/2, GL_DEPTH_COMPONENT, GL_FLOAT, depth);

(请注意,使用 iOS 上的 OpenGL ES,您无法从深度缓冲区中读取)

金属的等价物是什么?

看来我需要这样做:

_renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore;

然后以某种方式通过CPU从缓冲区中读取?

虽然也许有更好的方法,因为我只需要一个点(触摸的地方)。片段着色器能否仅存储该点的深度(或 2x2 用于双线性插值),从而允许我将 storeAction 保留为 MTLStoreActionDontCare?

4

1 回答 1

3

我将深度存储操作设置为MTLStoreActionStore,然后在渲染方法中执行以下操作:

[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {

    // We're done! So read from the depth texture.        

    // Just read the center pixel for now.
    MTLRegion region = MTLRegionMake2D(_depthTex.width/2, _depthTex.height/2, 1, 1);

    float depth;
    [_depthTex getBytes:&depth bytesPerRow:_depthTex.width*4 fromRegion:region mipmapLevel:0];

    NSLog(@"read depth: %f", depth);

    dispatch_semaphore_signal(block_sema);

}];

这很有效,并在论坛上被 Apple 开发人员确认为“正确的做法”。

请注意,使用 iOS 上的 OpenGL ES 无法读取深度缓冲区。金属FTW!

于 2015-02-10T18:49:00.103 回答