When doing GPU programming, it’s sometimes useful to determine the positions of the vertices of the current triangle primitive in a fragment shader. The usual advice here is to use a geometry or tessellation shader to gather up the positions and pass them along explicitly to the fragment shader. It turns out, though, that there is a relatively straightforward solution without any extra shader stages using the standard derivative functions dFdx and dFdy. It works as long as the (non-perspective-correct) barycentric coordinates for the current fragment are available. I’m sure I’m not the first one to come up with this technique, but a quick search didn’t find any explanations of it, so I thought I’d write it up, because it turned out to be useful in Pathfinder.

An important caveat applies: Make sure that you’re sure you need to do this. It’s easy to go wild with this technique and end up with more varyings than the simple solution would call for. In general, this technique is most useful when it saves a geometry or tessellation shader invocation.

Here I assume that the barycentric coordinates $\lambda_0, \lambda_1, \lambda_2$ of the current fragment are known. They must sum to 1, so it suffices to know two of them to compute the other one. If you need barycentric coordinates and cannot compute them from other varyings you have around, then you can pass them to the fragment shader by declaring noperspective in vec2 vLambda; and setting vLambda to $(1, 0)$, $(0, 1)$, and $(0, 0)$ for the first, second, and third vertices respectively. We also need the gradients $\nabla_{\lambda_0}$ and $\nabla_{\lambda_1}$. Following the example above, the function dFdx(vLambda) will yield $(D_x\lambda_0, D_x\lambda_1)$, and dFdy(vLambda) will yield $(D_y\lambda_0, D_y\lambda_1)$.

Let $p = (x, y)$ be the screen-space position of the current fragment (i.e. gl_FragCoord), and let $p_r = (x + 1, y)$ and $p_u = (x, y + 1)$ be the positions of the fragments to the immediate right and above, respectively. $\lambda_0$, $\lambda_1$, and $\lambda_2$ are the barycentric coordinates of the current fragment; likewise, $\lambda_{0r}$, $\lambda_{1r}$, and $\lambda_{2r}$ are those of the fragment to the right, and $\lambda_{0u}$, $\lambda_{1u}$, and $\lambda_{2u}$ are those of the fragment above.

We know that for all triangle vertices $i \in \{0, 1, 2\}$, $\lambda_{ir} = \lambda_i + D_x\lambda_i$, and $\lambda_{iu} = \lambda_i + D_y\lambda_i$. Putting it all together, we formulate a system of equations:

After some algebra and the application of Cramer’s rule, we get:

Notice that $% $ shows up over and over again in these formulas. That value is the Jacobian determinant $J$. Using $\lambda$ to represent the row vector $% $, we can more succinctly rewrite the above as:

Along with point positions, sometimes the triangle edge vectors $v_{01}$, $v_{12}$, and $v_{20}$ are useful. These have even simpler formulas:

The expressions for the vectors $v_{pi} = p - v_i$ (for vertices $i \in \{0, 1, 2\}$) are straightforward as well:

Below is some GLSL code to compute screen-space triangle vertex positions from $\lambda_0$ and $\lambda_1$ (as lambda.x and lambda.y respectively).