Browse Source

CPU reflection optimizations. Some bug fixes

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
eca9f94e29

+ 3 - 0
include/anki/scene/ReflectionProbe.h

@@ -43,6 +43,9 @@ public:
 		m_cubemapArrayIdx = cubemapArrayIdx;
 	}
 
+	ANKI_USE_RESULT Error frameUpdate(F32 prevUpdateTime,
+		F32 crntTime) override;
+
 private:
 	class CubeSide
 	{

+ 11 - 0
include/anki/scene/ReflectionProbeComponent.h

@@ -47,9 +47,20 @@ public:
 		m_radius = radius;
 	}
 
+	Bool getMarkedForRendering() const
+	{
+		return m_markedForRendering;
+	}
+
+	void setMarkedForRendering(Bool render)
+	{
+		m_markedForRendering = render;
+	}
+
 private:
 	Vec4 m_pos = Vec4(0.0);
 	F32 m_radius = 0.0;
+	Bool8 m_markedForRendering = false;
 };
 /// @}
 

+ 12 - 12
shaders/FsCommonFrag.glsl

@@ -18,7 +18,7 @@ layout(TEX_BINDING(1, 0)) uniform sampler2D anki_msDepthRt;
 #undef LIGHT_SS_BINDING
 #undef LIGHT_TEX_BINDING
 
-#define anki_u_time u_lightingUniforms.groundLightDirTime.w
+#define anki_u_time u_lightingUniforms.rendererSizeTimePad1.z
 
 layout(location = 0) in vec3 in_vertPosViewSpace;
 layout(location = 1) flat in float in_alpha;
