Not so much a question about coding, but about how the OpenGL “engine” decides which mipmap is the “nearest” one. In the fragment shader, the only input to sampling is a single texture coordinate, which by itself doesn’t tell you anything about the required level of detail. So … how?
GLSL provides various functions for sampling textures: The ones with neither “Grad” nor “Lod” in their name use the (approximated) partial derivatives of the texture coordinates, those with “Grad” require the caller to pass in the partial derivatives (which is necessary if the texture mapping has discontinuities, e.g. a modulo operation), those with “Lod” require the caller to pass in the level of detail.
Partial derivatives are approximated using finite differences, i.e. the difference between the texture coordinates for the current fragment and those for the adjacent fragments.
Thanks. I guess that means that
- Texture sampling calls must be synchronized between adjacent fragments (which shouldn’t cause delays as the same amount of work is performed in each case)?
- When there are multiple calls to the same texture per fragment (e.g. bump mapping), these are matched by their order?
GPUs are SIMD (single instruction, multiple data) devices. Each (non-uniform) variable in the shader is an array, each instruction is performed on all elements concurrently (i.e. c=a+b => c[i]=a[i]+b[i]). Rendering invokes the fragment shader for blocks of pixels. A call to a texture sampling function will return multiple samples for multiple texture coordinates. The differences between texture coordinates for adjacent fragments are used to determine the scale factor and thus level of detail. You can calculate the partial derivatives explicitly using the
dFdy functions. If necessary, the fragment shader will be executed for fragments outside of the primitive so that the derivatives can be calculated for fragments at the edge of the primitive (the boolean variable
gl_HelperInvocation will be true for such fragments).
Ah yes, of course - that makes perfect sense. Thanks again.