PocketMatrix forums

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

CGapiSurface::BltFast

I was looking at the source code for MFC example which basically draws increasing number of sprites using BlitFast until the frame rate drops below 30 fps.
On my device ( ipaq 3835 - 206 mhz , standard pocket pc) it was leveling off at 120-130 sprites.
That seems to be like an awfully small number - on the same hardware ( but under different operating system - Linux) I was able to get 450 of the very same sprites ( using the same algorithm etc - basically replacing GapiDraw blitting code with my own optimized asm routines.) running at 30 frames per second.

I wonder if GapiDraw code uses single pixel writes for non-rectangular ( color key coded ) sprites ?
I was getting similarly low (130 sprites at 30 fps) performance when doing single pixel(2 pixels) at a time.
Yes, the current releases (1.04 and 1.10 beta 2) use single-pixel writes for color keyed surfaces. There are several reasons for this, including both performance across several device configurations and expandability. Let me guess that those 450 sprites are not color keyed. Static surfaces in GapiDraw 1.1 will support fast multiple pixel-writes using RLE compression as long as alpha blends are not used...
warmi · 04 Feb 2003, 03:49 UTC — Sprites
Actually these 500 are the same set of sprites used in MFC demo (color keyed.)
I basically try to load everything using 32 bits or more even if it later means doing shifts/ands and other operations - it is still much faster then hitting memory with 16 bit reads.

Anyway, once I am done with everything I will post ASM code for everyone to use/play around etc...
Static surfaces in GapiDraw 1.1 will support fast multiple pixel-writes using RLE compression as long as alpha blends are not used...

What is a Static surface?

--Eric
warmi: Ok, cool. I'll have a look at it..
efortier: Surfaces with RLE-encoded color key information. Should improve performance significantly...
Wow... And are static surfaces present in 1.10 beta, or will they be available in 1.1 release?

--Eric
warmi · 05 Feb 2003, 03:03 UTC — RLE encoded
Yeah, RLE stuff should be generally faster altough I found that with SDL running on ARM (Sharp Zaurus - just like Ipaq) RLE encoded blitting code was actually slower.
I haven't looked at the code though - perhaps just a case of bad implementation.
efortier: They are not available in the current beta 2...
warmi: I just remembered why I never had the time to implement 32-bit blt.. Basically one has to check for odd word alignment of the top-left pixel of both the source and destination surface. If both are odd or both are even, then it's easy to copy in 32-bit chunks (after taking care of the odd start and possible odd width). If only one is odd then a special routine has to be created that reads and writes in 32-bit blocks that overlap each other.. Not really that big a deal, but it would at least take a full day of work... If performance increase is indeed almost 2x then I guess it could be worth the effort...
Guest · 11 Feb 2003, 16:41 UTC — Fast Blit
Yeah, precisely. In fact I do have three kinds of "subsections" for each blit - one for when destination and source are both either aligned or not aligned ( the fastest case) and two other for either only source or destination being aligned.
The inner loop does require some orring and shifting (it can get ugly like in the example below - a inner loop for source aligned non-keyed blit but in the end it is much faster).

blitNormalSourceAligned_octcopy:

ldmia r1!,{r4-r11}

strh r4, [r0], #2

mov lr,r5, lsl #16
orr r4,lr,r4,lsr #16
mov lr,r6,lsl #16
orr r5,lr,r5,lsr #16
mov lr,r7, lsl #16
orr r6,lr,r6,lsr #16
mov lr,r8,lsl #16
orr r7,lr,r7,lsr #16
mov lr,r9, lsl #16
orr r8,lr,r8,lsr #16
mov lr,r10,lsl #16
orr r9,lr,r9,lsr #16
mov lr,r11, lsl #16
orr r10,lr,r10,lsr #16

stmia r0!,{r4-r10}
mov r11,r11,lsr #16
strh r11,[r0],#2

subs r3,r3, #1
bne blitNormalSourceAligned_octcopy


I am right now cleaning up my code ( basically translating from GAS ( gnu assembler) to EVC++ style assembly) and then I will send you the example blits working with the current GAPI interface (sometime by the end of this week.)
warmi · 18 Feb 2003, 07:13 UTC — Ok - mystery has been solved
I think I solved the problem with slow GapiDraw color-keyed blits.
The good news is that there is no problem at all :-)

