0
votes

I am trying to modify Apple's sample GLPaint (paint app using OpenGL) to use Metal instead of OpenGL. I am able to render a brush stroke to screen using Metal, but am having difficulties "erasing" it.

In Metal, I am using the following blending parameters:

renderPipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
renderPipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
renderPipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .one
renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .one
renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha

The render function uses the above pipeline descriptor:

let descriptor = MTLRenderPassDescriptor()

descriptor.colorAttachments[0].loadAction = .load
descriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 0.0, alpha:0.0)
descriptor.colorAttachments[0].storeAction = .store
descriptor.colorAttachments[0].texture = colorAttachmentTexture

let renderCommandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: descriptor)
renderCommandEncoder?.setRenderPipelineState( blendRGBAndAlphaPipelineState)
    texturedQuad.encodeDrawCommands(encoder: renderCommandEncoder!, texture: texture)
    renderCommandEncoder?.endEncoding()

How can I create a pipeline descriptor to "erase" portions of the previously rendered texture? In OpenGL, I was able to toggle between "drawing" and "erasing" by performing the following:

if(eraserEnabled)
{
    glColor4f(0,0,0,1);
    glBlendFunc( GL_ZERO,GL_ONE_MINUS_SRC_ALPHA);
}
else
{

    // Set color of the brush 
    glColor4f(1,0,0,1);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}

I tried creating a second renderPipeline in Metal that is used for "erasing". I used the blending parameters below, but it is not working.

renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .zero
renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .zero
renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha

image shows how "erase" is not working.

Summary: I am rendering a Metal texture to screen but do not now how to set blending parameters "erase" selected regions of the previously drawn texture.

1
Define "it is not working".Ken Thomases
@KenThomases I added an image that shows the actual result, which does not match desired result.CTRLALTDELx64

1 Answers

0
votes

i setup only this sourceAlphaBlendFactor = .zero, whithout sourceRGBBlendFactor. For example I used code from https://github.com/codelynx/MetalPaint