PocketMatrix forums

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

Fastest 565 to 555 routine? and more?

1) What is the fastest 565 to 555 conversion routine? Is there a routine which operates on chunks of pixels at a time that is faster than mine below? Or maybe some low level GapiDraw funcs that are assembly optimized for the 555 devices that I can call?

2) What devices does GapiDraw support and been tested on? I could not find a specific device list anywhere, just the comment of "all 16-bit Pocket PCs and Smartphones".

3) On my desktop PC I have set the frame rate target to 1000, but GapiDraw seems to be limited to 100fps, is there a way to over-ride this?

some background...
Hello all, I am currently nearing the end of a commercial one-man game dev cycle. I am currently using some GAPI bare bones code to get my game's screen onto my iPaq 3630 and it is working quite nicely. I have developed a few PC games and know the value of having an already tested library. This is why I am going to use GapiDraw for the final blit to the device, as I anticipate my GAPI code to not work on all devices, because some use different memory arrangements and bit depths.

My game is in native 565 format and I have already got it working on my 3630 with GapiDraw, I have filled in the 555 routine as well, but it is currently untested, it currently looks like this:

Code:
// do this for all pixels
//from RRRR RGGG GGGB BBBB
//to xRRR RRGG GGGB BBBB
//convert from 16 bit to 15 bit
*dest = (((*src & 0xFFE) >> 6) << 5) | (*src & 0x001F);


Thanks for any advise or help.
Hi,

(1) 555 to 565

GapiDraw never converts 565 surfaces to 555. Surfaces are always stored in one format depending on the device to avoid a final conversion when drawing to the display.

There are several optimizations you could do in your 565 to 555 conversion routine:
(a) Read two pixels at a time
(b) Avoid *src twice on the same row in case the compiler does not read the contents on src to a register and uses that twice.

For example:
DWORD twopixels = *src32;
*dest32 = /* 32-bit 565 to 555 conversion of "twopixels" here */

(2) Devices

Basically it does support all 12-bit and 16-bit devices.. GapiDraw has been downloaded over 5000 times now, and tested on most Pocket PCs and Smartphones. The only device I have gotten reported issues with is the @migo, and I am investing that right now. It is probably just a bad GAPI implementation on that device.

(3) Target FPS 1000

One ---THOUSAND--- FPS? Why on earth would you want that??? Your monitor is only capable of refreshing the display at around 100 hz maximum, so why would you draw graphics to the display when noone can see it??
mm40 · 17 Mar 2003, 17:02 UTC — will this work
Hello, I am releasing a beta next week and just want a second pair of eyes on this to make sure it will work since I dont have a 555 device to test on, this is in my CMyApplication::Render555 function as in the plasma demo

Code:

void CMyApplication::Render555( WORD* pBits, int iwidth, int iheight, long xPitch, long yPitch)
{
// my internal framebuffer is 565
DWORD* src32 = (DWORD*)frameBuffer;
LONG nDestOffset = 0;

for(int i=0; i<iheight; i++)
{
DWORD* dest32 = (DWORD*)(pBits + nDestOffset);

for(int x=0; x<iwidth; x+=2)
{
// from RRRR RGGG GGGB BBBB RRRR RGGG GGGB BBBB
// to xRRR RRGG GGGB BBBB xRRR RRGG GGGB BBBB
// convert from 16 bit to 15 bit
DWORD srcpixel = *src32;
src32++;
*dest32 = ((srcpixel & 0xFFC0FFC0) >> 1) | (srcpixel & 0x001F001F);
dest32 += xPitch;
}

nDestOffset += yPitch;
}
}


And does anyone know specifically what devices are 555?
As I am aware of: Not a single Pocket PC. But several Windows 98/ME systems use 555 format. So if you distribute a windows client of your game it won't work on many Win98/ME systems if you don't support 555...

Check the ASM output.. It's possible you'll get slightly better code with:
DWORD srcpixel = *(src32++);

but probably not.. :)
Thanks for the quick response sbl :) Now for the next questions:

1) Does CGapiSurface::GetBuffer return the same properties as GXGetDisplayProperties?

2) In GDBUFFERDESC, where does pBuffer point to in the following cases

xPitch and yPitch are positive?
xPitch and yPitch are negative?
xPitch is positive and yPitch is negative?
xPitch is negative and yPitch is positive?
(1) Yes, I think so... They should be the same .. :) I guess.. :)

(2) pBuffer always points to pixel origo.. Which can be anywhere in memory (basically "top left", "top right", "bottom left", and "bottom right"). If you want the start of the memory area I'm sure there are some GAPI code floating around that does exactly that (such as clearing the display with memset(pMemStart, 0, 0))...

I guess you could add some code like:
if (xPitch is negative) decrease pBuffer by width and invert xPitch
if (yPitch is negative) increase pBuffer by height*yPitch and invert yPitch (note: yPitch will be negative and will be subtracted from pBuffer)
sbl wrote:
Check the ASM output.. It's possible you'll get slightly better code with:
DWORD srcpixel = *(src32++);

but probably not.. :)


I checked myself, and here is what I found, in release mode:

Code:


// start

; 283 : *S2 = 0;

