From d3b4d008b6049856e6025e3efd7e4f991fcdaa67 Mon Sep 17 00:00:00 2001
From: Yichao Yu <yyc1992@gmail.com>
Date: Thu, 3 Jul 2014 08:36:39 +0800
Subject: [PATCH] get_gl_object_info

---
 TODOs                                |  1 +
 pyopencl/__init__.py                 | 10 +++++-----
 pyopencl/_cffi.py                    |  6 ++++++
 pyopencl/c_wrapper/wrap_cl_gl_core.h |  2 ++
 pyopencl/cache.py                    |  1 +
 pyopencl/cffi_cl.py                  | 14 +++++++++++---
 src/c_wrapper/gl_obj.cpp             | 20 ++++++++++----------
 7 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/TODOs b/TODOs
index cf163866..b3681051 100644
--- a/TODOs
+++ b/TODOs
@@ -2,6 +2,7 @@
 - generic_info
 - Incorporate fixes in C++ stuff from after the fork
 - compare and tests
+- MemoryPool
 - enqueue_nd_range_kernel size/offset mess
 
 - CommandQueue.set_property
diff --git a/pyopencl/__init__.py b/pyopencl/__init__.py
index d6bec005..baa0dbc7 100644
--- a/pyopencl/__init__.py
+++ b/pyopencl/__init__.py
@@ -765,12 +765,12 @@ def _add_functionality():
 
     # }}}
 
-    # if _cl.have_gl():
-    #     def gl_object_get_gl_object(self):
-    #         return self.get_gl_object_info()[1]
+    if _cl.have_gl():
+        def gl_object_get_gl_object(self):
+            return self.get_gl_object_info()[1]
 
-    #     GLBuffer.gl_object = property(gl_object_get_gl_object)
-    #     GLTexture.gl_object = property(gl_object_get_gl_object)
+        GLBuffer.gl_object = property(gl_object_get_gl_object)
+        GLTexture.gl_object = property(gl_object_get_gl_object)
 
 _add_functionality()
 
diff --git a/pyopencl/_cffi.py b/pyopencl/_cffi.py
index edabae9d..bd68dbc7 100644
--- a/pyopencl/_cffi.py
+++ b/pyopencl/_cffi.py
@@ -117,6 +117,12 @@ typedef struct _cl_mem_ion_host_ptr {
 
 typedef cl_bitfield         cl_mem_migration_flags_ext;
 
+/* cl_gl.h */
+typedef cl_uint     cl_gl_object_type;
+typedef cl_uint     cl_gl_texture_info;
+typedef cl_uint     cl_gl_platform_info;
+typedef struct __GLsync *cl_GLsync;
+
 /* c++ class pointer */
 typedef struct clbase *clobj_t;
 """
diff --git a/pyopencl/c_wrapper/wrap_cl_gl_core.h b/pyopencl/c_wrapper/wrap_cl_gl_core.h
index 2a381234..8f326b37 100644
--- a/pyopencl/c_wrapper/wrap_cl_gl_core.h
+++ b/pyopencl/c_wrapper/wrap_cl_gl_core.h
@@ -11,3 +11,5 @@ error *enqueue_release_gl_objects(
     clobj_t *event, clobj_t queue, const clobj_t *mem_objects,
     uint32_t num_mem_objects, const clobj_t *wait_for, uint32_t num_wait_for);
 cl_context_properties get_apple_cgl_share_group();
+error *get_gl_object_info(clobj_t mem, cl_gl_object_type *otype,
+                          GLuint *gl_name);
diff --git a/pyopencl/cache.py b/pyopencl/cache.py
index 9a6a2cf6..9971b1e3 100644
--- a/pyopencl/cache.py
+++ b/pyopencl/cache.py
@@ -479,6 +479,7 @@ def create_built_program_from_source_cached(ctx, src, options=[], devices=None,
             already_built = False
 
     except Exception, e:
+        raise
         from pyopencl import Error
         if (isinstance(e, Error)
                 and e.code == _cl.status_code.BUILD_PROGRAM_FAILURE):
diff --git a/pyopencl/cffi_cl.py b/pyopencl/cffi_cl.py
index c132e524..222cbba5 100644
--- a/pyopencl/cffi_cl.py
+++ b/pyopencl/cffi_cl.py
@@ -1445,7 +1445,15 @@ def have_gl():
     return bool(_lib.have_gl())
 
 
-class GLBuffer(MemoryObject):
+class _GLObject(object):
+    def get_gl_object_info(self):
+        otype = _ffi.new('cl_gl_object_type*')
+        gl_name = _ffi.new('GLuint*')
+        _handle_error(_lib.get_gl_object_info(self.ptr, otype, gl_name))
+        return otype[0], gl_name[0]
+
+
+class GLBuffer(MemoryObject, _GLObject):
     _id = 'gl_buffer'
 
     def __init__(self, context, flags, bufobj):
@@ -1457,7 +1465,7 @@ class GLBuffer(MemoryObject):
         self.ptr = ptr[0]
 
 
-class GLRenderBuffer(MemoryObject):
+class GLRenderBuffer(MemoryObject, _GLObject):
     _id = 'gl_renderbuffer'
 
     def __init__(self, context, flags, bufobj):
@@ -1750,7 +1758,7 @@ class Sampler(_Common):
 
 # {{{ GLTexture
 
-class GLTexture(Image):
+class GLTexture(Image, _GLObject):
     _id = 'gl_texture'
 
     def __init__(self, context, flags, texture_target, miplevel, texture, dims):
diff --git a/src/c_wrapper/gl_obj.cpp b/src/c_wrapper/gl_obj.cpp
index 311ddd51..af80c3f9 100644
--- a/src/c_wrapper/gl_obj.cpp
+++ b/src/c_wrapper/gl_obj.cpp
@@ -47,16 +47,6 @@ create_from_gl_texture(const context *ctx, cl_mem_flags flags,
 }
 #endif
 
-// TODO:
-// PYOPENCL_INLINE
-// py::tuple get_gl_object_info(memory_object_holder const &mem)
-// {
-//   cl_gl_object_type otype;
-//   GLuint gl_name;
-//   PYOPENCL_CALL_GUARDED(clGetGLObjectInfo, (mem, &otype, &gl_name));
-//   return py::make_tuple(otype, gl_name);
-// }
-
 typedef cl_int (*clEnqueueGLObjectFunc)(cl_command_queue, cl_uint,
                                         const cl_mem*, cl_uint,
                                         const cl_event*, cl_event*);
@@ -155,3 +145,13 @@ get_apple_cgl_share_group()
     return (cl_context_properties)kCGLShareGroup;
 }
 #endif /* __APPLE__ */
+
+error*
+get_gl_object_info(clobj_t mem, cl_gl_object_type *otype, GLuint *gl_name)
+{
+    auto globj = static_cast<memory_object*>(mem);
+    return c_handle_error([&] {
+            pyopencl_call_guarded(clGetGLObjectInfo, globj, buf_arg(*otype),
+                                  buf_arg(*gl_name));
+        });
+}
-- 
GitLab