Jump to content
  • Advertisement
Sign in to follow this  
Madrankey

Understanding the Swap Chain

This topic is 2431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone,

I have been reading about the concept of the swap chain from directxtutorial.com, and a little from MSDN also. Unfortunately, I still feel a little lost. I understand that if the front buffer was updated during a screen refresh it would cause tearing, but wouldn't flipping buffers do the same thing? Do buffers not also get flipped during a screen refresh? I'm confused how flipping solves the problem of tearing unless it only occurs during a vertical sync.

Share this post


Link to post
Share on other sites
Advertisement
When you update directly to the front buffer, you draw every piece to something that's active, causing strange effects. To solve this, you can draw somewhere else and then draw the result to the front. Simply by doing it you guarantee your last frame will stay fully visible until your next one is ready, so you can draw it and wait for another frame.

Immagine this:
You have just the front buffer and want to draw the player, 5 monsters, the background and 20 projectiles:

Your rendering cycle is: draw background -> draw player -> draw monsters -> draw projectiles
Now you clear the screen to prepare the next draw, this will cause some flicker since this clearing is being rendered.
After that, you'll make 27 draws per frame, while showing every single draw to the screen one at a time, flickering them even more.
In this case, the projectiles will have the more flickering since they'll stay the least time rendered before getting erased.


Now you have a front and a back buffer:

Your rendering cycle is the same, but it's being drawn on the back buffer that's not visible.
Now you clear the back buffer, but as it's not being rendered this "clear" wont be visible.
You'll make 27 draws per frame, but while you do this the screen will be showing the last frame.
Now, you bring the back buffer to the front and render it just once, interrupting the visibility only once per frame and allowing every visible object to remain visible for the same amount of time.
This will avoid the flickering, since you'll be drawing the full screen only once and the buffer clearing won't be shown, passing from a fully rendered frame to other.

Share this post


Link to post
Share on other sites

Hello everyone,

I have been reading about the concept of the swap chain from directxtutorial.com, and a little from MSDN also. Unfortunately, I still feel a little lost. I understand that if the front buffer was updated during a screen refresh it would cause tearing, but wouldn't flipping buffers do the same thing? Do buffers not also get flipped during a screen refresh? I'm confused how flipping solves the problem of tearing unless it only occurs during a vertical sync.


Tearing in the normal sense only occurs if you use double buffering since the image is then rendered completely and then swapped over to the front buffer from the top, without double buffering you get flickering instead and for v-sync to work properly with only a single buffer the entire render has to complete during a vertical retrace.

Share this post


Link to post
Share on other sites
So you draw to a back buffer, and wait for the previous frame stored in the front buffer to be fully drawn to screen before flipping the buffers?

Share this post


Link to post
Share on other sites

So you draw to a back buffer, and wait for the previous frame stored in the front buffer to be fully drawn to screen before flipping the buffers?


That's basically what double buffering will do for you - you don't need to explicitly wait yourself; the hardware and/or API will handle that part for you.

The sequence is just:
- draw as normal
- swap buffers
- repeat

There's nothing else you explicitly need to do.

The tearing you then get when vsync is disabled occurs when the buffer swap occurs out of sync with the monitor refresh (as defined by it's refresh rate) - what you're actually seeing is a portion of the new backbuffer occupying one edge of the screen and a portion of the old occupying the other, hence the horizontal line across the screen.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!