PocketMatrix forums

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

Rotated Blits

<< Note: Even though we started this discussion in Phantom's forum under the "Easy CE 2.0" topic, I decided to start a new topic on this issue because it makes it easier for people to find during a search. It's also more of a general programming topic rather than something specific to Jacco's work, or Easy CE 2.0.>>

I've been playing around with blitting code lately in an attempt to determine the penalty for performing a rotated blit per frame. I've got some interesting numbers to share with you guys.

I'm able to do a 240x320 -> 320x240 rotated blit in 5.57 ms. That's close to 180 fps. This is 20 or so lines of C code that I could probably rewrite in ARM assembly to get closer to 200 fps.

Rewriting the routine so that it writes to sequential addresses to avoid tearing results in a severe perf penalty since the cache is getting thrashed. This dropped the throughput to approximately 22 ms per frame (45 fps).

The secret here is that the display memory is not cached. Therefore you don't pay any penalty for writing to non-contiguous addresses to display RAM. The back buffer however IS cached since it's in system memory. You certainly want to read sequentially from your back buffer to take advantage of the cache.

One other interesting thing: I tried these routines writing both to display memory as well as a system memory surface. If you're reading and writing to contiguous addresses, it's really fast. I was seeing 1.77 ms to memcpy from a 320x240 back buffer to another 320x240 offscreen buffer. That same operation when the target was display memory was 4.5 ms. That's the write cache in action I suppose. However, if I try the rotated blit code mentioned above to blit to a system memory surface instead of display memory, the elapsed time jumps to 17.57 ms! This is because the puny cache on the StrongARM is getting thrashed as the routine jumps across cache lines in order to write the data.

Just for the record I also tested GDI's BitBlt from a 240x320 DIB section selected into an off-screen memory DC blitted to the screen DC. It was about 36 ms (27 fps).
Johan · 12 Jan 2002, 21:43 UTC — Interesting article
Digby,

Very interesting article! Extremely well written and put together!

How bad is the tearing when rotating? Since I opted for the route with one format across all buffers, I always write sequentially to the display (4.5ms). Is it very noticable, in the sense that if you picked any man on the street he would say "nice application, but damn what is this tearing doing there?" :D, or is it a more subtle effect? I guess I could try it myself, but I have to submit an article until Monday (which is what I _really_ should be doing).

Best regards,
Johan Sanneblad
www.gapitools.com
I don't see any tearing, but then again, I'm blitting the same image over and over in my test <g>. I suppose the amount of tearing would depend on what is changing from one frame to the next. Also, it seems that there will always be the chance of tearing even if you are blitting in the same direction that the LCD controller refreshes because you have no way to sync your writes to display memory with the controller's memory access. In the case of a rotated blit it might look more obvious because the tear won't be a vertical line, it will be a diagonal.

Incidentally, the routines that I've written uses a similar approach that you did in that the sprites are rotated when they are loaded into their respective surfaces. This allows the back buffer to have the same orientation as the primary surface so that you can perform the fast copy. I have a two tiered software approach where there is an API middleware and a driver. The driver performs device specific code and parts of it are in ARM assembly language (for the iPaq). The driver knows about the orientation difference - the middleware doesn't. The middleware portion handles surface managment, thread safety, and interaction with GDI (I can return an HDC for any surface). I also allow locking/unlocking sprite surfaces and handing a pointer to the pixel data back to the application for procedural texturing effects. The driver performs a conversion (rotation in our case) if necessary between locking and unlocking so that the application will always get a surface in the expected orientation. This is exactly how DirectDraw interacts with its display drivers. Surfaces are typically stored in a device specific format under the hood and are converted during the lock/unlock. This process is commonly referred to as "swizzling".

Anyway, enough about that stuff. This thread is about rotated blits. I'll try to come up with some images that would demonstrate the tearing and run a few tests. I also want to play around with prefetching data to help the cache in reading from the back buffer. I'll let you know how it goes.
digby:any chance you show us your 5.7ms code? it's much better then the~10ms approaches I have heard of, unfortunetly I don't own any PPC so it's really hard for me to do such "low-level" stuff which might easily crash someone's testing machine but still work fine on the emulator
Kzinti · 13 Jan 2002, 04:48 UTC — Hello + Blit performances
Hello everyone, I just found this wonderfull site and I am impressed to see that there are many knowledgeable programmers around here. You might know me as being the author of the "GAPI emulator".

