Skip to content

Commit

Permalink
Refactor: Removed GL_SelectTexUnits()
Browse files Browse the repository at this point in the history
This ancient function made the assumption that the only texture type
we use is GL_TEXTURE_2D, which, while presently correct, will not be
true for much longer. Furthermore, changing the active state of a GL
texture unit should not inherently result in the implicit enabling or
disabling of a specific texture type for that unit. Thus the logic of
this function is no longer suitable as a global utility routine.

The two remaining users of this function (the render lists and sprite
renderer modules) now implement their own versions of this function.
  • Loading branch information
danij-deng committed Jan 24, 2012
1 parent e247a14 commit d7ebc28
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 72 deletions.
1 change: 0 additions & 1 deletion doomsday/engine/portable/include/gl_main.h
Expand Up @@ -77,7 +77,6 @@ void GL_UseFog(int yes);
void GL_LowRes(void);

void GL_ModulateTexture(int mode);
void GL_SelectTexUnits(int count);
void GL_SetVSync(boolean on);
void GL_SetMultisample(boolean on);
void GL_BlendOp(int op);
Expand Down
40 changes: 3 additions & 37 deletions doomsday/engine/portable/src/dgl_common.c
Expand Up @@ -331,41 +331,9 @@ boolean GL_Grab(int x, int y, int width, int height, dgltexformat_t format, void
return true;
}

static __inline void enableTexUnit(byte id)
{
glActiveTexture(GL_TEXTURE0 + id);
glEnable(GL_TEXTURE_2D);
}

static __inline void disableTexUnit(byte id)
{
glActiveTexture(GL_TEXTURE0 + id);
glDisable(GL_TEXTURE_2D);
}

/**
* The first selected unit is active after this call.
*/
void GL_SelectTexUnits(int count)
{
int i;
for(i = numTexUnits - 1; i >= count; i--)
{
disableTexUnit(i);
}

// Enable the selected units.
for(i = count - 1; i >= 0; i--)
{
if(i >= numTexUnits) continue;
enableTexUnit(i);
}
}

void GL_SetVSync(boolean on)
{
if(!GL_state.features.vsync)
return;
if(!GL_state.features.vsync) return;
#ifdef WIN32
wglSwapIntervalEXT(on? 1 : 0);
#endif
Expand All @@ -375,10 +343,8 @@ void GL_SetMultisample(boolean on)
{
if(!GL_state.features.multisample) return;
#if WIN32
if(on)
glEnable(GL_MULTISAMPLE_ARB);
else
glDisable(GL_MULTISAMPLE_ARB);
if(on) glEnable(GL_MULTISAMPLE_ARB);
else glDisable(GL_MULTISAMPLE_ARB);
#endif
}

Expand Down
74 changes: 48 additions & 26 deletions doomsday/engine/portable/src/rend_list.c
Expand Up @@ -1355,10 +1355,32 @@ static void drawPrimitives(int conditions, uint coords[MAX_TEX_UNITS],
}
}

/**
* The first selected unit is active after this call.
*/
static void selectTexUnits(int count)
{
int i;
for(i = numTexUnits - 1; i >= count; i--)
{
glActiveTexture(GL_TEXTURE0 + i);
glDisable(GL_TEXTURE_2D);
}

// Enable the selected units.
for(i = count - 1; i >= 0; i--)
{
if(i >= numTexUnits) continue;

glActiveTexture(GL_TEXTURE0 + i);
glEnable(GL_TEXTURE_2D);
}
}

