Ray tracing using CUDA, accessible from Python.
Real-time viewshed analysis with GPU-accelerated ray tracing. Green areas are visible from the observer position (blue dot). Run python examples/playground.py to try it interactively.
- NVIDIA GPU with RTX support (Maxwell architecture or newer)
- NVIDIA driver version:
- 456.71 or newer for Windows
- 455.28 or newer for Linux
- OptiX SDK 7.6+ (set
OptiX_INSTALL_DIRenvironment variable) - CUDA 12.x+
First, install the OptiX Python bindings (otk-pyoptix):
export OptiX_INSTALL_DIR=/path/to/OptiX-SDK
pip install otk-pyoptixThen install rtxpy:
pip install rtxpyTo install RTXpy from source:
export OptiX_INSTALL_DIR=/path/to/OptiX-SDK
pip install otk-pyoptix
pip install -ve .To run tests:
pip install -ve .[tests]
pytest -v rtxpy/testsIf you need to rebuild the PTX kernel (e.g., for a different GPU architecture or OptiX version):
# Detect your GPU's compute capability (e.g., 75 for Turing, 86 for Ampere)
GPU_ARCH=$(nvidia-smi --query-gpu=compute_cap --format=csv,noheader | tr -d '.')
# Compile for your GPU architecture
nvcc -ptx -o rtxpy/kernel.ptx cuda/kernel.cu \
-arch=sm_${GPU_ARCH} \
-I/path/to/OptiX-SDK/include \
-I cuda \
--use_fast_mathThe CUDA source files are in the cuda/ directory.
The easiest way to build rtxpy with all dependencies is using the included conda recipe:
# Install conda-build if not already installed
conda install conda-build
# Build the package (auto-detects GPU architecture)
conda build conda-recipe
# Or specify GPU architecture explicitly
GPU_ARCH=86 conda build conda-recipe # For RTX 30xx/A100
# Install the built package
conda install --use-local rtxpyThe conda build automatically:
- Clones OptiX SDK headers from NVIDIA/optix-dev
- Detects your GPU architecture (or uses
GPU_ARCHenv var) - Compiles the PTX kernel for your GPU
- Builds and installs otk-pyoptix
- Installs rtxpy
You can also specify the OptiX version:
OPTIX_VERSION=7.7.0 conda build conda-recipe # Requires driver 530.41+
OPTIX_VERSION=8.0.0 conda build conda-recipe # Requires driver 535+See conda-recipe/README.md for detailed documentation, GPU architecture reference, and troubleshooting.
To get OptiX working on WSL2, follow the instructions from the NVIDIA forums: https://forums.developer.nvidia.com/t/problem-running-optix-7-6-in-wsl/239355/8
Summary:
- Install WSL 2 and enable CUDA
- Download and extract the Linux display driver (e.g.,
NVIDIA-Linux-x86_64-590.44.01.run) - Extract with
./NVIDIA-Linux-x86_64-XXX.XX.run -x - Copy the following files to
C:/Windows/System32/lxss/lib:libnvoptix.so.XXX.00(rename tolibnvoptix.so.1)libnvidia-rtcore.so.XXX.00(keep original name)libnvidia-ptxjitcompiler.so.XXX.00(rename tolibnvidia-ptxjitcompiler.so.1)
- Add
/usr/lib/wsl/libto yourLD_LIBRARY_PATH - Reset WSL cache with
wsl --shutdownfrom PowerShell
import numpy as np
from rtxpy import RTX
# Create RTX instance
rtx = RTX()
# Define geometry (vertices and triangle indices)
verts = np.float32([0,0,0, 1,0,0, 0,1,0, 1,1,0])
triangles = np.int32([0,1,2, 2,1,3])
# Build acceleration structure
rtx.build(0, verts, triangles)
# Define rays: [ox, oy, oz, tmin, dx, dy, dz, tmax]
rays = np.float32([0.33, 0.33, 100, 0, 0, 0, -1, 1000])
hits = np.float32([0, 0, 0, 0])
# Trace rays
rtx.trace(rays, hits, 1)
# hits contains: [t, nx, ny, nz]
# t = distance to hit point (-1 if miss)
# nx, ny, nz = surface normal at hit point
print(hits) # [100.0, 0.0, 0.0, 1.0]For GPU-resident data, use CuPy arrays for better performance:
import cupy
verts = cupy.float32([0,0,0, 1,0,0, 0,1,0, 1,1,0])
triangles = cupy.int32([0,1,2, 2,1,3])
rays = cupy.float32([0.33, 0.33, 100, 0, 0, 0, -1, 1000])
hits = cupy.float32([0, 0, 0, 0])
rtx.build(0, verts, triangles)
rtx.trace(rays, hits, 1)