From 5c71a8498fec286df69c84e738f17f9d93d2606a Mon Sep 17 00:00:00 2001 From: Zhigang Gong Date: Fri, 16 Nov 2012 15:29:37 +0800 Subject: [PATCH] First implementation for extension cl_khr_gl_sharing. Signed-off-by: Zhigang Gong --- Makefile.lib | 2 +- backend/Makefile | 2 +- backend/src/llvm/llvm_passes.cpp | 8 ++ backend/src/llvm/llvm_to_gen.cpp | 2 + backend/src/ocl_stdlib.h | 1 + include/CL/cl.h | 2 + src/cl_context.c | 69 ++++++++---- src/cl_context.h | 23 +++- src/cl_driver.h | 9 +- src/cl_driver_defs.c | 2 +- src/cl_gl_api.c | 128 +++++++++++++++++++++ src/cl_mem.h | 13 +++ src/cl_mem_gl.c | 232 +++++++++++++++++++++++++++++++++++++++ src/cl_platform_id.c | 2 +- src/intel/intel_driver.c | 43 +++++++- 15 files changed, 508 insertions(+), 30 deletions(-) create mode 100644 src/cl_gl_api.c create mode 100644 src/cl_mem_gl.c diff --git a/Makefile.lib b/Makefile.lib index 58bd153..e61e8c1 100644 --- a/Makefile.lib +++ b/Makefile.lib @@ -2,7 +2,7 @@ TOP=. SUBDIRS=src src/sim src/intel src/x11 include Makefile.defs -LIBS=-Wl,--no-undefined $(LIB_BACKEND) $(LOCAL_LIBS) -ldl -lpthread +LIBS=-Wl,--no-undefined $(LIB_BACKEND) $(LOCAL_LIBS) -ldl -lpthread -lGL all: $(LIB) $(LIBMAJOR) $(LIBBASE) diff --git a/backend/Makefile b/backend/Makefile index bf4aca9..43d3c1b 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -1,4 +1,4 @@ TOP=. -SUBDIRS=src/backend src/backend/gen src/backend/sim src/ir src/llvm src/sys +SUBDIRS=src/backend src/backend/gen src/backend/sim src/ir src/llvm src/sys src include $(TOP)/Makefile.shared diff --git a/backend/src/llvm/llvm_passes.cpp b/backend/src/llvm/llvm_passes.cpp index 4881caa..f19278c 100644 --- a/backend/src/llvm/llvm_passes.cpp +++ b/backend/src/llvm/llvm_passes.cpp @@ -24,6 +24,14 @@ * \author Heldge RHodin */ +/* THIS CODE IS DERIVED FROM GPL LLVM PTX BACKEND. CODE IS HERE: + * http://sourceforge.net/scm/?type=git&group_id=319085 + * Note that however, the original author, Heldge Rhodin, granted me (Benjamin + * Segovia) the right to use another license for it (MIT here) + */ + +#define __STDC_LIMIT_MACROS +#define __STDC_CONSTANT_MACROS #include "llvm/CallingConv.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" diff --git a/backend/src/llvm/llvm_to_gen.cpp b/backend/src/llvm/llvm_to_gen.cpp index 21193a5..fc44623 100644 --- a/backend/src/llvm/llvm_to_gen.cpp +++ b/backend/src/llvm/llvm_to_gen.cpp @@ -22,6 +22,8 @@ * \author Benjamin Segovia */ +#define __STDC_LIMIT_MACROS +#define __STDC_CONSTANT_MACROS #include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "llvm/PassManager.h" diff --git a/backend/src/ocl_stdlib.h b/backend/src/ocl_stdlib.h index 8990e27..376a910 100644 --- a/backend/src/ocl_stdlib.h +++ b/backend/src/ocl_stdlib.h @@ -453,6 +453,7 @@ INLINE void barrier(cl_mem_fence_flags flags) { __gen_ocl_barrier_global(); } + ///////////////////////////////////////////////////////////////////////////// // Force the compilation to SIMD8 or SIMD16 ///////////////////////////////////////////////////////////////////////////// diff --git a/include/CL/cl.h b/include/CL/cl.h index 8201afc..b5d7f2e 100644 --- a/include/CL/cl.h +++ b/include/CL/cl.h @@ -144,6 +144,8 @@ typedef struct _cl_buffer_region { #define CL_INVALID_GLOBAL_WORK_SIZE -63 #define CL_INVALID_PROPERTY -64 +#define CL_INVALID_TEXTURE -65 + /* OpenCL Version */ #define CL_VERSION_1_0 1 #define CL_VERSION_1_1 1 diff --git a/src/cl_context.c b/src/cl_context.c index 385c733..d902537 100644 --- a/src/cl_context.c +++ b/src/cl_context.c @@ -27,6 +27,7 @@ #include "cl_driver.h" #include "CL/cl.h" +#include "CL/cl_gl.h" #include #include @@ -34,27 +35,52 @@ #include static cl_int -cl_context_properties_is_ok(const cl_context_properties *properties) +cl_context_properties_process(const cl_context_properties *prop, + struct _cl_context_prop *cl_props) { - const cl_context_properties *prop = properties; - size_t prop_n = 0; cl_int err = CL_SUCCESS; - if (properties == NULL) + cl_props->gl_type = CL_GL_NOSHARE; + cl_props->platform_id = 0; + + if (prop == NULL) goto exit; - while (*prop) { - prop += 2; - prop_n++; - } - /* XXX */ - FATAL_IF (prop_n > 1, "Only one property is supported now"); - INVALID_VALUE_IF (*properties != CL_CONTEXT_PLATFORM); - if (UNLIKELY((cl_platform_id) properties[1] != intel_platform)) { - err = CL_INVALID_PLATFORM; - goto error; - } + while(*prop) { + switch (*prop) { + case CL_CONTEXT_PLATFORM: + cl_props->platform_id = *(prop + 1); + if (UNLIKELY((cl_platform_id) cl_props->platform_id != intel_platform)) { + err = CL_INVALID_PLATFORM; + goto error; + } + break; + case CL_GL_CONTEXT_KHR: + cl_props->gl_context = *(prop + 1); + break; + case CL_EGL_DISPLAY_KHR: + cl_props->gl_type = CL_GL_EGL_DISPLAY; + cl_props->egl_display = *(prop + 1); + break; + case CL_GLX_DISPLAY_KHR: + cl_props->gl_type = CL_GL_GLX_DISPLAY; + cl_props->glx_display = *(prop + 1); + break; + case CL_WGL_HDC_KHR: + cl_props->gl_type = CL_GL_WGL_HDC; + cl_props->wgl_hdc = *(prop + 1); + break; + case CL_CGL_SHAREGROUP_KHR: + cl_props->gl_type = CL_GL_CGL_SHAREGROUP; + cl_props->cgl_sharegroup = *(prop + 1); + break; + default: + err = CL_INVALID_PROPERTY; + goto error; + } + prop += 2; + } exit: error: return err; @@ -75,6 +101,7 @@ cl_create_context(const cl_context_properties * properties, cl_int * errcode_ret) { /* cl_platform_id platform = NULL; */ + struct _cl_context_prop props; cl_context ctx = NULL; cl_int err = CL_SUCCESS; @@ -88,7 +115,10 @@ cl_create_context(const cl_context_properties * properties, FATAL_IF (num_devices != 1, "Only one device is supported"); /* Check that we are getting the right platform */ - if (UNLIKELY((err = cl_context_properties_is_ok(properties)) != CL_SUCCESS)) +// if (UNLIKELY((err = cl_context_properties_is_ok(properties)) != CL_SUCCESS)) +// goto error; + + if (UNLIKELY(((err = cl_context_properties_process(properties, &props)) != CL_SUCCESS))) goto error; /* platform = intel_platform; */ @@ -99,7 +129,7 @@ cl_create_context(const cl_context_properties * properties, } /* We are good */ - if (UNLIKELY((ctx = cl_context_new()) == NULL)) { + if (UNLIKELY((ctx = cl_context_new(&props)) == NULL)) { err = CL_OUT_OF_HOST_MEMORY; goto error; } @@ -118,12 +148,13 @@ error: } LOCAL cl_context -cl_context_new(void) +cl_context_new(struct _cl_context_prop *props) { cl_context ctx = NULL; TRY_ALLOC_NO_ERR (ctx, CALLOC(struct _cl_context)); - TRY_ALLOC_NO_ERR (ctx->drv, cl_driver_new()); + TRY_ALLOC_NO_ERR (ctx->drv, cl_driver_new(props)); + ctx->props = *props; ctx->magic = CL_MAGIC_CONTEXT_HEADER; ctx->ref_n = 1; ctx->ver = cl_driver_get_ver(ctx->drv); diff --git a/src/cl_context.h b/src/cl_context.h index 56fd01c..d9f2fe4 100644 --- a/src/cl_context.h +++ b/src/cl_context.h @@ -30,6 +30,26 @@ /* DRI device created at create context */ struct intel_driver; +enum _cl_gl_context_type { + CL_GL_NOSHARE, + CL_GL_EGL_DISPLAY, + CL_GL_GLX_DISPLAY, + CL_GL_WGL_HDC, + CL_GL_CGL_SHAREGROUP +}; + +struct _cl_context_prop { + cl_context_properties platform_id; + enum _cl_gl_context_type gl_type; + cl_context_properties gl_context; + union { + cl_context_properties egl_display; + cl_context_properties glx_display; + cl_context_properties wgl_hdc; + cl_context_properties cgl_sharegroup; + }; +}; + /* Encapsulate the whole device */ struct _cl_context { uint64_t magic; /* To identify it as a context */ @@ -45,6 +65,7 @@ struct _cl_context { pthread_mutex_t buffer_lock; /* To allocate and deallocate buffers */ pthread_mutex_t sampler_lock; /* To allocate and deallocate samplers */ uint32_t ver; /* Gen version */ + struct _cl_context_prop props; }; /* Implement OpenCL function */ @@ -56,7 +77,7 @@ extern cl_context cl_create_context(const cl_context_properties*, cl_int*); /* Allocate and initialize a context */ -extern cl_context cl_context_new(void); +extern cl_context cl_context_new(struct _cl_context_prop *); /* Destroy and deallocate a context */ extern void cl_context_delete(cl_context); diff --git a/src/cl_driver.h b/src/cl_driver.h index 76e5268..6a155bf 100644 --- a/src/cl_driver.h +++ b/src/cl_driver.h @@ -46,11 +46,13 @@ typedef struct _cl_driver *cl_driver; /* Encapsulates the gpgpu stream of commands */ typedef struct _cl_gpgpu *cl_gpgpu; +typedef struct _cl_context_prop *cl_context_prop; + /************************************************************************** * Driver **************************************************************************/ /* Create a new driver */ -typedef cl_driver (cl_driver_new_cb)(void); +typedef cl_driver (cl_driver_new_cb)(cl_context_prop); extern cl_driver_new_cb *cl_driver_new; /* Delete the driver */ @@ -178,6 +180,11 @@ extern cl_gpgpu_walker_cb *cl_gpgpu_walker; typedef cl_buffer (cl_buffer_alloc_cb)(cl_buffer_mgr, const char*, unsigned long, unsigned long); extern cl_buffer_alloc_cb *cl_buffer_alloc; +#include +#include "CL/cl.h" +typedef cl_buffer (cl_buffer_alloc_from_texture_cb)(cl_context, cl_mem_flags, GLenum, GLint, GLuint, GLuint); +extern cl_buffer_alloc_from_texture_cb *cl_buffer_alloc_from_texture; + /* Unref a buffer and destroy it if no more ref */ typedef void (cl_buffer_unreference_cb)(cl_buffer); extern cl_buffer_unreference_cb *cl_buffer_unreference; diff --git a/src/cl_driver_defs.c b/src/cl_driver_defs.c index 66d805d..a0d0d8c 100644 --- a/src/cl_driver_defs.c +++ b/src/cl_driver_defs.c @@ -16,7 +16,6 @@ * * Author: Benjamin Segovia */ - #include "cl_driver.h" #include "cl_utils.h" #include @@ -30,6 +29,7 @@ LOCAL cl_driver_get_device_id_cb *cl_driver_get_device_id = NULL; /* Buffer */ LOCAL cl_buffer_alloc_cb *cl_buffer_alloc = NULL; +LOCAL cl_buffer_alloc_from_texture_cb *cl_buffer_alloc_from_texture = NULL; LOCAL cl_buffer_reference_cb *cl_buffer_reference = NULL; LOCAL cl_buffer_unreference_cb *cl_buffer_unreference = NULL; LOCAL cl_buffer_map_cb *cl_buffer_map = NULL; diff --git a/src/cl_gl_api.c b/src/cl_gl_api.c new file mode 100644 index 0000000..3e9d88f --- /dev/null +++ b/src/cl_gl_api.c @@ -0,0 +1,128 @@ +/* + * Copyright © 2012 Intel Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Author: Zhigang Gong + */ +#include "cl_platform_id.h" +#include "cl_device_id.h" +#include "cl_context.h" +#include "cl_command_queue.h" +#include "cl_program.h" +#include "cl_kernel.h" +#include "cl_mem.h" +#include "cl_image.h" +#include "cl_sampler.h" +#include "cl_alloc.h" +#include "cl_utils.h" + +#include "CL/cl.h" +#include "CL/cl_gl.h" +#include "CL/cl_intel.h" + +#include +#include +#include + +#define CHECK_GL_CONTEXT(CTX) \ +do { \ + if (UNLIKELY(CTX->props.gl_type == CL_GL_NOSHARE)) { \ + err = CL_INVALID_CONTEXT; \ + goto error; \ + } \ +} while (0) + + +cl_mem +clCreateFromGLBuffer(cl_context context, + cl_mem_flags flags, + GLuint bufobj, + cl_int * errcode_ret) +{ + cl_mem mem = NULL; + cl_int err = CL_SUCCESS; + CHECK_CONTEXT (context); + CHECK_GL_CONTEXT (context); + + mem = cl_mem_new_gl_buffer(context, flags, bufobj, &err); +error: + if (errcode_ret) + *errcode_ret = err; + return mem; +} + +cl_mem +clCreateFromGLTexture2D(cl_context context, + cl_mem_flags flags, + GLenum texture_target, + GLint miplevel, + GLuint texture, + cl_int * errcode_ret) +{ + cl_mem mem = NULL; + cl_int err = CL_SUCCESS; + CHECK_CONTEXT (context); + CHECK_GL_CONTEXT (context); + + mem = cl_mem_new_gl_texture(context, flags, texture_target, miplevel, texture, 2, &err); +error: + if (errcode_ret) + *errcode_ret = err; + return mem; +} + +cl_mem +clCreateFromGLTexture3D(cl_context context, + cl_mem_flags flags, + GLenum texture_target, + GLint miplevel, + GLuint texture, + cl_int * errcode_ret) +{ + cl_mem mem = NULL; + cl_int err = CL_SUCCESS; + CHECK_CONTEXT (context); + CHECK_GL_CONTEXT (context); + + mem = cl_mem_new_gl_texture(context, flags, texture_target, miplevel, texture, 3, &err); +error: + if (errcode_ret) + *errcode_ret = err; + return mem; +} + +/* XXX NULL function currently. */ +cl_int clEnqueueAcquireGLObjects (cl_command_queue command_queue, + cl_uint num_objects, + const cl_mem *mem_objects, + cl_uint num_events_in_wait_list, + const cl_event *event_wait_list, + cl_event *event) +{ + cl_int err = CL_SUCCESS; + return err; +} + +/* XXX NULL function currently. */ +cl_int clEnqueueReleaseGLObjects (cl_command_queue command_queue, + cl_uint num_objects, + const cl_mem *mem_objects, + cl_uint num_events_in_wait_list, + const cl_event *event_wait_list, + cl_event *event) +{ + cl_int err = CL_SUCCESS; + return err; +} diff --git a/src/cl_mem.h b/src/cl_mem.h index 2cb983d..8618632 100644 --- a/src/cl_mem.h +++ b/src/cl_mem.h @@ -59,6 +59,19 @@ extern cl_mem cl_mem_new_image2D(cl_context, void *, cl_int *); +cl_mem cl_mem_new_gl_buffer(cl_context ctx, + cl_mem_flags flags, + GLuint buf_obj, + cl_int *errcode_ret); + +cl_mem cl_mem_new_gl_texture(cl_context ctx, + cl_mem_flags flags, + GLenum texture_target, + GLint miplevel, + GLuint texture, + GLuint dim, + cl_int *errcode_ret); + /* Unref the object and delete it if no more reference */ extern void cl_mem_delete(cl_mem); diff --git a/src/cl_mem_gl.c b/src/cl_mem_gl.c new file mode 100644 index 0000000..255dc6e --- /dev/null +++ b/src/cl_mem_gl.c @@ -0,0 +1,232 @@ +/* + * Copyright © 2012 Intel Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Author: Zhigang Gong + */ + +#include "cl_mem.h" +#include "cl_image.h" +#include "cl_context.h" +#include "cl_utils.h" +#include "cl_alloc.h" +#include "cl_device_id.h" +#include "cl_driver.h" + +#include +#define GL_GLEXT_PROTOTYPES 1 +#include +#include "CL/cl.h" +#include "CL/cl_intel.h" +#include "CL/cl_gl.h" +#include +#include + +static int cl_get_clformat_from_texture(GLint tex_format, cl_image_format * cl_format) +{ + cl_int ret = CL_SUCCESS; + + switch (tex_format) { + case GL_RGBA8: + case GL_RGBA: + case GL_RGBA16: + case GL_RGBA8I: + case GL_RGBA16I: + case GL_RGBA32I: + case GL_RGBA8UI: + case GL_RGBA16UI: + case GL_RGBA32UI: + case GL_RGBA16F: + case GL_RGBA32F: + cl_format->image_channel_order = CL_RGBA; + break; + case GL_BGRA: + cl_format->image_channel_order = CL_BGRA; + break; + default: + ret = CL_INVALID_TEXTURE; + goto error; + } + + switch (tex_format) { + case GL_RGBA8: + case GL_RGBA: + case GL_BGRA: + cl_format->image_channel_data_type = CL_UNORM_INT8; + break; + case GL_RGBA16: + cl_format->image_channel_data_type = CL_UNORM_INT16; + break; + case GL_RGBA8I: + cl_format->image_channel_data_type = CL_SIGNED_INT8; + break; + case GL_RGBA16I: + cl_format->image_channel_data_type = CL_SIGNED_INT16; + break; + case GL_RGBA32I: + cl_format->image_channel_data_type = CL_SIGNED_INT32; + break; + case GL_RGBA8UI: + cl_format->image_channel_data_type = CL_UNSIGNED_INT8; + break; + case GL_RGBA16UI: + cl_format->image_channel_data_type = CL_UNSIGNED_INT16; + break; + case GL_RGBA32UI: + cl_format->image_channel_data_type = CL_UNSIGNED_INT32; + break; + case GL_RGBA16F: + cl_format->image_channel_data_type = CL_HALF_FLOAT; + break; + case GL_RGBA32F: + cl_format->image_channel_order = CL_FLOAT; + break; + default: + ret = CL_INVALID_TEXTURE; + goto error; + } + +error: + return ret; +} + +static int cl_mem_process_texture(cl_context ctx, + cl_mem_flags flags, + GLenum texture_target, + GLint miplevel, + GLuint texture, + GLuint dim, + cl_mem mem) +{ + cl_int err = CL_SUCCESS; + GLint tex_format; + cl_image_format cl_format; + /* XXX why we use vendor related structure in this layer? */ + uint32_t intel_fmt, bpp, aligned_pitch; + int w,h; + + if ((dim == 2 && texture_target != GL_TEXTURE_2D) + || (dim == 3 && texture_target != GL_TEXTURE_3D)) { + err = CL_INVALID_TEXTURE; + goto error; + } + + if (dim == 2) + glBindTexture(GL_TEXTURE_2D, texture); + else if (dim == 3) + glBindTexture(GL_TEXTURE_3D, texture); + + glTexParameteri(GL_TEXTURE_2D, + GL_TEXTURE_MIN_FILTER, + GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, + GL_TEXTURE_MAG_FILTER, + GL_NEAREST); + + glGetTexLevelParameteriv(texture_target, miplevel, GL_TEXTURE_WIDTH, &w); + glGetTexLevelParameteriv(texture_target, miplevel, GL_TEXTURE_HEIGHT, &h); + glGetTexLevelParameteriv(texture_target, miplevel, GL_TEXTURE_INTERNAL_FORMAT, &tex_format); + + cl_get_clformat_from_texture(tex_format, &cl_format); + + intel_fmt = cl_image_get_intel_format(&cl_format); + + if (intel_fmt == INTEL_UNSUPPORTED_FORMAT) { + err = CL_INVALID_TEXTURE; + goto error; + } + + cl_image_byte_per_pixel(&cl_format, &bpp); + + /* XXX What's the tiling? */ + aligned_pitch = w * bpp; + mem->w = w; + mem->h = h; + mem->fmt = cl_format; + mem->intel_fmt = intel_fmt; + mem->bpp = bpp; + mem->is_image = 1; + mem->pitch = aligned_pitch; + mem->tiling = 0; + +error: + return err; +} + +LOCAL cl_mem cl_mem_new_gl_buffer(cl_context ctx, + cl_mem_flags flags, + GLuint buf_obj, + cl_int *errcode_ret) +{ + NOT_IMPLEMENTED; +} + + +LOCAL cl_mem cl_mem_new_gl_texture(cl_context ctx, + cl_mem_flags flags, + GLenum texture_target, + GLint miplevel, + GLuint texture, + GLuint dim, + cl_int *errcode_ret) +{ + cl_int err = CL_SUCCESS; + cl_mem mem = NULL; + + /* Check flags consistency */ + if (UNLIKELY(flags & CL_MEM_COPY_HOST_PTR)) { + err = CL_INVALID_ARG_VALUE; + goto error; + } + + TRY_ALLOC (mem, CALLOC(struct _cl_mem)); + if (cl_mem_process_texture(ctx, flags, texture_target, miplevel, texture, dim, mem) != CL_SUCCESS) { + printf("invalid texture.\n"); + err = CL_INVALID_TEXTURE; + goto error; + } + + mem->bo = cl_buffer_alloc_from_texture(ctx, flags, texture_target, miplevel, texture, dim); + + if (UNLIKELY(mem->bo == NULL)) { + err = CL_MEM_ALLOCATION_FAILURE; + goto error; + } + + mem->ref_n = 1; + mem->magic = CL_MAGIC_MEM_HEADER; + mem->flags = flags; + mem->ctx = ctx; + + /* Append the buffer in the context buffer list */ + pthread_mutex_lock(&ctx->buffer_lock); + mem->next = ctx->buffers; + if (ctx->buffers != NULL) + ctx->buffers->prev = mem; + ctx->buffers = mem; + pthread_mutex_unlock(&ctx->buffer_lock); + mem->ctx = ctx; + cl_context_add_ref(ctx); + +exit: + if (errcode_ret) + *errcode_ret = err; + return mem; +error: + cl_mem_delete(mem); + mem = NULL; + goto exit; + +} diff --git a/src/cl_platform_id.c b/src/cl_platform_id.c index 3c95513..ae38296 100644 --- a/src/cl_platform_id.c +++ b/src/cl_platform_id.c @@ -47,7 +47,7 @@ static struct _cl_platform_id intel_platform_data = { DECL_INFO_STRING(version, "OpenCL 1.1") DECL_INFO_STRING(name, "Experiment Intel Gen OCL Driver") DECL_INFO_STRING(vendor, "Intel") - DECL_INFO_STRING(extensions, "") + DECL_INFO_STRING(extensions, "cl_khr_gl_sharing") }; #undef DECL_INFO_STRING diff --git a/src/intel/intel_driver.c b/src/intel/intel_driver.c index 0c79713..3e53344 100644 --- a/src/intel/intel_driver.c +++ b/src/intel/intel_driver.c @@ -45,6 +45,7 @@ * Zou Nan hai * */ +#define GL_GLEXT_PROTOTYPES #include "intel_driver.h" #include "intel_gpgpu.h" #include "intel_batchbuffer.h" @@ -61,6 +62,7 @@ #include "cl_utils.h" #include "cl_alloc.h" +#include "cl_context.h" #include "cl_driver.h" #define SET_BLOCKED_SIGSET(DRIVER) do { \ @@ -163,9 +165,19 @@ intel_driver_init(intel_driver_t *driver, int dev_fd) } static void -intel_driver_open(intel_driver_t *intel) +intel_driver_open(intel_driver_t *intel, cl_context_prop props) { int cardi; + /* XXX We should use the display to get the correct dri driver to open. + * for now, we just use generic way to open a dri driver.*/ + if (props != NULL + && props->gl_type != CL_GL_NOSHARE + && props->gl_type != CL_GL_GLX_DISPLAY + && props->gl_type != CL_GL_EGL_DISPLAY) { + printf("Unsupported gl share type %d.\n", props->gl_type); + exit(-1); + } + intel->x11_display = XOpenDisplay(":0.0"); if(intel->x11_display) { @@ -317,7 +329,7 @@ intel_driver_shared_name(intel_driver_t *driver, dri_bo *bo) dri_bo_flink(bo, &name); return name; } - +/* XXX a null props is ok? */ static int intel_get_device_id(void) { @@ -326,7 +338,7 @@ intel_get_device_id(void) driver = intel_driver_new(); assert(driver != NULL); - intel_driver_open(driver); + intel_driver_open(driver, NULL); intel_device_id = driver->device_id; intel_driver_close(driver); intel_driver_terminate(driver); @@ -346,11 +358,11 @@ cl_intel_driver_delete(intel_driver_t *driver) } static intel_driver_t* -cl_intel_driver_new(void) +cl_intel_driver_new(cl_context_prop props) { intel_driver_t *driver = NULL; TRY_ALLOC_NO_ERR (driver, intel_driver_new()); - intel_driver_open(driver); + intel_driver_open(driver, props); exit: return driver; @@ -375,6 +387,26 @@ intel_driver_get_ver(struct intel_driver *drv) static size_t drm_intel_bo_get_size(drm_intel_bo *bo) { return bo->size; } static void* drm_intel_bo_get_virtual(drm_intel_bo *bo) { return bo->virtual; } +#include +#include +static cl_buffer intel_alloc_buffer_from_texture(cl_context ctx, + cl_mem_flags flags, + GLenum texture_target, + GLint miplevel, + GLuint texture, + GLuint dim) +{ + GLuint name; + drm_intel_bo *bo; + + glGetOCLSharedTexturesINTEL(texture_target, miplevel, 1, &texture, (void**)&bo); + + dri_bo_flink(bo, &name); + + return (cl_buffer)intel_driver_share_buffer((intel_driver_t *)ctx->drv, name); +} + + LOCAL void intel_setup_callbacks(void) { @@ -384,6 +416,7 @@ intel_setup_callbacks(void) cl_driver_get_bufmgr = (cl_driver_get_bufmgr_cb *) intel_driver_get_bufmgr; cl_driver_get_device_id = (cl_driver_get_device_id_cb *) intel_get_device_id; cl_buffer_alloc = (cl_buffer_alloc_cb *) drm_intel_bo_alloc; + cl_buffer_alloc_from_texture = (cl_buffer_alloc_from_texture_cb *) intel_alloc_buffer_from_texture; cl_buffer_reference = (cl_buffer_reference_cb *) drm_intel_bo_reference; cl_buffer_unreference = (cl_buffer_unreference_cb *) drm_intel_bo_unreference; cl_buffer_map = (cl_buffer_map_cb *) drm_intel_bo_map; -- 2.7.4