The mathematics of two dimensional interpolation on a quad I am trying to wrap my head around how exactly the GPU interpolates texture coords on a quad. I realize to the GPU a quad is two triangles but I find thinking in terms of the whole quad to be easiest.

If we define texture space as the texture coordinates from X:Y{0-1} and world space as the actual coordinates of the quad how can I given a point p1 that is on the quad figure out what its texture coordinate would be? Given a texture coordinate point p2 how would I figure out its real world coordinates?

Keep in mind that the quad can be any quadrilateral not necessarily always a parallelogram. 

As an extra note lets define the texture coordinates on the quad so that they are as such
(It actually is easier to think (and compute) about this with triangles, but for the sake of the answer, let's first stick to your quad example.)

For this you just have to define the point you're interested in in terms of the quad's (or whatever primitive's) vertices. This is called a barycentric coordinate system. More practically-speaking, you basically give the point $n$ weights, each describing the influence the $i$th vertex of the quad has on the point. Those are the point's barycentric coordinates with respect to that quad.

So for example the center point of the quad (or "center of mass") would have the weights $\frac{1}{4}$, $\frac{1}{4}$, $\frac{1}{4}$, and $\frac{1}{4}$. A point in the middle of the left edge would have a weight of $\frac{1}{2}$ for the left two vertices each and a weight of $0$ for the right two vertices. (Notice how they always sum to $1$?)

These barycentric coordinates can then be used to interpolate whatever kind of per-vertex values you want, be that positions or texture coordinates. It all reduces to a simple weighted sum. And even more than that, they can also be quite easily computed by simple vector algebra from any existing per-vertex values if you have the point's values given (comes down to just a few little 2x2 determinants for a triangle, or similarly easy computation). This means if you know a pixel's screen space position and those of the vertices, you can compute its barycentric coordinates. These you can then use to interpolate the pixel's texture coordinates (or normal, or whatever) from the per-vertex values, or vice versa.

So much to the theory, there's a few practical considerations to keep in mind, especially when using them for interpolation in a rasterizer, like GPUs do.


For anything but triangles the barycentric coordinates don't necessarily have to be uniquely defined for any arbitrary point. That's why thinking about triangles is much easier, especially for the computer.
Due to the fact that they are negative if and only if the point doesn't actually lie inside the triangle they can also be used for checking if the point belongs to the triangle at all and thus are more or less a by-product of the pixel-in-triangle test that the rasterizer already performs to, well, rasterize the triangle.
When it comes to interpolation in projected space, i.e. when projecting a 3D world onto the plane using perspective distortion, it becomes a little more involved since you have to account for the fact that a line's length in screen space is not the same as its length in world space, or more precisely its screen space length doesn't increase linearly when going along a triangle edge in world space. The keyword here is perspective correct interpolation. But this still uses the point's barycentric coordinates.
Of course in an actual rasterizer you also have to take account for shared triangle edges and only rasterize a shared edge once, which requires special rules as to which triangle a shared edge belongs and might make the computation of barycentric coordinates a little more complicated, together with using fixed-point coordinates or whatever optimized computations.


(This is a more general overview of the matter and how it generally works. For specific mathematical as well as technical details, I'd advise for further reading, maybe starting with the above Wikipedia articles.)