Browse Source

[REFACTORING] Move forward shading to world space normal

Panagiotis Christopoulos Charitos 8 years ago
parent
commit
c61af0799c

+ 1 - 1
programs/FinalComposite.ankiprog

@@ -86,7 +86,7 @@ void main()
 	out_color += blueNoise / 255.0;
 #endif
 
-#if 1
+#if 0
 	{
 		out_color = vec3(textureLod(u_isRt, uv, 0.0).rgb);
 	}

+ 6 - 2
programs/ForwardShadingFog.ankiprog

@@ -17,10 +17,12 @@ http://www.anki3d.org/LICENSE
 			<source><![CDATA[
 #include "shaders/ForwardShadingCommonVert.glsl"
 
+layout(location = 0) out float out_zVSpace;
+
 void main() 
 {
 	gl_Position = mvp * vec4(in_position, 1.0);
-	out_posViewSpace = (modelView * vec4(in_position, 1.0)).xyz;
+	out_zVSpace = (modelView * vec4(in_position, 1.0)).z;
 }
 			]]></source>
 		</shader>
@@ -35,9 +37,11 @@ void main()
 			<source><![CDATA[
 #include "shaders/ForwardShadingCommonFrag.glsl"
 
+layout(location = 0) in float in_zVSpace;
+
 void main() 
 {
-	fog(fogColor, fogAlphaScale, fogDistanceOfMaxThikness);
+	fog(fogColor, fogAlphaScale, fogDistanceOfMaxThikness, in_zVSpace);
 }
 			]]></source>
 		</shader>

+ 18 - 4
programs/ForwardShadingParticles.ankiprog

@@ -17,15 +17,26 @@ http://www.anki3d.org/LICENSE
 			<inputs>
 				<input name="mvp" type="mat4"/>
 				<input name="cameraRotMat" type="mat3"/>
-				<input name="viewMat" type="mat4"/>
 			</inputs>
 
 			<source><![CDATA[
 #include "shaders/ForwardShadingCommonVert.glsl"
 
+layout(location = SCALE_LOCATION) in float in_scale;
+layout(location = ALPHA_LOCATION) in float in_alpha;
+
+layout(location = 0) flat out float out_alpha;
+layout(location = 1) out vec2 out_uv;
+layout(location = 2) out vec3 out_worldPos;
+
 void main() 
 {
-	particle(mvp, cameraRotMat, viewMat);
+	out_uv = vec2(gl_VertexID & 1, gl_VertexID >> 1);
+
+	out_worldPos = cameraRotMat * vec3((out_uv - 0.5) * in_scale, 0.0) + in_position;
+	gl_Position = mvp * vec4(out_worldPos, 1.0);
+
+	out_alpha = in_alpha;
 }
 			]]>
 			</source>
