A blog which discusses various GPU applications including visualization, GPGPU and games.

Sunday, January 3, 2010

Integer division

Beware integer division on the GPU.

In a certain compute shader I am working on, this code

if (tcoord.x < 0) tcoord.x += gridsize.x;
if (tcoord.y < 0) tcoord.y += gridsize.y;
if (tcoord.z < 0) tcoord.z += gridsize.z;

if (tcoord.x >= gridsize.x) tcoord.x -= gridsize.x;
if (tcoord.y >= gridsize.y) tcoord.y -= gridsize.y;
if (tcoord.z >= gridsize.z) tcoord.z -= gridsize.z;

runs much faster than using

tcoord = (tcoord + gridsize) % gridsize;

No comments:

Post a Comment

Followers