[gegl/gsoc2011-opencl: 10/10] Unit testing for OpenCL-related functions



commit 0fc26f0a053608f039d4d1530406f8c2b65d1384
Author: Victor Oliveira <victormatheus gmail com>
Date:   Tue Jun 21 17:23:34 2011 -0300

    Unit testing for OpenCL-related functions
    
    This patch adds a opencl folder in gegl/tests where code
    related to the OpenCL support is going to be tested.
    
    Tests added so far:
    
    - GeglClTexture
      * clear
      * copy
      * dup
      * set
      * get
    
    - GeglTile
      * Locking mechanism beetween cpu memory
        and OpenCL memory
    
    In order to run the tests, type "make check" in the new folder.

 configure.ac                                       |    1 +
 gegl/buffer/gegl-buffer-private.h                  |    2 +-
 tests/opencl/Makefile.am                           |   38 +++++++
 tests/opencl/test-gegl-cl-texture-clear.c          |  105 ++++++++++++++++++
 tests/opencl/test-gegl-cl-texture-copy.c           |  111 +++++++++++++++++++
 tests/opencl/test-gegl-cl-texture-dup.c            |  106 ++++++++++++++++++
 tests/opencl/test-gegl-cl-texture-set.c            |  105 ++++++++++++++++++
 ...est-gegl-tile-lock-mode-cl-write-then-cl-read.c |  117 ++++++++++++++++++++
 .../test-gegl-tile-lock-mode-cl-write-then-read.c  |  116 +++++++++++++++++++
 .../test-gegl-tile-lock-mode-write-then-cl-read.c  |  116 +++++++++++++++++++
 .../test-gegl-tile-lock-mode-write-then-read.c     |  114 +++++++++++++++++++
 11 files changed, 930 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 5de301a..a96fddc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1050,6 +1050,7 @@ tests/buffer/tests/Makefile
 tests/compositions/Makefile
 tests/compositions/data/Makefile
 tests/simple/Makefile
+tests/opencl/Makefile
 gegl.pc
 gegl-uninstalled.pc
 ])
diff --git a/gegl/buffer/gegl-buffer-private.h b/gegl/buffer/gegl-buffer-private.h
index 4c5184d..020aca4 100644
--- a/gegl/buffer/gegl-buffer-private.h
+++ b/gegl/buffer/gegl-buffer-private.h
@@ -208,7 +208,7 @@ struct _GeglTile
 
 #ifndef __GEGL_TILE_C
 #define gegl_tile_get_data(tile)  ((guchar*)((tile)->data))
-#define gegl_tile_get_cl_data(tile)  ((tile)->cl_data))
+#define gegl_tile_get_cl_data(tile)  ((tile)->cl_data)
 #endif // __GEGL_TILE_C
 
 
