From 4932f9e49b93c1cee9150dfecf4a53e004609256 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20M=C3=BCller?= <tmueller@nvidia.com>
Date: Thu, 9 Feb 2023 09:42:29 +0100
Subject: [PATCH] Fix broken DLSS + `testbed.snap_to_pixel_centers = True`

---
 include/neural-graphics-primitives/common_device.cuh | 10 +++++-----
 include/neural-graphics-primitives/render_buffer.h   |  2 +-
 src/render_buffer.cu                                 |  4 ++--
 src/testbed.cu                                       |  2 +-
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/include/neural-graphics-primitives/common_device.cuh b/include/neural-graphics-primitives/common_device.cuh
index 361c62b..3ca1af0 100644
--- a/include/neural-graphics-primitives/common_device.cuh
+++ b/include/neural-graphics-primitives/common_device.cuh
@@ -613,15 +613,15 @@ inline NGP_HOST_DEVICE Eigen::Vector2f motion_vector(
 	const Foveation& prev_foveation = {},
 	const Lens& lens = {}
 ) {
-	Ray ray = pixel_to_ray(
+	Eigen::Vector2f pxf = pixel.cast<float>() + ld_random_pixel_offset(snap_to_pixel_centers ? 0 : sample_index);
+	Ray ray = uv_to_ray(
 		sample_index,
-		pixel,
+		pxf.cwiseQuotient(resolution.cast<float>()),
 		resolution,
 		focal_length,
 		camera,
 		screen_center,
 		parallax_shift,
-		snap_to_pixel_centers,
 		0.0f,
 		1.0f,
 		0.0f,
@@ -630,7 +630,7 @@ inline NGP_HOST_DEVICE Eigen::Vector2f motion_vector(
 		lens
 	);
 
-	Eigen::Vector2f prev_pixel = pos_to_pixel(
+	Eigen::Vector2f prev_pxf = pos_to_pixel(
 		ray(depth),
 		resolution,
 		focal_length,
@@ -641,7 +641,7 @@ inline NGP_HOST_DEVICE Eigen::Vector2f motion_vector(
 		lens
 	);
 
-	return prev_pixel - (pixel.cast<float>() + ld_random_pixel_offset(sample_index));
+	return prev_pxf - pxf;
 }
 
 // Maps view-space depth (physical units) in the range [znear, zfar] hyperbolically to
diff --git a/include/neural-graphics-primitives/render_buffer.h b/include/neural-graphics-primitives/render_buffer.h
index 0e51364..a9b3bce 100644
--- a/include/neural-graphics-primitives/render_buffer.h
+++ b/include/neural-graphics-primitives/render_buffer.h
@@ -231,7 +231,7 @@ public:
 
 	void accumulate(float exposure, cudaStream_t stream);
 
-	void tonemap(float exposure, const Eigen::Array4f& background_color, EColorSpace output_color_space, float znear, float zfar, cudaStream_t stream);
+	void tonemap(float exposure, const Eigen::Array4f& background_color, EColorSpace output_color_space, float znear, float zfar, bool snap_to_pixel_centers, cudaStream_t stream);
 
 	void overlay_image(
 		float alpha,
diff --git a/src/render_buffer.cu b/src/render_buffer.cu
index ab01d4d..f52eb6c 100644
--- a/src/render_buffer.cu
+++ b/src/render_buffer.cu
@@ -659,7 +659,7 @@ void CudaRenderBuffer::accumulate(float exposure, cudaStream_t stream) {
 	++m_spp;
 }
 
-void CudaRenderBuffer::tonemap(float exposure, const Array4f& background_color, EColorSpace output_color_space, float znear, float zfar, cudaStream_t stream) {
+void CudaRenderBuffer::tonemap(float exposure, const Array4f& background_color, EColorSpace output_color_space, float znear, float zfar, bool snap_to_pixel_centers, cudaStream_t stream) {
 	assert(m_dlss || out_resolution() == in_resolution());
 
 	auto res = in_resolution();
@@ -688,7 +688,7 @@ void CudaRenderBuffer::tonemap(float exposure, const Array4f& background_color,
 			res,
 			output_color_space == EColorSpace::Linear, /* HDR mode */
 			m_dlss_sharpening,
-			Vector2f::Constant(0.5f) - ld_random_pixel_offset(sample_index), /* jitter offset in [-0.5, 0.5] */
+			Vector2f::Constant(0.5f) - ld_random_pixel_offset(snap_to_pixel_centers ? 0 : sample_index), /* jitter offset in [-0.5, 0.5] */
 			sample_index == 0 /* reset history */
 		);
 
diff --git a/src/testbed.cu b/src/testbed.cu
index e442bd0..b30526a 100644
--- a/src/testbed.cu
+++ b/src/testbed.cu
@@ -4317,7 +4317,7 @@ void Testbed::render_frame_epilogue(
 	}
 
 	render_buffer.accumulate(m_exposure, stream);
-	render_buffer.tonemap(m_exposure, m_background_color, output_color_space, m_ndc_znear, m_ndc_zfar, stream);
+	render_buffer.tonemap(m_exposure, m_background_color, output_color_space, m_ndc_znear, m_ndc_zfar, m_snap_to_pixel_centers, stream);
 
 	if (m_testbed_mode == ETestbedMode::Nerf) {
 		// Overlay the ground truth image if requested
-- 
GitLab