OpenGL2: reduce varying usage in lightall shader.

This commit is contained in:
SmileTheory 2013-09-17 23:52:40 -07:00
parent cbd05da5d1
commit 3ab895d1cd
2 changed files with 60 additions and 64 deletions

View file

@ -24,45 +24,40 @@ uniform sampler2D u_ShadowMap;
uniform samplerCube u_CubeMap; uniform samplerCube u_CubeMap;
#endif #endif
#if defined(USE_LIGHT_VECTOR) #if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
uniform vec3 u_DirectedLight; uniform vec3 u_DirectedLight;
uniform vec3 u_AmbientLight; uniform vec3 u_AmbientLight;
uniform float u_LightRadius;
#endif #endif
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
uniform vec3 u_PrimaryLightColor; uniform vec3 u_PrimaryLightColor;
uniform vec3 u_PrimaryLightAmbient; uniform vec3 u_PrimaryLightAmbient;
uniform float u_PrimaryLightRadius;
#endif #endif
#if defined(USE_LIGHT) #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
uniform vec2 u_MaterialInfo; uniform vec2 u_MaterialInfo;
#endif #endif
varying vec2 var_DiffuseTex; varying vec4 var_TexCoords;
#if defined(USE_LIGHTMAP)
varying vec2 var_LightTex;
#endif
varying vec4 var_Color; varying vec4 var_Color;
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP) #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
varying vec3 var_ViewDir; varying vec4 var_Normal;
varying vec3 var_Normal; varying vec4 var_Tangent;
varying vec3 var_Tangent; varying vec4 var_Bitangent;
varying vec3 var_Bitangent;
#endif #endif
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT) #if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
varying vec3 var_lightColor; varying vec3 var_LightColor;
#endif #endif
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) #if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
varying vec4 var_LightDir; varying vec4 var_LightDir;
#endif #endif
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
varying vec3 var_PrimaryLightDir; varying vec4 var_PrimaryLightDir;
#endif #endif
@ -276,7 +271,7 @@ float CalcLightAttenuation(vec3 dir, float sqrRadius)
// clamp attenuation // clamp attenuation
#if defined(NO_LIGHT_CLAMP) #if defined(NO_LIGHT_CLAMP)
attenuation *= float(attenuation > 0.0); attenuation = max(attenuation, 0.0);
#else #else
attenuation = clamp(attenuation, 0.0, 1.0); attenuation = clamp(attenuation, 0.0, 1.0);
#endif #endif
@ -291,36 +286,36 @@ void main()
float NL, NH, NE, EH; float NL, NH, NE, EH;
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP) #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
mat3 tangentToWorld = mat3(var_Tangent, var_Bitangent, var_Normal); mat3 tangentToWorld = mat3(var_Tangent.xyz, var_Bitangent.xyz, var_Normal.xyz);
#endif #endif
#if defined(USE_DELUXEMAP) #if defined(USE_DELUXEMAP)
L = (2.0 * texture2D(u_DeluxeMap, var_LightTex).xyz - vec3(1.0)); L = (2.0 * texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(1.0));
#if defined(USE_TANGENT_SPACE_LIGHT) #if defined(USE_TANGENT_SPACE_LIGHT)
L = L * tangentToWorld; L = L * tangentToWorld;
#endif #endif
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) #elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
L = var_LightDir.xyz; L = var_LightDir.xyz;
#endif #endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP) #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
E = normalize(var_ViewDir); E = normalize(vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w));
#endif #endif
#if defined(USE_LIGHTMAP) #if defined(USE_LIGHTMAP)
vec4 lightSample = texture2D(u_LightMap, var_LightTex).rgba; vec4 lightSample = texture2D(u_LightMap, var_TexCoords.zw).rgba;
#if defined(RGBM_LIGHTMAP) #if defined(RGBM_LIGHTMAP)
lightSample.rgb *= 32.0 * lightSample.a; lightSample.rgb *= 32.0 * lightSample.a;
#endif #endif
vec3 lightColor = lightSample.rgb; vec3 lightColor = lightSample.rgb;
#elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT) #elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
vec3 lightColor = u_DirectedLight * CalcLightAttenuation(L, u_LightRadius * u_LightRadius); vec3 lightColor = u_DirectedLight * CalcLightAttenuation(L, var_LightDir.w);
vec3 ambientColor = u_AmbientLight; vec3 ambientColor = u_AmbientLight;
#elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT) #elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
vec3 lightColor = var_lightColor; vec3 lightColor = var_LightColor;
#endif #endif
vec2 texCoords = var_DiffuseTex; vec2 texCoords = var_TexCoords.xy;
#if defined(USE_PARALLAXMAP) #if defined(USE_PARALLAXMAP)
#if defined(USE_TANGENT_SPACE_LIGHT) #if defined(USE_TANGENT_SPACE_LIGHT)
@ -355,7 +350,7 @@ void main()
#elif defined(USE_TANGENT_SPACE_LIGHT) #elif defined(USE_TANGENT_SPACE_LIGHT)
N = vec3(0.0, 0.0, 1.0); N = vec3(0.0, 0.0, 1.0);
#else #else
N = normalize(var_Normal); N = normalize(var_Normal.xyz);
#endif #endif
L = normalize(L); L = normalize(L);
@ -368,7 +363,7 @@ void main()
#if defined(USE_TANGENT_SPACE_LIGHT) #if defined(USE_TANGENT_SPACE_LIGHT)
shadowValue *= float(var_PrimaryLightDir.z > 0.0); shadowValue *= float(var_PrimaryLightDir.z > 0.0);
#else #else
shadowValue *= float(dot(var_Normal, var_PrimaryLightDir) > 0.0); shadowValue *= float(dot(var_Normal.xyz, var_PrimaryLightDir.xyz) > 0.0);
#endif #endif
#if defined(SHADOWMAP_MODULATE) #if defined(SHADOWMAP_MODULATE)
@ -377,7 +372,7 @@ void main()
#if 0 #if 0
// Only shadow when the world light is parallel to the primary light // Only shadow when the world light is parallel to the primary light
shadowValue = 1.0 + (shadowValue - 1.0) * clamp(dot(L, var_PrimaryLightDir), 0.0, 1.0); shadowValue = 1.0 + (shadowValue - 1.0) * clamp(dot(L, var_PrimaryLightDir.xyz), 0.0, 1.0);
#endif #endif
lightColor = mix(shadowColor, lightColor, shadowValue); lightColor = mix(shadowColor, lightColor, shadowValue);
#endif #endif
@ -389,7 +384,7 @@ void main()
#if defined(USE_TANGENT_SPACE_LIGHT) #if defined(USE_TANGENT_SPACE_LIGHT)
float surfNL = L.z; float surfNL = L.z;
#else #else
float surfNL = clamp(dot(var_Normal, L), 0.0, 1.0); float surfNL = clamp(dot(var_Normal.xyz, L), 0.0, 1.0);
#endif #endif
// Scale the incoming light to compensate for the baked-in light angle // Scale the incoming light to compensate for the baked-in light angle
@ -474,7 +469,7 @@ void main()
#if defined(USE_LIGHTMAP) #if defined(USE_LIGHTMAP)
cubeLightColor *= lightSample.rgb; cubeLightColor *= lightSample.rgb;
#elif defined (USE_LIGHT_VERTEX) #elif defined (USE_LIGHT_VERTEX)
cubeLightColor *= var_lightColor; cubeLightColor *= var_LightColor;
#else #else
cubeLightColor *= lightColor * NL + ambientColor; cubeLightColor *= lightColor * NL + ambientColor;
#endif #endif
@ -484,7 +479,7 @@ void main()
#endif #endif
#if defined(USE_PRIMARY_LIGHT) #if defined(USE_PRIMARY_LIGHT)
L = normalize(var_PrimaryLightDir); L = normalize(var_PrimaryLightDir.xyz);
NL = clamp(dot(N, L), 0.0, 1.0); NL = clamp(dot(N, L), 0.0, 1.0);
H = normalize(L + E); H = normalize(L + E);
@ -494,7 +489,7 @@ void main()
reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess); reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, shininess); reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, shininess);
lightColor = u_PrimaryLightColor; // * CalcLightAttenuation(L, u_PrimaryLightRadius * u_PrimaryLightRadius); lightColor = u_PrimaryLightColor; // * CalcLightAttenuation(L, u_PrimaryLightDir.w);
#if defined(USE_SHADOWMAP) #if defined(USE_SHADOWMAP)
lightColor *= shadowValue; lightColor *= shadowValue;

