Instanced Line Rendering Part II: Alpha Blending
wwwtyro.net
wwwtyro.net
Last time I rendered this kind of thing, I just did it the simple way you describe so you're not wrong. I just used stencil to prevent alphablending overdraw issues. If you can afford it, it's the simplest and actually the most flexible approach, as it works with any topology, including multiple overlapping segments (e.g. curves, X- and T-shapes, etc).
The first line sets a bunch of pixels in the stencil buffer that somehow then need to get unset before drawing the next line, no?
I'm actually really interested in this as I'm having a really complicated time doing what should be relatively simple 2D CAD-type stuff with any of the modern graphics APIs (Vulkan or DirectX 12). Any time I want to draw a world-coordinate vertex thing with a device pixel width dimension life just gets horrible.
You can partially avoid this with early-Z and early-stencil (which will reject entire groups of pixels), but those will only help if most or all of your degenerate triangle is invisible. If a single pixel passes that early culling, it will have to shade at least 4 pixels to produce that one output pixel.
If you are concerned about the cost of drawing a quad where 25-50% of it is transparent/discarded, you can create a simple hull out of 2-4 triangles where it suffers less from the overhead of shading those 2x2 groups. That is a common compromise that can produce improved performance by skipping 2x2 groups that don't contribute to the output frame.
In practice all quads suffer from this, and the larger the quad the more cycles you waste shading the seam between the two triangles twice. The overhead for large quads is still pretty small in comparison, but if you're doing full-screen post-processing effects it can produce a measurable speed-up to draw a single huge (viewport clipped) triangle instead.
It's possible some hardware has optimizations to mitigate this, but it's also the case that since shaders can do things like read/write from arbitrary buffers, any optimization that avoids shading the seam twice would cause incorrect differences in behavior, so it's hard to do that optimization in all cases.
This is super useful though! You don't always have the ability to change the blending op, or you could be rendering on top of something that might give you artifacts. Thankfully I was rendering something like this on a blank canvas.
In the fifth figure, "Adjusting the intersecting vertices of intermediate segments to prevent overlap", you can see that the vertex adjustments cause the angle and thickness of the line segments to change. This looks wrong to me — shouldn't the angle and thickness be unaffected? Shouldn't the red boundaries along the tops and bottoms of the line segments be colinear with the dashed grey boundaries?