[gegl/gsoc2011-opencl] OpenCL Texture Class



commit 94773ee29f53234b424e724bb10b634ef41a2dad
Author: Victor Oliveira <victormatheus gmail com>
Date:   Wed Jun 1 14:25:16 2011 -0300

    OpenCL Texture Class

 gegl/opencl/Makefile.am       |    4 +-
 gegl/opencl/gegl-cl-texture.c |   96 +++++++++++++++++++++++++++++++++++++++++
 gegl/opencl/gegl-cl-texture.h |   43 ++++++++++++++++++
 3 files changed, 142 insertions(+), 1 deletions(-)
---
diff --git a/gegl/opencl/Makefile.am b/gegl/opencl/Makefile.am
index 539b481..53454d6 100644
--- a/gegl/opencl/Makefile.am
+++ b/gegl/opencl/Makefile.am
@@ -17,8 +17,10 @@ noinst_LTLIBRARIES = libcl.la
 #libcl_public_HEADERS = gegl-cl-init.h
 
 libcl_la_SOURCES = \
+	gegl-cl-types.h \
 	gegl-cl-init.c \
 	gegl-cl-init.h \
-	gegl-cl-types.h
+	gegl-cl-texture.c \
+	gegl-cl-texture.h
 
 #libcl_la_SOURCES = $(libcl_sources) $(libcl_public_HEADERS)
diff --git a/gegl/opencl/gegl-cl-texture.c b/gegl/opencl/gegl-cl-texture.c
new file mode 100644
index 0000000..1d9674e
--- /dev/null
+++ b/gegl/opencl/gegl-cl-texture.c
@@ -0,0 +1,96 @@
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-cl-types.h"
+#include "gegl-cl-init.h"
+
+#include "gegl-cl-texture.h"
+
+GeglClTexture *
+gegl_cl_texture_new (gint size,
+                     const Babl *format)
+{
+  cl_int errcode;
+  gint bpp = babl_format_get_bytes_per_pixel (format);
+  gint npixels = (size/bpp);
+
+  GeglClTexture *texture = g_new (GeglClTexture, 1);
+  texture->format = babl_format ("RGBA float");
+  texture->size   = npixels * babl_format_get_bytes_per_pixel (texture->format);
+  texture->data   = gegl_clCreateBuffer (gegl_cl_get_context(), CL_MEM_READ_ONLY,
+                                         texture->size, NULL, &errcode);
+  if (errcode != CL_SUCCESS)
+  {
+    g_free(texture);
+    return NULL;
+  }
+
+  return texture;
+}
+
+void
+gegl_cl_texture_free (GeglClTexture *texture)
+{
+  gegl_clReleaseMemObject (texture->data);
+  g_free (texture);
+}
+
+void
+gegl_cl_texture_get (const GeglClTexture *texture,
+                     gpointer             dst,
+                     const Babl          *format)
+{
+  gpointer buf;
+
+  if (format != NULL && format != texture->format)
+    buf = g_malloc (texture->size);
+  else
+    buf = dst;
+
+  gegl_clEnqueueReadBuffer(gegl_cl_get_command_queue(), 
+                           texture->data, CL_TRUE, 0, texture->size, buf,
+                           0, NULL, NULL);
+  gegl_clFinish(gegl_cl_get_command_queue());
+
+  if (format != NULL && format != texture->format)
+    {
+      Babl *fish = babl_fish ((gpointer) texture->format,
+                              (gpointer) format);
+      gint cl_bpp  = babl_format_get_bytes_per_pixel (texture->format);
+      gint npixels = texture->size/cl_bpp;
+      babl_process (fish, buf, dst, npixels);
+    }
+
+  if (buf != dst)
+    g_free (buf);
+}
+
+void
+gegl_cl_texture_set (GeglClTexture  *texture,
+                     const gpointer  src,
+                     const Babl     *format)
+{
+  gpointer buf;
+
+  if (format != NULL && format != texture->format)
+    {
+      Babl *fish = babl_fish ((gpointer) format,
+                              (gpointer) texture->format);
+
+      gint cl_bpp  = babl_format_get_bytes_per_pixel (texture->format);
+      gint npixels = texture->size/cl_bpp;
+
+      buf = g_malloc (texture->size);
+      babl_process (fish, src, buf, npixels);
+    }
+  else
+    buf = src;
+
+  gegl_clEnqueueWriteBuffer(gegl_cl_get_command_queue(), 
+                            texture->data, CL_TRUE, 0, texture->size, buf,
+                            0, NULL, NULL);
+  gegl_clFinish(gegl_cl_get_command_queue());
+
+  if (buf != src)
+    g_free (buf);
+}
diff --git a/gegl/opencl/gegl-cl-texture.h b/gegl/opencl/gegl-cl-texture.h
new file mode 100644
index 0000000..b66b94a
--- /dev/null
+++ b/gegl/opencl/gegl-cl-texture.h
@@ -0,0 +1,43 @@
+#ifndef __GEGL_CL_TEXTURE_H__
+#define __GEGL_CL_TEXTURE_H__
+
+#include <babl/babl.h>
+#include <glib-object.h>
+
+#include "gegl-cl-init.h"
+
+G_BEGIN_DECLS
+
+struct _GeglClTexture
+{
+  cl_mem data;
+  gint size;
+  Babl *format;
+};
+
+typedef struct _GeglClTexture GeglClTexture;
+
+GType           gegl_cl_texture_get_type (void) G_GNUC_CONST;
+
+GeglClTexture  *gegl_cl_texture_new      (gint                 size,
+                                          const Babl          *format);
+
+void            gegl_cl_texture_free     (GeglClTexture       *texture);
+
+void            gegl_cl_texture_get      (const GeglClTexture *texture,
+                                          gpointer             dest,
+                                          const Babl          *format);
+
+void            gegl_cl_texture_set      (GeglClTexture       *texture,
+                                          const gpointer       src,
+                                          const Babl          *format);
+
+GeglClTexture  *gegl_cl_texture_dup      (const GeglClTexture *texture);
+
+#define GEGL_TYPE_CL_TEXTURE   (gegl_cl_texture_get_type())
+
+#define gegl_cl_texture_get_data(texture) (texture->data)
+
+G_END_DECLS
+
+#endif  /* __GEGL_CL_TEXTURE_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]