PDA

View Full Version : What happens to the front buffer after I swap the buffers?



Coconut_Crab
09-04-2004, 02:45 PM
Hi, this is a quick question.

What happenes to the front buffer after I swap buffers, assuming I am in double buffering mode? Will the new back buffer contain the same data as what it had when it was the front buffer, or does it now contain something undefined?

Thanks in advance for the reply.

Coco

evanGLizr
09-04-2004, 04:52 PM
Originally posted by Coconut_Crab:
Hi, this is a quick question.

What happenes to the front buffer after I swap buffers, assuming I am in double buffering mode? Will the new back buffer contain the same data as what it had when it was the front buffer, or does it now contain something undefined?

Thanks in advance for the reply.

CocoSwapping buffers functionality doesn't belong to OpenGL, it belongs to the glue layer between OpenGL and the OS (WGL, GLX or AGL).
In WGL what happens depends on the pixelformat (http://msdn.microsoft.com/library/en-us/opengl/ntopnglr_73jm.asp?frame=true) you have selected:


With the glAddSwapHintRectWIN extension function, two new flags are included for the PIXELFORMATDESCRIPTOR pixel format structure. Value Meaning

PFD_SWAP_COPY Specifies the content of the back buffer in the double-buffered main color plane following a buffer swap. Swapping the color buffers causes the content of the back buffer to be copied to the front buffer. The content of the back buffer is not affected by the swap. PFD_SWAP_COPY is a hint only and might not be provided by a driver.

PFD_SWAP_EXCHANGE Specifies the content of the back buffer in the double-buffered main color plane following a buffer swap. Swapping the color buffers causes the exchange of the back buffer's content with the front buffer's content. Following the swap, the back buffer's content contains the front buffer's content before the swap. PFD_SWAP_EXCHANGE is a hint only and might not be provided by a driver.
Note that both flags are hints, so check for the PIXELFORMATDESCRIPTOR returned by the driver when you query it with DescribePixelformat (http://msdn.microsoft.com/library/en-us/opengl/ntopnglr_9ygk.asp?frame=true) . In the default case, the content of the backbuffer is undefined after a swap (which may even change in runtime depending on the window layout - you may get sometimes a swap and sometimes a blit).

If you want to use your backbuffer as a backstorage, the safest way is to use glCopyPixels to copy from the back to the frontbuffer.

MatthewL
09-04-2004, 09:59 PM
Hi! ;)

The opengl documentation file I have for win32, which is actually part of the Platform SDK, has a small section on just that!

I don't know where I got this file, but it's called opengl.chm and about 650 K large.
Under "Win32 extensions to opengl"->"OpenGL on Windows NT, Windows 2000, and Windows 95/98"->"Front, Back, and Other Buffers"->"Buffer Functions" it explains what happens to the buffers clearly.

And sure, like evanGLizr says, after a swap the content is undefined. :)

Coconut_Crab
09-05-2004, 06:22 AM
Thank you for the replies. They are both very helpful.

But as you said, that the data might be undefined after the buffer swapping, how can I make sure if I want to save the screen to a BMP file the file will be correct?

I cannot copy the back buffer to a file after the swapping because the data might be undefined on different OSs, and in the FAQ on this website it is said that if I copy the content of the OpenGL window while another window overlaps that window, I will copy what's on top of the OpenGL window.

So, should the solution be, creating a BMP file, copy the content of the back buffer to the BMP, then swap the buffer?

I just want to render an image to a file, that's all. I've read the red book in which it said I could render the image to a buffer instead of to the screen, but it didn't have a code example to demostrate that. Can someone enlighten me on how to do it?

Thanks in advance for any helps.

Coco

Coconut_Crab
09-05-2004, 06:26 AM
Oh and I just searched my C drive for opengl.chm, but couldn't find it. Mind telling where I can find it, maybe somewhere on the internet?

Thanks.

jabe
09-05-2004, 10:22 AM
So, should the solution be, creating a BMP file, copy the content of the back buffer to the BMP, then swap the buffer?
yes.

jwatte
09-05-2004, 05:53 PM
Neither the front nor the back buffer are defined to be valid for areas of the viewport that are overlapped by other windows. Thus, you can't take a good screen shot (necessarily) if, for example, the user has a Windows Media Player window, or Internet browser, overlapping your window. You must render to a pbuffer to be sure that all parts of your back buffer are valid.

