From 29681c2a7afdcc4ab38d14ea5bcf2dd2df9f6194 Mon Sep 17 00:00:00 2001 From: Jefferson Amstutz Date: Fri, 30 Oct 2026 18:00:12 -0500 Subject: [PATCH] Honor camera near/far and report the Far Limit as Miss Depth ANARI 1.1 gives perspective and orthographic cameras `near`/`far` clip distances but VisRTX ignored them, and the spec never says what `channel.depth` holds for a pixel with no hit. The renderers disagreed: fast/interactive left FLT_MAX, while debug/quality wrote the primary ray's `t.upper`, which a cutting plane could shorten to the cut distance (or 0 for a ray that never enters the visible half-space). Cameras now read `near`/`far` as clip planes measured from `position` along `direction`, and every primary ray is bounded by them. A Miss reports that ray's Far Limit: the distance along it to the far plane (far / cos for perspective, exactly far for orthographic), the ray buffer's `ray.tmax`, or FLT_MAX when nothing bounds the ray. Cutting planes no longer change what a Miss reports. An invalid ray buffer now misses with an empty interval at FLT_MAX instead of [0, 0]. Invalid near/far pairs warn and are ignored. ADR 0011 records why `far` is a plane and why unset `far` reports FLT_MAX rather than +inf; the render-pipeline glossary gains Miss, Far Limit and Miss Depth. TestCameraNearFar covers every renderer subtype. --- devices/rtx/apps/tests/api/CMakeLists.txt | 1 + .../rtx/apps/tests/api/TestCameraNearFar.cpp | 462 ++++++++++++++++++ devices/rtx/device/camera/Camera.cpp | 30 ++ devices/rtx/device/camera/Camera.h | 6 + devices/rtx/device/camera/Orthographic.cpp | 1 + devices/rtx/device/camera/Perspective.cpp | 1 + devices/rtx/device/gpu/cameraCreateRay.h | 16 +- devices/rtx/device/gpu/gpu_objects.h | 4 + .../rtx/device/gpu/renderer/raygen_helpers.h | 5 +- devices/rtx/device/renderer/CONTEXT.md | 17 + devices/rtx/device/renderer/Quality_ptx.cu | 8 +- devices/rtx/device/renderer/Test_ptx.cu | 2 +- ...011-camera-far-limit-defines-miss-depth.md | 26 + 13 files changed, 573 insertions(+), 6 deletions(-) create mode 100644 devices/rtx/apps/tests/api/TestCameraNearFar.cpp create mode 100644 devices/rtx/docs/adr/0011-camera-far-limit-defines-miss-depth.md diff --git a/devices/rtx/apps/tests/api/CMakeLists.txt b/devices/rtx/apps/tests/api/CMakeLists.txt index 4784f628d..359423ef5 100644 --- a/devices/rtx/apps/tests/api/CMakeLists.txt +++ b/devices/rtx/apps/tests/api/CMakeLists.txt @@ -67,6 +67,7 @@ make_test(TestPrimitiveInterior) make_test(TestGroundSphereShadows) make_test(TestPowerLightPick) make_test(TestRayBufferCamera) +make_test(TestCameraNearFar) make_test(TestPowerLightPickEnv) make_test(TestEmissiveGeometryLight) make_test(TestQuadLightSide) diff --git a/devices/rtx/apps/tests/api/TestCameraNearFar.cpp b/devices/rtx/apps/tests/api/TestCameraNearFar.cpp new file mode 100644 index 000000000..729561a0f --- /dev/null +++ b/devices/rtx/apps/tests/api/TestCameraNearFar.cpp @@ -0,0 +1,462 @@ +/* + * Copyright (c) 2019-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +// Camera 'near'/'far' clip planes and Miss Depth (ADR 0011), on every renderer. +// A pixel whose camera ray hits nothing reports the camera's Far Limit as its +// depth: the distance along that ray to the 'far' plane, the ray buffer's +// 'ray.tmax', or FLT_MAX when nothing bounds the ray. Clipping the ray short +// any other way (a renderer cutting plane, an invalid ray buffer) does not +// change what a Miss reports. + +// anari_cpp +#define ANARI_EXTENSION_UTILITY_IMPL +#include +#include +// VisRTX +#include +// std +#include +#include +#include +#include +#include +#include +#include +#include + +using uvec2 = std::array; +using vec3 = std::array; +using vec4 = std::array; + +static int g_clipWarnings = 0; + +static void statusFunc(const void *, + ANARIDevice, + ANARIObject source, + ANARIDataType, + ANARIStatusSeverity severity, + ANARIStatusCode, + const char *message) +{ + if (severity == ANARI_SEVERITY_FATAL_ERROR) { + fprintf(stderr, "[FATAL][%p] %s\n", source, message); + std::exit(1); + } else if (severity == ANARI_SEVERITY_ERROR) + fprintf(stderr, "[ERROR][%p] %s\n", source, message); + else if (severity == ANARI_SEVERITY_WARNING && std::strstr(message, "'near'") + && std::strstr(message, "'far'")) + g_clipWarnings++; +} + +static constexpr uvec2 IMAGE_SIZE = {64, 48}; +static constexpr unsigned NUM_PIXELS = IMAGE_SIZE[0] * IMAGE_SIZE[1]; +static constexpr unsigned MISS = ~0u; +static constexpr float FLOAT_MAX = std::numeric_limits::max(); + +static constexpr vec3 CAM_POS = {0.f, 0.f, 5.f}; +static constexpr vec3 CAM_DIR = {0.f, 0.f, -1.f}; +static constexpr vec3 CAM_UP = {0.f, 1.f, 0.f}; +static constexpr float CAM_FOVY = 0.8f; +static constexpr float ORTHO_HEIGHT = 6.f; + +// Sphere 0 sits ~4.4 from the camera; sphere 1 is larger, partly behind it, +// and ~12 away. 'far' between them clips sphere 1; 'near' between them clips +// sphere 0. +static constexpr float FAR_SPHERE_FRONT = 12.f; +static constexpr float CLIP_BETWEEN = 8.f; + +static vec3 operator+(vec3 a, vec3 b) +{ + return {a[0] + b[0], a[1] + b[1], a[2] + b[2]}; +} +static vec3 operator*(float s, vec3 a) +{ + return {s * a[0], s * a[1], s * a[2]}; +} +static float dot(vec3 a, vec3 b) +{ + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; +} +static vec3 cross(vec3 a, vec3 b) +{ + return {a[1] * b[2] - a[2] * b[1], + a[2] * b[0] - a[0] * b[2], + a[0] * b[1] - a[1] * b[0]}; +} +static vec3 normalize(vec3 a) +{ + return (1.f / std::sqrt(dot(a, a))) * a; +} + +struct Image +{ + std::vector depth; + std::vector primID; +}; + +static anari::World makeWorld(ANARIDevice device) +{ + const std::array centers = { + vec3{0.f, 0.f, 0.f}, vec3{1.5f, 0.f, -10.f}}; + const std::array radii = {0.6f, 3.f}; + + auto geom = anari::newObject(device, "sphere"); + anari::setParameterArray1D( + device, geom, "vertex.position", centers.data(), centers.size()); + anari::setParameterArray1D( + device, geom, "vertex.radius", radii.data(), radii.size()); + anari::commitParameters(device, geom); + + auto mat = anari::newObject(device, "matte"); + anari::commitParameters(device, mat); + + auto surface = anari::newObject(device); + anari::setAndReleaseParameter(device, surface, "geometry", geom); + anari::setAndReleaseParameter(device, surface, "material", mat); + anari::commitParameters(device, surface); + + auto world = anari::newObject(device); + anari::setParameterArray1D(device, world, "surface", &surface, 1); + anari::release(device, surface); + anari::commitParameters(device, world); + return world; +} + +static Image render(ANARIDevice device, + const char *rendererType, + anari::World world, + anari::Camera cam, + const vec4 *cutPlane = nullptr) +{ + auto renderer = anari::newObject(device, rendererType); + anari::setParameter(device, renderer, "pixelSamples", 1); + if (cutPlane) + anari::setParameter(device, renderer, "cutPlane", *cutPlane); + anari::commitParameters(device, renderer); + + auto frame = anari::newObject(device); + anari::setParameter(device, frame, "size", IMAGE_SIZE); + anari::setParameter(device, frame, "channel.color", ANARI_UFIXED8_RGBA_SRGB); + anari::setParameter(device, frame, "channel.depth", ANARI_FLOAT32); + anari::setParameter(device, frame, "channel.primitiveId", ANARI_UINT32); + anari::setParameter(device, frame, "world", world); + anari::setParameter(device, frame, "camera", cam); + anari::setAndReleaseParameter(device, frame, "renderer", renderer); + anari::commitParameters(device, frame); + + // The first sample of the first frame is pixel-centered for every camera. + anari::render(device, frame); + anari::wait(device, frame); + + Image img; + auto depth = anari::map(device, frame, "channel.depth"); + img.depth.assign(depth.data, depth.data + NUM_PIXELS); + anari::unmap(device, frame, "channel.depth"); + auto ids = anari::map(device, frame, "channel.primitiveId"); + img.primID.assign(ids.data, ids.data + NUM_PIXELS); + anari::unmap(device, frame, "channel.primitiveId"); + + anari::release(device, frame); + return img; +} + +static anari::Camera makeCamera(ANARIDevice device, const char *type) +{ + auto cam = anari::newObject(device, type); + anari::setParameter(device, cam, "position", CAM_POS); + anari::setParameter(device, cam, "direction", CAM_DIR); + anari::setParameter(device, cam, "up", CAM_UP); + anari::setParameter( + device, cam, "aspect", IMAGE_SIZE[0] / float(IMAGE_SIZE[1])); + if (std::strcmp(type, "perspective") == 0) + anari::setParameter(device, cam, "fovy", CAM_FOVY); + else + anari::setParameter(device, cam, "height", ORTHO_HEIGHT); + return cam; +} + +// Pixel-center ray directions of the perspective camera, row-major. +static std::vector perspectiveDirections() +{ + const float h = 2.f * std::tan(0.5f * CAM_FOVY); + const float w = h * IMAGE_SIZE[0] / float(IMAGE_SIZE[1]); + const vec3 du = w * normalize(cross(CAM_DIR, CAM_UP)); + const vec3 dv = h * normalize(cross(du, CAM_DIR)); + const vec3 d00 = CAM_DIR + -0.5f * du + -0.5f * dv; + + std::vector dirs(NUM_PIXELS); + for (unsigned y = 0; y < IMAGE_SIZE[1]; y++) { + for (unsigned x = 0; x < IMAGE_SIZE[0]; x++) { + const float sx = (x + 0.5f) / IMAGE_SIZE[0]; + const float sy = (y + 0.5f) / IMAGE_SIZE[1]; + dirs[y * IMAGE_SIZE[0] + x] = normalize(d00 + sx * du + sy * dv); + } + } + return dirs; +} + +// Far Limit of each perspective pixel ray for a far plane at 'farPlane'. +static std::vector perspectiveFarLimits(float farPlane) +{ + std::vector limits; + for (const vec3 &d : perspectiveDirections()) + limits.push_back(farPlane / dot(d, CAM_DIR)); + return limits; +} + +static bool nearlyEqual(float a, float b) +{ + return std::fabs(a - b) <= 1e-4f * std::fmax(1.f, std::fabs(b)); +} + +struct Counts +{ + unsigned misses{0}; + unsigned badMissDepth{0}; + unsigned nearHits{0}; + unsigned farHits{0}; + unsigned badHitDepth{0}; +}; + +// Every Miss must report missDepth[i]; every hit must lie in [lo, hi]. +static Counts tally(const Image &img, + const std::vector &missDepth, + float lo = 0.f, + float hi = FLOAT_MAX) +{ + Counts c; + for (unsigned i = 0; i < NUM_PIXELS; i++) { + if (img.primID[i] == MISS) { + c.misses++; + c.badMissDepth += !nearlyEqual(img.depth[i], missDepth[i]); + } else { + c.nearHits += img.primID[i] == 0; + c.farHits += img.primID[i] == 1; + c.badHitDepth += img.depth[i] < lo || img.depth[i] > hi; + } + } + return c; +} + +static bool check(bool cond, const char *renderer, const char *what) +{ + printf(" %-12s %-50s %s\n", renderer, what, cond ? "ok" : "FAIL"); + return cond; +} + +static bool testRenderer( + ANARIDevice device, anari::World world, const char *rendererType) +{ + bool ok = true; + const std::vector unbounded(NUM_PIXELS, FLOAT_MAX); + const bool hasCutPlane = std::strcmp(rendererType, "debug") != 0; + + // 1. No 'far': nothing is clipped and a Miss reports FLT_MAX. + { + auto cam = makeCamera(device, "perspective"); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, rendererType, world, cam), unbounded); + anari::release(device, cam); + ok &= check(c.misses > 0 && c.nearHits > 0 && c.farHits > 0, + rendererType, + "unclipped: both spheres and misses visible"); + ok &= check(c.badMissDepth == 0, rendererType, "unclipped: Miss = FLT_MAX"); + } + + // 2. Perspective 'far' between the spheres: the far sphere is clipped and a + // Miss reports the distance to the far plane along its ray. + { + auto cam = makeCamera(device, "perspective"); + anari::setParameter(device, cam, "far", CLIP_BETWEEN); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, rendererType, world, cam), + perspectiveFarLimits(CLIP_BETWEEN), + 0.f, + CLIP_BETWEEN); + anari::release(device, cam); + ok &= check(c.nearHits > 0 && c.farHits == 0 && c.badHitDepth == 0, + rendererType, + "perspective far clips the far sphere"); + ok &= + check(c.badMissDepth == 0, rendererType, "perspective Miss = far/cos"); + } + + // 3. Perspective 'near' between the spheres: the near sphere is clipped. + { + auto cam = makeCamera(device, "perspective"); + anari::setParameter(device, cam, "near", CLIP_BETWEEN); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, rendererType, world, cam), + unbounded, + FAR_SPHERE_FRONT - 1.f); + anari::release(device, cam); + ok &= check(c.nearHits == 0 && c.farHits > 0 && c.badHitDepth == 0 + && c.badMissDepth == 0, + rendererType, + "perspective near clips the near sphere"); + } + + // 4. Orthographic 'far': every Miss reports exactly 'far'. + { + auto cam = makeCamera(device, "orthographic"); + anari::setParameter(device, cam, "far", CLIP_BETWEEN); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, rendererType, world, cam), + std::vector(NUM_PIXELS, CLIP_BETWEEN)); + anari::release(device, cam); + ok &= check(c.nearHits > 0 && c.farHits == 0 && c.badMissDepth == 0, + rendererType, + "orthographic Miss = far"); + } + + // 5. A cutting plane hiding everything past z = 2 turns every pixel into a + // Miss, which still reports the Far Limit, not the cut distance. + if (hasCutPlane) { + const vec4 cut = {0.f, 0.f, 1.f, -2.f}; + auto cam = makeCamera(device, "perspective"); + anari::setParameter(device, cam, "far", CLIP_BETWEEN); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, rendererType, world, cam, &cut), + perspectiveFarLimits(CLIP_BETWEEN)); + anari::release(device, cam); + ok &= check(c.misses == NUM_PIXELS && c.badMissDepth == 0, + rendererType, + "cutting plane Miss = Far Limit"); + } + + // 6. Ray buffer: a Miss reports its own ray.tmax (all short of the spheres). + { + const auto dirs = perspectiveDirections(); + std::vector tmax(NUM_PIXELS); + for (unsigned i = 0; i < NUM_PIXELS; i++) + tmax[i] = 1.f + float(i % 3); + + auto cam = anari::newObject(device, "rayBuffer"); + anari::setParameter(device, cam, "position", CAM_POS); + anari::setAndReleaseParameter(device, + cam, + "ray.dir", + anari::newArray2D(device, dirs.data(), IMAGE_SIZE[0], IMAGE_SIZE[1])); + anari::setAndReleaseParameter(device, + cam, + "ray.tmax", + anari::newArray2D(device, tmax.data(), IMAGE_SIZE[0], IMAGE_SIZE[1])); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, rendererType, world, cam), tmax); + anari::release(device, cam); + ok &= check(c.misses == NUM_PIXELS && c.badMissDepth == 0, + rendererType, + "ray buffer Miss = ray.tmax"); + } + + // 7. Ray buffer sized unlike the frame: every ray misses with FLT_MAX. + { + const std::vector dirs(4, CAM_DIR); + auto cam = anari::newObject(device, "rayBuffer"); + anari::setParameter(device, cam, "position", CAM_POS); + anari::setAndReleaseParameter( + device, cam, "ray.dir", anari::newArray2D(device, dirs.data(), 2, 2)); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, rendererType, world, cam), unbounded); + anari::release(device, cam); + ok &= check(c.misses == NUM_PIXELS && c.badMissDepth == 0, + rendererType, + "invalid ray buffer Miss = FLT_MAX"); + } + + return ok; +} + +int main() +{ + auto device = makeVisRTXDevice(statusFunc); + auto world = makeWorld(device); + + bool ok = true; + + for (const char *renderer : + {"default", "debug", "fast", "interactive", "quality"}) + ok &= testRenderer(device, world, renderer); + + // Invalid pairs warn and leave the camera unclipped. + { + struct Case + { + const char *camera; + float nearPlane; + float farPlane; + }; + const Case cases[] = { + {"perspective", CLIP_BETWEEN, 2.f}, // near > far + {"perspective", 0.f, CLIP_BETWEEN}, // perspective needs near > 0 + {"orthographic", -1.f, CLIP_BETWEEN}, // negative near + }; + for (const Case &k : cases) { + g_clipWarnings = 0; + auto cam = makeCamera(device, k.camera); + anari::setParameter(device, cam, "near", k.nearPlane); + anari::setParameter(device, cam, "far", k.farPlane); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, "default", world, cam), + std::vector(NUM_PIXELS, FLOAT_MAX)); + anari::release(device, cam); + ok &= check(g_clipWarnings > 0 && c.nearHits > 0 && c.farHits > 0 + && c.badMissDepth == 0, + k.camera, + "invalid near/far warns and is ignored"); + } + } + + // Orthographic allows near == 0. + { + g_clipWarnings = 0; + auto cam = makeCamera(device, "orthographic"); + anari::setParameter(device, cam, "near", 0.f); + anari::setParameter(device, cam, "far", CLIP_BETWEEN); + anari::commitParameters(device, cam); + const Counts c = tally(render(device, "default", world, cam), + std::vector(NUM_PIXELS, CLIP_BETWEEN)); + anari::release(device, cam); + ok &= check(g_clipWarnings == 0 && c.farHits == 0 && c.badMissDepth == 0, + "orthographic", + "near == 0 is valid"); + } + + anari::release(device, world); + anari::release(device, device); + + if (!ok) { + fprintf(stderr, "FAIL: camera near/far\n"); + return 1; + } + printf("camera near/far test passed\n"); + return 0; +} diff --git a/devices/rtx/device/camera/Camera.cpp b/devices/rtx/device/camera/Camera.cpp index 8d3bdda51..0630baa52 100644 --- a/devices/rtx/device/camera/Camera.cpp +++ b/devices/rtx/device/camera/Camera.cpp @@ -84,6 +84,36 @@ void Camera::populateBaseFrameData(CameraGPUData &fd) const fd.pos = m_pos; fd.dir = m_dir; fd.up = m_up; + fd.nearPlane = m_near; + fd.farPlane = m_far; +} + +void Camera::commitClipPlanes(bool allowZeroNear) +{ + m_near = 0.f; + m_far = std::numeric_limits::max(); + + float nearPlane = m_near; + float farPlane = m_far; + const bool hasNear = getParam("near", ANARI_FLOAT32, &nearPlane); + const bool hasFar = getParam("far", ANARI_FLOAT32, &farPlane); + if (!hasNear && !hasFar) + return; + + // An unset 'near' means no near clipping, which is valid for any camera. + const bool validNear = + !hasNear || (allowZeroNear ? nearPlane >= 0.f : nearPlane > 0.f); + if (!validNear || !(nearPlane < farPlane)) { + reportMessage(ANARI_SEVERITY_WARNING, + "camera 'near' (%f) and 'far' (%f) must satisfy %s; ignoring both", + nearPlane, + farPlane, + allowZeroNear ? "0 <= near < far" : "0 < near < far"); + return; + } + + m_near = nearPlane; + m_far = farPlane; } float Camera::effectiveAspect(uvec2 frameSize) const diff --git a/devices/rtx/device/camera/Camera.h b/devices/rtx/device/camera/Camera.h index 6157c4c33..e0c34ac22 100644 --- a/devices/rtx/device/camera/Camera.h +++ b/devices/rtx/device/camera/Camera.h @@ -34,6 +34,7 @@ #include "Object.h" #include "gpu/gpu_objects.h" // std +#include #include namespace visrtx { @@ -51,12 +52,17 @@ struct Camera : public Object protected: void populateBaseFrameData(CameraGPUData &fd) const; float effectiveAspect(uvec2 frameSize) const; + // Reads 'near'/'far'; an invalid pair is ignored with a warning, leaving no + // clipping. Orthographic cameras allow near == 0, perspective ones do not. + void commitClipPlanes(bool allowZeroNear); vec4 m_region{0.f, 0.f, 1.f, 1.f}; vec3 m_pos{0.f}; vec3 m_dir{0.f, 0.f, 1.f}; vec3 m_up{0.f, 1.f, 0.f}; std::optional m_aspect; + float m_near{0.f}; + float m_far{std::numeric_limits::max()}; }; } // namespace visrtx diff --git a/devices/rtx/device/camera/Orthographic.cpp b/devices/rtx/device/camera/Orthographic.cpp index 1215cc74a..fa22efecf 100644 --- a/devices/rtx/device/camera/Orthographic.cpp +++ b/devices/rtx/device/camera/Orthographic.cpp @@ -38,6 +38,7 @@ Orthographic::Orthographic(DeviceGlobalState *s) : Camera(s) {} void Orthographic::commitParameters() { Camera::commitParameters(); + commitClipPlanes(true); m_height = getParam("height", 1.f); } diff --git a/devices/rtx/device/camera/Perspective.cpp b/devices/rtx/device/camera/Perspective.cpp index 3c29abf05..676225dbb 100644 --- a/devices/rtx/device/camera/Perspective.cpp +++ b/devices/rtx/device/camera/Perspective.cpp @@ -40,6 +40,7 @@ Perspective::Perspective(DeviceGlobalState *s) : Camera(s) {} void Perspective::commitParameters() { Camera::commitParameters(); + commitClipPlanes(false); m_fovy = getParam("fovy", glm::radians(60.f)); m_focusDistance = getParam("focusDistance", 1.f); diff --git a/devices/rtx/device/gpu/cameraCreateRay.h b/devices/rtx/device/gpu/cameraCreateRay.h index 70dcddb1b..9654d5961 100644 --- a/devices/rtx/device/gpu/cameraCreateRay.h +++ b/devices/rtx/device/gpu/cameraCreateRay.h @@ -69,6 +69,17 @@ VISRTX_DEVICE Ray cameraCreateRay(const CameraGPUData &c, vec2 screen, vec2 r) break; } + // Bound the ray by the near/far clip planes. The ray's t.upper is then the + // pixel's Far Limit: the distance along this ray to the far plane. + const float cosTheta = dot(ray.dir, c.dir); + const float orgDepth = dot(ray.org - c.pos, c.dir); + if (c.nearPlane > 0.f) + ray.t.lower = fmaxf((c.nearPlane - orgDepth) / cosTheta, 0.f); + if (c.farPlane < std::numeric_limits::max()) { + ray.t.upper = fminf( + (c.farPlane - orgDepth) / cosTheta, std::numeric_limits::max()); + } + return ray; } @@ -80,7 +91,10 @@ VISRTX_DEVICE Ray rayBufferCreateRay(const CameraGPUData &c, uvec2 pixel) if (!rb.valid) { ray.org = c.pos; ray.dir = c.dir; - ray.t = {0.f, 0.f}; // zero-length interval: every ray misses + // Empty interval at the default Far Limit: every ray misses, and a Miss + // reports FLT_MAX as its depth. + ray.t = { + std::numeric_limits::max(), std::numeric_limits::max()}; return ray; } diff --git a/devices/rtx/device/gpu/gpu_objects.h b/devices/rtx/device/gpu/gpu_objects.h index 5e188cfa0..bd4493e8b 100644 --- a/devices/rtx/device/gpu/gpu_objects.h +++ b/devices/rtx/device/gpu/gpu_objects.h @@ -112,6 +112,10 @@ struct CameraGPUData vec3 pos; vec3 dir; vec3 up; + // Clip plane distances along 'dir' from 'pos'; farPlane at FLT_MAX means no + // far clipping, and every Miss reports FLT_MAX as its depth (ADR 0011). + float nearPlane; + float farPlane; union { PerspectiveCameraGPUData perspective; diff --git a/devices/rtx/device/gpu/renderer/raygen_helpers.h b/devices/rtx/device/gpu/renderer/raygen_helpers.h index 9c0cd77ef..15a02695e 100644 --- a/devices/rtx/device/gpu/renderer/raygen_helpers.h +++ b/devices/rtx/device/gpu/renderer/raygen_helpers.h @@ -106,6 +106,9 @@ VISRTX_DEVICE void renderPixel(FrameGPUData &frameData, ScreenSample ss) else return radiance; }; + // A Miss reports the camera's Far Limit, not where a cutting plane ends + // the ray (ADR 0011). + const float farLimit = ray.t.upper; applyCuttingPlane(rendererParams.cutPlane, ray); float tmax = ray.t.upper; @@ -119,7 +122,7 @@ VISRTX_DEVICE void renderPixel(FrameGPUData &frameData, ScreenSample ss) vec3 remainingT(1.f); // First hit metadata (for picking) - float depth = std::numeric_limits::max(); + float depth = farLimit; uint32_t primID = ~0u; uint32_t objID = ~0u; uint32_t instID = ~0u; diff --git a/devices/rtx/device/renderer/CONTEXT.md b/devices/rtx/device/renderer/CONTEXT.md index 868a6f212..a98d87bef 100644 --- a/devices/rtx/device/renderer/CONTEXT.md +++ b/devices/rtx/device/renderer/CONTEXT.md @@ -19,6 +19,23 @@ One Launch's worth of samples, blended into the accumulated result. A converged image is many Accumulation Frames. _Avoid_: sub-frame, iteration, pass +**Miss**: +A pixel whose camera ray records no surface or volume hit. A ray shortened +by a clip plane or a ray-buffer `tmax`, or one that never enters the scene, +is a Miss — not a hit at the end of its interval. +_Avoid_: background pixel (the backdrop color is unrelated to hit status) + +**Far Limit**: +How far a camera ray may travel: the distance along that ray to the camera's +far clip plane, or the ray's own `tmax` for a ray-buffer camera. With no far +plane set there is no clipping, and the Far Limit is the largest finite +float. +_Avoid_: tfar (not an ANARI parameter; the camera parameter is `far`) + +**Miss Depth**: +The depth reported for a Miss: always that pixel's Far Limit, regardless of +what else shortened the ray. Every renderer reports it the same way. + **Callable**: A GPU function linked into a pipeline but dispatched by index at runtime. The unit that lets shading change without rebuilding the pipeline. diff --git a/devices/rtx/device/renderer/Quality_ptx.cu b/devices/rtx/device/renderer/Quality_ptx.cu index 1b545665d..66862a9be 100644 --- a/devices/rtx/device/renderer/Quality_ptx.cu +++ b/devices/rtx/device/renderer/Quality_ptx.cu @@ -628,10 +628,12 @@ VISRTX_GLOBAL void __raygen__() // run when the shaded RGB is completely replaced by fog. double appearanceWeight = 1.0; + // A Miss reports the camera's Far Limit, not where a cutting plane ends + // the ray (ADR 0011). + const float farLimit = ray.t.upper; applyCuttingPlane(rendererParams.cutPlane, ray); - SampleDetails sample = { - vec3(0.0f), 0.0f, vec3(0.0f), ray.t.upper, vec3(0.0f)}; + SampleDetails sample = {vec3(0.0f), 0.0f, vec3(0.0f), farLimit, vec3(0.0f)}; auto sampleContribution = vec3(1.0f); @@ -1060,7 +1062,7 @@ VISRTX_GLOBAL void __raygen__() } if (isFirstBounce) - setPixelIds(frameData.fb, ss.pixel, ray.t.upper, ~0u, ~0u, ~0u); + setPixelIds(frameData.fb, ss.pixel, farLimit, ~0u, ~0u, ~0u); break; } diff --git a/devices/rtx/device/renderer/Test_ptx.cu b/devices/rtx/device/renderer/Test_ptx.cu index c1bfef462..021e35e66 100644 --- a/devices/rtx/device/renderer/Test_ptx.cu +++ b/devices/rtx/device/renderer/Test_ptx.cu @@ -57,7 +57,7 @@ VISRTX_GLOBAL void __raygen__() return; auto ray = makePrimaryRay(ss, 0u); - setPixelIds(frameData.fb, ss.pixel, 1.0f, ~0u, ~0u, ~0u); + setPixelIds(frameData.fb, ss.pixel, ray.t.upper, ~0u, ~0u, ~0u); accumPixelSample(frameData, ss.pixel, vec4(ray.dir, 1.f), ray.dir, -ray.dir); } diff --git a/devices/rtx/docs/adr/0011-camera-far-limit-defines-miss-depth.md b/devices/rtx/docs/adr/0011-camera-far-limit-defines-miss-depth.md new file mode 100644 index 000000000..75c9e0a41 --- /dev/null +++ b/devices/rtx/docs/adr/0011-camera-far-limit-defines-miss-depth.md @@ -0,0 +1,26 @@ +# A Miss reports the camera's Far Limit as its depth + +ANARI 1.1 defines `channel.depth` as Euclidean distance to the camera but never +says what a pixel with no hit reports, and leaves the perspective/orthographic +`near`/`far` clip distances to the renderer when unset. Every RTX renderer now +reports a Miss's depth as that pixel's Far Limit: the distance along the ray to +the `far` clip *plane* (`far / dot(rayDir, cameraDir)` for perspective), the +ray's `tmax` for a ray-buffer camera, and `FLT_MAX` when `far` is unset. This +holds even when a clip plane or invalid ray buffer shortened the ray. + +## Considered Options + +- **A renderer parameter (`missDepth`)**. Rejected: the camera's `far` already + is the spec's notion of where a ray ends, so a second knob would duplicate it. +- **Spherical far limit (every Miss reports exactly `far`)**. Rejected: the spec + calls `far` a clip *plane*, so Miss Depth varies across a perspective image. +- **`+inf` when `far` is unset**. Rejected: `FLT_MAX` is what applications + already see from the Fast and Interactive renderers and test for, and `inf` + produces NaN when positions are reconstructed from depth. + +## Consequences + +- With `far` unset, Miss Depth is the constant `FLT_MAX`, not `FLT_MAX / cos` + (which would overflow to `inf` toward the image edges). +- Applications should identify a Miss through the ID channels (`~0u`), not by + comparing depth to a sentinel, once `far` is set.