These days, I'm working on my PocketPC as I figured out that while I may never write a good game for the PC (lack of time / resources), it might be possible to do so on my PocketPC. The discussions you have here about "rotating" blits is really interesting. I've come to similar conclusions about the cache, but I have a few other points of information I wish to share with you guys.

As Digby already said, the video RAM is not cached by the memory controller or CPU, and the reason for this is simple: the video RAM is not normal RAM, it is internal to the display chip. This memory is mapped in the address space of applications so that you can access it (memory mapped I/O).

I am not sure that writting to uncached memory is slower then cached memory, the thing is that when you write to cached memory, you are invalidating cache lines that are in use for reading, thereby killing the reading performance, not the writting. Nevertheless, the result is the same: you get faster blits writting directly to the display then to a memory buffer.

Another thing that I haven't seen adressed yet is the use of DMA transfers by certain devices. You all know that the iPAQ devices allows direct access to the display memory (no caching), so in this case the fastest possible way to "flip" is to blit directly to the display. On some other devices like my Casio E-125, the GAPI buffer is trsnaferred to the display using some kind of DMA. What this means is that the actual blitting is done in the back while the processor is free to do other things. That's also the reason that the Casio E-125 gets decent performances with a slower processor and with an intermediate buffer. (Note that most games use an intermediate buffer on iPAQ anyways, while this is not needed on the E-125.)

Direct access is also possible on the Casio E-125, but from the tests I have conducted, the frame rate drops. There is at least 2 reasons for this: 1) The DMA transfert is not used, 2) The MIPS is a 32/64 bits processor internally, but the external bus is only 16 bits.

There is also another benefit to DMA transfers: it is most likely synchronized with the "refresh" of the display as to reduce tearing effects.

I've conducted a test using a "graphic-bandwidth" limited program:

Direct access --> 24 fps
DMA --> 28 fps

Understand that the test is really poorly written and doesn't do anything else but GXBeginDraw()-Blits-GXEndDraw(). In an actual game, you would do many things after the GXEndDraw() and before the next GXBeginDraw(). Even with all that, you can see a considerable gain in performances.

Another important pointer is that if you want to get the most out of the DMA transfert, you have to minimize the time period spent between GXBeginDraw() and GXEndDraw(). As soon as GXEndDraw() is called, the DMA transfert starts, so this is where you game logic has to take place.

Another thing you can see is that updating part of the screen (1/2 or 1/4) doesn't affect the performances much. I think that the reason for that is that like most DMA controllers, doing the actual setup takes more time then the transfert.
Oups, forgot to include the performance when updating part of the screen, here goes:

Direct access --> 24 fps
DMA --> 28 fps
DMA + Update 1/2 screen --> 29 fps
DMA + Update 1/4 screen --> 30 fps
simonjacobs · 15 Jan 2002, 02:56 UTC
Digby:

Hi. Inspired by your post I have also managed to get a rotated blit working in 5.57 ms!

I wrote mine in assembler, if you get a big speed increase from redoing yours in assembler please let us know.
I managed to get the C code to run at 5.3ms by unrolling the inner loop a bit. I looked at the code the compiler was generating and nothing stood out as looking overly heinous so I didn't bother jumping into an assembly language coding fury and instead enjoyed my weekend. <g>

Since the 1:1 memcpy with an aligned back buffer is around 4.5ms, it's going to be hard to beat that if you're having to rotate the pixels during the blt. That being said I'm pretty much done with this stuff. I wasn't going to do the rotation in the "flip" anyway. My driver does the swizzle during lock/unlock so that everything is aligned to the native orientation. I like to deal with hard numbers in making decisions though rather than just going with "oh, it's a LOT faster". I hope my perf numbers of the various blits have been useful for some of you as well.

I did look at the tearing issue with doing a rotation and it's pretty hideous. Of course it all depends on what you're changing per frame. If you don't change much, it might not be too bad. You end up with a diagonal tear instead of the less intrusive vertical tear. It would really be nice to be able to know when the LCD controller is beginning its scan. That would probably go a long way towards minimizing this tearing if you could sync up with the controller's display memory access cycle.
Kzinti · 15 Jan 2002, 04:02 UTC — Hard numbers
Digby: I understand that you'd rather work with hard numbers, but when you deal with DMA, it is not as easy as using a performance counter around the GXBeginDraw() / GXEndDraw() pair.