I based my original observation on the MFC demo included in the GapiDraw distribution which, to me at least, looks like it was intended to be a benchmark for how many color-keyed sprites can be displayed on the screen while keeping the frame rate at 30 fps.
On my ipaq 3835 device it maxed out at around 130 sprites which seemed like a low number to me.
After investigating it further I finally figured out where the problem lies.
It has nothing to do with the blitting code but rather with the position/acceleration calculation/update loop ( can you believe that ?? )
Using CList for keeping coordinates and other sprite specific variables ( I am not all that familiar with CList and all this stuff - I haven't touched windows programming since 1997 or so) is extremely slow and actually causes the entire frame update process to fall below 30 fps at around 130 sprites.
When I replaced it with a bunch of simple arrays the number of sprites I was able to display on the screen went to 230.
One hell of a CList if you ask me :-)

Anyway, going back to my ASM code - after incorporating it into the MFC example and after "freeing" GapiDraw from the evils of CList, I noticed that the performance gap, which originally was like 200-300 %, shrunk to mere 20-30 % which, generally is pretty much what one can expect when trying to optimize with ASM already optimized C code.
The example timings for some of the bliting routines are ( running on ipaq 3835 .)

GapiDraw's BltFast with GDBLTFAST_KEYSRC: max 230 sprites at 30 fps.

Equivalent ASM blit: max 290 sprites at 30 fps.

GapiDraw's FillRec with 50% (fast) alpha: max 290 32x32 rectangles at 30 fps.

My ASM based RectFill with 50% alpha:maxes out at 420 32x32 rectangles at 30 fps.

etc ..etc
Basically, the GapiDraw code is pretty much as optimized as it can be using C (never say never though :-) ) and further
increase in speed can only be obtained using other tricks like RLE and perhaps span based z-buffer ( basically avoiding drawing whenever it makes sense.)
Originally, I wrote my asm code to create GapiDraw like framework for the Zaurus ( which is a Japanese/Sharp produced PDA - very similar to ipaqs.) but since both , PPC and the ZAurus use ARM cpu - this code runs just as well on Pocket PCs.
If anyone is interested in bunch of asm bliting code then it can be downloaded at http://www.warmi.net/pocketpc/files/Ppc-asm.zip
It contains modified MFC example from GapiDraw distro , tailored to run with my ASM code ( read ReadMe.txt before you attempt to do anything with this code.)

Walter Rawdanik
Thanks a lot for the excellent feedback! ESPECIALLY on CList !!! What a great idea to use MFC for a performance demonstration.. Not !! :)

I will however see to it so that it will catch up those extra 20% as well.. :)

The alpha blended FillRect in GapiDraw was also improved considerably with the release of GapiDraw 1.10. In some cases the performance improvement is nearly 100%.
Walter:

I have been spending some time reading / writing multiple pixels with BltFast and color keys and have some thoughts..

Thought: Two memory reads may be necessary for two pixels.

If one is processing just one pixel at a time (read one pixel, check for mask, copy to destination if needed), it is a simple task of just ignoring those pixels that contain a mask. If operating on two pixels it is not so simple.. The process can be either:

(a)
- Read two source pixels as a DWORD
- Read two destination pixels as a DWORD
- If pixel#1 is mask, copy destination#1 to pixel#1
- If pixel#2 is mask, copy destination#2 to pixel#2
- Copy both pixels as a DWORD to destination
Result: Two memory reads and one memory write for two pixels. The two memory reads are not cache-optimized.

(b)
- Read two source pixels as a DWORD
- If both pixels are masks, do nothing
- If no pixel is mask, copy source to destination
- Otherwise:
- If pixel#1 is mask, copy pixel#1 to destination as a WORD
- If pixel#2 is mask, copy pixel#1 to destination as a WORD
Result: One memory read for two pixels. Zero to two memory writes for two pixels depending on mask.

Right now I am not sure I will put more work into this.. I believe 20-30% performance gain can be had with certain spritemasks by reading/writing multiple pixels simultaneously. I think the current solution (processing one pixel at a time) is a fair middleway with acceptable performance...
Correct.
I actually have a third scenario , a case where source read is not dword-aligned ( this generally happens only when somebody is blitting part of the sprite.)
Another thing to remember is that the "a" scenario can also cover a case where both destination and source are not aligned. You need to check for this and if that is the case use word read for the first pixel and continue with dwords afterwards.

