Interaction of Clear(), BeginScene(), EndScene(), Present()

Started by
1 comment, last by Namethatnobodyelsetook 15 years, 3 months ago
My friend and I are starting to delve into game programming and had a question we were hoping someone could clear up for us. Every tutorial/book that we see has the following order Clear(); BeginScene(); EndScene(); Present(); At the most simple level (none of the advance features/options), we were wandering what exactly gets operated on between BeginScene and EndScene. From what we can see it is the back buffer. But why then can we operate on it outside of the "locked" area with Clear(). It seems like Clear() should be inside this area. Then it seems that Present() just swaps the current back buffer? We haven't been able to find any useful information on what this function does. We saw code that says that it displays the created frame, but we already unlocked whatever memory we locked before and so it could be corrupted, so that wouldn't make sense. To us it seems that in between begin and end we are operating on memory other than the back buffer and it is displayed before we unlock that memory and that present just performs housekeeping/updating the chain of buffers. Can someone kindly clear this up for us?
Advertisement
You're always working on the "backbuffer" (I put it in quotes, because it a bit more complicated but conceptually you're always working on the backbuffer).

So Clear clears the back buffer. BeginScene tells the driver "I'm about to start with the Draw calls". Each time you call Draw (or any DirectX call, really), the DirectX runtime queues that call to the driver - the driver runs in a separate threads and pulls work off the queue as fast as it can. EndScene will wait for that queue to empty so that when you call Present to swap the back buffer to the front, it knows that the scene has finished.

The whole Clear/BeginScene/EndScene/Present basically allow the DirectX runtime to run asynchronously to the rest of your application.

Note that it's a bit more complicated than that in practise (mainly because the queue can actually last a couple of frames) but that's conceptually how it works.

Also, in DirectX 10, the BeginScene and EndScene calls are gone.
D3D10 kills off begin/endscene because it also kills off most cases of lost devices.

BeginScene locks some GPU resources, guaranteeing that you'll be able access your existing resources until you EndScene. They cannot become lost part way through processing your frame. Not terribly important for clear, but drawing requires vertex buffers, index buffers, textures, render surfaces, renderstates, texturestagestates, shaders, etc. Once you've successfully called BeginScene, your resources will remain valid until you EndScene.

Present may just swap the front and backbuffers, if you created the device with swap effect FLIP. When using AA, you must use swap effect COPY. D3D will resolve the backbuffer AA buffer into a non-AA front buffer. Present may also detect that the GPU is running too far behind the app, and stall to keep your app and the display queue in sync.

This topic is closed to new replies.

Advertisement