Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion devices/rtx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ change.

This extension indicates that applications can use pointers to CUDA device
memory when created shared and captured arrays when using VisRTX. All the normal
rules for shared and captured array data still apply.
rules for shared and captured array data still apply. Mapping a CUDA-backed
shared or captured array returns the supplied CUDA pointer, not a host mapping.

#### "ANARI_NV_FRAME_BUFFERS_CUDA"

Expand Down
3 changes: 3 additions & 0 deletions devices/rtx/apps/tests/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ if (WIN32)
endif()

make_test(TestDeviceInit)
make_test(TestArrayMapping)
target_link_libraries(TestArrayMapping PRIVATE CUDA::cudart)
make_test(TestEvictGPUArray)
make_test(TestMultiThreadedObjectCreation)
make_test(TestSpheres)
Expand Down Expand Up @@ -102,6 +104,7 @@ if (VISRTX_USE_MDL_FOR_PHYSICALLY_BASED)
endif()
make_test(TestSamplerAlphaFill)
make_test(TestTextureDataUpdate)
target_link_libraries(TestTextureDataUpdate PRIVATE CUDA::cudart)
make_test(TestAlphaCutoffMode)
make_test(TestRendererFog)
make_test(TestRendererFogDiscovery)
Expand Down
114 changes: 114 additions & 0 deletions devices/rtx/apps/tests/api/TestArrayMapping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

#include <anari/ext/visrtx/makeVisRTXDevice.h>
#include <cuda_runtime.h>
#include <anari/anari_cpp.hpp>

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>

static void check(bool condition, const char *message)
{
if (!condition) {
std::fprintf(stderr, "FAIL: %s\n", message);
std::exit(1);
}
}

static void checkCUDA(cudaError_t result)
{
check(result == cudaSuccess, cudaGetErrorString(result));
}

static void statusFunc(const void *,
ANARIDevice,
ANARIObject,
ANARIDataType,
ANARIStatusSeverity severity,
ANARIStatusCode,
const char *message)
{
// Repeated successful map/unmap cycles must not warn about mapping state.
// Unrelated warnings (including device initialization) are not failures.
const bool mappingWarning = severity == ANARI_SEVERITY_WARNING
&& (std::strstr(message, "array mapped again")
|| std::strstr(message, "array unmapped again"));
check(severity != ANARI_SEVERITY_FATAL_ERROR
&& severity != ANARI_SEVERITY_ERROR && !mappingWarning,
message);
}

static void freeCUDA(void *ptr)
{
checkCUDA(cudaFree(ptr));
}

static void capturedDeleter(const void *userData, const void *ptr)
{
++*static_cast<unsigned *>(const_cast<void *>(userData));
freeCUDA(const_cast<void *>(ptr));
}

static void exercise(ANARIDevice device,
void *memory,
bool cudaMemory,
ANARIMemoryDeleter deleter = nullptr,
const void *userData = nullptr)
{
auto array =
anariNewArray1D(device, memory, deleter, userData, ANARI_FLOAT32, 4);
check(array != nullptr, "array creation failed");
for (int iteration = 0; iteration < 2; ++iteration) {
void *mapped = anariMapArray(device, array);
check(mapped != nullptr, "mapping returned null");
check(!memory || mapped == memory, "mapping changed application pointer");
const float values[4] = {float(iteration), 1.f, 2.f, 3.f};
if (cudaMemory) {
checkCUDA(
cudaMemcpy(mapped, values, sizeof(values), cudaMemcpyHostToDevice));
checkCUDA(cudaDeviceSynchronize());
} else {
auto *out = static_cast<float *>(mapped);
for (int i = 0; i < 4; ++i)
out[i] = values[i];
}
anariUnmapArray(device, array);
}
anariRelease(device, array);
}

int main()
{
statusFunc(nullptr,
nullptr,
nullptr,
ANARI_UNKNOWN,
ANARI_SEVERITY_WARNING,
ANARI_STATUS_NO_ERROR,
"unrelated initialization warning");
unsigned deleterCalls = 0;
auto device = makeVisRTXDevice(statusFunc);
check(device != nullptr, "device creation failed");

float host[4] = {};
exercise(device, host, false);
exercise(device, nullptr, false);

for (bool managed : {false, true}) {
void *ptr = nullptr;
checkCUDA(managed ? cudaMallocManaged(&ptr, sizeof(host))
: cudaMalloc(&ptr, sizeof(host)));
std::unique_ptr<void, decltype(&freeCUDA)> allocation(ptr, freeCUDA);
exercise(device, ptr, true);
}

void *captured = nullptr;
checkCUDA(cudaMalloc(&captured, sizeof(host)));
exercise(device, captured, true, capturedDeleter, &deleterCalls);
anariRelease(device, device);
check(deleterCalls == 1, "captured allocation must be deleted exactly once");
return 0;
}
130 changes: 130 additions & 0 deletions devices/rtx/apps/tests/api/TestTextureDataUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <anari/anari_cpp/ext/std.h>
#include <anari/ext/visrtx/makeVisRTXDevice.h>
#include <cuda_runtime.h>
#include <anari/anari_cpp.hpp>

#include <array>
Expand All @@ -39,6 +40,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <vector>

Expand Down Expand Up @@ -381,6 +383,86 @@ static void checkAll(
ok);
}

static void testSharedHostMappingAfterRender(anari::Device d, bool &ok)
{
TexturedScene s(d, 2, TexFormat::FLOAT_RGB, Ownership::SHARED);
check("shared-host-mapping/initial", renderMeanColor(s), GREEN, ok);

// Rendering forces GPU consumption, though the sampler may use staging
// rather than retain a persistent linear device copy.
void *mapped = anariMapArray(d, s.image);
if (mapped != s.appMemory.data()) {
fprintf(stderr, "FAIL: shared host mapping changed application pointer\n");
ok = false;
}
if (!mapped)
return;

writeTexels(mapped, s.format, std::vector<vec3>(s.texelCount, RED));
anariUnmapArray(d, s.image);
check("shared-host-mapping/updated", renderMeanColor(s), RED, ok);
}

static void requireCUDA(cudaError_t result)
{
if (result != cudaSuccess) {
fprintf(stderr, "FAIL: CUDA: %s\n", cudaGetErrorString(result));
std::exit(1);
}
}

static void freeCUDA(void *ptr)
{
requireCUDA(cudaFree(ptr));
}

static void testSharedCUDAUpdates(
anari::Device d, int dim, TexFormat format, bool &ok)
{
constexpr size_t WIDTH = 2;
const size_t texelCount = WIDTH << (dim - 1);
void *ptr = nullptr;
const size_t bytes = texelCount * channels(format)
* (isFloat(format) ? sizeof(float) : sizeof(uint8_t));
requireCUDA(cudaMalloc(&ptr, bytes));
// Destroy the scene and its shared array before freeing application memory.
std::unique_ptr<void, decltype(&freeCUDA)> allocation(ptr, freeCUDA);
TexturedScene s(d, dim, format);
ANARIArray image = newImage(d, dim, anariFormat(format), WIDTH, ptr);
if (!image) {
fprintf(stderr, "FAIL: CUDA image creation failed\n");
ok = false;
return;
}
anariRelease(d, s.image);
s.image = image;
setImage(d, s.samplers[0], "image", s.image, dim);

// Bind once; subsequent renders must observe writes through unmap alone.
int iteration = 0;
for (const auto &color : {GREEN, RED, BLUE}) {
void *mapped = anariMapArray(d, s.image);
if (mapped != ptr) {
fprintf(stderr, "FAIL: CUDA mapping changed application pointer\n");
ok = false;
}
if (!mapped)
return;

std::vector<float> texels((bytes + sizeof(float) - 1) / sizeof(float));
writeTexels(texels.data(), format, std::vector<vec3>(texelCount, color));
requireCUDA(
cudaMemcpy(mapped, texels.data(), bytes, cudaMemcpyHostToDevice));
requireCUDA(cudaDeviceSynchronize());
anariUnmapArray(d, s.image);
check("shared-cuda-update/image" + std::to_string(dim) + "D/"
+ formatName(format) + "/" + std::to_string(iteration++),
renderMeanColor(s),
color,
ok);
}
}

static void testWindows(
anari::Device d, TexFormat format, Ownership ownership, bool &ok)
{
Expand Down Expand Up @@ -475,6 +557,46 @@ static void checkValid(anari::Device d,
ok &= pass;
}

static void testForeignCUDAInput(anari::Device d, bool &ok)
{
int devices = 0;
requireCUDA(cudaGetDeviceCount(&devices));
if (devices < 2) {
std::puts("SKIP: foreign CUDA input requires two GPUs");
return;
}
int local = 0;
requireCUDA(cudaGetDevice(&local));
const int foreign = (local + 1) % devices;
for (auto format : {TexFormat::FLOAT_RGB, TexFormat::FLOAT_RGBA}) {
void *ptr = nullptr;
requireCUDA(cudaSetDevice(foreign));
requireCUDA(cudaMalloc(&ptr, 2 * channels(format) * sizeof(float)));
requireCUDA(cudaSetDevice(local));
{
TexturedScene s(d, 1, format);
ANARIArray image = newImage(d, 1, anariFormat(format), 2, ptr);
anariRelease(d, s.image);
s.image = image;
setImage(d, s.samplers[0], "image", s.image, 1);
const unsigned before = uploadErrors;
expectUploadError = true;
renderMeanColor(s);
expectUploadError = false;
if (uploadErrors != before + 1) {
std::fprintf(stderr, "FAIL: foreign CUDA input was not rejected\n");
ok = false;
}
checkValid(d, s.samplers[0], "foreign-cuda-input", false, ok);
// No kernel may touch the foreign pointer or poison the local context.
requireCUDA(cudaDeviceSynchronize());
}
requireCUDA(cudaSetDevice(foreign));
requireCUDA(cudaFree(ptr));
requireCUDA(cudaSetDevice(local));
}
}

static void testInvalidTransitions(
anari::Device d, int dim, bool unset, bool &ok)
{
Expand Down Expand Up @@ -626,6 +748,14 @@ int main()
setvbuf(stdout, nullptr, _IOLBF, 0);
auto device = makeVisRTXDevice(statusFunc);
bool ok = true;
testSharedHostMappingAfterRender(device, ok);
testForeignCUDAInput(device, ok);
for (int dim : {1, 2, 3})
for (auto format : {TexFormat::FLOAT_RGB,
TexFormat::FLOAT_RGBA,
TexFormat::SRGB_RGB,
TexFormat::SRGB_RGBA})
testSharedCUDAUpdates(device, dim, format, ok);
for (auto format : {TexFormat::SRGB_RGBA, TexFormat::SRGB_RGB})
for (auto ownership : {Ownership::MANAGED, Ownership::SHARED})
testWindows(device, format, ownership, ok);
Expand Down
10 changes: 10 additions & 0 deletions devices/rtx/apps/tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Copyright (c) 2019-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause

# GPU unit test using the same compiled RGB kernel as the device library.
add_executable(rtx_test_RGBExpansion test_RGBExpansion.cpp
$<TARGET_OBJECTS:visrtx_cuda_rgb_expansion>)
target_include_directories(rtx_test_RGBExpansion PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../../../device)
target_link_libraries(rtx_test_RGBExpansion PRIVATE CUDA::cudart)
target_compile_features(rtx_test_RGBExpansion PRIVATE cxx_std_17)
add_test(NAME rtx::RGBExpansion COMMAND rtx_test_RGBExpansion)
set_tests_properties(rtx::RGBExpansion PROPERTIES TIMEOUT 60)

# Host-only unit tests for the pure analytic primitive solvers. No CUDA/OptiX/GPU
# — these run on CPU and exercise devices/rtx/device/gpu/intersectPrimitives.h
# directly, so the intersection math is testable without a device.
Expand Down
Loading
Loading