@@ -69,9 +69,9 @@ void particleAlpha(in sampler2D tex, in float alpha)
 void particleSoftTextureAlpha(in sampler2D depthMap, in sampler2D tex,
 	in float alpha)
 {
-	const vec2 screenSize = vec2(
-		1.0 / float(ANKI_RENDERER_WIDTH),
-		1.0 / float(ANKI_RENDERER_HEIGHT));
+	vec2 screenSize = vec2(
+		1.0 / u_lightingUniforms.rendererSizeTimePad1.x,
+		1.0 / u_lightingUniforms.rendererSizeTimePad1.y);
 
 	float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
 
@@ -104,9 +104,9 @@ void particleTextureAlpha(in sampler2D tex, in float alpha)
 void particleSoftColorAlpha(in sampler2D depthMap, in vec3 icolor,
 	in float alpha)
 {
-	const vec2 screenSize = vec2(
-		1.0 / float(ANKI_RENDERER_WIDTH),
-		1.0 / float(ANKI_RENDERER_HEIGHT));
+	vec2 screenSize = vec2(
+		1.0 / u_lightingUniforms.rendererSizeTimePad1.x,
+		1.0 / u_lightingUniforms.rendererSizeTimePad1.y);
 
 	float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
 
@@ -137,9 +137,9 @@ vec3 computeLightColor(vec3 diffCol)
 		fragPos.z = u_lightingUniforms.projectionParams.z
 			/ (u_lightingUniforms.projectionParams.w + depth);
 
-		const vec2 screenSize = vec2(
-			1.0 / float(ANKI_RENDERER_WIDTH),
-			1.0 / float(ANKI_RENDERER_HEIGHT));
+		vec2 screenSize = vec2(
+			1.0 / u_lightingUniforms.rendererSizeTimePad1.x,
+			1.0 / u_lightingUniforms.rendererSizeTimePad1.y);
 
 		vec2 ndc = gl_FragCoord.xy * screenSize * 2.0 - 1.0;
 
@@ -267,8 +267,8 @@ void particleAnimatedTextureAlphaLight(sampler2DArray tex, float alpha,
 void fog(in sampler2D depthMap, in vec3 color, in float fogScale)
 {
 	const vec2 screenSize = vec2(
-		1.0 / float(ANKI_RENDERER_WIDTH),
-		1.0 / float(ANKI_RENDERER_HEIGHT));
+		1.0 / u_lightingUniforms.rendererSizeTimePad1.x,
+		1.0 / u_lightingUniforms.rendererSizeTimePad1.y);
 
 	vec2 texCoords = gl_FragCoord.xy * screenSize;
 	float depth = texture(depthMap, texCoords).r;

+ 12 - 1
shaders/FsCommonVert.glsl

@@ -6,6 +6,16 @@
 // Common code for all vertex shaders of FS
 #pragma anki include "shaders/MsFsCommon.glsl"
 
+// Global resources
+#define LIGHT_SET 1
+#define LIGHT_SS_BINDING 0
+#define LIGHT_TEX_BINDING 1
+#pragma anki include "shaders/LightResources.glsl"
+#undef LIGHT_SET
+#undef LIGHT_SS_BINDING
+#undef LIGHT_TEX_BINDING
+
+// 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;
@@ -46,7 +56,8 @@ void particle(in mat4 mvp)
 {
 	gl_Position = mvp * vec4(in_position, 1);
 	out_alpha = in_alpha;
-	gl_PointSize = in_scale * float(ANKI_RENDERER_WIDTH) / gl_Position.w;
+	gl_PointSize =
+		in_scale * u_lightingUniforms.rendererSizeTimePad1.x / gl_Position.w;
 }
 
 //==============================================================================

+ 0 - 6
shaders/IsLp.frag.glsl

@@ -160,12 +160,6 @@ void main()
 			* (att * spot * max(subsurface, lambert * shadow));
 	}
 
-#if GROUND_LIGHT
-	out_color +=
-		max(dot(normal, u_lightingUniforms.groundLightDirTime.xyz), 0.0)
-		* vec3(0.01, 0.001, 0.001);
-#endif
-
 #if IR == 1
 	{
 		float reflLod = float(IR_MIPMAP_COUNT) * roughness;

+ 1 - 1
shaders/LightResources.glsl

@@ -13,7 +13,7 @@ struct LightingUniforms
 {
 	vec4 projectionParams;
 	vec4 sceneAmbientColor;
-	vec4 groundLightDirTime;
+	vec4 rendererSizeTimePad1;
 	vec4 nearFarClustererDivisor;
 	mat4 viewMat;
 	uvec4 tileCountPad1;

+ 11 - 3
src/renderer/Ir.cpp

@@ -141,7 +141,7 @@ Error Ir::init(const ConfigSet& initializer)
 	config.set("pps.ssao.enabled", false);
 	config.set("pps.sslr.enabled", false);
 	config.set("renderingQuality", 1.0);
-	config.set("clusterSizeZ", 1); // XXX A bug if more. Fix it
+	config.set("clusterSizeZ", 4); // XXX A bug if more. Fix it
 	config.set("width", m_fbSize);
 	config.set("height", m_fbSize);
 	config.set("lodDistance", 10.0);
@@ -340,7 +340,7 @@ void Ir::binProbe(const SceneNode& node, U index, IrBuildContext& ctx)
 Error Ir::renderReflection(SceneNode& node, ShaderReflectionProbe& shaderProb)
 {
 	const FrustumComponent& frc = m_r->getActiveFrustumComponent();
-	const ReflectionProbeComponent& reflc =
+	ReflectionProbeComponent& reflc =
 		node.getComponent<ReflectionProbeComponent>();
 
 	// Get cache entry
@@ -354,7 +354,7 @@ Error Ir::renderReflection(SceneNode& node, ShaderReflectionProbe& shaderProb)
 	shaderProb.m_cubemapIndex = entry;
 
 	// Render cubemap
-	if(render)
+	if(reflc.getMarkedForRendering())
 	{
 		for(U i = 0; i < 6; ++i)
 		{
@@ -382,6 +382,14 @@ Error Ir::renderReflection(SceneNode& node, ShaderReflectionProbe& shaderProb)
 				cmdb[j]->flush();
 			}
 		}
+
+		reflc.setMarkedForRendering(false);
+	}
+
+	// If you need to render it mark it for the next frame
+	if(render)
+	{
+		reflc.setMarkedForRendering(true);
 	}
 
 	return ErrorCode::NONE;

+ 3 - 13
src/renderer/Is.cpp

@@ -53,7 +53,7 @@ struct ShaderCommonUniforms
 {
 	Vec4 m_projectionParams;
 	Vec4 m_sceneAmbientColor;
-	Vec4 m_groundLightDirTime;
+	Vec4 m_rendererSizeTimePad1;
 	Vec4 m_nearFarClustererDivisor;
 	Mat4 m_viewMat;
 	UVec4 m_tileCount;
@@ -799,18 +799,8 @@ void Is::updateCommonBlock(CommandBufferPtr& cmdb, const FrustumComponent& fr)
 		m_r->getClusterer().getDivisor(),
 		0.0);
 
-	Vec3 groundLightDir;
-	if(m_groundLightEnabled)
-	{
-		const Mat4& viewMat = m_frc->getViewMatrix();
-		blk->m_groundLightDirTime =
-			Vec4(-viewMat.getColumn(1).xyz(), HighRezTimer::getCurrentTime());
-	}
-	else
-	{
-		blk->m_groundLightDirTime =
-			Vec4(Vec3(0.0), HighRezTimer::getCurrentTime());
-	}
+	blk->m_rendererSizeTimePad1 = Vec4(m_r->getWidth(), m_r->getHeight(),
+		HighRezTimer::getCurrentTime(), 0.0);
 
 	blk->m_tileCount = UVec4(m_r->getTileCountXY(), m_r->getTileCount(), 0);
 }

+ 30 - 3
src/scene/ReflectionProbe.cpp

@@ -14,9 +14,13 @@
 namespace anki {
 
 //==============================================================================
-// ReflectionProbeMoveFeedbackComponent                                        =
+// Misc                                                                        =
 //==============================================================================
 
+const FrustumComponent::VisibilityTestFlag FRUSTUM_TEST_FLAGS =
+	FrustumComponent::VisibilityTestFlag::TEST_RENDER_COMPONENTS
+	| FrustumComponent::VisibilityTestFlag::TEST_LIGHT_COMPONENTS;
+
 /// Feedback component
 class ReflectionProbeMoveFeedbackComponent: public SceneComponent
 {
@@ -98,8 +102,7 @@ Error ReflectionProbe::create(const CString& name, F32 radius)
 			this, &m_cubeSides[i].m_frustum);
 
 		frc->setEnabledVisibilityTests(
-			FrustumComponent::VisibilityTestFlag::TEST_RENDER_COMPONENTS
-			| FrustumComponent::VisibilityTestFlag::TEST_LIGHT_COMPONENTS);
+			FrustumComponent::VisibilityTestFlag::TEST_NONE);
 
 		addComponent(frc, true);
 	}
@@ -152,4 +155,28 @@ void ReflectionProbe::onMoveUpdate(MoveComponent& move)
 	reflc.setPosition(move.getWorldTransform().getOrigin());
 }
 
+//==============================================================================
+Error ReflectionProbe::frameUpdate(F32 prevUpdateTime, F32 crntTime)
+{
+	// Check the reflection probe component and if it's marked for rendering
+	// enable the frustum components
+	const ReflectionProbeComponent& reflc =
+		getComponent<ReflectionProbeComponent>();
+
+	FrustumComponent::VisibilityTestFlag testFlags =
+		reflc.getMarkedForRendering()
+		? FRUSTUM_TEST_FLAGS
+		: FrustumComponent::VisibilityTestFlag::TEST_NONE;
+
+	Error err = iterateComponentsOfType<FrustumComponent>(
+		[testFlags](FrustumComponent& frc) -> Error
+	{
+		frc.setEnabledVisibilityTests(testFlags);
+		return ErrorCode::NONE;
+	});
+	(void)err;
+
+	return ErrorCode::NONE;
+}
+
 } // end namespace anki