/**
* Set per-list GL state.
*
* @return The conditions to select primitives.
* @return The conditions to select primitives.
*/
static int setupListState(listmode_t mode, rendlist_t* list)
{
Expand All @@ -1372,14 +1394,14 @@ static int setupListState(listmode_t mode, rendlist_t* list)
// Should we do blending?
if(TU(list, TU_INTER)->tex)
{
float color[4];
float color[4];

// Blend between two textures, modulate with primary color.
#ifdef _DEBUG
if(numTexUnits < 2)
Con_Error("setupListState: Not enough texture units.\n");
#endif
GL_SelectTexUnits(2);
selectTexUnits(2);

rlBindTo(0, TU(list, TU_PRIMARY));
rlBindTo(1, TU(list, TU_INTER));
Expand All @@ -1397,7 +1419,7 @@ if(numTexUnits < 2)
else
{
// Normal modulation.
GL_SelectTexUnits(1);
selectTexUnits(1);
rlBind2(TU(list, TU_PRIMARY));
GL_ModulateTexture(1);
}
Expand Down Expand Up @@ -1428,7 +1450,7 @@ if(numTexUnits < 2)
Con_Error("setupListState: Not enough texture units.\n");
#endif

GL_SelectTexUnits(2);
selectTexUnits(2);

rlBindTo(0, TU(list, TU_PRIMARY));
rlBindTo(1, TU(list, TU_INTER));
Expand Down Expand Up @@ -1472,7 +1494,7 @@ if(numTexUnits < 2)
if(numTexUnits < 2)
Con_Error("setupListState: Not enough texture units.\n");
#endif
GL_SelectTexUnits(2);
selectTexUnits(2);

rlBindTo(0, TU(list, TU_PRIMARY));
rlBindTo(1, TU(list, TU_INTER));
Expand All @@ -1485,7 +1507,7 @@ if(numTexUnits < 2)
return DCF_SET_MATRIX_TEXTURE0 | DCF_SET_MATRIX_TEXTURE1;
}
// No modulation at all.
GL_SelectTexUnits(1);
selectTexUnits(1);
rlBind2(TU(list, TU_PRIMARY));
GL_ModulateTexture(0);
return DCF_SET_MATRIX_TEXTURE0 | (mode == LM_MOD_TEXTURE_MANY_LIGHTS ? DCF_MANY_LIGHTS : 0);
Expand All @@ -1496,15 +1518,15 @@ if(numTexUnits < 2)
break;
if(TU(list, TU_PRIMARY_DETAIL)->tex)
{
GL_SelectTexUnits(2);
selectTexUnits(2);
GL_ModulateTexture(9); // Tex+Detail, no color.
rlBindTo(0, TU(list, TU_PRIMARY));
rlBindTo(1, TU(list, TU_PRIMARY_DETAIL));
return DCF_SET_MATRIX_TEXTURE0 | DCF_SET_MATRIX_DTEXTURE1;
}
else
{
GL_SelectTexUnits(1);
selectTexUnits(1);
GL_ModulateTexture(0);
rlBind2(TU(list, TU_PRIMARY));
return DCF_SET_MATRIX_TEXTURE0;
Expand All @@ -1526,7 +1548,7 @@ if(numTexUnits < 2)
break;
if(TU(list, TU_PRIMARY_DETAIL)->tex)
{
GL_SelectTexUnits(2);
selectTexUnits(2);
GL_ModulateTexture(8);
rlBindTo(0, TU(list, TU_PRIMARY));
rlBindTo(1, TU(list, TU_PRIMARY_DETAIL));
Expand All @@ -1535,7 +1557,7 @@ if(numTexUnits < 2)
else
{
// Normal modulation.
GL_SelectTexUnits(1);
selectTexUnits(1);
GL_ModulateTexture(1);
rlBind2(TU(list, TU_PRIMARY));
return DCF_SET_MATRIX_TEXTURE0;
Expand Down Expand Up @@ -1583,7 +1605,7 @@ if(numTexUnits < 2)
case LM_MASKED_SHINY:
if(TU(list, TU_INTER)->tex)
{
GL_SelectTexUnits(2);
selectTexUnits(2);
// The intertex holds the info for the mask texture.
rlBindTo(1, TU(list, TU_INTER));
{ float color[4];
Expand All @@ -1594,7 +1616,7 @@ if(numTexUnits < 2)
case LM_SHINY:
rlBindTo(0, TU(list, TU_PRIMARY));
if(!TU(list, TU_INTER)->tex)
GL_SelectTexUnits(1);
selectTexUnits(1);

// Render all primitives.
if(mode == LM_ALL_SHINY)
Expand Down Expand Up @@ -1670,15 +1692,15 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])
switch(mode)
{
case LM_SKYMASK:
GL_SelectTexUnits(0);
selectTexUnits(0);
glDisable(GL_ALPHA_TEST);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
break;

case LM_BLENDED:
GL_SelectTexUnits(2);
selectTexUnits(2);
case LM_ALL:
// The first texture unit is used for the main texture.
coords[0] = TCA_MAIN + 1;
Expand All @@ -1698,7 +1720,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])
case LM_LIGHT_MOD_TEXTURE:
case LM_TEXTURE_PLUS_LIGHT:
// Modulate sector light, dynamic light and regular texture.
GL_SelectTexUnits(2);
selectTexUnits(2);
if(mode == LM_LIGHT_MOD_TEXTURE)
{
coords[0] = TCA_LIGHT + 1;
Expand All @@ -1725,7 +1747,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])

case LM_FIRST_LIGHT:
// One light, no texture.
GL_SelectTexUnits(1);
selectTexUnits(1);
coords[0] = TCA_LIGHT + 1;
GL_ModulateTexture(6);
glDisable(GL_ALPHA_TEST);
Expand All @@ -1738,7 +1760,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])

case LM_BLENDED_FIRST_LIGHT:
// One additive light, no texture.
GL_SelectTexUnits(1);
selectTexUnits(1);
coords[0] = TCA_LIGHT + 1;
GL_ModulateTexture(7); // Add light, no color.
glEnable(GL_ALPHA_TEST);
Expand All @@ -1752,7 +1774,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])
break;

case LM_WITHOUT_TEXTURE:
GL_SelectTexUnits(0);
selectTexUnits(0);
GL_ModulateTexture(1);
glDisable(GL_ALPHA_TEST);
glDepthMask(GL_TRUE);
Expand All @@ -1763,7 +1785,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])
break;