Anyway, I did all of that because I was curious how it will affect performance.
I figured it would be nice to have a baseline keyed blit which is about 20-30% ( even more if care is taken to display sprites at aligned locations) faster
than a single pixel version.
Generally, after going thru all of this, I would agree that putting your resources into things like RLE encoded sprites can give you a much bigger boost in performance.
And this is what I am going to do next - try to incorporate RLE aware blitter into my ZSurface class.
Johan, have you ever though about open sourcing Gapidraw so people like warmi could directly work on the code. They could then submit the code back to you and you could do the "official" gapidraw builds.
Unfortunately my company owns the full rights to the source code. It was my only choice to keep working on GapiDraw - working on late evenings and weekends suddenly became too much work...

There has been a slight delay in releases lately (the past 6 months actually), but things will improve. I really appreciate the work done by warmi and will see if I can integrate it into the core..
warmi · 06 Mar 2003, 00:30 UTC — GapiDraw
In this case there won't be really much to gain by incorporating the asm code.

The C code is already very fast and definitely much cleaner.

I think what he was talking about is having people looking over the source code could only benefit the project but since as you mentioned this code is owned by your company there is not much that can be done here.
In any case, keep up the good work - GapiDraw is already the best Game API for the PocketPC devices.
Sm!rk · 06 Mar 2003, 22:07 UTC — GapiDraw
warmi wrote:
In any case, keep up the good work - GapiDraw is already the best Game API for the PocketPC devices.


I agree, but IMO its now suffering from the same problem all closed source products suffer from, slow updates.

There are some features I want that I could easily add in a few days.
And nearing product completion I'll have to revert back to 1.04 since 1.10 is still beta. And as evidenced by all the changes going to 1.10 I expect there will be more simple changes that will break the dll concept so I *have* to revert.
Just thought I should mention that I haven't given up on this topic yet...

Performance right now: 262 sprites each frame (on the IPAQ)... And I still have a few tricks up my sleeve.. Everything is still 100% C++. The performance difference to Walter's ASM code is less than 11%. GapiDraw also maintains this performance increase with all flags such as opacity included.

Image
warmi · 22 Apr 2003, 14:12 UTC — Fast Blit
Hehe.

Well, I am not goin to stand still - I am working in creating RLE encoded version. :-)

Basically now skipping over transparent areas , in the best possible case, is done on 2 pixel basis ( entire register of data is loaded and compared with value of two transparent pixels.)
but with RLE that could be extended to any number of pixels. On some sprites that would result in superior performance.
But I am sure you are working on that as well :-)
:wink:

I sure am.. :) My first implementation was an ugly one however, than only compacted using "for each row, create RLE-strip from left to right". Adding display rotations to the equation resulted in non-cache-optimized writes.. I am now rewriting the compression code for the RLE strips to always go in the same direction as the display.. Should be interesting...

I think I'll create a small GUI overlay to use as a demo application.. I'll post it later this week so we can both try out our code with it (I guess you'll continue to stick with ASM, and the performance difference could be cool to evaluate)...
warmi · 22 Apr 2003, 16:02 UTC — warmi
Actually, I am only using my ASM on another platform.
I continue to stick with GapiDraw for the PocketPC code - I have entire framework built on top of GapiDraw + I don't have the code to handle all rotations/ workarounds for Gapi bugs etc ...
So , in the end it is easier for me to use GapiDraw.
warmi: HAH! 270 sprites.. :D In plain c++ (well, template meta c++, but almost)!! And that includes all the verifications etc I do for each coordinate + more... I would say that the performance improvement is up to 40% (if both surfaces are aligned) compared to previous code.

Right now I also have three subloops:
(1) both surfaces are aligned or unaligned
Read source as DWORD. check source + each pixel for mask. copy one pixel as WORD or both pixels as DWORD depending on mask.
NOTE: If opacity is enabled, and both pixels should be copied, a real fast 32-bit 3-multiplications-for-two-pixels alpha blend will be performed (or a quick 50/50 if opacity is set to 128)
(2) destination is unaligned
Read source as DWORD. check source + each pixel for mask. copy each pixel as WORD.
(3) source is unaligned
Read source as WORD. check each pixel for mask. copy each pixel as WORD. write destination as DWORD.

The performance is around 7% slower than the optimized ASM routines posted earlier. Right now I am kind of satisfied with the performance. I have boosted the AlphaBltFast as well, and will do some benchmarks tomorrow...

Image