fae9f78348380445163d60d14d86954f396643cb
[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 cached.
34  *
35  * The result of shader execution will be a single closure. This means the
36  * closure type, associated label, data and weight. Sampling from multiple
37  * closures is supported through the mix closure node, the logic for that is
38  * mostly taken care of in the SVM compiler.
39  */
40
41 #include "kernel/svm/svm_types.h"
42
43 CCL_NAMESPACE_BEGIN
44
45 /* Stack */
46
47 ccl_device_inline float3 stack_load_float3(float *stack, uint a)
48 {
49         kernel_assert(a+2 < SVM_STACK_SIZE);
50
51         return make_float3(stack[a+0], stack[a+1], stack[a+2]);
52 }
53
54 ccl_device_inline void stack_store_float3(float *stack, uint a, float3 f)
55 {
56         kernel_assert(a+2 < SVM_STACK_SIZE);
57
58         stack[a+0] = f.x;
59         stack[a+1] = f.y;
60         stack[a+2] = f.z;
61 }
62
63 ccl_device_inline float stack_load_float(float *stack, uint a)
64 {
65         kernel_assert(a < SVM_STACK_SIZE);
66
67         return stack[a];
68 }
69
70 ccl_device_inline float stack_load_float_default(float *stack, uint a, uint value)
71 {
72         return (a == (uint)SVM_STACK_INVALID)? __uint_as_float(value): stack_load_float(stack, a);
73 }
74
75 ccl_device_inline void stack_store_float(float *stack, uint a, float f)
76 {
77         kernel_assert(a < SVM_STACK_SIZE);
78
79         stack[a] = f;
80 }
81
82 ccl_device_inline int stack_load_int(float *stack, uint a)
83 {
84         kernel_assert(a < SVM_STACK_SIZE);
85
86         return __float_as_int(stack[a]);
87 }
88
89 ccl_device_inline int stack_load_int_default(float *stack, uint a, uint value)
90 {
91         return (a == (uint)SVM_STACK_INVALID)? (int)value: stack_load_int(stack, a);
92 }
93
94 ccl_device_inline void stack_store_int(float *stack, uint a, int i)
95 {
96         kernel_assert(a < SVM_STACK_SIZE);
97
98         stack[a] = __int_as_float(i);
99 }
100
101 ccl_device_inline bool stack_valid(uint a)
102 {
103         return a != (uint)SVM_STACK_INVALID;
104 }
105
106 /* Reading Nodes */
107
108 ccl_device_inline uint4 read_node(KernelGlobals *kg, int *offset)
109 {
110         uint4 node = kernel_tex_fetch(__svm_nodes, *offset);
111         (*offset)++;
112         return node;
113 }
114
115 ccl_device_inline float4 read_node_float(KernelGlobals *kg, int *offset)
116 {
117         uint4 node = kernel_tex_fetch(__svm_nodes, *offset);
118         float4 f = make_float4(__uint_as_float(node.x), __uint_as_float(node.y), __uint_as_float(node.z), __uint_as_float(node.w));
119         (*offset)++;
120         return f;
121 }
122
123 ccl_device_inline float4 fetch_node_float(KernelGlobals *kg, int offset)
124 {
125         uint4 node = kernel_tex_fetch(__svm_nodes, offset);
126         return make_float4(__uint_as_float(node.x), __uint_as_float(node.y), __uint_as_float(node.z), __uint_as_float(node.w));
127 }
128
129 ccl_device_inline void decode_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
130 {
131         if(x) *x = (i & 0xFF);
132         if(y) *y = ((i >> 8) & 0xFF);
133         if(z) *z = ((i >> 16) & 0xFF);
134         if(w) *w = ((i >> 24) & 0xFF);
135 }
136
137 CCL_NAMESPACE_END
138
139 /* Nodes */
140
141 #include "kernel/svm/svm_noise.h"
142 #include "svm_texture.h"
143
144 #include "kernel/svm/svm_color_util.h"
145 #include "kernel/svm/svm_math_util.h"
146
147 #include "kernel/svm/svm_attribute.h"
148 #include "kernel/svm/svm_gradient.h"
149 #include "kernel/svm/svm_blackbody.h"
150 #include "kernel/svm/svm_closure.h"
151 #include "kernel/svm/svm_noisetex.h"
152 #include "kernel/svm/svm_convert.h"
153 #include "kernel/svm/svm_displace.h"
154 #include "kernel/svm/svm_fresnel.h"
155 #include "kernel/svm/svm_wireframe.h"
156 #include "kernel/svm/svm_wavelength.h"
157 #include "kernel/svm/svm_camera.h"
158 #include "kernel/svm/svm_geometry.h"
159 #include "kernel/svm/svm_hsv.h"
160 #include "kernel/svm/svm_image.h"
161 #include "kernel/svm/svm_gamma.h"
162 #include "kernel/svm/svm_brightness.h"
163 #include "kernel/svm/svm_invert.h"
164 #include "kernel/svm/svm_light_path.h"
165 #include "kernel/svm/svm_magic.h"
166 #include "kernel/svm/svm_mapping.h"
167 #include "kernel/svm/svm_normal.h"
168 #include "kernel/svm/svm_wave.h"
169 #include "kernel/svm/svm_math.h"
170 #include "kernel/svm/svm_mix.h"
171 #include "kernel/svm/svm_ramp.h"
172 #include "kernel/svm/svm_sepcomb_hsv.h"
173 #include "kernel/svm/svm_sepcomb_vector.h"
174 #include "kernel/svm/svm_musgrave.h"
175 #include "kernel/svm/svm_sky.h"
176 #include "kernel/svm/svm_tex_coord.h"
177 #include "kernel/svm/svm_value.h"
178 #include "kernel/svm/svm_voronoi.h"
179 #include "kernel/svm/svm_checker.h"
180 #include "kernel/svm/svm_brick.h"
181 #include "kernel/svm/svm_vector_transform.h"
182 #include "kernel/svm/svm_voxel.h"
183 #include "kernel/svm/svm_bump.h"
184
185 #ifdef __SHADER_RAYTRACE__
186 #  include "kernel/svm/svm_bevel.h"
187 #endif
188
189 CCL_NAMESPACE_BEGIN
190
191 #define NODES_GROUP(group) ((group) <= __NODES_MAX_GROUP__)
192 #define NODES_FEATURE(feature) ((__NODES_FEATURES__ & (feature)) != 0)
193
194 /* Main Interpreter Loop */
195 ccl_device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ccl_addr_space PathState *state, ShaderType type, int path_flag)
196 {
197         float stack[SVM_STACK_SIZE];
198         int offset = sd->shader & SHADER_MASK;
199
200         while(1) {
201                 uint4 node = read_node(kg, &offset);
202
203                 switch(node.x) {
204 #if NODES_GROUP(NODE_GROUP_LEVEL_0)
205                         case NODE_SHADER_JUMP: {
206                                 if(type == SHADER_TYPE_SURFACE) offset = node.y;
207                                 else if(type == SHADER_TYPE_VOLUME) offset = node.z;
208                                 else if(type == SHADER_TYPE_DISPLACEMENT) offset = node.w;
209                                 else return;
210                                 break;
211                         }
212                         case NODE_CLOSURE_BSDF:
213                                 svm_node_closure_bsdf(kg, sd, stack, node, type, path_flag, &offset);
214                                 break;
215                         case NODE_CLOSURE_EMISSION:
216                                 svm_node_closure_emission(sd, stack, node);
217                                 break;
218                         case NODE_CLOSURE_BACKGROUND:
219                                 svm_node_closure_background(sd, stack, node);
220                                 break;
221                         case NODE_CLOSURE_SET_WEIGHT:
222                                 svm_node_closure_set_weight(sd, node.y, node.z, node.w);
223                                 break;
224                         case NODE_CLOSURE_WEIGHT:
225                                 svm_node_closure_weight(sd, stack, node.y);
226                                 break;
227                         case NODE_EMISSION_WEIGHT:
228                                 svm_node_emission_weight(kg, sd, stack, node);
229                                 break;
230                         case NODE_MIX_CLOSURE:
231                                 svm_node_mix_closure(sd, stack, node);
232                                 break;
233                         case NODE_JUMP_IF_ZERO:
234                                 if(stack_load_float(stack, node.z) == 0.0f)
235                                         offset += node.y;
236                                 break;
237                         case NODE_JUMP_IF_ONE:
238                                 if(stack_load_float(stack, node.z) == 1.0f)
239                                         offset += node.y;
240                                 break;
241                         case NODE_GEOMETRY:
242                                 svm_node_geometry(kg, sd, stack, node.y, node.z);
243                                 break;
244                         case NODE_CONVERT:
245                                 svm_node_convert(sd, stack, node.y, node.z, node.w);
246                                 break;
247                         case NODE_TEX_COORD:
248                                 svm_node_tex_coord(kg, sd, path_flag, stack, node, &offset);
249                                 break;
250                         case NODE_VALUE_F:
251                                 svm_node_value_f(kg, sd, stack, node.y, node.z);
252                                 break;
253                         case NODE_VALUE_V:
254                                 svm_node_value_v(kg, sd, stack, node.y, &offset);
255                                 break;
256                         case NODE_ATTR:
257                                 svm_node_attr(kg, sd, stack, node);
258                                 break;
259 #  if NODES_FEATURE(NODE_FEATURE_BUMP)
260                         case NODE_GEOMETRY_BUMP_DX:
261                                 svm_node_geometry_bump_dx(kg, sd, stack, node.y, node.z);
262                                 break;
263                         case NODE_GEOMETRY_BUMP_DY:
264                                 svm_node_geometry_bump_dy(kg, sd, stack, node.y, node.z);
265                                 break;
266                         case NODE_SET_DISPLACEMENT:
267                                 svm_node_set_displacement(kg, sd, stack, node.y);
268                                 break;
269                         case NODE_DISPLACEMENT:
270                                 svm_node_displacement(kg, sd, stack, node);
271                                 break;
272                         case NODE_VECTOR_DISPLACEMENT:
273                                 svm_node_vector_displacement(kg, sd, stack, node, &offset);
274                                 break;
275 #  endif  /* NODES_FEATURE(NODE_FEATURE_BUMP) */
276 #  ifdef __TEXTURES__
277                         case NODE_TEX_IMAGE:
278                                 svm_node_tex_image(kg, sd, stack, node);
279                                 break;
280                         case NODE_TEX_IMAGE_BOX:
281                                 svm_node_tex_image_box(kg, sd, stack, node);
282                                 break;
283                         case NODE_TEX_NOISE:
284                                 svm_node_tex_noise(kg, sd, stack, node, &offset);
285                                 break;
286 #  endif  /* __TEXTURES__ */
287 #  ifdef __EXTRA_NODES__
288 #    if NODES_FEATURE(NODE_FEATURE_BUMP)
289                         case NODE_SET_BUMP:
290                                 svm_node_set_bump(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_TEX_COORD_BUMP_DX:
299                                 svm_node_tex_coord_bump_dx(kg, sd, path_flag, stack, node, &offset);
300                                 break;
301                         case NODE_TEX_COORD_BUMP_DY:
302                                 svm_node_tex_coord_bump_dy(kg, sd, path_flag, stack, node, &offset);
303                                 break;
304                         case NODE_CLOSURE_SET_NORMAL:
305                                 svm_node_set_normal(kg, sd, stack, node.y, node.z);
306                                 break;
307 #      if NODES_FEATURE(NODE_FEATURE_BUMP_STATE)
308                         case NODE_ENTER_BUMP_EVAL:
309                                 svm_node_enter_bump_eval(kg, sd, stack, node.y);
310                                 break;
311                         case NODE_LEAVE_BUMP_EVAL:
312                                 svm_node_leave_bump_eval(kg, sd, stack, node.y);
313                                 break;
314 #      endif /* NODES_FEATURE(NODE_FEATURE_BUMP_STATE) */
315 #    endif  /* NODES_FEATURE(NODE_FEATURE_BUMP) */
316                         case NODE_HSV:
317                                 svm_node_hsv(kg, sd, stack, node, &offset);
318                                 break;
319 #  endif  /* __EXTRA_NODES__ */
320 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_0) */
321
322 #if NODES_GROUP(NODE_GROUP_LEVEL_1)
323                         case NODE_CLOSURE_HOLDOUT:
324                                 svm_node_closure_holdout(sd, stack, node);
325                                 break;
326                         case NODE_CLOSURE_AMBIENT_OCCLUSION:
327                                 svm_node_closure_ambient_occlusion(sd, stack, node);
328                                 break;
329                         case NODE_FRESNEL:
330                                 svm_node_fresnel(sd, stack, node.y, node.z, node.w);
331                                 break;
332                         case NODE_LAYER_WEIGHT:
333                                 svm_node_layer_weight(sd, stack, node);
334                                 break;
335 #  if NODES_FEATURE(NODE_FEATURE_VOLUME)
336                         case NODE_CLOSURE_VOLUME:
337                                 svm_node_closure_volume(kg, sd, stack, node, type, path_flag);
338                                 break;
339 #  endif  /* NODES_FEATURE(NODE_FEATURE_VOLUME) */
340 #  ifdef __EXTRA_NODES__
341                         case NODE_MATH:
342                                 svm_node_math(kg, sd, stack, node.y, node.z, node.w, &offset);
343                                 break;
344                         case NODE_VECTOR_MATH:
345                                 svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset);
346                                 break;
347                         case NODE_RGB_RAMP:
348                                 svm_node_rgb_ramp(kg, sd, stack, node, &offset);
349                                 break;
350                         case NODE_GAMMA:
351                                 svm_node_gamma(sd, stack, node.y, node.z, node.w);
352                                 break;
353                         case NODE_BRIGHTCONTRAST:
354                                 svm_node_brightness(sd, stack, node.y, node.z, node.w);
355                                 break;
356                         case NODE_LIGHT_PATH:
357                                 svm_node_light_path(sd, state, stack, node.y, node.z, path_flag);
358                                 break;
359                         case NODE_OBJECT_INFO:
360                                 svm_node_object_info(kg, sd, stack, node.y, node.z);
361                                 break;
362                         case NODE_PARTICLE_INFO:
363                                 svm_node_particle_info(kg, sd, stack, node.y, node.z);
364                                 break;
365 #    ifdef __HAIR__
366 #      if NODES_FEATURE(NODE_FEATURE_HAIR)
367                         case NODE_HAIR_INFO:
368                                 svm_node_hair_info(kg, sd, stack, node.y, node.z);
369                                 break;
370 #      endif  /* NODES_FEATURE(NODE_FEATURE_HAIR) */
371 #    endif  /* __HAIR__ */
372 #  endif  /* __EXTRA_NODES__ */
373 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_1) */
374
375 #if NODES_GROUP(NODE_GROUP_LEVEL_2)
376                         case NODE_MAPPING:
377                                 svm_node_mapping(kg, sd, stack, node.y, node.z, &offset);
378                                 break;
379                         case NODE_MIN_MAX:
380                                 svm_node_min_max(kg, sd, stack, node.y, node.z, &offset);
381                                 break;
382                         case NODE_CAMERA:
383                                 svm_node_camera(kg, sd, stack, node.y, node.z, node.w);
384                                 break;
385 #  ifdef __TEXTURES__
386                         case NODE_TEX_ENVIRONMENT:
387                                 svm_node_tex_environment(kg, sd, stack, node);
388                                 break;
389                         case NODE_TEX_SKY:
390                                 svm_node_tex_sky(kg, sd, stack, node, &offset);
391                                 break;
392                         case NODE_TEX_GRADIENT:
393                                 svm_node_tex_gradient(sd, stack, node);
394                                 break;
395                         case NODE_TEX_VORONOI:
396                                 svm_node_tex_voronoi(kg, sd, stack, node, &offset);
397                                 break;
398                         case NODE_TEX_MUSGRAVE:
399                                 svm_node_tex_musgrave(kg, sd, stack, node, &offset);
400                                 break;
401                         case NODE_TEX_WAVE:
402                                 svm_node_tex_wave(kg, sd, stack, node, &offset);
403                                 break;
404                         case NODE_TEX_MAGIC:
405                                 svm_node_tex_magic(kg, sd, stack, node, &offset);
406                                 break;
407                         case NODE_TEX_CHECKER:
408                                 svm_node_tex_checker(kg, sd, stack, node);
409                                 break;
410                         case NODE_TEX_BRICK:
411                                 svm_node_tex_brick(kg, sd, stack, node, &offset);
412                                 break;
413 #  endif  /* __TEXTURES__ */
414 #  ifdef __EXTRA_NODES__
415                         case NODE_NORMAL:
416                                 svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset);
417                                 break;
418                         case NODE_LIGHT_FALLOFF:
419                                 svm_node_light_falloff(sd, stack, node);
420                                 break;
421 #  endif  /* __EXTRA_NODES__ */
422 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_2) */
423
424 #if NODES_GROUP(NODE_GROUP_LEVEL_3)
425                         case NODE_RGB_CURVES:
426                         case NODE_VECTOR_CURVES:
427                                 svm_node_curves(kg, sd, stack, node, &offset);
428                                 break;
429                         case NODE_TANGENT:
430                                 svm_node_tangent(kg, sd, stack, node);
431                                 break;
432                         case NODE_NORMAL_MAP:
433                                 svm_node_normal_map(kg, sd, stack, node);
434                                 break;
435 #  ifdef __EXTRA_NODES__
436                         case NODE_INVERT:
437                                 svm_node_invert(sd, stack, node.y, node.z, node.w);
438                                 break;
439                         case NODE_MIX:
440                                 svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
441                                 break;
442                         case NODE_SEPARATE_VECTOR:
443                                 svm_node_separate_vector(sd, stack, node.y, node.z, node.w);
444                                 break;
445                         case NODE_COMBINE_VECTOR:
446                                 svm_node_combine_vector(sd, stack, node.y, node.z, node.w);
447                                 break;
448                         case NODE_SEPARATE_HSV:
449                                 svm_node_separate_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
450                                 break;
451                         case NODE_COMBINE_HSV:
452                                 svm_node_combine_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
453                                 break;
454                         case NODE_VECTOR_TRANSFORM:
455                                 svm_node_vector_transform(kg, sd, stack, node);
456                                 break;
457                         case NODE_WIREFRAME:
458                                 svm_node_wireframe(kg, sd, stack, node);
459                                 break;
460                         case NODE_WAVELENGTH:
461                                 svm_node_wavelength(sd, stack, node.y, node.z);
462                                 break;
463                         case NODE_BLACKBODY:
464                                 svm_node_blackbody(kg, sd, stack, node.y, node.z);
465                                 break;
466 #  endif  /* __EXTRA_NODES__ */
467 #  if NODES_FEATURE(NODE_FEATURE_VOLUME)
468                         case NODE_TEX_VOXEL:
469                                 svm_node_tex_voxel(kg, sd, stack, node, &offset);
470                                 break;
471 #  endif  /* NODES_FEATURE(NODE_FEATURE_VOLUME) */
472 #  ifdef __SHADER_RAYTRACE__
473                         case NODE_BEVEL:
474                                 svm_node_bevel(kg, sd, state, stack, node);
475                                 break;
476 #  endif  /* __SHADER_RAYTRACE__ */
477 #endif  /* NODES_GROUP(NODE_GROUP_LEVEL_3) */
478                         case NODE_END:
479                                 return;
480                         default:
481                                 kernel_assert(!"Unknown node type was passed to the SVM machine");
482                                 return;
483                 }
484         }
485 }
486
487 #undef NODES_GROUP
488 #undef NODES_FEATURE
489
490 CCL_NAMESPACE_END
491
492 #endif /* __SVM_H__ */
493