00794 e1c240b0 strh r4, [r2]
00798 e1a01801 mov r1, r1, lsl #16

; 284 : S2++;

0079c e2822002 add r2, r2, #2
007a0 e1a01821 mov r1, r1, lsr #16

; 285 :
; 286 : *(S2++) = 0;

007a4 e1c240b0 strh r4, [r2]
007a8 e2822002 add r2, r2, #2

// end



So, using *(S2++) takes two instructions instead of four using the normal method.

Or, if you want to impress people more, tell them you found a way to change your app to reduce the size of some instructions by 50% :)

This was done on an S-ARM device, PocketPC2002, EVC++ 3.0, release mode with all optimizations.

--Eric
When I originally read sbl's suggestion, I thought it to be pointless and something any optimizing compiler could easily do. Then someone posted that evc does generate different code which I thought had to be false, or I would lose all faith in evc as a compiler.

So I retested it, I'm not exactly sure what the above poster compiled, but I get different results. However, results that I expected to get so maybe I'm biased.

Here is what I generated and I expect would be what is generated for the above code as well:
Code:
; int srcpixel = *src32;
; src32++;

ldr r0, [pc, #0xC] ; pc+8+12 = 00000014
ldr r1, [r0]
add r1, r1, #4
str r1, [r0]

; int srcpixel = *(src32++);

ldr r0, [pc, #0xC] ; pc+8+12 = 0000002C
ldr r1, [r0]
add r1, r1, #4
str r1, [r0]



One clue is that DWORD is 4 bytes, not 2 bytes like in efortier's code. And what you see in the "extra" code are the shifts needed for accessing the short. One reason accessing two "pixels" at a time is faster at all is because the register size is 32 bits, which is two 16 it pixels. So I don't know exactly why your code generated what it did, I do know it would not be generated for the above case. However, what you didn't post is the loading of the registers which I would include in the cost and have been included above in the output I got.


Here is the real code, identical either way:

Code:

; 60 : // convert from 16 bit to 15 bit
; 61 : int srcpixel = *src32;
; 62 : src32++;
; 63 : *dest32 = ((srcpixel & 0xFFC0FFC0) >> 1) | (srcpixel & 0x001F001F);

mov r10, #0x1F, 16 ; 0x1F0000 = 2031616
ldr r4, [r7]
ldr r11, [pc, #0x38] ; pc+8+56 = 00000114
orr r10, r10, #0x1F ; 0x1F = 31
and r10, r10, r4
and r11, r11, r4, lsr #1
orr r11, r11, r10
sub r0, r0, #1
str r11, [r5]
add r7, r7, #4

; 87 : // convert from 16 bit to 15 bit
; 88 : int srcpixel = *(src32++);
; 89 : *dest32 = ((srcpixel & 0xFFC0FFC0) >> 1) | (srcpixel & 0x001F001F);

mov r10, #0x1F, 16 ; 0x1F0000 = 2031616
ldr r4, [r7]
ldr r11, [pc, #0x38] ; pc+8+56 = 000001A0
orr r10, r10, #0x1F ; 0x1F = 31
and r10, r10, r4
and r11, r11, r4, lsr #1
orr r11, r11, r10
sub r0, r0, #1
str r11, [r5]
add r7, r7, #4



In summary its my belief you are wasting your time trying to second guess the compiler (especially in so slight a way), there are far greater speed ups than combining operations.
efortier wrote:
Code:


// start

; 283 : *S2 = 0;

00794 e1c240b0 strh r4, [r2]
00798 e1a01801 mov r1, r1, lsl #16

; 284 : S2++;

0079c e2822002 add r2, r2, #2
007a0 e1a01821 mov r1, r1, lsr #16

; 285 :
; 286 : *(S2++) = 0;

007a4 e1c240b0 strh r4, [r2]
007a8 e2822002 add r2, r2, #2

// end




You are wrong. The operations on the r1 register have nothing to do with the code you are trying to "optimize". It is obvious that these operations are for some other surrounding C++ statements and that they got inserted there to work around cache or pipeline issues.

The exact same two instructions are used in both cases.
You are wrong. The operations on the r1 register have nothing to do with the code you are trying to "optimize". It is obvious that these operations are for some other surrounding C++ statements and that they got inserted there to work around cache or pipeline issues.

The exact same two instructions are used in both cases.


I reviewed the code and this is absolutely true. I didn't notice it at first because I'm not that great in ASM.

Thanks a lot for posting this observation!

--Eric
well thanks for the all the discussion and assembly, but it turns out this point is moot, since the code doesn't work in all cases.

for example on some devices with some screen rotations, xpitch will be -320 and ypitch will be -1, when you try to write 2 pixels into the last destination pixel (cause that is where the buffer starts you at), guess what, crash, plus you get memory alignment problems since now you might be starting on an odd address and trying to copy in a dword across rows or columns, which some memory systems won't let you do.

so the only way is to copy a single pixel at a time, then add in special cases for certian xpitch ypitch combinations.
mm40 · 27 Mar 2003, 20:06 UTC — how to view assembly in release mode?
how do you view the assembly in release mode? i can do it in debug mode, but can't figure out how to view it in release mode?