From 4e5f708ba413f1c265a8cf62fea6faa79bfe2ac1 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 11 Aug 2019 10:43:43 +0300
Subject: [PATCH] Fix for incorrect energy memory

---
 applications/reconstruct/src/dibr.cu | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/applications/reconstruct/src/dibr.cu b/applications/reconstruct/src/dibr.cu
index a91ae7b87..52dd31965 100644
--- a/applications/reconstruct/src/dibr.cu
+++ b/applications/reconstruct/src/dibr.cu
@@ -13,7 +13,7 @@
 #define WARP_SIZE 32
 #define DEPTH_THRESHOLD 0.05f
 #define UPSAMPLE_MAX 60
-#define MAX_ITERATIONS 32
+#define MAX_ITERATIONS 64  // Note: Must be multiple of 32
 #define SPATIAL_SMOOTHING 0.005f
 
 using ftl::cuda::TextureObject;
@@ -476,15 +476,16 @@ __device__ inline float warpMin(float e) {
 	//if (y == 200) printf("interval: %f\n", maxDepth);
 
 
-	float maxenergy = 0.0f;
+	float maxenergy = -1.0f;
 	float bestdepth = 0.0f;
 
 	// Search for best or threshold energy
 	for (int k=lane; k<MAX_ITERATIONS; k+=WARP_SIZE) {
 		const float3 nearest = params.camera.kinectDepthToSkeleton(x,y,minDepth+float(k)*interval);
 		const float myenergy = ftl::cuda::mls_point_energy<MAX_NEIGHBORS_2>(neighborhood_cache[warp], nearest, nidx[warp], SPATIAL_SMOOTHING);
-		maxenergy = warpMax(max(myenergy, maxenergy));
-		bestdepth = (myenergy == maxenergy) ? nearest.z : 0.0f;
+		const float newenergy = warpMax(max(myenergy, maxenergy));
+		bestdepth = (myenergy == newenergy) ? nearest.z : (newenergy > maxenergy) ? 0.0f : bestdepth;
+		maxenergy = newenergy;
 	}
 
 	// Search for first energy maximum above a threshold
@@ -494,8 +495,8 @@ __device__ inline float warpMin(float e) {
 		const unsigned int cy = y;
 		if (bestdepth > params.camera.m_sensorDepthWorldMin && bestdepth < params.camera.m_sensorDepthWorldMax && cx < depth.width() && cy < depth.height()) {
 			// Transform estimated point to virtual cam space and output z
-			//atomicMin(&depth(cx,cy), bestdepth * 1000.0f);
-			depth(cx,cy) = bestdepth * 1000.0f;
+			atomicMin(&depth(cx,cy), bestdepth * 1000.0f);
+			//depth(cx,cy) = bestdepth * 1000.0f;
 		}
 	}
 }
-- 
GitLab