Once I have the common library code, it’s not that hard to run it on another platform
I just managed to increase FPS (by about 7 in demo2!) with optimized texture coordinate computation. Let me elaborate so that people can use this trick too:
I need to compute vertical texture coordinate for each pixel of a wall column. In these cases it’s good to precompute the coordinate step and then just keep accumulating it like this:
int32_t coordStep = textureHeight / wallHeight; // both in pixels
texCoords.y = 0;
for (y = 0; y < wallHeight; ++y) // for each wall column pixel
{
...
texCoords.y += coordStep;
}
But there is a big disadvantage: the bigger the wall drawn on screen, the less accurate this will be. Imagine the wall height on screen being between half to the full height of the texture. You’ll get coordStep
= 1 for all these sizes, which is highly inaccurate. If the wall is higher than the texture, which happens a lot, you’ll get 0 step and effectively no texture coordinates.
For this reason I went to computing texture coordinates something like this:
for (y = 0; y < wallHeight; ++y) // for each wall column pixel
{
texCoords.y = (y * textureHeight) / wallHeight;
...
}
Which is nicely accurate, but more expensive, as you perform multiplication and division in each loop cycle.
However today I realized this more expensive approach is only necessary in cases where the first approach fails and that I can keep the first one to be used in cases where it’s accurate enough, which can be easily decided by comparing the wall and texture height.
So now I have both these loops and choose the one to use depending on the coordinate step: if it’s too small, the wall is close to the texture size and I execute the more accurate loop, otherwise I choose the faster loop. Like this:
int32_t coordStep = textureHeight / wallHeight; // both in pixels
if (coordStep < LIMIT)
{
// more accurate
for (y = 0; y < wallHeight; ++y) // for each wall column pixel
{
texCoords.y = (y * textureHeight) / wallHeight;
...
}
}
else
{
// faster
texCoords.y = 0;
for (y = 0; y < wallHeight; ++y) // for each wall column pixel
{
...
texCoords.y += coordStep;
}
}
The if
could be inside the loop, but for the sake of performance I chose to have two versions of this time critical loop and branch early.