Cycles: constant folding for RGB/Vector Curves and Color Ramp.
[blender.git] / intern / cycles / kernel / svm / svm.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __SVM_H__
18 #define __SVM_H__
19
20 /* Shader Virtual Machine
21  *
22  * A shader is a list of nodes to be executed. These are simply read one after
23  * the other and executed, using an node counter. Each node and it's associated
24  * data is encoded as one or more uint4's in a 1D texture. If the data is larger
25  * than an uint4, the node can increase the node counter to compensate for this.
26  * Floats are encoded as int and then converted to float again.
27  *
28  * Nodes write their output into a stack. All stack data in the stack is
29  * floats, since it's all factors, colors and vectors. The stack will be stored
30  * in local memory on the GPU, as it would take too many register and indexes in
31  * ways not known at compile time. This seems the only solution even though it
32  * may be slow, with two positive factors. If the same shader is being executed,
33  * memory access will be coalesced, and on fermi cards, memory will actually be
34  * cached.
35  *
36  * The result of shader execution will be a single closure. This means the
37  * closure type, associated label, data and weight. Sampling from multiple
38  * closures is supported through the mix closure node, the logic for that is
39  * mostly taken care of in the SVM compiler.
40  */
41
42 #include "svm_types.h"
43
44 CCL_NAMESPACE_BEGIN
45
46 /* Stack */
47
48 ccl_device_inline float3 stack_load_float3(float *stack, uint a)
49 {
50         kernel_assert(a+2 < SVM_STACK_SIZE);
51
52         return make_float3(stack[a+0], stack[a+1], stack[a+2]);
53 }
54
55 ccl_device_inline void stack_store_float3(float *stack, uint a, float3 f)
56 {
57         kernel_assert(a+2 < SVM_STACK_SIZE);
58
59         stack[a+0] = f.x;
60         stack[a+1] = f.y;
61         stack[a+2] = f.z;
62 }
63
64 ccl_device_inline float stack_load_float(float *stack, uint a)
65 {
66         kernel_assert(a < SVM_STACK_SIZE);
67
68         return stack[a];
69 }
70
71 ccl_device_inline float stack_load_float_default(float *stack, uint a, uint value)
72 {
73         return (a == (uint)SVM_STACK_INVALID)? __uint_as_float(value): stack_load_float(stack, a);
74 }
75
76 ccl_device_inline void stack_store_float(float *stack, uint a, float f)
77 {
78         kernel_assert(a < SVM_STACK_SIZE);
79
80         stack[a] = f;
81 }
82
83 ccl_device_inline int stack_load_int(float *stack, uint a)
84 {
85         kernel_assert(a < SVM_STACK_SIZE);
86
87         return __float_as_int(stack[a]);
88 }
89
90 ccl_device_inline int stack_load_int_default(float *stack, uint a, uint value)
91 {
92         return (a == (uint)SVM_STACK_INVALID)? (int)value: stack_load_int(stack, a);
93 }
94
95 ccl_device_inline void stack_store_int(float *stack, uint a, int i)
96 {
97         kernel_assert(a < SVM_STACK_SIZE);
98
99         stack[a] = __int_as_float(i);
100 }
101
102 ccl_device_inline bool stack_valid(uint a)
103 {
104         return a != (uint)SVM_STACK_INVALID;
105 }
106
107 /* Reading Nodes */
108
109 ccl_device_inline uint4 read_node(KernelGlobals *kg, int *offset)
110 {
111         uint4 node = kernel_tex_fetch(__svm_nodes, *offset);
112         (*offset)++;
113         return node;
114 }
115
116 ccl_device_inline float4 read_node_float(KernelGlobals *kg, int *offset)
117 {
118         uint4 node = kernel_tex_fetch(__svm_nodes, *offset);
119         float4 f = make_float4(__uint_as_float(node.x), __uint_as_float(node.y), __uint_as_float(node.z), __uint_as_float(node.w));
120         (*offset)++;
121         return f;
122 }
123
124 ccl_device_inline float4 fetch_node_float(KernelGlobals *kg, int offset)
125 {
126         uint4 node = kernel_tex_fetch(__svm_nodes, offset);
127         return make_float4(__uint_as_float(node.x), __uint_as_float(node.y), __uint_as_float(node.z), __uint_as_float(node.w));
128 }
129
130 ccl_device_inline void decode_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
131 {
132         if(x) *x = (i & 0xFF);
133         if(y) *y = ((i >> 8) & 0xFF);
134         if(z) *z = ((i >> 16) & 0xFF);
135         if(w) *w = ((i >> 24) & 0xFF);
136 }
137
138 CCL_NAMESPACE_END
139
140 /* Nodes */
141
142 #include "svm_noise.h"
143 #include "svm_texture.h"
144
145 #include "svm_color_util.h"
146 #include "svm_math_util.h"
147
148 #include "svm_attribute.h"
149 #include "svm_gradient.h"
150 #include "svm_blackbody.h"
151 #include "svm_closure.h"
152 #include "svm_noisetex.h"
153 #include "svm_convert.h"
154 #include "svm_displace.h"
155 #include "svm_fresnel.h"
156 #include "svm_wireframe.h"
157 #include "svm_wavelength.h"
158 #include "svm_camera.h"
159 #include "svm_geometry.h"
160 #include "svm_hsv.h"
161 #include "svm_image.h"
162 #include "svm_gamma.h"
163 #include "svm_brightness.h"
164 #include "svm_invert.h"
165 #include "svm_light_path.h"
166 #include "svm_magic.h"
167 #include "svm_mapping.h"
168 #include "svm_normal.h"
169 #include "svm_wave.h"
170 #include "svm_math.h"
171 #include "svm_mix.h"
172 #include "svm_ramp.h"
173 #include "svm_sepcomb_hsv.h"
174 #include "svm_sepcomb_vector.h"
175 #include "svm_musgrave.h"
176 #include "svm_sky.h"
177 #include "svm_tex_coord.h"
178 #include "svm_value.h"
179 #include "svm_voronoi.h"
180 #include "svm_checker.h"
181 #include "svm_brick.h"
182 #include "svm_vector_transform.h"
183 #include "svm_voxel.h"
184
185 CCL_NAMESPACE_BEGIN
186
187 #define NODES_GROUP(group) ((group) <= __NODES_MAX_GROUP__)
188 #define NODES_FEATURE(feature) ((__NODES_FEATURES__ & (feature)) != 0)
189
190 /* Main Interpreter Loop */
191 ccl_device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ccl_addr_space PathState *state, ShaderType type, int path_flag)
192 {
193         float stack[SVM_STACK_SIZE];
194         int offset = ccl_fetch(sd, shader) & SHADER_MASK;
195
196         while(1) {
197                 uint4 node = read_node(kg, &offset);
198
199                 switch(node.x) {
200 #if NODES_GROUP(NODE_GROUP_LEVEL_0)
201                         case NODE_SHADER_JUMP: {
202                                 if(type == SHADER_TYPE_SURFACE) offset = node.y;
203                                 else if(type == SHADER_TYPE_VOLUME) offset = node.z;
204                                 else if(type == SHADER_TYPE_DISPLACEMENT) offset = node.w;
205                                 else return;
206                                 break;
207                         }
208                         case NODE_CLOSURE_BSDF:
209                                 svm_node_closure_bsdf(kg, sd, stack, node, path_flag, &offset);
210                                 break;
211                         case NODE_CLOSURE_EMISSION:
212                                 svm_node_closure_emission(sd, stack, node);
213                                 break;
214                         case NODE_CLOSURE_BACKGROUND:
215                                 svm_node_closure_background(sd, stack, node);
216                                 break;
217                         case NODE_CLOSURE_SET_WEIGHT:
218                                 svm_node_closure_set_weight(sd, node.y, node.z, node.w);
219                                 break;
220                         case NODE_CLOSURE_WEIGHT:
221                                 svm_node_closure_weight(sd, stack, node.y);
222                                 break;
223                         case NODE_EMISSION_WEIGHT:
224                                 svm_node_emission_weight(kg, sd, stack, node);
225                                 break;
226                         case NODE_MIX_CLOSURE:
227                                 svm_node_mix_closure(sd, stack, node);
228                                 break;
229                         case NODE_JUMP_IF_ZERO:
230                                 if(stack_load_float(stack, node.z) == 0.0f)
231                                         offset += node.y;
232                                 break;
233                         case NODE_JUMP_IF_ONE:
234                                 if(stack_load_float(stack, node.z) == 1.0f)
235                                         offset += node.y;
236                                 break;
237                         case NODE_GEOMETRY:
238                                 svm_node_geometry(kg, sd, stack, node.y, node.z);
239                                 break;
240                         case NODE_CONVERT:
241                                 svm_node_convert(sd, stack, node.y, node.z, node.w);
242                                 break;
243                         case NODE_TEX_COORD:
244                                 svm_node_tex_coord(kg, sd, path_flag, stack, node, &offset);
245                                 break;
246                         case NODE_VALUE_F:
247                                 svm_node_value_f(kg, sd, stack, node.y, node.z);
248                                 break;
249                         case NODE_VALUE_V:
250                                 svm_node_value_v(kg, sd, stack, node.y, &offset);
251                                 break;
252                         case NODE_ATTR:
253                                 svm_node_attr(kg, sd, stack, node);
254                                 break;
255 #  if NODES_FEATURE(NODE_FEATURE_BUMP)
256                         case NODE_GEOMETRY_BUMP_DX:
257                                 svm_node_geometry_bump_dx(kg, sd, stack, node.y, node.z);
258                                 break;
259                         case NODE_GEOMETRY_BUMP_DY:
260                                 svm_node_geometry_bump_dy(kg, sd, stack, node.y, node.z);
261                                 break;
262                         case NODE_SET_DISPLACEMENT:
263                                 svm_node_set_displacement(sd, stack, node.y);
264                                 break;
265 #  endif  /* NODES_FEATURE(NODE_FEATURE_BUMP) */
266 #  ifdef __TEXTURES__
267                         case NODE_TEX_IMAGE:
268                                 svm_node_tex_image(kg, sd, stack, node);
269                                 break;
270                         case NODE_TEX_IMAGE_BOX:
271                                 svm_node_tex_image_box(kg, sd, stack, node);
272                                 break;
273                         case NODE_TEX_NOISE:
274                                 svm_node_tex_noise(kg, sd, stack, node, &offset);
275                                 break;
276 #  endif  /* __TEXTURES__ */
277 #  ifdef __EXTRA_NODES__
278 #    if NODES_FEATURE(NODE_FEATURE_BUMP)
279                         case NODE_SET_BUMP:
280                                 svm_node_set_bump(kg, sd, stack, node);
281                                 break;
282                         case NODE_ATTR_BUMP_DX:
283                                 svm_node_attr_bump_dx(kg, sd, stack, node);
284                                 break;
285                         case NODE_ATTR_BUMP_DY:
286                                 svm_node_attr_bump_dy(kg, sd, stack, node);
287                                 break;
288                         case NODE_TEX_COORD_BUMP_DX:
289                                 svm_node_tex_coord_bump_dx(kg, sd, path_flag, stack, node, &offset);
290                                 break;
291                         case NODE_TEX_COORD_BUMP_DY:
292                                 svm_node_tex_coord_bump_dy(kg, sd, path_flag, stack, node, &offset);
293                                 break;
294                         case NODE_CLOSURE_SET_NORMAL:
295                                 svm_node_set_normal(kg, sd, stack, node.y, node.z);
296                                 break;
297 #    endif  /* NODES_FEATURE(NODE_FEATURE_BUMP) */
298                         case NODE_HSV:
299                                 svm_node_hsv(kg, sd, stack, node, &offset);
300                                 break;
301 #  endif  /* __EXTRA_NODES__ */
302 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_0) */
303
304 #if NODES_GROUP(NODE_GROUP_LEVEL_1)
305                         case NODE_CLOSURE_HOLDOUT:
306                                 svm_node_closure_holdout(sd, stack, node);
307                                 break;
308                         case NODE_CLOSURE_AMBIENT_OCCLUSION:
309                                 svm_node_closure_ambient_occlusion(sd, stack, node);
310                                 break;
311                         case NODE_FRESNEL:
312                                 svm_node_fresnel(sd, stack, node.y, node.z, node.w);
313                                 break;
314                         case NODE_LAYER_WEIGHT:
315                                 svm_node_layer_weight(sd, stack, node);
316                                 break;
317 #  if NODES_FEATURE(NODE_FEATURE_VOLUME)
318                         case NODE_CLOSURE_VOLUME:
319                                 svm_node_closure_volume(kg, sd, stack, node, path_flag);
320                                 break;
321 #  endif  /* NODES_FEATURE(NODE_FEATURE_VOLUME) */
322 #  ifdef __EXTRA_NODES__
323                         case NODE_MATH:
324                                 svm_node_math(kg, sd, stack, node.y, node.z, node.w, &offset);
325                                 break;
326                         case NODE_VECTOR_MATH:
327                                 svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset);
328                                 break;
329                         case NODE_RGB_RAMP:
330                                 svm_node_rgb_ramp(kg, sd, stack, node, &offset);
331                                 break;
332                         case NODE_GAMMA:
333                                 svm_node_gamma(sd, stack, node.y, node.z, node.w);
334                                 break;
335                         case NODE_BRIGHTCONTRAST:
336                                 svm_node_brightness(sd, stack, node.y, node.z, node.w);
337                                 break;
338                         case NODE_LIGHT_PATH:
339                                 svm_node_light_path(sd, state, stack, node.y, node.z, path_flag);
340                                 break;
341                         case NODE_OBJECT_INFO:
342                                 svm_node_object_info(kg, sd, stack, node.y, node.z);
343                                 break;
344                         case NODE_PARTICLE_INFO:
345                                 svm_node_particle_info(kg, sd, stack, node.y, node.z);
346                                 break;
347 #    ifdef __HAIR__
348 #      if NODES_FEATURE(NODE_FEATURE_HAIR)
349                         case NODE_HAIR_INFO:
350                                 svm_node_hair_info(kg, sd, stack, node.y, node.z);
351                                 break;
352 #      endif  /* NODES_FEATURE(NODE_FEATURE_HAIR) */
353 #    endif  /* __HAIR__ */
354 #  endif  /* __EXTRA_NODES__ */
355 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_1) */
356
357 #if NODES_GROUP(NODE_GROUP_LEVEL_2)
358                         case NODE_MAPPING:
359                                 svm_node_mapping(kg, sd, stack, node.y, node.z, &offset);
360                                 break;
361                         case NODE_MIN_MAX:
362                                 svm_node_min_max(kg, sd, stack, node.y, node.z, &offset);
363                                 break;
364                         case NODE_CAMERA:
365                                 svm_node_camera(kg, sd, stack, node.y, node.z, node.w);
366                                 break;
367 #  ifdef __TEXTURES__
368                         case NODE_TEX_ENVIRONMENT:
369                                 svm_node_tex_environment(kg, sd, stack, node);
370                                 break;
371                         case NODE_TEX_SKY:
372                                 svm_node_tex_sky(kg, sd, stack, node, &offset);
373                                 break;
374                         case NODE_TEX_GRADIENT:
375                                 svm_node_tex_gradient(sd, stack, node);
376                                 break;
377                         case NODE_TEX_VORONOI:
378                                 svm_node_tex_voronoi(kg, sd, stack, node, &offset);
379                                 break;
380                         case NODE_TEX_MUSGRAVE:
381                                 svm_node_tex_musgrave(kg, sd, stack, node, &offset);
382                                 break;
383                         case NODE_TEX_WAVE:
384                                 svm_node_tex_wave(kg, sd, stack, node, &offset);
385                                 break;
386                         case NODE_TEX_MAGIC:
387                                 svm_node_tex_magic(kg, sd, stack, node, &offset);
388                                 break;
389                         case NODE_TEX_CHECKER:
390                                 svm_node_tex_checker(kg, sd, stack, node);
391                                 break;
392                         case NODE_TEX_BRICK:
393                                 svm_node_tex_brick(kg, sd, stack, node, &offset);
394                                 break;
395 #  endif  /* __TEXTURES__ */
396 #  ifdef __EXTRA_NODES__
397                         case NODE_NORMAL:
398                                 svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset);
399                                 break;
400                         case NODE_LIGHT_FALLOFF:
401                                 svm_node_light_falloff(sd, stack, node);
402                                 break;
403 #  endif  /* __EXTRA_NODES__ */
404 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_2) */
405
406 #if NODES_GROUP(NODE_GROUP_LEVEL_3)
407                         case NODE_RGB_CURVES:
408                         case NODE_VECTOR_CURVES:
409                                 svm_node_curves(kg, sd, stack, node, &offset);
410                                 break;
411                         case NODE_TANGENT:
412                                 svm_node_tangent(kg, sd, stack, node);
413                                 break;
414                         case NODE_NORMAL_MAP:
415                                 svm_node_normal_map(kg, sd, stack, node);
416                                 break;
417 #  ifdef __EXTRA_NODES__
418                         case NODE_INVERT:
419                                 svm_node_invert(sd, stack, node.y, node.z, node.w);
420                                 break;
421                         case NODE_MIX:
422                                 svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
423                                 break;
424                         case NODE_SEPARATE_VECTOR:
425                                 svm_node_separate_vector(sd, stack, node.y, node.z, node.w);
426                                 break;
427                         case NODE_COMBINE_VECTOR:
428                                 svm_node_combine_vector(sd, stack, node.y, node.z, node.w);
429                                 break;
430                         case NODE_SEPARATE_HSV:
431                                 svm_node_separate_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
432                                 break;
433                         case NODE_COMBINE_HSV:
434                                 svm_node_combine_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
435                                 break;
436                         case NODE_VECTOR_TRANSFORM:
437                                 svm_node_vector_transform(kg, sd, stack, node);
438                                 break;
439                         case NODE_WIREFRAME:
440                                 svm_node_wireframe(kg, sd, stack, node);
441                                 break;
442                         case NODE_WAVELENGTH:
443                                 svm_node_wavelength(sd, stack, node.y, node.z);
444                                 break;
445                         case NODE_BLACKBODY:
446                                 svm_node_blackbody(kg, sd, stack, node.y, node.z);
447                                 break;
448 #  endif  /* __EXTRA_NODES__ */
449 #  if NODES_FEATURE(NODE_FEATURE_VOLUME)
450                         case NODE_TEX_VOXEL:
451                                 svm_node_tex_voxel(kg, sd, stack, node, &offset);
452                                 break;
453 #  endif  /* NODES_FEATURE(NODE_FEATURE_VOLUME) */
454 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_3) */
455                         case NODE_END:
456                                 return;
457                         default:
458                                 kernel_assert(!"Unknown node type was passed to the SVM machine");
459                                 return;
460                 }
461         }
462 }
463
464 #undef NODES_GROUP
465 #undef NODES_FEATURE
466
467 CCL_NAMESPACE_END
468
469 #endif /* __SVM_H__ */
470