OpenGL2: Reduce glsl shader count by using a uniform to disable textures.

This commit is contained in:
SmileTheory 2013-11-03 19:34:22 -08:00
parent 7ae49cc237
commit 3846c115e6
7 changed files with 147 additions and 154 deletions

View file

@ -24,6 +24,10 @@ uniform sampler2D u_ShadowMap;
uniform samplerCube u_CubeMap;
#endif
#if defined(USE_NORMALMAP) || defined(USE_DELUXEMAP) || defined(USE_SPECULARMAP) || defined(USE_CUBEMAP)
uniform vec4 u_EnableTextures; // x = normal, y = deluxe, z = specular, w = cube
#endif
#if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
uniform vec3 u_DirectedLight;
uniform vec3 u_AmbientLight;
@ -52,7 +56,7 @@ varying vec4 var_Bitangent;
varying vec3 var_LightColor;
#endif
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
varying vec4 var_LightDir;
#endif
@ -304,6 +308,7 @@ void main()
#if defined(USE_TANGENT_SPACE_LIGHT)
L = L * tangentToWorld;
#endif
L = L * u_EnableTextures.y + var_LightDir.xyz;
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
L = var_LightDir.xyz;
#endif
@ -352,6 +357,7 @@ void main()
#else
N.xy = 2.0 * texture2D(u_NormalMap, texCoords).rg - vec2(1.0);
#endif
N.xy *= u_EnableTextures.x;
N.z = sqrt(1.0 - clamp(dot(N.xy, N.xy), 0.0, 1.0));
#if !defined(USE_TANGENT_SPACE_LIGHT)
N = normalize(tangentToWorld * N);
@ -412,6 +418,7 @@ void main()
#if defined(USE_SPECULARMAP)
vec4 specular = texture2D(u_SpecularMap, texCoords);
specular = mix(vec4(1.0), specular, u_EnableTextures.z);
#if defined(USE_GAMMA2_TEXTURES)
specular.rgb *= specular.rgb;
#endif
@ -470,7 +477,7 @@ void main()
R = tangentToWorld * R;
#endif
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb;
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
#if defined(USE_LIGHTMAP)
cubeLightColor *= lightSample.rgb;
@ -480,7 +487,7 @@ void main()
cubeLightColor *= lightColor * NL + ambientColor;
#endif
//gl_FragColor.rgb += diffuse.rgb * textureCubeLod(u_CubeMap, N, 7.0).rgb;
//gl_FragColor.rgb += diffuse.rgb * textureCubeLod(u_CubeMap, N, 7.0).rgb * u_EnableTextures.w;
gl_FragColor.rgb += cubeLightColor * reflectance;
#endif

View file

@ -20,15 +20,19 @@ attribute vec3 attr_Bitangent2;
attribute vec3 attr_LightDirection;
#endif
#if defined(USE_TCGEN) || defined(USE_NORMALMAP) || defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
#if defined(USE_DELUXEMAP)
uniform vec4 u_EnableTextures; // x = normal, y = deluxe, z = specular, w = cube
#endif
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
uniform vec3 u_ViewOrigin;
uniform vec3 u_LocalViewOrigin;
#endif
#if defined(USE_TCGEN)
uniform int u_TCGen0;
uniform vec3 u_TCGen0Vector0;
uniform vec3 u_TCGen0Vector1;
uniform vec3 u_LocalViewOrigin;
#endif
#if defined(USE_TCMOD)
@ -51,8 +55,8 @@ uniform float u_VertexLerp;
#if defined(USE_LIGHT_VECTOR)
uniform vec4 u_LightOrigin;
uniform float u_LightRadius;
uniform vec3 u_DirectedLight;
#if defined(USE_FAST_LIGHT)
uniform vec3 u_DirectedLight;
uniform vec3 u_AmbientLight;
#endif
#endif
@ -66,7 +70,7 @@ varying vec4 var_TexCoords;
varying vec4 var_Color;
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
varying vec4 var_Normal;
varying vec4 var_Tangent;
varying vec4 var_Bitangent;
@ -76,7 +80,7 @@ varying vec4 var_Bitangent;
varying vec3 var_LightColor;
#endif
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
varying vec4 var_LightDir;
#endif
@ -184,7 +188,7 @@ void main()
#if defined(USE_LIGHT_VECTOR)
vec3 L = u_LightOrigin.xyz - (position * u_LightOrigin.w);
#elif defined(USE_LIGHT) && !defined(USE_DELUXEMAP)
#elif defined(USE_LIGHT)
vec3 L = attr_LightDirection;
#if defined(USE_MODELMATRIX)
L = (u_ModelMatrix * vec4(L, 0.0)).xyz;
@ -213,15 +217,18 @@ void main()
var_PrimaryLightDir.w = u_PrimaryLightRadius * u_PrimaryLightRadius;
#endif
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
#if defined(USE_LIGHT_VECTOR)
var_LightDir = vec4(L, u_LightRadius * u_LightRadius);
#else
var_LightDir = vec4(L, 0.0);
#endif
#if defined(USE_DELUXEMAP)
var_LightDir *= 1.0 - u_EnableTextures.y;
#endif
#endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
vec3 viewDir = u_ViewOrigin - position;
#endif
@ -232,16 +239,16 @@ void main()
var_PrimaryLightDir.xyz = var_PrimaryLightDir.xyz * tangentToWorld;
#endif
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
var_LightDir.xyz = var_LightDir.xyz * tangentToWorld;
#endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
viewDir = viewDir * tangentToWorld;
#endif
#endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
// store view direction in tangent space to save on varyings
var_Normal = vec4(normal, viewDir.x);
var_Tangent = vec4(tangent, viewDir.y);

