OpenGL2: Small glsl shader optimizations, fixes, and cleanup.
This commit is contained in:
parent
efe8437cde
commit
623d107f42
15 changed files with 255 additions and 244 deletions
|
@ -166,22 +166,41 @@ vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
|
|||
float a1 = t.w;
|
||||
return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
|
||||
#elif 0
|
||||
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
|
||||
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
|
||||
return mix(specular.rgb, max(specular.rgb, vec3(gloss)), CalcFresnel(NE));
|
||||
#else
|
||||
// from http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20%28Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course%29.pptx
|
||||
// from http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20%28Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course%29.pptx
|
||||
return mix(specular.rgb, vec3(1.0), CalcFresnel(NE) / (4.0 - 3.0 * gloss));
|
||||
#endif
|
||||
}
|
||||
|
||||
float CalcBlinn(float NH, float shininess)
|
||||
{
|
||||
#if 0
|
||||
// from http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
|
||||
float a = shininess + 0.775;
|
||||
return exp(a * NH - a);
|
||||
#if defined(USE_BLINN) || defined(USE_BLINN_FRESNEL)
|
||||
// Normalized Blinn-Phong
|
||||
float norm = shininess * 0.125 + 1.0;
|
||||
#elif defined(USE_MCAULEY)
|
||||
// Cook-Torrance as done by Stephen McAuley
|
||||
// http://blog.selfshadow.com/publications/s2012-shading-course/mcauley/s2012_pbs_farcry3_notes_v2.pdf
|
||||
float norm = shininess * 0.25 + 0.125;
|
||||
#elif defined(USE_GOTANDA)
|
||||
// Neumann-Neumann as done by Yoshiharu Gotanda
|
||||
// http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
|
||||
float norm = shininess * 0.124858 + 0.269182;
|
||||
#elif defined(USE_LAZAROV)
|
||||
// Cook-Torrance as done by Dimitar Lazarov
|
||||
// http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
|
||||
float norm = shininess * 0.125 + 0.25;
|
||||
#else
|
||||
return pow(NH, shininess);
|
||||
float norm = 1.0;
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
// from http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
|
||||
float a = shininess + 0.775;
|
||||
return norm * exp(a * NH - a);
|
||||
#else
|
||||
return norm * pow(NH, shininess);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -215,145 +234,122 @@ float CalcFresnel(float EH)
|
|||
|
||||
float CalcVisibility(float NH, float NL, float NE, float EH, float gloss)
|
||||
{
|
||||
#if 1
|
||||
// From http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
|
||||
#if defined(USE_GOTANDA)
|
||||
// Neumann-Neumann as done by Yoshiharu Gotanda
|
||||
// http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
|
||||
return 1.0 / max(max(NL, NE), EPSILON);
|
||||
#elif defined(USE_LAZAROV)
|
||||
// Cook-Torrance as done by Dimitar Lazarov
|
||||
// http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
|
||||
float k = min(1.0, gloss + 0.545);
|
||||
return 1.0 / (k * EH * EH + (1.0 - k));
|
||||
#elif 0
|
||||
return 1.0 / (k * (EH * EH - 1.0) + 1.0);
|
||||
#elif defined(USE_GGX)
|
||||
float roughness = exp2(gloss * -6.5);
|
||||
|
||||
#if defined(USE_GGX)
|
||||
// From http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
|
||||
float k = roughness + 1.0;
|
||||
k *= k * 0.125;
|
||||
#else
|
||||
float k = roughness;
|
||||
#endif
|
||||
// Modified from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
|
||||
// NL, NE in numerator factored out from cook-torrance
|
||||
float k = roughness + 1.0;
|
||||
k *= k * 0.125;
|
||||
|
||||
float k2 = 1.0 - k;
|
||||
|
||||
float invGeo1 = NL * k2 + k;
|
||||
float invGeo2 = NE * k2 + k;
|
||||
|
||||
|
||||
return 1.0 / (invGeo1 * invGeo2);
|
||||
#else
|
||||
float geo = 2.0 * NH * min(NE, NL);
|
||||
geo /= max(EH, geo);
|
||||
|
||||
return geo;
|
||||
return 1.0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float gloss, float shininess)
|
||||
{
|
||||
float blinn = CalcBlinn(NH, shininess);
|
||||
#if defined(USE_GGX)
|
||||
float distrib = CalcGGX(NH, gloss);
|
||||
#else
|
||||
float distrib = CalcBlinn(NH, shininess);
|
||||
#endif
|
||||
|
||||
#if defined(USE_BLINN)
|
||||
vec3 fSpecular = specular;
|
||||
#else
|
||||
vec3 fSpecular = mix(specular, vec3(1.0), CalcFresnel(EH));
|
||||
#endif
|
||||
|
||||
float vis = CalcVisibility(NH, NL, NE, EH, gloss);
|
||||
|
||||
#if defined(USE_BLINN)
|
||||
// Normalized Blinn-Phong
|
||||
return specular * blinn * (shininess * 0.125 + 1.0);
|
||||
#elif defined(USE_BLINN_FRESNEL)
|
||||
// Normalized Blinn-Phong with Fresnel
|
||||
return fSpecular * blinn * (shininess * 0.125 + 1.0);
|
||||
#elif defined(USE_MCAULEY)
|
||||
// Cook-Torrance as done by Stephen McAuley
|
||||
// http://blog.selfshadow.com/publications/s2012-shading-course/mcauley/s2012_pbs_farcry3_notes_v2.pdf
|
||||
return fSpecular * blinn * (shininess * 0.25 + 0.125);
|
||||
#elif defined(USE_GOTANDA)
|
||||
// Neumann-Neumann as done by Yoshiharu Gotanda
|
||||
// http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
|
||||
return fSpecular * blinn * (shininess * 0.124858 + 0.269182) / max(max(NL, NE), EPSILON);
|
||||
#elif defined(USE_LAZAROV)
|
||||
// Cook-Torrance as done by Dimitar Lazarov
|
||||
// http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
|
||||
return fSpecular * blinn * (shininess * 0.125 + 0.25) * vis;
|
||||
#endif
|
||||
|
||||
return vec3(0.0);
|
||||
return fSpecular * (distrib * vis);
|
||||
}
|
||||
|
||||
|
||||
float CalcLightAttenuation(vec3 dir, float sqrRadius)
|
||||
float CalcLightAttenuation(float point, float normDist)
|
||||
{
|
||||
// point light at >0 radius, directional otherwise
|
||||
float point = float(sqrRadius > 0.0);
|
||||
|
||||
// inverse square light
|
||||
float attenuation = sqrRadius / dot(dir, dir);
|
||||
|
||||
// zero light at radius, approximating q3 style
|
||||
// zero light at 1.0, approximating q3 style
|
||||
// also don't attenuate directional light
|
||||
attenuation = (0.5 * attenuation - 1.5) * point + 1.0;
|
||||
|
||||
float attenuation = (0.5 * normDist - 1.5) * point + 1.0;
|
||||
|
||||
// clamp attenuation
|
||||
#if defined(NO_LIGHT_CLAMP)
|
||||
attenuation = max(attenuation, 0.0);
|
||||
#else
|
||||
attenuation = clamp(attenuation, 0.0, 1.0);
|
||||
#endif
|
||||
|
||||
|
||||
return attenuation;
|
||||
}
|
||||
|
||||
// from http://www.thetenthplanet.de/archives/1180
|
||||
mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )
|
||||
{
|
||||
// get edge vectors of the pixel triangle
|
||||
vec3 dp1 = dFdx( p );
|
||||
vec3 dp2 = dFdy( p );
|
||||
vec2 duv1 = dFdx( uv );
|
||||
vec2 duv2 = dFdy( uv );
|
||||
|
||||
// solve the linear system
|
||||
vec3 dp2perp = cross( dp2, N );
|
||||
vec3 dp1perp = cross( N, dp1 );
|
||||
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
|
||||
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
|
||||
|
||||
// construct a scale-invariant frame
|
||||
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
|
||||
return mat3( T * invmax, B * invmax, N );
|
||||
// get edge vectors of the pixel triangle
|
||||
vec3 dp1 = dFdx( p );
|
||||
vec3 dp2 = dFdy( p );
|
||||
vec2 duv1 = dFdx( uv );
|
||||
vec2 duv2 = dFdy( uv );
|
||||
|
||||
// solve the linear system
|
||||
vec3 dp2perp = cross( dp2, N );
|
||||
vec3 dp1perp = cross( N, dp1 );
|
||||
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
|
||||
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
|
||||
|
||||
// construct a scale-invariant frame
|
||||
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
|
||||
return mat3( T * invmax, B * invmax, N );
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 L, N, E, H;
|
||||
float NL, NH, NE, EH;
|
||||
|
||||
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
|
||||
|
||||
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
||||
#if defined(USE_VERT_TANGENT_SPACE)
|
||||
mat3 tangentToWorld = mat3(var_Tangent.xyz, var_Bitangent.xyz, var_Normal.xyz);
|
||||
E = vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w);
|
||||
#else
|
||||
mat3 tangentToWorld = cotangent_frame(var_Normal, -var_ViewDir, var_TexCoords.xy);
|
||||
E = var_ViewDir;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(USE_DELUXEMAP)
|
||||
L = texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5);
|
||||
L = L * u_EnableTextures.y + var_LightDir.xyz;
|
||||
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
||||
E = normalize(E);
|
||||
|
||||
L = var_LightDir.xyz;
|
||||
#endif
|
||||
|
||||
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
|
||||
#if defined(USE_VERT_TANGENT_SPACE)
|
||||
E = normalize(vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w));
|
||||
#else
|
||||
E = normalize(var_ViewDir);
|
||||
#if defined(USE_DELUXEMAP)
|
||||
L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y;
|
||||
#endif
|
||||
float sqrLightDist = dot(L, L);
|
||||
#endif
|
||||
|
||||
#if defined(USE_LIGHTMAP)
|
||||
vec4 lightSample = texture2D(u_LightMap, var_TexCoords.zw).rgba;
|
||||
#if defined(RGBM_LIGHTMAP)
|
||||
lightSample.rgb *= 32.0 * lightSample.a;
|
||||
#endif
|
||||
vec4 lightSample = texture2D(u_LightMap, var_TexCoords.zw);
|
||||
vec3 lightColor = lightSample.rgb;
|
||||
#if defined(RGBM_LIGHTMAP)
|
||||
lightColor *= 32.0 * lightSample.a;
|
||||
#endif
|
||||
#elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
|
||||
vec3 lightColor = u_DirectedLight * CalcLightAttenuation(L, var_LightDir.w);
|
||||
vec3 lightColor = u_DirectedLight * CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist);
|
||||
vec3 ambientColor = u_AmbientLight;
|
||||
#elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
|
||||
vec3 lightColor = var_LightColor;
|
||||
|
@ -383,21 +379,22 @@ void main()
|
|||
N.xy = texture2D(u_NormalMap, texCoords).rg - vec2(0.5);
|
||||
#endif
|
||||
N.xy *= u_EnableTextures.x;
|
||||
N.z = sqrt(0.25 - dot(N.xy, N.xy));
|
||||
N = normalize(tangentToWorld * N);
|
||||
N.z = sqrt((0.25 - N.x * N.x) - N.y * N.y);
|
||||
N = tangentToWorld * N;
|
||||
#else
|
||||
N = normalize(var_Normal.xyz);
|
||||
N = var_Normal.xyz;
|
||||
#endif
|
||||
|
||||
L = normalize(L);
|
||||
|
||||
N = normalize(N);
|
||||
L /= sqrt(sqrLightDist);
|
||||
|
||||
#if defined(USE_SHADOWMAP)
|
||||
vec2 shadowTex = gl_FragCoord.xy * r_FBufScale;
|
||||
float shadowValue = texture2D(u_ShadowMap, shadowTex).r;
|
||||
|
||||
// surfaces not facing the light are always shadowed
|
||||
shadowValue *= step(0.0, dot(var_Normal.xyz, var_PrimaryLightDir.xyz));
|
||||
|
||||
shadowValue *= float(dot(var_Normal.xyz, var_PrimaryLightDir.xyz) > 0.0);
|
||||
|
||||
#if defined(SHADOWMAP_MODULATE)
|
||||
//vec3 shadowColor = min(u_PrimaryLightAmbient, lightColor);
|
||||
vec3 shadowColor = u_PrimaryLightAmbient * lightColor;
|
||||
|
@ -428,44 +425,41 @@ void main()
|
|||
NL = clamp(dot(N, L), 0.0, 1.0);
|
||||
NE = clamp(dot(N, E), 0.0, 1.0);
|
||||
|
||||
vec4 specular = vec4(1.0);
|
||||
#if defined(USE_SPECULARMAP)
|
||||
vec4 specular = texture2D(u_SpecularMap, texCoords);
|
||||
specular = (specular - vec4(1.0)) * u_EnableTextures.z + vec4(1.0);
|
||||
specular += texture2D(u_SpecularMap, texCoords) * u_EnableTextures.z - u_EnableTextures.zzzz;
|
||||
#if defined(USE_GAMMA2_TEXTURES)
|
||||
specular.rgb *= specular.rgb;
|
||||
#endif
|
||||
#else
|
||||
vec4 specular = vec4(1.0);
|
||||
#endif
|
||||
|
||||
specular *= u_MaterialInfo.xxxy;
|
||||
|
||||
|
||||
float gloss = specular.a;
|
||||
float shininess = exp2(gloss * 13.0);
|
||||
|
||||
#if defined(SPECULAR_IS_METALLIC)
|
||||
// diffuse is actually base color, and red of specular is metallicness
|
||||
// diffuse is actually base color, and red of specular is metallicness
|
||||
float metallic = specular.r;
|
||||
|
||||
specular.rgb = vec3(0.04) + 0.96 * diffuse.rgb * metallic;
|
||||
|
||||
specular.rgb = (0.96 * metallic) * diffuse.rgb + vec3(0.04);
|
||||
diffuse.rgb *= 1.0 - metallic;
|
||||
#else
|
||||
// adjust diffuse by specular reflectance, to maintain energy conservation
|
||||
diffuse.rgb *= vec3(1.0) - specular.rgb;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
|
||||
|
||||
#if defined(r_deluxeSpecular) || defined(USE_LIGHT_VECTOR)
|
||||
float adjGloss = gloss;
|
||||
float adjShininess = shininess;
|
||||
|
||||
#if !defined(USE_LIGHT_VECTOR)
|
||||
|
||||
#if !defined(USE_LIGHT_VECTOR)
|
||||
adjGloss *= r_deluxeSpecular;
|
||||
adjShininess = exp2(adjGloss * 13.0);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
H = normalize(L + E);
|
||||
|
||||
EH = clamp(dot(E, H), 0.0, 1.0);
|
||||
|
@ -477,16 +471,16 @@ void main()
|
|||
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
gl_FragColor.rgb = lightColor * reflectance * NL;
|
||||
|
||||
gl_FragColor.rgb = lightColor * reflectance * NL;
|
||||
gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb);
|
||||
|
||||
|
||||
#if defined(USE_CUBEMAP)
|
||||
reflectance = EnvironmentBRDF(gloss, NE, specular.rgb);
|
||||
|
||||
vec3 R = reflect(E, N);
|
||||
|
||||
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
|
||||
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
|
||||
|
||||
#if defined(USE_LIGHTMAP)
|
||||
cubeLightColor *= lightSample.rgb;
|
||||
|
@ -495,29 +489,40 @@ void main()
|
|||
#else
|
||||
cubeLightColor *= lightColor * NL + ambientColor;
|
||||
#endif
|
||||
|
||||
|
||||
//gl_FragColor.rgb += diffuse.rgb * textureCubeLod(u_CubeMap, N, 7.0).rgb * u_EnableTextures.w;
|
||||
gl_FragColor.rgb += cubeLightColor * reflectance;
|
||||
#endif
|
||||
|
||||
#if defined(USE_PRIMARY_LIGHT)
|
||||
L = var_PrimaryLightDir.xyz; //normalize(var_PrimaryLightDir.xyz);
|
||||
NL = clamp(dot(N, L), 0.0, 1.0);
|
||||
vec3 L2, H2;
|
||||
float NL2, EH2, NH2;
|
||||
|
||||
H = normalize(L + E);
|
||||
EH = clamp(dot(E, H), 0.0, 1.0);
|
||||
NH = clamp(dot(N, H), 0.0, 1.0);
|
||||
L2 = var_PrimaryLightDir.xyz;
|
||||
|
||||
reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
|
||||
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, gloss, shininess);
|
||||
// enable when point lights are supported as primary lights
|
||||
//sqrLightDist = dot(L2, L2);
|
||||
//L2 /= sqrt(sqrLightDist);
|
||||
|
||||
NL2 = clamp(dot(N, L2), 0.0, 1.0);
|
||||
|
||||
H2 = normalize(L2 + E);
|
||||
EH2 = clamp(dot(E, H2), 0.0, 1.0);
|
||||
NH2 = clamp(dot(N, H2), 0.0, 1.0);
|
||||
|
||||
reflectance = CalcDiffuse(diffuse.rgb, N, L2, E, NE, NL2, shininess);
|
||||
reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, gloss, shininess);
|
||||
|
||||
lightColor = u_PrimaryLightColor;
|
||||
|
||||
// enable when point lights are supported as primary lights
|
||||
//lightColor *= CalcLightAttenuation(float(u_PrimaryLightDir.w > 0.0), u_PrimaryLightDir.w / sqrLightDist);
|
||||
|
||||
lightColor = u_PrimaryLightColor; // * CalcLightAttenuation(L, u_PrimaryLightDir.w);
|
||||
|
||||
#if defined(USE_SHADOWMAP)
|
||||
lightColor *= shadowValue;
|
||||
#endif
|
||||
|
||||
gl_FragColor.rgb += lightColor * reflectance * NL;
|
||||
gl_FragColor.rgb += lightColor * reflectance * NL2;
|
||||
#endif
|
||||
|
||||
gl_FragColor.a = diffuse.a;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue