![](/img/trans.png)
[英]How to use Depth Testing with CAMetalLayer using Metal and Swift?
[英]How to turn-off/disable Metal depth testing
我知道如何使用 swift 在 Metal 中启用深度测试。
只需像这样使用适当的MTLDepthStencilState object 调用MTLRenderCommandEncoder.setDepthStencilState() ,它就可以正常工作。
renderCommandEncoder.setDepthStencilState( state )
关闭它,我认为这可以工作,但它在运行时给我一个错误。
renderCommandEncoder.setDepthStencilState( nil )
错误:
-[MTLDebugRenderCommandEncoder setDepthStencilState:]:3843: failed assertion `Set Depth Stencil State Validation
depthStencilState must not be nil.
这很奇怪,因为 Apple 的文档说默认值为 nil,而 function setDepthStencilState()采用可选值。
知道如何关闭深度测试还是我做错了什么?
您可以通过从MTLDepthStencilDescriptor
创建一个MTLDepthStencilState
并将depthCompareFunction
设置为always
来禁用深度测试。
let descriptor = MTLDepthStencilDescriptor()
descriptor.depthCompareFunction = .always
let depthStencilState = device.makeDepthStencilState(descriptor: descriptor)
renderCommandEncoder.setDepthStencilState(depthStencilState)
更新:只需将depthCompareFunction
设置为always
即可使深度测试始终通过,但它仍会写出所有片段的深度。 如果你想保持深度缓冲区在同一个 state,你可以设置isDepthWriteEnabled
为false
。
我不使用swift ,但你会理解我的逻辑。
在 Metal 中,您可以独立于渲染管道配置深度测试,因此您可以混合和匹配渲染管道和深度测试的组合。 深度测试由MTLDepthStencilState object 表示,就像您使用渲染管道一样,您可以创建此 object 的多个变体。
创建一个新的MTLDepthStencilState object 并使用以下设置对其进行配置:
DepthStencilDescriptor* depthStencilDescriptor = DepthStencilDescriptor::alloc()->init();
depthStencilDescriptor->setDepthWriteEnabled(false); /* disable depth write */
_depthStencilStateNoWrite = _device->newDepthStencilState(depthStencilDescriptor);
depthStencilDescriptor->release();
每当您希望 object 被深度测试忽略时使用它,如下所示:
renderEncoder->setDepthStencilState(_depthStencilStateNoWrite);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.