From 1db370da254ec61a2c14c3bb04fcaec30e608b5d Mon Sep 17 00:00:00 2001
From: Alexander Gauggel <agauggel@uni-koblenz.de>
Date: Tue, 21 Sep 2021 12:22:15 +0200
Subject: [PATCH] [#115] Fix border handling of velocity min/max computation

---
 .../assets/shaders/motionVectorMinMax.comp               | 9 +++++++++
 .../assets/shaders/motionVectorMinMaxNeighbourhood.comp  | 8 ++++++++
 2 files changed, 17 insertions(+)

diff --git a/projects/indirect_dispatch/assets/shaders/motionVectorMinMax.comp b/projects/indirect_dispatch/assets/shaders/motionVectorMinMax.comp
index 4ad350b0..06b1b98f 100644
--- a/projects/indirect_dispatch/assets/shaders/motionVectorMinMax.comp
+++ b/projects/indirect_dispatch/assets/shaders/motionVectorMinMax.comp
@@ -12,6 +12,7 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
 void main(){
     
     ivec2 outImageRes       = imageSize(outMotionMax);
+    ivec2 inImageRes        = textureSize(sampler2D(inMotion, textureSampler), 0);
     ivec2 motionTileCoord   = ivec2(gl_GlobalInvocationID.xy);
     
     if(any(greaterThanEqual(motionTileCoord, outImageRes)))
@@ -28,6 +29,14 @@ void main(){
     for(int x = 0; x < motionTileSize; x++){
         for(int y = 0; y < motionTileSize; y++){
             ivec2   sampleCoord     = motionBufferBaseCoord + ivec2(x, y);
+            
+            bool sampleIsOutsideImage = false;
+            sampleIsOutsideImage = sampleIsOutsideImage || any(greaterThanEqual(sampleCoord, inImageRes));
+            sampleIsOutsideImage = sampleIsOutsideImage || any(lessThan(sampleCoord, ivec2(0)));
+            
+            if(sampleIsOutsideImage)
+                continue;
+            
             vec2    motionSample    = texelFetch(sampler2D(inMotion, textureSampler), sampleCoord, 0).rg;
             float   velocitySample  = length(motionSample);
             
diff --git a/projects/indirect_dispatch/assets/shaders/motionVectorMinMaxNeighbourhood.comp b/projects/indirect_dispatch/assets/shaders/motionVectorMinMaxNeighbourhood.comp
index 4d6e7c0a..3f836341 100644
--- a/projects/indirect_dispatch/assets/shaders/motionVectorMinMaxNeighbourhood.comp
+++ b/projects/indirect_dispatch/assets/shaders/motionVectorMinMaxNeighbourhood.comp
@@ -12,6 +12,7 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
 void main(){
     
     ivec2 outImageRes       = imageSize(outMotionMaxNeighbourhood);
+    ivec2 inImageRes        = textureSize(sampler2D(inMotionMax, textureSampler), 0);
     ivec2 motionTileCoord   = ivec2(gl_GlobalInvocationID.xy);
     
     if(any(greaterThanEqual(motionTileCoord, outImageRes)))
@@ -27,6 +28,13 @@ void main(){
         for(int y = -1; y <= 1; y++){
             ivec2   sampleCoord         = motionTileCoord + ivec2(x, y);
             
+            bool sampleIsOutsideImage = false;
+            sampleIsOutsideImage = sampleIsOutsideImage || any(greaterThanEqual(sampleCoord, inImageRes));
+            sampleIsOutsideImage = sampleIsOutsideImage || any(lessThan(sampleCoord, ivec2(0)));
+            
+            if(sampleIsOutsideImage)
+                continue;
+            
             vec2    motionSampleMax     = texelFetch(sampler2D(inMotionMax, textureSampler), sampleCoord, 0).rg;
             float   velocitySampleMax   = length(motionSampleMax);
             
-- 
GitLab