首页 > 解决方案 > 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);

我的代码有什么问题?

标签: iosopengl-esmetal

解决方案


好的,我找到了解决方案。解决方案是在创建纹理时设置纹理使用标志 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)

推荐阅读