jabe
09-06-2004, 01:53 AM
i'm just curious about it. it works for me even with overlapped windows. i had to read back the back buffer because the front buffer had the limitations that were already discussed.

Relic
09-06-2004, 04:07 AM
Originally posted by jabe:
i'm just curious about it. it works for me even with overlapped windows. i had to read back the back buffer because the front buffer had the limitations that were already discussed.It's implementation dependent. OpenGL specs are pretty clear about it:

"4.1.1 Pixel Ownership Test
The first test is to determine if the pixel at location (xw, yw) in the framebuffer
is currently owned by the GL (more precisely, by this GL context). If it is not,
the window system decides the fate the incoming fragment. Possible results are
that the fragment is discarded or that some subset of the subsequent per-fragment
operations are applied to the fragment. This test allows the window system to
control the GLís behavior, for instance, when a GL window is obscured."

That means you can't draw into overlapped regions reliably and if you can't draw into some areas it's futile to read from there either:

"ReadPixels obtains values from the selected buffer from each pixel with lower
left hand corner at (x+i, y+j) for 0 <= i < width and 0 <= j < height; this pixel
is said to be the ith pixel in the jth row. If any of these pixels lies outside of the
window allocated to the current GL context, the values obtained for those pixels
are undefined. Results are also undefined for individual pixels that are not owned
by the current context. Otherwise, ReadPixels obtains values from the selected
buffer, regardless of how those values were placed there."

That it worked for you doesn't mean it works on other implementations.

jabe
09-06-2004, 04:28 AM
That it worked for you doesn't mean it works on other implementations.
of course. i cannot check any available implementations. i just answered from my experiences.
nevertheless, some things become clearer now. thanks.

Coconut_Crab
09-06-2004, 03:20 PM
Can someone please give me a code example on how to render an image to a user defined buffer?

Also, if I want to copy the content of the back buffer to a user defined buffer, should the pixel copying code be in the place right before GlutSwapBuffers(), assuming I am using Glut?

Thanks.

Coco

jwatte
09-06-2004, 05:57 PM
Look at the spec for WGL_ARB_pbuffer.

Basically, you allocate the pbuffer DC, allocate a GLRC on that DC, and wglMakeCurrent() to draw in that buffer. Then treat it as any other GL context. wglShareLists() will work IF the pbuffer is "compatible" with the screen, which is somewhat ill-defined.

If you're not on Windows, I presume other OS-es have a similar extension.

V-man
09-07-2004, 05:31 AM
I hope all OSs have this by now.

GLX_SGIX_pbuffer
http://oss.sgi.com/projects/ogl-sample/registry/SGIX/pbuffer.txt

GL_APPLE_pixel_buffer
http://developer.apple.com/opengl/extensions.html#GL_APPLE_pixel_buffer

Coconut_Crab
09-07-2004, 03:35 PM
Is there an OS independent way of allocating a buffer and tell OpenGL to render to that buffer? Does OpenGL have a native way to do it already or does it have to go through the OS? In the OpenGL Red Book it's only mentioned that we can render to an offscreen buffer, but it doesn't say it has to use an OS dependent routine, nor does it have a code example in the book.

V-man
09-07-2004, 05:50 PM
No, but pretty soon this should happen, and even then it will take time for it to be available on every system. See the other long thread with 46 posts.

If you want demoes using WGL, see http://download.nvidia.com/developer/SDK/Individual_Samples/samples.html

yjh1982
09-07-2004, 06:38 PM
I think glCopyPixels will flush the
opengl command.
if you glCopyPixels before
swap buffer;the data is correctly.

Relic
09-07-2004, 10:00 PM
No, pixel ownership test comes first. Read my post above.
If you want an unclipped offscreen buffer with hardware rendering you must use p-buffers today.

Coconut_Crab
09-08-2004, 02:33 PM
Thanks, yjh1982, I'll try that.

And what is a p-buffer? I mean what's the difference between a p-buffer and a regular buffer?

jwatte
09-08-2004, 07:30 PM
Is there an OS independent way of allocating a buffer and tell OpenGL to render to that buffer?
No. Until this point, surface and context allocation have been reserved for the host windowing system, and OpenGL has been focused on rasterization.



what is a p-buffer?
Exactly that which is specified by the WGL_ARB_pbuffer and GLX_SGIX_pbuffer specifications.