should fix builds for osx
[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 __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 __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 __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 __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 __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 __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 __device_inline float 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 __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 __device_inline bool stack_valid(uint a)
103 {
104         return a != (uint)SVM_STACK_INVALID;
105 }
106
107 /* Reading Nodes */
108
109 __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 __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 __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 __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_attribute.h"
146 #include "svm_gradient.h"
147 #include "svm_blackbody.h"
148 #include "svm_closure.h"
149 #include "svm_noisetex.h"
150 #include "svm_convert.h"
151 #include "svm_displace.h"
152 #include "svm_fresnel.h"
153 #include "svm_wireframe.h"
154 #include "svm_wavelength.h"
155 #include "svm_camera.h"
156 #include "svm_geometry.h"
157 #include "svm_hsv.h"
158 #include "svm_image.h"
159 #include "svm_gamma.h"
160 #include "svm_brightness.h"
161 #include "svm_invert.h"
162 #include "svm_light_path.h"
163 #include "svm_magic.h"
164 #include "svm_mapping.h"
165 #include "svm_normal.h"
166 #include "svm_wave.h"
167 #include "svm_math.h"
168 #include "svm_mix.h"
169 #include "svm_ramp.h"
170 #include "svm_sepcomb_rgb.h"
171 #include "svm_sepcomb_hsv.h"
172 #include "svm_musgrave.h"
173 #include "svm_sky.h"
174 #include "svm_tex_coord.h"
175 #include "svm_value.h"
176 #include "svm_voronoi.h"
177 #include "svm_checker.h"
178 #include "svm_brick.h"
179 #include "svm_vector_transform.h"
180
181 CCL_NAMESPACE_BEGIN
182
183 /* Main Interpreter Loop */
184
185 __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderType type, float randb, int path_flag)
186 {
187         float stack[SVM_STACK_SIZE];
188         float closure_weight = 1.0f;
189         int offset = sd->shader & SHADER_MASK;
190
191 #ifdef __MULTI_CLOSURE__
192         sd->num_closure = 0;
193         sd->randb_closure = randb;
194 #else
195         sd->closure.type = NBUILTIN_CLOSURES;
196 #endif
197
198         while(1) {
199                 uint4 node = read_node(kg, &offset);
200
201                 switch(node.x) {
202                         case NODE_SHADER_JUMP: {
203                                 if(type == SHADER_TYPE_SURFACE) offset = node.y;
204                                 else if(type == SHADER_TYPE_VOLUME) offset = node.z;
205                                 else if(type == SHADER_TYPE_DISPLACEMENT) offset = node.w;
206                                 else return;
207                                 break;
208                         }
209                         case NODE_CLOSURE_BSDF:
210                                 svm_node_closure_bsdf(kg, sd, stack, node, randb, path_flag, &offset);
211                                 break;
212                         case NODE_CLOSURE_EMISSION:
213                                 svm_node_closure_emission(sd, stack, node);
214                                 break;
215                         case NODE_CLOSURE_BACKGROUND:
216                                 svm_node_closure_background(sd, stack, node);
217                                 break;
218                         case NODE_CLOSURE_HOLDOUT:
219                                 svm_node_closure_holdout(sd, stack, node);
220                                 break;
221                         case NODE_CLOSURE_AMBIENT_OCCLUSION:
222                                 svm_node_closure_ambient_occlusion(sd, stack, node);
223                                 break;
224                         case NODE_CLOSURE_VOLUME:
225                                 svm_node_closure_volume(kg, sd, stack, node, path_flag);
226                                 break;
227                         case NODE_CLOSURE_SET_WEIGHT:
228                                 svm_node_closure_set_weight(sd, node.y, node.z, node.w);
229                                 break;
230                         case NODE_CLOSURE_WEIGHT:
231                                 svm_node_closure_weight(sd, stack, node.y);
232                                 break;
233                         case NODE_EMISSION_WEIGHT:
234                                 svm_node_emission_weight(kg, sd, stack, node);
235                                 break;
236                         case NODE_MIX_CLOSURE:
237                                 svm_node_mix_closure(sd, stack, node, &offset, &randb);
238                                 break;
239                         case NODE_ADD_CLOSURE:
240                                 svm_node_add_closure(sd, stack, node.y, node.z, &offset, &randb, &closure_weight);
241                                 break;
242                         case NODE_JUMP:
243                                 offset = node.y;
244                                 break;
245 #ifdef __IMAGE_TEXTURES__
246                         case NODE_TEX_IMAGE:
247                                 svm_node_tex_image(kg, sd, stack, node);
248                                 break;
249                         case NODE_TEX_IMAGE_BOX:
250                                 svm_node_tex_image_box(kg, sd, stack, node);
251                                 break;
252                         case NODE_TEX_ENVIRONMENT:
253                                 svm_node_tex_environment(kg, sd, stack, node);
254                                 break;
255 #endif
256 #ifdef __PROCEDURAL_TEXTURES__
257                         case NODE_TEX_SKY:
258                                 svm_node_tex_sky(kg, sd, stack, node.y, node.z);
259                                 break;
260                         case NODE_TEX_GRADIENT:
261                                 svm_node_tex_gradient(sd, stack, node);
262                                 break;
263                         case NODE_TEX_NOISE:
264                                 svm_node_tex_noise(kg, sd, stack, node, &offset);
265                                 break;
266                         case NODE_TEX_VORONOI:
267                                 svm_node_tex_voronoi(kg, sd, stack, node, &offset);
268                                 break;
269                         case NODE_TEX_MUSGRAVE:
270                                 svm_node_tex_musgrave(kg, sd, stack, node, &offset);
271                                 break;
272                         case NODE_TEX_WAVE:
273                                 svm_node_tex_wave(kg, sd, stack, node, &offset);
274                                 break;
275                         case NODE_TEX_MAGIC:
276                                 svm_node_tex_magic(kg, sd, stack, node, &offset);
277                                 break;
278                         case NODE_TEX_CHECKER:
279                                 svm_node_tex_checker(kg, sd, stack, node);
280                                 break;
281                         case NODE_TEX_BRICK:
282                                 svm_node_tex_brick(kg, sd, stack, node, &offset);
283                                 break;
284 #endif
285                         case NODE_CAMERA:
286                                 svm_node_camera(kg, sd, stack, node.y, node.z, node.w);
287                                 break;
288                         case NODE_GEOMETRY:
289                                 svm_node_geometry(kg, sd, stack, node.y, node.z);
290                                 break;
291 #ifdef __EXTRA_NODES__
292                         case NODE_GEOMETRY_BUMP_DX:
293                                 svm_node_geometry_bump_dx(kg, sd, stack, node.y, node.z);
294                                 break;
295                         case NODE_GEOMETRY_BUMP_DY:
296                                 svm_node_geometry_bump_dy(kg, sd, stack, node.y, node.z);
297                                 break;
298                         case NODE_LIGHT_PATH:
299                                 svm_node_light_path(sd, stack, node.y, node.z, path_flag);
300                                 break;
301                         case NODE_OBJECT_INFO:
302                                 svm_node_object_info(kg, sd, stack, node.y, node.z);
303                                 break;
304                         case NODE_PARTICLE_INFO:
305                                 svm_node_particle_info(kg, sd, stack, node.y, node.z);
306                                 break;
307 #ifdef __HAIR__
308                         case NODE_HAIR_INFO:
309                                 svm_node_hair_info(kg, sd, stack, node.y, node.z);
310                                 break;
311 #endif
312
313 #endif
314                         case NODE_CONVERT:
315                                 svm_node_convert(sd, stack, node.y, node.z, node.w);
316                                 break;
317                         case NODE_VALUE_F:
318                                 svm_node_value_f(kg, sd, stack, node.y, node.z);
319                                 break;
320                         case NODE_VALUE_V:
321                                 svm_node_value_v(kg, sd, stack, node.y, &offset);
322                                 break;
323 #ifdef __EXTRA_NODES__
324                         case NODE_INVERT:
325                                 svm_node_invert(sd, stack, node.y, node.z, node.w);
326                                 break;
327                         case NODE_GAMMA:
328                                 svm_node_gamma(sd, stack, node.y, node.z, node.w);
329                                 break;
330                         case NODE_BRIGHTCONTRAST:
331                                 svm_node_brightness(sd, stack, node.y, node.z, node.w);
332                                 break;
333                         case NODE_MIX:
334                                 svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
335                                 break;
336                         case NODE_SEPARATE_RGB:
337                                 svm_node_separate_rgb(sd, stack, node.y, node.z, node.w);
338                                 break;
339                         case NODE_COMBINE_RGB:
340                                 svm_node_combine_rgb(sd, stack, node.y, node.z, node.w);
341                                 break;
342                         case NODE_SEPARATE_HSV:
343                                 svm_node_separate_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
344                                 break;
345                         case NODE_COMBINE_HSV:
346                                 svm_node_combine_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
347                                 break;
348                         case NODE_HSV:
349                                 svm_node_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
350                                 break;
351 #endif
352                         case NODE_ATTR:
353                                 svm_node_attr(kg, sd, stack, node);
354                                 break;
355 #ifdef __EXTRA_NODES__
356                         case NODE_ATTR_BUMP_DX:
357                                 svm_node_attr_bump_dx(kg, sd, stack, node);
358                                 break;
359                         case NODE_ATTR_BUMP_DY:
360                                 svm_node_attr_bump_dy(kg, sd, stack, node);
361                                 break;
362 #endif
363                         case NODE_FRESNEL:
364                                 svm_node_fresnel(sd, stack, node.y, node.z, node.w);
365                                 break;
366                         case NODE_LAYER_WEIGHT:
367                                 svm_node_layer_weight(sd, stack, node);
368                                 break;
369 #ifdef __EXTRA_NODES__
370                         case NODE_WIREFRAME:
371                                 svm_node_wireframe(kg, sd, stack, node.y, node.z, node.w);
372                                 break;
373                         case NODE_WAVELENGTH:
374                                 svm_node_wavelength(sd, stack, node.y, node.z);
375                                 break;
376                         case NODE_BLACKBODY:
377                                 svm_node_blackbody(kg, sd, stack, node.y, node.z);
378                                 break;
379                         case NODE_SET_DISPLACEMENT:
380                                 svm_node_set_displacement(sd, stack, node.y);
381                                 break;
382                         case NODE_SET_BUMP:
383                                 svm_node_set_bump(kg, sd, stack, node);
384                                 break;
385                         case NODE_MATH:
386                                 svm_node_math(kg, sd, stack, node.y, node.z, node.w, &offset);
387                                 break;
388                         case NODE_VECTOR_MATH:
389                                 svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset);
390                                 break;
391                         case NODE_VECTOR_TRANSFORM:
392                                 svm_node_vector_transform(kg, sd, stack, node);
393                                 break;
394                         case NODE_NORMAL:
395                                 svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset);
396                                 break;
397 #endif
398                         case NODE_MAPPING:
399                                 svm_node_mapping(kg, sd, stack, node.y, node.z, &offset);
400                                 break;
401                         case NODE_MIN_MAX:
402                                 svm_node_min_max(kg, sd, stack, node.y, node.z, &offset);
403                                 break;
404                         case NODE_TEX_COORD:
405                                 svm_node_tex_coord(kg, sd, path_flag, stack, node.y, node.z);
406                                 break;
407 #ifdef __EXTRA_NODES__
408                         case NODE_TEX_COORD_BUMP_DX:
409                                 svm_node_tex_coord_bump_dx(kg, sd, path_flag, stack, node.y, node.z);
410                                 break;
411                         case NODE_TEX_COORD_BUMP_DY:
412                                 svm_node_tex_coord_bump_dy(kg, sd, path_flag, stack, node.y, node.z);
413                                 break;
414                         case NODE_CLOSURE_SET_NORMAL:
415                                 svm_node_set_normal(kg, sd, stack, node.y, node.z );
416                                 break;                  
417 #endif
418                         case NODE_EMISSION_SET_WEIGHT_TOTAL:
419                                 svm_node_emission_set_weight_total(kg, sd, node.y, node.z, node.w);
420                                 break;
421 #ifdef __EXTRA_NODES__
422                         case NODE_RGB_RAMP:
423                                 svm_node_rgb_ramp(kg, sd, stack, node, &offset);
424                                 break;
425                         case NODE_RGB_CURVES:
426                                 svm_node_rgb_curves(kg, sd, stack, node, &offset);
427                                 break;
428                         case NODE_VECTOR_CURVES:
429                                 svm_node_vector_curves(kg, sd, stack, node, &offset);
430                                 break;
431                         case NODE_LIGHT_FALLOFF:
432                                 svm_node_light_falloff(sd, stack, node);
433                                 break;
434 #endif                  
435 #ifdef __ANISOTROPIC__
436                         case NODE_TANGENT:
437                                 svm_node_tangent(kg, sd, stack, node);
438                                 break;
439 #endif                  
440 #ifdef __NORMAL_MAP__
441                         case NODE_NORMAL_MAP:
442                                 svm_node_normal_map(kg, sd, stack, node);
443                                 break;
444 #endif                  
445                         case NODE_END:
446                         default:
447 #ifndef __MULTI_CLOSURE__
448                                 sd->closure.weight *= closure_weight;
449 #endif
450                                 return;
451                 }
452         }
453 }
454
455 CCL_NAMESPACE_END
456
457 #endif /* __SVM_H__ */
458