Skip to content
Permalink
Browse files

Merge pull request #206 from illwieckz/shade-cleanup

some tr_shade/glsl code cleanup
  • Loading branch information...
illwieckz committed Jul 6, 2019
2 parents a980dff + b588e05 commit 145ff5fe1aaa043b7366ea3860967f3a9add62f1
@@ -1191,6 +1191,21 @@ void GLShaderManager::BindAttribLocations( GLuint program ) const
}
}

// reflective specular not implemented for PBR yet
bool GLCompileMacro_USE_REFLECTIVE_SPECULAR::HasConflictingMacros( size_t permutation, const std::vector< GLCompileMacro * > &macros ) const
{
for (const GLCompileMacro* macro : macros)
{
if ( ( permutation & macro->GetBit() ) != 0 && (macro->GetType() == USE_PHYSICAL_SHADING || macro->GetType() == USE_VERTEX_SPRITE) )
{
//Log::Notice("conflicting macro! canceling '%s' vs. '%s'", GetName(), macro->GetName());
return true;
}
}

return false;
}

bool GLCompileMacro_USE_VERTEX_SKINNING::HasConflictingMacros( size_t permutation, const std::vector< GLCompileMacro * > &macros ) const
{
for (const GLCompileMacro* macro : macros)
@@ -1048,6 +1048,8 @@ class GLCompileMacro_USE_REFLECTIVE_SPECULAR :
return "USE_REFLECTIVE_SPECULAR";
}

bool HasConflictingMacros(size_t permutation, const std::vector< GLCompileMacro * > &macros) const;

