Browse Source

Some tweaks in the renderer and visibility

Panagiotis Christopoulos Charitos 9 years ago
parent
commit
258abb3175

+ 8 - 18
shaders/Is.frag.glsl

@@ -25,25 +25,12 @@ layout(ANKI_TEX_BINDING(1, 1)) uniform sampler2D u_normalRoughnessDecalTex;
 layout(ANKI_TEX_BINDING(1, 2)) uniform sampler2D u_ssaoTex;
 
 layout(location = 0) in vec2 in_uv;
-layout(location = 1) flat in int in_instanceId;
-layout(location = 2) in vec2 in_projectionParams;
+layout(location = 1) in vec2 in_clusterIJ;
 
 layout(location = 0) out vec3 out_color;
 
-const uint TILE_COUNT = TILE_COUNT_X * TILE_COUNT_Y;
-
 const float SUBSURFACE_MIN = 0.05;
 
-// Return frag pos in view space
-vec3 getFragPosVSpace(float depth)
-{
-	vec3 fragPos;
-	fragPos.z = u_lightingUniforms.projectionParams.z / (u_lightingUniforms.projectionParams.w + depth);
-	fragPos.xy = in_projectionParams * fragPos.z;
-
-	return fragPos;
-}
-
 // Common code for lighting
 #define LIGHTING_COMMON_BRDF()                                                                                         \
 	vec3 frag2Light = light.posRadius.xyz - fragPos;                                                                   \
