Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm creating a shader with SharpDX (DirectX11 in C#) that takes a segment (2 points) from the output of a Vertex Shader and then passes them to a Geometry Shader, which converts this line into a rectangle (4 points) and assigns the four corners a texture coordinate.

After that I want a Fragment Shader (which recieves the interpolated position and the interpolated texture coordinates) that checks the depth at the "spine of the rectangle" (that is, in the line that passes through the middle of the rectangle.

The problem is I don't know how to extract the position of the corresponding fragment at the spine of the rectangle. This happens because I have the texture coordinates interpolated, but I don't know how to use them to get the fragment I want, because the coordinate system of a) the texture and b) the position of my fragment in screen space are not the same.

share|improve this question
the fragment shader interpolates the texture coods automatically IIRC – ratchet freak Nov 12 '13 at 16:08
Yes, they are automatically interpolated, but what I want is to get the position of the line that goes through the middle of the rectangle at it's closest point to my fragment. – c4sh Nov 12 '13 at 17:09
up vote 1 down vote accepted

One way to do this is to create another output from the geometry shader, called spinePos for example. In the geometry shader, calculate the spine position at each end of the rectangle, and assign it to spinePos for the two vertices at that end.

The rasterizer will interpolate this value across pixels as it normally does, but since the two vertices at each end have the same spinePos value, there will be no change in the value from interpolating across the thickness of the rectangle. It will only change as you move along the length of the rectangle.

share|improve this answer
I found a more complicated solution, so let me try yours because I think it's more elegant and efficient. Thanks! – c4sh Nov 13 '13 at 10:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.