Accessing cubemaps on the CPU

Started by
0 comments, last by Hodgman 12 years, 11 months ago
Currently I am looking at inequality tests to see which component has the gretest magnitude then using the sign to determine the

Is there an efficient with no branches, way of selecting the correct face of a cubemap in software from a normalized vector?
Advertisement
Off the top of my head, finding the component of the normal with the largest magnitude and then select between the two faces along that axis based on the sign sounds correct.

Not tested://assuming: enum FaceIndex { PosX, NegX, PosY, NegY, PosZ, NegZ }


//if your CPU is more int friendly
int GetFaceIndex( float x, float y, float z )
{
int xGtY = (int)(x > y);
int xGtZ = (int)(x > z);
int yGtz = (int)(y > z);
int xNeg = (int)(x < 0);
int yNeg = (int)(y < 0);
int zNeg = (int)(z < 0);
int xIsMax = xGtY & xGtZ;
int xIsntMax = ~xIsMax;
int yIsMax = xIsntMax & yGtz;
int yIsntMax = ~yIsMax;
int zIsMax = xIsntMax & yIsntMax;
int index = (xIsMax * xNeg) + (yIsMax * (2+yNeg)) + (zIsMax * (4+zNeg));
return index;
}

//if your CPU has fsel and is more float friendly
float GetFaceIndex( float x, float y, float z )
{
float xGtY = fsel(x - y, 0, 1);
float xGtZ = fsel(x - z, 0, 1);
float yGtz = fsel(y - z, 0, 1);
float xNeg = fsel(x, 1, 0);
float yNeg = fsel(y, 1, 0);
float zNeg = fsel(z, 1, 0);
float xIsMax = xGtY * xGtZ;
float xIsntMax = 1-xIsMax;
float yIsMax = xIsntMax * yGtz;
float yIsntMax = 1-yIsMax;
float zIsMax = xIsntMax * yIsntMax;
float index = (xIsMax * xNeg) + (yIsMax * (2+yNeg)) + (zIsMax * (4+zNeg));
return index;
}

This topic is closed to new replies.

Advertisement