OpenGL Integration
PyImageCUDA provides a GPU-to-GPU transfer with OpenGL through CUDA-OpenGL interop, enabling efficient real-time preview pipelines.
Overview
Traditional approach:
With OpenGL interop:
Performance:
- Zero CPU involvement (all transfers GPU-side)
- ~10x faster than CPU download/upload
- Sub-millisecond updates for 2K images (~1ms measured)
- Single GPU-GPU DMA copy (PBO→Texture, asynchronous)
GLResource API
Constructor
Registers an OpenGL PBO with CUDA for interop.Parameters: - pbo_id: OpenGL buffer ID from glGenBuffers()
Raises: - ValueError: If pbo_id is invalid - RuntimeError: If CUDA registration fails
copy_from()
Copies ImageU8 data directly to PBO (GPU→GPU, zero CPU overhead).Parameters: - image: Source ImageU8 buffer
Raises: - TypeError: If image is not ImageU8 - RuntimeError: If resource has been freed
free()
Unregisters the resource. Must be called before deleting the PBO.Context Manager:
Best Practices
Display Integration
- Invert texture Y-coordinates if image appears upside-down
- Enable
GL_BLENDfor alpha channel support
Resource Management
- Create PBO with
GL_STREAM_DRAWfor best performance - Pre-allocate buffers to avoid reallocation overhead
- Call
free()before deleting OpenGL objects
Performance
- Reuse
ImageU8buffers when possible - Use texture size matching image size (or larger)
glTexSubImage2Dwith PBO is asynchronous (no CPU stall)