• Advertisement
Sign in to follow this  

software renderer

This topic is 3866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let's say you are writing a software renderer. If you restrict texture sizes to powers of two, how could you use this fact to speed your code up?

Share this post


Link to post
Share on other sites
Advertisement
Generally speaking, such restrictions are far more significant for hardware rasterisers than software ones. Software is much more flexible.

Share this post


Link to post
Share on other sites
Typically your texture will be stored as a single linear array of data. If you want to access the pixel at position (x,y) in an image of size width*height then you can use the follwing formula:

offset = y * width + x;

If width is a power of two you can bit shift instead.

It also makes it easier to break images into tiles - and I suspect generating (and using) mip-maps is much easier too.

Share this post


Link to post
Share on other sites
Quote:
Original post by esuvs
Typically your texture will be stored as a single linear array of data. If you want to access the pixel at position (x,y) in an image of size width*height then you can use the follwing formula:

offset = y * width + x;

If width is a power of two you can bit shift instead.


bpp = 2^b (b = 0,1,2) - bytes per pixel (8, 16 or 32 bits)
width = 2^n - texture width (1, 2, 4, 8, 16, ....)

offset = ((y << n) | x) << b

Which may be faster.

But usually, the biggest gain from this aproach will come if you hard-code the n and b values. Then the compiler will be able to use a bit more optimization, and if you hardcode your width, then modern compilers should use bit shifts on their own.

Try out the ((y*width) + x) * bpp vs the bitshifted version. There might not be a difference at all.

Share this post


Link to post
Share on other sites
if your image is evenly divisible by 2 then you can quickly scale your image by using the shift>> register for doing things like mip mapping

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement