Reputation: 841
I did masking like this in opengl as follows
vec4 patCol = texture2D(pattern, samplePos);
vec4 maskCol = texture2D(tex0, texCoordVarying);
gl_FragColor=vec4(patCol.xyz, patCol.w);
I want to do the same masking in iOS the texture in maskCol is Semi Transparent. When I couldn't get the similar output in Metal. Can any body help me in this.
Renderpipeline Descriptor
let pipelineDescriptor = MTLRenderPipelineDescriptor()
pipelineDescriptor.fragmentFunction = fragmentFunction
pipelineDescriptor.vertexFunction = vertexFunction
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
pipelineDescriptor.sampleCount = 1
pipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
pipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .one
pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .one
pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
Upvotes: 0
Views: 764
Reputation: 96
Given you're not using maskCol in your return I'm going to assume you were trying to do something like:
float4 patCol...
float4 maskCol...
return float4(patCol.rgb, maskCol.a);
That is, taking the alpha value from the mask and applying it to the source patCol...
For it to work you need to set the blending options in your pipelineDescriptor:
pipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
pipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
That's going to give you the "default" behavior, source-over blending: source.rgba + destination.rgba
Upvotes: 1