diff --git a/tests/opencl/Makefile.am b/tests/opencl/Makefile.am
new file mode 100644
index 0000000..48432a6
--- /dev/null
+++ b/tests/opencl/Makefile.am
@@ -0,0 +1,38 @@
+# Make the tests run against the build and not the installation
+TESTS_ENVIRONMENT = \
+	GEGL_PATH=$(top_builddir)/operations/common:$(top_builddir)/operations/core:$(top_builddir)/operations/external:$(top_builddir)/operations/affine:$(top_builddir)/operations/generated \
+	ABS_TOP_BUILDDIR=$(top_builddir) \
+	ABS_TOP_SRCDIR=$(top_srcdir)
+
+# The tests
+noinst_PROGRAMS = \
+	test-gegl-cl-texture-dup \
+	test-gegl-cl-texture-copy \
+	test-gegl-cl-texture-set \
+	test-gegl-cl-texture-clear \
+	test-gegl-tile-lock-mode-write-then-read \
+	test-gegl-tile-lock-mode-cl-write-then-read \
+	test-gegl-tile-lock-mode-write-then-cl-read \
+	test-gegl-tile-lock-mode-cl-write-then-cl-read
+
+TESTS = $(noinst_PROGRAMS)
+
+# Common CPPFLAGS
+AM_CPPFLAGS = \
+	-I$(top_srcdir) \
+	-I$(top_builddir)/gegl \
+	-I$(top_srcdir)/gegl \
+	-I$(top_builddir)/gegl/buffer \
+	-I$(top_srcdir)/gegl/buffer \
+	-I$(top_builddir)/gegl/property-types \
+	-I$(top_srcdir)/gegl/property-types \
+	-I$(top_builddir)/gegl/operation \
+	-I$(top_srcdir)/gegl/operation \
+	-I$(top_builddir)/gegl/opencl \
+	-I$(top_srcdir)/gegl/opencl
+
+AM_CFLAGS = $(DEP_CFLAGS) $(BABL_CFLAGS)
+
+# Common libs
+LIBS = $(top_builddir)/gegl/libgegl-$(GEGL_API_VERSION).la	\
+	$(DEP_LIBS) $(BABL_LIBS)
diff --git a/tests/opencl/test-gegl-cl-texture-clear.c b/tests/opencl/test-gegl-cl-texture-clear.c
new file mode 100644
index 0000000..4fa0dd2
--- /dev/null
+++ b/tests/opencl/test-gegl-cl-texture-clear.c
@@ -0,0 +1,105 @@
+/* This file is part of GEGL.
+ *
+ * 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 3 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+#define TEXTURE_WIDTH  (256)
+#define TEXTURE_HEIGHT (256)
+
+#define TEXTURE_SIZE (TEXTURE_WIDTH * TEXTURE_HEIGHT)
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint retval = SUCCESS;
+
+  GeglClTexture *texture;
+  gfloat         *components;
+
+  gegl_init (&argc, &argv);
+
+  gegl_cl_init (NULL);
+
+  if (!gegl_cl_is_accelerated ())
+    {
+      g_warning ("OpenCL support is disabled. Skipping.\n");
+      goto skip;
+    }
+
+  texture = gegl_cl_texture_new (TEXTURE_WIDTH, TEXTURE_HEIGHT);
+
+  components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+    {
+      gint   cnt;
+      gfloat color[4];
+
+      color[0] = g_random_double ();
+      color[1] = g_random_double ();
+      color[2] = g_random_double ();
+      color[3] = g_random_double ();
+
+      for (cnt = 0; cnt < TEXTURE_SIZE; cnt++)
+        {
+          gint index = cnt * 4;
+
+          components[index    ] = color[0];
+          components[index + 1] = color[1];
+          components[index + 2] = color[2];
+          components[index + 3] = color[3];
+        }
+
+      /* set texture to some solid color to make sure that we aren't clearing
+       * an empty texture (note: this assumes that gegl_cl_texture_set()
+       * works as expected)
+       */
+      gegl_cl_texture_set   (texture, components);
+
+      /* clear whole texture */
+      gegl_cl_texture_clear (texture);
+      gegl_cl_texture_get   (texture, components);
+
+      for (cnt = 0; cnt < TEXTURE_SIZE * 4; cnt++)
+        if (components[cnt] > 1E-5f)
+          {
+            g_printerr ("The gegl_cl_texture_clear() test failed. "
+                        "Aborting.\n");
+
+            retval = FAILURE;
+            goto abort;
+          }
+    }
+
+abort:
+  g_free (components);
+  gegl_cl_texture_free (texture);
+
+skip:
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-cl-texture-copy.c b/tests/opencl/test-gegl-cl-texture-copy.c
new file mode 100644
index 0000000..e967734
--- /dev/null
+++ b/tests/opencl/test-gegl-cl-texture-copy.c
@@ -0,0 +1,111 @@
+/* This file is part of GEGL.
+ *
+ * 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 3 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+#define TEXTURE_WIDTH  (256)
+#define TEXTURE_HEIGHT (256)
+
+#define TEXTURE_SIZE (TEXTURE_WIDTH * TEXTURE_HEIGHT)
+
+#define RANDOM_PIXEL_COUNT 1000
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint retval = SUCCESS;
+
+  GeglClTexture *texture1;
+  GeglClTexture *texture2;
+
+  gfloat *components1;
+  gfloat *components2;
+
+  gegl_init (&argc, &argv);
+
+  gegl_cl_init (NULL);
+
+  if (!gegl_cl_is_accelerated ())
+    {
+      g_warning ("OpenCL support is disabled. Skipping.\n");
+      goto skip;
+    }
+
+  texture1 = gegl_cl_texture_new (TEXTURE_WIDTH, TEXTURE_HEIGHT);
+  texture2 = gegl_cl_texture_new (TEXTURE_WIDTH, TEXTURE_HEIGHT);
+
+  components1 = g_new (gfloat, TEXTURE_SIZE * 4);
+  components2 = g_new (gfloat, TEXTURE_SIZE * 4);
+
+    {
+      gint cnt;
+
+      for (cnt = 0; cnt < RANDOM_PIXEL_COUNT; cnt++)
+        {
+          gint i = g_random_int_range (0, TEXTURE_SIZE);
+
+          gfloat *pixel = &components1[i * 4];
+
+          pixel[0] = g_random_double ();
+          pixel[1] = g_random_double ();
+          pixel[2] = g_random_double ();
+          pixel[3] = g_random_double ();
+        }
+
+      /* set the texture to a random image (note: this test assumes that
+       * gegl_cl_texture_set() works as expected)
+       */
+      gegl_cl_texture_set  (texture1, components1);
+
+      /* copy first texture to second texture */
+      gegl_cl_texture_copy (texture1, texture2);
+      gegl_cl_texture_get  (texture2, components2);
+
+      /* compare the two textures */
+      for (cnt = 0; cnt < TEXTURE_SIZE * 4; cnt++)
+        if (!GEGL_FLOAT_EQUAL (components1[cnt], components2[cnt]))
+          {
+            g_printerr ("The gegl_cl_texture_copy() test failed. "
+                        "Aborting.\n");
+
+            retval = FAILURE;
+            goto abort;
+          }
+    }
+
+abort:
+  g_free (components2);
+  g_free (components1);
+
+  gegl_cl_texture_free (texture2);
+  gegl_cl_texture_free (texture1);
+
+skip:
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-cl-texture-dup.c b/tests/opencl/test-gegl-cl-texture-dup.c
new file mode 100644
index 0000000..9995ffd
--- /dev/null
+++ b/tests/opencl/test-gegl-cl-texture-dup.c
@@ -0,0 +1,106 @@
+/* This file is part of GEGL.
+ *
+ * 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 3 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+#define TEXTURE_WIDTH  (256)
+#define TEXTURE_HEIGHT (256)
+
+#define TEXTURE_SIZE (TEXTURE_WIDTH * TEXTURE_HEIGHT)
+
+#define RANDOM_PIXEL_COUNT 1000
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint retval = SUCCESS;
+
+  GeglClTexture *texture1;
+  GeglClTexture *texture2;
+
+  gfloat *components1;
+  gfloat *components2;
+
+  gegl_init (&argc, &argv);
+
+  gegl_cl_init (NULL);
+
+  if (!gegl_cl_is_accelerated ())
+    {
+      g_warning ("OpenCL support is disabled. Skipping.\n");
+      goto skip;
+    }
+
+  texture1 = gegl_cl_texture_new (TEXTURE_WIDTH, TEXTURE_HEIGHT);
+
+  components1 = g_new (gfloat, TEXTURE_SIZE * 4);
+  components2 = g_new (gfloat, TEXTURE_SIZE * 4);
+
+    {
+      gint cnt;
+
+      for (cnt = 0; cnt < RANDOM_PIXEL_COUNT; cnt++)
+        {
+          gint i = g_random_int_range (0, TEXTURE_SIZE);
+
+          gfloat *pixel = &components1[i * 4];
+
+          pixel[0] = g_random_double ();
+          pixel[1] = g_random_double ();
+          pixel[2] = g_random_double ();
+          pixel[3] = g_random_double ();
+        }
+
+      /* set texture to a random image */
+      gegl_cl_texture_set (texture1, components1);
+
+      /* duplicate texture */
+      texture2 = gegl_cl_texture_dup (texture1);
+      gegl_cl_texture_get (texture2, components2);
+
+      /* compare the two images */
+      for (cnt = 0; cnt < TEXTURE_SIZE * 4; cnt++)
+        if (!GEGL_FLOAT_EQUAL (components1[cnt], components2[cnt]))
+          {
+            g_printerr ("The gegl_cl_texture_dup() test failed. Aborting.\n");
+            retval = FAILURE;
+            goto abort;
+          }
+    }
+
+abort:
+  g_free (components2);
+  g_free (components1);
+
+  gegl_cl_texture_free (texture2);
+  gegl_cl_texture_free (texture1);
+
+skip:
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-cl-texture-set.c b/tests/opencl/test-gegl-cl-texture-set.c
new file mode 100644
index 0000000..67668ca
--- /dev/null
+++ b/tests/opencl/test-gegl-cl-texture-set.c
@@ -0,0 +1,105 @@
+/* This file is part of GEGL.
+ *
+ * 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 3 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <string.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+#define TEXTURE_WIDTH  (256)
+#define TEXTURE_HEIGHT (256)
+
+#define TEXTURE_SIZE (TEXTURE_WIDTH * TEXTURE_HEIGHT)
+
+#define RANDOM_PIXEL_COUNT 1000
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint retval = SUCCESS;
+
+  GeglClTexture *texture;
+
+  gfloat *components1;
+  gfloat *components2;
+
+  gegl_init (&argc, &argv);
+
+  gegl_cl_init (NULL);
+
+  if (!gegl_cl_is_accelerated ())
+    {
+      g_warning ("OpenCL support is disabled. Skipping.\n");
+      goto skip;
+    }
+
+  texture = gegl_cl_texture_new (TEXTURE_WIDTH, TEXTURE_HEIGHT);
+
+  components1 = g_new (gfloat, TEXTURE_SIZE * 4);
+  components2 = g_new (gfloat, TEXTURE_SIZE * 4);
+
+    {
+      gint cnt;
+
+      for (cnt = 0; cnt < RANDOM_PIXEL_COUNT; cnt++)
+        {
+          gint i = g_random_int_range (0, TEXTURE_SIZE);
+
+          gfloat *pixel = &components1[i * 4];
+
+          pixel[0] = g_random_double ();
+          pixel[1] = g_random_double ();
+          pixel[2] = g_random_double ();
+          pixel[3] = g_random_double ();
+        }
+
+      /* set the texture to a random image */
+      gegl_cl_texture_set (texture, components1);
+
+      /* get the texture and put it in a different buffer (actually, this test
+       * should also be considered as a test for gegl_cl_texture_get())
+       */
+      gegl_cl_texture_get (texture, components2);
+
+      for (cnt = 0; cnt < TEXTURE_SIZE * 4; cnt++)
+        if (!GEGL_FLOAT_EQUAL (components1[cnt], components2[cnt]))
+          {
+            g_printerr ("The gegl_cl_texture_set() test failed. Aborting.\n");
+            retval = FAILURE;
+            goto abort;
+          }
+    }
+
+abort:
+  g_free (components2);
+  g_free (components1);
+
+  gegl_cl_texture_free (texture);
+
+skip:
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-tile-lock-mode-cl-write-then-cl-read.c b/tests/opencl/test-gegl-tile-lock-mode-cl-write-then-cl-read.c
new file mode 100644
index 0000000..51e8b50
--- /dev/null
+++ b/tests/opencl/test-gegl-tile-lock-mode-cl-write-then-cl-read.c
@@ -0,0 +1,117 @@
+/* This file is part of GEGL.
+ *
+ * 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 3 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor Matheus de Araujo Oliveira <victormatheus gmail com>
+   Based on work from Jerson Michael Perpetua <jersonperpetua gmail com>
+ */
+
+#include <string.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#include "../../gegl/buffer/gegl-tile.h"
+#include "../../gegl/buffer/gegl-buffer-private.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+#define TEXTURE_WIDTH  (256)
+#define TEXTURE_HEIGHT (256)
+#define TEXTURE_SIZE (TEXTURE_WIDTH * TEXTURE_HEIGHT)
+#define TEXTURE_SIZE_BYTES (TEXTURE_SIZE * sizeof(gfloat))
+
+#define RANDOM_PIXEL_COUNT 1000
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint      retval = SUCCESS;
+
+  GeglTile *tile;
+  gfloat   *components;
+
+  gegl_init (&argc, &argv);
+
+  gegl_cl_init (NULL);
+
+  if (!gegl_cl_is_accelerated ())
+    {
+      g_warning ("OpenCL support is disabled. Skipping.\n");
+      goto skip;
+    }
+
+  tile = gegl_tile_new (TEXTURE_SIZE_BYTES * 4);
+  gegl_tile_cl_enable (tile, TEXTURE_WIDTH, TEXTURE_HEIGHT);
+
+  components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+    {
+      gint    cnt;
+      gfloat *tile_components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+      memset (gegl_tile_get_data(tile), 0, TEXTURE_SIZE_BYTES * 4);
+      gegl_cl_texture_clear (gegl_tile_get_cl_data(tile));
+
+      for (cnt = 0; cnt < RANDOM_PIXEL_COUNT; cnt++)
+        {
+          gint i = g_random_int_range (0, TEXTURE_SIZE);
+
+          gfloat *pixel = &components[i * 4];
+
+          pixel[0] = g_random_double ();
+          pixel[1] = g_random_double ();
+          pixel[2] = g_random_double ();
+          pixel[3] = g_random_double ();
+        }
+
+      gegl_tile_lock (tile, GEGL_TILE_LOCK_CL_WRITE);
+
+      /* set tile to a random image */
+      gegl_cl_texture_set (gegl_tile_get_cl_data(tile), components);
+
+      gegl_tile_unlock (tile);
+
+      /* check contents of tile for consistency with previous write */
+      gegl_tile_lock (tile, GEGL_TILE_LOCK_CL_READ);
+
+      gegl_cl_texture_get (gegl_tile_get_cl_data(tile), tile_components);
+
+      for (cnt = 0; cnt < TEXTURE_SIZE * 4; cnt++)
+        if (!GEGL_FLOAT_EQUAL (tile_components[cnt], components[cnt]))
+          {
+            g_printerr ("Tile OpenCL texture inconsistent with original OpenCL texture "
+                        "Aborting.\n");
+
+            retval = FAILURE;
+            break;
+          }
+
+      gegl_tile_unlock (tile);
+      g_free (tile_components);
+    }
+
+  g_free (components);
+  gegl_tile_unref (tile);
+
+skip:
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-tile-lock-mode-cl-write-then-read.c b/tests/opencl/test-gegl-tile-lock-mode-cl-write-then-read.c
new file mode 100644
index 0000000..c9f0a25
--- /dev/null
+++ b/tests/opencl/test-gegl-tile-lock-mode-cl-write-then-read.c
@@ -0,0 +1,116 @@
+/* This file is part of GEGL.
+ *
+ * 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 3 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor Matheus de Araujo Oliveira <victormatheus gmail com>
+   Based on work from Jerson Michael Perpetua <jersonperpetua gmail com>
+ */
+
+#include <string.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#include "../../gegl/buffer/gegl-tile.h"
+#include "../../gegl/buffer/gegl-buffer-private.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+#define TEXTURE_WIDTH  (256)
+#define TEXTURE_HEIGHT (256)
+#define TEXTURE_SIZE (TEXTURE_WIDTH * TEXTURE_HEIGHT)
+#define TEXTURE_SIZE_BYTES (TEXTURE_SIZE * sizeof(gfloat))
+
+#define RANDOM_PIXEL_COUNT 1000
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint      retval = SUCCESS;
+
+  GeglTile *tile;
+  gfloat   *components;
+
+  gegl_init (&argc, &argv);
+
+  gegl_cl_init (NULL);
+
+  if (!gegl_cl_is_accelerated ())
+    {
+      g_warning ("OpenCL support is disabled. Skipping.\n");
+      goto skip;
+    }
+
+  tile = gegl_tile_new (TEXTURE_SIZE_BYTES * 4);
+  gegl_tile_cl_enable (tile, TEXTURE_WIDTH, TEXTURE_HEIGHT);
+
+  components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+    {
+      gint    cnt;
+      gfloat *tile_components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+      memset (gegl_tile_get_data(tile), 0, TEXTURE_SIZE_BYTES * 4);
+      gegl_cl_texture_clear (gegl_tile_get_cl_data(tile));
+
+      for (cnt = 0; cnt < RANDOM_PIXEL_COUNT; cnt++)
+        {
+          gint i = g_random_int_range (0, TEXTURE_SIZE);
+
+          gfloat *pixel = &components[i * 4];
+
+          pixel[0] = g_random_double ();
+          pixel[1] = g_random_double ();
+          pixel[2] = g_random_double ();
+          pixel[3] = g_random_double ();
+        }
+
+      gegl_tile_lock (tile, GEGL_TILE_LOCK_CL_WRITE);
+
+      /* set tile to a random image */
+      gegl_cl_texture_set (gegl_tile_get_cl_data(tile), components);
+
+      gegl_tile_unlock (tile);
+
+      /* check contents of tile for consistency with previous write */
+      /* when locking for reading in cpu, memory copying from the gpu should be done */
+      gegl_tile_lock (tile, GEGL_TILE_LOCK_READ);
+      memcpy (tile_components, gegl_tile_get_data(tile), TEXTURE_SIZE_BYTES * 4);
+
+      for (cnt = 0; cnt < TEXTURE_SIZE * 4; cnt++)
+        if (!GEGL_FLOAT_EQUAL (tile_components[cnt], components[cnt]))
+          {
+            g_printerr ("Tile data inconsistent with original OpenCL texture "
+                        "Aborting.\n");
+
+            retval = FAILURE;
+            break;
+          }
+
+      gegl_tile_unlock (tile);
+    }
+
+  g_free (components);
+  gegl_tile_unref (tile);
+
+skip:
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-tile-lock-mode-write-then-cl-read.c b/tests/opencl/test-gegl-tile-lock-mode-write-then-cl-read.c
new file mode 100644
index 0000000..acb6956
--- /dev/null
+++ b/tests/opencl/test-gegl-tile-lock-mode-write-then-cl-read.c
@@ -0,0 +1,116 @@
+/* This file is part of GEGL.
+ *
+ * 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 3 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor Matheus de Araujo Oliveira <victormatheus gmail com>
+   Based on work from Jerson Michael Perpetua <jersonperpetua gmail com>
+ */
+
+#include <string.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#include "../../gegl/buffer/gegl-tile.h"
+#include "../../gegl/buffer/gegl-buffer-private.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+#define TEXTURE_WIDTH  (256)
+#define TEXTURE_HEIGHT (256)
+#define TEXTURE_SIZE (TEXTURE_WIDTH * TEXTURE_HEIGHT)
+#define TEXTURE_SIZE_BYTES (TEXTURE_SIZE * sizeof(gfloat))
+
+#define RANDOM_PIXEL_COUNT 1000
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint      retval = SUCCESS;
+
+  GeglTile *tile;
+  gfloat   *components;
+
+  gegl_init (&argc, &argv);
+
+  gegl_cl_init (NULL);
+
+  if (!gegl_cl_is_accelerated ())
+    {
+      g_warning ("OpenCL support is disabled. Skipping.\n");
+      goto skip;
+    }
+
+  tile = gegl_tile_new (TEXTURE_SIZE_BYTES * 4);
+  gegl_tile_cl_enable (tile, TEXTURE_WIDTH, TEXTURE_HEIGHT);
+
+  components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+    {
+      gint    cnt;
+      gfloat *tile_components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+      memset (gegl_tile_get_data(tile), 0, TEXTURE_SIZE_BYTES * 4);
+      gegl_cl_texture_clear (gegl_tile_get_cl_data(tile));
+
+      for (cnt = 0; cnt < RANDOM_PIXEL_COUNT; cnt++)
+        {
+          gint i = g_random_int_range (0, TEXTURE_SIZE);
+
+          gfloat *pixel = &components[i * 4];
+
+          pixel[0] = g_random_double ();
+          pixel[1] = g_random_double ();
+          pixel[2] = g_random_double ();
+          pixel[3] = g_random_double ();
+        }
+
+      gegl_tile_lock (tile, GEGL_TILE_LOCK_WRITE);
+
+      /* set tile to a random image */
+      memcpy (gegl_tile_get_data(tile), components, TEXTURE_SIZE_BYTES * 4);
+
+      gegl_tile_unlock (tile);
+
+      /* check contents of tile for consistency with previous write */
+      /* when locking for reading in gpu, memory copying from the cpu should be done */
+      gegl_tile_lock (tile, GEGL_TILE_LOCK_CL_READ);
+      gegl_cl_texture_get (gegl_tile_get_cl_data(tile), tile_components);
+
+      for (cnt = 0; cnt < TEXTURE_SIZE * 4; cnt++)
+        if (!GEGL_FLOAT_EQUAL (tile_components[cnt], components[cnt]))
+          {
+            g_printerr ("Tile OpenCL texture inconsistent with original original data "
+                        "Aborting.\n");
+
+            retval = FAILURE;
+            break;
+          }
+
+      gegl_tile_unlock (tile);
+    }
+
+  g_free (components);
+  gegl_tile_unref (tile);
+
+skip:
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-tile-lock-mode-write-then-read.c b/tests/opencl/test-gegl-tile-lock-mode-write-then-read.c
new file mode 100644
index 0000000..89f7a25
--- /dev/null
+++ b/tests/opencl/test-gegl-tile-lock-mode-write-then-read.c
@@ -0,0 +1,114 @@
+/* This file is part of GEGL.
+ *
+ * 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 3 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor Matheus de Araujo Oliveira <victormatheus gmail com>
+   Based on work from Jerson Michael Perpetua <jersonperpetua gmail com>
+ */
+
+#include <string.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#include "../../gegl/buffer/gegl-tile.h"
+#include "../../gegl/buffer/gegl-buffer-private.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+#define TEXTURE_WIDTH  (256)
+#define TEXTURE_HEIGHT (256)
+#define TEXTURE_SIZE (TEXTURE_WIDTH * TEXTURE_HEIGHT)
+#define TEXTURE_SIZE_BYTES (TEXTURE_SIZE * sizeof(gfloat))
+
+#define RANDOM_PIXEL_COUNT 1000
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint      retval = SUCCESS;
+
+  GeglTile *tile;
+  gfloat   *components;
+
+  gegl_init (&argc, &argv);
+
+  gegl_cl_init (NULL);
+
+  if (!gegl_cl_is_accelerated ())
+    {
+      g_warning ("OpenCL support is disabled. Skipping.\n");
+      goto skip;
+    }
+
+  tile = gegl_tile_new (TEXTURE_SIZE_BYTES * 4);
+  gegl_tile_cl_enable (tile, TEXTURE_WIDTH, TEXTURE_HEIGHT);
+
+  components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+    {
+      gint    cnt;
+      gfloat *tile_components = g_new (gfloat, TEXTURE_SIZE * 4);
+
+      memset (gegl_tile_get_data(tile), 0, TEXTURE_SIZE_BYTES * 4);
+
+      for (cnt = 0; cnt < RANDOM_PIXEL_COUNT; cnt++)
+        {
+          gint i = g_random_int_range (0, TEXTURE_SIZE);
+
+          gfloat *pixel = &components[i * 4];
+
+          pixel[0] = g_random_double ();
+          pixel[1] = g_random_double ();
+          pixel[2] = g_random_double ();
+          pixel[3] = g_random_double ();
+        }
+
+      gegl_tile_lock (tile, GEGL_TILE_LOCK_WRITE);
+
+      /* set tile to a random image */
+      memcpy (gegl_tile_get_data(tile), components, TEXTURE_SIZE_BYTES * 4);
+
+      gegl_tile_unlock (tile);
+
+      gegl_tile_lock (tile, GEGL_TILE_LOCK_READ);
+
+      memcpy (tile_components, gegl_tile_get_data(tile), TEXTURE_SIZE_BYTES * 4);
+
+      for (cnt = 0; cnt < TEXTURE_SIZE * 4; cnt++)
+        if (!GEGL_FLOAT_EQUAL (tile_components[cnt], components[cnt]))
+          {
+            g_printerr ("Tile data inconsistent with original original data "
+                        "Aborting.\n");
+
+            retval = FAILURE;
+            break;
+          }
+
+      gegl_tile_unlock (tile);
+    }
+
+  g_free (components);
+  gegl_tile_unref (tile);
+
+skip:
+  gegl_exit ();
+
+  return retval;
+}



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