Il giorno sab, 19/06/2010 alle 15.04 -0400, Andreas Kloeckner ha
scritto:
> Hi Paolo,
> 
> On Tue, 15 Jun 2010 12:02:07 +0200, Paolo Simone Gasparello
<djga...@gmail.=
> com> wrote:
> > Hi guys, anyone got OpenGL interoperability work?
> > With the following patch I'm able to create a context sharing OpenGL
> > buffers.
> 
> Applied, thanks. (And I'm kind of embarrassed that you had to fix
these
> two...) :)
> 
> > Where fun_double is a kernel included in the file program.cl that
simply
> > does each coord *=3D 2.
> >=20
> > There are better ways to do this? I tryed to perform the cast to
> > c_void_p in C++ code using boost::python, but I was not able to make
the
> > code work.
> 
> I took a look, and it doesn't seem like ctypes has much in the way of
> C-accessible interface. One can of course emulate the Python code in
> C++, but I'm not sure that's necessary. If you'd like guidance on how
to
> do that, let me know.
> 
> Lastly, would you mind posting a fully functional example so that I
can
> add it to the examples/ directory shipped with PyOpenCL? I think that
> could be rather helpful for people trying to follow in your footsteps.
> 
> Thanks for your work!
> Andreas

Hello everybody,
I make my code doing ctypes casting in C++ work, so there is no more the
need to do that casting explicitly in python code. (patch attached)

I also wrote a simple example working on linux (I have used glX APIs and
flags) showing the usage of GL interop. It draws the sin function using
coordinates that are generated directly by the video hardware.

I hope this will be useful :)

-- 
Paolo Simone Gasparello <[email protected]>
diff --git a/src/wrapper/wrap_cl.hpp b/src/wrapper/wrap_cl.hpp
index 8113fe9..4a1ba38 100644
--- a/src/wrapper/wrap_cl.hpp
+++ b/src/wrapper/wrap_cl.hpp
@@ -693,7 +693,10 @@ namespace pyopencl
             || prop == CL_CGL_SHAREGROUP_KHR
            )
        {
-          py::extract<cl_context_properties> value(prop_tuple[1]);
+          py::object ctypes = py::import("ctypes");
+          py::object prop = prop_tuple[1], c_void_p = ctypes.attr("c_void_p");
+          py::object ptr = ctypes.attr("cast")(prop, c_void_p);
+          py::extract<cl_context_properties> value(ptr.attr("value"));
           props.push_back(value);
        }
 #endif
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.raw.GL.VERSION.GL_1_5 import glBufferData as rawGlBufferData
from OpenGL.GLX import glXGetCurrentDisplay, glXGetCurrentContext
import pyopencl as cl


n_vertices = 10000

src = """

__kernel void generate_sin(__global float2* a)
{
    int id = get_global_id(0);
    int n = get_global_size(0);
    float r = (float)id / (float)n;
    float x = r * 16.0f * 3.1415f;
    a[id].x = r * 2.0f - 1.0f;
    a[id].y = native_sin(x);
}

"""

def initialize():
    plats = cl.get_platforms()
    ctx_props = cl.context_properties
    props = [(ctx_props.PLATFORM, plats[0]),
	    (ctx_props.GL_CONTEXT_KHR, glXGetCurrentContext()),
	    (ctx_props.GLX_DISPLAY_KHR, glXGetCurrentDisplay())]
    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.WRITE_ONLY, 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,), coords_dev)
    cl.enqueue_release_gl_objects(queue, [coords_dev])
    queue.finish()
    glFlush()

def display():
    glClear(GL_COLOR_BUFFER_BIT)
    glDrawArrays(GL_LINE_STRIP, 0, n_vertices)
    glFlush()

def reshape(w, h):
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glMatrixMode(GL_MODELVIEW)

if __name__ == '__main__':
    import sys
    glutInit(sys.argv)
    if len(sys.argv) > 1:
	n_vertices = int(sys.argv[1])
    glutInitWindowSize(800, 160)
    glutInitWindowPosition(0, 0)
    glutCreateWindow('OpenCL/OpenGL Interop Tutorial: Sin Generator')
    glutDisplayFunc(display)
    glutReshapeFunc(reshape)
    initialize()
    glutMainLoop()

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
PyOpenCL mailing list
[email protected]
http://lists.tiker.net/listinfo/pyopencl

Reply via email to