[Beignet] [PATCH 17/57] Add image common logic to runtime.

junyan.he at inbox.com junyan.he at inbox.com
Sun Jun 11 05:50:03 UTC 2017


From: Junyan He <junyan.he at intel.com>

The cl_image will handle common logic for image of Intel platform.

Signed-off-by: Junyan He <junyan.he at intel.com>
---
 runtime/cl_image.c | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 runtime/cl_image.h |  35 ++++++++++
 2 files changed, 227 insertions(+)
 create mode 100644 runtime/cl_image.c
 create mode 100644 runtime/cl_image.h

diff --git a/runtime/cl_image.c b/runtime/cl_image.c
new file mode 100644
index 0000000..8b61110
--- /dev/null
+++ b/runtime/cl_image.c
@@ -0,0 +1,192 @@
+/* 
+ * 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.1 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 <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia at intel.com>
+ */
+
+#include "CL/cl_ext.h"
+#include "cl_image.h"
+#include "cl_utils.h"
+#include "cl_context.h"
+#include "cl_device_id.h"
+#include <assert.h>
+
+LOCAL cl_int
+cl_image_byte_per_pixel(const cl_image_format *fmt, uint32_t *bpp)
+{
+  assert(bpp);
+
+  if (fmt == NULL)
+    return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+
+  const uint32_t type = fmt->image_channel_data_type;
+  const uint32_t order = fmt->image_channel_order;
+  switch (type) {
+#define DECL_BPP(DATA_TYPE, VALUE) \
+  case DATA_TYPE:                  \
+    *bpp = VALUE;
+    DECL_BPP(CL_SNORM_INT8, 1);
+    break;
+    DECL_BPP(CL_SNORM_INT16, 2);
+    break;
+    DECL_BPP(CL_UNORM_INT8, 1);
+    break;
+    DECL_BPP(CL_UNORM_INT16, 2);
+    break;
+    DECL_BPP(CL_UNORM_SHORT_565, 2);
+    if (order != CL_RGBx && order != CL_RGB)
+      return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+    break;
+    DECL_BPP(CL_UNORM_SHORT_555, 2);
+    if (order != CL_RGBx && order != CL_RGB)
+      return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+    break;
+    DECL_BPP(CL_UNORM_INT_101010, 4);
+    if (order != CL_RGBx && order != CL_RGB)
+      return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+    break;
+    DECL_BPP(CL_SIGNED_INT8, 1);
+    break;
+    DECL_BPP(CL_SIGNED_INT16, 2);
+    break;
+    DECL_BPP(CL_SIGNED_INT32, 4);
+    break;
+    DECL_BPP(CL_UNSIGNED_INT8, 1);
+    break;
+    DECL_BPP(CL_UNSIGNED_INT16, 2);
+    break;
+    DECL_BPP(CL_UNSIGNED_INT32, 4);
+    break;
+    DECL_BPP(CL_HALF_FLOAT, 2);
+    break;
+    DECL_BPP(CL_FLOAT, 4);
+    break;
+#undef DECL_BPP
+  default:
+    return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+  };
+
+  switch (order) {
+  case CL_Rx:
+    break;
+  case CL_R:
+    break;
+  case CL_A:
+    break;
+  case CL_RA:
+    *bpp *= 2;
+    break;
+  case CL_RG:
+    *bpp *= 2;
+    break;
+  case CL_INTENSITY:
+  case CL_LUMINANCE:
+    if (type != CL_UNORM_INT8 && type != CL_UNORM_INT16 &&
+        type != CL_SNORM_INT8 && type != CL_SNORM_INT16 &&
+        type != CL_HALF_FLOAT && type != CL_FLOAT)
+      return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+    break;
+  case CL_RGB:
+  case CL_RGBx:
+    if (type != CL_UNORM_SHORT_555 &&
+        type != CL_UNORM_SHORT_565 &&
+        type != CL_UNORM_INT_101010)
+      return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+    break;
+  case CL_RGBA:
+    *bpp *= 4;
+    break;
+  case CL_ARGB:
+  case CL_BGRA:
+    if (type != CL_UNORM_INT8 && type != CL_SIGNED_INT8 &&
+        type != CL_SNORM_INT8 && type != CL_UNSIGNED_INT8)
+      return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+    *bpp *= 4;
+    break;
+  case CL_sRGBA:
+  case CL_sBGRA:
+    if (type != CL_UNORM_INT8)
+      return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+    *bpp *= 4;
+    break;
+  case CL_NV12_INTEL:
+    break;
+
+  default:
+    return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
+  };
+
+  return CL_SUCCESS;
+}
+
+static const uint32_t cl_image_order[] = {
+  CL_R, CL_A, CL_RG, CL_RA, CL_RGB, CL_RGBA, CL_BGRA, CL_ARGB,
+  CL_INTENSITY, CL_LUMINANCE, CL_Rx, CL_RGx, CL_RGBx, CL_sRGBA, CL_sBGRA};
+
+static const uint32_t cl_image_type[] = {
+  CL_SNORM_INT8, CL_SNORM_INT16, CL_UNORM_INT8, CL_UNORM_INT16,
+  CL_UNORM_SHORT_565, CL_UNORM_SHORT_555, CL_UNORM_INT_101010,
+  CL_SIGNED_INT8, CL_SIGNED_INT16, CL_SIGNED_INT32,
+  CL_UNSIGNED_INT8, CL_UNSIGNED_INT16, CL_UNSIGNED_INT32,
+  CL_HALF_FLOAT, CL_FLOAT};
+
+static const size_t cl_image_order_n = SIZEOF32(cl_image_order);
+static const size_t cl_image_type_n = SIZEOF32(cl_image_type);
+
+cl_int
+cl_image_get_supported_fmt(cl_context ctx,
+                           cl_mem_flags flags,
+                           cl_mem_object_type image_type,
+                           cl_uint num_entries,
+                           cl_image_format *image_formats,
+                           cl_uint *num_image_formats)
+{
+  size_t i, j, k, n = 0;
+  cl_int ret = CL_SUCCESS;
+  cl_bool all_supported;
+
+  for (i = 0; i < cl_image_order_n; ++i) {
+    for (j = 0; j < cl_image_type_n; ++j) {
+      cl_image_format fmt = {
+        .image_channel_order = cl_image_order[i],
+        .image_channel_data_type = cl_image_type[j]};
+      if (cl_image_order[i] >= CL_sRGBA &&
+          ((flags & CL_MEM_WRITE_ONLY) || (flags & CL_MEM_READ_WRITE) ||
+           (flags & CL_MEM_KERNEL_READ_AND_WRITE)))
+        continue;
+
+      all_supported = CL_TRUE;
+      for (k = 0; k < ctx->device_num; k++) {
+        ret = ctx->devices[k]->api.image_format_support(ctx->devices[k], image_type, &fmt);
+        if (ret == CL_FALSE) {
+          all_supported = CL_FALSE;
+          break;
+        }
+      }
+
+      if (all_supported) {
+        if (n < num_entries && image_formats) {
+          image_formats[n] = fmt;
+        }
+        n++;
+      }
+    }
+  }
+
+  if (num_image_formats)
+    *num_image_formats = n;
+  return CL_SUCCESS;
+}
diff --git a/runtime/cl_image.h b/runtime/cl_image.h
new file mode 100644
index 0000000..9343ecd
--- /dev/null
+++ b/runtime/cl_image.h
@@ -0,0 +1,35 @@
+/* 
+ * 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.1 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 <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia at intel.com>
+ */
+
+#ifndef __CL_IMAGE_H__
+#define __CL_IMAGE_H__
+
+#include "CL/cl.h"
+#include <stdint.h>
+
+/* Returned when the OCL format is not supported */
+#define INTEL_UNSUPPORTED_FORMAT ((uint32_t) ~0x0u)
+/* Compute the number of bytes per pixel if the format is supported */
+extern cl_int cl_image_byte_per_pixel(const cl_image_format *fmt, uint32_t *bpp);
+/* Return the intel format for the given OCL format */
+extern uint32_t cl_image_get_intel_format(const cl_image_format *fmt);
+/* Return the list of formats supported by the API */
+extern cl_int cl_image_get_supported_fmt(cl_context context, cl_mem_flags flags, cl_mem_object_type image_type,
+                                         cl_uint num_entries, cl_image_format *image_formats, cl_uint *num_image_formats);
+#endif /* __CL_IMAGE_H__ */
-- 
2.7.4





More information about the Beignet mailing list