View file

@ -49,37 +49,30 @@ uniform float u_VertexLerp;
#if defined(USE_LIGHT_VECTOR) #if defined(USE_LIGHT_VECTOR)
uniform vec4 u_LightOrigin; uniform vec4 u_LightOrigin;
#if defined(USE_FAST_LIGHT)
uniform vec3 u_DirectedLight;
uniform vec3 u_AmbientLight;
uniform float u_LightRadius; uniform float u_LightRadius;
uniform vec3 u_DirectedLight;
#if defined(USE_FAST_LIGHT)
uniform vec3 u_AmbientLight;
#endif #endif
#endif #endif
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
uniform vec4 u_PrimaryLightOrigin; uniform vec4 u_PrimaryLightOrigin;
uniform float u_PrimaryLightRadius;
#endif #endif
varying vec2 var_DiffuseTex; varying vec4 var_TexCoords;
#if defined(USE_LIGHTMAP)
varying vec2 var_LightTex;
#endif
#if defined(USE_NORMALMAP) || (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT))
varying vec3 var_ViewDir;
#endif
varying vec4 var_Color; varying vec4 var_Color;
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP) #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
varying vec3 var_Normal; varying vec4 var_Normal;
varying vec3 var_Tangent; varying vec4 var_Tangent;
varying vec3 var_Bitangent; varying vec4 var_Bitangent;
#endif #endif
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT) #if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
varying vec3 var_lightColor; varying vec3 var_LightColor;
#endif #endif
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT) #if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
@ -87,7 +80,7 @@ varying vec4 var_LightDir;
#endif #endif
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
varying vec3 var_PrimaryLightDir; varying vec4 var_PrimaryLightDir;
#endif #endif
#if defined(USE_TCGEN) #if defined(USE_TCGEN)
@ -144,7 +137,7 @@ float CalcLightAttenuation(vec3 dir, float sqrRadius)
// clamp attenuation // clamp attenuation
#if defined(NO_LIGHT_CLAMP) #if defined(NO_LIGHT_CLAMP)
attenuation *= float(attenuation > 0.0); attenuation = max(attenuation, 0.0);
#else #else
attenuation = clamp(attenuation, 0.0, 1.0); attenuation = clamp(attenuation, 0.0, 1.0);
#endif #endif
@ -174,9 +167,9 @@ void main()
#endif #endif
#if defined(USE_TCMOD) #if defined(USE_TCMOD)
var_DiffuseTex = ModTexCoords(texCoords, position, u_DiffuseTexMatrix, u_DiffuseTexOffTurb); var_TexCoords.xy = ModTexCoords(texCoords, position, u_DiffuseTexMatrix, u_DiffuseTexOffTurb);
#else #else
var_DiffuseTex = texCoords; var_TexCoords.xy = texCoords;
#endif #endif
gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0); gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0);
@ -193,16 +186,16 @@ void main()
#elif defined(USE_LIGHT) && !defined(USE_LIGHT_VECTOR) #elif defined(USE_LIGHT) && !defined(USE_LIGHT_VECTOR)
vec3 L = attr_LightDirection; vec3 L = attr_LightDirection;
#if defined(USE_MODELMATRIX) #if defined(USE_MODELMATRIX)
L = (u_ModelMatrix * vec4(L, 0.0)).xyz; L = (u_ModelMatrix * vec4(L, 0.0)).xyz;
#endif #endif
#endif #endif
#if defined(USE_LIGHTMAP) #if defined(USE_LIGHTMAP)
var_LightTex = attr_TexCoord1.st; var_TexCoords.zw = attr_TexCoord1.st;
#endif #endif
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT) #if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
var_lightColor = u_VertColor.rgb * attr_Color.rgb; var_LightColor = u_VertColor.rgb * attr_Color.rgb;
var_Color.rgb = vec3(1.0); var_Color.rgb = vec3(1.0);
var_Color.a = u_VertColor.a * attr_Color.a + u_BaseColor.a; var_Color.a = u_VertColor.a * attr_Color.a + u_BaseColor.a;
#else #else
@ -217,40 +210,48 @@ void main()
#endif #endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP) #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
var_Normal = normal; var_Normal.xyz = normal;
var_Tangent = tangent; var_Tangent.xyz = tangent;
var_Bitangent = bitangent; var_Bitangent.xyz = bitangent;
#endif #endif
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
var_PrimaryLightDir = (u_PrimaryLightOrigin.xyz - (position * u_PrimaryLightOrigin.w)); var_PrimaryLightDir.xyz = (u_PrimaryLightOrigin.xyz - (position * u_PrimaryLightOrigin.w));
var_PrimaryLightDir.w = u_PrimaryLightRadius * u_PrimaryLightRadius;
#endif #endif
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT) #if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
#if defined(USE_LIGHT_VECTOR) #if defined(USE_LIGHT_VECTOR)
var_LightDir = vec4(L, u_LightOrigin.w); var_LightDir = vec4(L, u_LightRadius * u_LightRadius);
#else #else
var_LightDir = vec4(L, 0.0); var_LightDir = vec4(L, 0.0);
#endif #endif
#endif #endif
#if defined(USE_NORMALMAP) || (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
var_ViewDir = (u_ViewOrigin - position); vec3 viewDir = (u_ViewOrigin - position);
#endif #endif
#if defined(USE_TANGENT_SPACE_LIGHT) #if defined(USE_TANGENT_SPACE_LIGHT)
mat3 tangentToWorld = mat3(tangent, bitangent, normal); mat3 tangentToWorld = mat3(tangent, bitangent, normal);
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
var_PrimaryLightDir = var_PrimaryLightDir * tangentToWorld; var_PrimaryLightDir.xyz = var_PrimaryLightDir.xyz * tangentToWorld;
#endif #endif
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT) #if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
var_LightDir.xyz = var_LightDir.xyz * tangentToWorld; var_LightDir.xyz = var_LightDir.xyz * tangentToWorld;
#endif #endif
#if defined(USE_NORMALMAP) || (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
var_ViewDir = var_ViewDir * tangentToWorld; viewDir = viewDir * tangentToWorld;
#endif #endif
#endif #endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
// store view direction in tangent space to save on varyings
var_Normal.w = viewDir.x;
var_Tangent.w = viewDir.y;
var_Bitangent.w = viewDir.z;
#endif
} }