Fix T43755: Wireframe attribute doesn't work with displace
[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 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 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_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_util.h"
168 #include "svm_math.h"
169 #include "svm_mix.h"
170 #include "svm_ramp.h"
171 #include "svm_sepcomb_hsv.h"
172 #include "svm_sepcomb_vector.h"
173 #include "svm_musgrave.h"
174 #include "svm_sky.h"
175 #include "svm_tex_coord.h"
176 #include "svm_value.h"
177 #include "svm_voronoi.h"
178 #include "svm_checker.h"
179 #include "svm_brick.h"
180 #include "svm_vector_transform.h"
181
182 CCL_NAMESPACE_BEGIN
183
184 /* Main Interpreter Loop */
185
186 ccl_device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderType type, int path_flag)
187 {
188         float stack[SVM_STACK_SIZE];
189         int offset = sd->shader & SHADER_MASK;
190
191         while(1) {
192                 uint4 node = read_node(kg, &offset);
193
194                 switch(node.x) {
195                         case NODE_SHADER_JUMP: {
196                                 if(type == SHADER_TYPE_SURFACE) offset = node.y;
197                                 else if(type == SHADER_TYPE_VOLUME) offset = node.z;
198                                 else if(type == SHADER_TYPE_DISPLACEMENT) offset = node.w;
199                                 else return;
200                                 break;
201                         }
202                         case NODE_CLOSURE_BSDF:
203                                 svm_node_closure_bsdf(kg, sd, stack, node, path_flag, &offset);
204                                 break;
205                         case NODE_CLOSURE_EMISSION:
206                                 svm_node_closure_emission(sd, stack, node);
207                                 break;
208                         case NODE_CLOSURE_BACKGROUND:
209                                 svm_node_closure_background(sd, stack, node);
210                                 break;
211                         case NODE_CLOSURE_HOLDOUT:
212                                 svm_node_closure_holdout(sd, stack, node);
213                                 break;
214                         case NODE_CLOSURE_AMBIENT_OCCLUSION:
215                                 svm_node_closure_ambient_occlusion(sd, stack, node);
216                                 break;
217                         case NODE_CLOSURE_VOLUME:
218                                 svm_node_closure_volume(kg, sd, stack, node, path_flag);
219                                 break;
220                         case NODE_CLOSURE_SET_WEIGHT:
221                                 svm_node_closure_set_weight(sd, node.y, node.z, node.w);
222                                 break;
223                         case NODE_CLOSURE_WEIGHT:
224                                 svm_node_closure_weight(sd, stack, node.y);
225                                 break;
226                         case NODE_EMISSION_WEIGHT:
227                                 svm_node_emission_weight(kg, sd, stack, node);
228                                 break;
229                         case NODE_MIX_CLOSURE:
230                                 svm_node_mix_closure(sd, stack, node);
231                                 break;
232                         case NODE_JUMP_IF_ZERO:
233                                 if(stack_load_float(stack, node.z) == 0.0f)
234                                         offset += node.y;
235                                 break;
236                         case NODE_JUMP_IF_ONE:
237                                 if(stack_load_float(stack, node.z) == 1.0f)
238                                         offset += node.y;
239                                 break;
240 #ifdef __TEXTURES__
241                         case NODE_TEX_IMAGE:
242                                 svm_node_tex_image(kg, sd, stack, node);
243                                 break;
244                         case NODE_TEX_IMAGE_BOX:
245                                 svm_node_tex_image_box(kg, sd, stack, node);
246                                 break;
247                         case NODE_TEX_ENVIRONMENT:
248                                 svm_node_tex_environment(kg, sd, stack, node);
249                                 break;
250                         case NODE_TEX_SKY:
251                                 svm_node_tex_sky(kg, sd, stack, node, &offset);
252                                 break;
253                         case NODE_TEX_GRADIENT:
254                                 svm_node_tex_gradient(sd, stack, node);
255                                 break;
256                         case NODE_TEX_NOISE:
257                                 svm_node_tex_noise(kg, sd, stack, node, &offset);
258                                 break;
259                         case NODE_TEX_VORONOI:
260                                 svm_node_tex_voronoi(kg, sd, stack, node, &offset);
261                                 break;
262                         case NODE_TEX_MUSGRAVE:
263                                 svm_node_tex_musgrave(kg, sd, stack, node, &offset);
264                                 break;
265                         case NODE_TEX_WAVE:
266                                 svm_node_tex_wave(kg, sd, stack, node, &offset);
267                                 break;
268                         case NODE_TEX_MAGIC:
269                                 svm_node_tex_magic(kg, sd, stack, node, &offset);
270                                 break;
271                         case NODE_TEX_CHECKER:
272                                 svm_node_tex_checker(kg, sd, stack, node);
273                                 break;
274                         case NODE_TEX_BRICK:
275                                 svm_node_tex_brick(kg, sd, stack, node, &offset);
276                                 break;
277 #endif
278                         case NODE_CAMERA:
279                                 svm_node_camera(kg, sd, stack, node.y, node.z, node.w);
280                                 break;
281                         case NODE_GEOMETRY:
282                                 svm_node_geometry(kg, sd, stack, node.y, node.z);
283                                 break;
284 #ifdef __EXTRA_NODES__
285                         case NODE_GEOMETRY_BUMP_DX:
286                                 svm_node_geometry_bump_dx(kg, sd, stack, node.y, node.z);
287                                 break;
288                         case NODE_GEOMETRY_BUMP_DY:
289                                 svm_node_geometry_bump_dy(kg, sd, stack, node.y, node.z);
290                                 break;
291                         case NODE_LIGHT_PATH:
292                                 svm_node_light_path(sd, stack, node.y, node.z, path_flag);
293                                 break;
294                         case NODE_OBJECT_INFO:
295                                 svm_node_object_info(kg, sd, stack, node.y, node.z);
296                                 break;
297                         case NODE_PARTICLE_INFO:
298                                 svm_node_particle_info(kg, sd, stack, node.y, node.z);
299                                 break;
300 #ifdef __HAIR__
301                         case NODE_HAIR_INFO:
302                                 svm_node_hair_info(kg, sd, stack, node.y, node.z);
303                                 break;
304 #endif
305
306 #endif
307                         case NODE_CONVERT:
308                                 svm_node_convert(sd, stack, node.y, node.z, node.w);
309                                 break;
310                         case NODE_VALUE_F:
311                                 svm_node_value_f(kg, sd, stack, node.y, node.z);
312                                 break;
313                         case NODE_VALUE_V:
314                                 svm_node_value_v(kg, sd, stack, node.y, &offset);
315                                 break;
316 #ifdef __EXTRA_NODES__
317                         case NODE_INVERT:
318                                 svm_node_invert(sd, stack, node.y, node.z, node.w);
319                                 break;
320                         case NODE_GAMMA:
321                                 svm_node_gamma(sd, stack, node.y, node.z, node.w);
322                                 break;
323                         case NODE_BRIGHTCONTRAST:
324                                 svm_node_brightness(sd, stack, node.y, node.z, node.w);
325                                 break;
326                         case NODE_MIX:
327                                 svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
328                                 break;
329                         case NODE_SEPARATE_VECTOR:
330                                 svm_node_separate_vector(sd, stack, node.y, node.z, node.w);
331                                 break;
332                         case NODE_COMBINE_VECTOR:
333                                 svm_node_combine_vector(sd, stack, node.y, node.z, node.w);
334                                 break;
335                         case NODE_SEPARATE_HSV:
336                                 svm_node_separate_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
337                                 break;
338                         case NODE_COMBINE_HSV:
339                                 svm_node_combine_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
340                                 break;
341                         case NODE_HSV:
342                                 svm_node_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
343                                 break;
344 #endif
345                         case NODE_ATTR:
346                                 svm_node_attr(kg, sd, stack, node);
347                                 break;
348 #ifdef __EXTRA_NODES__
349                         case NODE_ATTR_BUMP_DX:
350                                 svm_node_attr_bump_dx(kg, sd, stack, node);
351                                 break;
352                         case NODE_ATTR_BUMP_DY:
353                                 svm_node_attr_bump_dy(kg, sd, stack, node);
354                                 break;
355 #endif
356                         case NODE_FRESNEL:
357                                 svm_node_fresnel(sd, stack, node.y, node.z, node.w);
358                                 break;
359                         case NODE_LAYER_WEIGHT:
360                                 svm_node_layer_weight(sd, stack, node);
361                                 break;
362 #ifdef __EXTRA_NODES__
363                         case NODE_WIREFRAME:
364                                 svm_node_wireframe(kg, sd, stack, node);
365                                 break;
366                         case NODE_WAVELENGTH:
367                                 svm_node_wavelength(sd, stack, node.y, node.z);
368                                 break;
369                         case NODE_BLACKBODY:
370                                 svm_node_blackbody(kg, sd, stack, node.y, node.z);
371                                 break;
372                         case NODE_SET_DISPLACEMENT:
373                                 svm_node_set_displacement(sd, stack, node.y);
374                                 break;
375                         case NODE_SET_BUMP:
376                                 svm_node_set_bump(kg, sd, stack, node);
377                                 break;
378                         case NODE_MATH:
379                                 svm_node_math(kg, sd, stack, node.y, node.z, node.w, &offset);
380                                 break;
381                         case NODE_VECTOR_MATH:
382                                 svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset);
383                                 break;
384                         case NODE_VECTOR_TRANSFORM:
385                                 svm_node_vector_transform(kg, sd, stack, node);
386                                 break;
387                         case NODE_NORMAL:
388                                 svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset);
389                                 break;
390 #endif
391                         case NODE_MAPPING:
392                                 svm_node_mapping(kg, sd, stack, node.y, node.z, &offset);
393                                 break;
394                         case NODE_MIN_MAX:
395                                 svm_node_min_max(kg, sd, stack, node.y, node.z, &offset);
396                                 break;
397                         case NODE_TEX_COORD:
398                                 svm_node_tex_coord(kg, sd, path_flag, stack, node, &offset);
399                                 break;
400 #ifdef __EXTRA_NODES__
401                         case NODE_TEX_COORD_BUMP_DX:
402                                 svm_node_tex_coord_bump_dx(kg, sd, path_flag, stack, node, &offset);
403                                 break;
404                         case NODE_TEX_COORD_BUMP_DY:
405                                 svm_node_tex_coord_bump_dy(kg, sd, path_flag, stack, node, &offset);
406                                 break;
407                         case NODE_CLOSURE_SET_NORMAL:
408                                 svm_node_set_normal(kg, sd, stack, node.y, node.z );
409                                 break;
410                         case NODE_RGB_RAMP:
411                                 svm_node_rgb_ramp(kg, sd, stack, node, &offset);
412                                 break;
413                         case NODE_RGB_CURVES:
414                                 svm_node_rgb_curves(kg, sd, stack, node, &offset);
415                                 break;
416                         case NODE_VECTOR_CURVES:
417                                 svm_node_vector_curves(kg, sd, stack, node, &offset);
418                                 break;
419                         case NODE_LIGHT_FALLOFF:
420                                 svm_node_light_falloff(sd, stack, node);
421                                 break;
422 #endif
423                         case NODE_TANGENT:
424                                 svm_node_tangent(kg, sd, stack, node);
425                                 break;
426                         case NODE_NORMAL_MAP:
427                                 svm_node_normal_map(kg, sd, stack, node);
428                                 break;  
429                         case NODE_END:
430                         default:
431                                 return;
432                 }
433         }
434 }
435
436 CCL_NAMESPACE_END
437
438 #endif /* __SVM_H__ */
439