PDA

View Full Version : glClear



DarioCiao!
09-18-2015, 08:29 AM
I'm unable to find complete documentation about that.

My guess is that most tutorials show wrong usage of states:

my assumption:

only write masks enable/disable clearing a buffer

so to clear stencil:

glStencilMask(GL_TRUE);
glClear(GL_STENCIL_BUFFER_BIT);

to clear depth:

glDepthMask(GL_TRUE);
glClear(GL_DETPH_BUFFER_BIT);

to clear color:

glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT);

if that is true than it is NOT mandatory enabling tests right?

Most tutorials show

glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT);

or

glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT);

Then the tutorials accidentaly work because MASKS by default are true right?

Alfonse Reinheart
09-18-2015, 09:36 AM
I'm unable to find complete documentation about that.

The OpenGL Wiki (https://www.opengl.org/wiki/Framebuffer#Buffer_clearing) seems to cover it well enough.


if that is true than it is NOT mandatory enabling tests right?

Well, if you don't enable the tests, it doesn't matter if the buffer is cleared or not. Right? ;)

Generally speaking, the tutorials aren't saying that you need to enable the test before you clear the buffer. They're saying that, if you want to actually do depth buffering, you need to both enable the depth test and clear the depth buffer to a known value. And since these are things that are usually done once-per-frame, as part of per-frame initialization, the lines have a tendency to be near one another.

Most tutorials will also put glClearColor/Depth/Stencil lines before glClear as well.

As for write masks, yes by default they're on. But most tutorials don't bother talking about them, because they're only useful in a few specific circumstances.

DarioCiao!
09-18-2015, 11:31 AM
Well, if you don't enable the tests, it doesn't matter if the buffer is cleared or not. Right? ;)


MY RENDERING LOOP:
1) clear all buffers at once (color,depth,stencil)
2) Z pre-pass
4) render regular stuff
5) render to stencil holes (make holes for cave entrances)
6) render entrances
7) particles
8) gui (color only)

as you see when I clear buffers I have both depth and stencil tests disabled.

Of course if later I need even more stenciling is possible I have to clear stencil in middle
of rendering loop but actually I don't have those in same lines.

My doubt Is If I should enable Depth and stencil tests just for clearing the buffers, actually
my code works without enabling them, however it could be possible my videodrivers are just "permissive"
and the same code won't work on a different machine. I always check specifications twice, however sometimes
they miss details like this one.

Alfonse Reinheart
09-18-2015, 01:54 PM
MY RENDERING LOOP:
1) clear all buffers at once (color,depth,stencil)
2) Z pre-pass
4) render regular stuff
5) render to stencil holes (make holes for cave entrances)
6) render entrances
7) particles
8) gui (color only)

as you see when I clear buffers I have both depth and stencil tests disabled.

Actually, I don't see that at all. You don't say where tests are enabled or disabled. Indeed, depending on what some of those mean, I don't see any rendering where you would disable the depth test. Even GUIs can benefit from depth testing.


My doubt Is If I should enable Depth and stencil tests just for clearing the buffers

No. You need to enable testing when rendering primitives where you want that testing.


I always check specifications twice, however sometimes
they miss details like this one.

The specification is quite clear; no detail has been missed. The assumed default case is that separate pieces of state are separate and therefore don't interact.

Ergo, if the description of glClear (https://www.opengl.org/sdk/docs/man2/xhtml/glClear.xml) does not say that it is affected by the enabling of various tests, then it is not affected by it. Note that the page there explicitly states that clearing is affected by write masks, the current clear colors, and so forth. But the enable states for tests are not mentioned.

Thus, they don't affect the result.

DarioCiao!
09-19-2015, 12:34 AM
Ok thanks that saves me few GL calls :o


Actually, I don't see that at all. You don't say where tests are enabled or disabled. Indeed, depending on what some of those mean, I don't see any rendering where you would disable the depth test. Even GUIs can benefit from depth testing.


If I don't forgot anything here are main pipeline configurations:

0) disableBlending
1) clear buffers
2) enableDepthTesting && enableDepthWrite && disableColorWriting
4) disableDepthWrite && enableColorWrite
5) disableDepthTesting && disableColorWrite && enableStencilTest && enableDepthWrite
6) enableColorWrite && enableDepthTesting && disableDepthWrite
7a) enableDepthWrite && disableStencilTest // solid particles
7b) disableDepthWrite && enableBlending //transparent and additive particles
8) enableBlending // transparent