Removed some useless code in cl_utils.h Started to implement support for samplers
authorbsegovia <devnull@localhost>
Sat, 15 Oct 2011 05:34:12 +0000 (05:34 +0000)
committerKeith Packard <keithp@keithp.com>
Fri, 10 Aug 2012 23:15:00 +0000 (16:15 -0700)
src/CMakeLists.txt
src/cl_context.c
src/cl_context.h
src/cl_internals.h
src/cl_mem.h
src/cl_sampler.c
src/cl_sampler.h
src/cl_utils.h
src/intel/intel_gpgpu.c

index d1fff6c..1805ef9 100644 (file)
@@ -8,6 +8,7 @@ SET(OPENCL_SRC
     cl_alloc.c
     cl_kernel.c
     cl_program.c
+    cl_sampler.c
     cl_event.c
     cl_image.c
     cl_mem.c
index 06fdd25..6dee771 100644 (file)
@@ -129,6 +129,7 @@ cl_context_new(void)
   pthread_mutex_init(&ctx->program_lock, NULL);
   pthread_mutex_init(&ctx->queue_lock, NULL);
   pthread_mutex_init(&ctx->buffer_lock, NULL);
+  pthread_mutex_init(&ctx->sampler_lock, NULL);
 
 exit:
   return ctx;
index 051a857..dae7657 100644 (file)
@@ -38,9 +38,11 @@ struct _cl_context {
   cl_command_queue queues;          /* All command queues currently allocated */
   cl_program programs;              /* All programs currently allocated */
   cl_mem buffers;                   /* All memory object currently allocated */
+  cl_sampler samplers;              /* All sampler object currently allocated */
   pthread_mutex_t queue_lock;       /* To allocate and deallocate queues */
   pthread_mutex_t program_lock;     /* To allocate and deallocate programs */
   pthread_mutex_t buffer_lock;      /* To allocate and deallocate buffers */
+  pthread_mutex_t sampler_lock;     /* To allocate and deallocate samplers */
 };
 
 /* Implement OpenCL function */
index 82f9081..b2b25b2 100644 (file)
@@ -27,6 +27,7 @@
 #define CL_MAGIC_CONTEXT_HEADER   0x0ab123456789cdefLL
 #define CL_MAGIC_PROGRAM_HEADER   0x34560ab12789cdefLL
 #define CL_MAGIC_QUEUE_HEADER     0x83650a12b79ce4dfLL
+#define CL_MAGIC_SAMPLER_HEADER   0x686a0ecba79ce33fLL
 #define CL_MAGIC_MEM_HEADER       0x381a27b9ce6504dfLL
 #define CL_MAGIC_DEAD_HEADER      0xdeaddeaddeaddeadLL
 
index 420e869..fff5ff3 100644 (file)
@@ -66,7 +66,7 @@ extern void *cl_mem_map(cl_mem);
 /* Unmap a memory object (just use drm_intel_bo_unmap) */
 extern cl_int cl_mem_unmap(cl_mem);
 
-/* Pin/unpin the buffer in memory (must be root) */
+/* 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);
 
index 12001d6..fd88a77 100644 (file)
  * Author: Benjamin Segovia <benjamin.segovia@intel.com>
  */
 
+#include "cl_context.h"
+#include "cl_sampler.h"
+#include "cl_utils.h"
+#include "cl_alloc.h"
+
+#include <assert.h>
+
+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_sampler sampler = NULL;
+  cl_int err = CL_SUCCESS;
+
+  /* Allocate and inialize the structure itself */
+  TRY_ALLOC (sampler, CALLOC(struct _cl_sampler));
+  sampler->ref_n = 1;
+  sampler->magic = CL_MAGIC_SAMPLER_HEADER;
+  sampler->normalized_coords = normalized_coords;
+  sampler->address = address;
+  sampler->filter = filter;
+
+  /* Append the sampler in the context sampler list */
+  pthread_mutex_lock(&ctx->sampler_lock);
+    sampler->next = ctx->samplers;
+    if (ctx->samplers != NULL)
+      ctx->samplers->prev = sampler;
+    ctx->samplers = sampler;
+  pthread_mutex_unlock(&ctx->sampler_lock);
+  sampler->ctx = ctx;
+  cl_context_add_ref(ctx);
+
+exit:
+  if (errcode_ret)
+    *errcode_ret = err;
+  return sampler;
+error:
+  cl_sampler_delete(sampler);
+  sampler = NULL;
+  goto exit;
+}
+
+LOCAL void
+cl_sampler_delete(cl_sampler sampler)
+{
+  if (UNLIKELY(sampler == NULL))
+    return;
+  if (atomic_dec(&sampler->ref_n) > 1)
+    return;
+
+  assert(sampler->ctx);
+  pthread_mutex_lock(&sampler->ctx->sampler_lock);
+    if (sampler->prev)
+      sampler->prev->next = sampler->next;
+    if (sampler->next)
+      sampler->next->prev = sampler->prev;
+    if (sampler->prev == NULL && sampler->next == NULL)
+      sampler->ctx->samplers = NULL;
+  pthread_mutex_unlock(&sampler->ctx->sampler_lock);
+  cl_context_delete(sampler->ctx);
+
+  cl_free(sampler);
+}
+
+LOCAL void
+cl_sampler_add_ref(cl_sampler sampler)
+{
+  assert(sampler);
+  atomic_inc(&sampler->ref_n);
+}
+
index 479438d..800de4c 100644 (file)
 #ifndef __CL_SAMPLER_H__
 #define __CL_SAMPLER_H__
 
