Direct3D Line thickness

Agnel Kurian picture Agnel Kurian · Feb 17, 2010 · Viewed 18.2k times · Source

How do I change the thickness of lines when drawing line lists using Direct3D?

This post says that line width is not supported and goes on to provide a workaround. Other options?

While we are on this topic, do shaders allow one to draw lines with dash patterns?

Answer

Stringer picture Stringer · Feb 17, 2010

You can use a geometry shader that will take as an input a segment of your line and output a quad (a triangle strip made of two triangles) so that the width of the quad is constant in screen space and matches the desire line thickness. It works perfectly well (for having implemented it in a CAD 3D engine).

If geometry shader is not an option, a workaround could be to use a vertex shader, but it will require some re-work of your VB. Keep in mind that the VS must then have knowledge of the line segment in its whole so you will end up storing p and p+1 for each of your VB elements, plus the cost of duplication of indices/vertices (depending the topology used and if you render your line as an indexed primitive or not).

If performance is not an issue, doing the expand on the CPU is maybe the way to go.

EDIT:

About dash patterns: you can use a geometry shader too in order to emulate glLineStipple behavior. If you have a GL_LINES topology, that is to say isolated lines, the pattern restarts at each new line segment. So you just have to compute in the geometry shader the screen-space horizontal start (or vertical start, depending the orientation) of your line segment and pass this extra infos to the pixel shader. The pixel shader will then be responsible of discarding fragments according to the factor and pattern values (DirectX 10/11 Integer and Bitwise instructions make it easy).

Again this works well, and you can combine it with emulated width lines (with the first technique above mentioned).

Now if you have GL_LINE_STRIP topology, the pattern restarts at each new primitive (so for each new draw call). The situation becomes a bit more complicated since you need to have the knowledge of the number of pixels that have been rendered before, and this for each line segment.

You can achieve that with rendering your line strip in a temporary VB using DirectX 10 stream-out functionality (each element of this VB corresponds to the screen-space length of each segment). Then you have to do a Parallel Prefix Sum (aka Scan) of this VB (for accumulating each line segment length values).

Lastly, you render your line strip like for GL_LINES but use this extra Scan VB informations in the PS.