簡體   English   中英

iOS /金屬:如何從深度緩沖區讀取一點?

[英]iOS/Metal: how to read from the depth buffer at a point?

我想從深度緩沖區中讀取內容。 在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?

我將深度存儲操作設置為MTLStoreActionStore ,然后在render方法中執行以下操作:

[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);

}];

在蘋果論壇上,這確實有效,並被蘋果開發人員確認為“正確的方法”。

請注意,iOS上的OpenGL ES無法從深度緩沖區讀取數據。 金屬FTW!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM