[Beignet] [PATCH 4/5] Refine clCreateSampler API.

junyan.he at inbox.com junyan.he at inbox.com
Thu Oct 13 08:40:56 UTC 2016


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

Also add some parameters check to this API.

Signed-off-by: Junyan He <junyan.he at intel.com>
---
 src/cl_api.c         | 17 -----------------
 src/cl_api_sampler.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/cl_sampler.c     | 24 ++++++++++--------------
 src/cl_sampler.h     | 28 ++++++++++------------------
 4 files changed, 67 insertions(+), 49 deletions(-)

diff --git a/src/cl_api.c b/src/cl_api.c
index 210b817..96aebc2 100644
--- a/src/cl_api.c
+++ b/src/cl_api.c
@@ -369,23 +369,6 @@ error:
   return err;
 }
 
-cl_sampler
-clCreateSampler(cl_context         context,
-                cl_bool            normalized,
-                cl_addressing_mode addressing,
-                cl_filter_mode     filter,
-                cl_int *           errcode_ret)
-{
-  cl_sampler sampler = NULL;
-  cl_int err = CL_SUCCESS;
-  CHECK_CONTEXT (context);
-  sampler = cl_sampler_new(context, normalized, addressing, filter, &err);
-error:
-  if (errcode_ret)
-    *errcode_ret = err;
-  return sampler;
-}
-
 cl_int
 clRetainSampler(cl_sampler sampler)
 {
diff --git a/src/cl_api_sampler.c b/src/cl_api_sampler.c
index 3172e1e..ffdcebb 100644
--- a/src/cl_api_sampler.c
+++ b/src/cl_api_sampler.c
@@ -17,6 +17,53 @@
  */
 
 #include "cl_sampler.h"
+#include "cl_context.h"
+#include "cl_device_id.h"
+
+cl_sampler
+clCreateSampler(cl_context context,
+                cl_bool normalized,
+                cl_addressing_mode addressing,
+                cl_filter_mode filter,
+                cl_int *errcode_ret)
+{
+  cl_sampler sampler = NULL;
+  cl_int err = CL_SUCCESS;
+  cl_uint i;
+
+  do {
+    if (!CL_OBJECT_IS_CONTEXT(context)) {
+      err = CL_INVALID_CONTEXT;
+      break;
+    }
+
+    if (addressing < CL_ADDRESS_NONE || addressing > CL_ADDRESS_MIRRORED_REPEAT) {
+      err = CL_INVALID_VALUE;
+      break;
+    }
+
+    if (filter < CL_FILTER_NEAREST || filter > CL_FILTER_LINEAR) {
+      err = CL_INVALID_VALUE;
+      break;
+    }
+
+    /* Check if images are not supported by any device associated with context */
+    for (i = 0; i < context->device_num; i++) {
+      if (context->devices[i]->image_support == CL_FALSE) {
+        err = CL_INVALID_OPERATION;
+        break;
+      }
+    }
+    if (err != CL_SUCCESS)
+      break;
+
+    sampler = cl_create_sampler(context, normalized, addressing, filter, &err);
+  } while (0);
+
+  if (errcode_ret)
+    *errcode_ret = err;
+  return sampler;
+}
 
 cl_int
 clGetSamplerInfo(cl_sampler sampler,
diff --git a/src/cl_sampler.c b/src/cl_sampler.c
index aad2761..d1e6dfe 100644
--- a/src/cl_sampler.c
+++ b/src/cl_sampler.c
@@ -71,17 +71,18 @@ int cl_set_sampler_arg_slot(cl_kernel k, int index, cl_sampler sampler)
 }
 
 LOCAL cl_sampler
-cl_sampler_new(cl_context ctx,
-               cl_bool normalized_coords,
-               cl_addressing_mode address,
-               cl_filter_mode filter,
-               cl_int *errcode_ret)
+cl_create_sampler(cl_context ctx, cl_bool normalized_coords, cl_addressing_mode address,
+                  cl_filter_mode filter, cl_int *errcode_ret)
 {
   cl_sampler sampler = NULL;
-  cl_int err = CL_SUCCESS;
 
   /* Allocate and inialize the structure itself */
-  TRY_ALLOC (sampler, CALLOC(struct _cl_sampler));
+  sampler = cl_calloc(1, sizeof(_cl_sampler));
+  if (sampler == NULL) {
+    *errcode_ret = CL_OUT_OF_HOST_MEMORY;
+    return NULL;
+  }
+
   CL_OBJECT_INIT_BASE(sampler, CL_OBJECT_SAMPLER_MAGIC);
   sampler->normalized_coords = normalized_coords;
   sampler->address = address;
@@ -90,16 +91,11 @@ cl_sampler_new(cl_context ctx,
   /* Append the sampler in the context sampler list */
   cl_context_add_sampler(ctx, sampler);
 
+  // TODO: May move it to other place, it's not a common sampler logic.
   sampler->clkSamplerValue = cl_to_clk(normalized_coords, address, filter);
 
-exit:
-  if (errcode_ret)
-    *errcode_ret = err;
+  *errcode_ret = CL_SUCCESS;
   return sampler;
-error:
-  cl_sampler_delete(sampler);
-  sampler = NULL;
-  goto exit;
 }
 
 LOCAL void
diff --git a/src/cl_sampler.h b/src/cl_sampler.h
index 25d1863..ce06eb4 100644
--- a/src/cl_sampler.h
+++ b/src/cl_sampler.h
@@ -26,35 +26,27 @@
 #include <stdint.h>
 
 /* How to access images */
-struct _cl_sampler {
+typedef struct _cl_sampler {
   _cl_base_object base;
-  cl_context ctx;            /* Context it belongs to */
-  cl_bool normalized_coords; /* Are coordinates normalized? */
-  cl_addressing_mode address;/* CLAMP / REPEAT and so on... */
-  cl_filter_mode filter;     /* LINEAR / NEAREST mostly */
+  cl_context ctx;             /* Context it belongs to */
+  cl_bool normalized_coords;  /* Are coordinates normalized? */
+  cl_addressing_mode address; /* CLAMP / REPEAT and so on... */
+  cl_filter_mode filter;      /* LINEAR / NEAREST mostly */
   uint32_t clkSamplerValue;
-};
+} _cl_sampler;
 
 #define CL_OBJECT_SAMPLER_MAGIC 0x686a0ecba79ce32fLL
-#define CL_OBJECT_IS_SAMPLER(obj) ((obj &&                           \
-         ((cl_base_object)obj)->magic == CL_OBJECT_SAMPLER_MAGIC &&  \
-         CL_OBJECT_GET_REF(obj) >= 1))
+#define CL_OBJECT_IS_SAMPLER(obj) ((obj &&                                                     \
+                                    ((cl_base_object)obj)->magic == CL_OBJECT_SAMPLER_MAGIC && \
+                                    CL_OBJECT_GET_REF(obj) >= 1))
 
 /* Create a new sampler object */
-extern cl_sampler cl_sampler_new(cl_context,
-                                 cl_bool,
-                                 cl_addressing_mode,
-                                 cl_filter_mode,
-                                 cl_int *err);
-
+extern cl_sampler cl_create_sampler(cl_context, cl_bool, cl_addressing_mode, cl_filter_mode, cl_int *err);
 /* Unref the object and delete it if no more reference on it */
 extern void cl_sampler_delete(cl_sampler);
-
 /* Add one more reference to this object */
 extern void cl_sampler_add_ref(cl_sampler);
-
 /* set a sampler kernel argument */
 int cl_set_sampler_arg_slot(cl_kernel k, int index, cl_sampler sampler);
 
 #endif /* __CL_SAMPLER_H__ */
-
-- 
2.7.4





More information about the Beignet mailing list