+#include "CL/cl.h"
+#include <stdint.h>
+
+/* How to access images */
 struct _cl_sampler {
+  uint64_t magic;            /* To identify it as a sampler object */
+  volatile int ref_n;        /* This object is reference counted */
+  cl_sampler prev, next;     /* We chain the samplers in the allocator */
+  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 */
 };
 
+/* Create a new sampler object */
+extern cl_sampler cl_sampler_new(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);
+
 #endif /* __CL_SAMPLER_H__ */
 
index 29268c6..913811d 100644 (file)
@@ -72,102 +72,6 @@ do {                                                                \
 #define MIN(x0, x1) ((x0) < (x1) ? (x0) : (x1))
 #define ALIGN(A, B) (((A) % (B)) ? (A) + (B) - ((A) % (B)) : (A))
 
-/* Spawn allocation / destruction / deletion / initialization functions */
-#define __STATIC__ static
-#define __NON_STATIC__
-
-#define DECL_FUNC_INIT(name, qualifier)                     \
-qualifier cl_error_t                                        \
-JOIN(name,_init)(JOIN(name,_t) *x)                          \
-{                                                           \
-  assert(x);                                                \
-  memset(x, 0, sizeof(*x));                                 \
-  return CL_SUCCESS;                                        \
-}
-
-#define DECL_FUNC_NEW(name, qualifier)                      \
-qualifier JOIN(name,_t)*                                    \
-JOIN(name,_new)(void)                                       \
-{                                                           \
-  JOIN(name,_t) *x = (JOIN(name,_t)*)                       \
-    cl_calloc(1, sizeof(JOIN(name,_t)));                    \
-  if (x == NULL)                                            \
-    return NULL;                                            \
-  if (UNLIKELY(JOIN(name,_init)(x) != CL_SUCCESS))          \
-    return NULL;                                            \
-  return x;                                                 \
-}
-
-#define DECL_FUNC_DESTROY(name, qualifier)                  \
-qualifier void                                              \
-JOIN(name,_destroy)(JOIN(name,_t) *x)                       \
-{                                                           \
-}
-
-#define DECL_FUNC_DELETE(name, qualifier)                   \
-qualifier void                                              \
-JOIN(name,_delete)(JOIN(name,_t) *x)                        \
-{                                                           \
-  if (x == NULL)                                            \
-    return;                                                 \
-  JOIN(name,_destroy)(x);                                   \
-  cl_free(x);                                               \
-}
-
-#define DECL_FUNC_CLONE(name, qualifier)                    \
-qualifier JOIN(name,_t)*                                    \
-JOIN(name,_clone)(const JOIN(name,_t) *from)                \
-{                                                           \
-  JOIN(name,_t) *to = NULL;                                 \
-  if (from == NULL)                                         \
-    return NULL;                                            \
-  if (UNLIKELY((to =  JOIN(name,_new)()) == NULL))          \
-    return NULL;                                            \
-  memcpy(to, from, sizeof(*to));                            \
-  return to;                                                \
-}
-
-#define DECL_STATIC_INIT(name) DECL_FUNC_INIT(name, __STATIC__)
-#define DECL_STATIC_NEW(name) DECL_FUNC_NEW(name, __STATIC__)
-#define DECL_STATIC_DESTROY(name) DECL_FUNC_DESTROY(name, __STATIC__)
-#define DECL_STATIC_DELETE(name) DECL_FUNC_DELETE(name, __STATIC__)
-#define DECL_STATIC_CLONE(name) DECL_FUNC_CLONE(name, __STATIC__)
-#define DECL_STATIC_ALL(name)                               \
-  DECL_STATIC_INIT(name)                                    \
-  DECL_STATIC_NEW(name)                                     \
-  DECL_STATIC_DESTROY(name)                                 \
-  DECL_STATIC_DELETE(name)
-
-#define DECL_NON_STATIC_INIT(name) DECL_FUNC_INIT(name, __NON_STATIC__)
-#define DECL_NON_STATIC_NEW(name) DECL_FUNC_NEW(name, __NON_STATIC__)
-#define DECL_NON_STATIC_DESTROY(name) DECL_FUNC_DESTROY(name, __NON_STATIC__)
-#define DECL_NON_STATIC_DELETE(name) DECL_FUNC_DELETE(name, __NON_STATIC__)
-#define DECL_NON_STATIC_CLONE(name) DECL_FUNC_CLONE(name, __NON_STATIC__)
-#define DECL_NON_STATIC_ALL(name)                           \
-  DECL_NON_STATIC_INIT(name)                                \
-  DECL_NON_STATIC_NEW(name)                                 \
-  DECL_NON_STATIC_DESTROY(name)                             \
-  DECL_NON_STATIC_DELETE(name)
-
-/* Make a list from a standard type */
-#define CL_MAKE_LIST(type_name, list_name)                  \
-typedef struct JOIN(_,JOIN(list_name,_t)) {                 \
-  JOIN(type_name,_t) elem;                                  \
-  struct JOIN(_,JOIN(list_name,_t)) *next;                  \
-} JOIN(list_name,_t);                                       \
-DECLARE_STD_INIT(list_name)                                 \
-DECLARE_STD_NEW(list_name)                                  \
-DECLARE_STD_DESTROY(list_name)                              \
-static void                                                 \
-JOIN(list_name,_delete)(JOIN(list_name,_t) *x) {            \
-  if (x == NULL)                                            \
-    return;                                                 \
-  JOIN(list_name,_destroy)(x);                              \
-  JOIN(list_name,_t) *next = x->next;                       \
-  cl_free(x);                                               \
-  JOIN(list_name,_delete)(next);                            \
-}
-
 #define DO_ALLOC_ERROR                                      \
 do {                                                        \
   err = CL_OUT_OF_HOST_MEMORY;                              \
@@ -217,11 +121,23 @@ do {                                                        \
 #define CHECK_MEM(MEM)                                      \
 do {                                                        \
   if (UNLIKELY(MEM == NULL)) {                              \
-    err = CL_INVALID_MEM;                            \
+    err = CL_INVALID_MEM;                                   \
     goto error;                                             \
   }                                                         \
   if (UNLIKELY(MEM->magic != CL_MAGIC_MEM_HEADER)) {        \
-    err = CL_INVALID_MEM;                            \
+    err = CL_INVALID_MEM;                                   \
+    goto error;                                             \
+  }                                                         \
+} while (0)
+
+#define CHECK_SAMPLER(SAMPLER)                              \
+do {                                                        \
+  if (UNLIKELY(SAMPLER == NULL)) {                          \
+    err = CL_INVALID_SAMPLER;                               \
+    goto error;                                             \
+  }                                                         \
+  if (UNLIKELY(SAMPLER->magic != CL_MAGIC_SAMPLER_HEADER)) {\
+    err = CL_INVALID_SAMPLER;                               \
     goto error;                                             \
   }                                                         \
 } while (0)
@@ -314,6 +230,10 @@ do {                                                    \
 /* Number of DWORDS */
 #define SIZEOF32(X) (sizeof(X) / sizeof(uint32_t))
 
+/* Memory quantity */
+#define KB 1024
+#define MB (KB*KB)
+
 /* 32 bits atomic variable */
 typedef volatile int atomic_t;
 
@@ -322,7 +242,6 @@ static INLINE int atomic_add(atomic_t *v, const int c) {
   __asm__ __volatile__("lock ; xaddl %0, %1;"
       : "+r"(i), "+m"(*v)
       : "m"(*v), "r"(i));
-
   return i;
 }
 
index eb5909a..7a08de8 100644 (file)
@@ -47,7 +47,7 @@ struct opaque_sampler_state {
   char opaque[SAMPLER_STATE_SIZE];
 };
 
-/* Store both binding tables and surface states */
+/* Stores both binding tables and surface states */
 typedef struct surface_heap {
   uint32_t binding_table[256];
   char surface[256][sizeof(gen6_surface_state_t)];
@@ -629,8 +629,6 @@ gpgpu_build_sampler_table(intel_gpgpu_t *state)
                  state->samplers);
 }
 
-#define KB 1024
-
 static void
 gpgpu_build_idrt(intel_gpgpu_t *state,
                  genx_gpgpu_kernel_t *kernel,