Brightness/Contrast Node for Cycles
[blender.git] / intern / cycles / kernel / svm / svm.h
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 #ifndef __SVM_H__
20 #define __SVM_H__
21
22 /* Shader Virtual Machine
23  *
24  * A shader is a list of nodes to be executed. These are simply read one after
25  * the other and executed, using an node counter. Each node and it's associated
26  * data is encoded as one or more uint4's in a 1D texture. If the data is larger
27  * than an uint4, the node can increase the node counter to compensate for this.
28  * Floats are encoded as int and then converted to float again.
29  *
30  * Nodes write their output into a stack. All stack data in the stack is
31  * floats, since it's all factors, colors and vectors. The stack will be stored
32  * in local memory on the GPU, as it would take too many register and indexes in
33  * ways not known at compile time. This seems the only solution even though it
34  * may be slow, with two positive factors. If the same shader is being executed,
35  * memory access will be coalesced, and on fermi cards, memory will actually be
36  * cached.
37  *
38  * The result of shader execution will be a single closure. This means the
39  * closure type, associated label, data and weight. Sampling from multiple
40  * closures is supported through the mix closure node, the logic for that is
41  * mostly taken care of in the SVM compiler.
42  */
43
44 #include "svm_types.h"
45
46 CCL_NAMESPACE_BEGIN
47
48 /* Stack */
49
50 __device_inline float3 stack_load_float3(float *stack, uint a)
51 {
52         kernel_assert(a+2 < SVM_STACK_SIZE);
53
54         return make_float3(stack[a+0], stack[a+1], stack[a+2]);
55 }
56
57 __device_inline void stack_store_float3(float *stack, uint a, float3 f)
58 {
59         kernel_assert(a+2 < SVM_STACK_SIZE);
60
61         stack[a+0] = f.x;
62         stack[a+1] = f.y;
63         stack[a+2] = f.z;
64 }
65
66 __device_inline float stack_load_float(float *stack, uint a)
67 {
68         kernel_assert(a < SVM_STACK_SIZE);
69
70         return stack[a];
71 }
72
73 __device_inline float stack_load_float_default(float *stack, uint a, uint value)
74 {
75         return (a == (uint)SVM_STACK_INVALID)? __int_as_float(value): stack_load_float(stack, a);
76 }
77
78 __device_inline void stack_store_float(float *stack, uint a, float f)
79 {
80         kernel_assert(a < SVM_STACK_SIZE);
81
82         stack[a] = f;
83 }
84
85 __device_inline bool stack_valid(uint a)
86 {
87         return a != (uint)SVM_STACK_INVALID;
88 }
89
90 /* Reading Nodes */
91
92 __device_inline uint4 read_node(KernelGlobals *kg, int *offset)
93 {
94         uint4 node = kernel_tex_fetch(__svm_nodes, *offset);
95         (*offset)++;
96         return node;
97 }
98
99 __device_inline float4 read_node_float(KernelGlobals *kg, int *offset)
100 {
101         uint4 node = kernel_tex_fetch(__svm_nodes, *offset);
102         float4 f = make_float4(__int_as_float(node.x), __int_as_float(node.y), __int_as_float(node.z), __int_as_float(node.w));
103         (*offset)++;
104         return f;
105 }
106
107 __device_inline void decode_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
108 {
109         if(x) *x = (i & 0xFF);
110         if(y) *y = ((i >> 8) & 0xFF);
111         if(z) *z = ((i >> 16) & 0xFF);
112         if(w) *w = ((i >> 24) & 0xFF);
113 }
114
115 CCL_NAMESPACE_END
116
117 /* Nodes */
118
119 #include "svm_noise.h"
120 #include "svm_texture.h"
121
122 #include "svm_attribute.h"
123 #include "svm_gradient.h"
124 #include "svm_closure.h"
125 #include "svm_noisetex.h"
126 #include "svm_convert.h"
127 #include "svm_displace.h"
128 #include "svm_fresnel.h"
129 #include "svm_camera.h"
130 #include "svm_geometry.h"
131 #include "svm_hsv.h"
132 #include "svm_image.h"
133 #include "svm_gamma.h"
134 #include "svm_brightness.h"
135 #include "svm_invert.h"
136 #include "svm_light_path.h"
137 #include "svm_magic.h"
138 #include "svm_mapping.h"
139 #include "svm_normal.h"
140 #include "svm_wave.h"
141 #include "svm_math.h"
142 #include "svm_mix.h"
143 #include "svm_sepcomb_rgb.h"
144 #include "svm_musgrave.h"
145 #include "svm_sky.h"
146 #include "svm_tex_coord.h"
147 #include "svm_value.h"
148 #include "svm_voronoi.h"
149 #include "svm_checker.h"
150
151 CCL_NAMESPACE_BEGIN
152
153 /* Main Interpreter Loop */
154
155 __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderType type, float randb, int path_flag)
156 {
157         float stack[SVM_STACK_SIZE];
158         float closure_weight = 1.0f;
159         int offset = sd->shader & SHADER_MASK;
160
161 #ifdef __MULTI_CLOSURE__
162         sd->num_closure = 0;
163         sd->randb_closure = randb;
164 #else
165         sd->closure.type = NBUILTIN_CLOSURES;
166 #endif
167
168         while(1) {
169                 uint4 node = read_node(kg, &offset);
170
171                 switch(node.x) {
172                         case NODE_SHADER_JUMP: {
173                                 if(type == SHADER_TYPE_SURFACE) offset = node.y;
174                                 else if(type == SHADER_TYPE_VOLUME) offset = node.z;
175                                 else if(type == SHADER_TYPE_DISPLACEMENT) offset = node.w;
176                                 else return;
177                                 break;
178                         }
179                         case NODE_CLOSURE_BSDF:
180                                 svm_node_closure_bsdf(kg, sd, stack, node, randb, path_flag);
181                                 break;
182                         case NODE_CLOSURE_EMISSION:
183                                 svm_node_closure_emission(sd, stack, node);
184                                 break;
185                         case NODE_CLOSURE_BACKGROUND:
186                                 svm_node_closure_background(sd, stack, node);
187                                 break;
188                         case NODE_CLOSURE_HOLDOUT:
189                                 svm_node_closure_holdout(sd, stack, node);
190                                 break;
191                         case NODE_CLOSURE_VOLUME:
192                                 svm_node_closure_volume(kg, sd, stack, node, path_flag);
193                                 break;
194                         case NODE_CLOSURE_SET_WEIGHT:
195                                 svm_node_closure_set_weight(sd, node.y, node.z, node.w);
196                                 break;
197                         case NODE_CLOSURE_WEIGHT:
198                                 svm_node_closure_weight(sd, stack, node.y);
199                                 break;
200                         case NODE_EMISSION_WEIGHT:
201                                 svm_node_emission_weight(kg, sd, stack, node);
202                                 break;
203                         case NODE_MIX_CLOSURE:
204                                 svm_node_mix_closure(sd, stack, node, &offset, &randb);
205                                 break;
206                         case NODE_ADD_CLOSURE:
207                                 svm_node_add_closure(sd, stack, node.y, node.z, &offset, &randb, &closure_weight);
208                                 break;
209                         case NODE_JUMP:
210                                 offset = node.y;
211                                 break;
212 #ifdef __TEXTURES__
213                         case NODE_TEX_IMAGE:
214                                 svm_node_tex_image(kg, sd, stack, node);
215                                 break;
216                         case NODE_TEX_ENVIRONMENT:
217                                 svm_node_tex_environment(kg, sd, stack, node);
218                                 break;
219                         case NODE_TEX_SKY:
220                                 svm_node_tex_sky(kg, sd, stack, node.y, node.z);
221                                 break;
222                         case NODE_TEX_GRADIENT:
223                                 svm_node_tex_gradient(sd, stack, node);
224                                 break;
225                         case NODE_TEX_NOISE:
226                                 svm_node_tex_noise(kg, sd, stack, node, &offset);
227                                 break;
228                         case NODE_TEX_VORONOI:
229                                 svm_node_tex_voronoi(kg, sd, stack, node, &offset);
230                                 break;
231                         case NODE_TEX_MUSGRAVE:
232                                 svm_node_tex_musgrave(kg, sd, stack, node, &offset);
233                                 break;
234                         case NODE_TEX_WAVE:
235                                 svm_node_tex_wave(kg, sd, stack, node, &offset);
236                                 break;
237                         case NODE_TEX_MAGIC:
238                                 svm_node_tex_magic(kg, sd, stack, node, &offset);
239                                 break;
240                         case NODE_TEX_CHECKER:
241                                 svm_node_tex_checker(kg, sd, stack, node, &offset);
242                                 break;
243 #endif
244                         case NODE_CAMERA:
245                                 svm_node_camera(kg, sd, stack, node.y, node.z, node.w);
246                                 break;
247                         case NODE_GEOMETRY:
248                                 svm_node_geometry(sd, stack, node.y, node.z);
249                                 break;
250                         case NODE_GEOMETRY_BUMP_DX:
251                                 svm_node_geometry_bump_dx(sd, stack, node.y, node.z);
252                                 break;
253                         case NODE_GEOMETRY_BUMP_DY:
254                                 svm_node_geometry_bump_dy(sd, stack, node.y, node.z);
255                                 break;
256                         case NODE_LIGHT_PATH:
257                                 svm_node_light_path(sd, stack, node.y, node.z, path_flag);
258                                 break;
259                         case NODE_CONVERT:
260                                 svm_node_convert(sd, stack, node.y, node.z, node.w);
261                                 break;
262                         case NODE_VALUE_F:
263                                 svm_node_value_f(kg, sd, stack, node.y, node.z);
264                                 break;
265                         case NODE_VALUE_V:
266                                 svm_node_value_v(kg, sd, stack, node.y, &offset);
267                                 break;
268                         case NODE_INVERT:
269                                 svm_node_invert(sd, stack, node.y, node.z, node.w);
270                                 break;
271                         case NODE_GAMMA:
272                                 svm_node_gamma(sd, stack, node.y, node.z, node.w);
273                                 break;
274                         case NODE_BRIGHTCONTRAST:
275                                 svm_node_brightness(sd, stack, node.y, node.z, node.w);
276                                 break;
277                         case NODE_MIX:
278                                 svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
279                                 break;
280                         case NODE_SEPARATE_RGB:
281                                 svm_node_separate_rgb(sd, stack, node.y, node.z, node.w);
282                                 break;
283                         case NODE_COMBINE_RGB:
284                                 svm_node_combine_rgb(sd, stack, node.y, node.z, node.w);
285                                 break;
286                         case NODE_HSV:
287                                 svm_node_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
288                                 break;
289                         case NODE_ATTR:
290                                 svm_node_attr(kg, sd, stack, node);
291                                 break;
292                         case NODE_ATTR_BUMP_DX:
293                                 svm_node_attr_bump_dx(kg, sd, stack, node);
294                                 break;
295                         case NODE_ATTR_BUMP_DY:
296                                 svm_node_attr_bump_dy(kg, sd, stack, node);
297                                 break;
298                         case NODE_FRESNEL:
299                                 svm_node_fresnel(sd, stack, node.y, node.z, node.w);
300                                 break;
301                         case NODE_LAYER_WEIGHT:
302                                 svm_node_layer_weight(sd, stack, node);
303                                 break;
304                         case NODE_SET_DISPLACEMENT:
305                                 svm_node_set_displacement(sd, stack, node.y);
306                                 break;
307                         case NODE_SET_BUMP:
308                                 svm_node_set_bump(sd, stack, node.y, node.z, node.w);
309                                 break;
310                         case NODE_MATH:
311                                 svm_node_math(kg, sd, stack, node.y, node.z, node.w, &offset);
312                                 break;
313                         case NODE_VECTOR_MATH:
314                                 svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset);
315                                 break;
316                         case NODE_NORMAL:
317                                 svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset);
318                                 break;
319                         case NODE_MAPPING:
320                                 svm_node_mapping(kg, sd, stack, node.y, node.z, &offset);
321                                 break;
322                         case NODE_TEX_COORD:
323                                 svm_node_tex_coord(kg, sd, stack, node.y, node.z);
324                                 break;
325                         case NODE_TEX_COORD_BUMP_DX:
326                                 svm_node_tex_coord_bump_dx(kg, sd, stack, node.y, node.z);
327                                 break;
328                         case NODE_TEX_COORD_BUMP_DY:
329                                 svm_node_tex_coord_bump_dy(kg, sd, stack, node.y, node.z);
330                                 break;
331                         case NODE_EMISSION_SET_WEIGHT_TOTAL:
332                                 svm_node_emission_set_weight_total(kg, sd, node.y, node.z, node.w);
333                                 break;
334                         case NODE_END:
335                         default:
336 #ifndef __MULTI_CLOSURE__
337                                 sd->closure.weight *= closure_weight;
338 #endif
339                                 return;
340                 }
341         }
342 }
343
344 CCL_NAMESPACE_END
345
346 #endif /* __SVM_H__ */
347