implement OCL 1.2 new APIs.
authorZhigang Gong <zhigang.gong@linux.intel.com>
Thu, 21 Feb 2013 08:44:40 +0000 (16:44 +0800)
committerZhigang Gong <zhigang.gong@linux.intel.com>
Wed, 10 Apr 2013 06:51:31 +0000 (14:51 +0800)
clCreateImage2D and clCreateFromGLTexture2D have been
deprecated from OCL1.2, we need to implement the new
API clCreateImage/clCreateFromGLTexture to replace them.

Signed-off-by: Zhigang Gong <zhigang.gong@linux.intel.com>
Reviewed-by: Homer Hsing <homer.xing@intel.com>
src/cl_api.c
src/cl_command_queue.c
src/cl_driver.h
src/cl_driver_defs.c
src/cl_gl_api.c
src/cl_mem.c
src/cl_mem.h
src/cl_mem_gl.c
src/intel/intel_driver.c
src/intel/intel_gpgpu.c

index 2c29dce..15ac1eb 100644 (file)
@@ -240,6 +240,30 @@ error:
 }
 
 cl_mem
+clCreateImage(cl_context context,
+              cl_mem_flags flags,
+              const cl_image_format *image_format,
+              const cl_image_desc *image_desc,
+              void *host_ptr,
+              cl_int * errcode_ret)
+{
+  cl_mem mem = NULL;
+  cl_int err = CL_SUCCESS;
+  CHECK_CONTEXT (context);
+
+  mem = cl_mem_new_image(context,
+                         flags,
+                         image_format,
+                         image_desc,
+                         host_ptr,
+                         errcode_ret);
+error:
+  if (errcode_ret)
+    *errcode_ret = err;
+  return mem;
+}
+
+cl_mem
 clCreateImage2D(cl_context              context,
                 cl_mem_flags            flags,
                 const cl_image_format * image_format,
@@ -252,15 +276,19 @@ clCreateImage2D(cl_context              context,
   cl_mem mem = NULL;
   cl_int err = CL_SUCCESS;
   CHECK_CONTEXT (context);
-
-  mem = cl_mem_new_image2D(context,
-                           flags,
-                           image_format,
-                           image_width,
-                           image_height,
-                           image_row_pitch,
-                           host_ptr,
-                           errcode_ret);
+  cl_image_desc image_desc;
+
+  image_desc.image_type = CL_MEM_OBJECT_IMAGE2D;
+  image_desc.image_width = image_width;
+  image_desc.image_height = image_height;
+  image_desc.image_row_pitch = image_row_pitch;
+
+  mem = cl_mem_new_image(context,
+                         flags,
+                         image_format,
+                         &image_desc,
+                         host_ptr,
+                         errcode_ret);
 error:
   if (errcode_ret)
     *errcode_ret = err;
index d31f58a..37e78b4 100644 (file)
@@ -113,10 +113,10 @@ cl_command_queue_bind_surface(cl_command_queue queue, cl_kernel k)
     offset = gbe_kernel_get_curbe_offset(k->opaque, GBE_CURBE_KERNEL_ARGUMENT, i);
     if (arg_type == GBE_ARG_IMAGE) {
       uint32_t *curbe_index = (uint32_t*)(k->curbe + offset);
-      cl_gpgpu_bind_image2D(queue->gpgpu, curbe_index, k->args[i].mem->bo,
-                            k->args[i].mem->intel_fmt, k->args[i].mem->w,
-                            k->args[i].mem->h, k->args[i].mem->pitch,
-                            k->args[i].mem->tiling);
+      cl_gpgpu_bind_image(queue->gpgpu, curbe_index, k->args[i].mem->bo,
+                          k->args[i].mem->intel_fmt, k->args[i].mem->type,
+                          k->args[i].mem->w, k->args[i].mem->h,
+                          k->args[i].mem->pitch, k->args[i].mem->tiling);
     } else if (arg_type == GBE_ARG_SAMPLER) {
       uint32_t *curbe_index = (uint32_t*)(k->curbe + offset);
       cl_gpgpu_insert_sampler(queue->gpgpu, curbe_index, k->args[i].sampler);
index 43719a9..614a78f 100644 (file)
@@ -115,15 +115,16 @@ typedef void (cl_gpgpu_insert_sampler_cb)(cl_gpgpu, uint32_t *curbe_index, cl_sa
 extern cl_gpgpu_insert_sampler_cb *cl_gpgpu_insert_sampler;
 
 /* Set a 2d texture */
-typedef void (cl_gpgpu_bind_image2D_cb)(cl_gpgpu state,
+typedef void (cl_gpgpu_bind_image_cb)(cl_gpgpu state,
                                         uint32_t *curbe_index,
                                         cl_buffer obj_bo,
                                         uint32_t format,
+                                        uint32_t type,
                                         int32_t w,
                                         int32_t h,
                                         int pitch,
                                         cl_gpgpu_tiling tiling);
-extern cl_gpgpu_bind_image2D_cb *cl_gpgpu_bind_image2D;
+extern cl_gpgpu_bind_image_cb *cl_gpgpu_bind_image;
 
 /* Setup a stack */
 typedef void (cl_gpgpu_set_stack_cb)(cl_gpgpu, uint32_t offset, uint32_t size, uint32_t cchint);
@@ -187,7 +188,7 @@ extern cl_buffer_alloc_cb *cl_buffer_alloc;
 
 #include <GL/gl.h>
 #include "CL/cl.h"
-typedef cl_buffer (cl_buffer_alloc_from_texture_cb)(cl_context, cl_mem_flags, GLenum, GLint, GLuint, GLuint);
+typedef cl_buffer (cl_buffer_alloc_from_texture_cb)(cl_context, cl_mem_flags, GLenum, GLint, GLuint);
 extern cl_buffer_alloc_from_texture_cb *cl_buffer_alloc_from_texture;
 
 /* Unref a buffer and destroy it if no more ref */
index a9c1e22..1a78cb7 100644 (file)
@@ -46,7 +46,7 @@ LOCAL cl_gpgpu_new_cb *cl_gpgpu_new = NULL;
 LOCAL cl_gpgpu_delete_cb *cl_gpgpu_delete = NULL;
 LOCAL cl_gpgpu_bind_buf_cb *cl_gpgpu_bind_buf = NULL;
 LOCAL cl_gpgpu_set_stack_cb *cl_gpgpu_set_stack = NULL;
-LOCAL cl_gpgpu_bind_image2D_cb *cl_gpgpu_bind_image2D = NULL;
+LOCAL cl_gpgpu_bind_image_cb *cl_gpgpu_bind_image = NULL;
 LOCAL cl_gpgpu_state_init_cb *cl_gpgpu_state_init = NULL;
 LOCAL cl_gpgpu_set_perf_counters_cb *cl_gpgpu_set_perf_counters = NULL;
 LOCAL cl_gpgpu_upload_constants_cb *cl_gpgpu_upload_constants = NULL;
index 3e9d88f..0c13567 100644 (file)
@@ -44,7 +44,6 @@ do {                                                      \
   }                                                       \
 } while (0)
 
-
 cl_mem
 clCreateFromGLBuffer(cl_context    context,
                      cl_mem_flags  flags,
@@ -76,7 +75,7 @@ clCreateFromGLTexture2D(cl_context    context,
   CHECK_CONTEXT (context);
   CHECK_GL_CONTEXT (context);
 
-  mem = cl_mem_new_gl_texture(context, flags, texture_target, miplevel, texture, 2, &err);
+  mem = cl_mem_new_gl_texture(context, flags, texture_target, miplevel, texture, &err);
 error:
   if (errcode_ret)
     *errcode_ret = err;
@@ -96,13 +95,34 @@ clCreateFromGLTexture3D(cl_context    context,
   CHECK_CONTEXT (context);
   CHECK_GL_CONTEXT (context);
 
-  mem = cl_mem_new_gl_texture(context, flags, texture_target, miplevel, texture, 3, &err);
+  mem = cl_mem_new_gl_texture(context, flags, texture_target, miplevel, texture, &err);
 error:
   if (errcode_ret)
     *errcode_ret = err;
   return mem;
 }
 
+cl_mem
+clCreateFromGLTexture(cl_context      context,
+                      cl_mem_flags    flags,
+                      cl_GLenum       target,
+                      cl_GLint        miplevel,
+                      cl_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, target, miplevel, texture, &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,
index c67d6ce..3a8cfdd 100644 (file)
@@ -237,15 +237,17 @@ cl_mem_copy_data_tiley(cl_mem mem,
   cl_buffer_unmap(mem->bo);
 }
 
-LOCAL cl_mem
-cl_mem_new_image2D(cl_context ctx,
-                   cl_mem_flags flags,
-                   const cl_image_format *fmt,
-                   size_t w,
-                   size_t h,
-                   size_t pitch,
-                   void *data,
-                   cl_int *errcode_ret)
+static cl_mem
+_cl_mem_new_image(cl_context ctx,
+                  cl_mem_flags flags,
+                  const cl_image_format *fmt,
+                  const cl_mem_object_type image_type,
+                  size_t w,
+                  size_t h,
+                  size_t pitch,
+                  int depth,
+                  void *data,
+                  cl_int *errcode_ret)
 {
   cl_int err = CL_SUCCESS;
   cl_mem mem = NULL;
@@ -278,14 +280,17 @@ cl_mem_new_image2D(cl_context ctx,
   } while (0);
   if (UNLIKELY(w == 0)) DO_IMAGE_ERROR;
   if (UNLIKELY(h == 0)) DO_IMAGE_ERROR;
-  if (UNLIKELY(w > ctx->device->image2d_max_width)) DO_IMAGE_ERROR;
-  if (UNLIKELY(h > ctx->device->image2d_max_height)) DO_IMAGE_ERROR;
-  if (UNLIKELY(bpp*w > pitch)) DO_IMAGE_ERROR;
-#undef DO_IMAGE_ERROR
 
-  /* Pick up tiling mode (we do only linear on SNB) */
-  if (cl_driver_get_ver(ctx->drv) != 6)
-    tiling = CL_TILE_Y;
+  if (image_type == CL_MEM_OBJECT_IMAGE2D) {
+    if (UNLIKELY(w > ctx->device->image2d_max_width)) DO_IMAGE_ERROR;
+    if (UNLIKELY(h > ctx->device->image2d_max_height)) DO_IMAGE_ERROR;
+    if (UNLIKELY(data && (bpp*w > pitch))) DO_IMAGE_ERROR;
+
+    /* Pick up tiling mode (we do only linear on SNB) */
+    if (cl_driver_get_ver(ctx->drv) != 6)
+      tiling = CL_TILE_Y;
+  }
+#undef DO_IMAGE_ERROR
 
   /* Tiling requires to align both pitch and height */
   if (tiling == CL_NO_TILE) {
@@ -322,6 +327,7 @@ cl_mem_new_image2D(cl_context ctx,
   mem->is_image = 1;
   mem->pitch = aligned_pitch;
   mem->tiling = tiling;
+  mem->type = image_type;
 
 exit:
   if (errcode_ret)
@@ -333,6 +339,34 @@ error:
   goto exit;
 }
 
+LOCAL cl_mem
+cl_mem_new_image(cl_context context,
+                 cl_mem_flags flags,
+                 const cl_image_format *image_format,
+                 const cl_image_desc *image_desc,
+                 void *host_ptr,
+                 cl_int *errcode_ret)
+{
+  switch (image_desc->image_type) {
+  case CL_MEM_OBJECT_IMAGE1D:
+  case CL_MEM_OBJECT_IMAGE2D:
+    return _cl_mem_new_image(context, flags, image_format, image_desc->image_type,
+                             image_desc->image_width, image_desc->image_height,
+                             image_desc->image_row_pitch, image_desc->image_depth,
+                             host_ptr, errcode_ret);
+  case CL_MEM_OBJECT_IMAGE3D:
+  case CL_MEM_OBJECT_IMAGE2D_ARRAY:
+  case CL_MEM_OBJECT_IMAGE1D_ARRAY:
+  case CL_MEM_OBJECT_IMAGE1D_BUFFER:
+    NOT_IMPLEMENTED;
+    break;
+  case CL_MEM_OBJECT_BUFFER:
+  default:
+    assert(0);
+  }
+  return NULL;
+}
+
 LOCAL void
 cl_mem_delete(cl_mem mem)
 {
index 8618632..9c2890e 100644 (file)
@@ -40,6 +40,7 @@ struct _cl_mem {
   cl_mem_flags flags;       /* Flags specified at the creation time */
   uint32_t is_image;        /* Indicate if this is an image or not */
   cl_image_format fmt;      /* only for images */
+  cl_mem_object_type type;  /* only for images 1D/2D...*/
   size_t w,h,depth,pitch;   /* only for images (depth is only for 3d images) */
   uint32_t intel_fmt;       /* format to provide in the surface state */
   uint32_t bpp;             /* number of bytes per pixel */
@@ -50,14 +51,13 @@ struct _cl_mem {
 extern cl_mem cl_mem_new(cl_context, cl_mem_flags, size_t, void*, cl_int*);
 
 /* Idem but this is an image */
-extern cl_mem cl_mem_new_image2D(cl_context,
-                                 cl_mem_flags,
-                                 const cl_image_format*,
-                                 size_t w,
-                                 size_t h,
-                                 size_t pitch,
-                                 void *,
-                                 cl_int *);
+extern cl_mem
+cl_mem_new_image(cl_context context,
+                 cl_mem_flags flags,
+                 const cl_image_format *image_format,
+                 const cl_image_desc *image_desc,
+                 void *host_ptr,
+                 cl_int *errcode_ret);
 
 cl_mem cl_mem_new_gl_buffer(cl_context ctx,
                             cl_mem_flags flags,
@@ -69,7 +69,6 @@ cl_mem cl_mem_new_gl_texture(cl_context ctx,
                              GLenum texture_target,
                              GLint miplevel,
                              GLuint texture,
-                             GLuint dim,
                              cl_int *errcode_ret);
 
 /* Unref the object and delete it if no more reference */
index 3d6d717..d45962b 100644 (file)
@@ -16,7 +16,6 @@
  *
  * Author: Zhigang Gong <zhigang.gong@intel.com>
  */
-
 #include "cl_mem.h"
 #include "cl_image.h"
 #include "cl_context.h"
@@ -102,12 +101,26 @@ error:
   return ret;
 }
 
+static cl_mem_object_type
+get_mem_type_from_target(GLenum texture_target)
+{
+  switch(texture_target) {
+  case GL_TEXTURE_1D: return CL_MEM_OBJECT_IMAGE1D;
+  case GL_TEXTURE_2D: return CL_MEM_OBJECT_IMAGE2D;
+  case GL_TEXTURE_3D: return CL_MEM_OBJECT_IMAGE3D;
+  case GL_TEXTURE_1D_ARRAY: return CL_MEM_OBJECT_IMAGE1D_ARRAY;
+  case GL_TEXTURE_2D_ARRAY: return CL_MEM_OBJECT_IMAGE2D_ARRAY;
+  default:
+    assert(0);
+  }
+  return 0;
+}
+
 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;
@@ -117,30 +130,14 @@ static int cl_mem_process_texture(cl_context ctx,
   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_IMAGE_DESCRIPTOR;
-    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);
-
+  glBindTexture(texture_target, texture);
   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);
 
+  /* XXX Maybe we'd better to check the hw format in driver? */
   intel_fmt = cl_image_get_intel_format(&cl_format);
 
   if (intel_fmt == INTEL_UNSUPPORTED_FORMAT) {
@@ -151,7 +148,8 @@ static int cl_mem_process_texture(cl_context ctx,
   cl_image_byte_per_pixel(&cl_format, &bpp);
 
   /* XXX What's the tiling? */
-  aligned_pitch = w * bpp;
+  mem->type = get_mem_type_from_target(texture_target);
+  aligned_pitch = ALIGN(w * bpp, 512); /*default tile format is tilex, the width should be 512 alignment.*/
   mem->w = w;
   mem->h = h;
   mem->fmt = cl_format;
@@ -175,12 +173,11 @@ LOCAL cl_mem cl_mem_new_gl_buffer(cl_context ctx,
 
 
 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_mem_flags flags,
+                                   GLenum texture_target,
+                                   GLint miplevel,
+                                   GLuint texture,
+                                   cl_int *errcode_ret)
 {
   cl_int err = CL_SUCCESS;
   cl_mem mem = NULL;
@@ -192,13 +189,13 @@ LOCAL cl_mem cl_mem_new_gl_texture(cl_context ctx,
   }
 
   TRY_ALLOC (mem, CALLOC(struct _cl_mem));
-  if (cl_mem_process_texture(ctx, flags, texture_target, miplevel, texture, dim, mem) != CL_SUCCESS) {
+  if (cl_mem_process_texture(ctx, flags, texture_target, miplevel, texture, mem) != CL_SUCCESS) {
     printf("invalid texture.\n");
     err = CL_INVALID_IMAGE_DESCRIPTOR;
     goto error;
   }
 
-  mem->bo = cl_buffer_alloc_from_texture(ctx, flags, texture_target, miplevel, texture, dim);
+  mem->bo = cl_buffer_alloc_from_texture(ctx, flags, texture_target, miplevel, texture);
 
   if (UNLIKELY(mem->bo == NULL)) {
     err = CL_MEM_OBJECT_ALLOCATION_FAILURE;
index 0be92b6..0f4b133 100644 (file)
@@ -391,8 +391,7 @@ 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 texture)
 {
   GLuint name;
   drm_intel_bo *bo;
index 1eb1e10..06602c8 100644 (file)
@@ -497,11 +497,29 @@ intel_gpgpu_get_free_sampler_index(intel_gpgpu_t *gpgpu)
   return slot - max_sampler_n;
 }
 
+static int
+intel_get_surface_type(cl_mem_object_type type)
+{
+  switch (type) {
+  case CL_MEM_OBJECT_IMAGE1D: return I965_SURFACE_1D;
+  case CL_MEM_OBJECT_IMAGE2D: return I965_SURFACE_2D;
+  case CL_MEM_OBJECT_IMAGE3D: return I965_SURFACE_3D;
+  case CL_MEM_OBJECT_IMAGE1D_BUFFER:
+  case CL_MEM_OBJECT_IMAGE2D_ARRAY:
+  case CL_MEM_OBJECT_IMAGE1D_ARRAY:
+    NOT_IMPLEMENTED;
+    break;
+  default:
+      assert(0);
+  }
+}
+
 static void
-intel_gpgpu_bind_image2D_gen7(intel_gpgpu_t *gpgpu,
+intel_gpgpu_bind_image_gen7(intel_gpgpu_t *gpgpu,
                               uint32_t *curbe_index,
                               dri_bo* obj_bo,
                               uint32_t format,
+                              cl_mem_object_type type,
                               int32_t w,
                               int32_t h,
                               int32_t pitch,
@@ -512,7 +530,8 @@ intel_gpgpu_bind_image2D_gen7(intel_gpgpu_t *gpgpu,
   gen7_surface_state_t *ss = (gen7_surface_state_t *) heap->surface[index];
 
   memset(ss, 0, sizeof(*ss));
-  ss->ss0.surface_type = I965_SURFACE_2D;
+
+  ss->ss0.surface_type = intel_get_surface_type(type);
   ss->ss0.surface_format = format;
   ss->ss1.base_addr = obj_bo->offset;
   ss->ss2.width = w - 1;
@@ -550,16 +569,17 @@ intel_gpgpu_set_stack(intel_gpgpu_t *gpgpu, uint32_t offset, uint32_t size, uint
 }
 
 static void
-intel_gpgpu_bind_image2D(intel_gpgpu_t *gpgpu,
-                         uint32_t *index,
-                         cl_buffer *obj_bo,
-                         uint32_t format,
-                         int32_t w,
-                         int32_t h,
-                         int32_t pitch,
-                         cl_gpgpu_tiling tiling)
-{
-  intel_gpgpu_bind_image2D_gen7(gpgpu, index, (drm_intel_bo*) obj_bo, format, w, h, pitch, tiling);
+intel_gpgpu_bind_image(intel_gpgpu_t *gpgpu,
+                       uint32_t *index,
+                       cl_buffer *obj_bo,
+                       uint32_t format,
+                       cl_mem_object_type type,
+                       int32_t w,
+                       int32_t h,
+                       int32_t pitch,
+                       cl_gpgpu_tiling tiling)
+{
+  intel_gpgpu_bind_image_gen7(gpgpu, index, (drm_intel_bo*) obj_bo, format, type, w, h, pitch, tiling);
   assert(*index < GEN_MAX_SURFACES);
 }
 
@@ -798,7 +818,7 @@ intel_set_gpgpu_callbacks(void)
 {
   cl_gpgpu_new = (cl_gpgpu_new_cb *) intel_gpgpu_new;
   cl_gpgpu_delete = (cl_gpgpu_delete_cb *) intel_gpgpu_delete;
-  cl_gpgpu_bind_image2D = (cl_gpgpu_bind_image2D_cb *) intel_gpgpu_bind_image2D;
+  cl_gpgpu_bind_image = (cl_gpgpu_bind_image_cb *) intel_gpgpu_bind_image;
   cl_gpgpu_bind_buf = (cl_gpgpu_bind_buf_cb *) intel_gpgpu_bind_buf;
   cl_gpgpu_set_stack = (cl_gpgpu_set_stack_cb *) intel_gpgpu_set_stack;
   cl_gpgpu_state_init = (cl_gpgpu_state_init_cb *) intel_gpgpu_state_init;