Cycles / Sky Model:
[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 #include "sky_model.h"
24
25 #include "util_transform.h"
26
27 CCL_NAMESPACE_BEGIN
28
29 /* Texture Mapping */
30
31 TextureMapping::TextureMapping()
32 {
33         translation = make_float3(0.0f, 0.0f, 0.0f);
34         rotation = make_float3(0.0f, 0.0f, 0.0f);
35         scale = make_float3(1.0f, 1.0f, 1.0f);
36
37         min = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
38         max = make_float3(FLT_MAX, FLT_MAX, FLT_MAX);
39
40         use_minmax = false;
41
42         x_mapping = X;
43         y_mapping = Y;
44         z_mapping = Z;
45
46         projection = FLAT;
47 }
48
49 Transform TextureMapping::compute_transform()
50 {
51         Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
52
53         if(x_mapping != NONE)
54                 mmat[0][x_mapping-1] = 1.0f;
55         if(y_mapping != NONE)
56                 mmat[1][y_mapping-1] = 1.0f;
57         if(z_mapping != NONE)
58                 mmat[2][z_mapping-1] = 1.0f;
59
60         Transform smat = transform_scale(scale);
61         Transform rmat = transform_euler(rotation);
62         Transform tmat = transform_translate(translation);
63
64         return tmat*rmat*smat*mmat;
65 }
66
67 bool TextureMapping::skip()
68 {
69         if(translation != make_float3(0.0f, 0.0f, 0.0f))
70                 return false;
71         if(rotation != make_float3(0.0f, 0.0f, 0.0f))
72                 return false;
73         if(scale != make_float3(1.0f, 1.0f, 1.0f))
74                 return false;
75         
76         if(x_mapping != X || y_mapping != Y || z_mapping != Z)
77                 return false;
78         if(use_minmax)
79                 return false;
80         
81         return true;
82 }
83
84 void TextureMapping::compile(SVMCompiler& compiler, int offset_in, int offset_out)
85 {
86         if(offset_in == SVM_STACK_INVALID || offset_out == SVM_STACK_INVALID)
87                 return;
88
89         compiler.add_node(NODE_MAPPING, offset_in, offset_out);
90
91         Transform tfm = compute_transform();
92         compiler.add_node(tfm.x);
93         compiler.add_node(tfm.y);
94         compiler.add_node(tfm.z);
95         compiler.add_node(tfm.w);
96
97         if(use_minmax) {
98                 compiler.add_node(NODE_MIN_MAX, offset_out, offset_out);
99                 compiler.add_node(float3_to_float4(min));
100                 compiler.add_node(float3_to_float4(max));
101         }
102 }
103
104 void TextureMapping::compile(OSLCompiler &compiler)
105 {
106         if(!skip()) {
107                 Transform tfm = transform_transpose(compute_transform());
108
109                 compiler.parameter("mapping", tfm);
110                 compiler.parameter("use_mapping", 1);
111         }
112 }
113
114 /* Image Texture */
115
116 static ShaderEnum color_space_init()
117 {
118         ShaderEnum enm;
119
120         enm.insert("None", 0);
121         enm.insert("Color", 1);
122
123         return enm;
124 }
125
126 static ShaderEnum image_projection_init()
127 {
128         ShaderEnum enm;
129
130         enm.insert("Flat", 0);
131         enm.insert("Box", 1);
132
133         return enm;
134 }
135
136 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
137 ShaderEnum ImageTextureNode::projection_enum = image_projection_init();
138
139 ImageTextureNode::ImageTextureNode()
140 : TextureNode("image_texture")
141 {
142         image_manager = NULL;
143         slot = -1;
144         is_float = -1;
145         is_linear = false;
146         filename = "";
147         builtin_data = NULL;
148         color_space = ustring("Color");
149         projection = ustring("Flat");
150         projection_blend = 0.0f;
151         animated = false;
152
153         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_UV);
154         add_output("Color", SHADER_SOCKET_COLOR);
155         add_output("Alpha", SHADER_SOCKET_FLOAT);
156 }
157
158 ImageTextureNode::~ImageTextureNode()
159 {
160         if(image_manager)
161                 image_manager->remove_image(filename, builtin_data);
162 }
163
164 ShaderNode *ImageTextureNode::clone() const
165 {
166         ImageTextureNode *node = new ImageTextureNode(*this);
167         node->image_manager = NULL;
168         node->slot = -1;
169         node->is_float = -1;
170         node->is_linear = false;
171         return node;
172 }
173
174 void ImageTextureNode::compile(SVMCompiler& compiler)
175 {
176         ShaderInput *vector_in = input("Vector");
177         ShaderOutput *color_out = output("Color");
178         ShaderOutput *alpha_out = output("Alpha");
179
180         image_manager = compiler.image_manager;
181         if(is_float == -1) {
182                 bool is_float_bool;
183                 slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear);
184                 is_float = (int)is_float_bool;
185         }
186
187         if(!color_out->links.empty())
188                 compiler.stack_assign(color_out);
189         if(!alpha_out->links.empty())
190                 compiler.stack_assign(alpha_out);
191
192         if(slot != -1) {
193                 compiler.stack_assign(vector_in);
194
195                 int srgb = (is_linear || color_space != "Color")? 0: 1;
196                 int vector_offset = vector_in->stack_offset;
197
198                 if(!tex_mapping.skip()) {
199                         vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
200                         tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
201                 }
202
203                 if(projection == "Flat") {
204                         compiler.add_node(NODE_TEX_IMAGE,
205                                 slot,
206                                 compiler.encode_uchar4(
207                                         vector_offset,
208                                         color_out->stack_offset,
209                                         alpha_out->stack_offset,
210                                         srgb));
211                 }
212                 else {
213                         compiler.add_node(NODE_TEX_IMAGE_BOX,
214                                 slot,
215                                 compiler.encode_uchar4(
216                                         vector_offset,
217                                         color_out->stack_offset,
218                                         alpha_out->stack_offset,
219                                         srgb),
220                                 __float_as_int(projection_blend));
221                 }
222         
223                 if(vector_offset != vector_in->stack_offset)
224                         compiler.stack_clear_offset(vector_in->type, vector_offset);
225         }
226         else {
227                 /* image not found */
228                 if(!color_out->links.empty()) {
229                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
230                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
231                                                                     TEX_IMAGE_MISSING_G,
232                                                                     TEX_IMAGE_MISSING_B));
233                 }
234                 if(!alpha_out->links.empty())
235                         compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
236         }
237 }
238
239 void ImageTextureNode::compile(OSLCompiler& compiler)
240 {
241         ShaderOutput *alpha_out = output("Alpha");
242
243         tex_mapping.compile(compiler);
244
245         if(is_float == -1)
246                 is_float = (int)image_manager->is_float_image(filename, NULL, is_linear);
247
248         compiler.parameter("filename", filename.c_str());
249         if(is_linear || color_space != "Color")
250                 compiler.parameter("color_space", "Linear");
251         else
252                 compiler.parameter("color_space", "sRGB");
253         compiler.parameter("projection", projection);
254         compiler.parameter("projection_blend", projection_blend);
255         compiler.parameter("is_float", is_float);
256         compiler.parameter("use_alpha", !alpha_out->links.empty());
257         compiler.add(this, "node_image_texture");
258 }
259
260 /* Environment Texture */
261
262 static ShaderEnum env_projection_init()
263 {
264         ShaderEnum enm;
265
266         enm.insert("Equirectangular", 0);
267         enm.insert("Mirror Ball", 1);
268
269         return enm;
270 }
271
272 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
273 ShaderEnum EnvironmentTextureNode::projection_enum = env_projection_init();
274
275 EnvironmentTextureNode::EnvironmentTextureNode()
276 : TextureNode("environment_texture")
277 {
278         image_manager = NULL;
279         slot = -1;
280         is_float = -1;
281         is_linear = false;
282         filename = "";
283         builtin_data = NULL;
284         color_space = ustring("Color");
285         projection = ustring("Equirectangular");
286         animated = false;
287
288         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
289         add_output("Color", SHADER_SOCKET_COLOR);
290         add_output("Alpha", SHADER_SOCKET_FLOAT);
291 }
292
293 EnvironmentTextureNode::~EnvironmentTextureNode()
294 {
295         if(image_manager)
296                 image_manager->remove_image(filename, builtin_data);
297 }
298
299 ShaderNode *EnvironmentTextureNode::clone() const
300 {
301         EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
302         node->image_manager = NULL;
303         node->slot = -1;
304         node->is_float = -1;
305         node->is_linear = false;
306         return node;
307 }
308
309 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
310 {
311         ShaderInput *vector_in = input("Vector");
312         ShaderOutput *color_out = output("Color");
313         ShaderOutput *alpha_out = output("Alpha");
314
315         image_manager = compiler.image_manager;
316         if(slot == -1) {
317                 bool is_float_bool;
318                 slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear);
319                 is_float = (int)is_float_bool;
320         }
321
322         if(!color_out->links.empty())
323                 compiler.stack_assign(color_out);
324         if(!alpha_out->links.empty())
325                 compiler.stack_assign(alpha_out);
326         
327         if(slot != -1) {
328                 compiler.stack_assign(vector_in);
329
330                 int srgb = (is_linear || color_space != "Color")? 0: 1;
331                 int vector_offset = vector_in->stack_offset;
332
333                 if(!tex_mapping.skip()) {
334                         vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
335                         tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
336                 }
337
338                 compiler.add_node(NODE_TEX_ENVIRONMENT,
339                         slot,
340                         compiler.encode_uchar4(
341                                 vector_offset,
342                                 color_out->stack_offset,
343                                 alpha_out->stack_offset,
344                                 srgb),
345                         projection_enum[projection]);
346         
347                 if(vector_offset != vector_in->stack_offset)
348                         compiler.stack_clear_offset(vector_in->type, vector_offset);
349         }
350         else {
351                 /* image not found */
352                 if(!color_out->links.empty()) {
353                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
354                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
355                                                                     TEX_IMAGE_MISSING_G,
356                                                                     TEX_IMAGE_MISSING_B));
357                 }
358                 if(!alpha_out->links.empty())
359                         compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
360         }
361 }
362
363 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
364 {
365         ShaderOutput *alpha_out = output("Alpha");
366
367         tex_mapping.compile(compiler);
368
369         if(is_float == -1)
370                 is_float = (int)image_manager->is_float_image(filename, NULL, is_linear);
371
372         compiler.parameter("filename", filename.c_str());
373         compiler.parameter("projection", projection);
374         if(is_linear || color_space != "Color")
375                 compiler.parameter("color_space", "Linear");
376         else
377                 compiler.parameter("color_space", "sRGB");
378         compiler.parameter("is_float", is_float);
379         compiler.parameter("use_alpha", !alpha_out->links.empty());
380         compiler.add(this, "node_environment_texture");
381 }
382
383 /* Sky Texture */
384
385 static float2 sky_spherical_coordinates(float3 dir)
386 {
387         return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
388 }
389
390 static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity, float albedo)
391 {
392         /* Calculate Sun Direction and save coordinates */
393         float2 spherical = sky_spherical_coordinates(dir);
394         float theta = spherical.x;
395         float phi = spherical.y;
396
397         ksunsky->theta = theta;
398         ksunsky->phi = phi;
399
400         double solarElevation = M_PI_2_F - theta;
401
402         /* Initialize Sky Model */
403         ArHosekSkyModelState *sky_state;
404         sky_state = arhosek_xyz_skymodelstate_alloc_init(turbidity, albedo, solarElevation);
405
406         /* Copy values from sky_state to kernel_data */
407         ksunsky->config_x[0] = sky_state->configs[0][0];
408         ksunsky->config_x[1] = sky_state->configs[0][1];
409         ksunsky->config_x[2] = sky_state->configs[0][2];
410         ksunsky->config_x[3] = sky_state->configs[0][3];
411         ksunsky->config_x[4] = sky_state->configs[0][4];
412         ksunsky->config_x[5] = sky_state->configs[0][5];
413         ksunsky->config_x[6] = sky_state->configs[0][6];
414         ksunsky->config_x[7] = sky_state->configs[0][7];
415         ksunsky->config_x[8] = sky_state->configs[0][8];
416
417         ksunsky->config_y[0] = sky_state->configs[1][0];
418         ksunsky->config_y[1] = sky_state->configs[1][1];
419         ksunsky->config_y[2] = sky_state->configs[1][2];
420         ksunsky->config_y[3] = sky_state->configs[1][3];
421         ksunsky->config_y[4] = sky_state->configs[1][4];
422         ksunsky->config_y[5] = sky_state->configs[1][5];
423         ksunsky->config_y[6] = sky_state->configs[1][6];
424         ksunsky->config_y[7] = sky_state->configs[1][7];
425         ksunsky->config_y[8] = sky_state->configs[1][8];
426
427         ksunsky->config_z[0] = sky_state->configs[2][0];
428         ksunsky->config_z[1] = sky_state->configs[2][1];
429         ksunsky->config_z[2] = sky_state->configs[2][2];
430         ksunsky->config_z[3] = sky_state->configs[2][3];
431         ksunsky->config_z[4] = sky_state->configs[2][4];
432         ksunsky->config_z[5] = sky_state->configs[2][5];
433         ksunsky->config_z[6] = sky_state->configs[2][6];
434         ksunsky->config_z[7] = sky_state->configs[2][7];
435         ksunsky->config_z[8] = sky_state->configs[2][8];
436
437         ksunsky->radiance_x = sky_state->radiances[0];
438         ksunsky->radiance_y = sky_state->radiances[1];
439         ksunsky->radiance_z = sky_state->radiances[2];
440
441         /* Free sky_state */
442         arhosekskymodelstate_free(sky_state);
443 }
444
445 SkyTextureNode::SkyTextureNode()
446 : TextureNode("sky_texture")
447 {
448         sun_direction = make_float3(0.0f, 0.0f, 1.0f);
449         turbidity = 2.2f;
450         albedo = 0.3f;
451
452         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
453         add_output("Color", SHADER_SOCKET_COLOR);
454 }
455
456 void SkyTextureNode::compile(SVMCompiler& compiler)
457 {
458         ShaderInput *vector_in = input("Vector");
459         ShaderOutput *color_out = output("Color");
460
461         if(compiler.sunsky) {
462                 sky_texture_precompute(compiler.sunsky, sun_direction, turbidity, albedo);
463                 compiler.sunsky = NULL;
464         }
465
466         if(vector_in->link)
467                 compiler.stack_assign(vector_in);
468
469         int vector_offset = vector_in->stack_offset;
470
471         if(!tex_mapping.skip()) {
472                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
473                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
474         }
475
476         compiler.stack_assign(color_out);
477         compiler.add_node(NODE_TEX_SKY, vector_offset, color_out->stack_offset);
478
479         if(vector_offset != vector_in->stack_offset)
480                 compiler.stack_clear_offset(vector_in->type, vector_offset);
481 }
482
483 void SkyTextureNode::compile(OSLCompiler& compiler)
484 {
485         tex_mapping.compile(compiler);
486
487         compiler.parameter_vector("sun_direction", sun_direction);
488         compiler.parameter("turbidity", turbidity);
489         compiler.add(this, "node_sky_texture");
490 }
491
492 /* Gradient Texture */
493
494 static ShaderEnum gradient_type_init()
495 {
496         ShaderEnum enm;
497
498         enm.insert("Linear", NODE_BLEND_LINEAR);
499         enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
500         enm.insert("Easing", NODE_BLEND_EASING);
501         enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
502         enm.insert("Radial", NODE_BLEND_RADIAL);
503         enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
504         enm.insert("Spherical", NODE_BLEND_SPHERICAL);
505
506         return enm;
507 }
508
509 ShaderEnum GradientTextureNode::type_enum = gradient_type_init();
510
511 GradientTextureNode::GradientTextureNode()
512 : TextureNode("gradient_texture")
513 {
514         type = ustring("Linear");
515
516         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
517         add_output("Color", SHADER_SOCKET_COLOR);
518         add_output("Fac", SHADER_SOCKET_FLOAT);
519 }
520
521 void GradientTextureNode::compile(SVMCompiler& compiler)
522 {
523         ShaderInput *vector_in = input("Vector");
524         ShaderOutput *color_out = output("Color");
525         ShaderOutput *fac_out = output("Fac");
526
527         if(vector_in->link) compiler.stack_assign(vector_in);
528
529         int vector_offset = vector_in->stack_offset;
530
531         if(!tex_mapping.skip()) {
532                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
533                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
534         }
535
536         if(!fac_out->links.empty())
537                 compiler.stack_assign(fac_out);
538         if(!color_out->links.empty())
539                 compiler.stack_assign(color_out);
540
541         compiler.add_node(NODE_TEX_GRADIENT,
542                 compiler.encode_uchar4(type_enum[type], vector_offset, fac_out->stack_offset, color_out->stack_offset));
543
544         if(vector_offset != vector_in->stack_offset)
545                 compiler.stack_clear_offset(vector_in->type, vector_offset);
546 }
547
548 void GradientTextureNode::compile(OSLCompiler& compiler)
549 {
550         tex_mapping.compile(compiler);
551
552         compiler.parameter("Type", type);
553         compiler.add(this, "node_gradient_texture");
554 }
555
556 /* Noise Texture */
557
558 NoiseTextureNode::NoiseTextureNode()
559 : TextureNode("noise_texture")
560 {
561         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
562         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
563         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
564         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
565
566         add_output("Color", SHADER_SOCKET_COLOR);
567         add_output("Fac", SHADER_SOCKET_FLOAT);
568 }
569
570 void NoiseTextureNode::compile(SVMCompiler& compiler)
571 {
572         ShaderInput *distortion_in = input("Distortion");
573         ShaderInput *detail_in = input("Detail");
574         ShaderInput *scale_in = input("Scale");
575         ShaderInput *vector_in = input("Vector");
576         ShaderOutput *color_out = output("Color");
577         ShaderOutput *fac_out = output("Fac");
578
579         if(vector_in->link) compiler.stack_assign(vector_in);
580         if(scale_in->link) compiler.stack_assign(scale_in);
581         if(detail_in->link) compiler.stack_assign(detail_in);
582         if(distortion_in->link) compiler.stack_assign(distortion_in);
583
584         int vector_offset = vector_in->stack_offset;
585
586         if(!tex_mapping.skip()) {
587                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
588                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
589         }
590
591         if(!fac_out->links.empty())
592                 compiler.stack_assign(fac_out);
593         if(!color_out->links.empty())
594                 compiler.stack_assign(color_out);
595
596         compiler.add_node(NODE_TEX_NOISE,
597                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
598                 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset));
599         compiler.add_node(
600                 __float_as_int(scale_in->value.x),
601                 __float_as_int(detail_in->value.x),
602                 __float_as_int(distortion_in->value.x));
603
604         if(vector_offset != vector_in->stack_offset)
605                 compiler.stack_clear_offset(vector_in->type, vector_offset);
606 }
607
608 void NoiseTextureNode::compile(OSLCompiler& compiler)
609 {
610         tex_mapping.compile(compiler);
611
612         compiler.add(this, "node_noise_texture");
613 }
614
615 /* Voronoi Texture */
616
617 static ShaderEnum voronoi_coloring_init()
618 {
619         ShaderEnum enm;
620
621         enm.insert("Intensity", NODE_VORONOI_INTENSITY);
622         enm.insert("Cells", NODE_VORONOI_CELLS);
623
624         return enm;
625 }
626
627 ShaderEnum VoronoiTextureNode::coloring_enum  = voronoi_coloring_init();
628
629 VoronoiTextureNode::VoronoiTextureNode()
630 : TextureNode("voronoi_texture")
631 {
632         coloring = ustring("Intensity");
633
634         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
635         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
636
637         add_output("Color", SHADER_SOCKET_COLOR);
638         add_output("Fac", SHADER_SOCKET_FLOAT);
639 }
640
641 void VoronoiTextureNode::compile(SVMCompiler& compiler)
642 {
643         ShaderInput *scale_in = input("Scale");
644         ShaderInput *vector_in = input("Vector");
645         ShaderOutput *color_out = output("Color");
646         ShaderOutput *fac_out = output("Fac");
647
648         if(vector_in->link) compiler.stack_assign(vector_in);
649         if(scale_in->link) compiler.stack_assign(scale_in);
650
651         int vector_offset = vector_in->stack_offset;
652
653         if(!tex_mapping.skip()) {
654                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
655                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
656         }
657
658         compiler.stack_assign(color_out);
659         compiler.stack_assign(fac_out);
660
661         compiler.add_node(NODE_TEX_VORONOI,
662                 coloring_enum[coloring],
663                 compiler.encode_uchar4(scale_in->stack_offset, vector_offset, fac_out->stack_offset, color_out->stack_offset),
664                 __float_as_int(scale_in->value.x));
665
666         if(vector_offset != vector_in->stack_offset)
667                 compiler.stack_clear_offset(vector_in->type, vector_offset);
668 }
669
670 void VoronoiTextureNode::compile(OSLCompiler& compiler)
671 {
672         tex_mapping.compile(compiler);
673
674         compiler.parameter("Coloring", coloring);
675         compiler.add(this, "node_voronoi_texture");
676 }
677
678 /* Musgrave Texture */
679
680 static ShaderEnum musgrave_type_init()
681 {
682         ShaderEnum enm;
683
684         enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
685         enm.insert("fBM", NODE_MUSGRAVE_FBM);
686         enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
687         enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
688         enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
689
690         return enm;
691 }
692
693 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
694
695 MusgraveTextureNode::MusgraveTextureNode()
696 : TextureNode("musgrave_texture")
697 {
698         type = ustring("fBM");
699
700         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
701         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
702         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
703         add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
704         add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
705         add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
706         add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
707
708         add_output("Fac", SHADER_SOCKET_FLOAT);
709         add_output("Color", SHADER_SOCKET_COLOR);
710 }
711
712 void MusgraveTextureNode::compile(SVMCompiler& compiler)
713 {
714         ShaderInput *vector_in = input("Vector");
715         ShaderInput *scale_in = input("Scale");
716         ShaderInput *dimension_in = input("Dimension");
717         ShaderInput *lacunarity_in = input("Lacunarity");
718         ShaderInput *detail_in = input("Detail");
719         ShaderInput *offset_in = input("Offset");
720         ShaderInput *gain_in = input("Gain");
721         ShaderOutput *fac_out = output("Fac");
722         ShaderOutput *color_out = output("Color");
723
724         if(vector_in->link) compiler.stack_assign(vector_in);
725         if(dimension_in->link) compiler.stack_assign(dimension_in);
726         if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
727         if(detail_in->link) compiler.stack_assign(detail_in);
728         if(offset_in->link) compiler.stack_assign(offset_in);
729         if(gain_in->link) compiler.stack_assign(gain_in);
730         if(scale_in->link) compiler.stack_assign(scale_in);
731
732         int vector_offset = vector_in->stack_offset;
733
734         if(!tex_mapping.skip()) {
735                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
736                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
737         }
738
739         if(!fac_out->links.empty())
740                 compiler.stack_assign(fac_out);
741         if(!color_out->links.empty())
742                 compiler.stack_assign(color_out);
743
744         compiler.add_node(NODE_TEX_MUSGRAVE,
745                 compiler.encode_uchar4(type_enum[type], vector_offset, color_out->stack_offset, fac_out->stack_offset),
746                 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, detail_in->stack_offset, offset_in->stack_offset),
747                 compiler.encode_uchar4(gain_in->stack_offset, scale_in->stack_offset));
748         compiler.add_node(__float_as_int(dimension_in->value.x),
749                 __float_as_int(lacunarity_in->value.x),
750                 __float_as_int(detail_in->value.x),
751                 __float_as_int(offset_in->value.x));
752         compiler.add_node(__float_as_int(gain_in->value.x),
753                 __float_as_int(scale_in->value.x));
754
755         if(vector_offset != vector_in->stack_offset)
756                 compiler.stack_clear_offset(vector_in->type, vector_offset);
757 }
758
759 void MusgraveTextureNode::compile(OSLCompiler& compiler)
760 {
761         tex_mapping.compile(compiler);
762
763         compiler.parameter("Type", type);
764
765         compiler.add(this, "node_musgrave_texture");
766 }
767
768 /* Wave Texture */
769
770 static ShaderEnum wave_type_init()
771 {
772         ShaderEnum enm;
773
774         enm.insert("Bands", NODE_WAVE_BANDS);
775         enm.insert("Rings", NODE_WAVE_RINGS);
776
777         return enm;
778 }
779
780 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
781
782 WaveTextureNode::WaveTextureNode()
783 : TextureNode("wave_texture")
784 {
785         type = ustring("Bands");
786
787         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
788         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
789         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
790         add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
791         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
792
793         add_output("Color", SHADER_SOCKET_COLOR);
794         add_output("Fac", SHADER_SOCKET_FLOAT);
795 }
796
797 void WaveTextureNode::compile(SVMCompiler& compiler)
798 {
799         ShaderInput *scale_in = input("Scale");
800         ShaderInput *distortion_in = input("Distortion");
801         ShaderInput *dscale_in = input("Detail Scale");
802         ShaderInput *detail_in = input("Detail");
803         ShaderInput *vector_in = input("Vector");
804         ShaderOutput *fac_out = output("Fac");
805         ShaderOutput *color_out = output("Color");
806
807         if(scale_in->link) compiler.stack_assign(scale_in);
808         if(detail_in->link) compiler.stack_assign(detail_in);
809         if(distortion_in->link) compiler.stack_assign(distortion_in);
810         if(dscale_in->link) compiler.stack_assign(dscale_in);
811         if(vector_in->link) compiler.stack_assign(vector_in);
812
813         int vector_offset = vector_in->stack_offset;
814
815         if(!tex_mapping.skip()) {
816                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
817                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
818         }
819
820         if(!fac_out->links.empty())
821                 compiler.stack_assign(fac_out);
822         if(!color_out->links.empty())
823                 compiler.stack_assign(color_out);
824
825         compiler.add_node(NODE_TEX_WAVE,
826                 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
827                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset));
828
829         compiler.add_node(
830                 __float_as_int(scale_in->value.x),
831                 __float_as_int(detail_in->value.x),
832                 __float_as_int(distortion_in->value.x),
833                 __float_as_int(dscale_in->value.x));
834
835         if(vector_offset != vector_in->stack_offset)
836                 compiler.stack_clear_offset(vector_in->type, vector_offset);
837 }
838
839 void WaveTextureNode::compile(OSLCompiler& compiler)
840 {
841         tex_mapping.compile(compiler);
842
843         compiler.parameter("Type", type);
844
845         compiler.add(this, "node_wave_texture");
846 }
847
848 /* Magic Texture */
849
850 MagicTextureNode::MagicTextureNode()
851 : TextureNode("magic_texture")
852 {
853         depth = 2;
854
855         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
856         add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
857         add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
858
859         add_output("Color", SHADER_SOCKET_COLOR);
860         add_output("Fac", SHADER_SOCKET_FLOAT);
861 }
862
863 void MagicTextureNode::compile(SVMCompiler& compiler)
864 {
865         ShaderInput *vector_in = input("Vector");
866         ShaderInput *scale_in = input("Scale");
867         ShaderInput *distortion_in = input("Distortion");
868         ShaderOutput *color_out = output("Color");
869         ShaderOutput *fac_out = output("Fac");
870
871         if(vector_in->link) compiler.stack_assign(vector_in);
872         if(distortion_in->link) compiler.stack_assign(distortion_in);
873         if(scale_in->link) compiler.stack_assign(scale_in);
874
875         int vector_offset = vector_in->stack_offset;
876
877         if(!tex_mapping.skip()) {
878                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
879                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
880         }
881
882         if(!fac_out->links.empty())
883                 compiler.stack_assign(fac_out);
884         if(!color_out->links.empty())
885                 compiler.stack_assign(color_out);
886
887         compiler.add_node(NODE_TEX_MAGIC,
888                 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
889                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, distortion_in->stack_offset));
890         compiler.add_node(
891                 __float_as_int(scale_in->value.x),
892                 __float_as_int(distortion_in->value.x));
893
894         if(vector_offset != vector_in->stack_offset)
895                 compiler.stack_clear_offset(vector_in->type, vector_offset);
896 }
897
898 void MagicTextureNode::compile(OSLCompiler& compiler)
899 {
900         tex_mapping.compile(compiler);
901
902         compiler.parameter("Depth", depth);
903         compiler.add(this, "node_magic_texture");
904 }
905
906 /* Checker Texture */
907
908 CheckerTextureNode::CheckerTextureNode()
909 : TextureNode("checker_texture")
910 {
911         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
912         add_input("Color1", SHADER_SOCKET_COLOR);
913         add_input("Color2", SHADER_SOCKET_COLOR);
914         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
915
916         add_output("Color", SHADER_SOCKET_COLOR);
917         add_output("Fac", SHADER_SOCKET_FLOAT);
918 }
919
920 void CheckerTextureNode::compile(SVMCompiler& compiler)
921 {
922         ShaderInput *vector_in = input("Vector");
923         ShaderInput *color1_in = input("Color1");
924         ShaderInput *color2_in = input("Color2");
925         ShaderInput *scale_in = input("Scale");
926         
927         ShaderOutput *color_out = output("Color");
928         ShaderOutput *fac_out = output("Fac");
929
930         compiler.stack_assign(vector_in);
931         compiler.stack_assign(color1_in);
932         compiler.stack_assign(color2_in);
933         if(scale_in->link) compiler.stack_assign(scale_in);
934
935         int vector_offset = vector_in->stack_offset;
936
937         if(!tex_mapping.skip()) {
938                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
939                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
940         }
941
942         if(!color_out->links.empty())
943                 compiler.stack_assign(color_out);
944         if(!fac_out->links.empty())
945                 compiler.stack_assign(fac_out);
946
947         compiler.add_node(NODE_TEX_CHECKER,
948                 compiler.encode_uchar4(vector_offset, color1_in->stack_offset, color2_in->stack_offset, scale_in->stack_offset),
949                 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset),
950                 __float_as_int(scale_in->value.x));
951
952         if(vector_offset != vector_in->stack_offset)
953                 compiler.stack_clear_offset(vector_in->type, vector_offset);
954 }
955
956 void CheckerTextureNode::compile(OSLCompiler& compiler)
957 {
958         tex_mapping.compile(compiler);
959
960         compiler.add(this, "node_checker_texture");
961 }
962
963 /* Brick Texture */
964
965 BrickTextureNode::BrickTextureNode()
966 : TextureNode("brick_texture")
967 {
968         offset = 0.5f;
969         offset_frequency = 2;
970         squash = 1.0f;
971         squash_frequency = 2;
972         
973         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
974         add_input("Color1", SHADER_SOCKET_COLOR);
975         add_input("Color2", SHADER_SOCKET_COLOR);
976         add_input("Mortar", SHADER_SOCKET_COLOR);
977         add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
978         add_input("Mortar Size", SHADER_SOCKET_FLOAT, 0.02f);
979         add_input("Bias", SHADER_SOCKET_FLOAT, 0.0f);
980         add_input("Brick Width", SHADER_SOCKET_FLOAT, 0.5f);
981         add_input("Row Height", SHADER_SOCKET_FLOAT, 0.25f);
982
983         add_output("Color", SHADER_SOCKET_COLOR);
984         add_output("Fac", SHADER_SOCKET_FLOAT);
985 }
986
987 void BrickTextureNode::compile(SVMCompiler& compiler)
988 {
989         ShaderInput *vector_in = input("Vector");
990         ShaderInput *color1_in = input("Color1");
991         ShaderInput *color2_in = input("Color2");
992         ShaderInput *mortar_in = input("Mortar");
993         ShaderInput *scale_in = input("Scale");
994         ShaderInput *mortar_size_in = input("Mortar Size");
995         ShaderInput *bias_in = input("Bias");
996         ShaderInput *brick_width_in = input("Brick Width");
997         ShaderInput *row_height_in = input("Row Height");
998         
999         ShaderOutput *color_out = output("Color");
1000         ShaderOutput *fac_out = output("Fac");
1001
1002         compiler.stack_assign(vector_in);
1003         compiler.stack_assign(color1_in);
1004         compiler.stack_assign(color2_in);
1005         compiler.stack_assign(mortar_in);
1006         if(scale_in->link) compiler.stack_assign(scale_in);
1007         if(mortar_size_in->link) compiler.stack_assign(mortar_size_in);
1008         if(bias_in->link) compiler.stack_assign(bias_in);
1009         if(brick_width_in->link) compiler.stack_assign(brick_width_in);
1010         if(row_height_in->link) compiler.stack_assign(row_height_in);
1011
1012         int vector_offset = vector_in->stack_offset;
1013
1014         if(!tex_mapping.skip()) {
1015                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1016                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1017         }
1018
1019         if(!color_out->links.empty())
1020                 compiler.stack_assign(color_out);
1021         if(!fac_out->links.empty())
1022                 compiler.stack_assign(fac_out);
1023
1024         compiler.add_node(NODE_TEX_BRICK,
1025                 compiler.encode_uchar4(vector_offset,
1026                         color1_in->stack_offset, color2_in->stack_offset, mortar_in->stack_offset),
1027                 compiler.encode_uchar4(scale_in->stack_offset,
1028                         mortar_size_in->stack_offset, bias_in->stack_offset, brick_width_in->stack_offset),
1029                 compiler.encode_uchar4(row_height_in->stack_offset,
1030                         color_out->stack_offset, fac_out->stack_offset));
1031                         
1032         compiler.add_node(compiler.encode_uchar4(offset_frequency, squash_frequency),
1033                 __float_as_int(scale_in->value.x),
1034                 __float_as_int(mortar_size_in->value.x),
1035                 __float_as_int(bias_in->value.x));
1036
1037         compiler.add_node(__float_as_int(brick_width_in->value.x),
1038                 __float_as_int(row_height_in->value.x),
1039                 __float_as_int(offset),
1040                 __float_as_int(squash));
1041
1042         if(vector_offset != vector_in->stack_offset)
1043                 compiler.stack_clear_offset(vector_in->type, vector_offset);
1044 }
1045
1046 void BrickTextureNode::compile(OSLCompiler& compiler)
1047 {
1048         tex_mapping.compile(compiler);
1049
1050         compiler.parameter("Offset", offset);
1051         compiler.parameter("OffsetFrequency", offset_frequency);
1052         compiler.parameter("Squash", squash);
1053         compiler.parameter("SquashFrequency", squash_frequency);
1054         compiler.add(this, "node_brick_texture");
1055 }
1056
1057 /* Normal */
1058
1059 NormalNode::NormalNode()
1060 : ShaderNode("normal")
1061 {
1062         direction = make_float3(0.0f, 0.0f, 1.0f);
1063
1064         add_input("Normal", SHADER_SOCKET_NORMAL);
1065         add_output("Normal", SHADER_SOCKET_NORMAL);
1066         add_output("Dot",  SHADER_SOCKET_FLOAT);
1067 }
1068
1069 void NormalNode::compile(SVMCompiler& compiler)
1070 {
1071         ShaderInput *normal_in = input("Normal");
1072         ShaderOutput *normal_out = output("Normal");
1073         ShaderOutput *dot_out = output("Dot");
1074
1075         compiler.stack_assign(normal_in);
1076         compiler.stack_assign(normal_out);
1077         compiler.stack_assign(dot_out);
1078
1079         compiler.add_node(NODE_NORMAL, normal_in->stack_offset, normal_out->stack_offset, dot_out->stack_offset);
1080         compiler.add_node(
1081                 __float_as_int(direction.x),
1082                 __float_as_int(direction.y),
1083                 __float_as_int(direction.z));
1084 }
1085
1086 void NormalNode::compile(OSLCompiler& compiler)
1087 {
1088         compiler.parameter_normal("Direction", direction);
1089         compiler.add(this, "node_normal");
1090 }
1091
1092 /* Mapping */
1093
1094 MappingNode::MappingNode()
1095 : ShaderNode("mapping")
1096 {
1097         add_input("Vector", SHADER_SOCKET_POINT);
1098         add_output("Vector", SHADER_SOCKET_POINT);
1099 }
1100
1101 void MappingNode::compile(SVMCompiler& compiler)
1102 {
1103         ShaderInput *vector_in = input("Vector");
1104         ShaderOutput *vector_out = output("Vector");
1105
1106         compiler.stack_assign(vector_in);
1107         compiler.stack_assign(vector_out);
1108
1109         tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
1110 }
1111
1112 void MappingNode::compile(OSLCompiler& compiler)
1113 {
1114         Transform tfm = transform_transpose(tex_mapping.compute_transform());
1115         compiler.parameter("Matrix", tfm);
1116         compiler.parameter_point("mapping_min", tex_mapping.min);
1117         compiler.parameter_point("mapping_max", tex_mapping.max);
1118         compiler.parameter("use_minmax", tex_mapping.use_minmax);
1119
1120         compiler.add(this, "node_mapping");
1121 }
1122
1123 /* Convert */
1124
1125 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_, bool autoconvert)
1126 : ShaderNode("convert")
1127 {
1128         from = from_;
1129         to = to_;
1130
1131         if(autoconvert)
1132                 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1133
1134         assert(from != to);
1135
1136         if(from == SHADER_SOCKET_FLOAT)
1137                 add_input("Val", SHADER_SOCKET_FLOAT);
1138         else if(from == SHADER_SOCKET_INT)
1139                 add_input("ValInt", SHADER_SOCKET_INT);
1140         else if(from == SHADER_SOCKET_COLOR)
1141                 add_input("Color", SHADER_SOCKET_COLOR);
1142         else if(from == SHADER_SOCKET_VECTOR)
1143                 add_input("Vector", SHADER_SOCKET_VECTOR);
1144         else if(from == SHADER_SOCKET_POINT)
1145                 add_input("Point", SHADER_SOCKET_POINT);
1146         else if(from == SHADER_SOCKET_NORMAL)
1147                 add_input("Normal", SHADER_SOCKET_NORMAL);
1148         else if(from == SHADER_SOCKET_STRING)
1149                 add_input("String", SHADER_SOCKET_STRING);
1150         else
1151                 assert(0);
1152
1153         if(to == SHADER_SOCKET_FLOAT)
1154                 add_output("Val", SHADER_SOCKET_FLOAT);
1155         else if(to == SHADER_SOCKET_INT)
1156                 add_output("ValInt", SHADER_SOCKET_INT);
1157         else if(to == SHADER_SOCKET_COLOR)
1158                 add_output("Color", SHADER_SOCKET_COLOR);
1159         else if(to == SHADER_SOCKET_VECTOR)
1160                 add_output("Vector", SHADER_SOCKET_VECTOR);
1161         else if(to == SHADER_SOCKET_POINT)
1162                 add_output("Point", SHADER_SOCKET_POINT);
1163         else if(to == SHADER_SOCKET_NORMAL)
1164                 add_output("Normal", SHADER_SOCKET_NORMAL);
1165         else if(to == SHADER_SOCKET_STRING)
1166                 add_output("String", SHADER_SOCKET_STRING);
1167         else
1168                 assert(0);
1169 }
1170
1171 void ConvertNode::compile(SVMCompiler& compiler)
1172 {
1173         ShaderInput *in = inputs[0];
1174         ShaderOutput *out = outputs[0];
1175
1176         if(from == SHADER_SOCKET_FLOAT) {
1177                 compiler.stack_assign(in);
1178                 compiler.stack_assign(out);
1179
1180                 if(to == SHADER_SOCKET_INT)
1181                         /* float to int */
1182                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, in->stack_offset, out->stack_offset);
1183                 else
1184                         /* float to float3 */
1185                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
1186         }
1187         else if(from == SHADER_SOCKET_INT) {
1188                 compiler.stack_assign(in);
1189                 compiler.stack_assign(out);
1190
1191                 if(to == SHADER_SOCKET_FLOAT)
1192                         /* int to float */
1193                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, in->stack_offset, out->stack_offset);
1194                 else
1195                         /* int to vector/point/normal */
1196                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, in->stack_offset, out->stack_offset);
1197         }
1198         else if(to == SHADER_SOCKET_FLOAT) {
1199                 compiler.stack_assign(in);
1200                 compiler.stack_assign(out);
1201
1202                 if(from == SHADER_SOCKET_COLOR)
1203                         /* color to float */
1204                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
1205                 else
1206                         /* vector/point/normal to float */
1207                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
1208         }
1209         else if(to == SHADER_SOCKET_INT) {
1210                 compiler.stack_assign(in);
1211                 compiler.stack_assign(out);
1212
1213                 if(from == SHADER_SOCKET_COLOR)
1214                         /* color to int */
1215                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, in->stack_offset, out->stack_offset);
1216                 else
1217                         /* vector/point/normal to int */
1218                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, in->stack_offset, out->stack_offset);
1219         }
1220         else {
1221                 /* float3 to float3 */
1222                 if(in->link) {
1223                         /* no op in SVM */
1224                         compiler.stack_link(in, out);
1225                 }
1226                 else {
1227                         /* set 0,0,0 value */
1228                         compiler.stack_assign(in);
1229                         compiler.stack_assign(out);
1230
1231                         compiler.add_node(NODE_VALUE_V, in->stack_offset);
1232                         compiler.add_node(NODE_VALUE_V, in->value);
1233                 }
1234         }
1235 }
1236
1237 void ConvertNode::compile(OSLCompiler& compiler)
1238 {
1239         if(from == SHADER_SOCKET_FLOAT)
1240                 compiler.add(this, "node_convert_from_float");
1241         else if(from == SHADER_SOCKET_INT)
1242                 compiler.add(this, "node_convert_from_int");
1243         else if(from == SHADER_SOCKET_COLOR)
1244                 compiler.add(this, "node_convert_from_color");
1245         else if(from == SHADER_SOCKET_VECTOR)
1246                 compiler.add(this, "node_convert_from_vector");
1247         else if(from == SHADER_SOCKET_POINT)
1248                 compiler.add(this, "node_convert_from_point");
1249         else if(from == SHADER_SOCKET_NORMAL)
1250                 compiler.add(this, "node_convert_from_normal");
1251         else
1252                 assert(0);
1253 }
1254
1255 /* Proxy */
1256
1257 ProxyNode::ProxyNode(ShaderSocketType type_)
1258 : ShaderNode("proxy")
1259 {
1260         type = type_;
1261         special_type = SHADER_SPECIAL_TYPE_PROXY;
1262
1263         add_input("Input", type);
1264         add_output("Output", type);
1265 }
1266
1267 void ProxyNode::compile(SVMCompiler& compiler)
1268 {
1269 }
1270
1271 void ProxyNode::compile(OSLCompiler& compiler)
1272 {
1273 }
1274
1275 /* BSDF Closure */
1276
1277 BsdfNode::BsdfNode(bool scattering_)
1278 : ShaderNode("bsdf"), scattering(scattering_)
1279 {
1280         closure = ccl::CLOSURE_BSSRDF_ID;
1281
1282         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1283         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
1284         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1285
1286         if(scattering)
1287                 add_output("BSSRDF", SHADER_SOCKET_CLOSURE);
1288         else
1289                 add_output("BSDF", SHADER_SOCKET_CLOSURE);
1290 }
1291
1292 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3)
1293 {
1294         ShaderInput *color_in = input("Color");
1295         ShaderInput *normal_in = input("Normal");
1296         ShaderInput *tangent_in = input("Tangent");
1297
1298         if(color_in->link) {
1299                 compiler.stack_assign(color_in);
1300                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1301         }
1302         else
1303                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1304         
1305         if(param1)
1306                 compiler.stack_assign(param1);
1307         if(param2)
1308                 compiler.stack_assign(param2);
1309         if(param3)
1310                 compiler.stack_assign(param3);
1311
1312         if(normal_in->link)
1313                 compiler.stack_assign(normal_in);
1314
1315         if(tangent_in && tangent_in->link)
1316                 compiler.stack_assign(tangent_in);
1317
1318         compiler.add_node(NODE_CLOSURE_BSDF,
1319                 compiler.encode_uchar4(closure,
1320                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1321                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1322                         compiler.closure_mix_weight_offset()),
1323                 __float_as_int((param1)? param1->value.x: 0.0f),
1324                 __float_as_int((param2)? param2->value.x: 0.0f));
1325
1326         if(tangent_in) {
1327                 compiler.add_node(NODE_CLOSURE_BSDF, normal_in->stack_offset, tangent_in->stack_offset,
1328                         (param3)? param3->stack_offset: SVM_STACK_INVALID);
1329         }
1330         else {
1331                 compiler.add_node(NODE_CLOSURE_BSDF, normal_in->stack_offset, SVM_STACK_INVALID,
1332                         (param3)? param3->stack_offset: SVM_STACK_INVALID);
1333         }
1334 }
1335
1336 void BsdfNode::compile(SVMCompiler& compiler)
1337 {
1338         compile(compiler, NULL, NULL);
1339 }
1340
1341 void BsdfNode::compile(OSLCompiler& compiler)
1342 {
1343         assert(0);
1344 }
1345
1346 /* Ward BSDF Closure */
1347
1348 WardBsdfNode::WardBsdfNode()
1349 {
1350         closure = CLOSURE_BSDF_WARD_ID;
1351
1352         add_input("Tangent", SHADER_SOCKET_VECTOR, ShaderInput::TANGENT);
1353
1354         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1355         add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.5f);
1356         add_input("Rotation", SHADER_SOCKET_FLOAT, 0.0f);
1357 }
1358
1359 void WardBsdfNode::attributes(AttributeRequestSet *attributes)
1360 {
1361         ShaderInput *tangent_in = input("Tangent");
1362
1363         if(!tangent_in->link)
1364                 attributes->add(ATTR_STD_GENERATED);
1365
1366         ShaderNode::attributes(attributes);
1367 }
1368
1369 void WardBsdfNode::compile(SVMCompiler& compiler)
1370 {
1371         BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1372 }
1373
1374 void WardBsdfNode::compile(OSLCompiler& compiler)
1375 {
1376         compiler.add(this, "node_ward_bsdf");
1377 }
1378
1379 /* Glossy BSDF Closure */
1380
1381 static ShaderEnum glossy_distribution_init()
1382 {
1383         ShaderEnum enm;
1384
1385         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1386         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1387         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1388
1389         return enm;
1390 }
1391
1392 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1393
1394 GlossyBsdfNode::GlossyBsdfNode()
1395 {
1396         distribution = ustring("Beckmann");
1397
1398         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1399 }
1400
1401 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1402 {
1403         closure = (ClosureType)distribution_enum[distribution];
1404
1405         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1406                 BsdfNode::compile(compiler, NULL, NULL);
1407         else
1408                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1409 }
1410
1411 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1412 {
1413         compiler.parameter("distribution", distribution);
1414         compiler.add(this, "node_glossy_bsdf");
1415 }
1416
1417 /* Glass BSDF Closure */
1418
1419 static ShaderEnum glass_distribution_init()
1420 {
1421         ShaderEnum enm;
1422
1423         enm.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1424         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1425         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1426
1427         return enm;
1428 }
1429
1430 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1431
1432 GlassBsdfNode::GlassBsdfNode()
1433 {
1434         distribution = ustring("Sharp");
1435
1436         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1437         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1438 }
1439
1440 void GlassBsdfNode::compile(SVMCompiler& compiler)
1441 {
1442         closure = (ClosureType)distribution_enum[distribution];
1443
1444         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
1445                 BsdfNode::compile(compiler, NULL, input("IOR"));
1446         else
1447                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1448 }
1449
1450 void GlassBsdfNode::compile(OSLCompiler& compiler)
1451 {
1452         compiler.parameter("distribution", distribution);
1453         compiler.add(this, "node_glass_bsdf");
1454 }
1455
1456 /* Refraction BSDF Closure */
1457
1458 static ShaderEnum refraction_distribution_init()
1459 {
1460         ShaderEnum enm;
1461
1462         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1463         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1464         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1465
1466         return enm;
1467 }
1468
1469 ShaderEnum RefractionBsdfNode::distribution_enum = refraction_distribution_init();
1470
1471 RefractionBsdfNode::RefractionBsdfNode()
1472 {
1473         distribution = ustring("Sharp");
1474
1475         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1476         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1477 }
1478
1479 void RefractionBsdfNode::compile(SVMCompiler& compiler)
1480 {
1481         closure = (ClosureType)distribution_enum[distribution];
1482
1483         if(closure == CLOSURE_BSDF_REFRACTION_ID)
1484                 BsdfNode::compile(compiler, NULL, input("IOR"));
1485         else
1486                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1487 }
1488
1489 void RefractionBsdfNode::compile(OSLCompiler& compiler)
1490 {
1491         compiler.parameter("distribution", distribution);
1492         compiler.add(this, "node_refraction_bsdf");
1493 }
1494
1495 /* Toon BSDF Closure */
1496
1497 static ShaderEnum toon_component_init()
1498 {
1499         ShaderEnum enm;
1500
1501         enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
1502         enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
1503
1504         return enm;
1505 }
1506
1507 ShaderEnum ToonBsdfNode::component_enum = toon_component_init();
1508
1509 ToonBsdfNode::ToonBsdfNode()
1510 {
1511         component = ustring("Diffuse");
1512
1513         add_input("Size", SHADER_SOCKET_FLOAT, 0.5f);
1514         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1515 }
1516
1517 void ToonBsdfNode::compile(SVMCompiler& compiler)
1518 {
1519         closure = (ClosureType)component_enum[component];
1520         
1521         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
1522 }
1523
1524 void ToonBsdfNode::compile(OSLCompiler& compiler)
1525 {
1526         compiler.parameter("component", component);
1527         compiler.add(this, "node_toon_bsdf");
1528 }
1529
1530 /* Velvet BSDF Closure */
1531
1532 VelvetBsdfNode::VelvetBsdfNode()
1533 {
1534         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1535
1536         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1537 }
1538
1539 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1540 {
1541         BsdfNode::compile(compiler, input("Sigma"), NULL);
1542 }
1543
1544 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1545 {
1546         compiler.add(this, "node_velvet_bsdf");
1547 }
1548
1549 /* Diffuse BSDF Closure */
1550
1551 DiffuseBsdfNode::DiffuseBsdfNode()
1552 {
1553         closure = CLOSURE_BSDF_DIFFUSE_ID;
1554         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1555 }
1556
1557 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1558 {
1559         BsdfNode::compile(compiler, input("Roughness"), NULL);
1560 }
1561
1562 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1563 {
1564         compiler.add(this, "node_diffuse_bsdf");
1565 }
1566
1567 /* Translucent BSDF Closure */
1568
1569 TranslucentBsdfNode::TranslucentBsdfNode()
1570 {
1571         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1572 }
1573
1574 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1575 {
1576         BsdfNode::compile(compiler, NULL, NULL);
1577 }
1578
1579 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1580 {
1581         compiler.add(this, "node_translucent_bsdf");
1582 }
1583
1584 /* Transparent BSDF Closure */
1585
1586 TransparentBsdfNode::TransparentBsdfNode()
1587 {
1588         name = "transparent";
1589         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1590 }
1591
1592 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1593 {
1594         BsdfNode::compile(compiler, NULL, NULL);
1595 }
1596
1597 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1598 {
1599         compiler.add(this, "node_transparent_bsdf");
1600 }
1601
1602 /* Subsurface Scattering Closure */
1603
1604 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
1605 : BsdfNode(true)
1606 {
1607         name = "subsurface_scattering";
1608         closure = CLOSURE_BSSRDF_ID;
1609
1610         add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
1611         add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
1612         add_input("IOR", SHADER_SOCKET_FLOAT, 1.3f);
1613 }
1614
1615 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
1616 {
1617         BsdfNode::compile(compiler, input("Scale"), input("IOR"), input("Radius"));
1618 }
1619
1620 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
1621 {
1622         compiler.add(this, "node_subsurface_scattering");
1623 }
1624
1625 /* Emissive Closure */
1626
1627 EmissionNode::EmissionNode()
1628 : ShaderNode("emission")
1629 {
1630         total_power = false;
1631
1632         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1633         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1634         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1635
1636         add_output("Emission", SHADER_SOCKET_CLOSURE);
1637 }
1638
1639 void EmissionNode::compile(SVMCompiler& compiler)
1640 {
1641         ShaderInput *color_in = input("Color");
1642         ShaderInput *strength_in = input("Strength");
1643
1644         if(color_in->link || strength_in->link) {
1645                 compiler.stack_assign(color_in);
1646                 compiler.stack_assign(strength_in);
1647                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1648         }
1649         else if(total_power)
1650                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1651         else
1652                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1653
1654         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1655 }
1656
1657 void EmissionNode::compile(OSLCompiler& compiler)
1658 {
1659         compiler.parameter("TotalPower", (total_power)? 1: 0);
1660         compiler.add(this, "node_emission");
1661 }
1662
1663 /* Background Closure */
1664
1665 BackgroundNode::BackgroundNode()
1666 : ShaderNode("background")
1667 {
1668         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1669         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1670         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1671
1672         add_output("Background", SHADER_SOCKET_CLOSURE);
1673 }
1674
1675 void BackgroundNode::compile(SVMCompiler& compiler)
1676 {
1677         ShaderInput *color_in = input("Color");
1678         ShaderInput *strength_in = input("Strength");
1679
1680         if(color_in->link || strength_in->link) {
1681                 compiler.stack_assign(color_in);
1682                 compiler.stack_assign(strength_in);
1683                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1684         }
1685         else
1686                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1687
1688         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1689 }
1690
1691 void BackgroundNode::compile(OSLCompiler& compiler)
1692 {
1693         compiler.add(this, "node_background");
1694 }
1695
1696 /* Holdout Closure */
1697
1698 HoldoutNode::HoldoutNode()
1699 : ShaderNode("holdout")
1700 {
1701         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1702         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1703
1704         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1705 }
1706
1707 void HoldoutNode::compile(SVMCompiler& compiler)
1708 {
1709         float3 value = make_float3(1.0f, 1.0f, 1.0f);
1710
1711         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
1712         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1713 }
1714
1715 void HoldoutNode::compile(OSLCompiler& compiler)
1716 {
1717         compiler.add(this, "node_holdout");
1718 }
1719
1720 /* Ambient Occlusion */
1721
1722 AmbientOcclusionNode::AmbientOcclusionNode()
1723 : ShaderNode("ambient_occlusion")
1724 {
1725         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1726         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1727         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1728
1729         add_output("AO", SHADER_SOCKET_CLOSURE);
1730 }
1731
1732 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
1733 {
1734         ShaderInput *color_in = input("Color");
1735
1736         if(color_in->link) {
1737                 compiler.stack_assign(color_in);
1738                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1739         }
1740         else
1741                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1742
1743         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
1744 }
1745
1746 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
1747 {
1748         compiler.add(this, "node_ambient_occlusion");
1749 }
1750
1751 /* Volume Closure */
1752
1753 VolumeNode::VolumeNode()
1754 : ShaderNode("volume")
1755 {
1756         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1757
1758         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1759         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1760         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1761
1762         add_output("Volume", SHADER_SOCKET_CLOSURE);
1763 }
1764
1765 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1766 {
1767         ShaderInput *color_in = input("Color");
1768
1769         if(color_in->link) {
1770                 compiler.stack_assign(color_in);
1771                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1772         }
1773         else
1774                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1775         
1776         if(param1)
1777                 compiler.stack_assign(param1);
1778         if(param2)
1779                 compiler.stack_assign(param2);
1780
1781         compiler.add_node(NODE_CLOSURE_VOLUME,
1782                 compiler.encode_uchar4(closure,
1783                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1784                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1785                         compiler.closure_mix_weight_offset()),
1786                 __float_as_int((param1)? param1->value.x: 0.0f),
1787                 __float_as_int((param2)? param2->value.x: 0.0f));
1788 }
1789
1790 void VolumeNode::compile(SVMCompiler& compiler)
1791 {
1792         compile(compiler, NULL, NULL);
1793 }
1794
1795 void VolumeNode::compile(OSLCompiler& compiler)
1796 {
1797         assert(0);
1798 }
1799
1800 /* Transparent Volume Closure */
1801
1802 TransparentVolumeNode::TransparentVolumeNode()
1803 {
1804         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1805 }
1806
1807 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1808 {
1809         VolumeNode::compile(compiler, input("Density"), NULL);
1810 }
1811
1812 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1813 {
1814         compiler.add(this, "node_isotropic_volume");
1815 }
1816
1817 /* Isotropic Volume Closure */
1818
1819 IsotropicVolumeNode::IsotropicVolumeNode()
1820 {
1821         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1822 }
1823
1824 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1825 {
1826         VolumeNode::compile(compiler, input("Density"), NULL);
1827 }
1828
1829 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1830 {
1831         compiler.add(this, "node_isotropic_volume");
1832 }
1833
1834 /* Geometry */
1835
1836 GeometryNode::GeometryNode()
1837 : ShaderNode("geometry")
1838 {
1839         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1840         add_output("Position", SHADER_SOCKET_POINT);
1841         add_output("Normal", SHADER_SOCKET_NORMAL);
1842         add_output("Tangent", SHADER_SOCKET_NORMAL);
1843         add_output("True Normal", SHADER_SOCKET_NORMAL);
1844         add_output("Incoming", SHADER_SOCKET_VECTOR);
1845         add_output("Parametric", SHADER_SOCKET_POINT);
1846         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1847 }
1848
1849 void GeometryNode::attributes(AttributeRequestSet *attributes)
1850 {
1851         if(!output("Tangent")->links.empty())
1852                 attributes->add(ATTR_STD_GENERATED);
1853
1854         ShaderNode::attributes(attributes);
1855 }
1856
1857 void GeometryNode::compile(SVMCompiler& compiler)
1858 {
1859         ShaderOutput *out;
1860         NodeType geom_node = NODE_GEOMETRY;
1861
1862         if(bump == SHADER_BUMP_DX)
1863                 geom_node = NODE_GEOMETRY_BUMP_DX;
1864         else if(bump == SHADER_BUMP_DY)
1865                 geom_node = NODE_GEOMETRY_BUMP_DY;
1866         
1867         out = output("Position");
1868         if(!out->links.empty()) {
1869                 compiler.stack_assign(out);
1870                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1871         }
1872
1873         out = output("Normal");
1874         if(!out->links.empty()) {
1875                 compiler.stack_assign(out);
1876                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1877         }
1878
1879         out = output("Tangent");
1880         if(!out->links.empty()) {
1881                 compiler.stack_assign(out);
1882                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1883         }
1884
1885         out = output("True Normal");
1886         if(!out->links.empty()) {
1887                 compiler.stack_assign(out);
1888                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1889         }
1890
1891         out = output("Incoming");
1892         if(!out->links.empty()) {
1893                 compiler.stack_assign(out);
1894                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1895         }
1896
1897         out = output("Parametric");
1898         if(!out->links.empty()) {
1899                 compiler.stack_assign(out);
1900                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1901         }
1902
1903         out = output("Backfacing");
1904         if(!out->links.empty()) {
1905                 compiler.stack_assign(out);
1906                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1907         }
1908 }
1909
1910 void GeometryNode::compile(OSLCompiler& compiler)
1911 {
1912         if(bump == SHADER_BUMP_DX)
1913                 compiler.parameter("bump_offset", "dx");
1914         else if(bump == SHADER_BUMP_DY)
1915                 compiler.parameter("bump_offset", "dy");
1916         else
1917                 compiler.parameter("bump_offset", "center");
1918
1919         compiler.add(this, "node_geometry");
1920 }
1921
1922 /* TextureCoordinate */
1923
1924 TextureCoordinateNode::TextureCoordinateNode()
1925 : ShaderNode("texture_coordinate")
1926 {
1927         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1928         add_output("Generated", SHADER_SOCKET_POINT);
1929         add_output("Normal", SHADER_SOCKET_NORMAL);
1930         add_output("UV", SHADER_SOCKET_POINT);
1931         add_output("Object", SHADER_SOCKET_POINT);
1932         add_output("Camera", SHADER_SOCKET_POINT);
1933         add_output("Window", SHADER_SOCKET_POINT);
1934         add_output("Reflection", SHADER_SOCKET_NORMAL);
1935
1936         from_dupli = false;
1937 }
1938
1939 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1940 {
1941         if(!from_dupli) {
1942                 if(!output("Generated")->links.empty())
1943                         attributes->add(ATTR_STD_GENERATED);
1944                 if(!output("UV")->links.empty())
1945                         attributes->add(ATTR_STD_UV);
1946         }
1947
1948         ShaderNode::attributes(attributes);
1949 }
1950
1951 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1952 {
1953         ShaderOutput *out;
1954         NodeType texco_node = NODE_TEX_COORD;
1955         NodeType attr_node = NODE_ATTR;
1956         NodeType geom_node = NODE_GEOMETRY;
1957
1958         if(bump == SHADER_BUMP_DX) {
1959                 texco_node = NODE_TEX_COORD_BUMP_DX;
1960                 attr_node = NODE_ATTR_BUMP_DX;
1961                 geom_node = NODE_GEOMETRY_BUMP_DX;
1962         }
1963         else if(bump == SHADER_BUMP_DY) {
1964                 texco_node = NODE_TEX_COORD_BUMP_DY;
1965                 attr_node = NODE_ATTR_BUMP_DY;
1966                 geom_node = NODE_GEOMETRY_BUMP_DY;
1967         }
1968         
1969         out = output("Generated");
1970         if(!out->links.empty()) {
1971                 if(compiler.background) {
1972                         compiler.stack_assign(out);
1973                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1974                 }
1975                 else {
1976                         if(from_dupli) {
1977                                 compiler.stack_assign(out);
1978                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
1979                         }
1980                         else {
1981                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
1982                                 compiler.stack_assign(out);
1983                                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1984                         }
1985                 }
1986         }
1987
1988         out = output("Normal");
1989         if(!out->links.empty()) {
1990                 compiler.stack_assign(out);
1991                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
1992         }
1993
1994         out = output("UV");
1995         if(!out->links.empty()) {
1996                 if(from_dupli) {
1997                         compiler.stack_assign(out);
1998                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
1999                 }
2000                 else {
2001                         int attr = compiler.attribute(ATTR_STD_UV);
2002                         compiler.stack_assign(out);
2003                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2004                 }
2005         }
2006
2007         out = output("Object");
2008         if(!out->links.empty()) {
2009                 compiler.stack_assign(out);
2010                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
2011         }
2012
2013         out = output("Camera");
2014         if(!out->links.empty()) {
2015                 compiler.stack_assign(out);
2016                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
2017         }
2018
2019         out = output("Window");
2020         if(!out->links.empty()) {
2021                 compiler.stack_assign(out);
2022                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
2023         }
2024
2025         out = output("Reflection");
2026         if(!out->links.empty()) {
2027                 if(compiler.background) {
2028                         compiler.stack_assign(out);
2029                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2030                 }
2031                 else {
2032                         compiler.stack_assign(out);
2033                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
2034                 }
2035         }
2036 }
2037
2038 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2039 {
2040         if(bump == SHADER_BUMP_DX)
2041                 compiler.parameter("bump_offset", "dx");
2042         else if(bump == SHADER_BUMP_DY)
2043                 compiler.parameter("bump_offset", "dy");
2044         else
2045                 compiler.parameter("bump_offset", "center");
2046         
2047         if(compiler.background)
2048                 compiler.parameter("is_background", true);
2049         
2050         compiler.parameter("from_dupli", from_dupli);
2051
2052         compiler.add(this, "node_texture_coordinate");
2053 }
2054
2055 /* Light Path */
2056
2057 LightPathNode::LightPathNode()
2058 : ShaderNode("light_path")
2059 {
2060         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
2061         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
2062         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
2063         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
2064         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
2065         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
2066         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
2067         add_output("Ray Length", SHADER_SOCKET_FLOAT);
2068         add_output("Ray Depth", SHADER_SOCKET_FLOAT);
2069 }
2070
2071 void LightPathNode::compile(SVMCompiler& compiler)
2072 {
2073         ShaderOutput *out;
2074
2075         out = output("Is Camera Ray");
2076         if(!out->links.empty()) {
2077                 compiler.stack_assign(out);
2078                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2079         }
2080
2081         out = output("Is Shadow Ray");
2082         if(!out->links.empty()) {
2083                 compiler.stack_assign(out);
2084                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2085         }
2086
2087         out = output("Is Diffuse Ray");
2088         if(!out->links.empty()) {
2089                 compiler.stack_assign(out);
2090                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2091         }
2092
2093         out = output("Is Glossy Ray");
2094         if(!out->links.empty()) {
2095                 compiler.stack_assign(out);
2096                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2097         }
2098
2099         out = output("Is Singular Ray");
2100         if(!out->links.empty()) {
2101                 compiler.stack_assign(out);
2102                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2103         }
2104
2105         out = output("Is Reflection Ray");
2106         if(!out->links.empty()) {
2107                 compiler.stack_assign(out);
2108                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2109         }
2110
2111
2112         out = output("Is Transmission Ray");
2113         if(!out->links.empty()) {
2114                 compiler.stack_assign(out);
2115                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2116         }
2117         
2118         out = output("Ray Length");
2119         if(!out->links.empty()) {
2120                 compiler.stack_assign(out);
2121                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2122         }
2123         
2124         out = output("Ray Depth");
2125         if(!out->links.empty()) {
2126                 compiler.stack_assign(out);
2127                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, out->stack_offset);
2128         }
2129
2130 }
2131
2132 void LightPathNode::compile(OSLCompiler& compiler)
2133 {
2134         compiler.add(this, "node_light_path");
2135 }
2136
2137 /* Light Falloff */
2138
2139 LightFalloffNode::LightFalloffNode()
2140 : ShaderNode("light_fallof")
2141 {
2142         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2143         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2144         add_output("Quadratic", SHADER_SOCKET_FLOAT);
2145         add_output("Linear", SHADER_SOCKET_FLOAT);
2146         add_output("Constant", SHADER_SOCKET_FLOAT);
2147 }
2148
2149 void LightFalloffNode::compile(SVMCompiler& compiler)
2150 {
2151         ShaderInput *strength_in = input("Strength");
2152         ShaderInput *smooth_in = input("Smooth");
2153
2154         compiler.stack_assign(strength_in);
2155         compiler.stack_assign(smooth_in);
2156
2157         ShaderOutput *out = output("Quadratic");
2158         if(!out->links.empty()) {
2159                 compiler.stack_assign(out);
2160                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2161                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2162         }
2163
2164         out = output("Linear");
2165         if(!out->links.empty()) {
2166                 compiler.stack_assign(out);
2167                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2168                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2169         }
2170
2171         out = output("Constant");
2172         if(!out->links.empty()) {
2173                 compiler.stack_assign(out);
2174                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2175                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2176         }
2177 }
2178
2179 void LightFalloffNode::compile(OSLCompiler& compiler)
2180 {
2181         compiler.add(this, "node_light_falloff");
2182 }
2183
2184 /* Object Info */
2185
2186 ObjectInfoNode::ObjectInfoNode()
2187 : ShaderNode("object_info")
2188 {
2189         add_output("Location", SHADER_SOCKET_VECTOR);
2190         add_output("Object Index", SHADER_SOCKET_FLOAT);
2191         add_output("Material Index", SHADER_SOCKET_FLOAT);
2192         add_output("Random", SHADER_SOCKET_FLOAT);
2193 }
2194
2195 void ObjectInfoNode::compile(SVMCompiler& compiler)
2196 {
2197         ShaderOutput *out = output("Location");
2198         if(!out->links.empty()) {
2199                 compiler.stack_assign(out);
2200                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2201         }
2202
2203         out = output("Object Index");
2204         if(!out->links.empty()) {
2205                 compiler.stack_assign(out);
2206                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2207         }
2208
2209         out = output("Material Index");
2210         if(!out->links.empty()) {
2211                 compiler.stack_assign(out);
2212                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2213         }
2214
2215         out = output("Random");
2216         if(!out->links.empty()) {
2217                 compiler.stack_assign(out);
2218                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2219         }
2220 }
2221
2222 void ObjectInfoNode::compile(OSLCompiler& compiler)
2223 {
2224         compiler.add(this, "node_object_info");
2225 }
2226
2227 /* Particle Info */
2228
2229 ParticleInfoNode::ParticleInfoNode()
2230 : ShaderNode("particle_info")
2231 {
2232         add_output("Index", SHADER_SOCKET_FLOAT);
2233         add_output("Age", SHADER_SOCKET_FLOAT);
2234         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2235         add_output("Location", SHADER_SOCKET_POINT);
2236         #if 0   /* not yet supported */
2237         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2238         #endif
2239         add_output("Size", SHADER_SOCKET_FLOAT);
2240         add_output("Velocity", SHADER_SOCKET_VECTOR);
2241         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2242 }
2243
2244 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
2245 {
2246         if(!output("Index")->links.empty())
2247                 attributes->add(ATTR_STD_PARTICLE);
2248         if(!output("Age")->links.empty())
2249                 attributes->add(ATTR_STD_PARTICLE);
2250         if(!output("Lifetime")->links.empty())
2251                 attributes->add(ATTR_STD_PARTICLE);
2252         if(!output("Location")->links.empty())
2253                 attributes->add(ATTR_STD_PARTICLE);
2254         #if 0   /* not yet supported */
2255         if(!output("Rotation")->links.empty())
2256                 attributes->add(ATTR_STD_PARTICLE);
2257         #endif
2258         if(!output("Size")->links.empty())
2259                 attributes->add(ATTR_STD_PARTICLE);
2260         if(!output("Velocity")->links.empty())
2261                 attributes->add(ATTR_STD_PARTICLE);
2262         if(!output("Angular Velocity")->links.empty())
2263                 attributes->add(ATTR_STD_PARTICLE);
2264
2265         ShaderNode::attributes(attributes);
2266 }
2267
2268 void ParticleInfoNode::compile(SVMCompiler& compiler)
2269 {
2270         ShaderOutput *out;
2271         
2272         out = output("Index");
2273         if(!out->links.empty()) {
2274                 compiler.stack_assign(out);
2275                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2276         }
2277         
2278         out = output("Age");
2279         if(!out->links.empty()) {
2280                 compiler.stack_assign(out);
2281                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2282         }
2283         
2284         out = output("Lifetime");
2285         if(!out->links.empty()) {
2286                 compiler.stack_assign(out);
2287                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2288         }
2289         
2290         out = output("Location");
2291         if(!out->links.empty()) {
2292                 compiler.stack_assign(out);
2293                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2294         }
2295         
2296         /* quaternion data is not yet supported by Cycles */
2297 #if 0
2298         out = output("Rotation");
2299         if(!out->links.empty()) {
2300                 compiler.stack_assign(out);
2301                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2302         }
2303 #endif
2304         
2305         out = output("Size");
2306         if(!out->links.empty()) {
2307                 compiler.stack_assign(out);
2308                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2309         }
2310         
2311         out = output("Velocity");
2312         if(!out->links.empty()) {
2313                 compiler.stack_assign(out);
2314                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2315         }
2316         
2317         out = output("Angular Velocity");
2318         if(!out->links.empty()) {
2319                 compiler.stack_assign(out);
2320                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2321         }
2322 }
2323
2324 void ParticleInfoNode::compile(OSLCompiler& compiler)
2325 {
2326         compiler.add(this, "node_particle_info");
2327 }
2328
2329 /* Hair Info */
2330
2331 HairInfoNode::HairInfoNode()
2332 : ShaderNode("hair_info")
2333 {
2334         add_output("Is Strand", SHADER_SOCKET_FLOAT);
2335         add_output("Intercept", SHADER_SOCKET_FLOAT);
2336         add_output("Thickness", SHADER_SOCKET_FLOAT);
2337         add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2338         /*output for minimum hair width transparency - deactivated*/
2339         /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2340 }
2341
2342 void HairInfoNode::attributes(AttributeRequestSet *attributes)
2343 {
2344         ShaderOutput *intercept_out = output("Intercept");
2345
2346         if(!intercept_out->links.empty())
2347                 attributes->add(ATTR_STD_CURVE_INTERCEPT);
2348         
2349         ShaderNode::attributes(attributes);
2350 }
2351
2352 void HairInfoNode::compile(SVMCompiler& compiler)
2353 {
2354         ShaderOutput *out;
2355         
2356         out = output("Is Strand");
2357         if(!out->links.empty()) {
2358                 compiler.stack_assign(out);
2359                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2360         }
2361
2362         out = output("Intercept");
2363         if(!out->links.empty()) {
2364                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2365                 compiler.stack_assign(out);
2366                 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2367         }
2368
2369         out = output("Thickness");
2370         if(!out->links.empty()) {
2371                 compiler.stack_assign(out);
2372                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2373         }
2374
2375         out = output("Tangent Normal");
2376         if(!out->links.empty()) {
2377                 compiler.stack_assign(out);
2378                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2379         }
2380
2381         /*out = output("Fade");
2382         if(!out->links.empty()) {
2383                 compiler.stack_assign(out);
2384                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2385         }*/
2386
2387 }
2388
2389 void HairInfoNode::compile(OSLCompiler& compiler)
2390 {
2391         compiler.add(this, "node_hair_info");
2392 }
2393
2394 /* Value */
2395
2396 ValueNode::ValueNode()
2397 : ShaderNode("value")
2398 {
2399         value = 0.0f;
2400
2401         add_output("Value", SHADER_SOCKET_FLOAT);
2402 }
2403
2404 void ValueNode::compile(SVMCompiler& compiler)
2405 {
2406         ShaderOutput *val_out = output("Value");
2407
2408         compiler.stack_assign(val_out);
2409         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2410 }
2411
2412 void ValueNode::compile(OSLCompiler& compiler)
2413 {
2414         compiler.parameter("value_value", value);
2415         compiler.add(this, "node_value");
2416 }
2417
2418 /* Color */
2419
2420 ColorNode::ColorNode()
2421 : ShaderNode("color")
2422 {
2423         value = make_float3(0.0f, 0.0f, 0.0f);
2424
2425         add_output("Color", SHADER_SOCKET_COLOR);
2426 }
2427
2428 void ColorNode::compile(SVMCompiler& compiler)
2429 {
2430         ShaderOutput *color_out = output("Color");
2431
2432         if(color_out && !color_out->links.empty()) {
2433                 compiler.stack_assign(color_out);
2434                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2435                 compiler.add_node(NODE_VALUE_V, value);
2436         }
2437 }
2438
2439 void ColorNode::compile(OSLCompiler& compiler)
2440 {
2441         compiler.parameter_color("color_value", value);
2442
2443         compiler.add(this, "node_value");
2444 }
2445
2446 /* Add Closure */
2447
2448 AddClosureNode::AddClosureNode()
2449 : ShaderNode("add_closure")
2450 {
2451         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2452         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2453         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2454 }
2455
2456 void AddClosureNode::compile(SVMCompiler& compiler)
2457 {
2458         /* handled in the SVM compiler */
2459 }
2460
2461 void AddClosureNode::compile(OSLCompiler& compiler)
2462 {
2463         compiler.add(this, "node_add_closure");
2464 }
2465
2466 /* Mix Closure */
2467
2468 MixClosureNode::MixClosureNode()
2469 : ShaderNode("mix_closure")
2470 {
2471         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2472         
2473         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2474         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2475         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2476         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2477 }
2478
2479 void MixClosureNode::compile(SVMCompiler& compiler)
2480 {
2481         /* handled in the SVM compiler */
2482 }
2483
2484 void MixClosureNode::compile(OSLCompiler& compiler)
2485 {
2486         compiler.add(this, "node_mix_closure");
2487 }
2488
2489 /* Mix Closure */
2490
2491 MixClosureWeightNode::MixClosureWeightNode()
2492 : ShaderNode("mix_closure_weight")
2493 {
2494         add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2495         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2496         add_output("Weight1", SHADER_SOCKET_FLOAT);
2497         add_output("Weight2", SHADER_SOCKET_FLOAT);
2498 }
2499
2500 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2501 {
2502         ShaderInput *weight_in = input("Weight");
2503         ShaderInput *fac_in = input("Fac");
2504         ShaderOutput *weight1_out = output("Weight1");
2505         ShaderOutput *weight2_out = output("Weight2");
2506
2507         compiler.stack_assign(weight_in);
2508         compiler.stack_assign(fac_in);
2509         compiler.stack_assign(weight1_out);
2510         compiler.stack_assign(weight2_out);
2511
2512         compiler.add_node(NODE_MIX_CLOSURE,
2513                 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2514                         weight1_out->stack_offset, weight2_out->stack_offset));
2515 }
2516
2517 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2518 {
2519         assert(0);
2520 }
2521
2522 /* Invert */
2523
2524 InvertNode::InvertNode()
2525 : ShaderNode("invert")
2526 {
2527         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2528         add_input("Color", SHADER_SOCKET_COLOR);
2529         add_output("Color",  SHADER_SOCKET_COLOR);
2530 }
2531
2532 void InvertNode::compile(SVMCompiler& compiler)
2533 {
2534         ShaderInput *fac_in = input("Fac");
2535         ShaderInput *color_in = input("Color");
2536         ShaderOutput *color_out = output("Color");
2537
2538         compiler.stack_assign(fac_in);
2539         compiler.stack_assign(color_in);
2540         compiler.stack_assign(color_out);
2541
2542         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2543 }
2544
2545 void InvertNode::compile(OSLCompiler& compiler)
2546 {
2547         compiler.add(this, "node_invert");
2548 }
2549
2550 /* Mix */
2551
2552 MixNode::MixNode()
2553 : ShaderNode("mix")
2554 {
2555         type = ustring("Mix");
2556
2557         use_clamp = false;
2558
2559         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2560         add_input("Color1", SHADER_SOCKET_COLOR);
2561         add_input("Color2", SHADER_SOCKET_COLOR);
2562         add_output("Color",  SHADER_SOCKET_COLOR);
2563 }
2564
2565 static ShaderEnum mix_type_init()
2566 {
2567         ShaderEnum enm;
2568
2569         enm.insert("Mix", NODE_MIX_BLEND);
2570         enm.insert("Add", NODE_MIX_ADD);
2571         enm.insert("Multiply", NODE_MIX_MUL);
2572         enm.insert("Screen", NODE_MIX_SCREEN);
2573         enm.insert("Overlay", NODE_MIX_OVERLAY);
2574         enm.insert("Subtract", NODE_MIX_SUB);
2575         enm.insert("Divide", NODE_MIX_DIV);
2576         enm.insert("Difference", NODE_MIX_DIFF);
2577         enm.insert("Darken", NODE_MIX_DARK);
2578         enm.insert("Lighten", NODE_MIX_LIGHT);
2579         enm.insert("Dodge", NODE_MIX_DODGE);
2580         enm.insert("Burn", NODE_MIX_BURN);
2581         enm.insert("Hue", NODE_MIX_HUE);
2582         enm.insert("Saturation", NODE_MIX_SAT);
2583         enm.insert("Value", NODE_MIX_VAL);
2584         enm.insert("Color", NODE_MIX_COLOR);
2585         enm.insert("Soft Light", NODE_MIX_SOFT);
2586         enm.insert("Linear Light", NODE_MIX_LINEAR);
2587
2588         return enm;
2589 }
2590
2591 ShaderEnum MixNode::type_enum = mix_type_init();
2592
2593 void MixNode::compile(SVMCompiler& compiler)
2594 {
2595         ShaderInput *fac_in = input("Fac");
2596         ShaderInput *color1_in = input("Color1");
2597         ShaderInput *color2_in = input("Color2");
2598         ShaderOutput *color_out = output("Color");
2599
2600         compiler.stack_assign(fac_in);
2601         compiler.stack_assign(color1_in);
2602         compiler.stack_assign(color2_in);
2603         compiler.stack_assign(color_out);
2604
2605         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2606         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2607
2608         if(use_clamp) {
2609                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2610                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2611         }
2612 }
2613
2614 void MixNode::compile(OSLCompiler& compiler)
2615 {
2616         compiler.parameter("type", type);
2617         compiler.parameter("Clamp", use_clamp);
2618         compiler.add(this, "node_mix");
2619 }
2620
2621 /* Combine RGB */
2622 CombineRGBNode::CombineRGBNode()
2623 : ShaderNode("combine_rgb")
2624 {
2625         add_input("R", SHADER_SOCKET_FLOAT);
2626         add_input("G", SHADER_SOCKET_FLOAT);
2627         add_input("B", SHADER_SOCKET_FLOAT);
2628         add_output("Image", SHADER_SOCKET_COLOR);
2629 }
2630
2631 void CombineRGBNode::compile(SVMCompiler& compiler)
2632 {
2633         ShaderInput *red_in = input("R");
2634         ShaderInput *green_in = input("G");
2635         ShaderInput *blue_in = input("B");
2636         ShaderOutput *color_out = output("Image");
2637
2638         compiler.stack_assign(color_out);
2639
2640         compiler.stack_assign(red_in);
2641         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2642
2643         compiler.stack_assign(green_in);
2644         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2645
2646         compiler.stack_assign(blue_in);
2647         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2648 }
2649
2650 void CombineRGBNode::compile(OSLCompiler& compiler)
2651 {
2652         compiler.add(this, "node_combine_rgb");
2653 }
2654
2655 /* Combine HSV */
2656 CombineHSVNode::CombineHSVNode()
2657 : ShaderNode("combine_hsv")
2658 {
2659         add_input("H", SHADER_SOCKET_FLOAT);
2660         add_input("S", SHADER_SOCKET_FLOAT);
2661         add_input("V", SHADER_SOCKET_FLOAT);
2662         add_output("Color", SHADER_SOCKET_COLOR);
2663 }
2664
2665 void CombineHSVNode::compile(SVMCompiler& compiler)
2666 {
2667         ShaderInput *hue_in = input("H");
2668         ShaderInput *saturation_in = input("S");
2669         ShaderInput *value_in = input("V");
2670         ShaderOutput *color_out = output("Color");
2671
2672         compiler.stack_assign(color_out);
2673         compiler.stack_assign(hue_in);
2674         compiler.stack_assign(saturation_in);
2675         compiler.stack_assign(value_in);
2676         
2677         compiler.add_node(NODE_COMBINE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2678         compiler.add_node(NODE_COMBINE_HSV, color_out->stack_offset);
2679 }
2680
2681 void CombineHSVNode::compile(OSLCompiler& compiler)
2682 {
2683         compiler.add(this, "node_combine_hsv");
2684 }
2685
2686 /* Gamma */
2687 GammaNode::GammaNode()
2688 : ShaderNode("gamma")
2689 {
2690         add_input("Color", SHADER_SOCKET_COLOR);
2691         add_input("Gamma", SHADER_SOCKET_FLOAT);
2692         add_output("Color", SHADER_SOCKET_COLOR);
2693 }
2694
2695 void GammaNode::compile(SVMCompiler& compiler)
2696 {
2697         ShaderInput *color_in = input("Color");
2698         ShaderInput *gamma_in = input("Gamma");
2699         ShaderOutput *color_out = output("Color");
2700
2701         compiler.stack_assign(color_in);
2702         compiler.stack_assign(gamma_in);
2703         compiler.stack_assign(color_out);
2704
2705         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2706 }
2707
2708 void GammaNode::compile(OSLCompiler& compiler)
2709 {
2710         compiler.add(this, "node_gamma");
2711 }
2712
2713 /* Bright Contrast */
2714 BrightContrastNode::BrightContrastNode()
2715 : ShaderNode("brightness")
2716 {
2717         add_input("Color", SHADER_SOCKET_COLOR);
2718         add_input("Bright", SHADER_SOCKET_FLOAT);
2719         add_input("Contrast", SHADER_SOCKET_FLOAT);
2720         add_output("Color", SHADER_SOCKET_COLOR);
2721 }
2722
2723 void BrightContrastNode::compile(SVMCompiler& compiler)
2724 {
2725         ShaderInput *color_in = input("Color");
2726         ShaderInput *bright_in = input("Bright");
2727         ShaderInput *contrast_in = input("Contrast");
2728         ShaderOutput *color_out = output("Color");
2729
2730         compiler.stack_assign(color_in);
2731         compiler.stack_assign(bright_in);
2732         compiler.stack_assign(contrast_in);
2733         compiler.stack_assign(color_out);
2734
2735         compiler.add_node(NODE_BRIGHTCONTRAST,
2736                 color_in->stack_offset, color_out->stack_offset,
2737                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2738 }
2739
2740 void BrightContrastNode::compile(OSLCompiler& compiler)
2741 {
2742         compiler.add(this, "node_brightness");
2743 }
2744
2745 /* Separate RGB */
2746 SeparateRGBNode::SeparateRGBNode()
2747 : ShaderNode("separate_rgb")
2748 {
2749         add_input("Image", SHADER_SOCKET_COLOR);
2750         add_output("R", SHADER_SOCKET_FLOAT);
2751         add_output("G", SHADER_SOCKET_FLOAT);
2752         add_output("B", SHADER_SOCKET_FLOAT);
2753 }
2754
2755 void SeparateRGBNode::compile(SVMCompiler& compiler)
2756 {
2757         ShaderInput *color_in = input("Image");
2758         ShaderOutput *red_out = output("R");
2759         ShaderOutput *green_out = output("G");
2760         ShaderOutput *blue_out = output("B");
2761
2762         compiler.stack_assign(color_in);
2763
2764         compiler.stack_assign(red_out);
2765         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2766
2767         compiler.stack_assign(green_out);
2768         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2769
2770         compiler.stack_assign(blue_out);
2771         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2772 }
2773
2774 void SeparateRGBNode::compile(OSLCompiler& compiler)
2775 {
2776         compiler.add(this, "node_separate_rgb");
2777 }
2778
2779 /* Separate HSV */
2780 SeparateHSVNode::SeparateHSVNode()
2781 : ShaderNode("separate_hsv")
2782 {
2783         add_input("Color", SHADER_SOCKET_COLOR);
2784         add_output("H", SHADER_SOCKET_FLOAT);
2785         add_output("S", SHADER_SOCKET_FLOAT);
2786         add_output("V", SHADER_SOCKET_FLOAT);
2787 }
2788
2789 void SeparateHSVNode::compile(SVMCompiler& compiler)
2790 {
2791         ShaderInput *color_in = input("Color");
2792         ShaderOutput *hue_out = output("H");
2793         ShaderOutput *saturation_out = output("S");
2794         ShaderOutput *value_out = output("V");
2795
2796         compiler.stack_assign(color_in);
2797         compiler.stack_assign(hue_out);
2798         compiler.stack_assign(saturation_out);
2799         compiler.stack_assign(value_out);
2800         
2801         compiler.add_node(NODE_SEPARATE_HSV, color_in->stack_offset, hue_out->stack_offset, saturation_out->stack_offset);
2802         compiler.add_node(NODE_SEPARATE_HSV, value_out->stack_offset);
2803
2804 }
2805
2806 void SeparateHSVNode::compile(OSLCompiler& compiler)
2807 {
2808         compiler.add(this, "node_separate_hsv");
2809 }
2810
2811 /* Hue Saturation Value */
2812 HSVNode::HSVNode()
2813 : ShaderNode("hsv")
2814 {
2815         add_input("Hue", SHADER_SOCKET_FLOAT);
2816         add_input("Saturation", SHADER_SOCKET_FLOAT);
2817         add_input("Value", SHADER_SOCKET_FLOAT);
2818         add_input("Fac", SHADER_SOCKET_FLOAT);
2819         add_input("Color", SHADER_SOCKET_COLOR);
2820         add_output("Color", SHADER_SOCKET_COLOR);
2821 }
2822
2823 void HSVNode::compile(SVMCompiler& compiler)
2824 {
2825         ShaderInput *hue_in = input("Hue");
2826         ShaderInput *saturation_in = input("Saturation");
2827         ShaderInput *value_in = input("Value");
2828         ShaderInput *fac_in = input("Fac");
2829         ShaderInput *color_in = input("Color");
2830         ShaderOutput *color_out = output("Color");
2831
2832         compiler.stack_assign(hue_in);
2833         compiler.stack_assign(saturation_in);
2834         compiler.stack_assign(value_in);
2835         compiler.stack_assign(fac_in);
2836         compiler.stack_assign(color_in);
2837         compiler.stack_assign(color_out);
2838
2839         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2840         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2841 }
2842
2843 void HSVNode::compile(OSLCompiler& compiler)
2844 {
2845         compiler.add(this, "node_hsv");
2846 }
2847
2848 /* Attribute */
2849
2850 AttributeNode::AttributeNode()
2851 : ShaderNode("attribute")
2852 {
2853         attribute = "";
2854
2855         add_output("Color",  SHADER_SOCKET_COLOR);
2856         add_output("Vector",  SHADER_SOCKET_VECTOR);
2857         add_output("Fac",  SHADER_SOCKET_FLOAT);
2858 }
2859
2860 void AttributeNode::attributes(AttributeRequestSet *attributes)
2861 {
2862         ShaderOutput *color_out = output("Color");
2863         ShaderOutput *vector_out = output("Vector");
2864         ShaderOutput *fac_out = output("Fac");
2865
2866         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2867                 attributes->add(attribute);
2868         
2869         ShaderNode::attributes(attributes);
2870 }
2871
2872 void AttributeNode::compile(SVMCompiler& compiler)
2873 {
2874         ShaderOutput *color_out = output("Color");
2875         ShaderOutput *vector_out = output("Vector");
2876         ShaderOutput *fac_out = output("Fac");
2877         NodeType attr_node = NODE_ATTR;
2878
2879         if(bump == SHADER_BUMP_DX)
2880                 attr_node = NODE_ATTR_BUMP_DX;
2881         else if(bump == SHADER_BUMP_DY)
2882                 attr_node = NODE_ATTR_BUMP_DY;
2883
2884         if(!color_out->links.empty() || !vector_out->links.empty()) {
2885                 int attr = compiler.attribute(attribute);
2886
2887                 if(!color_out->links.empty()) {
2888                         compiler.stack_assign(color_out);
2889                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2890                 }
2891                 if(!vector_out->links.empty()) {
2892                         compiler.stack_assign(vector_out);
2893                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2894                 }
2895         }
2896
2897         if(!fac_out->links.empty()) {
2898                 int attr = compiler.attribute(attribute);
2899
2900                 compiler.stack_assign(fac_out);
2901                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2902         }
2903 }
2904
2905 void AttributeNode::compile(OSLCompiler& compiler)
2906 {
2907         if(bump == SHADER_BUMP_DX)
2908                 compiler.parameter("bump_offset", "dx");
2909         else if(bump == SHADER_BUMP_DY)
2910                 compiler.parameter("bump_offset", "dy");
2911         else
2912                 compiler.parameter("bump_offset", "center");
2913
2914         compiler.parameter("name", attribute.c_str());
2915         compiler.add(this, "node_attribute");
2916 }
2917
2918 /* Camera */
2919
2920 CameraNode::CameraNode()
2921 : ShaderNode("camera")
2922 {
2923         add_output("View Vector",  SHADER_SOCKET_VECTOR);
2924         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
2925         add_output("View Distance",  SHADER_SOCKET_FLOAT);
2926 }
2927
2928 void CameraNode::compile(SVMCompiler& compiler)
2929 {
2930         ShaderOutput *vector_out = output("View Vector");
2931         ShaderOutput *z_depth_out = output("View Z Depth");
2932         ShaderOutput *distance_out = output("View Distance");
2933
2934         compiler.stack_assign(vector_out);
2935         compiler.stack_assign(z_depth_out);
2936         compiler.stack_assign(distance_out);
2937         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2938 }
2939
2940 void CameraNode::compile(OSLCompiler& compiler)
2941 {
2942         compiler.add(this, "node_camera");
2943 }
2944
2945 /* Fresnel */
2946
2947 FresnelNode::FresnelNode()
2948 : ShaderNode("fresnel")
2949 {
2950         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2951         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2952         add_output("Fac", SHADER_SOCKET_FLOAT);
2953 }
2954
2955 void FresnelNode::compile(SVMCompiler& compiler)
2956 {
2957         ShaderInput *ior_in = input("IOR");
2958         ShaderOutput *fac_out = output("Fac");
2959
2960         compiler.stack_assign(ior_in);
2961         compiler.stack_assign(fac_out);
2962         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2963 }
2964
2965 void FresnelNode::compile(OSLCompiler& compiler)
2966 {
2967         compiler.add(this, "node_fresnel");
2968 }
2969
2970 /* Layer Weight */
2971
2972 LayerWeightNode::LayerWeightNode()
2973 : ShaderNode("layer_weight")
2974 {
2975         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2976         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2977
2978         add_output("Fresnel", SHADER_SOCKET_FLOAT);
2979         add_output("Facing", SHADER_SOCKET_FLOAT);
2980 }
2981
2982 void LayerWeightNode::compile(SVMCompiler& compiler)
2983 {
2984         ShaderInput *blend_in = input("Blend");
2985
2986         if(blend_in->link)
2987                 compiler.stack_assign(blend_in);
2988
2989         ShaderOutput *fresnel_out = output("Fresnel");
2990         if(!fresnel_out->links.empty()) {
2991                 compiler.stack_assign(fresnel_out);
2992                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2993                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
2994         }
2995
2996         ShaderOutput *facing_out = output("Facing");
2997         if(!facing_out->links.empty()) {
2998                 compiler.stack_assign(facing_out);
2999                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3000                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
3001         }
3002 }
3003
3004 void LayerWeightNode::compile(OSLCompiler& compiler)
3005 {
3006         compiler.add(this, "node_layer_weight");
3007 }
3008
3009 /* Wireframe */
3010
3011 WireframeNode::WireframeNode()
3012 : ShaderNode("wireframe")
3013 {
3014         add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
3015         add_output("Fac", SHADER_SOCKET_FLOAT);
3016         
3017         use_pixel_size = false;
3018 }
3019
3020 void WireframeNode::compile(SVMCompiler& compiler)
3021 {
3022         ShaderInput *size_in = input("Size");
3023         ShaderOutput *fac_out = output("Fac");
3024
3025         compiler.stack_assign(size_in);
3026         compiler.stack_assign(fac_out);
3027         compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
3028 }
3029
3030 void WireframeNode::compile(OSLCompiler& compiler)
3031 {
3032         compiler.parameter("use_pixel_size", use_pixel_size);
3033         compiler.add(this, "node_wireframe");
3034 }
3035
3036 /* Wavelength */
3037
3038 WavelengthNode::WavelengthNode()
3039 : ShaderNode("wavelength")
3040 {
3041         add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
3042         add_output("Color", SHADER_SOCKET_COLOR);
3043 }
3044
3045 void WavelengthNode::compile(SVMCompiler& compiler)
3046 {
3047         ShaderInput *wavelength_in = input("Wavelength");
3048         ShaderOutput *color_out = output("Color");
3049
3050         compiler.stack_assign(wavelength_in);
3051         compiler.stack_assign(color_out);
3052         compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset);
3053 }
3054
3055 void WavelengthNode::compile(OSLCompiler& compiler)
3056 {
3057         compiler.add(this, "node_wavelength");
3058 }
3059
3060 /* Blackbody */
3061
3062 BlackbodyNode::BlackbodyNode()
3063 : ShaderNode("blackbody")
3064 {
3065         add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f);
3066         add_output("Color", SHADER_SOCKET_COLOR);
3067 }
3068
3069 void BlackbodyNode::compile(SVMCompiler& compiler)
3070 {
3071         ShaderInput *temperature_in = input("Temperature");
3072         ShaderOutput *color_out = output("Color");
3073
3074         compiler.stack_assign(temperature_in);
3075         compiler.stack_assign(color_out);
3076         compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset);
3077 }
3078
3079 void BlackbodyNode::compile(OSLCompiler& compiler)
3080 {
3081         compiler.add(this, "node_blackbody");
3082 }
3083
3084 /* Output */
3085
3086 OutputNode::OutputNode()
3087 : ShaderNode("output")
3088 {
3089         add_input("Surface", SHADER_SOCKET_CLOSURE);
3090         add_input("Volume", SHADER_SOCKET_CLOSURE);
3091         add_input("Displacement", SHADER_SOCKET_FLOAT);
3092         add_input("Normal", SHADER_SOCKET_NORMAL);
3093 }
3094
3095 void OutputNode::compile(SVMCompiler& compiler)
3096 {
3097         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
3098                 ShaderInput *displacement_in = input("Displacement");
3099
3100                 if(displacement_in->link) {
3101                         compiler.stack_assign(displacement_in);
3102                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
3103                 }
3104         }
3105 }
3106
3107 void OutputNode::compile(OSLCompiler& compiler)
3108 {
3109         if(compiler.output_type() == SHADER_TYPE_SURFACE)
3110                 compiler.add(this, "node_output_surface");
3111         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3112                 compiler.add(this, "node_output_volume");
3113         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3114                 compiler.add(this, "node_output_displacement");
3115 }
3116
3117 /* Math */
3118
3119 MathNode::MathNode()
3120 : ShaderNode("math")
3121 {
3122         type = ustring("Add");
3123
3124         use_clamp = false;
3125
3126         add_input("Value1", SHADER_SOCKET_FLOAT);
3127         add_input("Value2", SHADER_SOCKET_FLOAT);
3128         add_output("Value",  SHADER_SOCKET_FLOAT);
3129 }
3130
3131 static ShaderEnum math_type_init()
3132 {
3133         ShaderEnum enm;
3134
3135         enm.insert("Add", NODE_MATH_ADD);
3136         enm.insert("Subtract", NODE_MATH_SUBTRACT);
3137         enm.insert("Multiply", NODE_MATH_MULTIPLY);
3138         enm.insert("Divide", NODE_MATH_DIVIDE);
3139         enm.insert("Sine", NODE_MATH_SINE);
3140         enm.insert("Cosine", NODE_MATH_COSINE);
3141         enm.insert("Tangent", NODE_MATH_TANGENT);
3142         enm.insert("Arcsine", NODE_MATH_ARCSINE);
3143         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3144         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3145         enm.insert("Power", NODE_MATH_POWER);
3146         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3147         enm.insert("Minimum", NODE_MATH_MINIMUM);
3148         enm.insert("Maximum", NODE_MATH_MAXIMUM);
3149         enm.insert("Round", NODE_MATH_ROUND);
3150         enm.insert("Less Than", NODE_MATH_LESS_THAN);
3151         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3152         enm.insert("Modulo", NODE_MATH_MODULO);
3153
3154         return enm;
3155 }
3156
3157 ShaderEnum MathNode::type_enum = math_type_init();
3158
3159 void MathNode::compile(SVMCompiler& compiler)
3160 {
3161         ShaderInput *value1_in = input("Value1");
3162         ShaderInput *value2_in = input("Value2");
3163         ShaderOutput *value_out = output("Value");
3164
3165         compiler.stack_assign(value1_in);
3166         compiler.stack_assign(value2_in);
3167         compiler.stack_assign(value_out);
3168
3169         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3170         compiler.add_node(NODE_MATH, value_out->stack_offset);
3171
3172         if(use_clamp) {
3173                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3174                 compiler.add_node(NODE_MATH, value_out->stack_offset);
3175         }
3176 }
3177
3178 void MathNode::compile(OSLCompiler& compiler)
3179 {
3180         compiler.parameter("type", type);
3181         compiler.parameter("Clamp", use_clamp);
3182         compiler.add(this, "node_math");
3183 }
3184
3185 /* VectorMath */
3186
3187 VectorMathNode::VectorMathNode()
3188 : ShaderNode("vector_math")
3189 {
3190         type = ustring("Add");
3191
3192         add_input("Vector1", SHADER_SOCKET_VECTOR);
3193         add_input("Vector2", SHADER_SOCKET_VECTOR);
3194         add_output("Value",  SHADER_SOCKET_FLOAT);
3195         add_output("Vector",  SHADER_SOCKET_VECTOR);
3196 }
3197
3198 static ShaderEnum vector_math_type_init()
3199 {
3200         ShaderEnum enm;
3201
3202         enm.insert("Add", NODE_VECTOR_MATH_ADD);
3203         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3204         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3205         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3206         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3207         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3208
3209         return enm;
3210 }
3211
3212 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3213
3214 void VectorMathNode::compile(SVMCompiler& compiler)
3215 {
3216         ShaderInput *vector1_in = input("Vector1");
3217         ShaderInput *vector2_in = input("Vector2");
3218         ShaderOutput *value_out = output("Value");
3219         ShaderOutput *vector_out = output("Vector");
3220
3221         compiler.stack_assign(vector1_in);
3222         compiler.stack_assign(vector2_in);
3223         compiler.stack_assign(value_out);
3224         compiler.stack_assign(vector_out);
3225
3226         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
3227         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
3228 }
3229
3230 void VectorMathNode::compile(OSLCompiler& compiler)
3231 {
3232         compiler.parameter("type", type);
3233         compiler.add(this, "node_vector_math");
3234 }
3235
3236 /* VectorTransform */
3237
3238 VectorTransformNode::VectorTransformNode()
3239 : ShaderNode("vector_transform")
3240 {
3241         type = ustring("Vector");
3242         convert_from = ustring("world");
3243         convert_to = ustring("object");
3244
3245         add_input("Vector", SHADER_SOCKET_VECTOR);
3246         add_output("Vector",  SHADER_SOCKET_VECTOR);
3247 }
3248
3249 static ShaderEnum vector_transform_type_init()
3250 {
3251         ShaderEnum enm;
3252
3253         enm.insert("Vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
3254         enm.insert("Point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
3255         enm.insert("Normal", NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
3256
3257         return enm;
3258 }
3259
3260 static ShaderEnum vector_transform_convert_space_init()
3261 {
3262         ShaderEnum enm;
3263
3264         enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
3265         enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
3266         enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA);
3267
3268         return enm;
3269 }
3270
3271 ShaderEnum VectorTransformNode::type_enum = vector_transform_type_init();
3272 ShaderEnum VectorTransformNode::convert_space_enum = vector_transform_convert_space_init();
3273
3274 void VectorTransformNode::compile(SVMCompiler& compiler)
3275 {
3276         ShaderInput *vector_in = input("Vector");
3277         ShaderOutput *vector_out = output("Vector");
3278
3279         compiler.stack_assign(vector_in);
3280         compiler.stack_assign(vector_out);
3281
3282         compiler.add_node(NODE_VECTOR_TRANSFORM,
3283                 compiler.encode_uchar4(type_enum[type], convert_space_enum[convert_from], convert_space_enum[convert_to]),
3284                 compiler.encode_uchar4(vector_in->stack_offset, vector_out->stack_offset));
3285 }
3286
3287 void VectorTransformNode::compile(OSLCompiler& compiler)
3288 {
3289         compiler.parameter("type", type);
3290         compiler.parameter("convert_from", convert_from);
3291         compiler.parameter("convert_to", convert_to);
3292         compiler.add(this, "node_vector_transform");
3293 }
3294
3295 /* BumpNode */
3296
3297 BumpNode::BumpNode()
3298 : ShaderNode("bump")
3299 {
3300         invert = false;
3301
3302         /* this input is used by the user, but after graph transform it is no longer
3303          * used and moved to sampler center/x/y instead */
3304         add_input("Height", SHADER_SOCKET_FLOAT);
3305
3306         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
3307         add_input("SampleX", SHADER_SOCKET_FLOAT);
3308         add_input("SampleY", SHADER_SOCKET_FLOAT);
3309         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
3310         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3311         add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
3312
3313         add_output("Normal", SHADER_SOCKET_NORMAL);
3314 }
3315
3316 void BumpNode::compile(SVMCompiler& compiler)
3317 {
3318         ShaderInput *center_in = input("SampleCenter");
3319         ShaderInput *dx_in = input("SampleX");
3320         ShaderInput *dy_in = input("SampleY");
3321         ShaderInput *normal_in = input("Normal");
3322         ShaderInput *strength_in = input("Strength");
3323         ShaderInput *distance_in = input("Distance");
3324         ShaderOutput *normal_out = output("Normal");
3325
3326         compiler.stack_assign(center_in);
3327         compiler.stack_assign(dx_in);
3328         compiler.stack_assign(dy_in);
3329         compiler.stack_assign(strength_in);
3330         compiler.stack_assign(distance_in);
3331         compiler.stack_assign(normal_out);
3332
3333         if(normal_in->link)
3334                 compiler.stack_assign(normal_in);
3335         
3336         /* pack all parameters in the node */
3337         compiler.add_node(NODE_SET_BUMP,
3338                 compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
3339                 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
3340                         dy_in->stack_offset, strength_in->stack_offset),
3341                 normal_out->stack_offset);
3342 }
3343
3344 void BumpNode::compile(OSLCompiler& compiler)
3345 {
3346         compiler.parameter("invert", invert);
3347         compiler.add(this, "node_bump");
3348 }
3349
3350 /* RGBCurvesNode */
3351
3352 RGBCurvesNode::RGBCurvesNode()
3353 : ShaderNode("rgb_curves")
3354 {
3355         add_input("Fac", SHADER_SOCKET_FLOAT);
3356         add_input("Color", SHADER_SOCKET_COLOR);
3357         add_output("Color", SHADER_SOCKET_COLOR);
3358 }
3359
3360 void RGBCurvesNode::compile(SVMCompiler& compiler)
3361 {
3362         ShaderInput *fac_in = input("Fac");
3363         ShaderInput *color_in = input("Color");
3364         ShaderOutput *color_out = output("Color");
3365
3366         compiler.stack_assign(fac_in);
3367         compiler.stack_assign(color_in);
3368         compiler.stack_assign(color_out);
3369
3370         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3371         compiler.add_array(curves, RAMP_TABLE_SIZE);
3372 }
3373
3374 void RGBCurvesNode::compile(OSLCompiler& compiler)
3375 {
3376         float ramp[RAMP_TABLE_SIZE][3];
3377
3378         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3379                 ramp[i][0] = curves[i].x;
3380                 ramp[i][1] = curves[i].y;
3381                 ramp[i][2] = curves[i].z;
3382         }
3383
3384         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3385         compiler.add(this, "node_rgb_curves");
3386 }
3387
3388 /* VectorCurvesNode */
3389
3390 VectorCurvesNode::VectorCurvesNode()
3391 : ShaderNode("vector_curves")
3392 {
3393         add_input("Fac", SHADER_SOCKET_FLOAT);
3394         add_input("Vector", SHADER_SOCKET_VECTOR);
3395         add_output("Vector", SHADER_SOCKET_VECTOR);
3396 }
3397
3398 void VectorCurvesNode::compile(SVMCompiler& compiler)
3399 {
3400         ShaderInput *fac_in = input("Fac");
3401         ShaderInput *vector_in = input("Vector");
3402         ShaderOutput *vector_out = output("Vector");
3403
3404         compiler.stack_assign(fac_in);
3405         compiler.stack_assign(vector_in);
3406         compiler.stack_assign(vector_out);
3407
3408         compiler.add_node(NODE_VECTOR_CURVES, fac_in->stack_offset, vector_in->stack_offset, vector_out->stack_offset);
3409         compiler.add_array(curves, RAMP_TABLE_SIZE);
3410 }
3411
3412 void VectorCurvesNode::compile(OSLCompiler& compiler)
3413 {
3414         float ramp[RAMP_TABLE_SIZE][3];
3415
3416         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3417                 ramp[i][0] = curves[i].x;
3418                 ramp[i][1] = curves[i].y;
3419                 ramp[i][2] = curves[i].z;
3420         }
3421
3422         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3423         compiler.add(this, "node_vector_curves");
3424 }
3425
3426 /* RGBRampNode */
3427
3428 RGBRampNode::RGBRampNode()
3429 : ShaderNode("rgb_ramp")
3430 {
3431         add_input("Fac", SHADER_SOCKET_FLOAT);
3432         add_output("Color", SHADER_SOCKET_COLOR);
3433         add_output("Alpha", SHADER_SOCKET_FLOAT);
3434
3435         interpolate = true;
3436 }
3437
3438 void RGBRampNode::compile(SVMCompiler& compiler)
3439 {
3440         ShaderInput *fac_in = input("Fac");
3441         ShaderOutput *color_out = output("Color");
3442         ShaderOutput *alpha_out = output("Alpha");
3443
3444         compiler.stack_assign(fac_in);
3445         if(!color_out->links.empty())
3446                 compiler.stack_assign(color_out);
3447         if(!alpha_out->links.empty())
3448                 compiler.stack_assign(alpha_out);
3449
3450         compiler.add_node(NODE_RGB_RAMP,
3451                 compiler.encode_uchar4(
3452                         fac_in->stack_offset,
3453                         color_out->stack_offset,
3454                         alpha_out->stack_offset),
3455                 interpolate);
3456         compiler.add_array(ramp, RAMP_TABLE_SIZE);
3457 }
3458
3459 void RGBRampNode::compile(OSLCompiler& compiler)
3460 {
3461         /* OSL shader only takes separate RGB and A array, split the RGBA base array */
3462         /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
3463         float ramp_color[RAMP_TABLE_SIZE][3];
3464         float ramp_alpha[RAMP_TABLE_SIZE];
3465
3466         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3467                 ramp_color[i][0] = ramp[i].x;
3468                 ramp_color[i][1] = ramp[i].y;
3469                 ramp_color[i][2] = ramp[i].z;
3470                 ramp_alpha[i] = ramp[i].w;
3471         }
3472
3473         compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
3474         compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
3475         compiler.parameter("ramp_interpolate", interpolate);
3476         
3477         compiler.add(this, "node_rgb_ramp");
3478 }
3479
3480 /* Set Normal Node */
3481
3482 SetNormalNode::SetNormalNode()
3483 : ShaderNode("set_normal")
3484 {
3485         add_input("Direction", SHADER_SOCKET_VECTOR);
3486         add_output("Normal", SHADER_SOCKET_NORMAL);
3487 }
3488
3489 void SetNormalNode::compile(SVMCompiler& compiler)
3490 {
3491         ShaderInput  *direction_in = input("Direction");
3492         ShaderOutput *normal_out = output("Normal");
3493
3494         compiler.stack_assign(direction_in);
3495         compiler.stack_assign(normal_out);
3496
3497         compiler.add_node(NODE_CLOSURE_SET_NORMAL, direction_in->stack_offset, normal_out->stack_offset);
3498 }
3499
3500 void SetNormalNode::compile(OSLCompiler& compiler)
3501 {
3502         compiler.add(this, "node_set_normal"); 
3503 }
3504
3505 /* OSLScriptNode */
3506
3507 OSLScriptNode::OSLScriptNode()
3508 : ShaderNode("osl_script")
3509 {
3510 }
3511
3512 void OSLScriptNode::compile(SVMCompiler& compiler)
3513 {
3514         /* doesn't work for SVM, obviously ... */
3515 }
3516
3517 void OSLScriptNode::compile(OSLCompiler& compiler)
3518 {
3519         if(!filepath.empty())
3520                 compiler.add(this, filepath.c_str(), true);
3521         else
3522                 compiler.add(this, bytecode_hash.c_str(), false);
3523 }
3524
3525 /* Normal Map */
3526
3527 static ShaderEnum normal_map_space_init()
3528 {
3529         ShaderEnum enm;
3530
3531         enm.insert("Tangent", NODE_NORMAL_MAP_TANGENT);
3532         enm.insert("Object", NODE_NORMAL_MAP_OBJECT);
3533         enm.insert("World", NODE_NORMAL_MAP_WORLD);
3534         enm.insert("Blender Object", NODE_NORMAL_MAP_BLENDER_OBJECT);
3535         enm.insert("Blender World", NODE_NORMAL_MAP_BLENDER_WORLD);
3536
3537         return enm;
3538 }
3539
3540 ShaderEnum NormalMapNode::space_enum = normal_map_space_init();
3541
3542 NormalMapNode::NormalMapNode()
3543 : ShaderNode("normal_map")
3544 {
3545         space = ustring("Tangent");
3546         attribute = ustring("");
3547
3548         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3549         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3550         add_input("Color", SHADER_SOCKET_COLOR);
3551
3552         add_output("Normal", SHADER_SOCKET_NORMAL);
3553 }
3554
3555 void NormalMapNode::attributes(AttributeRequestSet *attributes)
3556 {
3557         if(space == ustring("Tangent")) {
3558                 if(attribute == ustring("")) {
3559                         attributes->add(ATTR_STD_UV_TANGENT);
3560                         attributes->add(ATTR_STD_UV_TANGENT_SIGN);
3561                 }
3562                 else {
3563                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3564                         attributes->add(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3565                 }
3566
3567                 attributes->add(ATTR_STD_VERTEX_NORMAL);
3568         }
3569         
3570         ShaderNode::attributes(attributes);
3571 }
3572
3573 void NormalMapNode::compile(SVMCompiler& compiler)
3574 {
3575         ShaderInput  *color_in = input("Color");
3576         ShaderInput  *strength_in = input("Strength");
3577         ShaderOutput *normal_out = output("Normal");
3578         int attr = 0, attr_sign = 0;
3579
3580         if(space == ustring("Tangent")) {
3581                 if(attribute == ustring("")) {
3582                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3583                         attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
3584                 }
3585                 else {
3586                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3587                         attr_sign = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3588                 }
3589         }
3590
3591         compiler.stack_assign(color_in);
3592         compiler.stack_assign(strength_in);
3593         compiler.stack_assign(normal_out);
3594
3595         compiler.add_node(NODE_NORMAL_MAP,
3596                 compiler.encode_uchar4(
3597                         color_in->stack_offset,
3598                         strength_in->stack_offset,
3599                         normal_out->stack_offset,
3600                         space_enum[space]),
3601                 attr, attr_sign);
3602 }
3603
3604 void NormalMapNode::compile(OSLCompiler& compiler)
3605 {
3606         if(space == ustring("Tangent")) {
3607                 if(attribute == ustring("")) {
3608                         compiler.parameter("attr_name", ustring("geom:tangent"));
3609                         compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
3610                 }
3611                 else {
3612                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3613                         compiler.parameter("attr_sign_name", ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3614                 }
3615         }
3616
3617         compiler.parameter("space", space);
3618
3619         compiler.add(this, "node_normal_map"); 
3620 }
3621
3622 /* Tangent */
3623
3624 static ShaderEnum tangent_direction_type_init()
3625 {
3626         ShaderEnum enm;
3627
3628         enm.insert("Radial", NODE_TANGENT_RADIAL);
3629         enm.insert("UV Map", NODE_TANGENT_UVMAP);
3630
3631         return enm;
3632 }
3633
3634 static ShaderEnum tangent_axis_init()
3635 {
3636         ShaderEnum enm;
3637
3638         enm.insert("X", NODE_TANGENT_AXIS_X);
3639         enm.insert("Y", NODE_TANGENT_AXIS_Y);
3640         enm.insert("Z", NODE_TANGENT_AXIS_Z);
3641
3642         return enm;
3643 }
3644
3645 ShaderEnum TangentNode::direction_type_enum = tangent_direction_type_init();
3646 ShaderEnum TangentNode::axis_enum = tangent_axis_init();
3647
3648 TangentNode::TangentNode()
3649 : ShaderNode("tangent")
3650 {
3651         direction_type = ustring("Radial");
3652         axis = ustring("X");
3653         attribute = ustring("");
3654
3655         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3656         add_output("Tangent", SHADER_SOCKET_NORMAL);
3657 }
3658
3659 void TangentNode::attributes(AttributeRequestSet *attributes)
3660 {
3661         if(direction_type == ustring("UV Map")) {
3662                 if(attribute == ustring(""))
3663                         attributes->add(ATTR_STD_UV_TANGENT);
3664                 else
3665                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3666         }
3667         else
3668                 attributes->add(ATTR_STD_GENERATED);
3669         
3670         ShaderNode::attributes(attributes);
3671 }
3672
3673 void TangentNode::compile(SVMCompiler& compiler)
3674 {
3675         ShaderOutput *tangent_out = output("Tangent");
3676         int attr;
3677
3678         if(direction_type == ustring("UV Map")) {
3679                 if(attribute == ustring(""))
3680                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3681                 else
3682                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3683         }
3684         else
3685                 attr = compiler.attribute(ATTR_STD_GENERATED);
3686
3687         compiler.stack_assign(tangent_out);
3688
3689         compiler.add_node(NODE_TANGENT,
3690                 compiler.encode_uchar4(
3691                         tangent_out->stack_offset,
3692                         direction_type_enum[direction_type],
3693                         axis_enum[axis]), attr);
3694 }
3695
3696 void TangentNode::compile(OSLCompiler& compiler)
3697 {
3698         if(direction_type == ustring("UV Map")) {
3699                 if(attribute == ustring(""))
3700                         compiler.parameter("attr_name", ustring("geom:tangent"));
3701                 else
3702                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3703         }
3704
3705         compiler.parameter("direction_type", direction_type);
3706         compiler.parameter("axis", axis);
3707         compiler.add(this, "node_tangent"); 
3708 }
3709
3710 CCL_NAMESPACE_END
3711