If one was to do that, he would be able to measure the time it takes to initiate the DMA transfer, not the time taken to do the blit. You could of course measure the maximum theorical frame rate by continuously doing GXBeginDraw()->Blit()->GXEndDraw(), but this is almost meaningless when using DMA since the real cost is the time it takes to initiate the DMA transfer. The actual blit will be done in the background while the CPU continues executing your game code.
simonjacobs · 15 Jan 2002, 04:07 UTC
Plan9:

That is not true on the iPaq at least. We are trying to measure the time to write to the screen memory which is done with the processor, not dma.
My comment about hard numbers is that unless you've measured what you are trying to improve, or compare with, you're just making blind assumptions.

The numbers I quoted only refer to the iPaq 3670. I have no idea of what it takes to rotate or blit on any other device. What I'm trying to do is encourage people to post hard numbers (as you've done) instead of subjective data such as "it won't be any faster" or "it will be a lot faster." Make sense?

I agree with you that on devices with hardware assisted blits, the actual blit time doesn't matter if it's happening asynchronously. However in the case of the iPaq the numbers that I've posted are quite meaningful since everything is performed by the host CPU.
Digby,

It's clear, and useful :). It's also good to know that a rotate is only slightly slower than a simple blit (excuse the subjective wording ;) ), at least in my book. 4.5 / 5.3 wouldn't influence my decision very much.

- Jacco.
<Edited>
I had some ideas on altering the tearing effect. After thinking a bit more on it I came to the conclusion that it would probably provide an even more disturbing effect than the original one. :)
My idea was basically to read the source image non-sequentially, providing an effect similar to that with interlaced video (sacrificing some speed). But after I did some tests in Photoshop I found out it would probably look even more strange than the original effect... :)

The tearing effect is probably the most serious issue with using a buffer in a format not compatible with the display surface... While it could be eliminated with Jaccos previous suggestion of using an extra buffer (memory->rotate->buffer->copy->screen), the time in ms to rotate (memory->rotate->memory) is too high to make it feasible. :/

Syncing to the lcd update cycle would surely be a nice solution for many. This was actually what I thought Compaq had done for the 3800 series (when everyone complained they could not get more than 30FPS from it). However, counting the number of negative replies from developers wanting more than 50FPS in their game, forcing them to sync to the display update cycle could probably cause a similar reaction.

Best regards,
Johan
Kzinti · 15 Jan 2002, 18:42 UTC — ...
Digby: agreed. I am curious to know what kind of overhead your Lock()/Unlock() design incurs on your game?

I assume that this overhead when loading images is non important, but what about using procedural images like a fire effect or wave effect? I seems to me that it would be better in these cases to directly use a "native orientation" format. Is this something possible with your current design?

In my own PocketFrog, I am yet to include support for "native orientation" which I intend to have for efficient sprite support. But I am struggling as to what is the best approach.
sbl,

Your comment about game developers wanting to run their games at 50 fps on the new iPaq makes me wonder if they realize that the LCD controller is probably running at 24 fps and that there's no way they are going to get data to the screen faster than that. Pretty silly to be rendering twice as many frames as necessary.

Does anyone here know what the actual update rate is on the LCD displays in the various Pocket PCs? I've heard it was ~24 Hz on the iPaq. Would it be possible to write a test app to find out what the refresh rate actually is? You might be able to write an app that copied alternating colors to display RAM and then vary the update time until the tear in the image was in the same place each frame. What do you think?
Digby: Well.. :) I would guess that it's the same problem as with LCD/DLP projectors. All projectors (except D-ILA) run at native speed 60hz. Everyone wants to run at more than 60hz (24x3=72hz in the US, 25x3=75Hz in Europe) and screams when if a projector does not support it (most projectors do these days though). Of course they will see tearing in the picture once they get their Hz of choice, and then they scream on that as well.. :) I guess I've been hanging on avsforum.com too long now... But the number of posts saying "its a shame they locked the refresh rate to 60hz rather than 72hz" is not that frequent... :)

This error report on pocketgamer.org says it all I would say:
http://www.pocketgamer.org/archives/00000511.shtml