case LM_LIGHTS:
GL_SelectTexUnits(1);
selectTexUnits(1);
coords[0] = TCA_MAIN + 1;
GL_ModulateTexture(1);
glEnable(GL_ALPHA_TEST);
Expand Down Expand Up @@ -1827,7 +1849,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])
break;

case LM_ALL_DETAILS:
GL_SelectTexUnits(1);
selectTexUnits(1);
coords[0] = TCA_MAIN + 1;
GL_ModulateTexture(0);
glDisable(GL_ALPHA_TEST);
Expand All @@ -1851,7 +1873,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])
break;

case LM_BLENDED_DETAILS:
GL_SelectTexUnits(2);
selectTexUnits(2);
coords[0] = TCA_MAIN + 1;
coords[1] = TCA_BLEND + 1;
GL_ModulateTexture(3);
Expand All @@ -1877,7 +1899,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])

case LM_SHADOW:
// A bit like 'negative lights'.
GL_SelectTexUnits(1);
selectTexUnits(1);
coords[0] = TCA_MAIN + 1;
GL_ModulateTexture(1);
glEnable(GL_ALPHA_TEST);
Expand All @@ -1896,7 +1918,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])
break;

case LM_SHINY:
GL_SelectTexUnits(1);
selectTexUnits(1);
coords[0] = TCA_MAIN + 1;
GL_ModulateTexture(1); // 8 for multitexture
glDisable(GL_ALPHA_TEST);
Expand All @@ -1914,7 +1936,7 @@ static void setupPassState(listmode_t mode, uint coords[MAX_TEX_UNITS])
break;

case LM_MASKED_SHINY:
GL_SelectTexUnits(2);
selectTexUnits(2);
coords[0] = TCA_MAIN + 1;
coords[1] = TCA_BLEND + 1; // the mask
GL_ModulateTexture(8); // same as with details
Expand Down Expand Up @@ -2265,7 +2287,7 @@ END_PROF( PROF_RL_RENDER_SHADOW );
}

// Return to the normal GL state.
GL_SelectTexUnits(1);
selectTexUnits(1);
GL_ModulateTexture(1);
glDisable(GL_TEXTURE_2D);
glDepthMask(GL_TRUE);
Expand Down
37 changes: 29 additions & 8 deletions doomsday/engine/portable/src/rend_sprite.c
Expand Up @@ -552,16 +552,38 @@ void Rend_Draw2DPlayerSprites(void)
glDisable(GL_FOG);
}

/**
* The first selected unit is active after this call.
*/
static void selectTexUnits(int count)
{
int i;
for(i = numTexUnits - 1; i >= count; i--)
{
glActiveTexture(GL_TEXTURE0 + i);
glDisable(GL_TEXTURE_2D);
}

// Enable the selected units.
for(i = count - 1; i >= 0; i--)
{
if(i >= numTexUnits) continue;

glActiveTexture(GL_TEXTURE0 + i);
glEnable(GL_TEXTURE_2D);
}
}

/**
* A sort of a sprite, I guess... Masked walls must be rendered sorted
* with sprites, so no artifacts appear when sprites are seen behind
* masked walls.
*/
void Rend_RenderMaskedWall(rendmaskedwallparams_t *params)
void Rend_RenderMaskedWall(rendmaskedwallparams_t* params)
{
boolean withDyn = false;
int normal = 0, dyn = 1;
GLenum normalTarget, dynTarget;
GLenum normalTarget, dynTarget;
boolean withDyn = false;
int normal = 0, dyn = 1;

// Do we have a dynamic light to blend with?
// This only happens when multitexturing is enabled.
Expand All @@ -578,7 +600,7 @@ void Rend_RenderMaskedWall(rendmaskedwallparams_t *params)
dyn = 1;
}

GL_SelectTexUnits(2);
selectTexUnits(2);
GL_ModulateTexture(IS_MUL ? 4 : 5);

// The dynamic light.
Expand All @@ -601,7 +623,6 @@ void Rend_RenderMaskedWall(rendmaskedwallparams_t *params)
}
else
{
GL_SelectTexUnits(1);
GL_ModulateTexture(1);

glBindTexture(GL_TEXTURE_2D, renderTextures? params->tex : 0);
Expand Down Expand Up @@ -650,7 +671,7 @@ void Rend_RenderMaskedWall(rendmaskedwallparams_t *params)
// lots of masked walls, but since 3D models and sprites must be
// rendered interleaved with masked walls, there's not much that can be
// done about this.
if(withDyn && numTexUnits > 1)
if(withDyn)
{
glBegin(GL_QUADS);
glColor4fv(params->vertices[0].color);
Expand Down Expand Up @@ -691,7 +712,7 @@ void Rend_RenderMaskedWall(rendmaskedwallparams_t *params)
glEnd();

// Restore normal GL state.
GL_SelectTexUnits(1);
selectTexUnits(1);
GL_ModulateTexture(1);
}
else
Expand Down

0 comments on commit d7ebc28

Please sign in to comment.