From 8adaed0a9e62795e8e3d28b3a052fd560f8a683b Mon Sep 17 00:00:00 2001 From: Kees Van Kooten Date: Tue, 27 Oct 2026 07:49:27 +0100 Subject: [PATCH] Reuse OptiX denoiser setup for unchanged configuration --- devices/rtx/apps/tests/api/CMakeLists.txt | 1 + .../rtx/apps/tests/api/TestDenoiserReuse.cpp | 127 ++++++++++++++++++ devices/rtx/device/frame/Denoiser.cu | 53 +++++--- devices/rtx/device/frame/Denoiser.h | 1 + 4 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 devices/rtx/apps/tests/api/TestDenoiserReuse.cpp diff --git a/devices/rtx/apps/tests/api/CMakeLists.txt b/devices/rtx/apps/tests/api/CMakeLists.txt index 98cf6c339..764c15896 100644 --- a/devices/rtx/apps/tests/api/CMakeLists.txt +++ b/devices/rtx/apps/tests/api/CMakeLists.txt @@ -45,6 +45,7 @@ endif() make_test(TestQuadLightShaping) make_test(TestQuadLightFocus) make_test(TestQuadLightTexture) +make_test(TestDenoiserReuse) if (WIN32) return() diff --git a/devices/rtx/apps/tests/api/TestDenoiserReuse.cpp b/devices/rtx/apps/tests/api/TestDenoiserReuse.cpp new file mode 100644 index 000000000..b95d19ebd --- /dev/null +++ b/devices/rtx/apps/tests/api/TestDenoiserReuse.cpp @@ -0,0 +1,127 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include + +static int errors = 0; +static int setups = 0; +static void status(const void *, + ANARIDevice, + ANARIObject, + ANARIDataType, + ANARIStatusSeverity severity, + ANARIStatusCode, + const char *message) +{ + if (std::strstr(message, "layers created")) + ++setups; + if (severity == ANARI_SEVERITY_ERROR + || severity == ANARI_SEVERITY_FATAL_ERROR) { + ++errors; + std::fprintf(stderr, "%s\n", message); + } +} + +int main() +{ + auto device = makeVisRTXDevice(status); + auto world = anari::newObject(device); + anari::commitParameters(device, world); + auto camera = anari::newObject(device, "perspective"); + anari::setParameter( + device, camera, "position", std::array{0, 0, 2}); + anari::setParameter( + device, camera, "direction", std::array{0, 0, -1}); + anari::setParameter(device, camera, "up", std::array{0, 1, 0}); + anari::commitParameters(device, camera); + auto renderer = anari::newObject(device, "interactive"); + anari::setParameter(device, renderer, "pixelSamples", 1); + anari::setParameter( + device, renderer, "background", std::array{.2f, .4f, .8f, 1}); + auto frame = anari::newObject(device); + anari::setParameter(device, frame, "channel.color", ANARI_FLOAT32_VEC4); + anari::setParameter(device, frame, "world", world); + anari::setParameter(device, frame, "camera", camera); + anari::setParameter(device, frame, "renderer", renderer); + const char *modes[] = {"color", + "colorAlbedo", + "colorAlbedoNormal", + "color", + "colorAlbedoNormal"}; + for (unsigned configuration = 0; configuration < 5; ++configuration) { + std::array size = { + 37 + configuration * 13, 29 + configuration * 7}; + anari::setParameter(device, renderer, "denoise", configuration != 3); + anari::setParameter(device, renderer, "denoiseMode", modes[configuration]); + anari::commitParameters(device, renderer); + anari::setParameter(device, frame, "size", size); + int firstSetups = -1; + for (unsigned step = 0; step < 8; ++step) { + anari::setParameter( + device, frame, "mdl.animationTime", float(step) * .1f); + anari::commitParameters(device, frame); + anari::render(device, frame); + anari::wait(device, frame); + auto image = anari::map(device, frame, "channel.color"); + if (!image.data || image.width != size[0] || image.height != size[1] + || image.pixelType != ANARI_FLOAT32_VEC4) + ++errors; + else + for (unsigned c = 0; c < 4; ++c) + if (!std::isfinite(image.data[c])) + ++errors; + anari::unmap(device, frame, "channel.color"); + if (step == 0) + firstSetups = setups; + else if (setups != firstSetups) { + std::fprintf( + stderr, "Repeated OptiX setup for unchanged configuration\n"); + ++errors; + } + } + } + anari::release(device, frame); + anari::release(device, renderer); + anari::release(device, camera); + anari::release(device, world); + anari::release(device, device); + std::printf( + "Denoiser commit/reuse, guide modes, disable/enable and resize: %d errors, %d layer messages\n", + errors, + setups); + return errors ? 1 : 0; +} diff --git a/devices/rtx/device/frame/Denoiser.cu b/devices/rtx/device/frame/Denoiser.cu index 9e4068a3d..cb8a4b401 100644 --- a/devices/rtx/device/frame/Denoiser.cu +++ b/devices/rtx/device/frame/Denoiser.cu @@ -71,36 +71,43 @@ void Denoiser::setup(uvec2 size, // per-tile tensor well under the limit; frames smaller than a tile collapse // to a single full-frame invoke with no overhead. constexpr uint32_t kMaxTile = 2048; - m_tileW = std::min(kMaxTile, size.x); - m_tileH = std::min(kMaxTile, size.y); + const uint32_t tileW = std::min(kMaxTile, size.x); + const uint32_t tileH = std::min(kMaxTile, size.y); + // Frame commits can change time/camera without changing denoiser resources. + // Image addresses are rebound below even when setup can be reused. + if (!m_setupValid || tileW != m_tileW || tileH != m_tileH) { + m_tileW = tileW; + m_tileH = tileH; - OptixDenoiserSizes sizes; - OPTIX_CHECK(optixDenoiserComputeMemoryResources( - m_denoiser, m_tileW, m_tileH, &sizes)); + OptixDenoiserSizes sizes; + OPTIX_CHECK(optixDenoiserComputeMemoryResources( + m_denoiser, m_tileW, m_tileH, &sizes)); - m_overlap = sizes.overlapWindowSizeInPixels; - m_state.reserve(sizes.stateSizeInBytes); - m_scratch.reserve(std::max(sizes.withOverlapScratchSizeInBytes, - std::max(sizes.computeIntensitySizeInBytes, - sizes.computeAverageColorSizeInBytes))); - m_intensity.reserve(sizeof(float)); - m_averageColor.reserve(3 * sizeof(float)); + m_overlap = sizes.overlapWindowSizeInPixels; + m_state.reserve(sizes.stateSizeInBytes); + m_scratch.reserve(std::max(sizes.withOverlapScratchSizeInBytes, + std::max(sizes.computeIntensitySizeInBytes, + sizes.computeAverageColorSizeInBytes))); + m_intensity.reserve(sizeof(float)); + m_averageColor.reserve(3 * sizeof(float)); + + // Setup must be sized for the largest padded tile the tiled invoke feeds. + OPTIX_CHECK(optixDenoiserSetup(m_denoiser, + state.stream, + m_tileW + 2 * m_overlap, + m_tileH + 2 * m_overlap, + (CUdeviceptr)m_state.ptr(), + m_state.bytes(), + (CUdeviceptr)m_scratch.ptr(), + m_scratch.bytes())); + m_setupValid = true; + } if (format != ANARI_FLOAT32_VEC4) m_uintPixels.resize(size_t(size.x) * size_t(size.y)); else m_uintPixels.clear(); - // Setup must be sized for the largest padded tile the tiled invoke feeds. - OPTIX_CHECK(optixDenoiserSetup(m_denoiser, - state.stream, - m_tileW + 2 * m_overlap, - m_tileH + 2 * m_overlap, - (CUdeviceptr)m_state.ptr(), - m_state.bytes(), - (CUdeviceptr)m_scratch.ptr(), - m_scratch.bytes())); - m_layer.input.data = (CUdeviceptr)input.ptr(); m_layer.input.width = size.x; m_layer.input.height = size.y; @@ -128,6 +135,7 @@ void Denoiser::setup(uvec2 size, void Denoiser::cleanup() { + m_setupValid = false; m_state.reset(); m_scratch.reset(); } @@ -226,6 +234,7 @@ void Denoiser::init( && (m_usingAlbedo != useAlbedo || m_usingNormal != useNormal)) { OPTIX_CHECK(optixDenoiserDestroy(m_denoiser)); m_denoiser = {}; + m_setupValid = false; } m_usingAlbedo = useAlbedo; diff --git a/devices/rtx/device/frame/Denoiser.h b/devices/rtx/device/frame/Denoiser.h index 126114e32..95984e756 100644 --- a/devices/rtx/device/frame/Denoiser.h +++ b/devices/rtx/device/frame/Denoiser.h @@ -78,6 +78,7 @@ struct Denoiser : public Object uint32_t m_overlap{0}; bool m_usingAlbedo{false}; bool m_usingNormal{false}; + bool m_setupValid{false}; // This buffer is only used when format != ANARI_FLOAT32_VEC4 HostDeviceArray m_uintPixels;