GitHub - mehmetoguzderin/triton-windowing

GitHub

2 min read Original article ↗

triton-windowing demo screenshot

A minimal Triton windowing demo example with ray tracing and GUI.

  • GLFW windowing (OpenGL 4.3 core)

  • PyOpenGL rendering (fullscreen triangle)

  • Dear ImGui Bundle UI overlay

  • CUDA–OpenGL SSBO interop (when OpenGL context is on an NVIDIA GPU and CUDA interop is available)

  • Torch tensor view via DLPack (no copy when interop + same device)

  • Triton kernels writing into the buffer

The renderer is ping-pong double-buffered.

Rendering modes (selected automatically)

The code will pick the least-copy path at runtime:

  1. Zero-copy (best, WIP):

    • OpenGL SSBO mapped as a CUDA graphics resource

    • Torch tensor view created over the mapped device pointer (via CuPy + DLPack)

    • Triton writes directly into the mapped SSBO (no extra copies)

  2. GPU copy (still fast, WIP):

    • Triton writes into a persistent Torch tensor (compute device)

    • Buffer is mapped on the OpenGL CUDA device

    • A device-to-device copy is issued into the mapped SSBO

      • If devices differ and peer access is supported, a peer copy is used

  3. Host staging (fallback, working):

    • Triton writes into a persistent Torch tensor

    • Copy to persistent pinned host staging buffer

    • Upload into OpenGL SSBO via glBufferSubData

What runs on the GPU

Per frame (ping-pong):

  1. OpenGL draws a fullscreen triangle; fragment shader reads SSBO(front)

  2. Triton produces the next frame into SSBO(back) (zero-copy) OR into a Torch tensor then copies

  3. swap(front, back)

Controls

ImGui panel:

  • Sphere center / radius / color

  • Camera orbit yaw/pitch/distance/FOV

  • Light direction

  • Toggles: sphere enabled, animate

Requirements

  • Linux host with NVIDIA GPU + drivers (for CUDA + Triton)

    • WSL2 can work with some setup like export MESA_D3D12_DEFAULT_ADAPTER_NAME=NVIDIA

  • OpenGL 4.3+ (SSBO)

  • Python 3.10+

  • PyTorch (CUDA build), Triton

  • cuda-python (for CUDA-OpenGL interop path)

  • CuPy (only required for the true zero-copy Torch view over a mapped SSBO)

  • imgui-bundle

Quickstart (VS Code Dev Container)

  1. On the host:

  2. Reopen in container.

  3. Run:

    python3 -m triton_windowing --width 1280 --height 720 --buffering 1

Optional:

python3 -m triton_windowing --compute-device 0 --gl-device 0

ESC to exit.

Notes

  • Import order matters: import imgui_bundle before importing glfw in Python.

  • CUDA/OpenGL interop requires that the OpenGL context and the CUDA context are on an NVIDIA GPU.

  • If OpenGL and Torch end up on different devices, the code falls back to a fast copy path automatically.

License

CC0 1.0 Universal. See LICENSE.

Security