PDA

View Full Version : CGL - How to get back buffer base address?


Twilight
2005.03.20, 02:27 AM
Hi all,

I've setup a double buffered fullscreen CGLContext (kCGLPFADoubleBuffer flag) and use CGLFlushDrawable( contextObj ); to switch between the front and back buffer. OpenGL drawing works perfectly...

For some special pixel manipulation I need to know the base address of the back buffer. I'm able to get the front buffer base address with CGDisplayBaseAddress( kCGDirectMainDisplay ) but how can I get the base address of the back buffer?

OneSadCookie
2005.03.20, 05:50 AM
You can't. It's in VRAM, you don't know the layout of the memory, and you don't know what OpenGL is doing to it. You shouldn't use CGDisplayBaseAddress either, for the same reasons. CGDisplayAddressForPosition is probably safe if you don't have an OpenGL context up...

You have several possibilities open to you; which you choose depends on your performance and hardware requirements, and the precise "pixel manipulation" you need to do.

Best case is, render to a PBuffer, then use ARB_fragment_program to do your pixel swizzling on the way to the back buffer. Requires a GeForce FX or a Radeon 9600 or better.

Next best is to render to a PBuffer, then use CopyTexSubImage2D from the PBuffer into an APPLE_texture_range client storage texture. That'll get you a DMA from the PBuffer into RAM, which is fast and asynchronous. You can then (later, give the DMA time to finish) use GetTexImage2D to flush the DMA, do your pixel swizzling on the CPU, use TexSubImage2D to update the texture back to the card (another DMA), then blit to the backbuffer. You'll end up 1-2 frames behind, but you should get good performance. Requires Mac OS X 10.3.4 or so, and probably a better-than-rage128 card. There's a discussion about precisely how to do all this in the mac-opengl mailing list archives.

If EXT_pixel_buffer_object is ever supported on the Mac, that'll provide a much nicer path than that :)

Worst is to use ReadPixels to get the framebuffer image back to memory, do your pixel swizzling there, then return it to GL using DrawPixels or texturing. ReadPixels is slow and synchronous. You won't be a frame behind, but your framerate will be pitiful. Requires: nothing.

Twilight
2005.03.20, 09:46 AM
Thank for the info! Too bad that its not possible

ARB_fragment_program is out of question due to its limited support...maybe an option in a year or so :)

The PBuffer thing sounds good, only 10.3.4 is a minus but I'll check the mail-lists and test it. I wonder if its faster than render to a hidden window....

EXT_pixel_buffer_object would be great - but well.....maybe next time :)

OneSadCookie
2005.03.20, 03:58 PM
Yeah, it doesn't really matter where you render to originally, though I'd recommend a second context so that you can benefit most from the asynchronicity available. Hidden window should be about as fast as a PBuffer, I just said "PBuffer" 'cos you said "CGL" :)