PDA

View Full Version : Query depth buffer state bit? (find out whether it's enabled or disabled)



Keithster
01-03-2016, 09:02 PM
Greetings!

In my scene, I draw gizmos on objects I pick in the scene (three, rgb lines). I disable depth testing when I draw them. But then I draw a grid, and want to turn on depth testing so it doesn't overwrite pixels from the rest of the scene.

However; I always like it to be obvious immediately who's setting/clearing the depth buffer bit, I've had problems where I accidentally disabled/enabled depth testing where I shouldn't.

This is trivial to solve if I could let functions modify the depth buffer this way:



GLboolOrSomething ZState = glGetBit(GL_DEPTH_BUFFER_BIT);

glDisable(GL_DEPTH_BUFFER_BIT);
// draw...

glEnable(GL_DEPTH_BUFFER_BIT);
// draw...

glSetBit(GL_DEPTH_BUFFER_BIT, ZState);


Then I could wrap this in two calls: BeginDepthState(NewState) (sets the new value) and EndDepthState() (reverts back to the previous value)

(I don't necessarily need the glSetBit because I could just do if (ZState) glEnable(...) else glDisable(...) but it would be nice to have)

I know of glPushAttrib but they say it's deprecated. I also heard of glGet but I don't seem to have it available, I'm using core profile only functions (parsed from glcorearb.h)

Any ideas?

Thanks!
-Keithster

Keithster
01-03-2016, 09:19 PM
I don't know why I thought about it right after asking, but I could actually implement this on my side:




struct gl_depth_state
{
ARRAY_STATIC(b32, DepthStack, 32);
};

internal inline void
GLSetDepthState(b32 Value)
{
if (Value)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
}

internal inline void
GLPushDepthState(gl_depth_state *State, b32 Value)
{
ARRAY_ADD(State->DepthStack, Value);
GLSetDepthState(Value);
}


internal inline void
GLPopDepthState(gl_depth_state *State)
{
b32 PreviousValue = ARRAY_LAST(State->DepthStack);
GLSetDepthTest(PreviousValue);
ARRAY_REMOVE_LAST(State->DepthStack);
}



But still interested if there's a built-in way to do it.

GClements
01-04-2016, 03:31 AM
It seems that you want glIsEnabled (https://www.opengl.org/sdk/docs/man4/html/glIsEnabled.xhtml).

Note that this has the same performance caveats as glGet*, i.e. it might result in a pipeline stall if the implementation has to read the state from from the hardware.