def get_gl_sharing_context_properties(): ctx_props = cl.context_properties from OpenGL import platform as gl_platform props = [] import sys if sys.platform in ["linux", "linux2"]: from OpenGL import GLX props.append( (ctx_props.GL_CONTEXT_KHR, gl_platform.GetCurrentContext())) props.append((ctx_props.GLX_DISPLAY_KHR, GLX.glXGetCurrentDisplay())) elif sys.platform == "win32": from OpenGL import WGL props.append( (ctx_props.GL_CONTEXT_KHR, gl_platform.GetCurrentContext())) props.append((ctx_props.WGL_HDC_KHR, WGL.wglGetCurrentDC())) elif sys.platform == "darwin": props.append((ctx_props.CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, cl.get_apple_cgl_share_group())) else: raise NotImplementedError("platform '%s' not yet supported" % sys.platform) return props
def initialize(): plats = cl.get_platforms() ctx_props = cl.context_properties props = [(ctx_props.PLATFORM, plats[0]), (ctx_props.GL_CONTEXT_KHR, platform.GetCurrentContext())] import sys if sys.platform == "linux2": props.append((ctx_props.GLX_DISPLAY_KHR, GLX.glXGetCurrentDisplay())) elif sys.platform == "win32": props.append((ctx_props.WGL_HDC_KHR, WGL.wglGetCurrentDC())) ctx = cl.Context(properties=props) glClearColor(1, 1, 1, 1) glColor(0, 0, 1) vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) rawGlBufferData(GL_ARRAY_BUFFER, n_vertices * 2 * 4, None, GL_STATIC_DRAW) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(2, GL_FLOAT, 0, None) coords_dev = cl.GLBuffer(ctx, cl.mem_flags.READ_WRITE, int(vbo)) prog = cl.Program(ctx, src).build() queue = cl.CommandQueue(ctx) cl.enqueue_acquire_gl_objects(queue, [coords_dev]) prog.generate_sin(queue, (n_vertices, ), None, coords_dev) cl.enqueue_release_gl_objects(queue, [coords_dev]) queue.finish() glFlush()
def glRenderMode(newMode): """Change to the given rendering mode If the current mode is GL_FEEDBACK or GL_SELECT, return the current buffer appropriate to the mode """ # must get the current mode to determine operation... from OpenGL.GL import glGetIntegerv from OpenGL.GL import selection, feedback currentMode = glGetIntegerv(simple.GL_RENDER_MODE) try: currentMode = currentMode[0] except (TypeError, ValueError, IndexError) as err: pass if currentMode in (simple.GL_RENDER, 0): # no array needs to be returned... return simple.glRenderMode(newMode) result = simple.glRenderMode(newMode) # result is now an integer telling us how many elements were copied... if result < 0: if currentMode == simple.GL_SELECT: raise error.GLError( simple.GL_STACK_OVERFLOW, "glSelectBuffer too small to hold selection results", ) elif currentMode == simple.GL_FEEDBACK: raise error.GLError( simple.GL_STACK_OVERFLOW, "glFeedbackBuffer too small to hold selection results", ) else: raise error.GLError( simple.GL_STACK_OVERFLOW, "Unknown glRenderMode buffer (%s) too small to hold selection results" % (currentMode, ), ) # Okay, now that the easy cases are out of the way... # Do we have a pre-stored pointer about which the user already knows? context = platform.GetCurrentContext() if context == 0: raise error.Error( """Returning from glRenderMode without a valid context!""") arrayConstant, wrapperFunction = { simple.GL_FEEDBACK: (simple.GL_FEEDBACK_BUFFER_POINTER, feedback.parseFeedback), simple.GL_SELECT: (simple.GL_SELECTION_BUFFER_POINTER, selection.GLSelectRecord.fromArray), }[currentMode] current = contextdata.getValue(arrayConstant) # XXX check to see if it's the *same* array we set currently! if current is None: current = glGetPointerv(arrayConstant) # XXX now, can turn the array into the appropriate wrapper type... if wrapperFunction: current = wrapperFunction(current, result) return current
def getContext(context=None): """Get the context (if passed, just return) context -- the context ID, if None, the current context """ if context is None: context = platform.GetCurrentContext() if context == 0: from OpenGL import error raise error.Error( """Attempt to retrieve context when no valid context""") return context
def cleanupContext(context=None): """Cleanup all held pointer objects for the given context Warning: this is dangerous, as if you call it before a context is destroyed you may release memory held by the context and cause a protection fault when the GL goes to render the scene! Normally you will want to get the context ID explicitly and then register cleanupContext as a weakref callback to your GUI library Context object with the (now invalid) context ID as parameter. """ if context is None: context = platform.GetCurrentContext() for storage in STORAGES: try: del storedPointers[context] except KeyError as err: return False else: return True
) elif currentMode == simple.GL_FEEDBACK: raise error.GLError( simple.GL_STACK_OVERFLOW, "glFeedbackBuffer too small to hold selection results", ) else: raise error.GLError( simple.GL_STACK_OVERFLOW, "Unknown glRenderMode buffer (%s) too small to hold selection results"%( currentMode, ), ) # Okay, now that the easy cases are out of the way... # Do we have a pre-stored pointer about which the user already knows? context = platform.GetCurrentContext() if context == 0: raise error.Error( """Returning from glRenderMode without a valid context!""" ) arrayConstant, wrapperFunction = { simple.GL_FEEDBACK: (simple.GL_FEEDBACK_BUFFER_POINTER,feedback.parseFeedback), simple.GL_SELECT: (simple.GL_SELECTION_BUFFER_POINTER, selection.GLSelectRecord.fromArray), }[ currentMode ] current = contextdata.getValue( arrayConstant ) # XXX check to see if it's the *same* array we set currently! if current is None: current = glGetPointerv( arrayConstant ) # XXX now, can turn the array into the appropriate wrapper type... if wrapperFunction: current = wrapperFunction( current, result )
from OpenGL.GLUT import * from OpenGL.GLU import * from OpenGL.GL import * from OpenGL import platform as gl_platform from OpenGL import GLX from pycl import * import numpy as np glutInit() gl_window = glutCreateWindow("window") gl_ctx = gl_platform.GetCurrentContext() glx_display = GLX.glXGetCurrentDisplay() cl_ctx_props = { CL_GL_CONTEXT_KHR: gl_ctx, CL_GLX_DISPLAY_KHR: glx_display, } cl_ctx = clCreateContext(other_props=cl_ctx_props) cl_queue = clCreateCommandQueue(cl_ctx) def idle(): input_data = np.arange(20, dtype=np.float32) gl_buf = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, gl_buf) glBufferData(GL_ARRAY_BUFFER, input_data, GL_STATIC_DRAW) glFinish() cl_buf = clCreateFromGLBuffer(cl_ctx, gl_buf)