9f12fe6002fbd79bfbf17309b0aafad35cc3e9b6
[blender.git] / intern / cycles / render / nodes.cpp
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 #include "image.h"
20 #include "nodes.h"
21 #include "svm.h"
22 #include "osl.h"
23
24 #include "util_transform.h"
25
26 CCL_NAMESPACE_BEGIN
27
28 /* Texture Mapping */
29
30 TextureMapping::TextureMapping()
31 {
32         translation = make_float3(0.0f, 0.0f, 0.0f);
33         rotation = make_float3(0.0f, 0.0f, 0.0f);
34         scale = make_float3(1.0f, 1.0f, 1.0f);
35
36         x_mapping = X;
37         y_mapping = Y;
38         z_mapping = Z;
39
40         projection = FLAT;
41 }
42
43 Transform TextureMapping::compute_transform()
44 {
45         Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
46
47         if(x_mapping != NONE)
48                 mmat[0][x_mapping-1] = 1.0f;
49         if(y_mapping != NONE)
50                 mmat[1][y_mapping-1] = 1.0f;
51         if(z_mapping != NONE)
52                 mmat[2][z_mapping-1] = 1.0f;
53
54         Transform smat = transform_scale(scale);
55         Transform rmat = transform_euler(rotation);
56         Transform tmat = transform_translate(translation);
57
58         return tmat*rmat*smat*mmat;
59 }
60
61 bool TextureMapping::skip()
62 {
63         if(translation != make_float3(0.0f, 0.0f, 0.0f))
64                 return false;
65         if(rotation != make_float3(0.0f, 0.0f, 0.0f))
66                 return false;
67         if(scale != make_float3(1.0f, 1.0f, 1.0f))
68                 return false;
69         
70         if(x_mapping != X || y_mapping != Y || z_mapping != Z)
71                 return false;
72         
73         return true;
74 }
75
76 void TextureMapping::compile(SVMCompiler& compiler, int offset_in, int offset_out)
77 {
78         if(offset_in == SVM_STACK_INVALID || offset_out == SVM_STACK_INVALID)
79                 return;
80
81         compiler.add_node(NODE_MAPPING, offset_in, offset_out);
82
83         Transform tfm = compute_transform();
84         compiler.add_node(tfm.x);
85         compiler.add_node(tfm.y);
86         compiler.add_node(tfm.z);
87         compiler.add_node(tfm.w);
88 }
89
90 /* Image Texture */
91
92 static ShaderEnum color_space_init()
93 {
94         ShaderEnum enm;
95
96         enm.insert("Linear", 0);
97         enm.insert("sRGB", 1);
98
99         return enm;
100 }
101
102 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
103
104 ImageTextureNode::ImageTextureNode()
105 : TextureNode("image_texture")
106 {
107         image_manager = NULL;
108         slot = -1;
109         filename = "";
110         color_space = ustring("sRGB");
111
112         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_UV);
113         add_output("Color", SHADER_SOCKET_COLOR);
114         add_output("Alpha", SHADER_SOCKET_FLOAT);
115 }
116
117 ImageTextureNode::~ImageTextureNode()
118 {
119         if(image_manager)
120                 image_manager->remove_image(filename);
121 }
122
123 ShaderNode *ImageTextureNode::clone() const
124 {
125         ImageTextureNode *node = new ImageTextureNode(*this);
126         node->image_manager = NULL;
127         node->slot = -1;
128         return node;
129 }
130
131 void ImageTextureNode::compile(SVMCompiler& compiler)
132 {
133         ShaderInput *vector_in = input("Vector");
134         ShaderOutput *color_out = output("Color");
135         ShaderOutput *alpha_out = output("Alpha");
136
137         image_manager = compiler.image_manager;
138         if(slot == -1)
139                 slot = image_manager->add_image(filename);
140
141         if(!color_out->links.empty())
142                 compiler.stack_assign(color_out);
143         if(!alpha_out->links.empty())
144                 compiler.stack_assign(alpha_out);
145
146         if(slot != -1) {
147                 compiler.stack_assign(vector_in);
148
149                 if(!tex_mapping.skip())
150                         tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
151
152                 compiler.add_node(NODE_TEX_IMAGE,
153                         slot,
154                         compiler.encode_uchar4(
155                                 vector_in->stack_offset,
156                                 color_out->stack_offset,
157                                 alpha_out->stack_offset,
158                                 color_space_enum[color_space]));
159         }
160         else {
161                 /* image not found */
162                 if(!color_out->links.empty()) {
163                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
164                         compiler.add_node(NODE_VALUE_V, make_float3(0, 0, 0));
165                 }
166                 if(!alpha_out->links.empty())
167                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), alpha_out->stack_offset);
168         }
169 }
170
171 void ImageTextureNode::compile(OSLCompiler& compiler)
172 {
173         compiler.parameter("filename", filename.c_str());
174         compiler.parameter("color_space", color_space.c_str());
175         compiler.add(this, "node_image_texture");
176 }
177
178 /* Environment Texture */
179
180 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
181
182 EnvironmentTextureNode::EnvironmentTextureNode()
183 : TextureNode("environment_texture")
184 {
185         image_manager = NULL;
186         slot = -1;
187         filename = "";
188         color_space = ustring("sRGB");
189
190         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
191         add_output("Color", SHADER_SOCKET_COLOR);
192         add_output("Alpha", SHADER_SOCKET_FLOAT);
193 }
194
195 EnvironmentTextureNode::~EnvironmentTextureNode()
196 {
197         if(image_manager)
198                 image_manager->remove_image(filename);
199 }
200
201 ShaderNode *EnvironmentTextureNode::clone() const
202 {
203         EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
204         node->image_manager = NULL;
205         node->slot = -1;
206         return node;
207 }
208
209 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
210 {
211         ShaderInput *vector_in = input("Vector");
212         ShaderOutput *color_out = output("Color");
213         ShaderOutput *alpha_out = output("Alpha");
214
215         image_manager = compiler.image_manager;
216         if(slot == -1)
217                 slot = image_manager->add_image(filename);
218
219         if(!color_out->links.empty())
220                 compiler.stack_assign(color_out);
221         if(!alpha_out->links.empty())
222                 compiler.stack_assign(alpha_out);
223
224         if(slot != -1) {
225                 compiler.stack_assign(vector_in);
226
227                 if(!tex_mapping.skip())
228                         tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
229
230                 compiler.add_node(NODE_TEX_ENVIRONMENT,
231                         slot,
232                         compiler.encode_uchar4(
233                                 vector_in->stack_offset,
234                                 color_out->stack_offset,
235                                 alpha_out->stack_offset,
236                                 color_space_enum[color_space]));
237         }
238         else {
239                 /* image not found */
240                 if(!color_out->links.empty()) {
241                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
242                         compiler.add_node(NODE_VALUE_V, make_float3(0, 0, 0));
243                 }
244                 if(!alpha_out->links.empty())
245                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), alpha_out->stack_offset);
246         }
247 }
248
249 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
250 {
251         compiler.parameter("filename", filename.c_str());
252         compiler.parameter("color_space", color_space.c_str());
253         compiler.add(this, "node_environment_texture");
254 }
255
256 /* Sky Texture */
257
258 static float2 sky_spherical_coordinates(float3 dir)
259 {
260         return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
261 }
262
263 static float sky_perez_function(float lam[6], float theta, float gamma)
264 {
265         return (1.f + lam[0]*expf(lam[1]/cosf(theta))) * (1.f + lam[2]*expf(lam[3]*gamma)  + lam[4]*cosf(gamma)*cosf(gamma));
266 }
267
268 static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity)
269 {
270         float2 spherical = sky_spherical_coordinates(dir);
271         float theta = spherical.x;
272         float phi = spherical.y;
273
274         ksunsky->theta = theta;
275         ksunsky->phi = phi;
276         ksunsky->dir = dir;
277
278         float theta2 = theta*theta;
279         float theta3 = theta*theta*theta;
280         float T = turbidity;
281         float T2 = T * T;
282
283         float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
284         ksunsky->zenith_Y = (4.0453f * T - 4.9710f) * tan(chi) - 0.2155f * T + 2.4192f;
285         ksunsky->zenith_Y *= 0.06f;
286
287         ksunsky->zenith_x =
288         (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
289         (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
290         (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
291
292         ksunsky->zenith_y =
293         (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
294         (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta  + 0.00516f) * T +
295         (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta  + 0.26688f);
296
297         ksunsky->perez_Y[0] = (0.1787f * T  - 1.4630f);
298         ksunsky->perez_Y[1] = (-0.3554f * T  + 0.4275f);
299         ksunsky->perez_Y[2] = (-0.0227f * T  + 5.3251f);
300         ksunsky->perez_Y[3] = (0.1206f * T  - 2.5771f);
301         ksunsky->perez_Y[4] = (-0.0670f * T  + 0.3703f);
302
303         ksunsky->perez_x[0] = (-0.0193f * T  - 0.2592f);
304         ksunsky->perez_x[1] = (-0.0665f * T  + 0.0008f);
305         ksunsky->perez_x[2] = (-0.0004f * T  + 0.2125f);
306         ksunsky->perez_x[3] = (-0.0641f * T  - 0.8989f);
307         ksunsky->perez_x[4] = (-0.0033f * T  + 0.0452f);
308
309         ksunsky->perez_y[0] = (-0.0167f * T  - 0.2608f);
310         ksunsky->perez_y[1] = (-0.0950f * T  + 0.0092f);
311         ksunsky->perez_y[2] = (-0.0079f * T  + 0.2102f);
312         ksunsky->perez_y[3] = (-0.0441f * T  - 1.6537f);
313         ksunsky->perez_y[4] = (-0.0109f * T  + 0.0529f);
314
315         ksunsky->zenith_Y /= sky_perez_function(ksunsky->perez_Y, 0, theta);
316         ksunsky->zenith_x /= sky_perez_function(ksunsky->perez_x, 0, theta);
317         ksunsky->zenith_y /= sky_perez_function(ksunsky->perez_y, 0, theta);
318 }
319
320 SkyTextureNode::SkyTextureNode()
321 : TextureNode("sky_texture")
322 {
323         sun_direction = make_float3(0.0f, 0.0f, 1.0f);
324         turbidity = 2.2f;
325
326         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
327         add_output("Color", SHADER_SOCKET_COLOR);
328 }
329
330 void SkyTextureNode::compile(SVMCompiler& compiler)
331 {
332         ShaderInput *vector_in = input("Vector");
333         ShaderOutput *color_out = output("Color");
334
335         if(compiler.sunsky) {
336                 sky_texture_precompute(compiler.sunsky, sun_direction, turbidity);
337                 compiler.sunsky = NULL;
338         }
339
340         if(vector_in->link)
341                 compiler.stack_assign(vector_in);
342         if(!tex_mapping.skip())
343                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
344
345         compiler.stack_assign(color_out);
346         compiler.add_node(NODE_TEX_SKY, vector_in->stack_offset, color_out->stack_offset);
347 }
348
349 void SkyTextureNode::compile(OSLCompiler& compiler)
350 {
351         compiler.parameter_vector("sun_direction", sun_direction);
352         compiler.parameter("turbidity", turbidity);
353         compiler.add(this, "node_sky_texture");
354 }
355
356 /* Gradient Texture */
357
358 static ShaderEnum gradient_type_init()
359 {
360         ShaderEnum enm;
361
362         enm.insert("Linear", NODE_BLEND_LINEAR);
363         enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
364         enm.insert("Easing", NODE_BLEND_EASING);
365         enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
366         enm.insert("Radial", NODE_BLEND_RADIAL);
367         enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
368         enm.insert("Spherical", NODE_BLEND_SPHERICAL);
369
370         return enm;
371 }
372
373 ShaderEnum GradientTextureNode::type_enum = gradient_type_init();
374
375 GradientTextureNode::GradientTextureNode()
376 : TextureNode("gradient_texture")
377 {
378         type = ustring("Linear");
379
380         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
381         add_output("Color", SHADER_SOCKET_COLOR);
382         add_output("Fac", SHADER_SOCKET_FLOAT);
383 }
384
385 void GradientTextureNode::compile(SVMCompiler& compiler)
386 {
387         ShaderInput *vector_in = input("Vector");
388         ShaderOutput *color_out = output("Color");
389         ShaderOutput *fac_out = output("Fac");
390
391         if(vector_in->link) compiler.stack_assign(vector_in);
392
393         if(!tex_mapping.skip())
394                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
395
396         if(!fac_out->links.empty())
397                 compiler.stack_assign(fac_out);
398         if(!color_out->links.empty())
399                 compiler.stack_assign(color_out);
400
401         compiler.add_node(NODE_TEX_GRADIENT,
402                 compiler.encode_uchar4(type_enum[type], vector_in->stack_offset, fac_out->stack_offset, color_out->stack_offset));
403 }
404
405 void GradientTextureNode::compile(OSLCompiler& compiler)
406 {
407         compiler.parameter("Type", type);
408         compiler.add(this, "node_gradient_texture");
409 }
410
411 /* Noise Texture */
412
413 NoiseTextureNode::NoiseTextureNode()
414 : TextureNode("noise_texture")
415 {
416         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
417         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
418         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
419         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
420
421         add_output("Color", SHADER_SOCKET_COLOR);
422         add_output("Fac", SHADER_SOCKET_FLOAT);
423 }
424
425 void NoiseTextureNode::compile(SVMCompiler& compiler)
426 {
427         ShaderInput *distortion_in = input("Distortion");
428         ShaderInput *detail_in = input("Detail");
429         ShaderInput *scale_in = input("Scale");
430         ShaderInput *vector_in = input("Vector");
431         ShaderOutput *color_out = output("Color");
432         ShaderOutput *fac_out = output("Fac");
433
434         if(vector_in->link) compiler.stack_assign(vector_in);
435         if(scale_in->link) compiler.stack_assign(scale_in);
436         if(detail_in->link) compiler.stack_assign(detail_in);
437         if(distortion_in->link) compiler.stack_assign(distortion_in);
438
439         if(!tex_mapping.skip())
440                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
441
442         if(!fac_out->links.empty())
443                 compiler.stack_assign(fac_out);
444         if(!color_out->links.empty())
445                 compiler.stack_assign(color_out);
446
447         compiler.add_node(NODE_TEX_NOISE,
448                 compiler.encode_uchar4(vector_in->stack_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
449                 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset));
450         compiler.add_node(
451                 __float_as_int(scale_in->value.x),
452                 __float_as_int(detail_in->value.x),
453                 __float_as_int(distortion_in->value.x));
454 }
455
456 void NoiseTextureNode::compile(OSLCompiler& compiler)
457 {
458         compiler.add(this, "node_noise_texture");
459 }
460
461 /* Voronoi Texture */
462
463 static ShaderEnum voronoi_coloring_init()
464 {
465         ShaderEnum enm;
466
467         enm.insert("Intensity", NODE_VORONOI_INTENSITY);
468         enm.insert("Cells", NODE_VORONOI_CELLS);
469
470         return enm;
471 }
472
473 ShaderEnum VoronoiTextureNode::coloring_enum  = voronoi_coloring_init();
474
475 VoronoiTextureNode::VoronoiTextureNode()
476 : TextureNode("voronoi_texture")
477 {
478         coloring = ustring("Intensity");
479
480         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
481         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
482
483         add_output("Color", SHADER_SOCKET_COLOR);
484         add_output("Fac", SHADER_SOCKET_FLOAT);
485 }
486
487 void VoronoiTextureNode::compile(SVMCompiler& compiler)
488 {
489         ShaderInput *scale_in = input("Scale");
490         ShaderInput *vector_in = input("Vector");
491         ShaderOutput *color_out = output("Color");
492         ShaderOutput *fac_out = output("Fac");
493
494         if(vector_in->link) compiler.stack_assign(vector_in);
495         if(scale_in->link) compiler.stack_assign(scale_in);
496
497         if(!tex_mapping.skip())
498                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
499
500         compiler.stack_assign(color_out);
501         compiler.stack_assign(fac_out);
502
503         compiler.add_node(NODE_TEX_VORONOI,
504                 coloring_enum[coloring],
505                 compiler.encode_uchar4(scale_in->stack_offset, vector_in->stack_offset, fac_out->stack_offset, color_out->stack_offset),
506                 __float_as_int(scale_in->value.x));
507 }
508
509 void VoronoiTextureNode::compile(OSLCompiler& compiler)
510 {
511         compiler.parameter("Coloring", coloring);
512         compiler.add(this, "node_voronoi_texture");
513 }
514
515 /* Musgrave Texture */
516
517 static ShaderEnum musgrave_type_init()
518 {
519         ShaderEnum enm;
520
521         enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
522         enm.insert("fBM", NODE_MUSGRAVE_FBM);
523         enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
524         enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
525         enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
526
527         return enm;
528 }
529
530 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
531
532 MusgraveTextureNode::MusgraveTextureNode()
533 : TextureNode("musgrave_texture")
534 {
535         type = ustring("fBM");
536
537         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
538         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
539         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
540         add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
541         add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
542         add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
543         add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
544
545         add_output("Fac", SHADER_SOCKET_FLOAT);
546         add_output("Color", SHADER_SOCKET_COLOR);
547 }
548
549 void MusgraveTextureNode::compile(SVMCompiler& compiler)
550 {
551         ShaderInput *vector_in = input("Vector");
552         ShaderInput *scale_in = input("Scale");
553         ShaderInput *dimension_in = input("Dimension");
554         ShaderInput *lacunarity_in = input("Lacunarity");
555         ShaderInput *detail_in = input("Detail");
556         ShaderInput *offset_in = input("Offset");
557         ShaderInput *gain_in = input("Gain");
558         ShaderOutput *fac_out = output("Fac");
559         ShaderOutput *color_out = output("Color");
560
561         if(vector_in->link) compiler.stack_assign(vector_in);
562         if(dimension_in->link) compiler.stack_assign(dimension_in);
563         if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
564         if(detail_in->link) compiler.stack_assign(detail_in);
565         if(offset_in->link) compiler.stack_assign(offset_in);
566         if(gain_in->link) compiler.stack_assign(gain_in);
567         if(scale_in->link) compiler.stack_assign(scale_in);
568
569         if(!tex_mapping.skip())
570                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
571
572         if(!fac_out->links.empty())
573                 compiler.stack_assign(fac_out);
574         if(!color_out->links.empty())
575                 compiler.stack_assign(color_out);
576
577         compiler.add_node(NODE_TEX_MUSGRAVE,
578                 compiler.encode_uchar4(type_enum[type], vector_in->stack_offset, color_out->stack_offset, fac_out->stack_offset),
579                 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, detail_in->stack_offset, offset_in->stack_offset),
580                 compiler.encode_uchar4(gain_in->stack_offset, scale_in->stack_offset));
581         compiler.add_node(__float_as_int(dimension_in->value.x),
582                 __float_as_int(lacunarity_in->value.x),
583                 __float_as_int(detail_in->value.x),
584                 __float_as_int(offset_in->value.x));
585         compiler.add_node(__float_as_int(gain_in->value.x),
586                 __float_as_int(scale_in->value.x));
587 }
588
589 void MusgraveTextureNode::compile(OSLCompiler& compiler)
590 {
591         compiler.parameter("Type", type);
592
593         compiler.add(this, "node_musgrave_texture");
594 }
595
596 /* Wave Texture */
597
598 static ShaderEnum wave_type_init()
599 {
600         ShaderEnum enm;
601
602         enm.insert("Bands", NODE_WAVE_BANDS);
603         enm.insert("Rings", NODE_WAVE_RINGS);
604
605         return enm;
606 }
607
608 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
609
610 WaveTextureNode::WaveTextureNode()
611 : TextureNode("marble_texture")
612 {
613         type = ustring("Bands");
614
615         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
616         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
617         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
618         add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
619         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
620
621         add_output("Color", SHADER_SOCKET_COLOR);
622         add_output("Fac", SHADER_SOCKET_FLOAT);
623 }
624
625 void WaveTextureNode::compile(SVMCompiler& compiler)
626 {
627         ShaderInput *scale_in = input("Scale");
628         ShaderInput *distortion_in = input("Distortion");
629         ShaderInput *dscale_in = input("Detail Scale");
630         ShaderInput *detail_in = input("Detail");
631         ShaderInput *vector_in = input("Vector");
632         ShaderOutput *fac_out = output("Fac");
633         ShaderOutput *color_out = output("Color");
634
635         if(scale_in->link) compiler.stack_assign(scale_in);
636         if(detail_in->link) compiler.stack_assign(detail_in);
637         if(distortion_in->link) compiler.stack_assign(distortion_in);
638         if(dscale_in->link) compiler.stack_assign(dscale_in);
639         if(vector_in->link) compiler.stack_assign(vector_in);
640
641         if(!tex_mapping.skip())
642                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
643
644         if(!fac_out->links.empty())
645                 compiler.stack_assign(fac_out);
646         if(!color_out->links.empty())
647                 compiler.stack_assign(color_out);
648
649         compiler.add_node(NODE_TEX_WAVE,
650                 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
651                 compiler.encode_uchar4(vector_in->stack_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset));
652
653         compiler.add_node(
654                 __float_as_int(scale_in->value.x),
655                 __float_as_int(detail_in->value.x),
656                 __float_as_int(distortion_in->value.x),
657                 __float_as_int(dscale_in->value.x));
658 }
659
660 void WaveTextureNode::compile(OSLCompiler& compiler)
661 {
662         compiler.parameter("Type", type);
663
664         compiler.add(this, "node_marble_texture");
665 }
666
667 /* Magic Texture */
668
669 MagicTextureNode::MagicTextureNode()
670 : TextureNode("magic_texture")
671 {
672         depth = 2;
673
674         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
675         add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
676         add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
677
678         add_output("Color", SHADER_SOCKET_COLOR);
679         add_output("Fac", SHADER_SOCKET_FLOAT);
680 }
681
682 void MagicTextureNode::compile(SVMCompiler& compiler)
683 {
684         ShaderInput *vector_in = input("Vector");
685         ShaderInput *scale_in = input("Scale");
686         ShaderInput *distortion_in = input("Distortion");
687         ShaderOutput *color_out = output("Color");
688         ShaderOutput *fac_out = output("Fac");
689
690         if(vector_in->link) compiler.stack_assign(vector_in);
691         if(distortion_in->link) compiler.stack_assign(distortion_in);
692         if(scale_in->link) compiler.stack_assign(scale_in);
693
694         if(!tex_mapping.skip())
695                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
696
697         if(!fac_out->links.empty())
698                 compiler.stack_assign(fac_out);
699         if(!color_out->links.empty())
700                 compiler.stack_assign(color_out);
701
702         compiler.add_node(NODE_TEX_MAGIC,
703                 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
704                 compiler.encode_uchar4(vector_in->stack_offset, scale_in->stack_offset, distortion_in->stack_offset));
705         compiler.add_node(
706                 __float_as_int(scale_in->value.x),
707                 __float_as_int(distortion_in->value.x));
708 }
709
710 void MagicTextureNode::compile(OSLCompiler& compiler)
711 {
712         compiler.parameter("Depth", depth);
713         compiler.add(this, "node_magic_texture");
714 }
715
716 /* Mapping */
717
718 MappingNode::MappingNode()
719 : ShaderNode("mapping")
720 {
721         add_input("Vector", SHADER_SOCKET_POINT);
722         add_output("Vector", SHADER_SOCKET_POINT);
723 }
724
725 void MappingNode::compile(SVMCompiler& compiler)
726 {
727         ShaderInput *vector_in = input("Vector");
728         ShaderOutput *vector_out = output("Vector");
729
730         compiler.stack_assign(vector_in);
731         compiler.stack_assign(vector_out);
732
733         tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
734 }
735
736 void MappingNode::compile(OSLCompiler& compiler)
737 {
738         Transform tfm = transform_transpose(tex_mapping.compute_transform());
739         compiler.parameter("Matrix", tfm);
740
741         compiler.add(this, "node_mapping");
742 }
743
744 /* Convert */
745
746 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_)
747 : ShaderNode("convert")
748 {
749         from = from_;
750         to = to_;
751
752         assert(from != to);
753
754         if(from == SHADER_SOCKET_FLOAT)
755                 add_input("Val", SHADER_SOCKET_FLOAT);
756         else if(from == SHADER_SOCKET_COLOR)
757                 add_input("Color", SHADER_SOCKET_COLOR);
758         else if(from == SHADER_SOCKET_VECTOR)
759                 add_input("Vector", SHADER_SOCKET_VECTOR);
760         else if(from == SHADER_SOCKET_POINT)
761                 add_input("Point", SHADER_SOCKET_POINT);
762         else if(from == SHADER_SOCKET_NORMAL)
763                 add_input("Normal", SHADER_SOCKET_NORMAL);
764         else
765                 assert(0);
766
767         if(to == SHADER_SOCKET_FLOAT)
768                 add_output("Val", SHADER_SOCKET_FLOAT);
769         else if(to == SHADER_SOCKET_COLOR)
770                 add_output("Color", SHADER_SOCKET_COLOR);
771         else if(to == SHADER_SOCKET_VECTOR)
772                 add_output("Vector", SHADER_SOCKET_VECTOR);
773         else if(to == SHADER_SOCKET_POINT)
774                 add_output("Point", SHADER_SOCKET_POINT);
775         else if(to == SHADER_SOCKET_NORMAL)
776                 add_output("Normal", SHADER_SOCKET_NORMAL);
777         else
778                 assert(0);
779 }
780
781 void ConvertNode::compile(SVMCompiler& compiler)
782 {
783         ShaderInput *in = inputs[0];
784         ShaderOutput *out = outputs[0];
785
786         if(to == SHADER_SOCKET_FLOAT) {
787                 compiler.stack_assign(in);
788                 compiler.stack_assign(out);
789
790                 if(from == SHADER_SOCKET_COLOR)
791                         /* color to float */
792                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
793                 else
794                         /* vector/point/normal to float */
795                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
796         }
797         else if(from == SHADER_SOCKET_FLOAT) {
798                 compiler.stack_assign(in);
799                 compiler.stack_assign(out);
800
801                 /* float to float3 */
802                 compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
803         }
804         else {
805                 /* float3 to float3 */
806                 if(in->link) {
807                         /* no op in SVM */
808                         compiler.stack_link(in, out);
809                 }
810                 else {
811                         /* set 0,0,0 value */
812                         compiler.stack_assign(in);
813                         compiler.stack_assign(out);
814
815                         compiler.add_node(NODE_VALUE_V, in->stack_offset);
816                         compiler.add_node(NODE_VALUE_V, in->value);
817                 }
818         }
819 }
820
821 void ConvertNode::compile(OSLCompiler& compiler)
822 {
823         if(from == SHADER_SOCKET_FLOAT)
824                 compiler.add(this, "node_convert_from_float");
825         else if(from == SHADER_SOCKET_COLOR)
826                 compiler.add(this, "node_convert_from_color");
827         else if(from == SHADER_SOCKET_VECTOR)
828                 compiler.add(this, "node_convert_from_vector");
829         else if(from == SHADER_SOCKET_POINT)
830                 compiler.add(this, "node_convert_from_point");
831         else if(from == SHADER_SOCKET_NORMAL)
832                 compiler.add(this, "node_convert_from_normal");
833         else
834                 assert(0);
835 }
836
837 /* BSDF Closure */
838
839 BsdfNode::BsdfNode()
840 : ShaderNode("bsdf")
841 {
842         closure = ccl::CLOSURE_BSDF_DIFFUSE_ID;
843
844         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
845         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
846
847         add_output("BSDF", SHADER_SOCKET_CLOSURE);
848 }
849
850 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
851 {
852         ShaderInput *color_in = input("Color");
853
854         if(color_in->link) {
855                 compiler.stack_assign(color_in);
856                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
857         }
858         else
859                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
860         
861         if(param1)
862                 compiler.stack_assign(param1);
863         if(param2)
864                 compiler.stack_assign(param2);
865
866         compiler.add_node(NODE_CLOSURE_BSDF,
867                 compiler.encode_uchar4(closure,
868                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
869                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
870                         compiler.closure_mix_weight_offset()),
871                 __float_as_int((param1)? param1->value.x: 0.0f),
872                 __float_as_int((param2)? param2->value.x: 0.0f));
873 }
874
875 void BsdfNode::compile(SVMCompiler& compiler)
876 {
877         compile(compiler, NULL, NULL);
878 }
879
880 void BsdfNode::compile(OSLCompiler& compiler)
881 {
882         assert(0);
883 }
884
885 /* Ward BSDF Closure */
886
887 WardBsdfNode::WardBsdfNode()
888 {
889         closure = CLOSURE_BSDF_WARD_ID;
890
891         add_input("Roughness U", SHADER_SOCKET_FLOAT, 0.2f);
892         add_input("Roughness V", SHADER_SOCKET_FLOAT, 0.2f);
893 }
894
895 void WardBsdfNode::compile(SVMCompiler& compiler)
896 {
897         BsdfNode::compile(compiler, input("Roughness U"), input("Roughness V"));
898 }
899
900 void WardBsdfNode::compile(OSLCompiler& compiler)
901 {
902         compiler.add(this, "node_ward_bsdf");
903 }
904
905 /* Glossy BSDF Closure */
906
907 static ShaderEnum glossy_distribution_init()
908 {
909         ShaderEnum enm;
910
911         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
912         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
913         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
914
915         return enm;
916 }
917
918 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
919
920 GlossyBsdfNode::GlossyBsdfNode()
921 {
922         distribution = ustring("Beckmann");
923
924         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
925 }
926
927 void GlossyBsdfNode::compile(SVMCompiler& compiler)
928 {
929         closure = (ClosureType)distribution_enum[distribution];
930
931         if(closure == CLOSURE_BSDF_REFLECTION_ID)
932                 BsdfNode::compile(compiler, NULL, NULL);
933         else
934                 BsdfNode::compile(compiler, input("Roughness"), NULL);
935 }
936
937 void GlossyBsdfNode::compile(OSLCompiler& compiler)
938 {
939         compiler.parameter("distribution", distribution);
940         compiler.add(this, "node_glossy_bsdf");
941 }
942
943 /* Glass BSDF Closure */
944
945 static ShaderEnum glass_distribution_init()
946 {
947         ShaderEnum enm;
948
949         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
950         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
951         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
952
953         return enm;
954 }
955
956 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
957
958 GlassBsdfNode::GlassBsdfNode()
959 {
960         distribution = ustring("Sharp");
961
962         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
963         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
964 }
965
966 void GlassBsdfNode::compile(SVMCompiler& compiler)
967 {
968         closure = (ClosureType)distribution_enum[distribution];
969
970         if(closure == CLOSURE_BSDF_REFRACTION_ID)
971                 BsdfNode::compile(compiler, NULL, input("IOR"));
972         else
973                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
974 }
975
976 void GlassBsdfNode::compile(OSLCompiler& compiler)
977 {
978         compiler.parameter("distribution", distribution);
979         compiler.add(this, "node_glass_bsdf");
980 }
981
982 /* Velvet BSDF Closure */
983
984 VelvetBsdfNode::VelvetBsdfNode()
985 {
986         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
987
988         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
989 }
990
991 void VelvetBsdfNode::compile(SVMCompiler& compiler)
992 {
993         BsdfNode::compile(compiler, input("Sigma"), NULL);
994 }
995
996 void VelvetBsdfNode::compile(OSLCompiler& compiler)
997 {
998         compiler.add(this, "node_velvet_bsdf");
999 }
1000
1001 /* Diffuse BSDF Closure */
1002
1003 DiffuseBsdfNode::DiffuseBsdfNode()
1004 {
1005         closure = CLOSURE_BSDF_DIFFUSE_ID;
1006         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1007 }
1008
1009 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1010 {
1011         BsdfNode::compile(compiler, input("Roughness"), NULL);
1012 }
1013
1014 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1015 {
1016         compiler.add(this, "node_diffuse_bsdf");
1017 }
1018
1019 /* Translucent BSDF Closure */
1020
1021 TranslucentBsdfNode::TranslucentBsdfNode()
1022 {
1023         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1024 }
1025
1026 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1027 {
1028         BsdfNode::compile(compiler, NULL, NULL);
1029 }
1030
1031 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1032 {
1033         compiler.add(this, "node_translucent_bsdf");
1034 }
1035
1036 /* Transparent BSDF Closure */
1037
1038 TransparentBsdfNode::TransparentBsdfNode()
1039 {
1040         name = "transparent";
1041         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1042 }
1043
1044 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1045 {
1046         BsdfNode::compile(compiler, NULL, NULL);
1047 }
1048
1049 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1050 {
1051         compiler.add(this, "node_transparent_bsdf");
1052 }
1053
1054 /* Emissive Closure */
1055
1056 EmissionNode::EmissionNode()
1057 : ShaderNode("emission")
1058 {
1059         total_power = false;
1060
1061         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1062         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1063         add_output("Emission", SHADER_SOCKET_CLOSURE);
1064 }
1065
1066 void EmissionNode::compile(SVMCompiler& compiler)
1067 {
1068         ShaderInput *color_in = input("Color");
1069         ShaderInput *strength_in = input("Strength");
1070
1071         if(color_in->link || strength_in->link) {
1072                 compiler.stack_assign(color_in);
1073                 compiler.stack_assign(strength_in);
1074                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1075         }
1076         else if(total_power)
1077                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1078         else
1079                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1080
1081         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1082 }
1083
1084 void EmissionNode::compile(OSLCompiler& compiler)
1085 {
1086         compiler.parameter("TotalPower", (total_power)? 1: 0);
1087         compiler.add(this, "node_emission");
1088 }
1089
1090 /* Background Closure */
1091
1092 BackgroundNode::BackgroundNode()
1093 : ShaderNode("background")
1094 {
1095         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1096         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1097         add_output("Background", SHADER_SOCKET_CLOSURE);
1098 }
1099
1100 void BackgroundNode::compile(SVMCompiler& compiler)
1101 {
1102         ShaderInput *color_in = input("Color");
1103         ShaderInput *strength_in = input("Strength");
1104
1105         if(color_in->link || strength_in->link) {
1106                 compiler.stack_assign(color_in);
1107                 compiler.stack_assign(strength_in);
1108                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1109         }
1110         else
1111                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1112
1113         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1114 }
1115
1116 void BackgroundNode::compile(OSLCompiler& compiler)
1117 {
1118         compiler.add(this, "node_background");
1119 }
1120
1121 /* Holdout Closure */
1122
1123 HoldoutNode::HoldoutNode()
1124 : ShaderNode("holdout")
1125 {
1126         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1127 }
1128
1129 void HoldoutNode::compile(SVMCompiler& compiler)
1130 {
1131         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1132 }
1133
1134 void HoldoutNode::compile(OSLCompiler& compiler)
1135 {
1136         compiler.add(this, "node_holdout");
1137 }
1138
1139 /* Volume Closure */
1140
1141 VolumeNode::VolumeNode()
1142 : ShaderNode("volume")
1143 {
1144         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1145
1146         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1147         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1148
1149         add_output("Volume", SHADER_SOCKET_CLOSURE);
1150 }
1151
1152 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1153 {
1154         ShaderInput *color_in = input("Color");
1155
1156         if(color_in->link) {
1157                 compiler.stack_assign(color_in);
1158                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1159         }
1160         else
1161                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1162         
1163         if(param1)
1164                 compiler.stack_assign(param1);
1165         if(param2)
1166                 compiler.stack_assign(param2);
1167
1168         compiler.add_node(NODE_CLOSURE_VOLUME,
1169                 compiler.encode_uchar4(closure,
1170                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1171                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1172                         compiler.closure_mix_weight_offset()),
1173                 __float_as_int((param1)? param1->value.x: 0.0f),
1174                 __float_as_int((param2)? param2->value.x: 0.0f));
1175 }
1176
1177 void VolumeNode::compile(SVMCompiler& compiler)
1178 {
1179         compile(compiler, NULL, NULL);
1180 }
1181
1182 void VolumeNode::compile(OSLCompiler& compiler)
1183 {
1184         assert(0);
1185 }
1186
1187 /* Transparent Volume Closure */
1188
1189 TransparentVolumeNode::TransparentVolumeNode()
1190 {
1191         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1192 }
1193
1194 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1195 {
1196         VolumeNode::compile(compiler, input("Density"), NULL);
1197 }
1198
1199 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1200 {
1201         compiler.add(this, "node_isotropic_volume");
1202 }
1203
1204 /* Isotropic Volume Closure */
1205
1206 IsotropicVolumeNode::IsotropicVolumeNode()
1207 {
1208         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1209 }
1210
1211 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1212 {
1213         VolumeNode::compile(compiler, input("Density"), NULL);
1214 }
1215
1216 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1217 {
1218         compiler.add(this, "node_isotropic_volume");
1219 }
1220
1221 /* Geometry */
1222
1223 GeometryNode::GeometryNode()
1224 : ShaderNode("geometry")
1225 {
1226         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1227         add_output("Position", SHADER_SOCKET_POINT);
1228         add_output("Normal", SHADER_SOCKET_NORMAL);
1229         add_output("Tangent", SHADER_SOCKET_NORMAL);
1230         add_output("True Normal", SHADER_SOCKET_NORMAL);
1231         add_output("Incoming", SHADER_SOCKET_VECTOR);
1232         add_output("Parametric", SHADER_SOCKET_POINT);
1233         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1234 }
1235
1236 void GeometryNode::compile(SVMCompiler& compiler)
1237 {
1238         ShaderOutput *out;
1239         NodeType geom_node = NODE_GEOMETRY;
1240
1241         if(bump == SHADER_BUMP_DX)
1242                 geom_node = NODE_GEOMETRY_BUMP_DX;
1243         else if(bump == SHADER_BUMP_DY)
1244                 geom_node = NODE_GEOMETRY_BUMP_DY;
1245         
1246         out = output("Position");
1247         if(!out->links.empty()) {
1248                 compiler.stack_assign(out);
1249                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1250         }
1251
1252         out = output("Normal");
1253         if(!out->links.empty()) {
1254                 compiler.stack_assign(out);
1255                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1256         }
1257
1258         out = output("Tangent");
1259         if(!out->links.empty()) {
1260                 compiler.stack_assign(out);
1261                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1262         }
1263
1264         out = output("True Normal");
1265         if(!out->links.empty()) {
1266                 compiler.stack_assign(out);
1267                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1268         }
1269
1270         out = output("Incoming");
1271         if(!out->links.empty()) {
1272                 compiler.stack_assign(out);
1273                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1274         }
1275
1276         out = output("Parametric");
1277         if(!out->links.empty()) {
1278                 compiler.stack_assign(out);
1279                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1280         }
1281
1282         out = output("Backfacing");
1283         if(!out->links.empty()) {
1284                 compiler.stack_assign(out);
1285                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1286         }
1287 }
1288
1289 void GeometryNode::compile(OSLCompiler& compiler)
1290 {
1291         if(bump == SHADER_BUMP_DX)
1292                 compiler.parameter("bump_offset", "dx");
1293         else if(bump == SHADER_BUMP_DY)
1294                 compiler.parameter("bump_offset", "dy");
1295         else
1296                 compiler.parameter("bump_offset", "center");
1297
1298         compiler.add(this, "node_geometry");
1299 }
1300
1301 /* TextureCoordinate */
1302
1303 TextureCoordinateNode::TextureCoordinateNode()
1304 : ShaderNode("texture_coordinate")
1305 {
1306         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1307         add_output("Generated", SHADER_SOCKET_POINT);
1308         add_output("UV", SHADER_SOCKET_POINT);
1309         add_output("Object", SHADER_SOCKET_POINT);
1310         add_output("Camera", SHADER_SOCKET_POINT);
1311         add_output("Window", SHADER_SOCKET_POINT);
1312         add_output("Reflection", SHADER_SOCKET_NORMAL);
1313 }
1314
1315 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1316 {
1317         if(!output("Generated")->links.empty())
1318                 attributes->add(Attribute::STD_GENERATED);
1319         if(!output("UV")->links.empty())
1320                 attributes->add(Attribute::STD_UV);
1321
1322         ShaderNode::attributes(attributes);
1323 }
1324
1325 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1326 {
1327         ShaderOutput *out;
1328         NodeType texco_node = NODE_TEX_COORD;
1329         NodeType attr_node = NODE_ATTR;
1330         NodeType geom_node = NODE_GEOMETRY;
1331
1332         if(bump == SHADER_BUMP_DX) {
1333                 texco_node = NODE_TEX_COORD_BUMP_DX;
1334                 attr_node = NODE_ATTR_BUMP_DX;
1335                 geom_node = NODE_GEOMETRY_BUMP_DX;
1336         }
1337         else if(bump == SHADER_BUMP_DY) {
1338                 texco_node = NODE_TEX_COORD_BUMP_DY;
1339                 attr_node = NODE_ATTR_BUMP_DY;
1340                 geom_node = NODE_GEOMETRY_BUMP_DY;
1341         }
1342         
1343         out = output("Generated");
1344         if(!out->links.empty()) {
1345                 if(compiler.background) {
1346                         compiler.stack_assign(out);
1347                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1348                 }
1349                 else {
1350                         int attr = compiler.attribute(Attribute::STD_GENERATED);
1351                         compiler.stack_assign(out);
1352                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1353                 }
1354         }
1355
1356         out = output("UV");
1357         if(!out->links.empty()) {
1358                 int attr = compiler.attribute(Attribute::STD_UV);
1359                 compiler.stack_assign(out);
1360                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1361         }
1362
1363         out = output("Object");
1364         if(!out->links.empty()) {
1365                 compiler.stack_assign(out);
1366                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
1367         }
1368
1369         out = output("Camera");
1370         if(!out->links.empty()) {
1371                 compiler.stack_assign(out);
1372                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
1373         }
1374
1375         out = output("Window");
1376         if(!out->links.empty()) {
1377                 compiler.stack_assign(out);
1378                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
1379         }
1380
1381         out = output("Reflection");
1382         if(!out->links.empty()) {
1383                 if(compiler.background) {
1384                         compiler.stack_assign(out);
1385                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1386                 }
1387                 else {
1388                         compiler.stack_assign(out);
1389                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
1390                 }
1391         }
1392 }
1393
1394 void TextureCoordinateNode::compile(OSLCompiler& compiler)
1395 {
1396         if(bump == SHADER_BUMP_DX)
1397                 compiler.parameter("bump_offset", "dx");
1398         else if(bump == SHADER_BUMP_DY)
1399                 compiler.parameter("bump_offset", "dy");
1400         else
1401                 compiler.parameter("bump_offset", "center");
1402         
1403         if(compiler.background)
1404                 compiler.parameter("is_background", true);
1405
1406         compiler.add(this, "node_texture_coordinate");
1407 }
1408
1409 /* Light Path */
1410
1411 LightPathNode::LightPathNode()
1412 : ShaderNode("light_path")
1413 {
1414         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
1415         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
1416         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
1417         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
1418         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
1419         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
1420         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
1421 }
1422
1423 void LightPathNode::compile(SVMCompiler& compiler)
1424 {
1425         ShaderOutput *out;
1426
1427         out = output("Is Camera Ray");
1428         if(!out->links.empty()) {
1429                 compiler.stack_assign(out);
1430                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
1431         }
1432
1433         out = output("Is Shadow Ray");
1434         if(!out->links.empty()) {
1435                 compiler.stack_assign(out);
1436                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
1437         }
1438
1439         out = output("Is Diffuse Ray");
1440         if(!out->links.empty()) {
1441                 compiler.stack_assign(out);
1442                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
1443         }
1444
1445         out = output("Is Glossy Ray");
1446         if(!out->links.empty()) {
1447                 compiler.stack_assign(out);
1448                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
1449         }
1450
1451         out = output("Is Singular Ray");
1452         if(!out->links.empty()) {
1453                 compiler.stack_assign(out);
1454                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
1455         }
1456
1457         out = output("Is Reflection Ray");
1458         if(!out->links.empty()) {
1459                 compiler.stack_assign(out);
1460                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
1461         }
1462
1463
1464         out = output("Is Transmission Ray");
1465         if(!out->links.empty()) {
1466                 compiler.stack_assign(out);
1467                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
1468         }
1469 }
1470
1471 void LightPathNode::compile(OSLCompiler& compiler)
1472 {
1473         compiler.add(this, "node_light_path");
1474 }
1475
1476 /* Value */
1477
1478 ValueNode::ValueNode()
1479 : ShaderNode("value")
1480 {
1481         value = 0.0f;
1482
1483         add_output("Value", SHADER_SOCKET_FLOAT);
1484 }
1485
1486 void ValueNode::compile(SVMCompiler& compiler)
1487 {
1488         ShaderOutput *val_out = output("Value");
1489
1490         compiler.stack_assign(val_out);
1491         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
1492 }
1493
1494 void ValueNode::compile(OSLCompiler& compiler)
1495 {
1496         compiler.parameter("value_value", value);
1497         compiler.add(this, "node_value");
1498 }
1499
1500 /* Color */
1501
1502 ColorNode::ColorNode()
1503 : ShaderNode("color")
1504 {
1505         value = make_float3(0.0f, 0.0f, 0.0f);
1506
1507         add_output("Color", SHADER_SOCKET_COLOR);
1508 }
1509
1510 void ColorNode::compile(SVMCompiler& compiler)
1511 {
1512         ShaderOutput *color_out = output("Color");
1513
1514         if(color_out && !color_out->links.empty()) {
1515                 compiler.stack_assign(color_out);
1516                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
1517                 compiler.add_node(NODE_VALUE_V, value);
1518         }
1519 }
1520
1521 void ColorNode::compile(OSLCompiler& compiler)
1522 {
1523         compiler.parameter_color("color_value", value);
1524
1525         compiler.add(this, "node_value");
1526 }
1527
1528 /* Add Closure */
1529
1530 AddClosureNode::AddClosureNode()
1531 : ShaderNode("add_closure")
1532 {
1533         add_input("Closure1", SHADER_SOCKET_CLOSURE);
1534         add_input("Closure2", SHADER_SOCKET_CLOSURE);
1535         add_output("Closure",  SHADER_SOCKET_CLOSURE);
1536 }
1537
1538 void AddClosureNode::compile(SVMCompiler& compiler)
1539 {
1540         /* handled in the SVM compiler */
1541 }
1542
1543 void AddClosureNode::compile(OSLCompiler& compiler)
1544 {
1545         compiler.add(this, "node_add_closure");
1546 }
1547
1548 /* Mix Closure */
1549
1550 MixClosureNode::MixClosureNode()
1551 : ShaderNode("mix_closure")
1552 {
1553         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1554         add_input("Closure1", SHADER_SOCKET_CLOSURE);
1555         add_input("Closure2", SHADER_SOCKET_CLOSURE);
1556         add_output("Closure",  SHADER_SOCKET_CLOSURE);
1557 }
1558
1559 void MixClosureNode::compile(SVMCompiler& compiler)
1560 {
1561         /* handled in the SVM compiler */
1562 }
1563
1564 void MixClosureNode::compile(OSLCompiler& compiler)
1565 {
1566         compiler.add(this, "node_mix_closure");
1567 }
1568
1569 /* Mix */
1570
1571 MixNode::MixNode()
1572 : ShaderNode("mix")
1573 {
1574         type = ustring("Mix");
1575
1576         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1577         add_input("Color1", SHADER_SOCKET_COLOR);
1578         add_input("Color2", SHADER_SOCKET_COLOR);
1579         add_output("Color",  SHADER_SOCKET_COLOR);
1580 }
1581
1582 static ShaderEnum mix_type_init()
1583 {
1584         ShaderEnum enm;
1585
1586         enm.insert("Mix", NODE_MIX_BLEND);
1587         enm.insert("Add", NODE_MIX_ADD);
1588         enm.insert("Multiply", NODE_MIX_MUL);
1589         enm.insert("Screen", NODE_MIX_SCREEN);
1590         enm.insert("Overlay", NODE_MIX_OVERLAY);
1591         enm.insert("Subtract", NODE_MIX_SUB);
1592         enm.insert("Divide", NODE_MIX_DIV);
1593         enm.insert("Difference", NODE_MIX_DIFF);
1594         enm.insert("Darken", NODE_MIX_DARK);
1595         enm.insert("Lighten", NODE_MIX_LIGHT);
1596         enm.insert("Dodge", NODE_MIX_DODGE);
1597         enm.insert("Burn", NODE_MIX_BURN);
1598         enm.insert("Hue", NODE_MIX_HUE);
1599         enm.insert("Saturation", NODE_MIX_SAT);
1600         enm.insert("Value", NODE_MIX_VAL );
1601         enm.insert("Color", NODE_MIX_COLOR);
1602         enm.insert("Soft Light", NODE_MIX_SOFT);
1603         enm.insert("Linear Light", NODE_MIX_LINEAR);
1604
1605         return enm;
1606 }
1607
1608 ShaderEnum MixNode::type_enum = mix_type_init();
1609
1610 void MixNode::compile(SVMCompiler& compiler)
1611 {
1612         ShaderInput *fac_in = input("Fac");
1613         ShaderInput *color1_in = input("Color1");
1614         ShaderInput *color2_in = input("Color2");
1615         ShaderOutput *color_out = output("Color");
1616
1617         compiler.stack_assign(fac_in);
1618         compiler.stack_assign(color1_in);
1619         compiler.stack_assign(color2_in);
1620         compiler.stack_assign(color_out);
1621
1622         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
1623         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
1624 }
1625
1626 void MixNode::compile(OSLCompiler& compiler)
1627 {
1628         compiler.parameter("type", type);
1629         compiler.add(this, "node_mix");
1630 }
1631
1632 /* Combine RGB */
1633 CombineRGBNode::CombineRGBNode()
1634 : ShaderNode("combine_rgb")
1635 {
1636         add_input("R", SHADER_SOCKET_FLOAT);
1637         add_input("G", SHADER_SOCKET_FLOAT);
1638         add_input("B", SHADER_SOCKET_FLOAT);
1639         add_output("Image", SHADER_SOCKET_COLOR);
1640 }
1641
1642 void CombineRGBNode::compile(SVMCompiler& compiler)
1643 {
1644         ShaderInput *red_in = input("R");
1645         ShaderInput *green_in = input("G");
1646         ShaderInput *blue_in = input("B");
1647         ShaderOutput *color_out = output("Image");
1648
1649         compiler.stack_assign(color_out);
1650
1651         compiler.stack_assign(red_in);
1652         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
1653
1654         compiler.stack_assign(green_in);
1655         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
1656
1657         compiler.stack_assign(blue_in);
1658         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
1659 }
1660
1661 void CombineRGBNode::compile(OSLCompiler& compiler)
1662 {
1663         compiler.add(this, "node_combine_rgb");
1664 }
1665
1666 /* Separate RGB */
1667 SeparateRGBNode::SeparateRGBNode()
1668 : ShaderNode("separate_rgb")
1669 {
1670         add_input("Image", SHADER_SOCKET_COLOR);
1671         add_output("R", SHADER_SOCKET_FLOAT);
1672         add_output("G", SHADER_SOCKET_FLOAT);
1673         add_output("B", SHADER_SOCKET_FLOAT);
1674 }
1675
1676 void SeparateRGBNode::compile(SVMCompiler& compiler)
1677 {
1678         ShaderInput *color_in = input("Image");
1679         ShaderOutput *red_out = output("R");
1680         ShaderOutput *green_out = output("G");
1681         ShaderOutput *blue_out = output("B");
1682
1683         compiler.stack_assign(color_in);
1684
1685         compiler.stack_assign(red_out);
1686         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
1687
1688         compiler.stack_assign(green_out);
1689         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
1690
1691         compiler.stack_assign(blue_out);
1692         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
1693 }
1694
1695 void SeparateRGBNode::compile(OSLCompiler& compiler)
1696 {
1697         compiler.add(this, "node_separate_rgb");
1698 }
1699
1700 /* Separate RGB */
1701 HSVNode::HSVNode()
1702 : ShaderNode("hsv")
1703 {
1704         add_input("Hue", SHADER_SOCKET_FLOAT);
1705         add_input("Saturation", SHADER_SOCKET_FLOAT);
1706         add_input("Value", SHADER_SOCKET_FLOAT);
1707         add_input("Fac", SHADER_SOCKET_FLOAT);
1708         add_input("Color", SHADER_SOCKET_COLOR);
1709         add_output("Color", SHADER_SOCKET_COLOR);
1710 }
1711
1712 void HSVNode::compile(SVMCompiler& compiler)
1713 {
1714         ShaderInput *hue_in = input("Hue");
1715         ShaderInput *saturation_in = input("Saturation");
1716         ShaderInput *value_in = input("Value");
1717         ShaderInput *fac_in = input("Fac");
1718         ShaderInput *color_in = input("Color");
1719         ShaderOutput *color_out = output("Color");
1720
1721         compiler.stack_assign(hue_in);
1722         compiler.stack_assign(saturation_in);
1723         compiler.stack_assign(value_in);
1724         compiler.stack_assign(fac_in);
1725         compiler.stack_assign(color_in);
1726         compiler.stack_assign(color_out);
1727
1728         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
1729         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
1730 }
1731
1732 void HSVNode::compile(OSLCompiler& compiler)
1733 {
1734         compiler.add(this, "node_hsv");
1735 }
1736
1737 /* Attribute */
1738
1739 AttributeNode::AttributeNode()
1740 : ShaderNode("attribute")
1741 {
1742         attribute = "";
1743
1744         add_output("Color",  SHADER_SOCKET_COLOR);
1745         add_output("Vector",  SHADER_SOCKET_VECTOR);
1746         add_output("Fac",  SHADER_SOCKET_FLOAT);
1747 }
1748
1749 void AttributeNode::attributes(AttributeRequestSet *attributes)
1750 {
1751         ShaderOutput *color_out = output("Color");
1752         ShaderOutput *vector_out = output("Vector");
1753         ShaderOutput *fac_out = output("Fac");
1754
1755         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
1756                 attributes->add(attribute);
1757         
1758         ShaderNode::attributes(attributes);
1759 }
1760
1761 void AttributeNode::compile(SVMCompiler& compiler)
1762 {
1763         ShaderOutput *color_out = output("Color");
1764         ShaderOutput *vector_out = output("Vector");
1765         ShaderOutput *fac_out = output("Fac");
1766         NodeType attr_node = NODE_ATTR;
1767
1768         if(bump == SHADER_BUMP_DX)
1769                 attr_node = NODE_ATTR_BUMP_DX;
1770         else if(bump == SHADER_BUMP_DY)
1771                 attr_node = NODE_ATTR_BUMP_DY;
1772
1773         if(!color_out->links.empty() || !vector_out->links.empty()) {
1774                 int attr = compiler.attribute(attribute);
1775
1776                 if(!color_out->links.empty()) {
1777                         compiler.stack_assign(color_out);
1778                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
1779                 }
1780                 if(!vector_out->links.empty()) {
1781                         compiler.stack_assign(vector_out);
1782                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
1783                 }
1784         }
1785
1786         if(!fac_out->links.empty()) {
1787                 int attr = compiler.attribute(attribute);
1788
1789                 compiler.stack_assign(fac_out);
1790                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
1791         }
1792 }
1793
1794 void AttributeNode::compile(OSLCompiler& compiler)
1795 {
1796         if(bump == SHADER_BUMP_DX)
1797                 compiler.parameter("bump_offset", "dx");
1798         else if(bump == SHADER_BUMP_DY)
1799                 compiler.parameter("bump_offset", "dy");
1800         else
1801                 compiler.parameter("bump_offset", "center");
1802
1803         compiler.parameter("name", attribute.c_str());
1804         compiler.add(this, "node_attribute");
1805 }
1806
1807 /* Camera */
1808
1809 CameraNode::CameraNode()
1810 : ShaderNode("camera")
1811 {
1812         add_output("View Vector",  SHADER_SOCKET_VECTOR);
1813         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
1814         add_output("View Distance",  SHADER_SOCKET_FLOAT);
1815 }
1816
1817 void CameraNode::compile(SVMCompiler& compiler)
1818 {
1819         ShaderOutput *vector_out = output("View Vector");
1820         ShaderOutput *z_depth_out = output("View Z Depth");
1821         ShaderOutput *distance_out = output("View Distance");
1822
1823         compiler.stack_assign(vector_out);
1824         compiler.stack_assign(z_depth_out);
1825         compiler.stack_assign(distance_out);
1826         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
1827 }
1828
1829 void CameraNode::compile(OSLCompiler& compiler)
1830 {
1831         compiler.add(this, "node_camera");
1832 }
1833
1834 /* Fresnel */
1835
1836 FresnelNode::FresnelNode()
1837 : ShaderNode("Fresnel")
1838 {
1839         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1840         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
1841         add_output("Fac", SHADER_SOCKET_FLOAT);
1842 }
1843
1844 void FresnelNode::compile(SVMCompiler& compiler)
1845 {
1846         ShaderInput *ior_in = input("IOR");
1847         ShaderOutput *fac_out = output("Fac");
1848
1849         compiler.stack_assign(ior_in);
1850         compiler.stack_assign(fac_out);
1851         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
1852 }
1853
1854 void FresnelNode::compile(OSLCompiler& compiler)
1855 {
1856         compiler.add(this, "node_fresnel");
1857 }
1858
1859 /* Blend Weight */
1860
1861 LayerWeightNode::LayerWeightNode()
1862 : ShaderNode("LayerWeight")
1863 {
1864         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1865         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
1866
1867         add_output("Fresnel", SHADER_SOCKET_FLOAT);
1868         add_output("Facing", SHADER_SOCKET_FLOAT);
1869 }
1870
1871 void LayerWeightNode::compile(SVMCompiler& compiler)
1872 {
1873         ShaderInput *blend_in = input("Blend");
1874
1875         if(blend_in->link)
1876                 compiler.stack_assign(blend_in);
1877
1878         ShaderOutput *fresnel_out = output("Fresnel");
1879         if(!fresnel_out->links.empty()) {
1880                 compiler.stack_assign(fresnel_out);
1881                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
1882                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
1883         }
1884
1885         ShaderOutput *facing_out = output("Facing");
1886         if(!facing_out->links.empty()) {
1887                 compiler.stack_assign(facing_out);
1888                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
1889                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
1890         }
1891 }
1892
1893 void LayerWeightNode::compile(OSLCompiler& compiler)
1894 {
1895         compiler.add(this, "node_layer_height");
1896 }
1897
1898 /* Output */
1899
1900 OutputNode::OutputNode()
1901 : ShaderNode("output")
1902 {
1903         add_input("Surface", SHADER_SOCKET_CLOSURE);
1904         add_input("Volume", SHADER_SOCKET_CLOSURE);
1905         add_input("Displacement", SHADER_SOCKET_FLOAT);
1906 }
1907
1908 void OutputNode::compile(SVMCompiler& compiler)
1909 {
1910         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
1911                 ShaderInput *displacement_in = input("Displacement");
1912
1913                 if(displacement_in->link) {
1914                         compiler.stack_assign(displacement_in);
1915                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
1916                 }
1917         }
1918 }
1919
1920 void OutputNode::compile(OSLCompiler& compiler)
1921 {
1922         if(compiler.output_type() == SHADER_TYPE_SURFACE)
1923                 compiler.add(this, "node_output_surface");
1924         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
1925                 compiler.add(this, "node_output_volume");
1926         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
1927                 compiler.add(this, "node_output_displacement");
1928 }
1929
1930 /* Math */
1931
1932 MathNode::MathNode()
1933 : ShaderNode("math")
1934 {
1935         type = ustring("Add");
1936
1937         add_input("Value1", SHADER_SOCKET_FLOAT);
1938         add_input("Value2", SHADER_SOCKET_FLOAT);
1939         add_output("Value",  SHADER_SOCKET_FLOAT);
1940 }
1941
1942 static ShaderEnum math_type_init()
1943 {
1944         ShaderEnum enm;
1945
1946         enm.insert("Add", NODE_MATH_ADD);
1947         enm.insert("Subtract", NODE_MATH_SUBTRACT);
1948         enm.insert("Multiply", NODE_MATH_MULTIPLY);
1949         enm.insert("Divide", NODE_MATH_DIVIDE);
1950         enm.insert("Sine", NODE_MATH_SINE);
1951         enm.insert("Cosine", NODE_MATH_COSINE);
1952         enm.insert("Tangent", NODE_MATH_TANGENT);
1953         enm.insert("Arcsine", NODE_MATH_ARCSINE);
1954         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
1955         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
1956         enm.insert("Power", NODE_MATH_POWER);
1957         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
1958         enm.insert("Minimum", NODE_MATH_MINIMUM);
1959         enm.insert("Maximum", NODE_MATH_MAXIMUM);
1960         enm.insert("Round", NODE_MATH_ROUND);
1961         enm.insert("Less Than", NODE_MATH_LESS_THAN);
1962         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
1963
1964         return enm;
1965 }
1966
1967 ShaderEnum MathNode::type_enum = math_type_init();
1968
1969 void MathNode::compile(SVMCompiler& compiler)
1970 {
1971         ShaderInput *value1_in = input("Value1");
1972         ShaderInput *value2_in = input("Value2");
1973         ShaderOutput *value_out = output("Value");
1974
1975         compiler.stack_assign(value1_in);
1976         compiler.stack_assign(value2_in);
1977         compiler.stack_assign(value_out);
1978
1979         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
1980         compiler.add_node(NODE_MATH, value_out->stack_offset);
1981 }
1982
1983 void MathNode::compile(OSLCompiler& compiler)
1984 {
1985         compiler.parameter("type", type);
1986         compiler.add(this, "node_math");
1987 }
1988
1989 /* VectorMath */
1990
1991 VectorMathNode::VectorMathNode()
1992 : ShaderNode("vector_math")
1993 {
1994         type = ustring("Add");
1995
1996         add_input("Vector1", SHADER_SOCKET_VECTOR);
1997         add_input("Vector2", SHADER_SOCKET_VECTOR);
1998         add_output("Value",  SHADER_SOCKET_FLOAT);
1999         add_output("Vector",  SHADER_SOCKET_VECTOR);
2000 }
2001
2002 static ShaderEnum vector_math_type_init()
2003 {
2004         ShaderEnum enm;
2005
2006         enm.insert("Add", NODE_VECTOR_MATH_ADD);
2007         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
2008         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
2009         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
2010         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
2011         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
2012
2013         return enm;
2014 }
2015
2016 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
2017
2018 void VectorMathNode::compile(SVMCompiler& compiler)
2019 {
2020         ShaderInput *vector1_in = input("Vector1");
2021         ShaderInput *vector2_in = input("Vector2");
2022         ShaderOutput *value_out = output("Value");
2023         ShaderOutput *vector_out = output("Vector");
2024
2025         compiler.stack_assign(vector1_in);
2026         compiler.stack_assign(vector2_in);
2027         compiler.stack_assign(value_out);
2028         compiler.stack_assign(vector_out);
2029
2030         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
2031         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
2032 }
2033
2034 void VectorMathNode::compile(OSLCompiler& compiler)
2035 {
2036         compiler.parameter("type", type);
2037         compiler.add(this, "node_vector_math");
2038 }
2039
2040 /* BumpNode */
2041
2042 BumpNode::BumpNode()
2043 : ShaderNode("bump")
2044 {
2045         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
2046         add_input("SampleX", SHADER_SOCKET_FLOAT);
2047         add_input("SampleY", SHADER_SOCKET_FLOAT);
2048
2049         add_output("Normal", SHADER_SOCKET_NORMAL);
2050 }
2051
2052 void BumpNode::compile(SVMCompiler& compiler)
2053 {
2054         ShaderInput *center_in = input("SampleCenter");
2055         ShaderInput *dx_in = input("SampleX");
2056         ShaderInput *dy_in = input("SampleY");
2057
2058         compiler.stack_assign(center_in);
2059         compiler.stack_assign(dx_in);
2060         compiler.stack_assign(dy_in);
2061
2062         compiler.add_node(NODE_SET_BUMP, center_in->stack_offset, dx_in->stack_offset, dy_in->stack_offset);
2063 }
2064
2065 void BumpNode::compile(OSLCompiler& compiler)
2066 {
2067         compiler.add(this, "node_bump");
2068 }
2069
2070 CCL_NAMESPACE_END
2071