|
|
@@ -26,36 +26,36 @@ Vec3 F_Unreal(Vec3 specular, F32 VoH)
|
|
|
// It has lower VGRPs than F_Unreal
|
|
|
Vec3 F_Schlick(Vec3 specular, F32 VoH)
|
|
|
{
|
|
|
- F32 a = 1.0 - VoH;
|
|
|
- F32 a2 = a * a;
|
|
|
- F32 a5 = a2 * a2 * a; // a5 = a^5
|
|
|
+ const F32 a = 1.0 - VoH;
|
|
|
+ const F32 a2 = a * a;
|
|
|
+ const F32 a5 = a2 * a2 * a; // a5 = a^5
|
|
|
return /*saturate(50.0 * specular.g) */ a5 + (1.0 - a5) * specular;
|
|
|
}
|
|
|
|
|
|
// D(n,h) aka NDF: GGX Trowbridge-Reitz
|
|
|
F32 D_GGX(F32 roughness, F32 NoH)
|
|
|
{
|
|
|
- F32 a = roughness * roughness;
|
|
|
- F32 a2 = a * a;
|
|
|
+ const F32 a = roughness * roughness;
|
|
|
+ const F32 a2 = a * a;
|
|
|
|
|
|
- F32 D = (NoH * a2 - NoH) * NoH + 1.0;
|
|
|
+ const F32 D = (NoH * a2 - NoH) * NoH + 1.0;
|
|
|
return a2 / (PI * D * D);
|
|
|
}
|
|
|
|
|
|
// Visibility term: Geometric shadowing divided by BRDF denominator
|
|
|
F32 V_Schlick(F32 roughness, F32 NoV, F32 NoL)
|
|
|
{
|
|
|
- F32 k = (roughness * roughness) * 0.5;
|
|
|
- F32 Vis_SchlickV = NoV * (1.0 - k) + k;
|
|
|
- F32 Vis_SchlickL = NoL * (1.0 - k) + k;
|
|
|
+ const F32 k = (roughness * roughness) * 0.5;
|
|
|
+ const F32 Vis_SchlickV = NoV * (1.0 - k) + k;
|
|
|
+ const F32 Vis_SchlickL = NoL * (1.0 - k) + k;
|
|
|
return 0.25 / (Vis_SchlickV * Vis_SchlickL);
|
|
|
}
|
|
|
|
|
|
Vec3 envBRDF(Vec3 specular, F32 roughness, sampler2D integrationLut, F32 NoV)
|
|
|
{
|
|
|
- F32 a = roughness * roughness;
|
|
|
- F32 a2 = a * a;
|
|
|
- Vec2 envBRDF = textureLod(integrationLut, Vec2(a2, NoV), 0.0).xy;
|
|
|
+ const F32 a = roughness * roughness;
|
|
|
+ const F32 a2 = a * a;
|
|
|
+ const Vec2 envBRDF = textureLod(integrationLut, Vec2(a2, NoV), 0.0).xy;
|
|
|
return specular * envBRDF.x + /*min(1.0, 50.0 * specular.g) */ envBRDF.y;
|
|
|
}
|
|
|
|
|
|
@@ -67,33 +67,33 @@ Vec3 diffuseLambert(Vec3 diffuse)
|
|
|
// Performs BRDF specular lighting
|
|
|
Vec3 computeSpecularColorBrdf(GbufferInfo gbuffer, Vec3 viewDir, Vec3 frag2Light)
|
|
|
{
|
|
|
- Vec3 H = normalize(frag2Light + viewDir);
|
|
|
+ const Vec3 H = normalize(frag2Light + viewDir);
|
|
|
|
|
|
- F32 NoL = max(EPSILON, dot(gbuffer.m_normal, frag2Light));
|
|
|
- F32 VoH = max(EPSILON, dot(viewDir, H));
|
|
|
- F32 NoH = max(EPSILON, dot(gbuffer.m_normal, H));
|
|
|
- F32 NoV = max(EPSILON, dot(gbuffer.m_normal, viewDir));
|
|
|
+ const F32 NoL = max(EPSILON, dot(gbuffer.m_normal, frag2Light));
|
|
|
+ const F32 VoH = max(EPSILON, dot(viewDir, H));
|
|
|
+ const F32 NoH = max(EPSILON, dot(gbuffer.m_normal, H));
|
|
|
+ const F32 NoV = max(EPSILON, dot(gbuffer.m_normal, viewDir));
|
|
|
|
|
|
// F
|
|
|
#if 0
|
|
|
- Vec3 F = F_Unreal(gbuffer.m_specular, VoH);
|
|
|
+ const Vec3 F = F_Unreal(gbuffer.m_specular, VoH);
|
|
|
#else
|
|
|
- Vec3 F = F_Schlick(gbuffer.m_specular, VoH);
|
|
|
+ const Vec3 F = F_Schlick(gbuffer.m_specular, VoH);
|
|
|
#endif
|
|
|
|
|
|
// D
|
|
|
- F32 D = D_GGX(gbuffer.m_roughness, NoH);
|
|
|
+ const F32 D = D_GGX(gbuffer.m_roughness, NoH);
|
|
|
|
|
|
// Vis
|
|
|
- F32 V = V_Schlick(gbuffer.m_roughness, NoV, NoL);
|
|
|
+ const F32 V = V_Schlick(gbuffer.m_roughness, NoV, NoL);
|
|
|
|
|
|
return F * (V * D);
|
|
|
}
|
|
|
|
|
|
F32 computeSpotFactor(Vec3 l, F32 outerCos, F32 innerCos, Vec3 spotDir)
|
|
|
{
|
|
|
- F32 costheta = -dot(l, spotDir);
|
|
|
- F32 spotFactor = smoothstep(outerCos, innerCos, costheta);
|
|
|
+ const F32 costheta = -dot(l, spotDir);
|
|
|
+ const F32 spotFactor = smoothstep(outerCos, innerCos, costheta);
|
|
|
return spotFactor;
|
|
|
}
|
|
|
|
|
|
@@ -101,25 +101,25 @@ U32 computeShadowSampleCount(const U32 COUNT, F32 zVSpace)
|
|
|
{
|
|
|
const F32 MAX_DISTANCE = 5.0;
|
|
|
|
|
|
- F32 z = max(zVSpace, -MAX_DISTANCE);
|
|
|
+ const F32 z = max(zVSpace, -MAX_DISTANCE);
|
|
|
F32 sampleCountf = F32(COUNT) + z * (F32(COUNT) / MAX_DISTANCE);
|
|
|
sampleCountf = max(sampleCountf, 1.0);
|
|
|
- U32 sampleCount = U32(sampleCountf);
|
|
|
+ const U32 sampleCount = U32(sampleCountf);
|
|
|
|
|
|
return sampleCount;
|
|
|
}
|
|
|
|
|
|
F32 computeShadowFactorSpotLight(SpotLight light, Vec3 worldPos, sampler2D spotMapArr)
|
|
|
{
|
|
|
- Vec4 texCoords4 = light.m_texProjectionMat * Vec4(worldPos, 1.0);
|
|
|
- Vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
|
|
|
+ const Vec4 texCoords4 = light.m_texProjectionMat * Vec4(worldPos, 1.0);
|
|
|
+ const Vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
|
|
|
|
|
|
const F32 near = LIGHT_FRUSTUM_NEAR_PLANE;
|
|
|
const F32 far = light.m_radius;
|
|
|
|
|
|
- F32 linearDepth = linearizeDepth(texCoords3.z, near, far);
|
|
|
+ const F32 linearDepth = linearizeDepth(texCoords3.z, near, far);
|
|
|
|
|
|
- F32 shadowFactor = textureLod(spotMapArr, texCoords3.xy, 0.0).r;
|
|
|
+ const F32 shadowFactor = textureLod(spotMapArr, texCoords3.xy, 0.0).r;
|
|
|
|
|
|
return saturate(exp(ESM_CONSTANT * (shadowFactor - linearDepth)));
|
|
|
}
|
|
|
@@ -127,14 +127,14 @@ F32 computeShadowFactorSpotLight(SpotLight light, Vec3 worldPos, sampler2D spotM
|
|
|
// Compute the shadow factor of point (omni) lights.
|
|
|
F32 computeShadowFactorPointLight(PointLight light, Vec3 frag2Light, sampler2D shadowMap)
|
|
|
{
|
|
|
- Vec3 dir = -frag2Light;
|
|
|
- Vec3 dirabs = abs(dir);
|
|
|
- F32 dist = max(dirabs.x, max(dirabs.y, dirabs.z));
|
|
|
+ const Vec3 dir = -frag2Light;
|
|
|
+ const Vec3 dirabs = abs(dir);
|
|
|
+ const F32 dist = max(dirabs.x, max(dirabs.y, dirabs.z));
|
|
|
|
|
|
const F32 near = LIGHT_FRUSTUM_NEAR_PLANE;
|
|
|
const F32 far = light.m_radius;
|
|
|
|
|
|
- F32 linearDepth = (dist - near) / (far - near);
|
|
|
+ const F32 linearDepth = (dist - near) / (far - near);
|
|
|
|
|
|
// Read tex
|
|
|
F32 shadowFactor;
|
|
|
@@ -161,12 +161,12 @@ F32 computeShadowFactorPointLight(PointLight light, Vec3 frag2Light, sampler2D s
|
|
|
// Compute the shadow factor of a directional light
|
|
|
F32 computeShadowFactorDirLight(DirectionalLight light, U32 cascadeIdx, Vec3 worldPos, sampler2D shadowMap)
|
|
|
{
|
|
|
- Mat4 lightProjectionMat = light.m_textureMatrices[cascadeIdx];
|
|
|
+ const Mat4 lightProjectionMat = light.m_textureMatrices[cascadeIdx];
|
|
|
|
|
|
- Vec4 texCoords4 = lightProjectionMat * Vec4(worldPos, 1.0);
|
|
|
- Vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
|
|
|
+ const Vec4 texCoords4 = lightProjectionMat * Vec4(worldPos, 1.0);
|
|
|
+ const Vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
|
|
|
|
|
|
- F32 cascadeLinearDepth = texCoords3.z;
|
|
|
+ const F32 cascadeLinearDepth = texCoords3.z;
|
|
|
|
|
|
F32 shadowFactor = textureLod(shadowMap, texCoords3.xy, 0.0).r;
|
|
|
shadowFactor = saturate(exp(ESM_CONSTANT * 3.0 * (shadowFactor - cascadeLinearDepth)));
|
|
|
@@ -177,22 +177,22 @@ F32 computeShadowFactorDirLight(DirectionalLight light, U32 cascadeIdx, Vec3 wor
|
|
|
// Compute the shadow factor of a directional light
|
|
|
F32 computeShadowFactorDirLight(Mat4 lightProjectionMat, Vec3 worldPos, sampler2DShadow shadowMap)
|
|
|
{
|
|
|
- Vec4 texCoords4 = lightProjectionMat * Vec4(worldPos, 1.0);
|
|
|
- Vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
|
|
|
+ const Vec4 texCoords4 = lightProjectionMat * Vec4(worldPos, 1.0);
|
|
|
+ const Vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
|
|
|
|
|
|
- F32 shadowFactor = textureLod(shadowMap, texCoords3, 0.0);
|
|
|
+ const F32 shadowFactor = textureLod(shadowMap, texCoords3, 0.0);
|
|
|
return shadowFactor;
|
|
|
}
|
|
|
|
|
|
// Compute the cubemap texture lookup vector given the reflection vector (r) the radius squared of the probe (R2) and
|
|
|
// the frag pos in sphere space (f)
|
|
|
-Vec3 computeCubemapVecAccurate(in Vec3 r, in F32 R2, in Vec3 f)
|
|
|
+Vec3 computeCubemapVecAccurate(Vec3 r, F32 R2, Vec3 f)
|
|
|
{
|
|
|
// Compute the collision of the r to the inner part of the sphere
|
|
|
// From now on we work on the sphere's space
|
|
|
|
|
|
// Project the center of the sphere (it's zero now since we are in sphere space) in ray "f,r"
|
|
|
- Vec3 p = f - r * dot(f, r);
|
|
|
+ const Vec3 p = f - r * dot(f, r);
|
|
|
|
|
|
// The collision to the sphere is point x where x = p + T * r
|
|
|
// Because of the pythagorean theorem: R^2 = dot(p, p) + dot(T * r, T * r)
|
|
|
@@ -200,21 +200,21 @@ Vec3 computeCubemapVecAccurate(in Vec3 r, in F32 R2, in Vec3 f)
|
|
|
// then x becomes x = sqrt(R^2 - dot(p, p)) * r + p;
|
|
|
F32 pp = dot(p, p);
|
|
|
pp = min(pp, R2);
|
|
|
- F32 sq = sqrt(R2 - pp);
|
|
|
- Vec3 x = p + sq * r;
|
|
|
+ const F32 sq = sqrt(R2 - pp);
|
|
|
+ const Vec3 x = p + sq * r;
|
|
|
|
|
|
return x;
|
|
|
}
|
|
|
|
|
|
// Cheap version of computeCubemapVecAccurate
|
|
|
-Vec3 computeCubemapVecCheap(in Vec3 r, in F32 R2, in Vec3 f)
|
|
|
+Vec3 computeCubemapVecCheap(Vec3 r, F32 R2, Vec3 f)
|
|
|
{
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
F32 computeAttenuationFactor(F32 lightRadius, Vec3 frag2Light)
|
|
|
{
|
|
|
- F32 fragLightDist = dot(frag2Light, frag2Light);
|
|
|
+ const F32 fragLightDist = dot(frag2Light, frag2Light);
|
|
|
F32 att = 1.0 - fragLightDist * lightRadius;
|
|
|
att = max(0.0, att);
|
|
|
return att * att;
|
|
|
@@ -229,8 +229,8 @@ Vec3 intersectProbe(Vec3 fragPos, // Ray origin
|
|
|
)
|
|
|
{
|
|
|
// Compute the intersection point
|
|
|
- F32 intresectionDist = rayAabbIntersectionInside(fragPos, rayDir, probeAabbMin, probeAabbMax);
|
|
|
- Vec3 intersectionPoint = fragPos + intresectionDist * rayDir;
|
|
|
+ const F32 intresectionDist = rayAabbIntersectionInside(fragPos, rayDir, probeAabbMin, probeAabbMax);
|
|
|
+ const Vec3 intersectionPoint = fragPos + intresectionDist * rayDir;
|
|
|
|
|
|
// Compute the cubemap vector
|
|
|
return intersectionPoint - probeOrigin;
|
|
|
@@ -244,10 +244,10 @@ F32 computeProbeBlendWeight(Vec3 fragPos, // Doesn't need to be inside the AABB
|
|
|
F32 fadeDistance)
|
|
|
{
|
|
|
// Compute the min distance of fragPos from the edges of the AABB
|
|
|
- Vec3 distFromMin = fragPos - probeAabbMin;
|
|
|
- Vec3 distFromMax = probeAabbMax - fragPos;
|
|
|
- Vec3 minDistVec = min(distFromMin, distFromMax);
|
|
|
- F32 minDist = min(minDistVec.x, min(minDistVec.y, minDistVec.z));
|
|
|
+ const Vec3 distFromMin = fragPos - probeAabbMin;
|
|
|
+ const Vec3 distFromMax = probeAabbMax - fragPos;
|
|
|
+ const Vec3 minDistVec = min(distFromMin, distFromMax);
|
|
|
+ const F32 minDist = min(minDistVec.x, min(minDistVec.y, minDistVec.z));
|
|
|
|
|
|
// Use saturate because minDist might be negative.
|
|
|
return saturate(minDist / fadeDistance);
|