@@ -53,9 +64,12 @@ void main()
 			</inputs>
 
 			<source><![CDATA[
-
 #include "shaders/ForwardShadingCommonFrag.glsl"
 
+layout(location = 0) flat in float in_alpha;
+layout(location = 1) in vec2 in_uv;
+layout(location = 2) in vec3 in_worldPos;
+
 void main() 
 {
 #if ANIMATED_TEXTURE == 1
@@ -65,7 +79,7 @@ void main()
 #endif
 
 #if LIGHT
-	texCol.rgb = computeLightColor(texCol.rgb);
+	texCol.rgb = computeLightColor(texCol.rgb, in_worldPos);
 #endif
 	
 	vec4 colScale = colorScale;

+ 8 - 17
shaders/ForwardShadingCommonFrag.glsl

@@ -24,11 +24,6 @@ layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D anki_msDepthRt;
 #define anki_u_time u_time
 #define RENDERER_SIZE (u_rendererSize * 0.5)
 
-// Varyings
-layout(location = 0) flat in float in_alpha;
-layout(location = 1) in vec2 in_uv;
-layout(location = 2) in vec3 in_posViewSpace;
-
 layout(location = 0) out vec4 out_color;
 
 void writeGBuffer(in vec4 color)
@@ -43,17 +38,13 @@ vec4 readAnimatedTextureRgba(sampler2DArray tex, float period, vec2 uv, float ti
 	return texture(tex, vec3(uv, layer));
 }
 
-vec3 computeLightColor(vec3 diffCol)
+vec3 computeLightColor(vec3 diffCol, vec3 worldPos)
 {
 	vec3 outColor = vec3(0.0);
 
-	// Compute frag pos in view space
-	vec3 fragPos = in_posViewSpace;
-
 	// Find the cluster and then the light counts
 	uint clusterIdx = computeClusterIndex(
-		u_clustererMagic, gl_FragCoord.xy / RENDERER_SIZE, vec3(0.0), u_clusterCountX, u_clusterCountY);
-	clusterIdx = 0; // TODO
+		u_clustererMagic, gl_FragCoord.xy / RENDERER_SIZE, worldPos, u_clusterCountX, u_clusterCountY);
 
 	uint idxOffset = u_clusters[clusterIdx];
 
@@ -69,7 +60,7 @@ vec3 computeLightColor(vec3 diffCol)
 
 		vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
 
-		vec3 frag2Light = light.posRadius.xyz - fragPos;
+		vec3 frag2Light = light.posRadius.xyz - worldPos;
 		float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
 
 #if LOD > 1
@@ -97,7 +88,7 @@ vec3 computeLightColor(vec3 diffCol)
 
 		vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
 
-		vec3 frag2Light = light.posRadius.xyz - fragPos;
+		vec3 frag2Light = light.posRadius.xyz - worldPos;
 		float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
 
 		vec3 l = normalize(frag2Light);
@@ -111,7 +102,8 @@ vec3 computeLightColor(vec3 diffCol)
 		float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
 		if(shadowmapLayerIdx >= 0.0)
 		{
-			shadow = computeShadowFactorSpot(light.texProjectionMat, fragPos, light.specularColorRadius.w, u_shadowTex);
+			shadow =
+				computeShadowFactorSpot(light.texProjectionMat, worldPos, light.specularColorRadius.w, u_shadowTex);
 		}
 #endif
 
@@ -126,7 +118,7 @@ void particleAlpha(vec4 color, vec4 scaleColor, vec4 biasColor)
 	writeGBuffer(color * scaleColor + biasColor);
 }
 
-void fog(vec3 color, float fogAlphaScale, float fogDistanceOfMaxThikness)
+void fog(vec3 color, float fogAlphaScale, float fogDistanceOfMaxThikness, float zVSpace)
 {
 	const vec2 screenSize = 1.0 / RENDERER_SIZE;
 
@@ -137,12 +129,11 @@ void fog(vec3 color, float fogAlphaScale, float fogDistanceOfMaxThikness)
 	vec4 fragPosVspace4 = u_invProjMat * vec4(vec3(UV_TO_NDC(texCoords), depth), 1.0);
 	float sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
 
-	float diff = max(0.0, in_posViewSpace.z - sceneZVspace);
+	float diff = max(0.0, zVSpace - sceneZVspace);
 
 	zFeatherFactor = min(1.0, diff / fogDistanceOfMaxThikness);
 
 	writeGBuffer(vec4(color, zFeatherFactor * fogAlphaScale));
-	// writeGBuffer(vec4(vec3(zFeatherFactor), 1.0));
 }
 
 #endif

+ 0 - 18
shaders/ForwardShadingCommonVert.glsl

@@ -22,28 +22,10 @@
 
 // In/out
 layout(location = POSITION_LOCATION) in vec3 in_position;
-layout(location = SCALE_LOCATION) in float in_scale;
-layout(location = ALPHA_LOCATION) in float in_alpha;
-
-layout(location = 0) flat out float out_alpha;
-layout(location = 1) out vec2 out_uv;
-layout(location = 2) out vec3 out_posViewSpace;
 
 out gl_PerVertex
 {
 	vec4 gl_Position;
 };
 
-void particle(in mat4 mvp, in mat3 camRot, in mat4 viewMat)
-{
-	out_uv = vec2(gl_VertexID & 1, gl_VertexID >> 1);
-
-	vec3 worldPos = camRot * vec3((out_uv - 0.5) * in_scale, 0.0) + in_position;
-	gl_Position = mvp * vec4(worldPos, 1.0);
-
-	out_posViewSpace = (viewMat * vec4(worldPos, 1.0)).xyz;
-
-	out_alpha = in_alpha;
-}
-
 #endif

+ 1 - 1
src/anki/renderer/Clusterer.cpp

@@ -208,7 +208,7 @@ void Clusterer::prepare(ThreadPool& threadPool, const ClustererPrepareInfo& inf)
 	{
 		// Given a distance 'd' from the camera's near plane in world space the 'k' split is calculated like:
 		// k = sqrt(d / (f - n) * Cz2)  (1)
-		// where 'n' and 'f' are the near and f vals of the projection and Cz2 is the m_counts[2]^2
+		// where 'n' and 'f' are the near and far vals of the projection and Cz2 is the m_counts[2]^2
 		// If the 'd' is not known and the world position instead is known then 'd' is the distance from that position
 		// to the camera's near plane.
 		// d = dot(Pn, W) - Po  (2)

+ 0 - 1
src/anki/renderer/ForwardShading.cpp

@@ -130,7 +130,6 @@ void ForwardShading::drawVolumetric(RenderingContext& ctx, RenderPassWorkContext
 
 void ForwardShading::drawUpscale(const RenderingContext& ctx, RenderPassWorkContext& rgraphCtx)
 {
-	return; // TODO
 	CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
 
 	// **WARNING** Remember to update the consumers of the render pass that calls this method