ios - Metal API 验证崩溃
问题描述
我编写了以下代码来实现屏幕外渲染到金属纹理的附加混合,但如果启用金属 API 验证,它会崩溃:
validateRenderPassDescriptor:487: failed assertion `Texture at colorAttachment[0] has usage (0x02) which doesn't specify MTLTextureUsageRenderTarget (0x04)'
这是代码:
let renderPipelineDescriptorGreen = MTLRenderPipelineDescriptor()
renderPipelineDescriptorGreen.vertexFunction = vertexFunctionGreen
renderPipelineDescriptorGreen.fragmentFunction = fragmentFunctionAccumulator
renderPipelineDescriptorGreen.colorAttachments[0].pixelFormat = .bgra8Unorm
renderPipelineDescriptorGreen.colorAttachments[0].isBlendingEnabled = true
renderPipelineDescriptorGreen.colorAttachments[0].rgbBlendOperation = .add
renderPipelineDescriptorGreen.colorAttachments[0].sourceRGBBlendFactor = .one
renderPipelineDescriptorGreen.colorAttachments[0].destinationRGBBlendFactor = .one
我想要实现的只是加色混合,在 OpenGLES 中是这样的:
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_BLEND);
我的代码有什么问题?
解决方案
好的,我找到了解决方案。解决方案是在创建纹理时设置纹理使用标志 MTLTextureUsage.renderTarget (或代替取决于使用情况、shaderRead 或 shaderWrite):
let textureDescriptor = MTLTextureDescriptor()
textureDescriptor.textureType = .type3D
textureDescriptor.pixelFormat = .bgra8Unorm
textureDescriptor.width = 256
textureDescriptor.height = 1
textureDescriptor.usage = .renderTarget
let texture = device.makeTexture(descriptor: textureDescriptor)
推荐阅读
- binding - 使用绑定创建的菜单与使用 MaterialDesignThemes 包创建的菜单不同
- javascript - LightningChart createProgressiveTraceGenerator 不是函数
- leakcanary - leakcanary-android 使用错误 Illegal class file: Class module-info is missing an super type
- javascript - 如何在 NextJs 中使用使用 `webfontloader` 的包(服务器端渲染)
- reactjs - 如何隐藏错误消息并仅在 yup 测试中突出显示字段
- css - 为什么布尔玛左边距(填充)不能正确显示
- python - 检查单元格内容是否包含特定形状
- php - 如何使用 cli/git 升级 moodle 版本?
- python - Numpy 数组条件更新
- clickonce - clickonce 开发和生产环境