EGLCompileMacro GetType() const
{
return EGLCompileMacro::USE_REFLECTIVE_SPECULAR;
@@ -49,18 +49,18 @@ uniform int u_numLights;
uniform vec2 u_SpecularExponent;

// lighting helper functions
void computeLight( vec3 lightDir, vec3 normal, vec3 eyeDir, vec3 lightColor,
void computeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
vec4 diffuseColor, vec4 specularColor,
inout vec4 accumulator ) {
vec3 H = normalize( lightDir + eyeDir );
vec3 H = normalize( lightDir + viewDir );
float NdotH = clamp( dot( normal, H ), 0.0, 1.0 );

#if defined(USE_PHYSICAL_SHADING)
float metalness = specularColor.x;
float roughness = specularColor.y;

float NdotV = clamp( dot( normal, eyeDir ), 0.0, 1.0);
float VdotH = clamp( dot( eyeDir, H ), 0.0, 1.0);
float NdotV = clamp( dot( normal, viewDir ), 0.0, 1.0);
float VdotH = clamp( dot( viewDir, H ), 0.0, 1.0);
float NdotL = clamp( dot( normal, lightDir ), 0.0, 1.0 );

float alpha = roughness * roughness;
@@ -128,7 +128,7 @@ int nextIdx( inout idxs_t idxs ) {

const int numLayers = MAX_REF_LIGHTS / 256;

void computeDLight( int idx, vec3 P, vec3 N, vec3 I, vec4 diffuse,
void computeDLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
vec4 specular, inout vec4 color ) {
vec4 center_radius = GetLight( idx, center_radius );
vec4 color_type = GetLight( idx, color_type );
@@ -155,12 +155,12 @@ void computeDLight( int idx, vec3 P, vec3 N, vec3 I, vec4 diffuse,
L = GetLight( idx, direction_angle ).xyz;
attenuation = 1.0;
}
computeLight( L, N, I,
computeLight( L, normal, viewDir,
attenuation * attenuation * color_type.xyz,
diffuse, specular, color );
}

void computeDLights( vec3 P, vec3 N, vec3 I, vec4 diffuse, vec4 specular,
void computeDLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 specular,
inout vec4 color ) {
vec2 tile = floor( gl_FragCoord.xy * (1.0 / float( TILE_SIZE ) ) ) + 0.5;
vec3 tileScale = vec3( r_tileStep, 1.0/numLayers );
@@ -185,7 +185,7 @@ void computeDLights( vec3 P, vec3 N, vec3 I, vec4 diffuse, vec4 specular,
return;
}

computeDLight( idx, P, N, I, diffuse, specular, color );
computeDLight( idx, P, normal, viewDir, diffuse, specular, color );

#if defined(r_showLightTiles)
numLights++;
@@ -37,26 +37,26 @@ DECLARE_OUTPUT(vec4)
void main()
{
// compute incident ray
vec3 I = normalize(var_Position - u_ViewOrigin);
vec3 incidentRay = normalize(var_Position - u_ViewOrigin);

// compute normal
vec3 N = normalize(var_Normal); // FIXME normalize?
vec3 normal = normalize(var_Normal); // FIXME normalize?

// compute reflection ray
vec3 R = reflect(I, N);
vec3 reflectionRay = reflect(incidentRay, normal);

// compute fresnel term
float fresnel = u_FresnelBias + pow(1.0 - dot(I, N), u_FresnelPower) * u_FresnelScale;
float fresnel = u_FresnelBias + pow(1.0 - dot(incidentRay, normal), u_FresnelPower) * u_FresnelScale;

// compute reflection color
vec3 reflectColor = textureCube(u_ColorMap, R).rgb;
vec3 reflectColor = textureCube(u_ColorMap, reflectionRay).rgb;

// compute refraction color using a refraction ray for each channel
vec3 refractColor;

refractColor.r = textureCube(u_ColorMap, refract(I, N, u_EtaRatio.x)).r;
refractColor.g = textureCube(u_ColorMap, refract(I, N, u_EtaRatio.y)).g;
refractColor.b = textureCube(u_ColorMap, refract(I, N, u_EtaRatio.z)).b;
refractColor.r = textureCube(u_ColorMap, refract(incidentRay, normal, u_EtaRatio.x)).r;
refractColor.g = textureCube(u_ColorMap, refract(incidentRay, normal, u_EtaRatio.y)).g;
refractColor.b = textureCube(u_ColorMap, refract(incidentRay, normal, u_EtaRatio.z)).b;

// compute final color
vec4 color;
@@ -443,23 +443,23 @@ vec4 PCF(vec4 shadowVert, float filterWidth, float samples, out vec4 clipMoments

#else

void FetchShadowMoments(vec3 I, out vec4 shadowMoments, out vec4 shadowClipMoments)
void FetchShadowMoments(vec3 incidentRay, out vec4 shadowMoments, out vec4 shadowClipMoments)
{
#if defined(EVSM) && defined(r_EVSMPostProcess)
shadowMoments = ShadowDepthToEVSM(textureCube(u_ShadowMap, I).SWIZ1);
shadowClipMoments = ShadowDepthToEVSM(textureCube(u_ShadowClipMap, I).SWIZ1);
shadowMoments = ShadowDepthToEVSM(textureCube(u_ShadowMap, incidentRay).SWIZ1);
shadowClipMoments = ShadowDepthToEVSM(textureCube(u_ShadowClipMap, incidentRay).SWIZ1);
#else
shadowMoments = FixShadowMoments(textureCube(u_ShadowMap, I));
shadowClipMoments = FixShadowMoments(textureCube(u_ShadowClipMap, I));
shadowMoments = FixShadowMoments(textureCube(u_ShadowMap, incidentRay));
shadowClipMoments = FixShadowMoments(textureCube(u_ShadowClipMap, incidentRay));
#endif
}

#if defined(r_PCFSamples)
vec4 PCF(vec4 I, float filterWidth, float samples, out vec4 clipMoments)
vec4 PCF(vec4 incidentRay, float filterWidth, float samples, out vec4 clipMoments)
{
vec3 forward, right, up;

forward = normalize(I.xyz);
forward = normalize(incidentRay.xyz);
MakeNormalVectors(forward, right, up);

vec4 moments = vec4(0.0, 0.0, 0.0, 0.0);
@@ -474,7 +474,7 @@ vec4 PCF(vec4 I, float filterWidth, float samples, out vec4 clipMoments)
for(float j = -filterWidth; j < filterWidth; j += stepSize)
{
vec4 sm, scm;
FetchShadowMoments(I.xyz + right * i + up * j, sm, scm);
FetchShadowMoments(incidentRay.xyz + right * i + up * j, sm, scm);
moments += sm;
clipMoments += scm;
}
@@ -489,7 +489,7 @@ vec4 PCF(vec4 I, float filterWidth, float samples, out vec4 clipMoments)
// rand = normalize(rand) * filterWidth;

vec4 sm, scm;
FetchShadowMoments(I.xyz + right * rand.x + up * rand.y, sm, scm);
FetchShadowMoments(incidentRay.xyz + right * rand.x + up * rand.y, sm, scm);
moments += sm;
clipMoments += scm;
}
@@ -548,11 +548,11 @@ float SumBlocker(vec4 shadowVert, float vertexDistance, float filterWidth, float
}
#else
// case LIGHT_OMNI
float SumBlocker(vec4 I, float vertexDistance, float filterWidth, float samples)
float SumBlocker(vec4 incidentRay, float vertexDistance, float filterWidth, float samples)
{
vec3 forward, right, up;

forward = normalize(I.xyz);
forward = normalize(incidentRay.xyz);
MakeNormalVectors(forward, right, up);

float stepSize = 2.0 * filterWidth / samples;
@@ -564,7 +564,7 @@ float SumBlocker(vec4 I, float vertexDistance, float filterWidth, float samples)
{
for(float j = -filterWidth; j < filterWidth; j += stepSize)
{
float shadowDistance = textureCube(u_ShadowMap, I.xyz + right * i + up * j).SWIZ1;
float shadowDistance = textureCube(u_ShadowMap, incidentRay.xyz + right * i + up * j).SWIZ1;

if(vertexDistance > shadowDistance)
{
@@ -596,12 +596,12 @@ float EstimatePenumbra(float vertexDistance, float blocker)
return penumbra;
}

vec4 PCSS( vec4 I, float vertexDistance, float PCFSamples )
vec4 PCSS( vec4 incidentRay, float vertexDistance, float PCFSamples )
{
// step 1: find blocker estimate
const float blockerSamples = 6.0;
float blockerSearchWidth = u_ShadowTexelSize * u_LightRadius / vertexDistance;
float blocker = SumBlocker(I, vertexDistance, blockerSearchWidth, blockerSamples);
float blocker = SumBlocker(incidentRay, vertexDistance, blockerSearchWidth, blockerSamples);

// step 2: estimate penumbra using parallel planes approximation
float penumbra = EstimatePenumbra(vertexDistance, blocker);
@@ -616,13 +616,13 @@ vec4 PCSS( vec4 I, float vertexDistance, float PCFSamples )
// penumbra = maxpen;
//

// shadowMoments = PCF(I, penumbra, PCFsamples);
// shadowMoments = PCF(incidentRay, penumbra, PCFsamples);
vec4 clipMoments;
shadowMoments = PCF(I, u_ShadowTexelSize * u_ShadowBlur * penumbra, PCFsamples, clipMoments);
shadowMoments = PCF(incidentRay, u_ShadowTexelSize * u_ShadowBlur * penumbra, PCFsamples, clipMoments);
}
else
{
shadowMoments = FetchShadowMoments(I);
shadowMoments = FetchShadowMoments(incidentRay);
}
}
#endif
@@ -870,9 +870,9 @@ void main()
vec4 shadowVert = u_ShadowMatrix[0] * vec4(var_Position.xyz, 1.0);

// compute incident ray
vec3 I = var_Position.xyz - u_LightOrigin;
vec3 incidentRay = var_Position.xyz - u_LightOrigin;

float vertexDistance = length(I) / u_LightRadius - SHADOW_BIAS;
float vertexDistance = length(incidentRay) / u_LightRadius - SHADOW_BIAS;
if( vertexDistance >= 1.0f ) {
discard;
return;
@@ -896,26 +896,26 @@ void main()

#else
// compute incident ray
vec3 I = var_Position.xyz - u_LightOrigin;
float ILen = length(I);
float vertexDistance = ILen / u_LightRadius - SHADOW_BIAS;
vec3 incidentRay = var_Position.xyz - u_LightOrigin;
float incidentRayLen = length(incidentRay);
float vertexDistance = incidentRayLen / u_LightRadius - SHADOW_BIAS;

#if 0
outputColor = vec4(u_ShadowTexelSize * u_ShadowBlur * ILen, 0.0, 0.0, 1.0);
outputColor = vec4(u_ShadowTexelSize * u_ShadowBlur * incidentRayLen, 0.0, 0.0, 1.0);
return;
#endif

#if defined(r_PCFSamples)
#if 0//defined(PCSS)
vec4 shadowMoments = PCSS(vec4(I, 0.0), r_PCFSamples);
vec4 shadowMoments = PCSS(vec4(incidentRay, 0.0), r_PCFSamples);
#else
vec4 shadowClipMoments;
vec4 shadowMoments = PCF(vec4(I, 0.0), u_ShadowTexelSize * u_ShadowBlur * ILen, r_PCFSamples, shadowClipMoments);
vec4 shadowMoments = PCF(vec4(incidentRay, 0.0), u_ShadowTexelSize * u_ShadowBlur * incidentRayLen, r_PCFSamples, shadowClipMoments);
#endif
#else
// no extra filtering, single tap
vec4 shadowMoments, shadowClipMoments;
FetchShadowMoments(I, shadowMoments, shadowClipMoments);
FetchShadowMoments(incidentRay, shadowMoments, shadowClipMoments);
#endif
#endif
shadow = ShadowTest(vertexDistance, shadowMoments, shadowClipMoments);
@@ -957,13 +957,13 @@ void main()
vec3 H = normalize(L + viewDir);

// compute normal in world space from normal map
vec3 N = NormalInWorldSpace(texCoords, tangentToWorldMatrix);
vec3 normal = NormalInWorldSpace(texCoords, tangentToWorldMatrix);

// compute the light term
#if defined(r_WrapAroundLighting)
float NL = clamp(dot(N, L) + u_LightWrapAround, 0.0, 1.0) / clamp(1.0 + u_LightWrapAround, 0.0, 1.0);
float NL = clamp(dot(normal, L) + u_LightWrapAround, 0.0, 1.0) / clamp(1.0 + u_LightWrapAround, 0.0, 1.0);
#else
float NL = clamp(dot(N, L), 0.0, 1.0);
float NL = clamp(dot(normal, L), 0.0, 1.0);
#endif

// compute the diffuse term
@@ -978,7 +978,7 @@ void main()
#if defined(r_specularMapping)
// compute the specular term
vec4 spec = texture2D(u_SpecularMap, texCoords).rgba;
vec3 specular = spec.rgb * u_LightColor * pow(clamp(dot(N, H), 0.0, 1.0), u_SpecularExponent.x * spec.a + u_SpecularExponent.y) * r_SpecularScale;
vec3 specular = spec.rgb * u_LightColor * pow(clamp(dot(normal, H), 0.0, 1.0), u_SpecularExponent.x * spec.a + u_SpecularExponent.y) * r_SpecularScale;
#endif // r_specularMapping

// compute light attenuation
@@ -35,13 +35,13 @@ void main()
vec4 color;

// compute normal in tangent space from normalmap
vec3 N = NormalInTangentSpace(var_TexCoords);
vec3 normal = NormalInTangentSpace(var_TexCoords);

// calculate the screen texcoord in the 0.0 to 1.0 range
vec2 st = gl_FragCoord.st * r_FBufScale;

// offset by the scaled normal and clamp it to 0.0 - 1.0
st += N.xy * var_Deform;
st += normal.xy * var_Deform;
st = clamp(st, 0.0, 1.0);

color = texture2D(u_CurrentMap, st);
@@ -70,7 +70,7 @@ void main()
vec4 specular = texture2D(u_SpecularMap, texCoords);

// compute normal in world space from normalmap
vec3 N = NormalInWorldSpace(texCoords, tangentToWorldMatrix);
vec3 normal = NormalInWorldSpace(texCoords, tangentToWorldMatrix);

// compute light color from world space lightmap
vec3 lightColor = texture2D(u_LightMap, var_TexLight).xyz;
@@ -80,23 +80,21 @@ void main()
#if defined(USE_DELUXE_MAPPING)
// compute light direction in world space
vec4 deluxe = texture2D(u_DeluxeMap, var_TexLight);
#else // !USE_DELUXE_MAPPING
// normal/deluxe mapping is disabled
color.xyz += lightColor.xyz * diffuse.xyz;
#endif // USE_DELUXE_MAPPING

#if defined(USE_DELUXE_MAPPING)
vec3 L = 2.0 * deluxe.xyz - 1.0;
L = normalize(L);

// divide by cosine term to restore original light color
lightColor /= clamp(dot(normalize(var_Normal), L), 0.004, 1.0);

// compute final color
computeLight( L, N, viewDir, lightColor, diffuse, specular, color );
computeLight( L, normal, viewDir, lightColor, diffuse, specular, color );
#else // !USE_DELUXE_MAPPING
// normal/deluxe mapping is disabled
color.xyz += lightColor.xyz * diffuse.xyz;
#endif // USE_DELUXE_MAPPING

computeDLights( var_Position, N, viewDir, diffuse, specular, color );
computeDLights( var_Position, normal, viewDir, diffuse, specular, color );

#if defined(r_glowMapping)
color.rgb += texture2D(u_GlowMap, texCoords).rgb;
@@ -58,10 +58,10 @@ void main()
P.xyz /= P.w;

// compute incident ray in world space
vec3 R = normalize(P.xyz - u_ViewOrigin);
//vec3 R = normalize(u_ViewOrigin - P.xyz);
vec3 incidentRay = normalize(P.xyz - u_ViewOrigin);
//vec3 incidentRay = normalize(u_ViewOrigin - P.xyz);

//float traceDistance = dot(P.xyz - (u_ViewOrigin.xyz + R * u_ZNear ), forward);
//float traceDistance = dot(P.xyz - (u_ViewOrigin.xyz + incidentRay * u_ZNear ), forward);
//traceDistance = clamp(traceDistance, 0.0, 2500.0 ); // Far trace distance

float traceDistance = distance(P.xyz, u_ViewOrigin);
@@ -79,9 +79,9 @@ void main()

for(float tracedDistance = 0.0; tracedDistance < traceDistance; tracedDistance += stepSize)
{
//vec3 T = P.xyz + (R * stepSize * float(i));
//vec3 T = u_ViewOrigin + (R * stepSize * float(i));
vec3 T = u_ViewOrigin + (R * tracedDistance);
//vec3 T = P.xyz + (incidentRay * stepSize * float(i));
//vec3 T = u_ViewOrigin + (incidentRay * stepSize * float(i));
vec3 T = u_ViewOrigin + (incidentRay * tracedDistance);

// compute attenuation
vec3 texAttenXYZ = (u_LightAttenuationMatrix * vec4(T, 1.0)).xyz;

0 comments on commit 145ff5f

Please sign in to comment.
You can’t perform that action at this time.