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