overlapping Metal Point primitives and blending

2019-05-29 14:09发布

I am rendering Points primitives that overlap partially. The fragment shader shades parts of each Point primitive square transparent (solid center circle). A point primitive that does not overlap any other point primitive, shades as expected (the transparent areas of the square show the background).

When such a point primitive overlaps another point primitive, the behavior is a unexpected. specifically: the transparent area does not show the opaque color of the unlerlying points primitive, but instead clears it and shows the background color.

In other words, point primitives with a higher vertex ID number clear the previsously shaded fragments of point primitives with a lower vertex ID number.

The points primitives are encoded like this:

command_encoder.drawPrimitives(.Point, vertexStart: 0, vertexCount: stroke.count) 

And the vertex & fragment shaders looks like this:

vertex OutVertex vertex_func(constant InVertex* vertex_array [[buffer(0)]],
                          constant Uniforms& uniforms [[buffer(1)]],
                          uint vid [[vertex_id]]) {

    OutVertex out;
    InVertex in = vertex_array[vid];

    // transform vertex into NDC space
    out.position = uniforms.projection * float4(in.position.x, in.position.y, 0, 1);

    out.pointSize = 60;

    return out;
}

fragment float4 fragment_func(OutVertex vert [[stage_in]], float2 uv[[point_coord]]) {
    float2 uvPos = uv;

    uvPos.x -= 0.5f;
    uvPos.y -= 0.5f;

    uvPos *= 2.0f;

    float dist = sqrt(uvPos.x*uvPos.x + uvPos.y*uvPos.y);
    float circleAlpha = 1.0f-dist;

    half4 color = half4(0.0f, 0.0f, 1.0f, 1.0f);
    color *= circleAlpha;

    return float4(color.r, color.g, color.b, circleAlpha);
}

The results looks like this:

enter image description here enter image description here

I would like to find out how to prevent the clearing of overlapping transparent areas but maintain the transparency against the background.

Thank you.

Update 5/25/16:

I was able to turn on fixed function blending with these additions to my render pipeline descriptor:

    rpld.colorAttachments[0].blendingEnabled = true
    rpld.colorAttachments[0].rgbBlendOperation = .Add;
    rpld.colorAttachments[0].alphaBlendOperation = .Add;
    rpld.colorAttachments[0].sourceRGBBlendFactor = .One;
    rpld.colorAttachments[0].sourceAlphaBlendFactor = .One;
    rpld.colorAttachments[0].destinationRGBBlendFactor = .OneMinusSourceAlpha;
    rpld.colorAttachments[0].destinationAlphaBlendFactor = .OneMinusSourceAlpha;

And now things look as expected:

enter image description here

0条回答
登录 后发表回答