From 90d7d405711de8920c9823bdeb23842edb29efb2 Mon Sep 17 00:00:00 2001
From: Alexander Gauggel <agauggel@uni-koblenz.de>
Date: Wed, 11 Aug 2021 15:57:14 +0200
Subject: [PATCH] [#106] Add motion blur early out for barely moving pixels

---
 .../resources/shaders/motionBlurDummy.comp          | 10 ++++++++++
 projects/indirect_dispatch/src/App.cpp              | 13 +++++++++----
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/projects/indirect_dispatch/resources/shaders/motionBlurDummy.comp b/projects/indirect_dispatch/resources/shaders/motionBlurDummy.comp
index 55ea9e0c..a57319b9 100644
--- a/projects/indirect_dispatch/resources/shaders/motionBlurDummy.comp
+++ b/projects/indirect_dispatch/resources/shaders/motionBlurDummy.comp
@@ -11,6 +11,7 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
 layout( push_constant ) uniform constants{
     // computed from delta time and shutter speed
     float motionFactor;
+    float minVelocity;
 };
 
 
@@ -24,6 +25,15 @@ void main(){
     vec2    uv          = vec2(coord) / textureRes;
 
     vec2    motion      = texture(sampler2D(inMotion, textureSampler), uv).rg;
+    float   velocity    = length(motion);
+    
+    // early out on little movement
+    if(velocity < minVelocity){
+        vec3 color = texture(sampler2D(inColor, textureSampler), uv).rgb;
+        imageStore(outImage, coord, vec4(color, 0.f));
+        return;
+    }
+    
     motion              *= motionFactor;
 
     vec3        color       = vec3(0);
diff --git a/projects/indirect_dispatch/src/App.cpp b/projects/indirect_dispatch/src/App.cpp
index 9bcf3a57..e8987aa0 100644
--- a/projects/indirect_dispatch/src/App.cpp
+++ b/projects/indirect_dispatch/src/App.cpp
@@ -101,8 +101,9 @@ void App::run() {
 	eDebugView          debugView       = eDebugView::None;
 	eMotionBlurInput    motionBlurInput = eMotionBlurInput::MotionVectorMaxTileNeighbourhood;
 
-	float   objectVerticalSpeed       = 0.005;
-	int     cameraShutterSpeedInverse = 48;
+	float   objectVerticalSpeed         = 0.005;
+	float   motionBlurMinVelocity       = 0.001;
+	int     cameraShutterSpeedInverse   = 48;
 
 	glm::mat4 mvpPrevious               = glm::mat4(1.f);
 	glm::mat4 viewProjectionPrevious    = m_cameraManager.getActiveCamera().getMVP();
@@ -285,8 +286,11 @@ void App::run() {
 		const float fDeltatimeSeconds       = microsecondToSecond * std::chrono::duration_cast<std::chrono::microseconds>(frameEndTime - frameStartTime).count();
 		const float motionBlurMotionFactor  = 1 / (fDeltatimeSeconds * cameraShutterSpeedInverse);
 
-		vkcv::PushConstants motionBlurPushConstants(sizeof(float));
-		motionBlurPushConstants.appendDrawcall(motionBlurMotionFactor);
+		vkcv::PushConstants motionBlurPushConstants(sizeof(float) * 2);
+
+		float motionBlurConstantData[2] = { motionBlurMotionFactor, motionBlurMinVelocity };
+
+		motionBlurPushConstants.appendDrawcall(motionBlurConstantData);
 
 		m_core.recordComputeDispatchToCmdStream(
 			cmdStream,
@@ -350,6 +354,7 @@ void App::run() {
 
 		ImGui::InputFloat("Object movement speed", &objectVerticalSpeed);
 		ImGui::InputInt("Camera shutter speed inverse", &cameraShutterSpeedInverse);
+		ImGui::DragFloat("Motion blur min velocity", &motionBlurMinVelocity, 0.01, 0, 1);
 
 		ImGui::End();
 		gui.endGUI();
-- 
GitLab