summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Zubieta <czubieta.dev@gmail.com>2013-09-06 15:30:51 -0500
committerTéo Mazars <teo.mazars@ensimag.fr>2013-10-31 11:41:44 +0100
commite8565879d62b6068e7e97420c61c146ea6589685 (patch)
tree4ea79f76b7bffa2493bf714836d168659f1f4704
parent56d4ffae24cf3e8d91712fecabbfc292e07bb263 (diff)
Fixing random.cl path error and making it available for all the kernels
-rw-r--r--gegl/opencl/gegl-cl-init.c8
-rw-r--r--gegl/opencl/gegl-cl-random.c3
-rw-r--r--gegl/opencl/gegl-cl.h1
-rw-r--r--opencl/random.cl (renamed from opencl/random.h)4
-rw-r--r--opencl/random.cl.h186
5 files changed, 195 insertions, 7 deletions
diff --git a/gegl/opencl/gegl-cl-init.c b/gegl/opencl/gegl-cl-init.c
index 47c69387..0d39202a 100644
--- a/gegl/opencl/gegl-cl-init.c
+++ b/gegl/opencl/gegl-cl-init.c
@@ -33,6 +33,7 @@
#include <stdio.h>
#include "gegl-cl-color.h"
+#include "opencl/random.cl.h"
#include "gegl/gegl-debug.h"
#include "gegl-config.h"
@@ -445,7 +446,8 @@ gegl_cl_compile_and_build (const char *program_source, const char *kernel_name[]
if ((cl_data = (GeglClRunData *)g_hash_table_lookup(cl_program_hash, program_source)) == NULL)
{
- size_t length = strlen(program_source);
+ const size_t lengths[] = {strlen(cl_random_source), strlen(program_source)};
+ const char *sources[] = {cl_random_source , program_source};
gint i;
guint kernel_n = 0;
@@ -453,8 +455,8 @@ gegl_cl_compile_and_build (const char *program_source, const char *kernel_name[]
cl_data = (GeglClRunData *) g_new(GeglClRunData, 1);
- CL_SAFE_CALL( cl_data->program = gegl_clCreateProgramWithSource(gegl_cl_get_context(), 1, &program_source,
- &length, &errcode) );
+ CL_SAFE_CALL( cl_data->program = gegl_clCreateProgramWithSource(gegl_cl_get_context(), 2, sources,
+ lengths, &errcode) );
errcode = gegl_clBuildProgram(cl_data->program, 0, NULL, NULL, NULL, NULL);
if (errcode != CL_SUCCESS)
diff --git a/gegl/opencl/gegl-cl-random.c b/gegl/opencl/gegl-cl-random.c
index 6814beeb..efd7c97c 100644
--- a/gegl/opencl/gegl-cl-random.c
+++ b/gegl/opencl/gegl-cl-random.c
@@ -20,11 +20,10 @@
#include "gegl-cl-random.h"
#include "opencl/gegl-cl.h"
-/*XXX: This file has to be kept in sync whit gegl-random.c*/
+/*XXX: defined in gegl-random.c*/
#define RANDOM_DATA_SIZE (15083+15091+15101)
#define PRIMES_SIZE 533
-/*declared in gegl-random.c*/
extern gint32 *gegl_random_data;
extern long *gegl_random_primes;
extern inline void gegl_random_init (void);
diff --git a/gegl/opencl/gegl-cl.h b/gegl/opencl/gegl-cl.h
index 0ff978a0..1a4c231b 100644
--- a/gegl/opencl/gegl-cl.h
+++ b/gegl/opencl/gegl-cl.h
@@ -22,6 +22,7 @@
#include "gegl-cl-types.h"
#include "gegl-cl-init.h"
#include "gegl-cl-color.h"
+#include "gegl-cl-random.h"
cl_int gegl_cl_set_kernel_args (cl_kernel kernel, ...) G_GNUC_NULL_TERMINATED;
diff --git a/opencl/random.h b/opencl/random.cl
index fa892460..86b4b3b6 100644
--- a/opencl/random.h
+++ b/opencl/random.cl
@@ -145,7 +145,7 @@ gegl_cl_random_float_range (__global const int *cl_random_data,
seed, x, y, z, n);
return f * (max - min) + min;
}
-
+/*
float4
gegl_cl_random_float4 (__global const int *cl_random_data,
__global const long *cl_random_primes,
@@ -181,4 +181,4 @@ gegl_cl_random_float4_range (__global const int *cl_random_data,
seed, x, y, z, n);
return f4 * (float4)((max - min) + min);
}
-
+*/
diff --git a/opencl/random.cl.h b/opencl/random.cl.h
new file mode 100644
index 00000000..d5b651d3
--- /dev/null
+++ b/opencl/random.cl.h
@@ -0,0 +1,186 @@
+static const char* cl_random_source =
+"/* This file is part of GEGL \n"
+" * \n"
+" * GEGL is free software; you can redistribute it and/or \n"
+" * modify it under the terms of the GNU Lesser General Public \n"
+" * License as published by the Free Software Foundation; either \n"
+" * version 3 of the License, or (at your option) any later version. \n"
+" * \n"
+" * GEGL is distributed in the hope that it will be useful, \n"
+" * but WITHOUT ANY WARRANTY; without even the implied warranty of \n"
+" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU \n"
+" * Lesser General Public License for more details. \n"
+" * \n"
+" * You should have received a copy of the GNU Lesser General Public \n"
+" * License along with GEGL; if not, see <http://www.gnu.org/licenses/>. \n"
+" * \n"
+" * Copyright 2013 Carlos Zubieta (czubieta.dev@gmail.com) \n"
+" */ \n"
+" \n"
+"/* XXX: this file should be kept in sync with gegl-random. */ \n"
+"#define XPRIME 103423 \n"
+"#define YPRIME 101359 \n"
+"#define NPRIME 101111 \n"
+" \n"
+"#define RANDOM_DATA_SIZE (15083+15091+15101) \n"
+"#define PRIME_SIZE 533 \n"
+" \n"
+"static inline int \n"
+"_gegl_cl_random_int (__global const int *cl_random_data, \n"
+" __global const long *cl_random_primes, \n"
+" int seed, \n"
+" int x, \n"
+" int y, \n"
+" int z, \n"
+" int n) \n"
+"{ \n"
+" unsigned long idx = x * XPRIME + \n"
+" y * YPRIME * XPRIME + \n"
+" n * NPRIME * YPRIME * XPRIME; \n"
+"#define ROUNDS 3 \n"
+" /* 3 rounds gives a reasonably high cycle for */ \n"
+" /* our synthesized larger random set. */ \n"
+" unsigned long seed_idx = seed % (PRIME_SIZE - 1 - ROUNDS); \n"
+" int prime0 = convert_int(cl_random_primes[seed_idx]), \n"
+" prime1 = convert_int(cl_random_primes[seed_idx+1]), \n"
+" prime2 = convert_int(cl_random_primes[seed_idx+2]); \n"
+" int r0 = cl_random_data[idx % prime0], \n"
+" r1 = cl_random_data[prime0 + (idx % (prime1))], \n"
+" r2 = cl_random_data[prime0 + prime1 + (idx % (prime2))]; \n"
+" return r0 ^ r1 ^ r2; \n"
+"} \n"
+" \n"
+"int \n"
+"gegl_cl_random_int (__global const int *cl_random_data, \n"
+" __global const long *cl_random_primes, \n"
+" int seed, \n"
+" int x, \n"
+" int y, \n"
+" int z, \n"
+" int n) \n"
+"{ \n"
+" return _gegl_cl_random_int (cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n); \n"
+"} \n"
+" \n"
+"int \n"
+"gegl_cl_random_int_range (__global const int *cl_random_data, \n"
+" __global const long *cl_random_primes, \n"
+" int seed, \n"
+" int x, \n"
+" int y, \n"
+" int z, \n"
+" int n, \n"
+" int min, \n"
+" int max) \n"
+"{ \n"
+" int ret = _gegl_cl_random_int (cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n); \n"
+" return (ret % (max-min)) + min; \n"
+"} \n"
+" \n"
+"int4 \n"
+"gegl_cl_random_int4 (__global const int *cl_random_data, \n"
+" __global const long *cl_random_primes, \n"
+" int seed, \n"
+" int x, \n"
+" int y, \n"
+" int z, \n"
+" int n) \n"
+"{ \n"
+" int r0 = _gegl_cl_random_int(cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n); \n"
+" int r1 = _gegl_cl_random_int(cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n+1); \n"
+" int r2 = _gegl_cl_random_int(cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n+2); \n"
+" int r3 = _gegl_cl_random_int(cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n+3); \n"
+" return (int4)(r0, r1, r2, r3); \n"
+"} \n"
+" \n"
+"int4 \n"
+"gegl_cl_random_int4_range (__global const int *cl_random_data, \n"
+" __global const long *cl_random_primes, \n"
+" int seed, \n"
+" int x, \n"
+" int y, \n"
+" int z, \n"
+" int n, \n"
+" int min, \n"
+" int max) \n"
+"{ \n"
+" int4 ret = gegl_cl_random_int4 (cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n); \n"
+" return (ret % (max-min)) + min; \n"
+"} \n"
+" \n"
+"#define G_RAND_FLOAT_TRANSFORM 0.00001525902189669642175f \n"
+" \n"
+"float \n"
+"gegl_cl_random_float (__global const int *cl_random_data, \n"
+" __global const long *cl_random_primes, \n"
+" int seed, \n"
+" int x, \n"
+" int y, \n"
+" int z, \n"
+" int n) \n"
+"{ \n"
+" int u = _gegl_cl_random_int (cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n); \n"
+" return convert_float(u & 0xffff) * G_RAND_FLOAT_TRANSFORM; \n"
+"} \n"
+" \n"
+"float \n"
+"gegl_cl_random_float_range (__global const int *cl_random_data, \n"
+" __global const long *cl_random_primes, \n"
+" int seed, \n"
+" int x, \n"
+" int y, \n"
+" int z, \n"
+" int n, \n"
+" float min, \n"
+" float max) \n"
+"{ \n"
+" float f = gegl_cl_random_float (cl_random_data, cl_random_primes, \n"
+" seed, x, y, z, n); \n"
+" return f * (max - min) + min; \n"
+"} \n"
+;
+/*
+float4
+gegl_cl_random_float4 (__global const int *cl_random_data,
+ __global const long *cl_random_primes,
+ int seed,
+ int x,
+ int y,
+ int z,
+ int n)
+{
+ float r0 = gegl_cl_random_float(cl_random_data, cl_random_primes,
+ seed x, y, z, n);
+ float r1 = gegl_cl_random_float(cl_random_data, cl_random_primes,
+ seed x, y, z, n+1);
+ float r2 = gegl_cl_random_float(cl_random_data, cl_random_primes,
+ seed x, y, z, n+2);
+ float r3 = gegl_cl_random_float(cl_random_data, cl_random_primes,
+ seed x, y, z, n+3);
+ return (float4)(r0, r1, r2, r3);
+}
+
+float4
+gegl_cl_random_float4_range (__global const int *cl_random_data,
+ __global const long *cl_random_primes,
+ int seed,
+ int x,
+ int y,
+ int z,
+ int n,
+ float min,
+ float max)
+{
+ float4 f = gegl_cl_random_float4 (cl_random_data, cl_random_primes,
+ seed, x, y, z, n);
+ return f4 * (float4)((max - min) + min);
+}
+*/