繁体   English   中英

glCopyTexImage2d 的金属等效物

[英]Metal equivalent for glCopyTexImage2d

我需要获取绘制到帧缓冲区的任何内容的纹理,换句话说,我将要绘制的效果下方出现的任何内容。 用例是将此纹理提供给对其执行扭曲的着色器。

到目前为止我已经尝试过使用 MTLBlitCommandEncoder。

auto commandQueue = [device newCommandQueue];
auto commandBuffer = [commandQueue commandBuffer];
[commandBuffer enqueue];
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:mtlDescriptor];

// perform encoding

[renderEncoder endEncoding];

// now after several render passes i need to commit all the rendering for behind the effect, 
// so that the texture i am grabbing will omit whatever is about to be drawn after this point

[commandBuffer commit];
[commandBuffer waitUntilCompleted];

接下来,我必须创建一个新的命令缓冲区,因为如果我不这样做,我将在此提交后调用 addCompletedHandler 时收到错误消息。 我想命令缓冲区不能提交多次,对吗?

auto commandBuffer = [commandQueue commandBuffer];

id<MTLBlitCommandEncoder> blitEncoder = [commandBuffer blitCommandEncoder];
[blitEncoder enqueue];

[blitEncoder copyFromTexture:drawable.texture sourceSlice:0 sourceLevel:level sourceOrigin:region.origin sourceSize:region.size toTexture:_mtlTexture destinationSlice:0 destinationLevel:level destinationOrigin:{xOffset, yOffset, 0}];

[blitEncoder endEncoding];
.
// continue with more other render encoding

这可以在没有任何断言错误的情况下运行。 但问题是深度测试显示不正确。 效果似乎被绘制在它应该在上面的模型后面(当我只使用 1 个命令缓冲区而不执行 blit 时,它呈现正确)。 我正在使用此设置,我认为写入深度纹理的任何内容都将被保留?

mtlDescritpor.depthAttachment.loadAction = MTLLoadActionLoad;
mtlDescritpor.depthAttachment.storeAction = MTLStoreActionStore;

谁能指出我哪里出错了?

编辑:我尝试使用 2 个命令缓冲区,一个接一个,没有执行 blit,并且深度也出现错误。

当它是一个新的命令缓冲区时,这是否意味着深度测试无法工作? 或者对于我想要实现的目标是否有更推荐的实现? 我似乎找不到任何例子..

EDIT2:经过更多测试,即使仅使用 1 个命令缓冲区,似乎也存在非常不一致的行为。 有时效果呈现在下方(不正确),有时是正确的。 (它的一部分应该在 OpenGL 上进行测试时在上面渲染)当我注释掉一行代码或添加更多代码行时,结果会随机改变。 我目前正在使用 depthCompareFunction MTLCompareFunctionLessEqual 如果我更改为MTLCompareFunctionNotEqual ,则所有内容都将始终绘制在顶部,这也是错误的。

所以我意识到我一直有一个错误的印象,即需要首先执行提交,以便将绘制到该点的内容“保存”到纹理中。

基于此处的此信息https://github.com/gfx-rs/gfx/issues/2232就好像执行的任何渲染编码都已绘制到纹理。 因此根本不需要 2 个命令缓冲区。

至于深度测试问题,我的错误是没有将 MTLRenderCommandEncoder 的视口 znear 和 zfar 设置为与模型相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM