Skip to content

Commit 6cbbc10

Browse files
committed
docs(examples): restore plasma walkthrough details
Keep the normalized file banner while restoring the detailed GraphicsResource walkthrough that makes the OpenGL interop example easier to follow. Made-with: Cursor
1 parent e9a66a9 commit 6cbbc10

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

cuda_core/examples/gl_interop_plasma.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,53 @@
1010
#
1111
# ################################################################################
1212

13+
# What this example teaches
14+
# =========================
15+
# How to use cuda.core.GraphicsResource to let a CUDA kernel write pixels
16+
# directly into an OpenGL buffer with zero copies through the CPU.
17+
#
18+
# How it works
19+
# ============
20+
# Normally, getting CUDA results onto the screen would require:
21+
# CUDA -> CPU memory -> OpenGL (two slow copies across the PCIe bus)
22+
#
23+
# GraphicsResource eliminates the CPU round-trip. The pixel data stays
24+
# on the GPU the entire time:
25+
#
26+
# 1. OpenGL allocates a PBO (Pixel Buffer Object) -- a raw GPU buffer.
27+
# 2. GraphicsResource.from_gl_buffer() registers that PBO with CUDA.
28+
# Now both CUDA and OpenGL have access to the same GPU memory.
29+
#
30+
# +----------------------+ +---------------------+
31+
# | OpenGL PBO | | GraphicsResource |
32+
# | (pixel buffer on GPU)| <---> | (CUDA handle to |
33+
# +----------------------+ | the same memory) |
34+
# +---------------------+
35+
#
36+
# EACH FRAME (all on GPU -- nothing touches the CPU or PCIe bus)
37+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38+
# 1. map() -- CUDA gets a device pointer into the PBO
39+
# 2. launch kernel -- CUDA writes pixel colors into that memory
40+
# 3. unmap() -- ownership returns to OpenGL
41+
# 4. glTexSubImage2D -- OpenGL copies PBO into a texture (GPU-to-GPU)
42+
# 5. draw -- OpenGL renders the texture to the window
43+
#
44+
# Why is there a copy in step 4? OpenGL can only render from a
45+
# texture object, not from a raw buffer. The glTexSubImage2D step
46+
# copies the PBO bytes into a texture, but this happens entirely on
47+
# the GPU and it is very fast. The big win from GraphicsResource is
48+
# that we never copy pixels from the CPU to the GPU and then back.
49+
#
50+
# What you should see
51+
# ===================
52+
# A window showing smoothly animated, colorful swirling patterns (a plasma
53+
# effect popular in the demoscene). The window title shows the current FPS.
54+
# Close the window or press Escape to exit.
55+
#
56+
# Requirements
57+
# ============
58+
# pip install pyglet
59+
1360
import ctypes
1461
import sys
1562
import time

0 commit comments

Comments
 (0)