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