Browse Source

Add const correctness to shaders

Panagiotis Christopoulos Charitos 6 years ago
parent
commit
294e0ca8b0

+ 2 - 2
shaders/ApplyIrradianceToReflection.glslp

@@ -34,10 +34,10 @@ void main()
 	readGBuffer(u_gbufferTex0, u_gbufferTex1, u_gbufferTex2, sampleUv, 0.0, gbuffer);
 
 	// Read the irradiance. Use the layer 0 because C++ will set the appropriate texture view
-	Vec3 irradiance = textureLod(u_irradianceTex, Vec4(gbuffer.m_normal, 0.0), 0.0).rgb;
+	const Vec3 irradiance = textureLod(u_irradianceTex, Vec4(gbuffer.m_normal, 0.0), 0.0).rgb;
 
 	// Compute the indirect term
-	Vec3 indirect = gbuffer.m_diffuse * irradiance;
+	const Vec3 indirect = gbuffer.m_diffuse * irradiance;
 
 	// Write it
 	out_color = indirect;

+ 1 - 1
shaders/Bloom.glslp

@@ -37,7 +37,7 @@ void main()
 		}
 	}
 
-	Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
+	const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
 
 	Vec3 color = textureLod(u_tex, uv, 0.0).rgb;
 	color += textureLodOffset(u_tex, uv, 0.0, ivec2(+1, +1)).rgb;

+ 7 - 7
shaders/BloomUpscale.glslp

@@ -46,17 +46,17 @@ Vec3 ssLensFlare(Vec2 uv)
 	const Vec3 DISTORTION = Vec3(-TEXEL_SIZE.x * CHROMATIC_DISTORTION, 0.0, TEXEL_SIZE.x * CHROMATIC_DISTORTION);
 	const F32 LEN_OF_HALF = length(Vec2(0.5));
 
-	Vec2 flipUv = Vec2(1.0) - uv;
+	const Vec2 flipUv = Vec2(1.0) - uv;
 
-	Vec2 ghostVec = (Vec2(0.5) - flipUv) * GHOST_DISPERSAL;
+	const Vec2 ghostVec = (Vec2(0.5) - flipUv) * GHOST_DISPERSAL;
 
-	Vec2 direction = normalize(ghostVec);
+	const Vec2 direction = normalize(ghostVec);
 	Vec3 result = Vec3(0.0);
 
 	// sample ghosts:
 	ANKI_UNROLL for(U32 i = 0u; i < MAX_GHOSTS; ++i)
 	{
-		Vec2 offset = fract(flipUv + ghostVec * F32(i));
+		const Vec2 offset = fract(flipUv + ghostVec * F32(i));
 
 		F32 weight = length(Vec2(0.5) - offset) / LEN_OF_HALF;
 		weight = pow(1.0 - weight, 10.0);
@@ -66,7 +66,7 @@ Vec3 ssLensFlare(Vec2 uv)
 
 	// Sample halo
 #if ENABLE_HALO
-	Vec2 haloVec = normalize(ghostVec) * HALO_WIDTH;
+	const Vec2 haloVec = normalize(ghostVec) * HALO_WIDTH;
 	F32 weight = length(Vec2(0.5) - fract(flipUv + haloVec)) / LEN_OF_HALF;
 	weight = pow(1.0 - weight, 20.0);
 	result += textureDistorted(u_tex, flipUv + haloVec, direction, DISTORTION) * (weight * HALO_OPACITY);
@@ -100,9 +100,9 @@ void main()
 		}
 	}
 
-	Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
+	const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
 
-	Vec3 outColor = ssLensFlare(uv) + upscale(uv);
+	const Vec3 outColor = ssLensFlare(uv) + upscale(uv);
 	imageStore(out_img, IVec2(gl_GlobalInvocationID.xy), Vec4(outColor, 0.0));
 }
 #pragma anki end

+ 1 - 1
shaders/ClusteredShadingCommon.glsl

@@ -159,6 +159,6 @@ Vec3 lightHeatmap(U32 firstIndex, U32 maxLights, Bool decals, Bool plights, Bool
 		count += (fogVolumes) ? 1u : 0u;
 	}
 
-	F32 factor = min(1.0, F32(count) / F32(maxLights));
+	const F32 factor = min(1.0, F32(count) / F32(maxLights));
 	return heatmap(factor);
 }

+ 6 - 6
shaders/DepthAwareBlur.glsl

@@ -54,8 +54,8 @@ layout(location = 0) out COL_TYPE out_color;
 
 F32 computeDepthWeight(F32 refDepth, F32 depth)
 {
-	F32 diff = abs(refDepth - depth);
-	F32 weight = 1.0 / (EPSILON + diff);
+	const F32 diff = abs(refDepth - depth);
+	const F32 weight = 1.0 / (EPSILON + diff);
 	return sqrt(weight);
 }
 
@@ -66,7 +66,7 @@ F32 readDepth(Vec2 uv)
 
 void sampleTex(Vec2 uv, F32 refDepth, inout COL_TYPE col, inout F32 weight)
 {
-	COL_TYPE color = textureLod(u_inTex, uv, 0.0).TEX_FETCH;
+	const COL_TYPE color = textureLod(u_inTex, uv, 0.0).TEX_FETCH;
 	F32 w = computeDepthWeight(refDepth, readDepth(uv));
 	col += color * w;
 	weight += w;
@@ -82,16 +82,16 @@ void main()
 		return;
 	}
 
-	Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(TEXTURE_SIZE);
+	const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(TEXTURE_SIZE);
 #else
-	Vec2 uv = in_uv;
+	const Vec2 uv = in_uv;
 #endif
 
 	const Vec2 TEXEL_SIZE = 1.0 / Vec2(TEXTURE_SIZE);
 
 	// Sample
 	COL_TYPE color = textureLod(u_inTex, uv, 0.0).TEX_FETCH;
-	F32 refDepth = readDepth(uv);
+	const F32 refDepth = readDepth(uv);
 	F32 weight = 1.0;
 
 #if !defined(BOX)

+ 3 - 3
shaders/DepthDownscale.glslp

