0

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?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
  • 1
    You know the answer to this question – that is, how to fix the validation crash – so what's left to it? Also, the texture descriptor `usage` can contain more than one of the usage values. You need *at least* `.renderTarget`, but that doesn't have to be "instead of" `.shaderRead` or `.shaderWrite`. It can be combined with them. – Ken Thomases May 27 '18 at 17:40
  • I am not sure if the OpenGLES code I wrote is exactly equivalent to Metal lines of code I wrote above. Am I missing something, because Metal code is supposed to be faster than OpenGLES, not slower. – Deepak Sharma May 27 '18 at 18:13
  • I recommend that you provide and accept your own answer to this question (about the validation crash). That is, remove your note from the question and provide the first part of it as an answer. Then, open a new question for the correctness of the blending configuration (if it's really not working properly). And maybe another for the performance issue. – Ken Thomases May 27 '18 at 23:49
  • Ok I posted the new question here - https://stackoverflow.com/questions/50561522/opengles-blending-code-to-metal-translation – Deepak Sharma May 28 '18 at 08:01

1 Answers1

2

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)
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131