1 /* This file is part of GEGL
2  *
3  * GEGL is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 3 of the License, or (at your option) any later version.
7  *
8  * GEGL is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
15  *
16  * Copyright 2013 Carlos Zubieta (czubieta.dev@gmail.com)
17  */
18 
19 #include <glib.h>
20 #include "gegl-cl-random.h"
21 #include "opencl/gegl-cl.h"
22 #include "gegl-random-private.h"
23 
24 static cl_mem cl_random_data = NULL;
25 
26 cl_mem
gegl_cl_load_random_data(gint * cl_err)27 gegl_cl_load_random_data (gint *cl_err)
28 {
29   if (cl_random_data == NULL)
30     {
31       guint32 *random_data;
32 
33       random_data = gegl_random_get_data ();
34       cl_random_data = gegl_clCreateBuffer (gegl_cl_get_context (),
35                                             CL_MEM_USE_HOST_PTR | CL_MEM_READ_ONLY,
36                                             RANDOM_DATA_SIZE * sizeof (guint32),
37                                             (void*) random_data,
38                                             cl_err);
39     }
40   else
41     {
42       *cl_err = CL_SUCCESS;
43     }
44   return cl_random_data;
45 }
46 
47 cl_int
gegl_cl_random_cleanup(void)48 gegl_cl_random_cleanup (void)
49 {
50   cl_int cl_err = CL_SUCCESS;
51   if (cl_random_data != NULL)
52     {
53       cl_err = gegl_clReleaseMemObject (cl_random_data);
54       cl_random_data = NULL;
55     }
56   return cl_err;
57 }
58 
59 void
gegl_cl_random_get_ushort4(const GeglRandom * in_rand,cl_ushort4 * out_rand)60 gegl_cl_random_get_ushort4 (const GeglRandom *in_rand,
61                                   cl_ushort4 *out_rand)
62 {
63   out_rand->x = in_rand->prime0;
64   out_rand->y = in_rand->prime1;
65   out_rand->z = in_rand->prime2;
66   out_rand->w = 0;
67 }
68