@@ -57,7 +57,7 @@ F32 resolveDepths(Vec4 depths)
 void main()
 {
 	// Read depth
-	Vec2 readUv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(u_level0WriteImgSize);
+	const Vec2 readUv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(u_level0WriteImgSize);
 	Vec4 depths = textureGather(u_readTex, readUv, 0);
 
 	// Resolve & store the 1st level
@@ -89,14 +89,14 @@ void main()
 
 		depth = resolveDepths(depths);
 
-		UVec2 writeUv = gl_GlobalInvocationID.xy >> 1u;
+		const UVec2 writeUv = gl_GlobalInvocationID.xy >> 1u;
 		if(all(lessThan(writeUv, u_level1WriteImgSize)))
 		{
 			imageStore(u_level1WriteImg, IVec2(writeUv), Vec4(depth));
 
 			if(u_copyToClientLevel == 1u)
 			{
-				U32 idx = writeUv.y * u_level1WriteImgSize.x + writeUv.x;
+				const U32 idx = writeUv.y * u_level1WriteImgSize.x + writeUv.x;
 				u_clientBuf[idx] = depth;
 			}
 		}

+ 1 - 1
shaders/ExponentialShadowmappingResolve.glslp

@@ -42,7 +42,7 @@ layout(location = 2) flat out Vec2 out_minUv;
 void main()
 {
 	out_uv = Vec2(gl_VertexID & 1, gl_VertexID >> 1) * 2.0;
-	Vec2 pos = out_uv * 2.0 - 1.0;
+	const Vec2 pos = out_uv * 2.0 - 1.0;
 
 	out_uv = fma(out_uv, u_uvScale, u_uvTranslation);
 	gl_Position = Vec4(pos, 0.0, 1.0);

+ 4 - 4
shaders/FinalComposite.glslp

@@ -45,19 +45,19 @@ ANKI_PUSH_CONSTANTS(PushConsts, u_regs);
 layout(location = 0) in Vec2 in_uv;
 layout(location = 0) out Vec3 out_color;
 
-Vec3 colorGrading(in Vec3 color)
+Vec3 colorGrading(Vec3 color)
 {
 	const Vec3 LUT_SCALE = Vec3((F32(LUT_SIZE) - 1.0) / F32(LUT_SIZE));
 	const Vec3 LUT_OFFSET = Vec3(1.0 / (2.0 * F32(LUT_SIZE)));
 
 	color = min(color, Vec3(1.0));
-	Vec3 lutCoords = color * LUT_SCALE + LUT_OFFSET;
+	const Vec3 lutCoords = color * LUT_SCALE + LUT_OFFSET;
 	return textureLod(u_lut, lutCoords, 0.0).rgb;
 }
 
 void main()
 {
-	Vec2 uv = in_uv.xy;
+	const Vec2 uv = in_uv.xy;
 
 	if(MOTION_BLUR_SAMPLES > 0u)
 	{
@@ -76,7 +76,7 @@ void main()
 	out_color = tonemap(out_color, UNIFORM(u_exposureThreshold0));
 
 #if BLOOM_ENABLED
-	Vec3 bloom = textureLod(u_ppsBloomLfRt, uv, 0.0).rgb;
+	const Vec3 bloom = textureLod(u_ppsBloomLfRt, uv, 0.0).rgb;
 	out_color += bloom;
 #endif
 

+ 23 - 23
shaders/ForwardShadingCommonFrag.glsl

@@ -25,15 +25,15 @@ layout(ANKI_TEX_BINDING(0, 1)) uniform sampler3D u_lightVol;
 
 layout(location = 0) out Vec4 out_color;
 
-void writeGBuffer(in Vec4 color)
+void writeGBuffer(Vec4 color)
 {
 	out_color = Vec4(color.rgb, color.a);
 }
 
 Vec4 readAnimatedTextureRgba(sampler2DArray tex, F32 period, Vec2 uv, F32 time)
 {
-	F32 layerCount = F32(textureSize(tex, 0).z);
-	F32 layer = mod(time * layerCount / period, layerCount);
+	const F32 layerCount = F32(textureSize(tex, 0).z);
+	const F32 layer = mod(time * layerCount / period, layerCount);
 	return texture(tex, Vec3(uv, layer));
 }
 
@@ -44,7 +44,7 @@ Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos)
 	Vec3 outColor = Vec3(0.0);
 
 	// Find the cluster and then the light counts
-	U32 clusterIdx = computeClusterIndex(
+	const U32 clusterIdx = computeClusterIndex(
 		u_clustererMagic, gl_FragCoord.xy / RENDERER_SIZE, worldPos, u_clusterCountX, u_clusterCountY);
 
 	U32 idxOffset = u_clusters[clusterIdx];
@@ -53,12 +53,12 @@ Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos)
 	U32 idx;
 	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
 	{
-		PointLight light = u_pointLights[idx];
+		const PointLight light = u_pointLights[idx];
 
-		Vec3 diffC = diffCol * light.m_diffuseColor;
+		const Vec3 diffC = diffCol * light.m_diffuseColor;
 
-		Vec3 frag2Light = light.m_position - worldPos;
-		F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
+		const Vec3 frag2Light = light.m_position - worldPos;
+		const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
 
 #if LOD > 1
 		const F32 shadow = 1.0;
@@ -76,22 +76,22 @@ Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos)
 	// Spot lights
 	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
 	{
-		SpotLight light = u_spotLights[idx];
+		const SpotLight light = u_spotLights[idx];
 
-		Vec3 diffC = diffCol * light.m_diffuseColor;
+		const Vec3 diffC = diffCol * light.m_diffuseColor;
 
-		Vec3 frag2Light = light.m_position - worldPos;
-		F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
+		const Vec3 frag2Light = light.m_position - worldPos;
+		const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
 
-		Vec3 l = normalize(frag2Light);
+		const Vec3 l = normalize(frag2Light);
 
-		F32 spot = computeSpotFactor(l, light.m_outerCos, light.m_innerCos, light.m_dir);
+		const F32 spot = computeSpotFactor(l, light.m_outerCos, light.m_innerCos, light.m_dir);
 
 #if LOD > 1
 		const F32 shadow = 1.0;
 #else
 		F32 shadow = 1.0;
-		F32 shadowmapLayerIdx = light.m_shadowmapId;
+		const F32 shadowmapLayerIdx = light.m_shadowmapId;
 		if(shadowmapLayerIdx >= 0.0)
 		{
 			shadow = computeShadowFactorSpotLight(light, worldPos, u_shadowTex);
@@ -107,10 +107,10 @@ Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos)
 // Just read the light color from the vol texture
 Vec3 computeLightColorLow(Vec3 diffCol, Vec3 worldPos)
 {
-	Vec2 uv = gl_FragCoord.xy / RENDERER_SIZE;
-	Vec3 uv3d = computeClustererVolumeTextureUvs(u_clustererMagic, uv, worldPos, u_lightVolumeLastCluster + 1u);
+	const Vec2 uv = gl_FragCoord.xy / RENDERER_SIZE;
+	const Vec3 uv3d = computeClustererVolumeTextureUvs(u_clustererMagic, uv, worldPos, u_lightVolumeLastCluster + 1u);
 
-	Vec3 light = textureLod(u_lightVol, uv3d, 0.0).rgb;
+	const Vec3 light = textureLod(u_lightVol, uv3d, 0.0).rgb;
 	return diffuseLambert(diffCol) * light;
 }
 
@@ -123,14 +123,14 @@ void fog(Vec3 color, F32 fogAlphaScale, F32 fogDistanceOfMaxThikness, F32 zVSpac
 {
 	const Vec2 screenSize = 1.0 / RENDERER_SIZE;
 
-	Vec2 texCoords = gl_FragCoord.xy * screenSize;
-	F32 depth = texture(u_gbufferDepthRt, texCoords).r;
+	const Vec2 texCoords = gl_FragCoord.xy * screenSize;
+	const F32 depth = texture(u_gbufferDepthRt, texCoords).r;
 	F32 zFeatherFactor;
 
-	Vec4 fragPosVspace4 = u_invProjMat * Vec4(Vec3(UV_TO_NDC(texCoords), depth), 1.0);
-	F32 sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
+	const Vec4 fragPosVspace4 = u_invProjMat * Vec4(Vec3(UV_TO_NDC(texCoords), depth), 1.0);
+	const F32 sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
 
-	F32 diff = max(0.0, zVSpace - sceneZVspace);
+	const F32 diff = max(0.0, zVSpace - sceneZVspace);
 
 	zFeatherFactor = min(1.0, diff / fogDistanceOfMaxThikness);
 

+ 36 - 35
shaders/Functions.glsl

@@ -8,7 +8,7 @@
 #include <shaders/Common.glsl>
 
 #if defined(ANKI_FRAGMENT_SHADER)
-Vec3 dither(in Vec3 col, in F32 C)
+Vec3 dither(Vec3 col, F32 C)
 {
 	Vec3 vDither = Vec3(dot(Vec2(171.0, 231.0), gl_FragCoord.xy));
 	vDither.rgb = fract(vDither.rgb / Vec3(103.0, 71.0, 97.0));
@@ -20,7 +20,7 @@ Vec3 dither(in Vec3 col, in F32 C)
 	return col;
 }
 
-F32 dither(in F32 col, in F32 C)
+F32 dither(F32 col, F32 C)
 {
 	F32 vDither = dot(Vec2(171.0, 231.0), gl_FragCoord.xy);
 	vDither = fract(vDither / 103.0);
@@ -34,25 +34,25 @@ F32 dither(in F32 col, in F32 C)
 #endif
 
 // Convert to linear depth
-F32 linearizeDepth(in F32 depth, in F32 zNear, in F32 zFar)
+F32 linearizeDepth(F32 depth, F32 zNear, F32 zFar)
 {
 	return zNear / ((zNear - zFar) + zFar / depth);
 }
 
 // This is the optimal linearizeDepth where a=(n-f)/n and b=f/n
-F32 linearizeDepthOptimal(in F32 depth, in F32 a, in F32 b)
+F32 linearizeDepthOptimal(F32 depth, F32 a, F32 b)
 {
 	return 1.0 / (a + b / depth);
 }
 
 // This is the optimal linearizeDepth where a=(n-f)/n and b=f/n
-Vec4 linearizeDepthOptimal(in Vec4 depths, in F32 a, in F32 b)
+Vec4 linearizeDepthOptimal(Vec4 depths, F32 a, F32 b)
 {
 	return 1.0 / (a + b / depths);
 }
 
 // Project a vector by knowing only the non zero values of a perspective matrix
-Vec4 projectPerspective(in Vec4 vec, in F32 m00, in F32 m11, in F32 m22, in F32 m23)
+Vec4 projectPerspective(Vec4 vec, F32 m00, F32 m11, F32 m22, F32 m23)
 {
 	Vec4 o;
 	o.x = vec.x * m00;
@@ -65,12 +65,12 @@ Vec4 projectPerspective(in Vec4 vec, in F32 m00, in F32 m11, in F32 m22, in F32
 // Stolen from shadertoy.com/view/4tyGDD
 Vec4 textureCatmullRom4Samples(sampler2D tex, Vec2 uv, Vec2 texSize)
 {
-	Vec2 halff = 2.0 * fract(0.5 * uv * texSize - 0.25) - 1.0;
-	Vec2 f = fract(halff);
-	Vec2 sum0 = (2.0 * f - 3.5) * f + 0.5;
-	Vec2 sum1 = (2.0 * f - 2.5) * f - 0.5;
+	const Vec2 halff = 2.0 * fract(0.5 * uv * texSize - 0.25) - 1.0;
+	const Vec2 f = fract(halff);
+	const Vec2 sum0 = (2.0 * f - 3.5) * f + 0.5;
+	const Vec2 sum1 = (2.0 * f - 2.5) * f - 0.5;
 	Vec4 w = Vec4(f * sum0 + 1.0, f * sum1);
-	Vec4 pos = Vec4((((-2.0 * f + 3.0) * f + 0.5) * f - 1.5) * f / (w.xy * texSize) + uv,
+	const Vec4 pos = Vec4((((-2.0 * f + 3.0) * f + 0.5) * f - 1.5) * f / (w.xy * texSize) + uv,
 		(((-2.0 * f + 5.0) * f - 2.5) * f - 0.5) / (sum1 * texSize) + uv);
 	w.xz *= halff.x * halff.y > 0.0 ? 1.0 : -1.0;
 
@@ -92,7 +92,7 @@ Vec4 nearestDepthUpscale(
 	Vec4 halfDepths = textureGather(depthHalf, uv, 0);
 	halfDepths = linearizeDepthOptimal(halfDepths, linearDepthCf.x, linearDepthCf.y);
 
-	Vec4 diffs = abs(Vec4(fullDepth) - halfDepths);
+	const Vec4 diffs = abs(Vec4(fullDepth) - halfDepths);
 	Vec4 color;
 
 	if(all(lessThan(diffs, Vec4(depthThreshold))))
@@ -103,10 +103,10 @@ Vec4 nearestDepthUpscale(
 	else
 	{
 		// Some discontinuites, need to use the newUv
-		Vec4 r = textureGather(colorTex, uv, 0);
-		Vec4 g = textureGather(colorTex, uv, 1);
-		Vec4 b = textureGather(colorTex, uv, 2);
-		Vec4 a = textureGather(colorTex, uv, 3);
+		const Vec4 r = textureGather(colorTex, uv, 0);
+		const Vec4 g = textureGather(colorTex, uv, 1);
+		const Vec4 b = textureGather(colorTex, uv, 2);
+		const Vec4 a = textureGather(colorTex, uv, 3);
 
 		F32 minDiff = diffs.x;
 		U32 comp = 0;
@@ -136,8 +136,8 @@ Vec4 nearestDepthUpscale(
 
 F32 _calcDepthWeight(sampler2D depthLow, Vec2 uv, F32 ref, Vec2 linearDepthCf)
 {
-	F32 d = texture(depthLow, uv).r;
-	F32 linearD = linearizeDepthOptimal(d, linearDepthCf.x, linearDepthCf.y);
+	const F32 d = texture(depthLow, uv).r;
+	const F32 linearD = linearizeDepthOptimal(d, linearDepthCf.x, linearDepthCf.y);
 	return 1.0 / (EPSILON + abs(ref - linearD));
 }
 
@@ -152,8 +152,8 @@ Vec4 _sampleAndWeight(sampler2D depthLow,
 	inout F32 normalize)
 {
 	uv += offset * lowInvSize;
-	F32 dw = _calcDepthWeight(depthLow, uv, ref, linearDepthCf);
-	Vec4 v = texture(colorLow, uv);
+	const F32 dw = _calcDepthWeight(depthLow, uv, ref, linearDepthCf);
+	const Vec4 v = texture(colorLow, uv);
 	normalize += weight * dw;
 	return v * dw * weight;
 }
@@ -162,7 +162,7 @@ Vec4 bilateralUpsample(
 	sampler2D depthHigh, sampler2D depthLow, sampler2D colorLow, Vec2 lowInvSize, Vec2 uv, Vec2 linearDepthCf)
 {
 	const Vec3 WEIGHTS = Vec3(0.25, 0.125, 0.0625);
-	F32 depthRef = linearizeDepthOptimal(texture(depthHigh, uv).r, linearDepthCf.x, linearDepthCf.y);
+	const F32 depthRef = linearizeDepthOptimal(texture(depthHigh, uv).r, linearDepthCf.x, linearDepthCf.y);
 	F32 normalize = 0.0;
 
 	Vec4 sum = _sampleAndWeight(
@@ -191,15 +191,16 @@ Vec3 getCubemapDirection(Vec2 norm, U32 faceIdx)
 {
 	Vec3 zDir = Vec3((faceIdx <= 1u) ? 1 : 0, (faceIdx & 2u) >> 1u, (faceIdx & 4u) >> 2u);
 	zDir *= (((faceIdx & 1u) == 1u) ? -1.0 : 1.0);
-	Vec3 yDir = (faceIdx == 2u) ? Vec3(0.0, 0.0, 1.0) : (faceIdx == 3u) ? Vec3(0.0, 0.0, -1.0) : Vec3(0.0, -1.0, 0.0);
-	Vec3 xDir = cross(zDir, yDir);
+	const Vec3 yDir =
+		(faceIdx == 2u) ? Vec3(0.0, 0.0, 1.0) : (faceIdx == 3u) ? Vec3(0.0, 0.0, -1.0) : Vec3(0.0, -1.0, 0.0);
+	const Vec3 xDir = cross(zDir, yDir);
 	return normalize(norm.x * xDir + norm.y * yDir + zDir);
 }
 
 // Convert 3D cubemap coordinates to 2D plus face index. v doesn't need to be normalized.
 Vec2 convertCubeUvs(Vec3 v, out F32 faceIndex)
 {
-	Vec3 absV = abs(v);
+	const Vec3 absV = abs(v);
 	F32 mag;
 	Vec2 uv;
 
@@ -228,7 +229,7 @@ Vec2 convertCubeUvs(Vec3 v, out F32 faceIndex)
 // Same as convertCubeUvs but it returns the faceIndex as unsigned I32.
 Vec2 convertCubeUvsu(Vec3 v, out U32 faceIndex)
 {
-	Vec3 absV = abs(v);
+	const Vec3 absV = abs(v);
 	F32 mag;
 	Vec2 uv;
 
@@ -256,14 +257,14 @@ Vec2 convertCubeUvsu(Vec3 v, out U32 faceIndex)
 
 Vec3 grayScale(Vec3 col)
 {
-	F32 grey = (col.r + col.g + col.b) * (1.0 / 3.0);
+	const F32 grey = (col.r + col.g + col.b) * (1.0 / 3.0);
 	return Vec3(grey);
 }
 
 Vec3 saturateColor(Vec3 col, F32 factor)
 {
 	const Vec3 LUM_COEFF = Vec3(0.2125, 0.7154, 0.0721);
-	Vec3 intensity = Vec3(dot(col, LUM_COEFF));
+	const Vec3 intensity = Vec3(dot(col, LUM_COEFF));
 	return mix(intensity, col, factor);
 }
 
@@ -324,7 +325,7 @@ Vec3 readErosion(sampler2D tex, Vec2 uv)
 Vec3 heatmap(F32 factor)
 {
 	F32 intPart;
-	F32 fractional = modf(factor * 4.0, intPart);
+	const F32 fractional = modf(factor * 4.0, intPart);
 
 	if(intPart < 1.0)
 	{
@@ -359,9 +360,9 @@ F32 areaElement(F32 x, F32 y)
 // http://www.rorydriscoll.com/2012/01/15/cubemap-texel-solid-angle/
 F32 cubeCoordSolidAngle(Vec2 norm, F32 cubeFaceSize)
 {
-	Vec2 invSize = Vec2(1.0 / cubeFaceSize);
-	Vec2 v0 = norm - invSize;
-	Vec2 v1 = norm + invSize;
+	const Vec2 invSize = Vec2(1.0 / cubeFaceSize);
+	const Vec2 v0 = norm - invSize;
+	const Vec2 v1 = norm + invSize;
 	return areaElement(v0.x, v0.y) - areaElement(v0.x, v1.y) - areaElement(v1.x, v0.y) + areaElement(v1.x, v1.y);
 }
 
@@ -370,10 +371,10 @@ F32 cubeCoordSolidAngle(Vec2 norm, F32 cubeFaceSize)
 // https://community.arm.com/graphics/b/blog/posts/reflections-based-on-local-cubemaps-in-unity
 F32 rayAabbIntersectionInside(Vec3 rayOrigin, Vec3 rayDir, Vec3 aabbMin, Vec3 aabbMax)
 {
-	Vec3 intersectMaxPointPlanes = (aabbMax - rayOrigin) / rayDir;
-	Vec3 intersectMinPointPlanes = (aabbMin - rayOrigin) / rayDir;
-	Vec3 largestParams = max(intersectMaxPointPlanes, intersectMinPointPlanes);
-	F32 distToIntersect = min(min(largestParams.x, largestParams.y), largestParams.z);
+	const Vec3 intersectMaxPointPlanes = (aabbMax - rayOrigin) / rayDir;
+	const Vec3 intersectMinPointPlanes = (aabbMin - rayOrigin) / rayDir;
+	const Vec3 largestParams = max(intersectMaxPointPlanes, intersectMinPointPlanes);
+	const F32 distToIntersect = min(min(largestParams.x, largestParams.y), largestParams.z);
 	return distToIntersect;
 }
 

+ 25 - 25
shaders/GBufferCommonFrag.glsl

@@ -45,25 +45,25 @@ layout(location = 3) out Vec2 out_gbuffer3;
 Vec3 readNormalFromTexture(sampler2D map, highp Vec2 texCoords)
 {
 	// First read the texture
-	Vec3 nAtTangentspace = normalize((texture(map, texCoords).rgb - 0.5) * 2.0);
+	const Vec3 nAtTangentspace = normalize((texture(map, texCoords).rgb - 0.5) * 2.0);
 
-	Vec3 n = normalize(in_normal);
-	Vec3 t = normalize(in_tangent.xyz);
+	const Vec3 n = normalize(in_normal);
+	const Vec3 t = normalize(in_tangent.xyz);
 #	if CALC_BITANGENT_IN_VERT
-	Vec3 b = normalize(in_bitangent.xyz);
+	const Vec3 b = normalize(in_bitangent.xyz);
 #	else
-	Vec3 b = cross(n, t) * in_tangent.w;
+	const Vec3 b = cross(n, t) * in_tangent.w;
 #	endif
 
-	Mat3 tbnMat = Mat3(t, b, n);
+	const Mat3 tbnMat = Mat3(t, b, n);
 
 	return tbnMat * nAtTangentspace;
 }
 
 // Using a 4-channel texture and a tolerance discard the fragment if the texture's alpha is less than the tolerance
-Vec3 readTextureRgbAlphaTesting(sampler2D map, in highp Vec2 texCoords, F32 tolerance)
+Vec3 readTextureRgbAlphaTesting(sampler2D map, highp Vec2 texCoords, F32 tolerance)
 {
-	Vec4 col = Vec4(texture(map, texCoords));
+	const Vec4 col = Vec4(texture(map, texCoords));
 	if(col.a < tolerance)
 	{
 		discard;
@@ -72,33 +72,33 @@ Vec3 readTextureRgbAlphaTesting(sampler2D map, in highp Vec2 texCoords, F32 tole
 	return col.rgb;
 }
 
-Vec2 computeTextureCoordParallax(in sampler2D heightMap, in Vec2 uv, in F32 heightMapScale)
+Vec2 computeTextureCoordParallax(sampler2D heightMap, Vec2 uv, F32 heightMapScale)
 {
 	const U32 MAX_SAMPLES = 25;
 	const U32 MIN_SAMPLES = 1;
 	const F32 MAX_EFFECTIVE_DISTANCE = 32.0;
 
 	// Get that because we are sampling inside a loop
-	Vec2 dPdx = dFdx(uv);
-	Vec2 dPdy = dFdy(uv);
+	const Vec2 dPdx = dFdx(uv);
+	const Vec2 dPdy = dFdy(uv);
 
-	Vec3 eyeTangentSpace = in_eyeTangentSpace;
-	Vec3 normTangentSpace = in_normalTangentSpace;
+	const Vec3 eyeTangentSpace = in_eyeTangentSpace;
+	const Vec3 normTangentSpace = in_normalTangentSpace;
 
 	F32 parallaxLimit = -length(eyeTangentSpace.xy) / eyeTangentSpace.z;
 	parallaxLimit *= heightMapScale;
 
-	Vec2 offsetDir = normalize(eyeTangentSpace.xy);
-	Vec2 maxOffset = offsetDir * parallaxLimit;
+	const Vec2 offsetDir = normalize(eyeTangentSpace.xy);
+	const Vec2 maxOffset = offsetDir * parallaxLimit;
 
-	Vec3 E = normalize(eyeTangentSpace);
+	const Vec3 E = normalize(eyeTangentSpace);
 
-	F32 factor0 = -dot(E, normTangentSpace);
-	F32 factor1 = in_distFromTheCamera / -MAX_EFFECTIVE_DISTANCE;
-	F32 factor = (1.0 - factor0) * (1.0 - factor1);
-	F32 sampleCountf = mix(F32(MIN_SAMPLES), F32(MAX_SAMPLES), factor);
+	const F32 factor0 = -dot(E, normTangentSpace);
+	const F32 factor1 = in_distFromTheCamera / -MAX_EFFECTIVE_DISTANCE;
+	const F32 factor = (1.0 - factor0) * (1.0 - factor1);
+	const F32 sampleCountf = mix(F32(MIN_SAMPLES), F32(MAX_SAMPLES), factor);
 
-	F32 stepSize = 1.0 / sampleCountf;
+	const F32 stepSize = 1.0 / sampleCountf;
 
 	F32 crntRayHeight = 1.0;
 	Vec2 crntOffset = Vec2(0.0);
@@ -109,16 +109,16 @@ Vec2 computeTextureCoordParallax(in sampler2D heightMap, in Vec2 uv, in F32 heig
 
 	U32 crntSample = 0;
 
-	U32 sampleCount = U32(sampleCountf);
+	const U32 sampleCount = U32(sampleCountf);
 	ANKI_LOOP while(crntSample < sampleCount)
 	{
 		crntSampledHeight = textureGrad(heightMap, uv + crntOffset, dPdx, dPdy).r;
 
 		if(crntSampledHeight > crntRayHeight)
 		{
-			F32 delta1 = crntSampledHeight - crntRayHeight;
-			F32 delta2 = (crntRayHeight + stepSize) - lastSampledHeight;
-			F32 ratio = delta1 / (delta1 + delta2);
+			const F32 delta1 = crntSampledHeight - crntRayHeight;
+			const F32 delta2 = (crntRayHeight + stepSize) - lastSampledHeight;
+			const F32 ratio = delta1 / (delta1 + delta2);
 
 			crntOffset = mix(crntOffset, lastOffset, ratio);
 

+ 11 - 11
shaders/GBufferCommonVert.glsl

@@ -91,14 +91,14 @@ void positionUvNormalTangent(Mat4 mvp, Mat3 rotationMat)
 #if PASS == PASS_GB
 void parallax(Mat4 modelViewMat)
 {
-	Vec3 n = in_normal;
-	Vec3 t = in_tangent.xyz;
-	Vec3 b = cross(n, t) * in_tangent.w;
+	const Vec3 n = in_normal;
+	const Vec3 t = in_tangent.xyz;
+	const Vec3 b = cross(n, t) * in_tangent.w;
 
-	Mat3 normalMat = Mat3(modelViewMat);
-	Mat3 invTbn = transpose(normalMat * Mat3(t, b, n));
+	const Mat3 normalMat = Mat3(modelViewMat);
+	const Mat3 invTbn = transpose(normalMat * Mat3(t, b, n));
 
-	Vec3 viewPos = (modelViewMat * Vec4(g_position, 1.0)).xyz;
+	const Vec3 viewPos = (modelViewMat * Vec4(g_position, 1.0)).xyz;
 	out_distFromTheCamera = viewPos.z;
 
 	out_eyeTangentSpace = invTbn * viewPos;
@@ -115,10 +115,10 @@ void skinning()
 	Vec3 tangent = Vec3(0.0);
 	for(U32 i = 0; i < 4; ++i)
 	{
-		U32 boneIdx = in_boneIndices[i];
+		const U32 boneIdx = in_boneIndices[i];
 		if(boneIdx < 0xFFFF)
 		{
-			F32 boneWeight = in_boneWeights[i];
+			const F32 boneWeight = in_boneWeights[i];
 
 			position += (u_boneTransforms[boneIdx] * Vec4(g_position * boneWeight, 1.0)).xyz;
 #	if PASS == PASS_GB
@@ -139,10 +139,10 @@ void skinning()
 #if VELOCITY && PASS == PASS_GB
 void velocity(Mat4 prevMvp)
 {
-	Vec4 v4 = prevMvp * Vec4(g_position, 1.0);
-	Vec2 prevNdc = v4.xy / v4.w;
+	const Vec4 v4 = prevMvp * Vec4(g_position, 1.0);
+	const Vec2 prevNdc = v4.xy / v4.w;
 
-	Vec2 crntNdc = gl_Position.xy / gl_Position.w;
+	const Vec2 crntNdc = gl_Position.xy / gl_Position.w;
 
 	// It's NDC_TO_UV(prevNdc) - NDC_TO_UV(crntNdc) or:
 	out_velocity = (prevNdc - crntNdc) * 0.5;

+ 11 - 11
shaders/GBufferGeneric.glslp

@@ -68,41 +68,41 @@ void main()
 {
 #if PASS == PASS_GB
 #	if heightTex_DEFINED
-	Vec2 uv = computeTextureCoordParallax(heightTex, in_uv, heightMapScale);
+	const Vec2 uv = computeTextureCoordParallax(heightTex, in_uv, heightMapScale);
 #	else
-	Vec2 uv = in_uv;
+	const Vec2 uv = in_uv;
 #	endif
 
 #	if diffTex_DEFINED
-	Vec3 diffColor = texture(diffTex, uv).rgb;
+	const Vec3 diffColor = texture(diffTex, uv).rgb;
 #	endif
 
 #	if specTex_DEFINED
-	Vec3 specColor = texture(specTex, uv).rgb;
+	const Vec3 specColor = texture(specTex, uv).rgb;
 #	endif
 
 #	if roughnessTex_DEFINED
-	F32 roughness = texture(roughnessTex, uv).r;
+	const F32 roughness = texture(roughnessTex, uv).r;
 #	endif
 
 #	if metalTex_DEFINED
-	F32 metallic = texture(metalTex, uv).r;
+	const F32 metallic = texture(metalTex, uv).r;
 #	endif
 
 #	if normalTex_DEFINED
-	Vec3 normal = readNormalFromTexture(normalTex, uv);
+	const Vec3 normal = readNormalFromTexture(normalTex, uv);
 #	else
-	Vec3 normal = normalize(in_normal);
+	const Vec3 normal = normalize(in_normal);
 #	endif
 
 #	if emissiveTex_DEFINED
-	Vec3 emission = texture(emissiveTex, uv).rgb;
+	const Vec3 emission = texture(emissiveTex, uv).rgb;
 #	endif
 
 #	if VELOCITY
-	Vec2 velocity = in_velocity;
+	const Vec2 velocity = in_velocity;
 #	else
-	Vec2 velocity = Vec2(-1.0);
+	const Vec2 velocity = Vec2(-1.0);
 #	endif
 
 	writeRts(diffColor, normal, specColor, roughness, subsurface, emission, metallic, velocity);

+ 13 - 13
shaders/GBufferPost.glslp

@@ -21,7 +21,7 @@ out gl_PerVertex
 void main()
 {
 	out_uv = Vec2(gl_VertexID & 1, gl_VertexID >> 1) * 2.0;
-	Vec2 pos = out_uv * 2.0 - 1.0;
+	const Vec2 pos = out_uv * 2.0 - 1.0;
 	gl_Position = Vec4(pos, 0.0, 1.0);
 
 	out_clusterIJ = Vec2(CLUSTER_COUNT_X, CLUSTER_COUNT_Y) * out_uv;
@@ -61,16 +61,16 @@ void main()
 	Vec4 outSpecular = Vec4(0.0, 0.0, 0.0, 1.0);
 
 	// Get worldPos
-	F32 depth = textureLod(u_msDepthRt, in_uv, 0.0).r;
-	Vec2 ndc = UV_TO_NDC(in_uv);
-	Vec4 worldPos4 = u_invViewProjMat * Vec4(ndc, depth, 1.0);
-	Vec3 worldPos = worldPos4.xyz / worldPos4.w;
+	const F32 depth = textureLod(u_msDepthRt, in_uv, 0.0).r;
+	const Vec2 ndc = UV_TO_NDC(in_uv);
+	const Vec4 worldPos4 = u_invViewProjMat * Vec4(ndc, depth, 1.0);
+	const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
 
 	// Get first decal index
 	U32 idxOffset;
 	{
-		U32 k = computeClusterK(u_clustererMagic, worldPos);
-		U32 clusterIdx =
+		const U32 k = computeClusterK(u_clustererMagic, worldPos);
+		const U32 clusterIdx =
 			k * (CLUSTER_COUNT_X * CLUSTER_COUNT_Y) + U32(in_clusterIJ.y) * CLUSTER_COUNT_X + U32(in_clusterIJ.x);
 
 		idxOffset = u_clusters[clusterIdx];
@@ -86,10 +86,10 @@ void main()
 
 	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
 	{
-		Decal decal = u_decals[idx];
+		const Decal decal = u_decals[idx];
 
 		// Project pos to decal space
-		Vec4 texCoords4 = decal.m_texProjectionMat * Vec4(worldPos, 1.0);
+		const Vec4 texCoords4 = decal.m_texProjectionMat * Vec4(worldPos, 1.0);
 		if(texCoords4.w <= 0.7)
 		{
 			// Behind the decal, skip
@@ -102,12 +102,12 @@ void main()
 		texCoords2 = saturate(texCoords2);
 
 		// Read diffuse
-		Vec2 diffUv = mad(texCoords2, decal.m_diffUv.zw, decal.m_diffUv.xy);
-		Vec4 decalDiff = texture(u_diffDecalTex, diffUv);
+		const Vec2 diffUv = mad(texCoords2, decal.m_diffUv.zw, decal.m_diffUv.xy);
+		const Vec4 decalDiff = texture(u_diffDecalTex, diffUv);
 
 		// Read roughness
-		Vec2 specUv = mad(texCoords2, decal.m_normRoughnessUv.zw, decal.m_normRoughnessUv.xy);
-		Vec3 spec = texture(u_specularRoughnessDecalTex, specUv).rgb;
+		const Vec2 specUv = mad(texCoords2, decal.m_normRoughnessUv.zw, decal.m_normRoughnessUv.xy);
+		const Vec3 spec = texture(u_specularRoughnessDecalTex, specUv).rgb;
 
 		// Update diffuse
 		F32 f = decalDiff.a * decal.m_blendFactors[0];

+ 3 - 3
shaders/GaussianBlur.glsl

@@ -59,9 +59,9 @@ void main()
 		return;
 	}
 
-	Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(TEXTURE_SIZE);
+	const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(TEXTURE_SIZE);
 #else
-	Vec2 uv = in_uv;
+	const Vec2 uv = in_uv;
 #endif
 
 	const Vec2 TEXEL_SIZE = 1.0 / Vec2(TEXTURE_SIZE);
@@ -82,7 +82,7 @@ void main()
 
 	ANKI_UNROLL for(U32 i = 0u; i < STEP_COUNT; ++i)
 	{
-		COL_TYPE col =
+		const COL_TYPE col =
 			textureLod(u_tex, uv + uvOffset, 0.0).TEX_FETCH + textureLod(u_tex, uv - uvOffset, 0.0).TEX_FETCH;
 		color += WEIGHTS[i + 1u] * col;
 

+ 7 - 7
shaders/Irradiance.glslp

@@ -25,11 +25,11 @@ ANKI_PUSH_CONSTANTS(UVec4, u_faceIdxPad3);
 // Integrate the environment map to compute the irradiance for a single fragment
 void main()
 {
-	U32 face = u_faceIdxPad3.x;
+	const U32 face = u_faceIdxPad3.x;
 	const F32 texArrIdx = 0.0; // The C++ code gives the layer idx using a tex view
 
 	// Get the r coordinate of the current face and fragment
-	Vec3 ri = getCubemapDirection(UV_TO_NDC(in_uv), face);
+	const Vec3 ri = getCubemapDirection(UV_TO_NDC(in_uv), face);
 
 	Vec3 outCol = Vec3(0.0);
 
@@ -40,15 +40,15 @@ void main()
 		{
 			ANKI_LOOP for(U32 j = 0u; j < ENV_TEX_TILE_SIZE; ++j)
 			{
-				Vec2 uv = Vec2(j, i) / F32(ENV_TEX_TILE_SIZE);
-				Vec2 ndc = UV_TO_NDC(uv);
+				const Vec2 uv = Vec2(j, i) / F32(ENV_TEX_TILE_SIZE);
+				const Vec2 ndc = UV_TO_NDC(uv);
 
-				Vec3 r = getCubemapDirection(ndc, f);
-				F32 lambert = dot(r, ri);
+				const Vec3 r = getCubemapDirection(ndc, f);
+				const F32 lambert = dot(r, ri);
 
 				if(lambert > 0.0)
 				{
-					Vec3 col = textureLod(u_envTex, Vec4(r, texArrIdx), ENV_TEX_MIP).rgb;
+					const Vec3 col = textureLod(u_envTex, Vec4(r, texArrIdx), ENV_TEX_MIP).rgb;
 					outCol += col * lambert * cubeCoordSolidAngle(ndc, F32(ENV_TEX_TILE_SIZE));
 				}
 			}

+ 4 - 4
shaders/LensFlareSprite.glslp

@@ -25,14 +25,14 @@ out gl_PerVertex
 
 void main()
 {
-	Vec2 position = UV_TO_NDC(Vec2(gl_VertexID & 1, gl_VertexID >> 1));
+	const Vec2 position = UV_TO_NDC(Vec2(gl_VertexID & 1, gl_VertexID >> 1));
 
-	LensFlareSprite sprite = u_sprites[gl_InstanceID];
+	const LensFlareSprite sprite = u_sprites[gl_InstanceID];
 
 	// Write tex coords of the 2D array texture
 	out_uv = Vec3((position * 0.5) + 0.5, sprite.m_depthPad3.x);
 
-	Vec4 posScale = sprite.m_posScale;
+	const Vec4 posScale = sprite.m_posScale;
 	gl_Position = Vec4(position * posScale.zw + posScale.xy, 0.0, 1.0);
 
 	out_color = sprite.m_color;
@@ -51,7 +51,7 @@ layout(location = 0) out Vec4 out_color;
 
 void main()
 {
-	Vec4 col = texture(u_tex, in_uv);
+	const Vec4 col = texture(u_tex, in_uv);
 	out_color = col * in_color;
 }
 #pragma anki end

+ 10 - 9
shaders/LensFlareUpdateIndirectInfo.glslp

@@ -45,18 +45,19 @@ void main()
 	barrier();
 
 	// Project the flare
-	U32 flareIdx = gl_WorkGroupID.x;
-	Vec4 posClip = u_mvp * u_flarePositions[flareIdx];
-	Vec3 posNdc = posClip.xyz / posClip.w;
-	F32 depth = posNdc.z;
+	const U32 flareIdx = gl_WorkGroupID.x;
+	const Vec4 posClip = u_mvp * u_flarePositions[flareIdx];
+	const Vec3 posNdc = posClip.xyz / posClip.w;
+	const F32 depth = posNdc.z;
 
 	// Compute the UVs to sample the depth map
-	Vec2 displacement = Vec2(gl_LocalInvocationID.xy) - Vec2(WORKGROUP_SIZE / 2u); // [-WORKGROUP_SIZE, WORKGROUP_SIZE]
+	// Belongs to [-WORKGROUP_SIZE, WORKGROUP_SIZE]
+	const Vec2 displacement = Vec2(gl_LocalInvocationID.xy) - Vec2(WORKGROUP_SIZE / 2u);
 	const Vec2 TEXEL_SIZE = 1.0 / IN_DEPTH_MAP_SIZE;
-	Vec2 uv = NDC_TO_UV(posNdc.xy) + displacement * TEXEL_SIZE;
+	const Vec2 uv = NDC_TO_UV(posNdc.xy) + displacement * TEXEL_SIZE;
 
 	// Sample and store depth
-	F32 refDepth = textureLod(u_depthMap, uv, 0.0).r;
+	const F32 refDepth = textureLod(u_depthMap, uv, 0.0).r;
 	atomicMax(s_maxDepth, U32(refDepth * F32(MAX_U32)));
 
 	// Sync
@@ -65,8 +66,8 @@ void main()
 
 	if(gl_LocalInvocationIndex == 0)
 	{
-		F32 refDepth = F32(s_maxDepth) / F32(MAX_U32);
-		u_indirectInfo[flareIdx].count = (depth > refDepth) ? 0u : 4u;
+		const F32 refDepth2 = F32(s_maxDepth) / F32(MAX_U32);
+		u_indirectInfo[flareIdx].count = (depth > refDepth2) ? 0u : 4u;
 
 		u_indirectInfo[flareIdx].instanceCount = 1u;
 		u_indirectInfo[flareIdx].first = 0u;

+ 52 - 52
shaders/LightFunctions.glsl

@@ -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);

+ 44 - 60
shaders/LightShading.glslp

@@ -55,59 +55,13 @@ layout(location = 1) in Vec2 in_clusterIJ;
 
 layout(location = 0) out Vec3 out_color;
 
-// Note: All calculations in world space
-void readReflectionsAndIrradianceFromProbes(U32 idxOffset,
-	Vec3 worldPos,
-	Vec3 normal,
-	Vec3 viewDir,
-	F32 roughness,
-	out Vec3 specIndirect,
-	out Vec3 diffIndirect)
-{
-	specIndirect = Vec3(0.0);
-	diffIndirect = Vec3(0.0);
-
-	Vec3 reflDir = reflect(-viewDir, normal);
-	F32 reflLod = F32(IR_MIPMAP_COUNT - 1u) * roughness;
-	F32 totalBlendWeight = EPSILON;
-
-	// Check proxy
-	U32 idx;
-	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
-	{
-		ReflectionProbe probe = u_reflectionProbes[idx];
-		Vec3 aabbMin = probe.m_aabbMin;
-		Vec3 aabbMax = probe.m_aabbMax;
-		Vec3 probeOrigin = probe.m_position;
-		F32 cubemapIndex = probe.m_cubemapIndex;
-
-		// Compute blend weight
-		F32 blendWeight = computeProbeBlendWeight(worldPos, aabbMin, aabbMax, 0.2);
-		totalBlendWeight += blendWeight;
-
-		// Sample reflections
-		Vec3 cubeUv = intersectProbe(worldPos, reflDir, aabbMin, aabbMax, probeOrigin);
-		Vec3 c = textureLod(u_reflectionsTex, Vec4(cubeUv, cubemapIndex), reflLod).rgb;
-		specIndirect += c * blendWeight;
-
-		// Sample irradiance
-		cubeUv = intersectProbe(worldPos, normal, aabbMin, aabbMax, probeOrigin);
-		c = textureLod(u_irradianceTex, Vec4(cubeUv, cubemapIndex), 0.0).rgb;
-		diffIndirect += c * blendWeight;
-	}
-
-	// Normalize the colors
-	specIndirect /= totalBlendWeight;
-	diffIndirect /= totalBlendWeight;
-}
-
 // Common code for lighting
 #define LIGHTING_COMMON_BRDF() \
-	Vec3 frag2Light = light.m_position - worldPos; \
-	Vec3 l = normalize(frag2Light); \
-	Vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l); \
-	Vec3 diffC = diffuseLambert(gbuffer.m_diffuse); \
-	F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light); \
+	const Vec3 frag2Light = light.m_position - worldPos; \
+	const Vec3 l = normalize(frag2Light); \
+	const Vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l); \
+	const Vec3 diffC = diffuseLambert(gbuffer.m_diffuse); \
+	const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light); \
 	F32 lambert = max(0.0, dot(gbuffer.m_normal, l));
 
 void main()
@@ -210,22 +164,52 @@ void main()
 	// Refl & indirect
 	{
 		// Do the probe read
-		Vec3 envColor;
-		Vec3 indirectColor;
-		readReflectionsAndIrradianceFromProbes(
-			idxOffset, worldPos, gbuffer.m_normal, viewDir, gbuffer.m_roughness, envColor, indirectColor);
+		Vec3 specIndirect = Vec3(0.0);
+		Vec3 diffIndirect = Vec3(0.0);
+
+		const Vec3 reflDir = reflect(-viewDir, gbuffer.m_normal);
+		const F32 reflLod = F32(IR_MIPMAP_COUNT - 1u) * gbuffer.m_roughness;
+		F32 totalBlendWeight = EPSILON;
+
+		// Loop probes
+		ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
+		{
+			const ReflectionProbe probe = u_reflectionProbes[idx];
+			const Vec3 aabbMin = probe.m_aabbMin;
+			const Vec3 aabbMax = probe.m_aabbMax;
+			const Vec3 probeOrigin = probe.m_position;
+			const F32 cubemapIndex = probe.m_cubemapIndex;
+
+			// Compute blend weight
+			const F32 blendWeight = computeProbeBlendWeight(worldPos, aabbMin, aabbMax, 0.2);
+			totalBlendWeight += blendWeight;
+
+			// Sample reflections
+			Vec3 cubeUv = intersectProbe(worldPos, reflDir, aabbMin, aabbMax, probeOrigin);
+			Vec3 c = textureLod(u_reflectionsTex, Vec4(cubeUv, cubemapIndex), reflLod).rgb;
+			specIndirect += c * blendWeight;
+
+			// Sample irradiance
+			cubeUv = intersectProbe(worldPos, gbuffer.m_normal, aabbMin, aabbMax, probeOrigin);
+			c = textureLod(u_irradianceTex, Vec4(cubeUv, cubemapIndex), 0.0).rgb;
+			diffIndirect += c * blendWeight;
+		}
+
+		// Normalize the colors
+		specIndirect /= totalBlendWeight;
+		diffIndirect /= totalBlendWeight;
 
 		// Read the SSL result
-		Vec4 ssr = textureLod(u_ssrRt, in_uv, 0.0);
+		const Vec4 ssr = textureLod(u_ssrRt, in_uv, 0.0);
 
 		// Combine the SSR and probe reflections and write the result
-		Vec3 finalRefl = envColor * ssr.a + ssr.rgb;
+		const Vec3 finalRefl = specIndirect * ssr.a + ssr.rgb;
 
 		// Compute env BRDF
-		F32 NoV = max(EPSILON, dot(gbuffer.m_normal, viewDir));
-		Vec3 env = envBRDF(gbuffer.m_specular, gbuffer.m_roughness, u_integrationLut, NoV);
+		const F32 NoV = max(EPSILON, dot(gbuffer.m_normal, viewDir));
+		const Vec3 env = envBRDF(gbuffer.m_specular, gbuffer.m_roughness, u_integrationLut, NoV);
 
-		out_color += indirectColor * gbuffer.m_diffuse + finalRefl * env;
+		out_color += diffIndirect * gbuffer.m_diffuse + finalRefl * env;
 	}
 }
 #pragma anki end

+ 7 - 7
shaders/LightShadingApplyFog.glslp

@@ -31,17 +31,17 @@ ANKI_PUSH_CONSTANTS(PushConsts, u_regs);
 
 void main()
 {
-	F32 depth = textureLod(u_depthRt, in_uv, 0.0).r;
+	const F32 depth = textureLod(u_depthRt, in_uv, 0.0).r;
 
 	// Get world position
-	Vec4 worldPos4 = u_invViewProjMat * Vec4(UV_TO_NDC(in_uv), depth, 1.0);
-	Vec3 worldPos = worldPos4.xyz / worldPos4.w;
+	const Vec4 worldPos4 = u_invViewProjMat * Vec4(UV_TO_NDC(in_uv), depth, 1.0);
+	const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
 
 	// Read the volume
-	Vec3 uv3d = computeClustererVolumeTextureUvs(u_clustererMagic, in_uv, worldPos, FOG_LAST_CLASTER + 1u);
-	Vec4 fogVals = textureLod(u_fogVolume, uv3d, 0.0);
-	Vec3 inScattering = fogVals.rgb;
-	F32 transmittance = fogVals.a;
+	const Vec3 uv3d = computeClustererVolumeTextureUvs(u_clustererMagic, in_uv, worldPos, FOG_LAST_CLASTER + 1u);
+	const Vec4 fogVals = textureLod(u_fogVolume, uv3d, 0.0);
+	const Vec3 inScattering = fogVals.rgb;
+	const F32 transmittance = fogVals.a;
 
 	// Apply the fog
 	out_color = Vec4(inScattering, transmittance);

+ 4 - 4
shaders/LumaAwareBlur.glslp

@@ -37,9 +37,9 @@ layout(location = 0) in Vec2 in_uv;
 
 F32 computeLumaWeight(F32 refLuma, COL_TYPE col)
 {
-	F32 l = computeLuminance(col);
-	F32 diff = abs(refLuma - l);
-	F32 weight = 1.0 / (EPSILON + diff);
+	const F32 l = computeLuminance(col);
+	const F32 diff = abs(refLuma - l);
+	const F32 weight = 1.0 / (EPSILON + diff);
 	return weight;
 }
 
@@ -56,7 +56,7 @@ void main()
 
 	COL_TYPE col = texture(u_tex, in_uv).TEX_FETCH;
 	out_color = col;
-	F32 refLuma = computeLuminance(col);
+	const F32 refLuma = computeLuminance(col);
 	F32 weight = 1.0;
 	Vec2 texCoordOffset = 1.5 * TEXEL_SIZE;
 

+ 9 - 9
shaders/MotionBlur.glsl

@@ -28,27 +28,27 @@ Vec3 motionBlur(sampler2D velocityTex,
 	ANKI_BRANCH if(velocity.x == -1.0)
 	{
 #if TAA_FIX
-		Vec2 a = textureLodOffset(velocityTex, nowUv, 0.0, ivec2(-2, 2)).rg;
-		Vec2 b = textureLodOffset(velocityTex, nowUv, 0.0, ivec2(2, 2)).rg;
-		Vec2 c = textureLodOffset(velocityTex, nowUv, 0.0, ivec2(0, -2)).rg;
+		const Vec2 a = textureLodOffset(velocityTex, nowUv, 0.0, ivec2(-2, 2)).rg;
+		const Vec2 b = textureLodOffset(velocityTex, nowUv, 0.0, ivec2(2, 2)).rg;
+		const Vec2 c = textureLodOffset(velocityTex, nowUv, 0.0, ivec2(0, -2)).rg;
 
 		velocity = max(max(a, b), c);
 
 		ANKI_BRANCH if(velocity.x == -1.0)
 #endif
 		{
-			F32 depth = textureLod(depthTex, nowUv, 0.0).r;
+			const F32 depth = textureLod(depthTex, nowUv, 0.0).r;
 
-			Vec4 v4 = prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(nowUv), depth, 1.0);
+			const Vec4 v4 = prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(nowUv), depth, 1.0);
 			velocity = NDC_TO_UV(v4.xy / v4.w) - nowUv;
 		}
 	}
 
 	// March direction
-	Vec2 slopes = abs(velocity);
+	const Vec2 slopes = abs(velocity);
 
 	// Compute the sample count
-	Vec2 sampleCount2D = slopes * Vec2(FB_SIZE);
+	const Vec2 sampleCount2D = slopes * Vec2(FB_SIZE);
 	F32 sampleCountf = max(sampleCount2D.x, sampleCount2D.y);
 	sampleCountf = clamp(sampleCountf, 1.0, F32(maxSamples));
 	sampleCountf = round(sampleCountf);
@@ -57,8 +57,8 @@ Vec3 motionBlur(sampler2D velocityTex,
 	Vec3 outColor = Vec3(0.0);
 	ANKI_LOOP for(F32 s = 0.0; s < sampleCountf; s += 1.0)
 	{
-		F32 f = s / sampleCountf;
-		Vec2 sampleUv = nowUv + velocity * f;
+		const F32 f = s / sampleCountf;
+		const Vec2 sampleUv = nowUv + velocity * f;
 
 		outColor += textureLod(toBlurTex, sampleUv, 0.0).rgb;
 	}

+ 33 - 33
shaders/Pack.glsl

@@ -9,19 +9,19 @@
 
 /// Pack 3D normal to 2D vector
 /// See the clean code in comments in revision < r467
-Vec2 packNormal(in Vec3 normal)
+Vec2 packNormal(const Vec3 normal)
 {
 	const F32 SCALE = 1.7777;
-	F32 scalar1 = (normal.z + 1.0) * (SCALE * 2.0);
+	const F32 scalar1 = (normal.z + 1.0) * (SCALE * 2.0);
 	return normal.xy / scalar1 + 0.5;
 }
 
 /// Reverse the packNormal
-Vec3 unpackNormal(in Vec2 enc)
+Vec3 unpackNormal(const Vec2 enc)
 {
 	const F32 SCALE = 1.7777;
-	Vec2 nn = enc * (2.0 * SCALE) - SCALE;
-	F32 g = 2.0 / (dot(nn.xy, nn.xy) + 1.0);
+	const Vec2 nn = enc * (2.0 * SCALE) - SCALE;
+	const F32 g = 2.0 / (dot(nn.xy, nn.xy) + 1.0);
 	Vec3 normal;
 	normal.xy = g * nn.xy;
 	normal.z = g - 1.0;
@@ -34,7 +34,7 @@ Vec3 signedOctEncode(Vec3 n)
 {
 	Vec3 outn;
 
-	Vec3 nabs = abs(n);
+	const Vec3 nabs = abs(n);
 	n /= nabs.x + nabs.y + nabs.z;
 
 	outn.y = n.y * 0.5 + 0.5;
@@ -46,7 +46,7 @@ Vec3 signedOctEncode(Vec3 n)
 }
 
 // See http://johnwhite3d.blogspot.no/2017/10/signed-octahedron-normal-encoding.html
-Vec3 signedOctDecode(Vec3 n)
+Vec3 signedOctDecode(const Vec3 n)
 {
 	Vec3 outn;
 
@@ -60,64 +60,64 @@ Vec3 signedOctDecode(Vec3 n)
 }
 
 // Vectorized version. See clean one at <= r1048
-U32 newPackUnorm4x8(Vec4 v)
+U32 newPackUnorm4x8(const Vec4 v)
 {
-	Vec4 a = clamp(v, 0.0, 1.0) * 255.0;
+	Vec4 a = saturate(v) * 255.0;
 	UVec4 b = UVec4(a) << UVec4(0U, 8U, 16U, 24U);
 	UVec2 c = b.xy | b.zw;
 	return c.x | c.y;
 }
 
 // Vectorized version. See clean one at <= r1048
-Vec4 newUnpackUnorm4x8(highp U32 u)
+Vec4 newUnpackUnorm4x8(const U32 u)
 {
-	UVec4 a = UVec4(u) >> UVec4(0U, 8U, 16U, 24U);
-	UVec4 b = a & UVec4(0xFFU);
-	Vec4 c = Vec4(b);
+	const UVec4 a = UVec4(u) >> UVec4(0U, 8U, 16U, 24U);
+	const UVec4 b = a & UVec4(0xFFU);
+	const Vec4 c = Vec4(b);
 	return c * (1.0 / 255.0);
 }
 
 // Convert from RGB to YCbCr.
 // The RGB should be in [0, 1] and the output YCbCr will be in [0, 1] as well.
-Vec3 rgbToYCbCr(in Vec3 rgb)
+Vec3 rgbToYCbCr(const Vec3 rgb)
 {
-	F32 y = dot(rgb, Vec3(0.299, 0.587, 0.114));
-	F32 cb = 0.5 + dot(rgb, Vec3(-0.168736, -0.331264, 0.5));
-	F32 cr = 0.5 + dot(rgb, Vec3(0.5, -0.418688, -0.081312));
+	const F32 y = dot(rgb, Vec3(0.299, 0.587, 0.114));
+	const F32 cb = 0.5 + dot(rgb, Vec3(-0.168736, -0.331264, 0.5));
+	const F32 cr = 0.5 + dot(rgb, Vec3(0.5, -0.418688, -0.081312));
 	return Vec3(y, cb, cr);
 }
 
 // Convert the output of rgbToYCbCr back to RGB.
-Vec3 yCbCrToRgb(in Vec3 ycbcr)
+Vec3 yCbCrToRgb(const Vec3 ycbcr)
 {
-	F32 cb = ycbcr.y - 0.5;
-	F32 cr = ycbcr.z - 0.5;
-	F32 y = ycbcr.x;
-	F32 r = 1.402 * cr;
-	F32 g = -0.344 * cb - 0.714 * cr;
-	F32 b = 1.772 * cb;
+	const F32 cb = ycbcr.y - 0.5;
+	const F32 cr = ycbcr.z - 0.5;
+	const F32 y = ycbcr.x;
+	const F32 r = 1.402 * cr;
+	const F32 g = -0.344 * cb - 0.714 * cr;
+	const F32 b = 1.772 * cb;
 	return Vec3(r, g, b) + y;
 }
 
 // Pack a Vec2 to a single F32.
 // comp should be in [0, 1] and the output will be in [0, 1].
-F32 packUnorm2ToUnorm1(in Vec2 comp)
+F32 packUnorm2ToUnorm1(const Vec2 comp)
 {
 	return dot(round(comp * 15.0), Vec2(1.0 / (255.0 / 16.0), 1.0 / 255.0));
 }
 
 // Unpack a single F32 to Vec2. Does the oposite of packUnorm2ToUnorm1.
-Vec2 unpackUnorm1ToUnorm2(in F32 c)
+Vec2 unpackUnorm1ToUnorm2(F32 c)
 {
 #if 1
-	F32 temp = c * (255.0 / 16.0);
-	F32 a = floor(temp);
-	F32 b = temp - a; // b = fract(temp)
+	const F32 temp = c * (255.0 / 16.0);
+	const F32 a = floor(temp);
+	const F32 b = temp - a; // b = fract(temp)
 	return Vec2(a, b) * Vec2(1.0 / 15.0, 16.0 / 15.0);
 #else
-	U32 temp = U32(c * 255.0);
-	U32 a = temp >> 4;
-	U32 b = temp & 0xF;
+	const U32 temp = U32(c * 255.0);
+	const U32 a = temp >> 4;
+	const U32 b = temp & 0xF;
 	return Vec2(a, b) / 15.0;
 #endif
 }
@@ -144,7 +144,7 @@ void writeGBuffer(GbufferInfo g, out Vec4 rt0, out Vec4 rt1, out Vec4 rt2, out V
 	rt0 = Vec4(g.m_diffuse, g.m_subsurface);
 	rt1 = Vec4(g.m_roughness, g.m_metallic, g.m_specular.x, 0.0);
 
-	Vec3 encNorm = signedOctEncode(g.m_normal);
+	const Vec3 encNorm = signedOctEncode(g.m_normal);
 	rt2 = Vec4(encNorm.xy, g.m_emission / MAX_EMISSION, encNorm.z);
 
 	rt3 = g.m_velocity;

+ 1 - 1
shaders/QuadVert.glsl

@@ -15,7 +15,7 @@ layout(location = 0) out Vec2 out_uv;
 void main()
 {
 	out_uv = Vec2(gl_VertexID & 1, gl_VertexID >> 1) * 2.0;
-	Vec2 pos = out_uv * 2.0 - 1.0;
+	const Vec2 pos = out_uv * 2.0 - 1.0;
 
 	gl_Position = Vec4(pos, 0.0, 1.0);
 }

+ 4 - 4
shaders/SceneDebug.glslp

@@ -62,10 +62,10 @@ void main()
 {
 	// Check if we should skip the frag
 #if DITHERED_DEPTH_TEST == 1
-	Vec2 uv = gl_FragCoord.xy / Vec2(textureSize(u_depthRt, 0));
-	F32 depthRef = textureLod(u_depthRt, uv, 0.0).r;
-	Bool depthTestFailed = gl_FragCoord.z >= depthRef;
-	IVec2 fragCoordi = IVec2(gl_FragCoord.xy);
+	const Vec2 uv = gl_FragCoord.xy / Vec2(textureSize(u_depthRt, 0));
+	const F32 depthRef = textureLod(u_depthRt, uv, 0.0).r;
+	const Bool depthTestFailed = gl_FragCoord.z >= depthRef;
+	const IVec2 fragCoordi = IVec2(gl_FragCoord.xy);
 	if(depthTestFailed && ((fragCoordi.x + fragCoordi.y) % 8) != 0)
 	{
 		discard;

+ 9 - 9
shaders/ScreenSpaceLensFlare.glslp

@@ -30,10 +30,10 @@ layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_lensDirtTex;
 
 layout(location = 0) out Vec3 out_color;
 
-Vec3 textureDistorted(in sampler2D tex,
-	in Vec2 texcoord,
-	in Vec2 direction, // direction of distortion
-	in Vec3 distortion) // per-channel distortion factor
+Vec3 textureDistorted(sampler2D tex,
+	Vec2 texcoord,
+	Vec2 direction, // direction of distortion
+	Vec3 distortion) // per-channel distortion factor
 {
 #if ENABLE_CHROMATIC_DISTORTION
 	return Vec3(texture(tex, texcoord + direction * distortion.r).r,
@@ -46,9 +46,9 @@ Vec3 textureDistorted(in sampler2D tex,
 
 void main()
 {
-	Vec2 texcoord = Vec2(1.0) - in_texCoord;
+	const Vec2 texcoord = Vec2(1.0) - in_texCoord;
 
-	Vec2 ghostVec = (Vec2(0.5) - texcoord) * GHOST_DISPERSAL;
+	const Vec2 ghostVec = (Vec2(0.5) - texcoord) * GHOST_DISPERSAL;
 
 	const Vec2 texelSize = 1.0 / Vec2(INPUT_TEX_SIZE);
 
@@ -56,14 +56,14 @@ void main()
 
 	const F32 lenOfHalf = length(Vec2(0.5));
 
-	Vec2 direction = normalize(ghostVec);
+	const Vec2 direction = normalize(ghostVec);
 
 	Vec3 result = Vec3(0.0);
 
 	// sample ghosts:
 	for(I32 i = 0; i < MAX_GHOSTS; ++i)
 	{
-		Vec2 offset = fract(texcoord + ghostVec * F32(i));
+		const Vec2 offset = fract(texcoord + ghostVec * F32(i));
 
 		F32 weight = length(Vec2(0.5) - offset) / lenOfHalf;
 		weight = pow(1.0 - weight, 10.0);
@@ -73,7 +73,7 @@ void main()
 
 // sample halo
 #if ENABLE_HALO
-	Vec2 haloVec = normalize(ghostVec) * HALO_WIDTH;
+	const Vec2 haloVec = normalize(ghostVec) * HALO_WIDTH;
 	F32 weight = length(Vec2(0.5) - fract(texcoord + haloVec)) / lenOfHalf;
 	weight = pow(1.0 - weight, 20.0);
 	result += textureDistorted(u_rt, texcoord + haloVec, direction, distortion) * (weight * HALO_OPACITY);

+ 37 - 37
shaders/Ssao.glsl

@@ -63,7 +63,7 @@ shared Vec3 s_scratch[WORKGROUP_SIZE.y][WORKGROUP_SIZE.x];
 
 #if USE_NORMAL
 // Get normal
-Vec3 readNormal(in Vec2 uv)
+Vec3 readNormal(Vec2 uv)
 {
 	Vec3 normal = readNormalFromGBuffer(u_msRt, uv);
 	normal = u_viewRotMat * normal;
@@ -75,7 +75,7 @@ Vec3 readNormal(in Vec2 uv)
 Vec3 readRandom(Vec2 uv, F32 layer)
 {
 	const Vec2 tmp = Vec2(F32(FB_SIZE.x) / F32(NOISE_MAP_SIZE), F32(FB_SIZE.y) / F32(NOISE_MAP_SIZE));
-	Vec3 r = texture(u_noiseMap, Vec3(tmp * uv, layer)).rgb;
+	const Vec3 r = texture(u_noiseMap, Vec3(tmp * uv, layer)).rgb;
 	return r;
 }
 
@@ -86,15 +86,15 @@ Vec4 project(Vec4 point)
 
 Vec3 unproject(Vec2 ndc, F32 depth)
 {
-	F32 z = u_unprojectionParams.z / (u_unprojectionParams.w + depth);
-	Vec2 xy = ndc * u_unprojectionParams.xy * z;
+	const F32 z = u_unprojectionParams.z / (u_unprojectionParams.w + depth);
+	const Vec2 xy = ndc * u_unprojectionParams.xy * z;
 	return Vec3(xy, z);
 }
 
 F32 smallerDelta(F32 left, F32 mid, F32 right)
 {
-	F32 a = mid - left;
-	F32 b = right - mid;
+	const F32 a = mid - left;
+	const F32 b = right - mid;
 
 	return (abs(a) < abs(b)) ? a : b;
 }
@@ -103,23 +103,23 @@ F32 smallerDelta(F32 left, F32 mid, F32 right)
 Vec3 computeNormal(Vec2 uv, Vec3 origin, F32 depth)
 {
 #if USE_NORMAL
-	Vec3 normal = readNormal(uv);
+	const Vec3 normal = readNormal(uv);
 #elif !COMPLEX_NORMALS
-	Vec3 normal = normalize(cross(dFdx(origin), dFdy(origin)));
+	const Vec3 normal = normalize(cross(dFdx(origin), dFdy(origin)));
 #else
-	F32 depthLeft = textureLodOffset(u_depthRt, uv, 0.0, ivec2(-2, 0)).r;
-	F32 depthRight = textureLodOffset(u_depthRt, uv, 0.0, ivec2(2, 0)).r;
-	F32 depthTop = textureLodOffset(u_depthRt, uv, 0.0, ivec2(0, 2)).r;
-	F32 depthBottom = textureLodOffset(u_depthRt, uv, 0.0, ivec2(0, -2)).r;
+	const F32 depthLeft = textureLodOffset(u_depthRt, uv, 0.0, ivec2(-2, 0)).r;
+	const F32 depthRight = textureLodOffset(u_depthRt, uv, 0.0, ivec2(2, 0)).r;
+	const F32 depthTop = textureLodOffset(u_depthRt, uv, 0.0, ivec2(0, 2)).r;
+	const F32 depthBottom = textureLodOffset(u_depthRt, uv, 0.0, ivec2(0, -2)).r;
 
-	F32 ddx = smallerDelta(depthLeft, depth, depthRight);
-	F32 ddy = smallerDelta(depthBottom, depth, depthTop);
+	const F32 ddx = smallerDelta(depthLeft, depth, depthRight);
+	const F32 ddy = smallerDelta(depthBottom, depth, depthTop);
 
-	Vec2 ndc = UV_TO_NDC(uv);
+	const Vec2 ndc = UV_TO_NDC(uv);
 	const Vec2 TEXEL_SIZE = 1.0 / Vec2(FB_SIZE);
 	const Vec2 NDC_TEXEL_SIZE = 2.0 * TEXEL_SIZE;
-	Vec3 right = unproject(ndc + Vec2(NDC_TEXEL_SIZE.x, 0.0), depth + ddx);
-	Vec3 top = unproject(ndc + Vec2(0.0, NDC_TEXEL_SIZE.y), depth + ddy);
+	const Vec3 right = unproject(ndc + Vec2(NDC_TEXEL_SIZE.x, 0.0), depth + ddx);
+	const Vec3 top = unproject(ndc + Vec2(0.0, NDC_TEXEL_SIZE.y), depth + ddy);
 
 	Vec3 normal = cross(origin - top, right - origin);
 	normal = normalize(normal);
@@ -142,35 +142,35 @@ void main(void)
 		return;
 	}
 
-	Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
+	const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
 #else
-	Vec2 uv = in_uv;
+	const Vec2 uv = in_uv;
 #endif
 
-	Vec2 ndc = UV_TO_NDC(uv);
+	const Vec2 ndc = UV_TO_NDC(uv);
 
 	// Compute origin
-	F32 depth = textureLod(u_depthRt, uv, 0.0).r;
-	Vec3 origin = unproject(ndc, depth);
+	const F32 depth = textureLod(u_depthRt, uv, 0.0).r;
+	const Vec3 origin = unproject(ndc, depth);
 
 	// Get normal
-	Vec3 normal = computeNormal(uv, origin, depth);
+	const Vec3 normal = computeNormal(uv, origin, depth);
 
 	// Find the projected radius
-	Vec3 sphereLimit = origin + Vec3(RADIUS, 0.0, 0.0);
-	Vec4 projSphereLimit = project(Vec4(sphereLimit, 1.0));
-	Vec2 projSphereLimit2 = projSphereLimit.xy / projSphereLimit.w;
-	F32 projRadius = length(projSphereLimit2 - ndc);
+	const Vec3 sphereLimit = origin + Vec3(RADIUS, 0.0, 0.0);
+	const Vec4 projSphereLimit = project(Vec4(sphereLimit, 1.0));
+	const Vec2 projSphereLimit2 = projSphereLimit.xy / projSphereLimit.w;
+	const F32 projRadius = length(projSphereLimit2 - ndc);
 
 	// Loop to compute
-	F32 randFactor = readRandom(uv, 0.0).r;
+	const F32 randFactor = readRandom(uv, 0.0).r;
 	F32 ssao = 0.0;
 	const F32 SAMPLE_COUNTF = F32(SAMPLE_COUNT);
 	ANKI_UNROLL for(U32 i = 0; i < SAMPLE_COUNT; ++i)
 	{
 		// Compute disk. Basically calculate a point in a spiral. See how it looks here
 		// https://shadertoy.com/view/Md3fDr
-		F32 fi = F32(i);
+		const F32 fi = F32(i);
 		const F32 TURNS = F32(SAMPLE_COUNT) / 2.0; // Calculate the number of the spiral turns
 		const F32 ANG = (PI * 2.0 * TURNS) / (SAMPLE_COUNTF - 1.0); // The angle distance between samples
 		F32 ang = ANG * fi;
@@ -179,13 +179,13 @@ void main(void)
 		F32 radius = (1.0 / SAMPLE_COUNTF) * (fi + 1.0);
 		radius = sqrt(radius); // Move the points a bit away from the center of the spiral
 
-		Vec2 point = Vec2(cos(ang), sin(ang)) * radius; // In NDC
+		const Vec2 point = Vec2(cos(ang), sin(ang)) * radius; // In NDC
 
-		Vec2 finalDiskPoint = ndc + point * projRadius;
+		const Vec2 finalDiskPoint = ndc + point * projRadius;
 
 		// Compute factor
-		Vec3 s = unproject(finalDiskPoint, textureLod(u_depthRt, NDC_TO_UV(finalDiskPoint), 0.0).r);
-		Vec3 u = s - origin;
+		const Vec3 s = unproject(finalDiskPoint, textureLod(u_depthRt, NDC_TO_UV(finalDiskPoint), 0.0).r);
+		const Vec3 u = s - origin;
 		ssao += max(dot(normal, u) + BIAS, EPSILON) / max(dot(u, u), EPSILON);
 	}
 
@@ -198,11 +198,11 @@ void main(void)
 	s_scratch[gl_LocalInvocationID.y][gl_LocalInvocationID.x].x = ssao;
 
 	// Do some pre-work to find out the neighbours
-	U32 left = (gl_LocalInvocationID.x != 0u) ? (gl_LocalInvocationID.x - 1u) : 0u;
-	U32 right =
+	const U32 left = (gl_LocalInvocationID.x != 0u) ? (gl_LocalInvocationID.x - 1u) : 0u;
+	const U32 right =
 		(gl_LocalInvocationID.x != WORKGROUP_SIZE.x - 1u) ? (gl_LocalInvocationID.x + 1u) : (WORKGROUP_SIZE.x - 1u);
-	U32 bottom = (gl_LocalInvocationID.y != 0u) ? (gl_LocalInvocationID.y - 1u) : 0u;
-	U32 top =
+	const U32 bottom = (gl_LocalInvocationID.y != 0u) ? (gl_LocalInvocationID.y - 1u) : 0u;
+	const U32 top =
 		(gl_LocalInvocationID.y != WORKGROUP_SIZE.y - 1u) ? (gl_LocalInvocationID.y + 1u) : (WORKGROUP_SIZE.y - 1u);
 
 	// Wait for all threads

+ 34 - 34
shaders/Ssr.glslp

@@ -49,11 +49,11 @@ shared Vec4 s_pixels[WORKGROUP_SIZE.y][WORKGROUP_SIZE.x];
 // It returns the UV coordinates of the reflection (xy) and the contrubution factor (z)
 Vec3 raymarch(Vec3 r, Vec3 n, Vec3 viewPos, Vec2 uv, F32 depth)
 {
-	Vec3 p0 = viewPos;
+	const Vec3 p0 = viewPos;
 
 	// Check for view facing reflections [sakibsaikia]
-	Vec3 viewDir = normalize(viewPos);
-	F32 cameraContribution = 1.0 - smoothstep(0.25, 0.5, dot(-viewDir, r));
+	const Vec3 viewDir = normalize(viewPos);
+	const F32 cameraContribution = 1.0 - smoothstep(0.25, 0.5, dot(-viewDir, r));
 	if(cameraContribution <= 0.0)
 	{
 		return Vec3(0.0);
@@ -61,20 +61,20 @@ Vec3 raymarch(Vec3 r, Vec3 n, Vec3 viewPos, Vec2 uv, F32 depth)
 
 	// Compute an end point p1. This point is supposed to fall in front of the near plane. Add a small padding to near
 	// to avoid having p1 touching the near plane.
-	Vec3 p1 = p0 + r * (-p0.z - (u_near + 0.1));
+	const Vec3 p1 = p0 + r * (-p0.z - (u_near + 0.1));
 
 	// Start point
-	Vec3 start = Vec3(uv, depth);
+	const Vec3 start = Vec3(uv, depth);
 
 	// Project end point
-	Vec4 end4 = u_projMat * Vec4(p1, 1.0);
+	const Vec4 end4 = u_projMat * Vec4(p1, 1.0);
 	Vec3 end = end4.xyz / end4.w;
 	end.xy = NDC_TO_UV(end.xy);
 
 	// Compute the ray and step size
 	Vec3 ray = end - start;
-	Vec2 texelDims = abs(ray.xy) * Vec2(FB_SIZE);
-	F32 stepSize = length(ray.xy) / max(texelDims.x, texelDims.y);
+	const Vec2 texelDims = abs(ray.xy) * Vec2(FB_SIZE);
+	const F32 stepSize = length(ray.xy) / max(texelDims.x, texelDims.y);
 	ray = normalize(ray);
 
 	// Compute step
@@ -83,8 +83,8 @@ Vec3 raymarch(Vec3 r, Vec3 n, Vec3 viewPos, Vec2 uv, F32 depth)
 
 	const U32 STEP_FRACTION = BIG_STEP_SKIP / (4u + 1u);
 	const U32 STEPS_ARR[4] = U32[](STEP_FRACTION, 4 * STEP_FRACTION, 2 * STEP_FRACTION, 3 * STEP_FRACTION);
-	U32 l = gl_GlobalInvocationID.x & 1u;
-	U32 j = gl_GlobalInvocationID.y & 1u;
+	const U32 l = gl_GlobalInvocationID.x & 1u;
+	const U32 j = gl_GlobalInvocationID.y & 1u;
 	U32 step = STEPS_ARR[l * 2u + j];
 
 	// Iterate
@@ -100,9 +100,9 @@ Vec3 raymarch(Vec3 r, Vec3 n, Vec3 viewPos, Vec2 uv, F32 depth)
 			break;
 		}
 
-		F32 depth = textureLod(u_depthRt, raySample.xy, 0.0).r;
+		const F32 depth = textureLod(u_depthRt, raySample.xy, 0.0).r;
 
-		Bool hit = raySample.z - depth >= 0.0;
+		const Bool hit = raySample.z - depth >= 0.0;
 		if(!hit)
 		{
 			step += stepSkip;
@@ -117,7 +117,7 @@ Vec3 raymarch(Vec3 r, Vec3 n, Vec3 viewPos, Vec2 uv, F32 depth)
 			// Found it
 
 			// Re-project previous frame
-			Vec4 v4 = u_prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(raySample.xy), raySample.z, 1.0);
+			const Vec4 v4 = u_prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(raySample.xy), raySample.z, 1.0);
 			Vec2 ndc = v4.xy / v4.w;
 			raySample.xy = NDC_TO_UV(ndc);
 
@@ -153,35 +153,35 @@ void main()
 		return;
 	}
 
-	Vec2 uv = (Vec2(fixedInvocationId.xy) + 0.5) / Vec2(FB_SIZE);
+	const Vec2 uv = (Vec2(fixedInvocationId.xy) + 0.5) / Vec2(FB_SIZE);
 
 	// Read part of the G-buffer
-	F32 roughness = readRoughnessFromGBuffer(u_gbufferRt1, uv);
-	Vec3 worldNormal = readNormalFromGBuffer(u_gbufferRt2, uv);
+	const F32 roughness = readRoughnessFromGBuffer(u_gbufferRt1, uv);
+	const Vec3 worldNormal = readNormalFromGBuffer(u_gbufferRt2, uv);
 
 	// Get depth
-	F32 depth = textureLod(u_depthRt, uv, 0.0).r;
+	const F32 depth = textureLod(u_depthRt, uv, 0.0).r;
 
 	// Get view pos
-	Vec4 viewPos4 = u_invProjMat * Vec4(UV_TO_NDC(uv), depth, 1.0);
-	Vec3 viewPos = viewPos4.xyz / viewPos4.w;
+	const Vec4 viewPos4 = u_invProjMat * Vec4(UV_TO_NDC(uv), depth, 1.0);
+	const Vec3 viewPos = viewPos4.xyz / viewPos4.w;
 
 	// Compute refl vector
-	Vec3 viewDir = normalize(viewPos);
-	Vec3 viewNormal = u_normalMat * worldNormal;
-	Vec3 reflVec = reflect(viewDir, viewNormal);
+	const Vec3 viewDir = normalize(viewPos);
+	const Vec3 viewNormal = u_normalMat * worldNormal;
+	const Vec3 reflVec = reflect(viewDir, viewNormal);
 
 	// Raymatch
-	Vec3 ssr = raymarch(reflVec, viewNormal, viewPos, uv, depth);
-	Vec2 reflUv = ssr.xy;
-	F32 factor = ssr.z;
+	const Vec3 ssr = raymarch(reflVec, viewNormal, viewPos, uv, depth);
+	const Vec2 reflUv = ssr.xy;
+	const F32 factor = ssr.z;
 
 	// Read the reflection
 	Vec4 outColor;
 	ANKI_BRANCH if(factor > 0.0)
 	{
 		// Read the refl
-		F32 lod = F32(LIGHT_BUFFER_MIP_COUNT - 1u) * roughness;
+		const F32 lod = F32(LIGHT_BUFFER_MIP_COUNT - 1u) * roughness;
 		outColor.rgb = textureLod(u_lightBufferRt, reflUv, lod).rgb;
 		outColor.rgb = clamp(outColor.rgb, 0.0, FLT_MAX); // Fix the value just in case
 		outColor.rgb *= factor;
@@ -195,11 +195,11 @@ void main()
 	// Use the prev color
 	if(HISTORY_COLOR_BLEND_FACTOR > 0.0)
 	{
-		Vec4 v4 = u_prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(uv), depth, 1.0);
-		Vec2 oldNdc = v4.xy / v4.w;
-		Vec2 oldUv = saturate(NDC_TO_UV(oldNdc));
+		const Vec4 v4 = u_prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(uv), depth, 1.0);
+		const Vec2 oldNdc = v4.xy / v4.w;
+		const Vec2 oldUv = saturate(NDC_TO_UV(oldNdc));
 
-		Vec4 prevColor = imageLoad(out_img, ivec2(oldUv * vec2(FB_SIZE)));
+		const Vec4 prevColor = imageLoad(out_img, ivec2(oldUv * vec2(FB_SIZE)));
 
 		outColor = mix(prevColor, outColor, HISTORY_COLOR_BLEND_FACTOR);
 	}
@@ -217,18 +217,18 @@ void main()
 	storePixel.y = fixedInvocationId.y;
 
 #if VARIANT == 0
-	Bool pickRightNeighbour = (fixedInvocationId.y & 1) == 1;
+	const Bool pickRightNeighbour = (fixedInvocationId.y & 1) == 1;
 #else
-	Bool pickRightNeighbour = (fixedInvocationId.y & 1) == 0;
+	const Bool pickRightNeighbour = (fixedInvocationId.y & 1) == 0;
 #endif
-	I32 xOffset = (pickRightNeighbour) ? 1 : -1;
+	const I32 xOffset = (pickRightNeighbour) ? 1 : -1;
 
 	readPixel.x = I32(gl_LocalInvocationID.x) + xOffset;
 	readPixel.x = clamp(readPixel.x, 0, I32(WORKGROUP_SIZE.x - 1));
 
 	storePixel.x = fixedInvocationId.x + xOffset;
 
-	Vec4 missingColor = (outColor + s_pixels[readPixel.y][readPixel.x]) * 0.5; // average
+	const Vec4 missingColor = (outColor + s_pixels[readPixel.y][readPixel.x]) * 0.5; // average
 
 	// Store both the pixels
 	imageStore(out_img, fixedInvocationId, outColor);

+ 32 - 32
shaders/TemporalAAResolve.glslp

@@ -50,14 +50,14 @@ layout(ANKI_IMAGE_BINDING(0, 0)) writeonly uniform image2D out_img;
 
 Vec3 sharpen(Vec2 uv)
 {
-	Vec3 center = sample(u_inputRt, uv);
+	const Vec3 center = sample(u_inputRt, uv);
 #if SHARPEN == 1
 	Vec3 near = sampleOffset(u_inputRt, uv, 1, 0) + sampleOffset(u_inputRt, uv, -1, 0);
 #else
 	Vec3 near = sampleOffset(u_inputRt, uv, 0, 1) + sampleOffset(u_inputRt, uv, 0, -1);
 #endif
 	near *= 0.5;
-	F32 sharpness = 1.0;
+	const F32 sharpness = 1.0;
 	return center + max(Vec3(0.0), center - near) * sharpness;
 }
 
@@ -71,13 +71,13 @@ void main()
 		}
 	}
 
-	Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
-	F32 depth = textureLod(u_depthRt, uv, 0.0).r;
+	const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
+	const F32 depth = textureLod(u_depthRt, uv, 0.0).r;
 
 	// Get prev uv coords
 	Vec2 oldUv;
 #if VELOCITY
-	Vec2 velocity = textureLod(u_velocityRt, uv, 0.0).rg;
+	const Vec2 velocity = textureLod(u_velocityRt, uv, 0.0).rg;
 	if(velocity.x != -1.0)
 	{
 		oldUv = uv + velocity;
@@ -85,66 +85,66 @@ void main()
 	else
 #endif
 	{
-		Vec4 v4 = u_prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(uv), depth, 1.0);
+		const Vec4 v4 = u_prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(uv), depth, 1.0);
 		oldUv = NDC_TO_UV(v4.xy / v4.w);
 	}
 
 	// Read textures
 	Vec3 historyCol = sample(u_historyRt, oldUv);
 #if SHARPEN > 0
-	Vec3 crntCol = sharpen(uv);
+	const Vec3 crntCol = sharpen(uv);
 #else
-	Vec3 crntCol = sample(u_inputRt, uv);
+	const Vec3 crntCol = sample(u_inputRt, uv);
 #endif
 
 	// Remove ghosting by clamping the history color to neighbour's AABB
-	Vec3 near0 = sampleOffset(u_inputRt, uv, 1, 0);
-	Vec3 near1 = sampleOffset(u_inputRt, uv, 0, 1);
-	Vec3 near2 = sampleOffset(u_inputRt, uv, -1, 0);
-	Vec3 near3 = sampleOffset(u_inputRt, uv, 0, -1);
+	const Vec3 near0 = sampleOffset(u_inputRt, uv, 1, 0);
+	const Vec3 near1 = sampleOffset(u_inputRt, uv, 0, 1);
+	const Vec3 near2 = sampleOffset(u_inputRt, uv, -1, 0);
+	const Vec3 near3 = sampleOffset(u_inputRt, uv, 0, -1);
 
 #if VARIANCE_CLIPPING
-	Vec3 m1 = crntCol + near0 + near1 + near2 + near3;
-	Vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
+	const Vec3 m1 = crntCol + near0 + near1 + near2 + near3;
+	const Vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
 
-	Vec3 mu = m1 / 5.0;
-	Vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
+	const Vec3 mu = m1 / 5.0;
+	const Vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
 
-	Vec3 boxMin = mu - VARIANCE_CLIPPING_GAMMA * sigma;
-	Vec3 boxMax = mu + VARIANCE_CLIPPING_GAMMA * sigma;
+	const Vec3 boxMin = mu - VARIANCE_CLIPPING_GAMMA * sigma;
+	const Vec3 boxMax = mu + VARIANCE_CLIPPING_GAMMA * sigma;
 #else
-	Vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
-	Vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
+	const Vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
+	const Vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
 #endif
 
 	historyCol = clamp(historyCol, boxMin, boxMax);
 
 	// Remove jitter (T. Lottes)
 #if YCBCR
-	F32 lum0 = crntCol.r;
-	F32 lum1 = historyCol.r;
-	F32 maxLum = boxMax.r;
+	const F32 lum0 = crntCol.r;
+	const F32 lum1 = historyCol.r;
+	const F32 maxLum = boxMax.r;
 #elif TONEMAP_FIX
-	F32 lum0 = computeLuminance(tonemap(crntCol, u_exposureThreshold0));
-	F32 lum1 = computeLuminance(tonemap(historyCol, u_exposureThreshold0));
+	const F32 lum0 = computeLuminance(tonemap(crntCol, u_exposureThreshold0));
+	const F32 lum1 = computeLuminance(tonemap(historyCol, u_exposureThreshold0));
 	// F32 maxLum = computeLuminance(tonemap(boxMax, u_exposureThreshold0));
-	F32 maxLum = 1.0;
+	const F32 maxLum = 1.0;
 #else
-	F32 lum0 = computeLuminance(crntCol);
-	F32 lum1 = computeLuminance(historyCol);
-	F32 maxLum = computeLuminance(boxMax);
+	const F32 lum0 = computeLuminance(crntCol);
+	const F32 lum1 = computeLuminance(historyCol);
+	const F32 maxLum = computeLuminance(boxMax);
 #endif
 
 	F32 diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum + EPSILON));
 	diff = 1.0 - diff;
 	diff = diff * diff;
-	F32 feedback = mix(0.0, BLEND_FACTOR, diff);
+	const F32 feedback = mix(0.0, BLEND_FACTOR, diff);
 
 	// Write result
 #if YCBCR
-	Vec3 outColor = yCbCrToRgb(mix(historyCol, crntCol, feedback));
+	const Vec3 outColor = yCbCrToRgb(mix(historyCol, crntCol, feedback));
 #else
-	Vec3 outColor = mix(historyCol, crntCol, feedback);
+	const Vec3 outColor = mix(historyCol, crntCol, feedback);
 #endif
 	imageStore(out_img, IVec2(gl_GlobalInvocationID.xy), Vec4(outColor, 0.0));
 }

+ 11 - 11
shaders/Tonemapping.glsl

@@ -8,41 +8,41 @@
 #include <shaders/Common.glsl>
 
 // A tick to compute log of base 10
-F32 log10(in F32 x)
+F32 log10(F32 x)
 {
 	return log(x) / log(10.0);
 }
 
-F32 computeLuminance(in Vec3 color)
+F32 computeLuminance(Vec3 color)
 {
 	return max(dot(Vec3(0.30, 0.59, 0.11), color), EPSILON);
 }
 
 F32 computeExposure(F32 avgLum, F32 threshold)
 {
-	F32 keyValue = 1.03 - (2.0 / (2.0 + log10(avgLum + 1.0)));
-	F32 linearExposure = (keyValue / avgLum);
+	const F32 keyValue = 1.03 - (2.0 / (2.0 + log10(avgLum + 1.0)));
+	const F32 linearExposure = (keyValue / avgLum);
 	F32 exposure = log2(linearExposure);
 
 	exposure -= threshold;
 	return exp2(exposure);
 }
 
-Vec3 computeExposedColor(in Vec3 color, in F32 avgLum, in F32 threshold)
+Vec3 computeExposedColor(Vec3 color, F32 avgLum, F32 threshold)
 {
 	return computeExposure(avgLum, threshold) * color;
 }
 
 // Reinhard operator
-Vec3 tonemapReinhard(in Vec3 color, in F32 saturation)
+Vec3 tonemapReinhard(Vec3 color, F32 saturation)
 {
-	F32 lum = computeLuminance(color);
-	F32 toneMappedLuminance = lum / (lum + 1.0);
+	const F32 lum = computeLuminance(color);
+	const F32 toneMappedLuminance = lum / (lum + 1.0);
 	return toneMappedLuminance * pow(color / lum, Vec3(saturation));
 }
 
 // Uncharted 2 operator
-Vec3 tonemapUncharted2(in Vec3 color)
+Vec3 tonemapUncharted2(Vec3 color)
 {
 	const F32 A = 0.15;
 	const F32 B = 0.50;
@@ -69,7 +69,7 @@ Vec3 tonemap(Vec3 color, F32 exposure)
 {
 	color *= exposure;
 #if 0
-	F32 saturation = 1.0;
+	const F32 saturation = 1.0;
 	return tonemapReinhard(color, saturation);
 #else
 	return tonemapACESFilm(color);
@@ -78,6 +78,6 @@ Vec3 tonemap(Vec3 color, F32 exposure)
 
 Vec3 tonemap(Vec3 color, F32 avgLum, F32 threshold)
 {
-	F32 exposure = computeExposure(avgLum, threshold);
+	const F32 exposure = computeExposure(avgLum, threshold);
 	return tonemap(color, exposure);
 }

+ 5 - 5
shaders/TonemappingAverageLuminance.glslp

@@ -34,8 +34,8 @@ void main()
 	const U32 PIXEL_READ_X = TRIMMED_INPUT_TEX_SIZE.x / WORKGROUP_SIZE.x;
 	const U32 PIXEL_READ_Y = TRIMMED_INPUT_TEX_SIZE.y / WORKGROUP_SIZE.y;
 
-	U32 yStart = gl_LocalInvocationID.y * PIXEL_READ_Y;
-	U32 xStart = gl_LocalInvocationID.x * PIXEL_READ_X;
+	const U32 yStart = gl_LocalInvocationID.y * PIXEL_READ_Y;
+	const U32 xStart = gl_LocalInvocationID.x * PIXEL_READ_X;
 
 	F32 avgLum = 0.0;
 	ANKI_UNROLL for(U32 y = 0; y < PIXEL_READ_Y; ++y)
@@ -74,13 +74,13 @@ void main()
 	ANKI_BRANCH if(gl_LocalInvocationIndex == 0u)
 	{
 #if LOG_AVG
-		F32 crntLum = exp(s_avgLum[0] * (1.0 / F32(TRIMMED_INPUT_TEX_SIZE.x * TRIMMED_INPUT_TEX_SIZE.y)));
+		const F32 crntLum = exp(s_avgLum[0] * (1.0 / F32(TRIMMED_INPUT_TEX_SIZE.x * TRIMMED_INPUT_TEX_SIZE.y)));
 #else
-		F32 crntLum = s_avgLum[0] * (1.0 / F32(TRIMMED_INPUT_TEX_SIZE.x * TRIMMED_INPUT_TEX_SIZE.y));
+		const F32 crntLum = s_avgLum[0] * (1.0 / F32(TRIMMED_INPUT_TEX_SIZE.x * TRIMMED_INPUT_TEX_SIZE.y));
 #endif
 
 #if 1
-		F32 prevLum = u_averageLuminance;
+		const F32 prevLum = u_averageLuminance;
 
 		// Lerp between previous and new L value
 		const F32 INTERPOLATION_FACTOR = 0.05;

+ 22 - 22
shaders/TraditionalDeferredShading.glslp

@@ -76,10 +76,10 @@ layout(ANKI_TEX_BINDING(
 void main()
 {
 	// Compute UV coordinates
-	Vec2 uv = Vec2(gl_FragCoord.xy) / u_unis.m_fbSize;
-	Vec2 uvToRead = fma(uv, u_unis.m_inputTexUvScale, u_unis.m_inputTexUvOffset);
+	const Vec2 uv = Vec2(gl_FragCoord.xy) / u_unis.m_fbSize;
+	const Vec2 uvToRead = fma(uv, u_unis.m_inputTexUvScale, u_unis.m_inputTexUvOffset);
 
-	F32 depth = texture(u_msDepthRt, uvToRead).r;
+	const F32 depth = texture(u_msDepthRt, uvToRead).r;
 
 #if LIGHT_TYPE != DIR_LIGHT_TYPE
 	// Do manual depth test
@@ -94,35 +94,35 @@ void main()
 	readGBuffer(u_msRt0, u_msRt1, u_msRt2, uvToRead, 0.0, gbuffer);
 	gbuffer.m_subsurface = max(gbuffer.m_subsurface, SUBSURFACE_MIN * 8.0);
 
-	Vec4 worldPos4 = u_unis.m_invViewProjMat * Vec4(UV_TO_NDC(uv), depth, 1.0);
-	Vec3 worldPos = worldPos4.xyz / worldPos4.w;
+	const Vec4 worldPos4 = u_unis.m_invViewProjMat * Vec4(UV_TO_NDC(uv), depth, 1.0);
+	const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
 
 	// Compute diff
-	Vec3 diffC = diffuseLambert(gbuffer.m_diffuse);
+	const Vec3 diffC = diffuseLambert(gbuffer.m_diffuse);
 
 	// Compute spec
-	Vec3 viewDir = normalize(u_unis.m_camPos - worldPos);
+	const Vec3 viewDir = normalize(u_unis.m_camPos - worldPos);
 #if LIGHT_TYPE == DIR_LIGHT_TYPE
-	Vec3 l = -u_unis.m_lightDir;
+	const Vec3 l = -u_unis.m_lightDir;
 #else
-	Vec3 frag2Light = u_unis.m_position - worldPos;
-	Vec3 l = normalize(frag2Light);
-	F32 nol = max(0.0, dot(gbuffer.m_normal, l));
+	const Vec3 frag2Light = u_unis.m_position - worldPos;
+	const Vec3 l = normalize(frag2Light);
+	const F32 nol = max(0.0, dot(gbuffer.m_normal, l));
 #endif
-	Vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l);
+	const Vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l);
 
 	// Compute factors
 #if LIGHT_TYPE == POINT_LIGHT_TYPE
-	F32 att = computeAttenuationFactor(u_unis.m_oneOverSquareRadius, frag2Light);
-	F32 lambert = nol;
-	F32 factor = att * max(lambert, gbuffer.m_subsurface);
+	const F32 att = computeAttenuationFactor(u_unis.m_oneOverSquareRadius, frag2Light);
+	const F32 lambert = nol;
+	const F32 factor = att * max(lambert, gbuffer.m_subsurface);
 #elif LIGHT_TYPE == SPOT_LIGHT_TYPE
-	F32 att = computeAttenuationFactor(u_unis.m_oneOverSquareRadius, frag2Light);
-	F32 lambert = nol;
-	F32 spot = computeSpotFactor(l, u_unis.m_outerCos, u_unis.m_innerCos, u_unis.m_lightDir);
-	F32 factor = att * spot * max(lambert, gbuffer.m_subsurface);
+	const F32 att = computeAttenuationFactor(u_unis.m_oneOverSquareRadius, frag2Light);
+	const F32 lambert = nol;
+	const F32 spot = computeSpotFactor(l, u_unis.m_outerCos, u_unis.m_innerCos, u_unis.m_lightDir);
+	const F32 factor = att * spot * max(lambert, gbuffer.m_subsurface);
 #else
-	F32 linearDepth = linearizeDepth(depth, u_unis.m_near, u_unis.m_far);
+	const F32 linearDepth = linearizeDepth(depth, u_unis.m_near, u_unis.m_far);
 	F32 shadowFactor;
 	if(linearDepth * (u_unis.m_far - u_unis.m_near) < u_unis.m_effectiveShadowDistance)
 	{
@@ -135,8 +135,8 @@ void main()
 		shadowFactor = 1.0;
 	}
 
-	F32 lambert = dot(l, gbuffer.m_normal);
-	F32 factor = shadowFactor * max(gbuffer.m_subsurface, lambert);
+	const F32 lambert = dot(l, gbuffer.m_normal);
+	const F32 factor = shadowFactor * max(gbuffer.m_subsurface, lambert);
 #endif
 
 	out_color = (specC + diffC) * u_unis.m_diffuseColor * factor;

+ 1 - 1
shaders/Ui.glslp

@@ -36,7 +36,7 @@ void main()
 #endif
 	out_col = in_col;
 
-	Vec2 pos = u_transform.xy * in_pos + u_transform.zw;
+	const Vec2 pos = u_transform.xy * in_pos + u_transform.zw;
 	gl_Position = Vec4(pos, 0.0, 1.0);
 }
 #pragma anki end

+ 11 - 11
shaders/VolumetricFogAccumulation.glslp

@@ -39,21 +39,21 @@ void main()
 		return;
 	}
 
-	Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(VOLUME_SIZE.xy);
+	const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(VOLUME_SIZE.xy);
 
 	Vec4 colorAndDensityFront = Vec4(0.0);
 	ANKI_LOOP for(U32 i = 0u; i < VOLUME_SIZE.z; ++i)
 	{
 		// Compute the cluster K limits of this cluster fragment
-		F32 clusterKNear = F32(i) * (F32(FINAL_CLUSTER_Z + 1u) / F32(VOLUME_SIZE.z));
-		F32 clusterKFar = F32(i + 1u) * (F32(FINAL_CLUSTER_Z + 1u) / F32(VOLUME_SIZE.z));
+		const F32 clusterKNear = F32(i) * (F32(FINAL_CLUSTER_Z + 1u) / F32(VOLUME_SIZE.z));
+		const F32 clusterKFar = F32(i + 1u) * (F32(FINAL_CLUSTER_Z + 1u) / F32(VOLUME_SIZE.z));
 
 		// Compute the min and max Z in view space if this cluster fragment
-		F32 zVSpaceNear = -computeClusterNearf(u_clustererMagic, clusterKNear);
-		F32 zVSpaceFar = -computeClusterNearf(u_clustererMagic, clusterKFar);
+		const F32 zVSpaceNear = -computeClusterNearf(u_clustererMagic, clusterKNear);
+		const F32 zVSpaceFar = -computeClusterNearf(u_clustererMagic, clusterKFar);
 
 		// Compute the thikness of this fragment
-		F32 layerThinkness = abs(zVSpaceNear - zVSpaceFar);
+		const F32 layerThinkness = abs(zVSpaceNear - zVSpaceFar);
 
 		// Read the light value and the fog density from the fog volumes
 		Vec4 lightAndFogDensity = textureLod(u_lightVolume, Vec3(uv, clusterKFar / F32(FINAL_CLUSTER_Z + 1u)), 0.0);
@@ -61,17 +61,17 @@ void main()
 		lightAndFogDensity.w += u_density; // Apply the default density
 
 		// Scattering & absorption
-		F32 scattering = lightAndFogDensity.w * u_fogScatteringCoeff * layerThinkness;
-		F32 absorption = lightAndFogDensity.w * u_fogAbsorptionCoeff * layerThinkness;
+		const F32 scattering = lightAndFogDensity.w * u_fogScatteringCoeff * layerThinkness;
+		const F32 absorption = lightAndFogDensity.w * u_fogAbsorptionCoeff * layerThinkness;
 
 		// Integrate
-		Vec4 colorAndDensityBack = Vec4(lightAndFogDensity.xyz * scattering, scattering + absorption);
+		const Vec4 colorAndDensityBack = Vec4(lightAndFogDensity.xyz * scattering, scattering + absorption);
 
-		Vec3 l = colorAndDensityFront.rgb + saturate(exp(-colorAndDensityFront.a)) * colorAndDensityBack.rgb;
+		const Vec3 l = colorAndDensityFront.rgb + saturate(exp(-colorAndDensityFront.a)) * colorAndDensityBack.rgb;
 		colorAndDensityFront = Vec4(l.rgb, colorAndDensityFront.a + colorAndDensityBack.a);
 
 		// Write the value
-		Vec4 valToWrite = Vec4(colorAndDensityFront.rgb, saturate(exp(-colorAndDensityFront.a)));
+		const Vec4 valToWrite = Vec4(colorAndDensityFront.rgb, saturate(exp(-colorAndDensityFront.a)));
 		imageStore(u_fogVolume, IVec3(UVec3(gl_GlobalInvocationID.xy, i)), valToWrite);
 	}
 }

+ 43 - 42
shaders/VolumetricLightingAccumulation.glslp

@@ -57,24 +57,24 @@ Vec3 readRand()
 Vec3 worldPosInsideClusterAndZViewSpace(Vec3 relativePos, out F32 negativeZViewSpace)
 {
 	// Compute the cluster Z as float
-	F32 clusterKNear = g_globalInvocationID.z * (F32(FINAL_CLUSTER_Z + 1u) / F32(VOLUME_SIZE.z));
-	F32 clusterKFar = (g_globalInvocationID.z + 1.0) * (F32(FINAL_CLUSTER_Z + 1u) / F32(VOLUME_SIZE.z));
-	F32 clusterK = mix(clusterKNear, clusterKFar, relativePos.z);
+	const F32 clusterKNear = g_globalInvocationID.z * (F32(FINAL_CLUSTER_Z + 1u) / F32(VOLUME_SIZE.z));
+	const F32 clusterKFar = (g_globalInvocationID.z + 1.0) * (F32(FINAL_CLUSTER_Z + 1u) / F32(VOLUME_SIZE.z));
+	const F32 clusterK = mix(clusterKNear, clusterKFar, relativePos.z);
 
 	// Get a Z value
 	negativeZViewSpace = computeClusterNearf(u_clustererMagic, clusterK);
-	F32 zVSpace = -negativeZViewSpace;
+	const F32 zVSpace = -negativeZViewSpace;
 
 	// Get a XY value
-	Vec2 uvMin = g_globalInvocationID.xy / Vec2(VOLUME_SIZE.xy);
-	Vec2 uvMax = uvMin + 1.0 / Vec2(VOLUME_SIZE.xy);
-	Vec2 uv = mix(uvMin, uvMax, relativePos.xy);
-	Vec2 ndc = UV_TO_NDC(uv);
-	Vec2 xyZVspace = ndc * u_unprojectionParams.xy * zVSpace;
+	const Vec2 uvMin = g_globalInvocationID.xy / Vec2(VOLUME_SIZE.xy);
+	const Vec2 uvMax = uvMin + 1.0 / Vec2(VOLUME_SIZE.xy);
+	const Vec2 uv = mix(uvMin, uvMax, relativePos.xy);
+	const Vec2 ndc = UV_TO_NDC(uv);
+	const Vec2 xyZVspace = ndc * u_unprojectionParams.xy * zVSpace;
 
 	// Get the pos
-	Vec4 worldPos4 = u_invViewMat * Vec4(xyZVspace, zVSpace, 1.0);
-	Vec3 worldPos = worldPos4.xyz;
+	const Vec4 worldPos4 = u_invViewMat * Vec4(xyZVspace, zVSpace, 1.0);
+	const Vec3 worldPos = worldPos4.xyz;
 
 	return worldPos;
 }
@@ -88,12 +88,12 @@ Vec3 worldPosInsideCluster(Vec3 relativePos)
 // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter16.html
 F32 phaseFunction(Vec3 viewDir, Vec3 lightDir, F32 g)
 {
-	F32 g2 = g * g;
-	F32 cosTheta = max(0.0, dot(viewDir, lightDir));
-	F32 cosTheta2 = cosTheta * cosTheta;
+	const F32 g2 = g * g;
+	const F32 cosTheta = max(0.0, dot(viewDir, lightDir));
+	const F32 cosTheta2 = cosTheta * cosTheta;
 
-	F32 a = (3.0 * (1.0 - g2)) / (2.0 * (2.0 + g2));
-	F32 b = (1.0 + cosTheta2) / pow(1.0 + g2 - 2.0 * g * cosTheta, 3.0 / 2.0);
+	const F32 a = (3.0 * (1.0 - g2)) / (2.0 * (2.0 + g2));
+	const F32 b = (1.0 + cosTheta2) / pow(1.0 + g2 - 2.0 * g * cosTheta, 3.0 / 2.0);
 
 	return saturate(a * b);
 }
@@ -101,7 +101,7 @@ F32 phaseFunction(Vec3 viewDir, Vec3 lightDir, F32 g)
 Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos, F32 linearDepth)
 {
 	Vec3 color = Vec3(0.0);
-	Vec3 viewDir = normalize(u_cameraPos - worldPos);
+	const Vec3 viewDir = normalize(u_cameraPos - worldPos);
 
 	// Get ID offset
 	U32 idxOffset = u_clusters[clusterIdx];
@@ -114,8 +114,8 @@ Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos, F32 linearDepth)
 #if ENABLE_SHADOWS
 		if(u_dirLight.m_cascadeCount > 0u)
 		{
-			F32 cascadeCountf = F32(u_dirLight.m_cascadeCount);
-			U32 cascadeIdx = min(U32(linearDepth * cascadeCountf), u_dirLight.m_cascadeCount - 1u);
+			const F32 cascadeCountf = F32(u_dirLight.m_cascadeCount);
+			const U32 cascadeIdx = min(U32(linearDepth * cascadeCountf), u_dirLight.m_cascadeCount - 1u);
 			factor *= computeShadowFactorDirLight(u_dirLight, cascadeIdx, worldPos, u_shadowTex);
 		}
 #endif
@@ -127,9 +127,9 @@ Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos, F32 linearDepth)
 	U32 idx;
 	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
 	{
-		PointLight light = u_pointLights[idx];
+		const PointLight light = u_pointLights[idx];
 
-		Vec3 frag2Light = light.m_position - worldPos;
+		const Vec3 frag2Light = light.m_position - worldPos;
 		F32 factor = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
 
 		factor *= phaseFunction(viewDir, normalize(worldPos - light.m_position), PHASE_FUNCTION_ANISOTROPY);
@@ -147,19 +147,19 @@ Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos, F32 linearDepth)
 	// Spot lights
 	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
 	{
-		SpotLight light = u_spotLights[idx];
+		const SpotLight light = u_spotLights[idx];
 
-		Vec3 frag2Light = light.m_position - worldPos;
+		const Vec3 frag2Light = light.m_position - worldPos;
 		F32 factor = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
 
-		Vec3 l = normalize(frag2Light);
+		const Vec3 l = normalize(frag2Light);
 
 		factor *= computeSpotFactor(l, light.m_outerCos, light.m_innerCos, light.m_dir);
 
 		factor *= phaseFunction(viewDir, light.m_dir, PHASE_FUNCTION_ANISOTROPY);
 
 #if ENABLE_SHADOWS
-		F32 shadowmapLayerIdx = light.m_shadowmapId;
+		const F32 shadowmapLayerIdx = light.m_shadowmapId;
 		if(shadowmapLayerIdx >= 0.0)
 		{
 			factor *= computeShadowFactorSpotLight(light, worldPos, u_shadowTex);
@@ -174,13 +174,13 @@ Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos, F32 linearDepth)
 	Vec3 diffIndirect = Vec3(0.0);
 	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
 	{
-		ReflectionProbe probe = u_reflectionProbes[idx];
-		Vec3 aabbMin = probe.m_aabbMin;
-		Vec3 aabbMax = probe.m_aabbMax;
-		Vec3 probeOrigin = probe.m_position;
-		F32 cubemapIndex = probe.m_cubemapIndex;
+		const ReflectionProbe probe = u_reflectionProbes[idx];
+		const Vec3 aabbMin = probe.m_aabbMin;
+		const Vec3 aabbMax = probe.m_aabbMax;
+		const Vec3 probeOrigin = probe.m_position;
+		const F32 cubemapIndex = probe.m_cubemapIndex;
 
-		F32 blendWeight = computeProbeBlendWeight(worldPos, aabbMin, aabbMax, 0.2);
+		const F32 blendWeight = computeProbeBlendWeight(worldPos, aabbMin, aabbMax, 0.2);
 		totalBlendWeight += blendWeight;
 
 		Vec3 c = textureLod(u_irradianceTex, Vec4(viewDir, cubemapIndex), 0.0).rgb;
@@ -197,7 +197,7 @@ Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos, F32 linearDepth)
 	idxOffset = u_lightIndices[idxOffset - 1u];
 	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
 	{
-		FogDensityVolume vol = u_fogDensityVolumes[idx];
+		const FogDensityVolume vol = u_fogDensityVolumes[idx];
 
 		F32 factor;
 		ANKI_BRANCH if(vol.m_isBox == 1u)
@@ -207,7 +207,7 @@ Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos, F32 linearDepth)
 		}
 		else
 		{
-			Vec3 diff = worldPos - vol.m_aabbMinOrSphereCenter;
+			const Vec3 diff = worldPos - vol.m_aabbMinOrSphereCenter;
 			F32 distSq = dot(diff, diff) / vol.m_aabbMaxOrSphereRadiusSquared.x;
 			distSq = min(1.0, distSq);
 			factor = 1.0 - distSq;
@@ -227,35 +227,36 @@ void main()
 	}
 
 	// Find the cluster
-	UVec3 clusterXYZ = gl_GlobalInvocationID / FRACTION;
-	U32 clusterIdx = clusterXYZ.z * (CLUSTER_COUNT.x * CLUSTER_COUNT.y) + clusterXYZ.y * CLUSTER_COUNT.x + clusterXYZ.x;
+	const UVec3 clusterXYZ = gl_GlobalInvocationID / FRACTION;
+	const U32 clusterIdx =
+		clusterXYZ.z * (CLUSTER_COUNT.x * CLUSTER_COUNT.y) + clusterXYZ.y * CLUSTER_COUNT.x + clusterXYZ.x;
 
 	// Find a random pos inside the cluster
 	F32 negativeZViewSpace;
-	Vec3 worldPos = worldPosInsideClusterAndZViewSpace(readRand(), negativeZViewSpace);
+	const Vec3 worldPos = worldPosInsideClusterAndZViewSpace(readRand(), negativeZViewSpace);
 
 	// Get lighting
-	F32 linearDepth = negativeZViewSpace / (u_far - u_near);
+	const F32 linearDepth = negativeZViewSpace / (u_far - u_near);
 	Vec4 lightAndFog = accumulateLightsAndFog(clusterIdx, worldPos, linearDepth);
 
 	// Read the prev result
 	{
 		// Better get a new world pos in the center of the cluster. Using worldPos creates noisy results
-		Vec3 midWPos = worldPosInsideCluster(Vec3(0.5));
+		const Vec3 midWPos = worldPosInsideCluster(Vec3(0.5));
 
 		// Compute UV
-		Vec4 prevClipPos4 = u_prevViewProjMat * Vec4(midWPos, 1.0);
-		Vec2 prevUv = NDC_TO_UV(prevClipPos4.xy / prevClipPos4.w);
+		const Vec4 prevClipPos4 = u_prevViewProjMat * Vec4(midWPos, 1.0);
+		const Vec2 prevUv = NDC_TO_UV(prevClipPos4.xy / prevClipPos4.w);
 
 		// Compute new Z tex coord
 		F32 k = computeClusterKf(u_prevClustererMagic, midWPos);
 		k /= F32(FINAL_CLUSTER_Z + 1u);
 
 		// Read prev
-		Vec3 uvw = Vec3(prevUv, k);
+		const Vec3 uvw = Vec3(prevUv, k);
 		if(all(lessThan(abs(uvw), Vec3(1.0))))
 		{
-			Vec4 prev = textureLod(u_prevVolume, uvw, 0.0);
+			const Vec4 prev = textureLod(u_prevVolume, uvw, 0.0);
 
 			// Modulate
 			lightAndFog = mix(prev, lightAndFog, 1.0 / 16.0);

+ 4 - 4
shaders/glsl_cpp_common/ClusteredShading.h

@@ -133,7 +133,7 @@ ANKI_SHADER_STATIC_ASSERT(sizeof(LightingUniforms) == SIZEOF_LIGHTING_UNIFORMS)
 
 ANKI_SHADER_FUNC_INLINE F32 computeClusterKf(ClustererMagicValues magic, Vec3 worldPos)
 {
-	F32 fz = sqrt(dot(magic.m_val0.xyz(), worldPos) - magic.m_val0.w());
+	const F32 fz = sqrt(dot(magic.m_val0.xyz(), worldPos) - magic.m_val0.w());
 	return fz;
 }
 
@@ -146,8 +146,8 @@ ANKI_SHADER_FUNC_INLINE U32 computeClusterK(ClustererMagicValues magic, Vec3 wor
 ANKI_SHADER_FUNC_INLINE U32 computeClusterIndex(
 	ClustererMagicValues magic, Vec2 uv, Vec3 worldPos, U32 clusterCountX, U32 clusterCountY)
 {
-	UVec2 xy = UVec2(uv * Vec2(clusterCountX, clusterCountY));
-	U32 k = computeClusterK(magic, worldPos);
+	const UVec2 xy = UVec2(uv * Vec2(clusterCountX, clusterCountY));
+	const U32 k = computeClusterK(magic, worldPos);
 	return k * (clusterCountX * clusterCountY) + xy.y() * clusterCountX + xy.x();
 }
 
@@ -167,7 +167,7 @@ ANKI_SHADER_FUNC_INLINE F32 computeClusterNear(ClustererMagicValues magic, U32 k
 ANKI_SHADER_FUNC_INLINE Vec3 computeClustererVolumeTextureUvs(
 	ClustererMagicValues magic, Vec2 uv, Vec3 worldPos, U32 clusterCountZ)
 {
-	F32 k = computeClusterKf(magic, worldPos);
+	const F32 k = computeClusterKf(magic, worldPos);
 	return Vec3(uv, k / F32(clusterCountZ));
 }