Initially, the most complaints on the new H3800 was that "it will be virtually impossible to go beyond a screen update rate of 30-35 frames/second." - not that it was in fact much slower in performance. What was important was the number of FPS one could achieve from it... I believe many people will always count in FPS or Hz. Even if it doesn't matter. :/ And once they get sufficient FPS or Hz (~100 or so), they will start complaining on the picture issues such as tearing. But I could be wrong of course. :D

Re: refresh rates. I would guess color copying would work, however it could probably be simpler to spot on a simple panning (textured) background. I always thought the refresh rate on the IPAQ was closer to 30Hz. It would be an interesting test. :D Let the bidding begin! :)

/Johan
I personally wont be including an FPS counter in my games just for that very reason. It's Irrelevant to the game and usually results in just more complaints.

As for the tearing, since my game is a side scroller it's something I can't live with. I started my 2D lib without an iPAQ so I didn't realize the tearing would be so prominant. As a result I have to redesign portions of the lib... aggrivating, but I guess it's a learning experience.

What I'm thinking, and I get from the GAPITools website this is what sbl does, is to have an internal coordinate system that's the same as the display and all surfaces use it and an external coordinate system for the interface and to transform those coordinates in the blits. Minor performance hit, but that's the only way I see this working.

-Hosed
I finally got my 3rd PPC, an iPAQ 3630 so I got to test the tearing out.

Curious to see just how bad the so called tearing from updating the iPAQ's screen opposite of it's memory alignment would really be when compared to the Cassio EM-500 or Jornada 545 I went ahead and finished my blitters in their original design (blitting a portrait back buffer to the landscape display) and pieced together a quick scroller test.

The scroller test scrolled horizontally at varying speeds and then vertically at varying speeds, but never in both directions at once.

My results, from best to worst in terms of noticable tearing:
Jornada 54x
iPAQ
Cassio EM-500

The Jornada performed best only because it's display is so horrible refresh wise that any tearing is blurred out.

The Cassio curiously was by far the worst. It also had some odd pixel blur at any speed. I would of thought this one would do the best, but it appears the DMA transfer isn't vsynced.

Explanation of my blitter:
I always read from the back buffer from top-left to bottom-right, portait orientation. When on a portrait display I just do a memcpy, when on a landscape display I find the corresponding pixels on the display, blitting to it "against the grain" so to speak.

My conclusion is that all this concern on rotated blits is pointless, other then the desire to do the fastest flip possible. If you're getting anything above 30 FPS (with game logic) on the target device though, then it seems like a fruitless effort. What's more important is a vsync and we don't have one yet. Hopefully we'll start to see some devices with the new ATI mobile chip or the promised Nvidia one soon and then all our worries will be null 'n void :)

Hope this is helpful. I haven't written my performance unit tests yet so I can't give FPS numbers, but before the updated blitters I was seeing 64 FPS on the Cassio EM-500 and 20 FPS on the Jornada 54x. No idea with the iPAQ.

-Hosed
Kzinti · 21 Jan 2002, 22:11 UTC — To rotate or not to rotate
I've just finished converting PocketFrog to use "native" surface orientation. That means that all surfaces use the same physical orientation as the buffer returned by GAPI. This also means that I never need to do any rotation "on the fly". All rotations are done while loading the images.

My "Blit" sample went from 29 fps to a whooping 145 fps in all screen orientations! I must say I am really impressed and pleased.

I took Digby's / DirectX 8 approach for direct access by using the Lock()/Unlock() methods. In a near futur, I intend to allow the user to specify a rectangle when locking the surface (for better performances).

I'm quite happy with these results and don't see the point anymore in trying to optimize rotation blits.
That's good to hear. I'm glad things worked out for you.
Digby: Just curious.. What game(s) do you work with currently? Or is it classified? :) I tried searching the forum for more info, but had no luck.

Your programming skills (as well as with the other professionals on this forum I might add) suggests several years of games programming, and I have personally found this forum to be exceptionally inspiring!

/Johan
I'm not working on anything specific other than toying around with the best way to implement routines for a graphics library. I'm a software design engineer who likes to tinker with ARM assembly from time to time.

I agree with you, the developer community here is a good one. We're pretty open with ideas around here and that's a good thing. I hopeful that others who come this way can benefit from it.
>~24 Hz on the iPaq

Ermmm that's a bit on the low side, much nearer 60 from what I can tell.

Else how would explain how you can tell the difference between a game running at 30fps and 60?