iOS/金属:如何从深度缓冲区读取一点? [英] iOS/Metal: how to read from the depth buffer at a point?

查看:64
本文介绍了iOS/金属:如何从深度缓冲区读取一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从深度缓冲区中读取内容.在OS X的GL中,我可以这样做:

I'd like to read from the depth buffer. In GL on OS X I could do:

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时,您无法从深度缓冲区读取内容)

(Note that with OpenGL ES on iOS you can't read from the depth buffer)

Metal相当于什么?

看来我需要做:

_renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore;

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

And then somehow read from the buffer via the CPU?

尽管也许有更好的方法,因为我只需要一点(触摸的位置).片段着色器是否可以仅存储该点的深度(对于双线性插值,可以存储2x2的深度),从而使我可以将storeAction保留为MTLStoreActionDontCare?

Though perhaps there's a better way since I only need one point (where the touch is). Can the fragment shader store the depth for just that point (or a 2x2 for bilinear interpolation), thus allowing me to leave the storeAction as MTLStoreActionDontCare?

推荐答案

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

I set the depth store action to MTLStoreActionStore and then did the following in the render method:

[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开发人员确认为正确的方法".

This worked and was confirmed as the "right way to do it" by an Apple developer over on the forums.

请注意,iOS上的OpenGL ES无法从深度缓冲区进行读取.金属FTW!

Note that reading from the depth buffer isn't possible with OpenGL ES on iOS. Metal FTW!

这篇关于iOS/金属:如何从深度缓冲区读取一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