Deepak Sharma
Deepak Sharma

Reputation: 6487

Metal API validation crash

I wrote the following code to implement additive blending for offscreen rendering to a Metal texture but it crashes if Metal API Validation is enabled:

validateRenderPassDescriptor:487: failed assertion `Texture at colorAttachment[0] has usage (0x02) which doesn't specify MTLTextureUsageRenderTarget (0x04)'

Here is the code:

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

All I want to implement is additive color blending, something like this in OpenGLES:

glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_BLEND);

What is wrong in my code?

Upvotes: 0

Views: 2520

Answers (1)

Deepak Sharma
Deepak Sharma

Reputation: 6487

Ok I found the solution. The solution was to set texture usage flag MTLTextureUsage.renderTarget in addition to (or in place of depending upon usage, shaderRead or shaderWrite when creating the texture:

        let textureDescriptor = MTLTextureDescriptor()
        textureDescriptor.textureType = .type3D
        textureDescriptor.pixelFormat = .bgra8Unorm
        textureDescriptor.width = 256
        textureDescriptor.height = 1
        textureDescriptor.usage = .renderTarget

        let texture = device.makeTexture(descriptor: textureDescriptor)

Upvotes: 2

Related Questions