Metal texture - Draw and Erase

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:46:43

问题


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

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:


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



来源:https://stackoverflow.com/questions/52267291/metal-texture-draw-and-erase

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!