Correctly restoring the client state, enhanced error handling

This commit is contained in:
Jens Hemprich 2006-08-22 21:14:13 +00:00
parent 44b073af49
commit 20f46da51d

View File

@ -832,6 +832,9 @@ void RenderUpdateState()
{ {
glReportErrors("RenderUpdateState"); glReportErrors("RenderUpdateState");
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.ColorAlphaUnit1);
VERIFY_TEXTURE_ENABLED_STATE();
// This filters out a few state changes in Carmageddon // This filters out a few state changes in Carmageddon
// (changing chromakey mode is expensive) // (changing chromakey mode is expensive)
if (OldGlideState.ChromaKeyMode != Glide.State.ChromaKeyMode) if (OldGlideState.ChromaKeyMode != Glide.State.ChromaKeyMode)
@ -870,14 +873,10 @@ void RenderUpdateState()
bool active_texture_unit_not_coloralpha1 = false; bool active_texture_unit_not_coloralpha1 = false;
bool active_texture_unit_client_state_not_coloralpha1 = false; bool active_texture_unit_client_state_not_coloralpha1 = false;
if (s_bUpdateFogModeState || // color or alpha inversion also triggers a fog mode state so we don't have to check explicitly
s_bUpdateFogColorState || if (s_bUpdateFogModeState || s_bUpdateFogColorState)
s_bUpdateColorInvertState ||
s_bUpdateAlphaInvertState)
{ {
if (InternalConfig.FogMode == OpenGLideFogEmulation_EnvCombine) if (InternalConfig.FogMode == OpenGLideFogEmulation_EnvCombine)
{
if (s_bUpdateFogModeState || s_bUpdateFogColorState)
{ {
glActiveTextureARB(OpenGL.FogTextureUnit); glActiveTextureARB(OpenGL.FogTextureUnit);
if (InternalConfig.EXT_compiled_vertex_array) if (InternalConfig.EXT_compiled_vertex_array)
@ -887,12 +886,17 @@ void RenderUpdateState()
} }
active_texture_unit_not_coloralpha1 = true; active_texture_unit_not_coloralpha1 = true;
glReportError(); glReportError();
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.FogTextureUnit);
// Fog color
if (s_bUpdateFogColorState)
{
s_bUpdateFogColorState = false;
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &OpenGL.FogColor[0]);
glReportError();
} }
// Fog mode
if (s_bUpdateFogModeState) if (s_bUpdateFogModeState)
{ {
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.FogTextureUnit);
s_bUpdateFogModeState = false; s_bUpdateFogModeState = false;
if (Glide.State.FogMode & (GR_FOG_WITH_ITERATED_ALPHA | GR_FOG_WITH_TABLE)) if (Glide.State.FogMode & (GR_FOG_WITH_ITERATED_ALPHA | GR_FOG_WITH_TABLE))
{ {
@ -963,17 +967,16 @@ void RenderUpdateState()
OGLRender.UseEnvCombineFog = false; OGLRender.UseEnvCombineFog = false;
} }
} }
// Fog Color
if (s_bUpdateFogColorState)
{
s_bUpdateFogColorState = false;
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &OpenGL.FogColor[0]);
glReportError();
}
} }
else if (InternalConfig.FogMode != OpenGLideFogEmulation_None) else if (InternalConfig.FogMode != OpenGLideFogEmulation_None)
{ {
if (s_bUpdateFogColorState)
{
s_bUpdateFogColorState = false;
glFogfv(GL_FOG_COLOR, &OpenGL.FogColor[0]);
glReportError();
}
if (s_bUpdateFogModeState) if (s_bUpdateFogModeState)
{ {
s_bUpdateFogModeState = false; s_bUpdateFogModeState = false;
@ -1003,19 +1006,13 @@ void RenderUpdateState()
} }
glReportError(); glReportError();
} }
if (s_bUpdateFogColorState)
{
s_bUpdateFogColorState = false;
glFogfv(GL_FOG_COLOR, &OpenGL.FogColor[0]);
glReportError();
}
} }
// Means fog texture unit is active if (s_bUpdateColorInvertState || s_bUpdateAlphaInvertState)
if (active_texture_unit_not_coloralpha1 == false &&
(s_bUpdateColorInvertState || s_bUpdateAlphaInvertState))
{ {
if (active_texture_unit_not_coloralpha1 == false)
{
active_texture_unit_not_coloralpha1 = true;
glActiveTextureARB(OpenGL.FogTextureUnit); glActiveTextureARB(OpenGL.FogTextureUnit);
if (InternalConfig.EXT_compiled_vertex_array) if (InternalConfig.EXT_compiled_vertex_array)
{ {
@ -1023,9 +1020,7 @@ void RenderUpdateState()
active_texture_unit_client_state_not_coloralpha1 = true; active_texture_unit_client_state_not_coloralpha1 = true;
} }
glReportError(); glReportError();
active_texture_unit_not_coloralpha1 = true;
} }
if (s_bUpdateColorInvertState) if (s_bUpdateColorInvertState)
{ {
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.FogTextureUnit); VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.FogTextureUnit);
@ -1044,6 +1039,9 @@ void RenderUpdateState()
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_EXT, invert ? GL_ONE_MINUS_SRC_ALPHA : GL_SRC_ALPHA); glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_EXT, invert ? GL_ONE_MINUS_SRC_ALPHA : GL_SRC_ALPHA);
} }
} }
}
VERIFY_TEXTURE_ENABLED_STATE();
// Coloralpha state changes // Coloralpha state changes
if (s_bUpdateTextureState || if (s_bUpdateTextureState ||
@ -1161,38 +1159,43 @@ void RenderUpdateState()
if (InternalConfig.EXT_compiled_vertex_array) if (InternalConfig.EXT_compiled_vertex_array)
{ {
glClientActiveTextureARB(OpenGL.ColorAlphaUnit1 + unit_index); glClientActiveTextureARB(OpenGL.ColorAlphaUnit1 + unit_index);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(4, GL_FLOAT, 0, NULL);
active_texture_unit_client_state_not_coloralpha1 = (unit_index != 0); active_texture_unit_client_state_not_coloralpha1 = (unit_index != 0);
} }
active_texture_unit_not_coloralpha1 = (unit_index != 0); active_texture_unit_not_coloralpha1 = (unit_index != 0);
glReportError(); glReportError();
} }
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.ColorAlphaUnit1 + unit_index); if (InternalConfig.EXT_compiled_vertex_array)
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(4, GL_FLOAT, 0, NULL);
}
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
glReportError(); glReportError();
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.ColorAlphaUnit1 + unit_index);
} }
#ifdef OPENGL_DEBUG
else else
{ {
#ifdef OPENGL_DEBUG
GlideMsg("Unused texture unit GL_TEXTURE%d_ARB already disabled\n", unit_index); GlideMsg("Unused texture unit GL_TEXTURE%d_ARB already disabled\n", unit_index);
}
#endif #endif
} }
} }
}
else else
{ {
// Save state changes if fog state hasn't been changed // Any of the updates needs the active texture unit set, but not the client state
// and only coloralpha texture unit 0 is used // Save state changes if fog state hasn't been changed and only coloralpha texture unit 0 is used
const bool set_active_texture_unit = active_texture_unit_not_coloralpha1 || unit_index != 0; const bool set_active_texture_unit = active_texture_unit_not_coloralpha1 || unit_index != 0;
if (set_active_texture_unit) if (set_active_texture_unit)
{ {
glActiveTextureARB(OpenGL.ColorAlphaUnit1 + unit_index); glActiveTextureARB(OpenGL.ColorAlphaUnit1 + unit_index);
glReportError(); glReportError();
active_texture_unit_not_coloralpha1 = (unit_index != 0); active_texture_unit_not_coloralpha1 = (unit_index != 0);
// The client state is changed only if the texture unit must be enabled, otherwise the
// active client texture unit is restored to color alpha unit 1 at the end
} }
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.ColorAlphaUnit1 + unit_index); // Don't check VERIFY_ACTIVE_TEXTURE_UNIT yet as it hasn't been set up completely yet
// Texture state // Texture state
if (s_bUpdateTextureState) if (s_bUpdateTextureState)
@ -1227,9 +1230,21 @@ void RenderUpdateState()
#endif #endif
} }
} }
#ifdef OPENGL_DEBUG
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.ColorAlphaUnit1 + unit_index); // At this point, the client state doesn't matter anymore, and we just need to check the active texture
GLint y;
const GLint x = OpenGL.ColorAlphaUnit1 + unit_index;
glGetIntegerv(GL_ACTIVE_TEXTURE_ARB, &y);
glReportError();
const bool verified = x == y;
if (!verified)
{
GlideMsg("Warning: %s() active texture unit is GL_TEXTURE%d_ARB, but should be GL_TEXTURE%d_ARB\n", __glide_functionname, y - GL_TEXTURE0_ARB, x - GL_TEXTURE0_ARB);
}
#ifdef OGL_STOP_ON_GL_ERROR
assert(verified);
#endif
#endif
// ColorCombineState // ColorCombineState
if (s_bUpdateColorCombineState) if (s_bUpdateColorCombineState)
{ {
@ -1402,7 +1417,7 @@ void RenderUpdateState()
const GrCombineLocal_t a_local = Glide.State.AlphaLocal; const GrCombineLocal_t a_local = Glide.State.AlphaLocal;
const GrCombineOther_t a_other = Glide.State.AlphaOther; const GrCombineOther_t a_other = Glide.State.AlphaOther;
const CombineArgument** combine_argument = &OpenGL.AlphaCombineArguments[0]; const CombineArgument** combine_argument = &OpenGL.AlphaCombineArguments[0];
// combine_argument[CFARG_Local] = 0; // This is set to 0 onn initialising OpenGL // combine_argument[CFARG_Local] = 0; // This is set to 0 on initialising OpenGL
// combine_argument[CFARG_Other] = 0; // combine_argument[CFARG_Other] = 0;
combine_argument[CFARG_LocalAlpha] = &AlphaCombineLocals[a_local]; combine_argument[CFARG_LocalAlpha] = &AlphaCombineLocals[a_local];
combine_argument[CFARG_OtherAlpha] = Glide.State.TextureCombineAInvert ? &AlphaCombineOthersInverted[a_other] : &AlphaCombineOthers[a_other]; combine_argument[CFARG_OtherAlpha] = Glide.State.TextureCombineAInvert ? &AlphaCombineOthersInverted[a_other] : &AlphaCombineOthers[a_other];
@ -1565,16 +1580,16 @@ void RenderUpdateState()
} }
} }
} }
// Restore state
if (active_texture_unit_not_coloralpha1) if (active_texture_unit_not_coloralpha1)
{ {
glActiveTextureARB(OpenGL.ColorAlphaUnit1);
glReportError();
}
if (active_texture_unit_client_state_not_coloralpha1) if (active_texture_unit_client_state_not_coloralpha1)
{ {
glClientActiveTextureARB(OpenGL.ColorAlphaUnit1); glClientActiveTextureARB(OpenGL.ColorAlphaUnit1);
} }
glActiveTextureARB(OpenGL.ColorAlphaUnit1);
glReportError();
}
VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.ColorAlphaUnit1); VERIFY_ACTIVE_TEXTURE_UNIT(OpenGL.ColorAlphaUnit1);
VERIFY_TEXTURE_ENABLED_STATE(); VERIFY_TEXTURE_ENABLED_STATE();