Skip to content

Commit

Permalink
OpenGL2: Reduce glsl shader count by using a uniform to disable textu…
Browse files Browse the repository at this point in the history
…res.
  • Loading branch information
SmileTheory committed Nov 4, 2013
1 parent 7ae49cc commit 3846c11
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 152 deletions.
13 changes: 10 additions & 3 deletions code/renderergl2/glsl/lightall_fp.glsl
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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

Expand Down
31 changes: 19 additions & 12 deletions code/renderergl2/glsl/lightall_vp.glsl
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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

Expand Down Expand Up @@ -150,7 +154,7 @@ float CalcLightAttenuation(vec3 dir, float sqrRadius)
void main()
{
#if defined(USE_VERTEX_ANIMATION)
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp));
vec3 tangent = normalize(mix(attr_Tangent, attr_Tangent2, u_VertexLerp));
vec3 bitangent = normalize(mix(attr_Bitangent, attr_Bitangent2, u_VertexLerp));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand All @@ -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);
Expand Down
117 changes: 54 additions & 63 deletions code/renderergl2/tr_glsl.c
Expand Up @@ -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 },
Expand Down Expand Up @@ -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)
continue;

if ((i & LIGHTDEF_USE_CUBEMAP) && !r_cubeMapping->integer)
if (!lightType && (i & LIGHTDEF_USE_PARALLAXMAP))
continue;

if (!((i & LIGHTDEF_LIGHTTYPE_MASK) == LIGHTDEF_USE_LIGHTMAP) && (i & LIGHTDEF_USE_DELUXEMAP))
if (!lightType && (i & LIGHTDEF_USE_SHADOWMAP))
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';
Expand All @@ -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:
Expand All @@ -1062,61 +1056,58 @@ void GLSL_InitGPUShaders(void)
default:
break;
}
}

if (r_normalMapping->integer)
{
Q_strcat(extradefines, 1024, "#define USE_NORMALMAP\n");
if (r_normalMapping->integer)
{
Q_strcat(extradefines, 1024, "#define USE_NORMALMAP\n");

if (r_normalMapping->integer == 2)
Q_strcat(extradefines, 1024, "#define USE_OREN_NAYAR\n");
if (r_normalMapping->integer == 2)
Q_strcat(extradefines, 1024, "#define USE_OREN_NAYAR\n");

if (r_normalMapping->integer == 3)
Q_strcat(extradefines, 1024, "#define USE_TRIACE_OREN_NAYAR\n");
if (r_normalMapping->integer == 3)
Q_strcat(extradefines, 1024, "#define USE_TRIACE_OREN_NAYAR\n");

#ifdef USE_VERT_TANGENT_SPACE
Q_strcat(extradefines, 1024, "#define USE_VERT_TANGENT_SPACE\n");
attribs |= ATTR_TANGENT | ATTR_BITANGENT;
Q_strcat(extradefines, 1024, "#define USE_VERT_TANGENT_SPACE\n");
attribs |= ATTR_TANGENT | ATTR_BITANGENT;
#endif
}

if (r_specularMapping->integer)
{
Q_strcat(extradefines, 1024, "#define USE_SPECULARMAP\n");
if ((i & LIGHTDEF_USE_PARALLAXMAP) && !(i & LIGHTDEF_ENTITY) && r_parallaxMapping->integer)
Q_strcat(extradefines, 1024, "#define USE_PARALLAXMAP\n");
}

switch (r_specularMapping->integer)
if (r_specularMapping->integer)
{
case 1:
default:
Q_strcat(extradefines, 1024, "#define USE_BLINN\n");
break;

case 2:
Q_strcat(extradefines, 1024, "#define USE_BLINN_FRESNEL\n");
break;

case 3:
Q_strcat(extradefines, 1024, "#define USE_MCAULEY\n");
break;

case 4:
Q_strcat(extradefines, 1024, "#define USE_GOTANDA\n");
break;

case 5:
Q_strcat(extradefines, 1024, "#define USE_LAZAROV\n");
break;
Q_strcat(extradefines, 1024, "#define USE_SPECULARMAP\n");

switch (r_specularMapping->integer)
{
case 1:
default:
Q_strcat(extradefines, 1024, "#define USE_BLINN\n");
break;

case 2:
Q_strcat(extradefines, 1024, "#define USE_BLINN_FRESNEL\n");
break;

case 3:
Q_strcat(extradefines, 1024, "#define USE_MCAULEY\n");
break;

case 4:
Q_strcat(extradefines, 1024, "#define USE_GOTANDA\n");
break;

case 5:
Q_strcat(extradefines, 1024, "#define USE_LAZAROV\n");
break;
}
}
}

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))
Q_strcat(extradefines, 1024, "#define USE_CUBEMAP\n");
if (r_cubeMapping->integer)
Q_strcat(extradefines, 1024, "#define USE_CUBEMAP\n");
}

if (i & LIGHTDEF_USE_SHADOWMAP)
{
Expand Down
3 changes: 0 additions & 3 deletions code/renderergl2/tr_image.c
Expand Up @@ -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++)
Expand Down
13 changes: 6 additions & 7 deletions code/renderergl2/tr_local.h
Expand Up @@ -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
Expand Down Expand Up @@ -722,6 +720,8 @@ typedef enum
UNIFORM_SHADOWMVP2,
UNIFORM_SHADOWMVP3,

UNIFORM_ENABLETEXTURES,

UNIFORM_DIFFUSETEXMATRIX,
UNIFORM_DIFFUSETEXOFFTURB,
UNIFORM_TEXTURE1ENV,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 3846c11

Please sign in to comment.