View file

@ -84,6 +84,8 @@ static uniformInfo_t uniformsInfo[] =
{ "u_ShadowMvp2", GLSL_MAT16 },
{ "u_ShadowMvp3", GLSL_MAT16 },
{ "u_EnableTextures", GLSL_VEC4 },
{ "u_DiffuseTexMatrix", GLSL_VEC4 },
{ "u_DiffuseTexOffTurb", GLSL_VEC4 },
{ "u_Texture1Env", GLSL_INT },
@ -997,27 +999,19 @@ void GLSL_InitGPUShaders(void)
for (i = 0; i < LIGHTDEF_COUNT; i++)
{
int lightType = i & LIGHTDEF_LIGHTTYPE_MASK;
qboolean fastLight = !(r_normalMapping->integer || r_specularMapping->integer);
// skip impossible combos
if ((i & LIGHTDEF_USE_PARALLAXMAP) && !r_parallaxMapping->integer)
continue;
if ((i & LIGHTDEF_USE_DELUXEMAP) && !r_deluxeMapping->integer)
if (!lightType && (i & LIGHTDEF_USE_PARALLAXMAP))
continue;
if ((i & LIGHTDEF_USE_CUBEMAP) && !r_cubeMapping->integer)
if (!lightType && (i & LIGHTDEF_USE_SHADOWMAP))
continue;
if (!((i & LIGHTDEF_LIGHTTYPE_MASK) == LIGHTDEF_USE_LIGHTMAP) && (i & LIGHTDEF_USE_DELUXEMAP))
continue;
if (!(i & LIGHTDEF_LIGHTTYPE_MASK))
{
if (i & LIGHTDEF_USE_SHADOWMAP)
continue;
if (i & LIGHTDEF_USE_CUBEMAP)
continue;
}
attribs = ATTR_POSITION | ATTR_TEXCOORD | ATTR_COLOR | ATTR_NORMAL;
extradefines[0] = '\0';
@ -1026,30 +1020,30 @@ void GLSL_InitGPUShaders(void)
Q_strcat(extradefines, 1024, va("#define r_deluxeSpecular %f\n", r_deluxeSpecular->value));
if (r_specularIsMetallic->value)
Q_strcat(extradefines, 1024, va("#define SPECULAR_IS_METALLIC\n"));
Q_strcat(extradefines, 1024, "#define SPECULAR_IS_METALLIC\n");
if (r_dlightMode->integer >= 2)
Q_strcat(extradefines, 1024, "#define USE_SHADOWMAP\n");
if (1)
{
Q_strcat(extradefines, 1024, "#define SWIZZLE_NORMALMAP\n");
}
if (r_hdr->integer && !(glRefConfig.textureFloat && glRefConfig.halfFloatPixel && r_floatLightmap->integer))
Q_strcat(extradefines, 1024, "#define RGBM_LIGHTMAP\n");
if (i & LIGHTDEF_LIGHTTYPE_MASK)
if (lightType)
{
Q_strcat(extradefines, 1024, "#define USE_LIGHT\n");
if (r_normalMapping->integer == 0 && r_specularMapping->integer == 0)
if (fastLight)
Q_strcat(extradefines, 1024, "#define USE_FAST_LIGHT\n");
switch (i & LIGHTDEF_LIGHTTYPE_MASK)
switch (lightType)
{
case LIGHTDEF_USE_LIGHTMAP:
Q_strcat(extradefines, 1024, "#define USE_LIGHTMAP\n");
if (r_deluxeMapping->integer && !fastLight)
Q_strcat(extradefines, 1024, "#define USE_DELUXEMAP\n");
attribs |= ATTR_LIGHTCOORD | ATTR_LIGHTDIRECTION;
break;
case LIGHTDEF_USE_LIGHT_VECTOR:
@ -1062,7 +1056,6 @@ void GLSL_InitGPUShaders(void)
default:
break;
}
}
if (r_normalMapping->integer)
{
@ -1078,6 +1071,9 @@ void GLSL_InitGPUShaders(void)
Q_strcat(extradefines, 1024, "#define USE_VERT_TANGENT_SPACE\n");
attribs |= ATTR_TANGENT | ATTR_BITANGENT;
#endif
if ((i & LIGHTDEF_USE_PARALLAXMAP) && !(i & LIGHTDEF_ENTITY) && r_parallaxMapping->integer)
Q_strcat(extradefines, 1024, "#define USE_PARALLAXMAP\n");
}
if (r_specularMapping->integer)
@ -1109,14 +1105,9 @@ void GLSL_InitGPUShaders(void)
}
}
if ((i & LIGHTDEF_USE_DELUXEMAP) && r_deluxeMapping->integer)
Q_strcat(extradefines, 1024, "#define USE_DELUXEMAP\n");
if ((i & LIGHTDEF_USE_PARALLAXMAP) && !(i & LIGHTDEF_ENTITY) && r_parallaxMapping->integer)
Q_strcat(extradefines, 1024, "#define USE_PARALLAXMAP\n");
if ((i & LIGHTDEF_USE_CUBEMAP))
if (r_cubeMapping->integer)
Q_strcat(extradefines, 1024, "#define USE_CUBEMAP\n");
}
if (i & LIGHTDEF_USE_SHADOWMAP)
{

View file

@ -2875,9 +2875,6 @@ void R_CreateBuiltinImages( void ) {
Com_Memset( data, 255, sizeof( data ) );
tr.whiteImage = R_CreateImage("*white", (byte *)data, 8, 8, IMGTYPE_COLORALPHA, IMGFLAG_NONE, 0);
Com_Memset( data, 128, sizeof( data ) );
tr.greyImage = R_CreateImage("*grey", (byte *)data, 8, 8, IMGTYPE_COLORALPHA, IMGFLAG_NONE, GL_RGBA8);
if (r_dlightMode->integer >= 2)
{
for( x = 0; x < MAX_DLIGHTS; x++)

View file

@ -680,12 +680,10 @@ enum
LIGHTDEF_LIGHTTYPE_MASK = 0x0003,
LIGHTDEF_ENTITY = 0x0004,
LIGHTDEF_USE_TCGEN_AND_TCMOD = 0x0008,
LIGHTDEF_USE_DELUXEMAP = 0x0010,
LIGHTDEF_USE_PARALLAXMAP = 0x0020,
LIGHTDEF_USE_SHADOWMAP = 0x0040,
LIGHTDEF_USE_CUBEMAP = 0x0080,
LIGHTDEF_ALL = 0x00FF,
LIGHTDEF_COUNT = 0x0100
LIGHTDEF_USE_PARALLAXMAP = 0x0010,
LIGHTDEF_USE_SHADOWMAP = 0x0020,
LIGHTDEF_ALL = 0x003F,
LIGHTDEF_COUNT = 0x0040
};
enum
@ -722,6 +720,8 @@ typedef enum
UNIFORM_SHADOWMVP2,
UNIFORM_SHADOWMVP3,
UNIFORM_ENABLETEXTURES,
UNIFORM_DIFFUSETEXMATRIX,
UNIFORM_DIFFUSETEXOFFTURB,
UNIFORM_TEXTURE1ENV,
@ -1609,7 +1609,6 @@ typedef struct {
image_t *fogImage;
image_t *dlightImage; // inverse-quare highlight for projective adding
image_t *flareImage;
image_t *greyImage; // full of 0x80
image_t *whiteImage; // full of 0xff
image_t *identityLightImage; // full of tr.identityLightByte

View file

@ -730,7 +730,7 @@ static void ForwardDlight( void ) {
{
int index = pStage->glslShaderIndex;
index &= ~(LIGHTDEF_LIGHTTYPE_MASK | LIGHTDEF_USE_DELUXEMAP);
index &= ~LIGHTDEF_LIGHTTYPE_MASK;
index |= LIGHTDEF_USE_LIGHT_VECTOR;
sp = &tr.lightallShader[index];
@ -1098,11 +1098,6 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input )
index |= LIGHTDEF_USE_SHADOWMAP;
}
if (!(tr.viewParms.flags & VPF_NOCUBEMAPS) && (index & LIGHTDEF_LIGHTTYPE_MASK) && input->cubemapIndex)
{
index |= LIGHTDEF_USE_CUBEMAP;
}
if (r_lightmap->integer && index & LIGHTDEF_USE_LIGHTMAP)
{
index = LIGHTDEF_USE_LIGHTMAP;
@ -1228,6 +1223,7 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input )
else if ( pStage->glslShaderGroup == tr.lightallShader )
{
int i;
vec4_t enableTextures;
if ((backEnd.viewParms.flags & VPF_USESUNLIGHT) && (pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK))
{
@ -1237,73 +1233,78 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input )
GLSL_SetUniformVec4(sp, UNIFORM_PRIMARYLIGHTORIGIN, backEnd.refdef.sunDir);
}
VectorSet4(enableTextures, 0, 0, 0, 0);
if ((r_lightmap->integer == 1 || r_lightmap->integer == 2) && pStage->bundle[TB_LIGHTMAP].image[0])
{
for (i = 0; i < NUM_TEXTURE_BUNDLES; i++)
{
if (pStage->bundle[i].image[0])
{
switch(i)
{
case TB_LIGHTMAP:
if (i == TB_LIGHTMAP)
R_BindAnimatedImageToTMU( &pStage->bundle[TB_LIGHTMAP], i);
break;
case TB_DIFFUSEMAP:
case TB_SPECULARMAP:
case TB_SHADOWMAP:
case TB_CUBEMAP:
default:
else
GL_BindToTMU( tr.whiteImage, i );
break;
case TB_NORMALMAP:
case TB_DELUXEMAP:
GL_BindToTMU( tr.greyImage, i);
break;
}
}
}
}
else if (r_lightmap->integer == 3 && pStage->bundle[TB_DELUXEMAP].image[0])
{
for (i = 0; i < NUM_TEXTURE_BUNDLES; i++)
{
if (pStage->bundle[i].image[0])
{
switch(i)
{
case TB_LIGHTMAP:
if (i == TB_LIGHTMAP)
R_BindAnimatedImageToTMU( &pStage->bundle[TB_DELUXEMAP], i);
break;
case TB_DIFFUSEMAP:
case TB_SPECULARMAP:
case TB_SHADOWMAP:
case TB_CUBEMAP:
default:
else
GL_BindToTMU( tr.whiteImage, i );
break;
case TB_NORMALMAP:
case TB_DELUXEMAP:
GL_BindToTMU( tr.greyImage, i);
break;
}
}
}
}
else
{
for (i = 0; i < NUM_TEXTURE_BUNDLES; i++)
qboolean light = (pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK) != 0;
qboolean fastLight = !(r_normalMapping->integer || r_specularMapping->integer);
if (pStage->bundle[TB_DIFFUSEMAP].image[0])
R_BindAnimatedImageToTMU( &pStage->bundle[TB_DIFFUSEMAP], TB_DIFFUSEMAP);
if (pStage->bundle[TB_LIGHTMAP].image[0])
R_BindAnimatedImageToTMU( &pStage->bundle[TB_LIGHTMAP], TB_LIGHTMAP);
// bind textures that are sampled and used in the glsl shader, and
// bind whiteImage to textures that are sampled but zeroed in the glsl shader
//
// alternatives:
// - use the last bound texture
// -> costs more to sample a higher res texture then throw out the result
// - disable texture sampling in glsl shader with #ifdefs, as before
// -> increases the number of shaders that must be compiled
//
if (light && !fastLight)
{
if (pStage->bundle[i].image[0])
if (pStage->bundle[TB_NORMALMAP].image[0])
{
R_BindAnimatedImageToTMU( &pStage->bundle[i], i);
R_BindAnimatedImageToTMU( &pStage->bundle[TB_NORMALMAP], TB_NORMALMAP);
enableTextures[0] = 1.0f;
}
else if (r_normalMapping->integer)
GL_BindToTMU( tr.whiteImage, TB_NORMALMAP );
if (pStage->bundle[TB_DELUXEMAP].image[0])
{
R_BindAnimatedImageToTMU( &pStage->bundle[TB_DELUXEMAP], TB_DELUXEMAP);
enableTextures[1] = 1.0f;
}
else if (r_deluxeMapping->integer)
GL_BindToTMU( tr.whiteImage, TB_DELUXEMAP );
if (pStage->bundle[TB_SPECULARMAP].image[0])
{
R_BindAnimatedImageToTMU( &pStage->bundle[TB_SPECULARMAP], TB_SPECULARMAP);
enableTextures[2] = 1.0f;
}
else if (r_specularMapping->integer)
GL_BindToTMU( tr.whiteImage, TB_SPECULARMAP );
}
enableTextures[3] = (r_cubeMapping->integer && !(tr.viewParms.flags & VPF_NOCUBEMAPS) && input->cubemapIndex) ? 1.0f : 0.0f;
}
GLSL_SetUniformVec4(sp, UNIFORM_ENABLETEXTURES, enableTextures);
}
else if ( pStage->bundle[1].image[0] != 0 )
{

View file

@ -2208,7 +2208,6 @@ static void CollapseStagesToLightall(shaderStage_t *diffuse,
//ri.Printf(PRINT_ALL, ", deluxemap");
diffuse->bundle[TB_DELUXEMAP] = lightmap->bundle[0];
diffuse->bundle[TB_DELUXEMAP].image[0] = tr.deluxemaps[shader.lightmapIndex];
defs |= LIGHTDEF_USE_DELUXEMAP;
}
if (r_normalMapping->integer)
@ -2525,8 +2524,6 @@ static qboolean CollapseStagesToGLSL(void)
{
pStage->glslShaderGroup = tr.lightallShader;
pStage->glslShaderIndex = LIGHTDEF_USE_LIGHTMAP;
if (r_deluxeMapping->integer && tr.worldDeluxeMapping)
pStage->glslShaderIndex |= LIGHTDEF_USE_DELUXEMAP;
pStage->bundle[TB_LIGHTMAP] = pStage->bundle[TB_DIFFUSEMAP];
pStage->bundle[TB_DIFFUSEMAP].image[0] = tr.whiteImage;
pStage->bundle[TB_DIFFUSEMAP].isLightmap = qfalse;
@ -2559,7 +2556,7 @@ static qboolean CollapseStagesToGLSL(void)
}
}
// insert default normal and specular textures if necessary
// insert default material info if needed
for (i = 0; i < MAX_SHADER_STAGES; i++)
{
shaderStage_t *pStage = &stages[i];
@ -2573,14 +2570,8 @@ static qboolean CollapseStagesToGLSL(void)
if ((pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK) == 0)
continue;
if (!pStage->bundle[TB_NORMALMAP].image[0] && r_normalMapping->integer)
{
pStage->bundle[TB_NORMALMAP].image[0] = tr.greyImage;
}
if (!pStage->bundle[TB_SPECULARMAP].image[0] && r_specularMapping->integer)
{
pStage->bundle[TB_SPECULARMAP].image[0] = tr.whiteImage;
if (!pStage->materialInfo[0])
pStage->materialInfo[0] = r_baseSpecular->value;
if (!pStage->materialInfo[1])