From 5029a6ef7f88865be88b4f2d629abf56abe5a27c Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Mon, 19 Jul 2010 22:15:12 -0500 Subject: [PATCH] GL interpop fixes by Gregor Thalhammer. --- examples/gl_interop_demo.py | 165 ++++++++++++++++++++---------------- src/wrapper/wrap_cl.hpp | 10 ++- 2 files changed, 98 insertions(+), 77 deletions(-) diff --git a/examples/gl_interop_demo.py b/examples/gl_interop_demo.py index dd4bf3e3..29ee8a17 100644 --- a/examples/gl_interop_demo.py +++ b/examples/gl_interop_demo.py @@ -1,75 +1,90 @@ -from OpenGL.GL import * -from OpenGL.GLUT import * -from OpenGL.raw.GL.VERSION.GL_1_5 import glBufferData as rawGlBufferData -try: - from OpenGL.WGL import wglGetCurrentDisplay as GetCurrentDisplay, wglGetCurrentContext as GetCurrentContext -except: - pass -try: - from OpenGL.GLX import glXGetCurrentDisplay as GetCurrentDisplay, glXGetCurrentContext as GetCurrentContext -except: - pass -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, - GetCurrentContext()), (ctx_props.GLX_DISPLAY_KHR, GetCurrentDisplay())] - 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 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() +from OpenGL.GL import * +from OpenGL.GLUT import * +from OpenGL.raw.GL.VERSION.GL_1_5 import glBufferData as rawGlBufferData + +from OpenGL import platform + +try: + import OpenGL.WGL +except: + pass +try: + import OpenGL.GLX +except: + pass +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, platform.GetCurrentContext())] + + import sys + if sys.platform == "linux2": + props.append( + (ctx_props.GLX_DISPLAY_KHR, + OpenGl.GLX.glXGetCurrentDisplay())) + elif sys.platform == "nt": + props.append( + (ctx_props.WGL_HDC_KHR, + OpenGL.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 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() diff --git a/src/wrapper/wrap_cl.hpp b/src/wrapper/wrap_cl.hpp index b12286c9..2e977039 100644 --- a/src/wrapper/wrap_cl.hpp +++ b/src/wrapper/wrap_cl.hpp @@ -19,6 +19,12 @@ #include <CL/cl.h> // FIXME: Nvidia doesn't install cl_ext.h by default. Grr. // #include <CL/cl_ext.h> + +#ifdef _WIN32 +#define NOMINMAX +#include <windows.h> +#endif + #ifdef HAVE_GL #include <GL/gl.h> #include <CL/cl_gl.h> @@ -2874,8 +2880,8 @@ namespace pyopencl std::vector<cl_context_properties> props = parse_context_properties(py_properties); - typedef CL_API_ENTRY cl_int CL_API_CALL - (*func_ptr_type)(const cl_context_properties * /* properties */, + typedef CL_API_ENTRY cl_int (CL_API_CALL + *func_ptr_type)(const cl_context_properties * /* properties */, cl_gl_context_info /* param_name */, size_t /* param_value_size */, void * /* param_value */, -- GitLab