Mipmap selection in too much detail 09 May 2025 In this post, I want to shed some light on something I’ve been wondering about for a while: How exactly are mipmap levels selected when sampling textures on the GPU? If you already know what mipmapping is, why we use it, and what pixel derivatives (ddx() / ddy()) are, you can skip to the section Derivatives to mipmap levels. The post does, however, assume some knowledge of graphics programming. Mipmapping primer A very common operation in shaders is texture sampling. In HLSL, we typically use the Texture2D.Sample() function for this. For a regular 4-channel floating-point texture, the full function signature looks like this: float4 Texture2D.Sample(SamplerState sampler, float2 location); It takes as input a location to sample the texture at, and a SamplerState, which is an object containing metadata used to influence the sampling, like which texture filtering mode should be used. Here is an example of a simple fragment shader which shades each pixel by sampling a texture _Texture using the texture coordinates in the 0th UV channel: float4 frag (float2 uv : TEXCOORD0) : SV_Target { return _Texture.Sample(sampler_Texture, uv); } When applied to a quadrilateral and fed a brick texture as input, we get this: Notice that the surface looks rather ‘pixelated’, especially near the far edge. This is called texture aliasing. The root cause is that the size of the texture space region covered by a screen pixel varies with distance and viewing angle. Consider, for illustration, the tilted quadrilateral. Observe how the same area in screen space can correspond to very different areas in texture space. The blue square covers roughly 4 pixels, while the green one covers roughly 12, even though they have the same area in screen space. The more shallow the viewing angle, the more texels are covered by each pixel on the screen. Since we only sample 1 texel for every screen pixel, most of the texels covered by the screen pixel are aliase...
First seen: 2025-05-14 03:33
Last seen: 2025-05-14 06:33