PocketMatrix forums

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

Creating video surfaces

Hi, Quick poll: What should happen if you try CreateSurface(GDSURFACE_VIDEOMEMORY, ...) and it does not succeed? Possible causes are:
(1) The program is run on a PDA that does not have video acceleration
(2) The program runs on a Stationary PC with limited video memory

Feel free to post any comments on the issue..

The first option (the D3D way), generally results in code like this (taken from http://www.infinitecode.com/t3b3srctxt.html ):
Code:
dxsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
if(dxdd->CreateSurface(&dxsd, &dxstexture[j*8+i], NULL)!=D3D_OK){
dxsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
if(dxdd->CreateSurface(&dxsd, &dxstexture[j*8+1], NULL)!=D3D_OK) dxerror();
}


Also note that all CreateSurface(GDSURFACE_VIDEOMEMORY, ...) automatically will fail on all current PocketPCs since they have no hw acceleration.
How about:

dxsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;

Will try to use video memory first, and if that fails then it will use system memory.

If the call is done ONLY with DDSCAPS_VIDEOMEMORY and that fails then it should return with an error and leave it to the programmer to do what he wants (possibly use the code that you showed).
Hm. I'm not too keen on this one... It doesn't feel right to supply both flags. I've added a couple of new flags as well (local and non local video memory), so basically it could look like:

CreateSurface(GDSURFACE_VIDEOMEMORY | GDSURFACE_SYSTEMMEMORY | GDSURFACE_LOCALVIDMEM, ...)

Not too good...

Current flags are: GDSURFACE_CLEAR, GDSURFACE_NOCOORDCHECK, GDSURFACE_SYSTEMMEMORY, GDSURFACE_VIDEOMEMORY, GDSURFACE_LOCALVIDMEM, GDSURFACE_NONLOCALVIDMEM

Maybe another flag, like GDSURFACE_FALLBACKTOSYSMEM or similar? This would enable code like:

CreateSurface(GDSURFACE_VIDEOMEMORY | GDSURFACE_LOCALVIDMEM | GDSURFACE_FALLBACKTOSYSMEM, ...)
Oh yeah. Looks VERY nice to me. Also makes more sense.
Is there a reason you would not want it to automatically fall back to system memory? Maybe the flag should be the other way DONTFALLBACK...

It seems like 9 times out of 10 you want the program to just keep going. If I am guessing right video or system memory is transparent to the rest of the calls so if it falls back the only difference is the program would run slower. Right?
Yes, correct, the only difference will be that the game runs slower..

Also, since no current Pocket PCs have HW acceleration built-in, using cross platform code to create surfaces in video memory will always fail on these devices (unless GD is allowed to create in system memory if no video mem is available)...
The Toshiba e740 has an ATI Imageon W100 graphics accelerator chip. There's a Casio device that has been out for almost a year now that has a MediaQ 1188 graphics accelerator chip.

Back on topic: Why don't you use the same model used by DirectDraw for creating video or system memory surfaces? If the caller doesn't specify a location for the surface, the middleware tries to create the surface in video memory. If that fails, it creates it in system memory.
If the caller specifically requests that the surface should reside in video or system memory (by specifying a flag) and that operation fails, the API returns failure. No fall back in this case.
People are very familiar with this concept as it's been this way with DDraw since its inception in 1995.
Cool, I didn't know that you could omit location and DirectDraw would automatically fit the surface at the best possible location. Maybe it's documented in a book somewhere, I find the online documentation to be extremely poor.

Ok, maybe I should be more precise next time: "since no current Pocket PCs have HW acceleration -> with enough video memory to actually be able to store full screen video surfaces <- ...". :D

Still can't figure out why they only put 354 kb into the ATI chip.. Front + backbuffer = 320x240x2x2 = 300kb, and 54 kb is not that much to play around with.. On the other hand, I can't figure out why they chose their current GAPI implementation either (where the GAPI memory address is copied on each EndDraw to the backbuffer which is flipped synced to the vsync of the display).
Here's the actual text from the DX7 docs that explains what I was babbling about earlier in a more precise manner:

By default, for all surfaces except client memory surfaces, DirectDraw attempts to create a surface in local video memory. If there isn't enough local video memory available to hold the surface, DirectDraw will try to use non-local video memory (on some Accelerated Graphics Port-equipped systems), and fall back on system memory if all other types of memory are unavailable. You can explicitly request that a surface be created in a certain type of memory by including the appropriate flags in the associated DDSCAPS2 structure when calling IDirectDraw7::CreateSurface.



If I had to guess, I'd say ATI only put 354K in their W100 chip due to cost. :)

I suppose their GAPI implementation does what it does because that's the easiest way to support the API.

I agree with you in that the chip doesn't seem to be designed for full-screen flipping apps like games in mind. From looking at the W100's specs, it was designed to accelerate graphics operations like video playback and text/windowing drawing operations.

ATI does have an SDK that will let an app control the chip directly. I've heard they have been a bit reluctant to hand it out to just anybody though.
Thanks a lot Digby, I'll just paste that text right into the GapiDraw documentation! ;)
I apologize if this is common knowledge, but does GAPIDraw support client memory surfaces?
Sorry it does not. The internal optimizations require all surfaces involved in a blit operation to have the same orientation and pixel format. One could get a similar "effect" I guess by creating a CGapiSurface in system memory and then call "GetBuffer()" to get the memory address (where X=Y=0), size, pitch and pixel format. Since the surface is created using malloc its address will never change. The biggest issue with this I guess is that you have to call OpenDisplay() first for GapiDraw to get the display orientation... GetBuffer() has zero overhead since it returns the buffer "as is".

By the way, in some tests I did I found that a raw memcpy from a memory area into a compatible video surface was extremely faster than creating a client memory surface and blitting using DirectDraw to a compatible video surface. It should in theory be faster with DirectDraw, but since none of my Graphic Cards (NVidia cards) support DMA copy from system memory to video memory the performance loss was terrible.

I guess the main use for client memory surfaces is that you can avoid Lock() (which really locks the entire OS on Win98 systems that does not support NOSYSLOCK) with all its overhead and render to the memory area directly. I have no experience in practical use of client memory surfaces however, but in my experience they perform really bad.