Cycles: merge of changes from tomato branch.
[blender-staging.git] / intern / cycles / kernel / kernel.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 /* CPU kernel entry points */
20
21 #include "kernel.h"
22 #include "kernel_compat_cpu.h"
23 #include "kernel_math.h"
24 #include "kernel_types.h"
25 #include "kernel_globals.h"
26 #include "kernel_film.h"
27 #include "kernel_path.h"
28 #include "kernel_displace.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 /* Globals */
33
34 KernelGlobals *kernel_globals_create()
35 {
36         KernelGlobals *kg = new KernelGlobals();
37 #ifdef WITH_OSL
38         kg->osl.use = false;
39 #endif
40         return kg;
41 }
42
43 void kernel_globals_free(KernelGlobals *kg)
44 {
45         delete kg;
46 }
47
48 /* OSL */
49
50 #ifdef WITH_OSL
51
52 void *kernel_osl_memory(KernelGlobals *kg)
53 {
54         return (void*)&kg->osl;
55 }
56
57 bool kernel_osl_use(KernelGlobals *kg)
58 {
59         return kg->osl.use;
60 }
61
62 #endif
63
64 /* Memory Copy */
65
66 void kernel_const_copy(KernelGlobals *kg, const char *name, void *host, size_t size)
67 {
68         if(strcmp(name, "__data") == 0)
69                 memcpy(&kg->__data, host, size);
70         else
71                 assert(0);
72 }
73
74 void kernel_tex_copy(KernelGlobals *kg, const char *name, device_ptr mem, size_t width, size_t height)
75 {
76         if(0) {
77         }
78
79 #define KERNEL_TEX(type, ttype, tname) \
80         else if(strcmp(name, #tname) == 0) { \
81                 kg->tname.data = (type*)mem; \
82                 kg->tname.width = width; \
83         }
84 #define KERNEL_IMAGE_TEX(type, ttype, tname)
85 #include "kernel_textures.h"
86
87         else if(strstr(name, "__tex_image_float")) {
88                 texture_image_float4 *tex = NULL;
89                 int id = atoi(name + strlen("__tex_image_float_"));
90                 int array_index = id;
91
92                 if (array_index >= 0 && array_index < MAX_FLOAT_IMAGES) {
93                         tex = &kg->texture_float_images[array_index];
94                 }
95
96                 if(tex) {
97                         tex->data = (float4*)mem;
98                         tex->width = width;
99                         tex->height = height;
100                 }
101         }
102         else if(strstr(name, "__tex_image")) {
103                 texture_image_uchar4 *tex = NULL;
104                 int id = atoi(name + strlen("__tex_image_"));
105                 int array_index = id - MAX_FLOAT_IMAGES;
106
107                 if (array_index >= 0 && array_index < MAX_BYTE_IMAGES) {
108                         tex = &kg->texture_byte_images[array_index];
109                 }
110
111                 if(tex) {
112                         tex->data = (uchar4*)mem;
113                         tex->width = width;
114                         tex->height = height;
115                 }
116         }
117         else
118                 assert(0);
119 }
120
121 /* Path Tracing */
122
123 void kernel_cpu_path_trace(KernelGlobals *kg, float *buffer, unsigned int *rng_state, int sample, int x, int y, int offset, int stride)
124 {
125         kernel_path_trace(kg, buffer, rng_state, sample, x, y, offset, stride);
126 }
127
128 /* Tonemapping */
129
130 void kernel_cpu_tonemap(KernelGlobals *kg, uchar4 *rgba, float *buffer, int sample, int resolution, int x, int y, int offset, int stride)
131 {
132         kernel_film_tonemap(kg, rgba, buffer, sample, resolution, x, y, offset, stride);
133 }
134
135 /* Shader Evaluation */
136
137 void kernel_cpu_shader(KernelGlobals *kg, uint4 *input, float4 *output, int type, int i)
138 {
139         kernel_shader_evaluate(kg, input, output, (ShaderEvalType)type, i);
140 }
141
142 CCL_NAMESPACE_END
143