/* Calculate texture scale in the horizontal and vertical screen axis. */
px = GPU_SQRT(dudx * dudx + dvdx * dvdx);
py = GPU_SQRT(dudy * dudy + dvdy * dvdy);
/* Calculate texture scale on the XY/YX axis (axis rotated 45 degrees). */
pxy = GPU_SQRT((dudx + dudy) * (dudx + dudy) + (dvdx + dvdy) * (dvdx + dvdy));
pyx = GPU_SQRT((dudx - dudy) * (dudx - dudy) + (dvdx - dvdy) * (dvdx - dvdy));
/* Calculate the minimum and maximum for both axis. */
pMin = GPU_MIN(px, py);
pMax = GPU_MAX(px, py);
/* Calculate ratio for X/Y axis. */
N = GPU_MIN(pMax/pMin, f32bit(maxAniso));
/* Calculate the minimum and maximum for both axis. */
pMin2 = GPU_MIN(pxy, pyx);
pMax2 = GPU_MAX(pxy, pyx);
/* Calculate ratio for XY/YX axis. */
N2 = GPU_MIN(pMax2/pMin2, f32bit(maxAniso));
/* Determine which */
if (N >= N2)
{
if (pMax == px)
axis = TextureAccess::X_AXIS;
else
axis = TextureAccess::Y_AXIS;
/* Calculate the number of samples required. */
samples = u32bit(GPU_CEIL(N));
/* Calculate the texture scale for each sample. */
//scale = pMax / f32bit(samples);
scale = pMax / N;
}
else
{
/* Determine the anisotropy axis. */
if (pMax2 == pxy)
//axis = TextureAccess::XY_AXIS;
axis = TextureAccess::X_AXIS;
else
//axis = TextureAccess::YX_AXIS;
axis = TextureAccess::Y_AXIS;
/* Calculate the number of samples required. */
samples = u32bit(GPU_CEIL(N2));
/* Calculate the texture scale for each sample. */
//scale = pMax / f32bit(samples);
scale = pMax / N2;
}
/* Calculate the per anisotropic sample offsets in s,t space. */
switch(axis)
{
case TextureAccess::X_AXIS:
//printf("X ");
dsOffset = dudx / f32bit(samples + 1);
dtOffset = dvdx / f32bit(samples + 1);
break;
case TextureAccess::Y_AXIS:
//printf("Y ");
dsOffset = dudy / f32bit(samples + 1);
dtOffset = dvdy / f32bit(samples + 1);
break;
case TextureAccess::XY_AXIS:
//printf("XY ");
dsOffset = (dudx + dudy) / f32bit(samples + 1);
dtOffset = (dvdx + dvdy) / f32bit(samples + 1);
break;
case TextureAccess::YX_AXIS:
//printf("YX ");
dsOffset = (dudx - dudy) / f32bit(samples + 1);
dtOffset = (dvdx - dvdy) / f32bit(samples + 1);
break;
}
/* Normalize to s,t space : [0..1]. */
dsOffset = dsOffset / f32bit(1 << textureWidth2[textUnit]);
dtOffset = dtOffset / f32bit(1 << textureHeight2[textUnit]);
}