PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Bitmap Rotation and memory caching...

I'm currently writing a program that rotates and scales a massive bitmap to the screen in real time. The problem is that at certain angles the routine is incredibly fast (12ms) while at others it takes up to 100ms to complete. My code loops are really tight (i.e. no multiplications or divides etc.) so I'm thinking I have a memory caching problem because I am not accessing local ram in sequencial order. Is there a way of stopping memory caching or temporarily turning or even controling the amount of memory cachd at any one time...

:?
Yes, don't access video ram in sequential order. :) The video ram is not cached, so that solves the problem. That is rather tricky for rotation however.
Another way to make the rotation faster is to store a copy of the source bitmap rotated 90 degrees, and always read from the bitmap that is "most horizontal" (i.e. most cache-line efficient) to the reads. That way even a read of a "vertical" line will be cached nicely (and most other lines will be cahced better).

As Phantom says, the screen memory is largely uncached and unbuffered, so write that in any order you like and the speed will normally be the same.
refractor wrote:
Another way to make the rotation faster is to store a copy of the source bitmap rotated 90 degrees, and always read from the bitmap that is "most horizontal" (i.e. most cache-line efficient) to the reads. That way even a read of a "vertical" line will be cached nicely (and most other lines will be cahced better).


I tried the method you suggested and it worked well on the PC but not so well on my IPAQ. In the end I tried plitting thr source bitmap up into 64x64 pixel tiles and rotated these into place instead. This method gives me approx 25fps which is definately an improvement. This works well because it's not such a jump to a neighbouring pixel, so there is not so much cache thrashing...
A silly little question...

Is it any faster to draw two pixels at a time rather than 1? By that, I mean combining two unsigned short (I assume a 16-bit FB) into an unsigned long using OR and <<16 on the second pixel?

I suppose its no faster to blit 4 bytes because of lack of caching, but since I do it (320x240x20 = 1536000) times per second (running at 20fps) (twice as many on a HPC!), it's worth asking :P
I don't have the answer as I haven't optimized my rotated blit yet... But why don't you try it and see how it goes?
One other thing you could try: There's a doc called 'fatmap.txt' floating around on the web (easy to find using google) that describes a technique using 8x8 pixel tiles to speed up texture mapping. The idea is that once you read the first pixel of a 8x4 tile, all other pixels are in the cache. It thus doesn't matter whether you're scanning from left to right, right to left or up-down. The inner loop of such a mapper is remarkably simple in x86 assembler. I'm not sure if the ideas port well to C(++) or arm assembly though.
Hey... I been trying to rotate my bit maps too..
er... Maybe this is a dumb one, but HOW to do it man?
I need some directions. How do you rotate a BIT MAP!????
When rotating, each line on your display is constructed by scanning a line in the source bitmap. Step over this line in 240 steps (when filling 240 pixel wide screen lines) and voila. Observe that you can do zooming and rotating together at the same cost as rotating alone.
That fatmap.txt document is really nice and giving me tons of ideas... Alas I still don't have enough spare time as I would like.

The XBOX is using a tiled format that is really nice (to optimize texture lookup), it could probably be used for software rendering also. Basically, you start by tilling everything (8x8). Then you tile the tiles (16x16). Then you tile the tiles (32x32) and so on.
Cyberghost: I find it easy to think of the rotation as being an affine transformation represented by a matrix.

Say you want to rotate an image (w,h) and center it on your back buffer at (cx,cy). The rotation is of course taking place around the center of the image (at (w/2, h/2)).

You first define a matrix to move the center of the image to the origin (0,0):

Code:

( 1 0 0 )
I2O = ( 0 1 0 )
( -w/2 -h/2 1 )

You then define the rotation matrix (note that the angle is first negated):

Code:

( cos(-a), sin(-a), 0 )
R = ( -sin(-a), cos(-a), 0 )
( 0 0 1 )

Finally, you want to center the image on (cx,cy):

Code:

( 1 0 0 )
O2S = ( 0 1 0 )
( cx cy 1 )

You then concatenate your transforms:

Code:

T = I2O * R * O2S

You then have a nice matrix to transform coordinates from the image to the screen.

This is the basic maths behind any blit with rotation. I didn't double check everything, but that's the basic idea.

Note that you don't want to actually use matrices for efficiency reasons and that you will encounter rounding issues (particularly when using fixed point maths).

Hope this helps!
Hm, yeah, that should help. :)
Oh well, that's the easiest answer I could come with. It seems pretty straightforward to me... I started with this in PocketFrog to make sure everything worked properly. I am now in the process of optimizing it...
I suppose its no faster to blit 4 bytes because of lack of caching


It depends how the "graphics memory" is configured. On the iPAQs, the "graphics memory" is part of the main block of memory, attached to the CPU on a 32-bit bus. So, plotting 32-bit words should be more efficient than plotting two 16s and letting the hardware combine it.

Testing that should be pretty simple :) One of these days I'll fire up a test suite and have a play, but as my free-time at the moment is tending towards zero... :(
Kzinti: I was kidding. I noticed that some people like one approach, some people the other to get something clear. Your approach would not be mine, that's all. :)

When thinking about rotations I always try to visualize a rotating square in the source image (because the display is standing still, duh ;) ), so I need the four corners of that square. For that, I use plain old sinus / cosinus stuff (trial and error usually does the job ;) ). Then, I determine the 320 begin and endpoints of the 320 lines that I need to fill the screen by interpolating in 320 steps between the 4 points. Finally, I interpolate over these lines by doing 240 steps. I do this in fixed point to avoid the floats.

Notice that the four points don't have to describe a rotated rectangle, they can be anything. If they *are* the points of a rectangle however, you can optimize the line stuff a bit: Every line is the same, just displaced. Saves a divide per line (and some other calculations too).

Well that's my way of saying the same thing. ;)

Respect,
- Jacco.
Oh and sorry 'bout the 'duh', smileys and 'respect' thing, I've not become a scener all of a sudden or whatever, I'm just working on a chatbot that mimics stuff like that when the user enters it. Guess I got carried away. ;)

Respect,
- Jacco.
Hey no problem Jacco, I perfectly understand =)

The path doesn't really matter, the important thing is the result. This is what provides programmers with satisfaction.

Respect,
- Thierry =)
All right man...
Thanks for all the info.
I guess I need to revise a little of my school work to get the maths out.
he he he...

One LAZY question.
The GAPI 1.2 Does not have inbuild rotation and such functions like the DirectX i use?
boring....
For people interested by fatmap documentation maybe you like the next issue fatmap2
Hm yes, the fatmap2 contains stuff about subpixel accuracy. Something that a lot of nowadays PPC games could use... I posted something about that Jet Fighter thing and that problem on PocketGamer, but the post was removed. Odd huh? ;)