2D Line drawing with Pixel Shaders

Recently I asked a question, what is the fastest way to render 2D lines using DirectX, and one of the answers mentioned this paper on Fast Prefiltered Lines which uses Pixel Shaders to accomplish wireframe drawing. This paper draws quads per line segment then uses a pixel shader to draw a line by computing distance from the pixel to the line down the centre of the quad before filling with line-color or transparent.

So, I'm wondering how can this be done? I wasn't aware the pixel shader can get knowledge of the input geometry (e.g. the quad) that creates it. Is the following possible? Ideally in DirectX9 but DX10 or OpenGL solutions also appreciated!

EDIT

Turns out I don't draw the quad like that, but like this. So the question is: How can I get information about x1y1 x2y2 into the pixel shader so I can compute distance to line edge & get a nice antialiased line segment?

• You don't want to draw a giant quad like that; it's a colossal waste of compute power, since most of the pixels it touches will be completely transparent. Just draw a thin quad, oriented along the line, covering just the width of the line plus the filter radius on all sides. Jan 21, 2013 at 20:30
• Ok, lol. So the method in that paper requires quads are setup (rather like my current Geo-shader implementation), just the Pixel shader is used to do the soft edges? If so, I'm still interested in learning how to get info about quad vertices into the pixel shader! Jan 21, 2013 at 20:34
• Yep, it was pretty basic stuff though (some lighting shaders, some GPGPU) and many years ago. Ok, let me look this up, and cheers again for pointing me in the right direction. Jan 21, 2013 at 20:52
• Writing a pixel shader that draws segments is pretty straight-forward. I think the problem is you need to get started and show us what is not working for yoo? You need to pass x0, y0, x1, y1 (see answer) Jan 29, 2014 at 15:55

See pseudo code First check if the current pixel is in range:

if(current.x >= - max_distance + min(x0, x1) && current.x =< max_distance + max(x0, x1))
{
if(current.y >= -max_distance + min(y0, y1) && current.y =< max_distance + max(y0, y1))
{
result.color = some_color;
float distance = simple_function_that_computes_distance_from_segment(x0, x1, y0, y1);
if(distance < max_distance)
result.alpha = (max_distance - distance) / max_distance;
else
result.alpha = 0.0;
}
}


Get a tutorial for some generic pixel shader and re-write the code. To compute the distance of a point from a segment, you could google.