|
10 | 10 | # |
11 | 11 | # ################################################################################ |
12 | 12 |
|
| 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 | + |
13 | 60 | import ctypes |
14 | 61 | import sys |
15 | 62 | import time |
|
0 commit comments