[Beignet] [PATCH 1/2] Implement clEnqueueMapImage.

Dag Lem dag at nimrod.no
Mon May 20 03:28:27 PDT 2013


For correct mapping and unmapping of tiled/untiled buffers, the new
functions cl_mem_map_auto and cl_mem_unmap_auto are used in
clEnqueueMapBuffer, clEnqueueMapImage, and clEnqueueUnmapMemObject.

Signed-off-by: Dag Lem <dag at nimrod.no>
---
 src/cl_api.c   | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 src/cl_mem.c   | 19 +++++++++++++++
 src/cl_mem.h   |  6 +++++
 src/cl_utils.h |  9 +++++++
 4 files changed, 103 insertions(+), 6 deletions(-)

diff --git a/src/cl_api.c b/src/cl_api.c
index 3981554..d4470c7 100644
--- a/src/cl_api.c
+++ b/src/cl_api.c
@@ -1048,13 +1048,30 @@ clEnqueueMapBuffer(cl_command_queue  command_queue,
                    cl_event *        event,
                    cl_int *          errcode_ret)
 {
-  void *p;
+  void *ptr = NULL;
+  cl_int err = CL_SUCCESS;
+
+  CHECK_QUEUE(command_queue);
+  CHECK_MEM(buffer);
+  if (command_queue->ctx != buffer->ctx) {
+    err = CL_INVALID_CONTEXT;
+    goto error;
+  }
+
   if (blocking_map != CL_TRUE)
      NOT_IMPLEMENTED;
   if (offset != 0)
      NOT_IMPLEMENTED;
-  p = clMapBufferIntel(buffer, errcode_ret);
-  return p;
+
+  if (!(ptr = cl_mem_map_auto(buffer))) {
+    err = CL_MAP_FAILURE;
+    goto error;
+  }
+
+error:
+  if (errcode_ret)
+    *errcode_ret = err;
+  return ptr;
 }
 
 void *
@@ -1071,8 +1088,54 @@ clEnqueueMapImage(cl_command_queue   command_queue,
                   cl_event *         event,
                   cl_int *           errcode_ret)
 {
-  NOT_IMPLEMENTED;
-  return NULL;
+  void *ptr = NULL;
+  cl_int err = CL_SUCCESS;
+
+  CHECK_QUEUE(command_queue);
+  CHECK_IMAGE(image);
+  if (command_queue->ctx != image->ctx) {
+    err = CL_INVALID_CONTEXT;
+    goto error;
+  }
+
+  if (blocking_map != CL_TRUE)
+    NOT_IMPLEMENTED;
+
+  if (!origin || !region || origin[0] + region[0] > image->w || origin[1] + region[1] > image->h || origin[2] + region[2] > image->depth) {
+    err = CL_INVALID_VALUE;
+    goto error;
+  }
+
+  if (!image_row_pitch || (image->slice_pitch && !image_slice_pitch)) {
+    err = CL_INVALID_VALUE;
+    goto error;
+  }
+
+  *image_row_pitch = image->row_pitch;
+  if (image_slice_pitch)
+    *image_slice_pitch = image->slice_pitch;
+
+  if ((map_flags & CL_MAP_READ &&
+       image->flags & (CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS)) ||
+      (map_flags & (CL_MAP_WRITE | CL_MAP_WRITE_INVALIDATE_REGION) &&
+       image->flags & (CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS)))
+  {
+    err = CL_INVALID_OPERATION;
+    goto error;
+  }
+
+  if (!(ptr = cl_mem_map_auto(image))) {
+    err = CL_MAP_FAILURE;
+    goto error;
+  }
+
+  size_t offset = image->bpp*origin[0] + image->row_pitch*origin[1] + image->slice_pitch*origin[2];
+  ptr = (char*)ptr + offset;
+
+error:
+  if (errcode_ret)
+    *errcode_ret = err;
+  return ptr;
 }
 
 cl_int
@@ -1083,7 +1146,7 @@ clEnqueueUnmapMemObject(cl_command_queue  command_queue,
                         const cl_event *  event_wait_list,
                         cl_event *        event)
 {
-  return clUnmapBufferIntel(memobj);
+  return cl_mem_unmap_auto(memobj);
 }
 
 cl_int
diff --git a/src/cl_mem.c b/src/cl_mem.c
index a832de1..354fe34 100644
--- a/src/cl_mem.c
+++ b/src/cl_mem.c
@@ -522,6 +522,25 @@ cl_mem_unmap_gtt(cl_mem mem)
   return CL_SUCCESS;
 }
 
+LOCAL void*
+cl_mem_map_auto(cl_mem mem)
+{
+  if (mem->is_image && mem->tiling != CL_NO_TILE)
+    return cl_mem_map_gtt(mem);
+  else
+    return cl_mem_map(mem);
+}
+
+LOCAL cl_int
+cl_mem_unmap_auto(cl_mem mem)
+{
+  if (mem->is_image && mem->tiling != CL_NO_TILE)
+    cl_buffer_unmap_gtt(mem->bo);
+  else
+    cl_buffer_unmap(mem->bo);
+  return CL_SUCCESS;
+}
+
 LOCAL cl_int
 cl_mem_pin(cl_mem mem)
 {
diff --git a/src/cl_mem.h b/src/cl_mem.h
index 341ccd9..3ca6381 100644
--- a/src/cl_mem.h
+++ b/src/cl_mem.h
@@ -83,6 +83,12 @@ extern void *cl_mem_map_gtt(cl_mem);
 /* Unmap a memory object in GTT mode */
 extern cl_int cl_mem_unmap_gtt(cl_mem);
 
+/* Directly map a memory object - tiled images are mapped in GTT mode */
+extern void *cl_mem_map_auto(cl_mem);
+
+/* Unmap a memory object - tiled images are unmapped in GTT mode */
+extern cl_int cl_mem_unmap_auto(cl_mem);
+
 /* Pin/unpin the buffer in memory (you must be root) */
 extern cl_int cl_mem_pin(cl_mem);
 extern cl_int cl_mem_unpin(cl_mem);
diff --git a/src/cl_utils.h b/src/cl_utils.h
index 4493858..dfb1369 100644
--- a/src/cl_utils.h
+++ b/src/cl_utils.h
@@ -130,6 +130,15 @@ do {                                                        \
   }                                                         \
 } while (0)
 
+#define CHECK_IMAGE(IMAGE)                                  \
+CHECK_MEM(image);                                           \
+do {                                                        \
+  if (UNLIKELY(!IMAGE->is_image)) {                         \
+    err = CL_INVALID_MEM_OBJECT;                            \
+    goto error;                                             \
+  }                                                         \
+} while (0)
+
 #define CHECK_SAMPLER(SAMPLER)                              \
 do {                                                        \
   if (UNLIKELY(SAMPLER == NULL)) {                          \
-- 
1.8.1.4



More information about the Beignet mailing list