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