Cycles: add Displacement node.
[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 "kernel/svm/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 "kernel/svm/svm_noise.h"
143 #include "svm_texture.h"
144
145 #include "kernel/svm/svm_color_util.h"
146 #include "kernel/svm/svm_math_util.h"
147
148 #include "kernel/svm/svm_attribute.h"
149 #include "kernel/svm/svm_gradient.h"
150 #include "kernel/svm/svm_blackbody.h"
151 #include "kernel/svm/svm_closure.h"
152 #include "kernel/svm/svm_noisetex.h"
153 #include "kernel/svm/svm_convert.h"
154 #include "kernel/svm/svm_displace.h"
155 #include "kernel/svm/svm_fresnel.h"
156 #include "kernel/svm/svm_wireframe.h"
157 #include "kernel/svm/svm_wavelength.h"
158 #include "kernel/svm/svm_camera.h"
159 #include "kernel/svm/svm_geometry.h"
160 #include "kernel/svm/svm_hsv.h"
161 #include "kernel/svm/svm_image.h"
162 #include "kernel/svm/svm_gamma.h"
163 #include "kernel/svm/svm_brightness.h"
164 #include "kernel/svm/svm_invert.h"
165 #include "kernel/svm/svm_light_path.h"
166 #include "kernel/svm/svm_magic.h"
167 #include "kernel/svm/svm_mapping.h"
168 #include "kernel/svm/svm_normal.h"
169 #include "kernel/svm/svm_wave.h"
170 #include "kernel/svm/svm_math.h"
171 #include "kernel/svm/svm_mix.h"
172 #include "kernel/svm/svm_ramp.h"
173 #include "kernel/svm/svm_sepcomb_hsv.h"
174 #include "kernel/svm/svm_sepcomb_vector.h"
175 #include "kernel/svm/svm_musgrave.h"
176 #include "kernel/svm/svm_sky.h"
177 #include "kernel/svm/svm_tex_coord.h"
178 #include "kernel/svm/svm_value.h"
179 #include "kernel/svm/svm_voronoi.h"
180 #include "kernel/svm/svm_checker.h"
181 #include "kernel/svm/svm_brick.h"
182 #include "kernel/svm/svm_vector_transform.h"
183 #include "kernel/svm/svm_voxel.h"
184 #include "kernel/svm/svm_bump.h"
185
186 #ifdef __SHADER_RAYTRACE__
187 #  include "kernel/svm/svm_bevel.h"
188 #endif
189
190 CCL_NAMESPACE_BEGIN
191
192 #define NODES_GROUP(group) ((group) <= __NODES_MAX_GROUP__)
193 #define NODES_FEATURE(feature) ((__NODES_FEATURES__ & (feature)) != 0)
194
195 /* Main Interpreter Loop */
196 ccl_device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ccl_addr_space PathState *state, ShaderType type, int path_flag)
197 {
198         float stack[SVM_STACK_SIZE];
199         int offset = sd->shader & SHADER_MASK;
200
201         while(1) {
202                 uint4 node = read_node(kg, &offset);
203
204                 switch(node.x) {
205 #if NODES_GROUP(NODE_GROUP_LEVEL_0)
206                         case NODE_SHADER_JUMP: {
207                                 if(type == SHADER_TYPE_SURFACE) offset = node.y;
208                                 else if(type == SHADER_TYPE_VOLUME) offset = node.z;
209                                 else if(type == SHADER_TYPE_DISPLACEMENT) offset = node.w;
210                                 else return;
211                                 break;
212                         }
213                         case NODE_CLOSURE_BSDF:
214                                 svm_node_closure_bsdf(kg, sd, stack, node, type, path_flag, &offset);
215                                 break;
216                         case NODE_CLOSURE_EMISSION:
217                                 svm_node_closure_emission(sd, stack, node);
218                                 break;
219                         case NODE_CLOSURE_BACKGROUND:
220                                 svm_node_closure_background(sd, stack, node);
221                                 break;
222                         case NODE_CLOSURE_SET_WEIGHT:
223                                 svm_node_closure_set_weight(sd, node.y, node.z, node.w);
224                                 break;
225                         case NODE_CLOSURE_WEIGHT:
226                                 svm_node_closure_weight(sd, stack, node.y);
227                                 break;
228                         case NODE_EMISSION_WEIGHT:
229                                 svm_node_emission_weight(kg, sd, stack, node);
230                                 break;
231                         case NODE_MIX_CLOSURE:
232                                 svm_node_mix_closure(sd, stack, node);
233                                 break;
234                         case NODE_JUMP_IF_ZERO:
235                                 if(stack_load_float(stack, node.z) == 0.0f)
236                                         offset += node.y;
237                                 break;
238                         case NODE_JUMP_IF_ONE:
239                                 if(stack_load_float(stack, node.z) == 1.0f)
240                                         offset += node.y;
241                                 break;
242                         case NODE_GEOMETRY:
243                                 svm_node_geometry(kg, sd, stack, node.y, node.z);
244                                 break;
245                         case NODE_CONVERT:
246                                 svm_node_convert(sd, stack, node.y, node.z, node.w);
247                                 break;
248                         case NODE_TEX_COORD:
249                                 svm_node_tex_coord(kg, sd, path_flag, stack, node, &offset);
250                                 break;
251                         case NODE_VALUE_F:
252                                 svm_node_value_f(kg, sd, stack, node.y, node.z);
253                                 break;
254                         case NODE_VALUE_V:
255                                 svm_node_value_v(kg, sd, stack, node.y, &offset);
256                                 break;
257                         case NODE_ATTR:
258                                 svm_node_attr(kg, sd, stack, node);
259                                 break;
260 #  if NODES_FEATURE(NODE_FEATURE_BUMP)
261                         case NODE_GEOMETRY_BUMP_DX:
262                                 svm_node_geometry_bump_dx(kg, sd, stack, node.y, node.z);
263                                 break;
264                         case NODE_GEOMETRY_BUMP_DY:
265                                 svm_node_geometry_bump_dy(kg, sd, stack, node.y, node.z);
266                                 break;
267                         case NODE_SET_DISPLACEMENT:
268                                 svm_node_set_displacement(kg, sd, stack, node.y);
269                                 break;
270                         case NODE_DISPLACEMENT:
271                                 svm_node_displacement(kg, sd, stack, node);
272                                 break;
273 #  endif  /* NODES_FEATURE(NODE_FEATURE_BUMP) */
274 #  ifdef __TEXTURES__
275                         case NODE_TEX_IMAGE:
276                                 svm_node_tex_image(kg, sd, stack, node);
277                                 break;
278                         case NODE_TEX_IMAGE_BOX:
279                                 svm_node_tex_image_box(kg, sd, stack, node);
280                                 break;
281                         case NODE_TEX_NOISE:
282                                 svm_node_tex_noise(kg, sd, stack, node, &offset);
283                                 break;
284 #  endif  /* __TEXTURES__ */
285 #  ifdef __EXTRA_NODES__
286 #    if NODES_FEATURE(NODE_FEATURE_BUMP)
287                         case NODE_SET_BUMP:
288                                 svm_node_set_bump(kg, sd, stack, node);
289                                 break;
290                         case NODE_ATTR_BUMP_DX:
291                                 svm_node_attr_bump_dx(kg, sd, stack, node);
292                                 break;
293                         case NODE_ATTR_BUMP_DY:
294                                 svm_node_attr_bump_dy(kg, sd, stack, node);
295                                 break;
296                         case NODE_TEX_COORD_BUMP_DX:
297                                 svm_node_tex_coord_bump_dx(kg, sd, path_flag, stack, node, &offset);
298                                 break;
299                         case NODE_TEX_COORD_BUMP_DY:
300                                 svm_node_tex_coord_bump_dy(kg, sd, path_flag, stack, node, &offset);
301                                 break;
302                         case NODE_CLOSURE_SET_NORMAL:
303                                 svm_node_set_normal(kg, sd, stack, node.y, node.z);
304                                 break;
305 #      if NODES_FEATURE(NODE_FEATURE_BUMP_STATE)
306                         case NODE_ENTER_BUMP_EVAL:
307                                 svm_node_enter_bump_eval(kg, sd, stack, node.y);
308                                 break;
309                         case NODE_LEAVE_BUMP_EVAL:
310                                 svm_node_leave_bump_eval(kg, sd, stack, node.y);
311                                 break;
312 #      endif /* NODES_FEATURE(NODE_FEATURE_BUMP_STATE) */
313 #    endif  /* NODES_FEATURE(NODE_FEATURE_BUMP) */
314                         case NODE_HSV:
315                                 svm_node_hsv(kg, sd, stack, node, &offset);
316                                 break;
317 #  endif  /* __EXTRA_NODES__ */
318 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_0) */
319
320 #if NODES_GROUP(NODE_GROUP_LEVEL_1)
321                         case NODE_CLOSURE_HOLDOUT:
322                                 svm_node_closure_holdout(sd, stack, node);
323                                 break;
324                         case NODE_CLOSURE_AMBIENT_OCCLUSION:
325                                 svm_node_closure_ambient_occlusion(sd, stack, node);
326                                 break;
327                         case NODE_FRESNEL:
328                                 svm_node_fresnel(sd, stack, node.y, node.z, node.w);
329                                 break;
330                         case NODE_LAYER_WEIGHT:
331                                 svm_node_layer_weight(sd, stack, node);
332                                 break;
333 #  if NODES_FEATURE(NODE_FEATURE_VOLUME)
334                         case NODE_CLOSURE_VOLUME:
335                                 svm_node_closure_volume(kg, sd, stack, node, type, path_flag);
336                                 break;
337 #  endif  /* NODES_FEATURE(NODE_FEATURE_VOLUME) */
338 #  ifdef __EXTRA_NODES__
339                         case NODE_MATH:
340                                 svm_node_math(kg, sd, stack, node.y, node.z, node.w, &offset);
341                                 break;
342                         case NODE_VECTOR_MATH:
343                                 svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset);
344                                 break;
345                         case NODE_RGB_RAMP:
346                                 svm_node_rgb_ramp(kg, sd, stack, node, &offset);
347                                 break;
348                         case NODE_GAMMA:
349                                 svm_node_gamma(sd, stack, node.y, node.z, node.w);
350                                 break;
351                         case NODE_BRIGHTCONTRAST:
352                                 svm_node_brightness(sd, stack, node.y, node.z, node.w);
353                                 break;
354                         case NODE_LIGHT_PATH:
355                                 svm_node_light_path(sd, state, stack, node.y, node.z, path_flag);
356                                 break;
357                         case NODE_OBJECT_INFO:
358                                 svm_node_object_info(kg, sd, stack, node.y, node.z);
359                                 break;
360                         case NODE_PARTICLE_INFO:
361                                 svm_node_particle_info(kg, sd, stack, node.y, node.z);
362                                 break;
363 #    ifdef __HAIR__
364 #      if NODES_FEATURE(NODE_FEATURE_HAIR)
365                         case NODE_HAIR_INFO:
366                                 svm_node_hair_info(kg, sd, stack, node.y, node.z);
367                                 break;
368 #      endif  /* NODES_FEATURE(NODE_FEATURE_HAIR) */
369 #    endif  /* __HAIR__ */
370 #  endif  /* __EXTRA_NODES__ */
371 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_1) */
372
373 #if NODES_GROUP(NODE_GROUP_LEVEL_2)
374                         case NODE_MAPPING:
375                                 svm_node_mapping(kg, sd, stack, node.y, node.z, &offset);
376                                 break;
377                         case NODE_MIN_MAX:
378                                 svm_node_min_max(kg, sd, stack, node.y, node.z, &offset);
379                                 break;
380                         case NODE_CAMERA:
381                                 svm_node_camera(kg, sd, stack, node.y, node.z, node.w);
382                                 break;
383 #  ifdef __TEXTURES__
384                         case NODE_TEX_ENVIRONMENT:
385                                 svm_node_tex_environment(kg, sd, stack, node);
386                                 break;
387                         case NODE_TEX_SKY:
388                                 svm_node_tex_sky(kg, sd, stack, node, &offset);
389                                 break;
390                         case NODE_TEX_GRADIENT:
391                                 svm_node_tex_gradient(sd, stack, node);
392                                 break;
393                         case NODE_TEX_VORONOI:
394                                 svm_node_tex_voronoi(kg, sd, stack, node, &offset);
395                                 break;
396                         case NODE_TEX_MUSGRAVE:
397                                 svm_node_tex_musgrave(kg, sd, stack, node, &offset);
398                                 break;
399                         case NODE_TEX_WAVE:
400                                 svm_node_tex_wave(kg, sd, stack, node, &offset);
401                                 break;
402                         case NODE_TEX_MAGIC:
403                                 svm_node_tex_magic(kg, sd, stack, node, &offset);
404                                 break;
405                         case NODE_TEX_CHECKER:
406                                 svm_node_tex_checker(kg, sd, stack, node);
407                                 break;
408                         case NODE_TEX_BRICK:
409                                 svm_node_tex_brick(kg, sd, stack, node, &offset);
410                                 break;
411 #  endif  /* __TEXTURES__ */
412 #  ifdef __EXTRA_NODES__
413                         case NODE_NORMAL:
414                                 svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset);
415                                 break;
416                         case NODE_LIGHT_FALLOFF:
417                                 svm_node_light_falloff(sd, stack, node);
418                                 break;
419 #  endif  /* __EXTRA_NODES__ */
420 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_2) */
421
422 #if NODES_GROUP(NODE_GROUP_LEVEL_3)
423                         case NODE_RGB_CURVES:
424                         case NODE_VECTOR_CURVES:
425                                 svm_node_curves(kg, sd, stack, node, &offset);
426                                 break;
427                         case NODE_TANGENT:
428                                 svm_node_tangent(kg, sd, stack, node);
429                                 break;
430                         case NODE_NORMAL_MAP:
431                                 svm_node_normal_map(kg, sd, stack, node);
432                                 break;
433 #  ifdef __EXTRA_NODES__
434                         case NODE_INVERT:
435                                 svm_node_invert(sd, stack, node.y, node.z, node.w);
436                                 break;
437                         case NODE_MIX:
438                                 svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
439                                 break;
440                         case NODE_SEPARATE_VECTOR:
441                                 svm_node_separate_vector(sd, stack, node.y, node.z, node.w);
442                                 break;
443                         case NODE_COMBINE_VECTOR:
444                                 svm_node_combine_vector(sd, stack, node.y, node.z, node.w);
445                                 break;
446                         case NODE_SEPARATE_HSV:
447                                 svm_node_separate_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
448                                 break;
449                         case NODE_COMBINE_HSV:
450                                 svm_node_combine_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
451                                 break;
452                         case NODE_VECTOR_TRANSFORM:
453                                 svm_node_vector_transform(kg, sd, stack, node);
454                                 break;
455                         case NODE_WIREFRAME:
456                                 svm_node_wireframe(kg, sd, stack, node);
457                                 break;
458                         case NODE_WAVELENGTH:
459                                 svm_node_wavelength(sd, stack, node.y, node.z);
460                                 break;
461                         case NODE_BLACKBODY:
462                                 svm_node_blackbody(kg, sd, stack, node.y, node.z);
463                                 break;
464 #  endif  /* __EXTRA_NODES__ */
465 #  if NODES_FEATURE(NODE_FEATURE_VOLUME)
466                         case NODE_TEX_VOXEL:
467                                 svm_node_tex_voxel(kg, sd, stack, node, &offset);
468                                 break;
469 #  endif  /* NODES_FEATURE(NODE_FEATURE_VOLUME) */
470 #  ifdef __SHADER_RAYTRACE__
471                         case NODE_BEVEL:
472                                 svm_node_bevel(kg, sd, state, stack, node);
473                                 break;
474 #  endif  /* __SHADER_RAYTRACE__ */
475 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_3) */
476                         case NODE_END:
477                                 return;
478                         default:
479                                 kernel_assert(!"Unknown node type was passed to the SVM machine");
480                                 return;
481                 }
482         }
483 }
484
485 #undef NODES_GROUP
486 #undef NODES_FEATURE
487
488 CCL_NAMESPACE_END
489
490 #endif /* __SVM_H__ */
491