From 3aa777a713b814606487bdac48db6380f1aefca2 Mon Sep 17 00:00:00 2001
From: JamesPerlman <jam.e.perl@gmail.com>
Date: Tue, 18 Oct 2022 21:51:28 -0700
Subject: [PATCH] emit ray origins from start of near plane to preserve quality

---
 include/neural-graphics-primitives/common_device.cuh | 4 ++++
 src/testbed_nerf.cu                                  | 3 ++-
 src/testbed_sdf.cu                                   | 2 +-
 src/testbed_volume.cu                                | 2 +-
 4 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/include/neural-graphics-primitives/common_device.cuh b/include/neural-graphics-primitives/common_device.cuh
index 5029cb0..9a33d7e 100644
--- a/include/neural-graphics-primitives/common_device.cuh
+++ b/include/neural-graphics-primitives/common_device.cuh
@@ -266,6 +266,7 @@ inline NGP_HOST_DEVICE Ray pixel_to_ray(
 	const Eigen::Vector2f& screen_center,
 	const Eigen::Vector3f& parallax_shift,
 	bool snap_to_pixel_centers = false,
+	float near_distance = 0.0f,
 	float focus_z = 1.0f,
 	float aperture_size = 0.0f,
 	const Lens& lens = {},
@@ -303,6 +304,7 @@ inline NGP_HOST_DEVICE Ray pixel_to_ray(
 
 	Eigen::Vector3f origin = camera_matrix.block<3, 3>(0, 0) * head_pos + camera_matrix.col(3);
 	if (aperture_size == 0.0f) {
+		origin += dir * near_distance;
 		return {origin, dir};
 	}
 
@@ -310,6 +312,7 @@ inline NGP_HOST_DEVICE Ray pixel_to_ray(
 	Eigen::Vector2f blur = aperture_size * square2disk_shirley(ld_random_val_2d(spp, (uint32_t)pixel.x() * 19349663 + (uint32_t)pixel.y() * 96925573) * 2.0f - Eigen::Vector2f::Ones());
 	origin += camera_matrix.block<3, 2>(0, 0) * blur;
 	dir = (lookat - origin) / focus_z;
+	origin += dir * near_distance;
 
 	return {origin, dir};
 }
@@ -371,6 +374,7 @@ inline NGP_HOST_DEVICE Eigen::Vector2f motion_vector_3d(
 		screen_center,
 		parallax_shift,
 		snap_to_pixel_centers,
+		0.0f,
 		1.0f,
 		0.0f,
 		lens,
diff --git a/src/testbed_nerf.cu b/src/testbed_nerf.cu
index fdf92a6..7d7eb65 100644
--- a/src/testbed_nerf.cu
+++ b/src/testbed_nerf.cu
@@ -1837,6 +1837,7 @@ __global__ void init_rays_with_payload_kernel_nerf(
 		screen_center,
 		parallax_shift,
 		snap_to_pixel_centers,
+		near_distance,
 		plane_z,
 		aperture_size,
 		lens,
@@ -1867,7 +1868,7 @@ __global__ void init_rays_with_payload_kernel_nerf(
 		framebuffer[idx] = read_envmap(envmap_data, envmap_resolution, ray.d);
 	}
 
-	float t = fmaxf(render_aabb.ray_intersect(render_aabb_to_local * ray.o, render_aabb_to_local * ray.d).x(), near_distance) + 1e-6f;
+	float t = fmaxf(render_aabb.ray_intersect(render_aabb_to_local * ray.o, render_aabb_to_local * ray.d).x(), 0.0f) + 1e-6f;
 
 	if (!render_aabb.contains(render_aabb_to_local * (ray.o + ray.d * t))) {
 		payload.origin = ray.o;
diff --git a/src/testbed_sdf.cu b/src/testbed_sdf.cu
index bdb1181..2e42b3f 100644
--- a/src/testbed_sdf.cu
+++ b/src/testbed_sdf.cu
@@ -525,7 +525,7 @@ __global__ void init_rays_with_payload_kernel_sdf(
 		aperture_size = 0.0;
 	}
 
-	Ray ray = pixel_to_ray(sample_index, {x, y}, resolution, focal_length, camera_matrix, screen_center, parallax_shift, snap_to_pixel_centers, plane_z, aperture_size);
+	Ray ray = pixel_to_ray(sample_index, {x, y}, resolution, focal_length, camera_matrix, screen_center, parallax_shift, snap_to_pixel_centers, 0.0f, plane_z, aperture_size);
 
 	distances[idx] = 10000.0f;
 
diff --git a/src/testbed_volume.cu b/src/testbed_volume.cu
index f84eb28..36fa323 100644
--- a/src/testbed_volume.cu
+++ b/src/testbed_volume.cu
@@ -239,7 +239,7 @@ __global__ void init_rays_volume(
 	if (plane_z < 0) {
 		aperture_size = 0.0;
 	}
-	Ray ray = pixel_to_ray(sample_index, {x, y}, resolution, focal_length, camera_matrix, screen_center, parallax_shift, snap_to_pixel_centers, plane_z, aperture_size);
+	Ray ray = pixel_to_ray(sample_index, {x, y}, resolution, focal_length, camera_matrix, screen_center, parallax_shift, snap_to_pixel_centers, 0.0f, plane_z, aperture_size);
 	ray.d = ray.d.normalized();
 	auto box_intersection = aabb.ray_intersect(ray.o, ray.d);
 	float t = max(box_intersection.x(), 0.0f);
-- 
GitLab