Hi,
In a fragment shader I’m trying to calculate the mipmap level of a texture for the current fragment. To be more specific, my texture is not a real texture per se but a structure organized like a mipmap. I can easily see if my mipmap level is incorrect, eg. too “optimistic”.
My textures/structures are almost always highly anisotropic.
So far I’ve been using the following formulae, but it seems to be too “optimistic” - it is clear the lookup should be done lower down in the mipmap hierarchy, as I can see groups of fragments suddenly popping.
int calculateMip(vec2 texCoord) // both values between 0.0 and 1.0, inclusive
{
float maxDerU = max( abs(dFdx(texCoord.x)), abs(dFdx(texCoord.y)) );
float maxDerV = max( abs(dFdy(texCoord.x)), abs(dFdy(texCoord.y)) );
float mip = log2(1.0 / max(maxDerU, maxDerV));
return int(ceil(mip)); // returns the mipmap level
}
My calculation seems correct: I’m taking the maximum of all 4 partial derivatives, and make it a mipmap value. But this doesn’t work.
Do dFdx/dFdy return the derivatives between the current pixel and the pixel right to it/above it? Or the derivatives between the pixel left of the current pixel and right of it (resp. below and above)?
Thanks,
Fred