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!

enter image description here


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?

enter image description here

  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Jan 21, 2013 at 20:30
  • \$\begingroup\$ 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! \$\endgroup\$ Jan 21, 2013 at 20:34
  • \$\begingroup\$ You've had a bit of shader experience before, right? So you know how UVs, normal vectors, etc. get passed from the vertex shader to the pixel shader, by writing output params in the vertex shader that match up to input params in the pixel shader. You can have output params in the geometry shader too, so just create some and put your information in them, and the pixel shader will have access to that just as it always does. \$\endgroup\$ Jan 21, 2013 at 20:46
  • \$\begingroup\$ 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. \$\endgroup\$ Jan 21, 2013 at 20:52
  • \$\begingroup\$ 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) \$\endgroup\$
    – AturSams
    Jan 29, 2014 at 15:55

1 Answer 1


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;
            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.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .