Yeo
Yeo

Reputation: 11784

Do we need to clear the buffer if we use double buffering?

Let say, we use double buffering. We first write the frame into the back buffer, then it will be swap into the front buffer to be displayed.

There are 2 scenario here, which I assume have the same outcome.

Thus, assuming I was right and provided we use double buffering, whether clearing or not clearing buffer, both will then end up with the same display, is that true?

Will there be any possible rendering artifacts, if we didn't clear the buffer?

Upvotes: 2

Views: 2475

Answers (4)

Nicol Bolas
Nicol Bolas

Reputation: 473212

Clearing of the buffers is absolutely essential if you like performance on modern hardware. Clearing buffers doesn't necessarily write to memory. It instead does some cache magic such that, whenever the system tries to read from the memory (if it hasn't been written to since it was cleared), it will read the clear color. So it won't even really access that memory.

This is very important for things like the depth buffer. Depth tests/writes are a read/modify/write operation. The first read will essentially be free.

So while you do not technically need to clear the back buffers if you're going to overwrite every pixel, you really should.

Upvotes: 1

ratchet freak
ratchet freak

Reputation: 48186

During rendering you depend on the fact that at least the depth buffer is cleared.

When double buffering the value of the back buffer will (possibly) be that what you rendered 2 frames ago.

If the depth buffer is not cleared then that wall you planted your face on will never go away.

The depth buffer can be cleared by for example rendering a full screen quad textured with your skybox while the depth test is disabled.

Upvotes: 1

datenwolf
datenwolf

Reputation: 162164

After buffer swap the contents of the back buffer are undefined, i.e. they could be anything. Since many OpenGL rendering operations depend on a well known state of the destination frame buffer to work properly (depth testing, stencil testing, blending) the back buffer has to be brought into a well known state before doing anything else.

Hence, unless you take carefull measures to make sure your rendering operations do not depend on destination buffer contents, you'll have to clear the back buffer after a swap before doing anything else.

Upvotes: 0

Kromster
Kromster

Reputation: 7387

The key of the second approach is in this assumption:

the back buffer will be overwritten with a new frame

I assume we are talking about OpenGL frame buffer which contains Color values, Depth, Stencil and etc. How exactly will they be overwritten in next frame?

Rendering code does constant depth comparisons, so see which objects need to be drawn. With old frame depth data it will be all messed up. Same happens if you render any semi-transparent items, with blending enabled.

Clearing buffer is fastest way to reset everything to ground zero (or any other specific value you need).

There are techniques that rely on buffer not being cleared (considering this is verified to be a costly operation on a platform). For example not having transparent geometry without opaque behind it, and toggling depth-test less/greater in 0-0.5 / 0.5-1.0 ranges to make it always overwrite old frames values.

Upvotes: 1

Related Questions