Python wrapper for OpenCL enabling GPU and parallel computing with comprehensive array operations and mathematical functions
86
OpenGL/OpenCL interoperability enables sharing of graphics resources between OpenGL rendering and OpenCL compute contexts. This allows efficient graphics/compute workflows without costly data transfers between GPU contexts.
Create OpenCL memory objects from existing OpenGL buffers, textures, and renderbuffers for shared GPU memory access.
class GLBuffer:
"""
OpenCL buffer created from OpenGL buffer object.
Inherits from MemoryObject with OpenGL-specific functionality.
"""
def __init__(self, context, flags, bufobj):
"""
Create OpenCL buffer from OpenGL buffer.
Parameters:
- context (Context): OpenCL context with OpenGL sharing
- flags (mem_flags): Memory access flags
- bufobj (int): OpenGL buffer object ID
"""
def get_gl_object_info(self):
"""
Get OpenGL object information.
Returns:
tuple[gl_object_type, int]: Object type and OpenGL object ID
"""
class GLRenderBuffer:
"""
OpenCL memory object created from OpenGL renderbuffer.
Inherits from MemoryObject for OpenGL renderbuffer sharing.
"""
def __init__(self, context, flags, bufobj):
"""
Create OpenCL memory object from OpenGL renderbuffer.
Parameters:
- context (Context): OpenCL context with OpenGL sharing
- flags (mem_flags): Memory access flags
- bufobj (int): OpenGL renderbuffer object ID
"""
def get_gl_object_info(self):
"""Get OpenGL renderbuffer information."""
class GLTexture:
"""
OpenCL image created from OpenGL texture object.
Inherits from Image with OpenGL texture sharing capabilities.
"""
def __init__(self, context, flags, texture_target, miplevel, texture, dims):
"""
Create OpenCL image from OpenGL texture.
Parameters:
- context (Context): OpenCL context with OpenGL sharing
- flags (mem_flags): Memory access flags
- texture_target (int): OpenGL texture target (GL_TEXTURE_2D, etc.)
- miplevel (int): Mipmap level
- texture (int): OpenGL texture object ID
- dims (int): Number of texture dimensions
"""
def get_gl_object_info(self):
"""Get OpenGL texture information."""Manage synchronization between OpenGL and OpenCL operations to ensure data consistency.
def enqueue_acquire_gl_objects(queue, mem_objects, wait_for=None):
"""
Acquire OpenGL objects for OpenCL use.
Must be called before OpenCL operations on shared OpenGL objects.
Parameters:
- queue (CommandQueue): OpenCL command queue
- mem_objects (list): List of OpenGL memory objects to acquire
- wait_for (list[Event], optional): Events to wait for
Returns:
Event: Completion event
"""
def enqueue_release_gl_objects(queue, mem_objects, wait_for=None):
"""
Release OpenGL objects back to OpenGL.
Must be called after OpenCL operations before OpenGL can use objects.
Parameters:
- queue (CommandQueue): OpenCL command queue
- mem_objects (list): List of OpenGL memory objects to release
- wait_for (list[Event], optional): Events to wait for
Returns:
Event: Completion event
"""Query OpenGL interoperability support and context information.
def have_gl():
"""
Check if OpenGL interoperability is available.
Returns:
bool: True if OpenGL/OpenCL interop is supported
"""
def get_apple_cgl_share_group():
"""
Get Apple CGL share group for context creation.
macOS-specific function for OpenGL context sharing.
Returns:
object: CGL share group handle
"""import pyopencl as cl
import numpy as np
from OpenGL.GL import *
# Create OpenCL context with OpenGL sharing
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create OpenGL buffer
gl_buffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, gl_buffer)
glBufferData(GL_ARRAY_BUFFER, 1024, None, GL_DYNAMIC_DRAW)
# Create OpenCL buffer from OpenGL buffer
cl_buffer = cl.GLBuffer(ctx, cl.mem_flags.READ_WRITE, gl_buffer)
# Acquire for OpenCL use
cl.enqueue_acquire_gl_objects(queue, [cl_buffer])
# Perform OpenCL operations
# ... OpenCL compute operations on cl_buffer ...
# Release back to OpenGL
cl.enqueue_release_gl_objects(queue, [cl_buffer])
# Now OpenGL can use the buffer
glBindBuffer(GL_ARRAY_BUFFER, gl_buffer)
# ... OpenGL rendering operations ...# OpenGL object types
class gl_object_type: ...
# OpenGL texture information
class gl_texture_info: ...
# OpenGL context properties
class gl_context_info: ...OpenGL interoperability requires careful synchronization between OpenGL and OpenCL operations using acquire/release semantics to ensure data consistency across graphics and compute operations.
Install with Tessl CLI
npx tessl i tessl/pypi-pyopencldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10