Public Group

# software renderer

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

## 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 on other sites
Generally speaking, such restrictions are far more significant for hardware rasterisers than software ones. Software is much more flexible.

##### 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 on other sites
Quote:
 Original post by esuvsTypically 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 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

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009410
• ### Who's Online (See full list)

There are no registered users currently online

×