@@ -142,9 +129,12 @@ float readSsao(float depth, vec2 ndc)
 void main()
 {
 	float depth = texture(u_msDepthRt, in_uv, 0.0).r;
+	vec2 ndc = UV_TO_NDC(in_uv);
 
 	// Get frag pos in view space
-	vec3 fragPos = getFragPosVSpace(depth);
+	vec3 fragPos;
+	fragPos.z = u_lightingUniforms.projectionParams.z / (u_lightingUniforms.projectionParams.w + depth);
+	fragPos.xy = ndc * u_lightingUniforms.projectionParams.xy * fragPos.z;
 	vec3 viewDir = normalize(-fragPos);
 
 	// Decode GBuffer
@@ -167,15 +157,15 @@ void main()
 	emission = gbuffer.emission;
 
 	// Get SSAO
-	float ssao = readSsao(depth, UV_TO_NDC(in_uv));
+	float ssao = readSsao(depth, ndc);
 	diffCol *= ssao;
 
 	// Get counts and offsets
 	uint clusterIdx =
 		computeClusterK(
 			u_lightingUniforms.nearFarClustererMagicPad1.x, u_lightingUniforms.nearFarClustererMagicPad1.z, fragPos.z)
-			* TILE_COUNT
-		+ in_instanceId;
+			* (CLUSTER_COUNT_X * CLUSTER_COUNT_Y)
+		+ uint(in_clusterIJ.y) * CLUSTER_COUNT_X + uint(in_clusterIJ.x);
 
 	uint idxOffset = u_clusters[clusterIdx];
 	uint idx;

+ 8 - 21
shaders/Is.vert.glsl

@@ -3,14 +3,10 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
-#define LIGHT_SET 0
-#define LIGHT_SS_BINDING 0
-#define LIGHT_UBO_BINDING 0
-#include "shaders/ClusterLightCommon.glsl"
+#include "shaders/Common.glsl"
 
-layout(location = 0) out vec2 out_texCoord;
-layout(location = 1) flat out int out_instanceId;
-layout(location = 2) out vec2 out_projectionParams;
+layout(location = 0) out vec2 out_uv;
+layout(location = 1) out vec2 out_clusterIJ;
 
 out gl_PerVertex
 {
@@ -19,20 +15,11 @@ out gl_PerVertex
 
 void main()
 {
-	float instIdF = float(gl_InstanceID);
+	const vec2 POSITIONS[3] = vec2[](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
 
-	vec2 ij = vec2(mod(instIdF, float(TILE_COUNT_X)), floor(instIdF / float(TILE_COUNT_X)));
+	vec2 pos = POSITIONS[gl_VertexID];
+	out_uv = NDC_TO_UV(pos);
+	ANKI_WRITE_POSITION(vec4(pos, 0.0, 1.0));
 
-	out_instanceId = int(gl_InstanceID);
-
-	const vec2 SIZES = vec2(1.0 / float(TILE_COUNT_X), 1.0 / float(TILE_COUNT_Y));
-
-	const vec2 UVS[4] =
-		vec2[](vec2(0.0, 0.0) * SIZES, vec2(1.0, 0.0) * SIZES, vec2(0.0, 1.0) * SIZES, vec2(1.0, 1.0) * SIZES);
-
-	out_texCoord = UVS[gl_VertexID] + ij * SIZES;
-	vec2 pos = out_texCoord * 2.0 - 1.0;
-
-	gl_Position = vec4(pos, 0.0, 1.0);
-	out_projectionParams = u_lightingUniforms.projectionParams.xy * pos;
+	out_clusterIJ = vec2(CLUSTER_COUNT_X, CLUSTER_COUNT_Y) * out_uv;
 }

+ 3 - 3
src/anki/renderer/Is.cpp

@@ -99,8 +99,8 @@ Error Is::initInternal(const ConfigSet& config)
 	//
 	StringAuto pps(getAllocator());
 
-	pps.sprintf("#define TILE_COUNT_X %u\n"
-				"#define TILE_COUNT_Y %u\n"
+	pps.sprintf("#define CLUSTER_COUNT_X %u\n"
+				"#define CLUSTER_COUNT_Y %u\n"
 				"#define CLUSTER_COUNT %u\n"
 				"#define RENDERER_WIDTH %u\n"
 				"#define RENDERER_HEIGHT %u\n"
@@ -201,7 +201,7 @@ void Is::run(RenderingContext& ctx)
 	bindStorage(cmdb, 0, 0, ctx.m_is.m_clustersToken);
 	bindStorage(cmdb, 0, 1, ctx.m_is.m_lightIndicesToken);
 
-	cmdb->drawArrays(PrimitiveTopology::TRIANGLE_STRIP, 4, m_clusterCount);
+	cmdb->drawArrays(PrimitiveTopology::TRIANGLES, 3);
 	cmdb->endRenderPass();
 }
 

+ 35 - 13
src/anki/scene/Visibility.cpp

@@ -126,6 +126,7 @@ void VisibilityContext::submitNewWork(FrustumComponent& frc, ThreadHive& hive)
 		test.m_sectorsCtx = &gather->m_sectorsCtx;
 		test.m_taskIdx = i;
 		test.m_taskCount = testCount;
+		test.m_r = r;
 
 		auto& task = testTasks[i];
 		task.m_callback = VisibilityTestTask::callback;
@@ -381,21 +382,37 @@ void VisibilityTestTask::test(ThreadHive& hive)
 
 		if(lc && wantsLightComponents)
 		{
-			VisibilityGroupType gt;
-			switch(lc->getLightComponentType())
+			// Perform an additional check against the rasterizer
+			Bool in;
+			if(lc->getShadowEnabled())
 			{
-			case LightComponentType::POINT:
-				gt = VisibilityGroupType::LIGHTS_POINT;
-				break;
-			case LightComponentType::SPOT:
-				gt = VisibilityGroupType::LIGHTS_SPOT;
-				break;
-			default:
-				ANKI_ASSERT(0);
-				gt = VisibilityGroupType::TYPE_COUNT;
+				ANKI_ASSERT(spIdx == 1);
+				const SpatialComponent& sc = *sps[0].m_sp;
+				in = testAgainstRasterizer(sc.getSpatialCollisionShape(), sc.getAabb());
+			}
+			else
+			{
+				in = true;
 			}
 
-			visible->moveBack(alloc, gt, visibleNode);
+			if(in)
+			{
+				VisibilityGroupType gt;
+				switch(lc->getLightComponentType())
+				{
+				case LightComponentType::POINT:
+					gt = VisibilityGroupType::LIGHTS_POINT;
+					break;
+				case LightComponentType::SPOT:
+					gt = VisibilityGroupType::LIGHTS_SPOT;
+					break;
+				default:
+					ANKI_ASSERT(0);
+					gt = VisibilityGroupType::TYPE_COUNT;
+				}
+
+				visible->moveBack(alloc, gt, visibleNode);
+			}
 		}
 
 		if(lfc && wantsFlareComponents)
@@ -405,7 +422,12 @@ void VisibilityTestTask::test(ThreadHive& hive)
 
 		if(reflc && wantsReflectionProbes)
 		{
-			visible->moveBack(alloc, VisibilityGroupType::REFLECTION_PROBES, visibleNode);
+			ANKI_ASSERT(spIdx == 1);
+			const SpatialComponent& sc = *sps[0].m_sp;
+			if(testAgainstRasterizer(sc.getSpatialCollisionShape(), sc.getAabb()))
+			{
+				visible->moveBack(alloc, VisibilityGroupType::REFLECTION_PROBES, visibleNode);
+			}
 		}
 
 		if(proxyc && wantsReflectionProxies)

+ 6 - 0
src/anki/scene/VisibilityInternal.h

@@ -150,6 +150,7 @@ public:
 	U32 m_taskCount;
 	WeakPtr<VisibilityTestResults> m_result;
 	Timestamp m_timestamp = 0;
+	SoftwareRasterizer* m_r ANKI_DBG_NULLIFY_PTR;
 
 	/// Thread hive task.
 	static void callback(void* ud, U32 threadId, ThreadHive& hive)
@@ -161,6 +162,11 @@ public:
 private:
 	void test(ThreadHive& hive);
 	void updateTimestamp(const SceneNode& node);
+
+	ANKI_USE_RESULT Bool testAgainstRasterizer(const CollisionShape& cs, const Aabb& aabb) const
+	{
+		return (m_r) ? m_r->visibilityTest(cs, aabb) : true;
+	}
 };
 
 /// Task that combines and sorts the results.