Cg code for texture LOD calculation (copied from http://linedef.com/personal/demos/?p=virtual-texturing):
1 #define TEXTURE_SIZE 512 2 #define TEXTURE_MIPCOUNT 10 3 4 float MipLevel( float2 uv ) 5 { 6 float2 dx = ddx( uv * TEXTURE_SIZE ); 7 float2 dy = ddy( uv * TEXTURE_SIZE ); 8 float d = max( dot( dx, dx ), dot( dy, dy ) ); 9 10 // Clamp the value to the max mip level counts 11 float rangeClamp = pow( 2, (TEXTURE_MIPCOUNT - 1) * 2 ); 12 d = clamp( d, 1.0, rangeClamp ); 13 14 float mipLevel = floor( 0.5 * log2(d) ); 15 return mipLevel; 16 }