ios - Metal API 验证崩溃

标签 ios opengl-es metal

我编写了以下代码来为 Metal 纹理的离屏渲染实现加法混合,但如果启用了 Metal 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)

关于ios - Metal API 验证崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50554465/

相关文章:

iphone - 如何为 iPhone 创建 32RGBA 格式的 CVPixelBuffer?

objective-c - 片段着色器功能中 float4 和 half4 之间的主要区别是什么?

ios - lipo 无法打开输入文件(没有这样的文件或目录)xcode

ios - 黑色 View 而不是表格

linux - 如何使用 clutter 直接渲染到 linux framebuffer?

android - 如何转换 Android 位图以包裹圆柱体并改变透视图

ios - Metal 片段着色器 uv 坐标在读取顶点颜色时发生变化

ios - 相当于内核程序的 Metal 中的 glColorMask?

ios - 我如何在 Unity3d (iOS) 上获得正确的堆栈跟踪

ios - 失败(POSIXErrorCode : Address already in use)