SVN merge r57320-57365 into soc-2013-dingto
[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 /* Toon BSDF Closure */
1486
1487 static ShaderEnum toon_component_init()
1488 {
1489         ShaderEnum enm;
1490
1491         enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
1492         enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
1493
1494         return enm;
1495 }
1496
1497 ShaderEnum ToonBsdfNode::component_enum = toon_component_init();
1498
1499 ToonBsdfNode::ToonBsdfNode()
1500 {
1501         component = ustring("Diffuse");
1502
1503         add_input("Size", SHADER_SOCKET_FLOAT, 0.5f);
1504         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1505 }
1506
1507 void ToonBsdfNode::compile(SVMCompiler& compiler)
1508 {
1509         closure = (ClosureType)component_enum[component];
1510         
1511         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
1512 }
1513
1514 void ToonBsdfNode::compile(OSLCompiler& compiler)
1515 {
1516         compiler.parameter("component", component);
1517         compiler.add(this, "node_toon_bsdf");
1518 }
1519
1520 /* Velvet BSDF Closure */
1521
1522 VelvetBsdfNode::VelvetBsdfNode()
1523 {
1524         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1525
1526         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1527 }
1528
1529 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1530 {
1531         BsdfNode::compile(compiler, input("Sigma"), NULL);
1532 }
1533
1534 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1535 {
1536         compiler.add(this, "node_velvet_bsdf");
1537 }
1538
1539 /* Diffuse BSDF Closure */
1540
1541 DiffuseBsdfNode::DiffuseBsdfNode()
1542 {
1543         closure = CLOSURE_BSDF_DIFFUSE_ID;
1544         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1545 }
1546
1547 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1548 {
1549         BsdfNode::compile(compiler, input("Roughness"), NULL);
1550 }
1551
1552 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1553 {
1554         compiler.add(this, "node_diffuse_bsdf");
1555 }
1556
1557 /* Translucent BSDF Closure */
1558
1559 TranslucentBsdfNode::TranslucentBsdfNode()
1560 {
1561         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1562 }
1563
1564 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1565 {
1566         BsdfNode::compile(compiler, NULL, NULL);
1567 }
1568
1569 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1570 {
1571         compiler.add(this, "node_translucent_bsdf");
1572 }
1573
1574 /* Transparent BSDF Closure */
1575
1576 TransparentBsdfNode::TransparentBsdfNode()
1577 {
1578         name = "transparent";
1579         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1580 }
1581
1582 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1583 {
1584         BsdfNode::compile(compiler, NULL, NULL);
1585 }
1586
1587 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1588 {
1589         compiler.add(this, "node_transparent_bsdf");
1590 }
1591
1592 /* Subsurface Scattering Closure */
1593
1594 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
1595 : BsdfNode(true)
1596 {
1597         name = "subsurface_scattering";
1598         closure = CLOSURE_BSSRDF_ID;
1599
1600         add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
1601         add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
1602         add_input("IOR", SHADER_SOCKET_FLOAT, 1.3f);
1603 }
1604
1605 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
1606 {
1607         BsdfNode::compile(compiler, input("Scale"), input("IOR"), input("Radius"));
1608 }
1609
1610 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
1611 {
1612         compiler.add(this, "node_subsurface_scattering");
1613 }
1614
1615 /* Emissive Closure */
1616
1617 EmissionNode::EmissionNode()
1618 : ShaderNode("emission")
1619 {
1620         total_power = false;
1621
1622         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1623         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1624         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1625
1626         add_output("Emission", SHADER_SOCKET_CLOSURE);
1627 }
1628
1629 void EmissionNode::compile(SVMCompiler& compiler)
1630 {
1631         ShaderInput *color_in = input("Color");
1632         ShaderInput *strength_in = input("Strength");
1633
1634         if(color_in->link || strength_in->link) {
1635                 compiler.stack_assign(color_in);
1636                 compiler.stack_assign(strength_in);
1637                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1638         }
1639         else if(total_power)
1640                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1641         else
1642                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1643
1644         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1645 }
1646
1647 void EmissionNode::compile(OSLCompiler& compiler)
1648 {
1649         compiler.parameter("TotalPower", (total_power)? 1: 0);
1650         compiler.add(this, "node_emission");
1651 }
1652
1653 /* Background Closure */
1654
1655 BackgroundNode::BackgroundNode()
1656 : ShaderNode("background")
1657 {
1658         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1659         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1660         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1661
1662         add_output("Background", SHADER_SOCKET_CLOSURE);
1663 }
1664
1665 void BackgroundNode::compile(SVMCompiler& compiler)
1666 {
1667         ShaderInput *color_in = input("Color");
1668         ShaderInput *strength_in = input("Strength");
1669
1670         if(color_in->link || strength_in->link) {
1671                 compiler.stack_assign(color_in);
1672                 compiler.stack_assign(strength_in);
1673                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1674         }
1675         else
1676                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1677
1678         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1679 }
1680
1681 void BackgroundNode::compile(OSLCompiler& compiler)
1682 {
1683         compiler.add(this, "node_background");
1684 }
1685
1686 /* Holdout Closure */
1687
1688 HoldoutNode::HoldoutNode()
1689 : ShaderNode("holdout")
1690 {
1691         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1692         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1693
1694         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1695 }
1696
1697 void HoldoutNode::compile(SVMCompiler& compiler)
1698 {
1699         float3 value = make_float3(1.0f, 1.0f, 1.0f);
1700
1701         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
1702         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1703 }
1704
1705 void HoldoutNode::compile(OSLCompiler& compiler)
1706 {
1707         compiler.add(this, "node_holdout");
1708 }
1709
1710 /* Ambient Occlusion */
1711
1712 AmbientOcclusionNode::AmbientOcclusionNode()
1713 : ShaderNode("ambient_occlusion")
1714 {
1715         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1716         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1717         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1718
1719         add_output("AO", SHADER_SOCKET_CLOSURE);
1720 }
1721
1722 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
1723 {
1724         ShaderInput *color_in = input("Color");
1725
1726         if(color_in->link) {
1727                 compiler.stack_assign(color_in);
1728                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1729         }
1730         else
1731                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1732
1733         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
1734 }
1735
1736 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
1737 {
1738         compiler.add(this, "node_ambient_occlusion");
1739 }
1740
1741 /* Volume Closure */
1742
1743 VolumeNode::VolumeNode()
1744 : ShaderNode("volume")
1745 {
1746         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1747
1748         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1749         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1750         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1751
1752         add_output("Volume", SHADER_SOCKET_CLOSURE);
1753 }
1754
1755 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1756 {
1757         ShaderInput *color_in = input("Color");
1758
1759         if(color_in->link) {
1760                 compiler.stack_assign(color_in);
1761                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1762         }
1763         else
1764                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1765         
1766         if(param1)
1767                 compiler.stack_assign(param1);
1768         if(param2)
1769                 compiler.stack_assign(param2);
1770
1771         compiler.add_node(NODE_CLOSURE_VOLUME,
1772                 compiler.encode_uchar4(closure,
1773                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1774                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1775                         compiler.closure_mix_weight_offset()),
1776                 __float_as_int((param1)? param1->value.x: 0.0f),
1777                 __float_as_int((param2)? param2->value.x: 0.0f));
1778 }
1779
1780 void VolumeNode::compile(SVMCompiler& compiler)
1781 {
1782         compile(compiler, NULL, NULL);
1783 }
1784
1785 void VolumeNode::compile(OSLCompiler& compiler)
1786 {
1787         assert(0);
1788 }
1789
1790 /* Transparent Volume Closure */
1791
1792 TransparentVolumeNode::TransparentVolumeNode()
1793 {
1794         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1795 }
1796
1797 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1798 {
1799         VolumeNode::compile(compiler, input("Density"), NULL);
1800 }
1801
1802 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1803 {
1804         compiler.add(this, "node_isotropic_volume");
1805 }
1806
1807 /* Isotropic Volume Closure */
1808
1809 IsotropicVolumeNode::IsotropicVolumeNode()
1810 {
1811         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1812 }
1813
1814 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1815 {
1816         VolumeNode::compile(compiler, input("Density"), NULL);
1817 }
1818
1819 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1820 {
1821         compiler.add(this, "node_isotropic_volume");
1822 }
1823
1824 /* Geometry */
1825
1826 GeometryNode::GeometryNode()
1827 : ShaderNode("geometry")
1828 {
1829         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1830         add_output("Position", SHADER_SOCKET_POINT);
1831         add_output("Normal", SHADER_SOCKET_NORMAL);
1832         add_output("Tangent", SHADER_SOCKET_NORMAL);
1833         add_output("True Normal", SHADER_SOCKET_NORMAL);
1834         add_output("Incoming", SHADER_SOCKET_VECTOR);
1835         add_output("Parametric", SHADER_SOCKET_POINT);
1836         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1837 }
1838
1839 void GeometryNode::attributes(AttributeRequestSet *attributes)
1840 {
1841         if(!output("Tangent")->links.empty())
1842                 attributes->add(ATTR_STD_GENERATED);
1843
1844         ShaderNode::attributes(attributes);
1845 }
1846
1847 void GeometryNode::compile(SVMCompiler& compiler)
1848 {
1849         ShaderOutput *out;
1850         NodeType geom_node = NODE_GEOMETRY;
1851
1852         if(bump == SHADER_BUMP_DX)
1853                 geom_node = NODE_GEOMETRY_BUMP_DX;
1854         else if(bump == SHADER_BUMP_DY)
1855                 geom_node = NODE_GEOMETRY_BUMP_DY;
1856         
1857         out = output("Position");
1858         if(!out->links.empty()) {
1859                 compiler.stack_assign(out);
1860                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1861         }
1862
1863         out = output("Normal");
1864         if(!out->links.empty()) {
1865                 compiler.stack_assign(out);
1866                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1867         }
1868
1869         out = output("Tangent");
1870         if(!out->links.empty()) {
1871                 compiler.stack_assign(out);
1872                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1873         }
1874
1875         out = output("True Normal");
1876         if(!out->links.empty()) {
1877                 compiler.stack_assign(out);
1878                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1879         }
1880
1881         out = output("Incoming");
1882         if(!out->links.empty()) {
1883                 compiler.stack_assign(out);
1884                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1885         }
1886
1887         out = output("Parametric");
1888         if(!out->links.empty()) {
1889                 compiler.stack_assign(out);
1890                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1891         }
1892
1893         out = output("Backfacing");
1894         if(!out->links.empty()) {
1895                 compiler.stack_assign(out);
1896                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1897         }
1898 }
1899
1900 void GeometryNode::compile(OSLCompiler& compiler)
1901 {
1902         if(bump == SHADER_BUMP_DX)
1903                 compiler.parameter("bump_offset", "dx");
1904         else if(bump == SHADER_BUMP_DY)
1905                 compiler.parameter("bump_offset", "dy");
1906         else
1907                 compiler.parameter("bump_offset", "center");
1908
1909         compiler.add(this, "node_geometry");
1910 }
1911
1912 /* TextureCoordinate */
1913
1914 TextureCoordinateNode::TextureCoordinateNode()
1915 : ShaderNode("texture_coordinate")
1916 {
1917         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1918         add_output("Generated", SHADER_SOCKET_POINT);
1919         add_output("Normal", SHADER_SOCKET_NORMAL);
1920         add_output("UV", SHADER_SOCKET_POINT);
1921         add_output("Object", SHADER_SOCKET_POINT);
1922         add_output("Camera", SHADER_SOCKET_POINT);
1923         add_output("Window", SHADER_SOCKET_POINT);
1924         add_output("Reflection", SHADER_SOCKET_NORMAL);
1925
1926         from_dupli = false;
1927 }
1928
1929 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1930 {
1931         if(!from_dupli) {
1932                 if(!output("Generated")->links.empty())
1933                         attributes->add(ATTR_STD_GENERATED);
1934                 if(!output("UV")->links.empty())
1935                         attributes->add(ATTR_STD_UV);
1936         }
1937
1938         ShaderNode::attributes(attributes);
1939 }
1940
1941 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1942 {
1943         ShaderOutput *out;
1944         NodeType texco_node = NODE_TEX_COORD;
1945         NodeType attr_node = NODE_ATTR;
1946         NodeType geom_node = NODE_GEOMETRY;
1947
1948         if(bump == SHADER_BUMP_DX) {
1949                 texco_node = NODE_TEX_COORD_BUMP_DX;
1950                 attr_node = NODE_ATTR_BUMP_DX;
1951                 geom_node = NODE_GEOMETRY_BUMP_DX;
1952         }
1953         else if(bump == SHADER_BUMP_DY) {
1954                 texco_node = NODE_TEX_COORD_BUMP_DY;
1955                 attr_node = NODE_ATTR_BUMP_DY;
1956                 geom_node = NODE_GEOMETRY_BUMP_DY;
1957         }
1958         
1959         out = output("Generated");
1960         if(!out->links.empty()) {
1961                 if(compiler.background) {
1962                         compiler.stack_assign(out);
1963                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1964                 }
1965                 else {
1966                         if(from_dupli) {
1967                                 compiler.stack_assign(out);
1968                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
1969                         }
1970                         else {
1971                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
1972                                 compiler.stack_assign(out);
1973                                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1974                         }
1975                 }
1976         }
1977
1978         out = output("Normal");
1979         if(!out->links.empty()) {
1980                 compiler.stack_assign(out);
1981                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
1982         }
1983
1984         out = output("UV");
1985         if(!out->links.empty()) {
1986                 if(from_dupli) {
1987                         compiler.stack_assign(out);
1988                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
1989                 }
1990                 else {
1991                         int attr = compiler.attribute(ATTR_STD_UV);
1992                         compiler.stack_assign(out);
1993                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1994                 }
1995         }
1996
1997         out = output("Object");
1998         if(!out->links.empty()) {
1999                 compiler.stack_assign(out);
2000                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
2001         }
2002
2003         out = output("Camera");
2004         if(!out->links.empty()) {
2005                 compiler.stack_assign(out);
2006                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
2007         }
2008
2009         out = output("Window");
2010         if(!out->links.empty()) {
2011                 compiler.stack_assign(out);
2012                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
2013         }
2014
2015         out = output("Reflection");
2016         if(!out->links.empty()) {
2017                 if(compiler.background) {
2018                         compiler.stack_assign(out);
2019                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2020                 }
2021                 else {
2022                         compiler.stack_assign(out);
2023                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
2024                 }
2025         }
2026 }
2027
2028 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2029 {
2030         if(bump == SHADER_BUMP_DX)
2031                 compiler.parameter("bump_offset", "dx");
2032         else if(bump == SHADER_BUMP_DY)
2033                 compiler.parameter("bump_offset", "dy");
2034         else
2035                 compiler.parameter("bump_offset", "center");
2036         
2037         if(compiler.background)
2038                 compiler.parameter("is_background", true);
2039         
2040         compiler.parameter("from_dupli", from_dupli);
2041
2042         compiler.add(this, "node_texture_coordinate");
2043 }
2044
2045 /* Light Path */
2046
2047 LightPathNode::LightPathNode()
2048 : ShaderNode("light_path")
2049 {
2050         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
2051         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
2052         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
2053         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
2054         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
2055         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
2056         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
2057         add_output("Ray Length", SHADER_SOCKET_FLOAT);
2058 }
2059
2060 void LightPathNode::compile(SVMCompiler& compiler)
2061 {
2062         ShaderOutput *out;
2063
2064         out = output("Is Camera Ray");
2065         if(!out->links.empty()) {
2066                 compiler.stack_assign(out);
2067                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2068         }
2069
2070         out = output("Is Shadow Ray");
2071         if(!out->links.empty()) {
2072                 compiler.stack_assign(out);
2073                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2074         }
2075
2076         out = output("Is Diffuse Ray");
2077         if(!out->links.empty()) {
2078                 compiler.stack_assign(out);
2079                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2080         }
2081
2082         out = output("Is Glossy Ray");
2083         if(!out->links.empty()) {
2084                 compiler.stack_assign(out);
2085                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2086         }
2087
2088         out = output("Is Singular Ray");
2089         if(!out->links.empty()) {
2090                 compiler.stack_assign(out);
2091                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2092         }
2093
2094         out = output("Is Reflection Ray");
2095         if(!out->links.empty()) {
2096                 compiler.stack_assign(out);
2097                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2098         }
2099
2100
2101         out = output("Is Transmission Ray");
2102         if(!out->links.empty()) {
2103                 compiler.stack_assign(out);
2104                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2105         }
2106         
2107         out = output("Ray Length");
2108         if(!out->links.empty()) {
2109                 compiler.stack_assign(out);
2110                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2111         }
2112
2113 }
2114
2115 void LightPathNode::compile(OSLCompiler& compiler)
2116 {
2117         compiler.add(this, "node_light_path");
2118 }
2119
2120 /* Light Falloff */
2121
2122 LightFalloffNode::LightFalloffNode()
2123 : ShaderNode("light_path")
2124 {
2125         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2126         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2127         add_output("Quadratic", SHADER_SOCKET_FLOAT);
2128         add_output("Linear", SHADER_SOCKET_FLOAT);
2129         add_output("Constant", SHADER_SOCKET_FLOAT);
2130 }
2131
2132 void LightFalloffNode::compile(SVMCompiler& compiler)
2133 {
2134         ShaderInput *strength_in = input("Strength");
2135         ShaderInput *smooth_in = input("Smooth");
2136
2137         compiler.stack_assign(strength_in);
2138         compiler.stack_assign(smooth_in);
2139
2140         ShaderOutput *out = output("Quadratic");
2141         if(!out->links.empty()) {
2142                 compiler.stack_assign(out);
2143                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2144                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2145         }
2146
2147         out = output("Linear");
2148         if(!out->links.empty()) {
2149                 compiler.stack_assign(out);
2150                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2151                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2152         }
2153
2154         out = output("Constant");
2155         if(!out->links.empty()) {
2156                 compiler.stack_assign(out);
2157                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2158                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2159         }
2160 }
2161
2162 void LightFalloffNode::compile(OSLCompiler& compiler)
2163 {
2164         compiler.add(this, "node_light_falloff");
2165 }
2166
2167 /* Object Info */
2168
2169 ObjectInfoNode::ObjectInfoNode()
2170 : ShaderNode("object_info")
2171 {
2172         add_output("Location", SHADER_SOCKET_VECTOR);
2173         add_output("Object Index", SHADER_SOCKET_FLOAT);
2174         add_output("Material Index", SHADER_SOCKET_FLOAT);
2175         add_output("Random", SHADER_SOCKET_FLOAT);
2176 }
2177
2178 void ObjectInfoNode::compile(SVMCompiler& compiler)
2179 {
2180         ShaderOutput *out = output("Location");
2181         if(!out->links.empty()) {
2182                 compiler.stack_assign(out);
2183                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2184         }
2185
2186         out = output("Object Index");
2187         if(!out->links.empty()) {
2188                 compiler.stack_assign(out);
2189                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2190         }
2191
2192         out = output("Material Index");
2193         if(!out->links.empty()) {
2194                 compiler.stack_assign(out);
2195                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2196         }
2197
2198         out = output("Random");
2199         if(!out->links.empty()) {
2200                 compiler.stack_assign(out);
2201                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2202         }
2203 }
2204
2205 void ObjectInfoNode::compile(OSLCompiler& compiler)
2206 {
2207         compiler.add(this, "node_object_info");
2208 }
2209
2210 /* Particle Info */
2211
2212 ParticleInfoNode::ParticleInfoNode()
2213 : ShaderNode("particle_info")
2214 {
2215         add_output("Index", SHADER_SOCKET_FLOAT);
2216         add_output("Age", SHADER_SOCKET_FLOAT);
2217         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2218         add_output("Location", SHADER_SOCKET_POINT);
2219         #if 0   /* not yet supported */
2220         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2221         #endif
2222         add_output("Size", SHADER_SOCKET_FLOAT);
2223         add_output("Velocity", SHADER_SOCKET_VECTOR);
2224         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2225 }
2226
2227 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
2228 {
2229         if(!output("Index")->links.empty())
2230                 attributes->add(ATTR_STD_PARTICLE);
2231         if(!output("Age")->links.empty())
2232                 attributes->add(ATTR_STD_PARTICLE);
2233         if(!output("Lifetime")->links.empty())
2234                 attributes->add(ATTR_STD_PARTICLE);
2235         if(!output("Location")->links.empty())
2236                 attributes->add(ATTR_STD_PARTICLE);
2237         #if 0   /* not yet supported */
2238         if(!output("Rotation")->links.empty())
2239                 attributes->add(ATTR_STD_PARTICLE);
2240         #endif
2241         if(!output("Size")->links.empty())
2242                 attributes->add(ATTR_STD_PARTICLE);
2243         if(!output("Velocity")->links.empty())
2244                 attributes->add(ATTR_STD_PARTICLE);
2245         if(!output("Angular Velocity")->links.empty())
2246                 attributes->add(ATTR_STD_PARTICLE);
2247
2248         ShaderNode::attributes(attributes);
2249 }
2250
2251 void ParticleInfoNode::compile(SVMCompiler& compiler)
2252 {
2253         ShaderOutput *out;
2254         
2255         out = output("Index");
2256         if(!out->links.empty()) {
2257                 compiler.stack_assign(out);
2258                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2259         }
2260         
2261         out = output("Age");
2262         if(!out->links.empty()) {
2263                 compiler.stack_assign(out);
2264                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2265         }
2266         
2267         out = output("Lifetime");
2268         if(!out->links.empty()) {
2269                 compiler.stack_assign(out);
2270                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2271         }
2272         
2273         out = output("Location");
2274         if(!out->links.empty()) {
2275                 compiler.stack_assign(out);
2276                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2277         }
2278         
2279         /* quaternion data is not yet supported by Cycles */
2280 #if 0
2281         out = output("Rotation");
2282         if(!out->links.empty()) {
2283                 compiler.stack_assign(out);
2284                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2285         }
2286 #endif
2287         
2288         out = output("Size");
2289         if(!out->links.empty()) {
2290                 compiler.stack_assign(out);
2291                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2292         }
2293         
2294         out = output("Velocity");
2295         if(!out->links.empty()) {
2296                 compiler.stack_assign(out);
2297                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2298         }
2299         
2300         out = output("Angular Velocity");
2301         if(!out->links.empty()) {
2302                 compiler.stack_assign(out);
2303                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2304         }
2305 }
2306
2307 void ParticleInfoNode::compile(OSLCompiler& compiler)
2308 {
2309         compiler.add(this, "node_particle_info");
2310 }
2311
2312 /* Hair Info */
2313
2314 HairInfoNode::HairInfoNode()
2315 : ShaderNode("hair_info")
2316 {
2317         add_output("Is Strand", SHADER_SOCKET_FLOAT);
2318         add_output("Intercept", SHADER_SOCKET_FLOAT);
2319         add_output("Thickness", SHADER_SOCKET_FLOAT);
2320         add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2321         /*output for minimum hair width transparency - deactivated*/
2322         /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2323 }
2324
2325 void HairInfoNode::attributes(AttributeRequestSet *attributes)
2326 {
2327         ShaderOutput *intercept_out = output("Intercept");
2328
2329         if(!intercept_out->links.empty())
2330                 attributes->add(ATTR_STD_CURVE_INTERCEPT);
2331         
2332         ShaderNode::attributes(attributes);
2333 }
2334
2335 void HairInfoNode::compile(SVMCompiler& compiler)
2336 {
2337         ShaderOutput *out;
2338         
2339         out = output("Is Strand");
2340         if(!out->links.empty()) {
2341                 compiler.stack_assign(out);
2342                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2343         }
2344
2345         out = output("Intercept");
2346         if(!out->links.empty()) {
2347                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2348                 compiler.stack_assign(out);
2349                 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2350         }
2351
2352         out = output("Thickness");
2353         if(!out->links.empty()) {
2354                 compiler.stack_assign(out);
2355                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2356         }
2357
2358         out = output("Tangent Normal");
2359         if(!out->links.empty()) {
2360                 compiler.stack_assign(out);
2361                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2362         }
2363
2364         /*out = output("Fade");
2365         if(!out->links.empty()) {
2366                 compiler.stack_assign(out);
2367                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2368         }*/
2369
2370 }
2371
2372 void HairInfoNode::compile(OSLCompiler& compiler)
2373 {
2374         compiler.add(this, "node_hair_info");
2375 }
2376
2377 /* Value */
2378
2379 ValueNode::ValueNode()
2380 : ShaderNode("value")
2381 {
2382         value = 0.0f;
2383
2384         add_output("Value", SHADER_SOCKET_FLOAT);
2385 }
2386
2387 void ValueNode::compile(SVMCompiler& compiler)
2388 {
2389         ShaderOutput *val_out = output("Value");
2390
2391         compiler.stack_assign(val_out);
2392         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2393 }
2394
2395 void ValueNode::compile(OSLCompiler& compiler)
2396 {
2397         compiler.parameter("value_value", value);
2398         compiler.add(this, "node_value");
2399 }
2400
2401 /* Color */
2402
2403 ColorNode::ColorNode()
2404 : ShaderNode("color")
2405 {
2406         value = make_float3(0.0f, 0.0f, 0.0f);
2407
2408         add_output("Color", SHADER_SOCKET_COLOR);
2409 }
2410
2411 void ColorNode::compile(SVMCompiler& compiler)
2412 {
2413         ShaderOutput *color_out = output("Color");
2414
2415         if(color_out && !color_out->links.empty()) {
2416                 compiler.stack_assign(color_out);
2417                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2418                 compiler.add_node(NODE_VALUE_V, value);
2419         }
2420 }
2421
2422 void ColorNode::compile(OSLCompiler& compiler)
2423 {
2424         compiler.parameter_color("color_value", value);
2425
2426         compiler.add(this, "node_value");
2427 }
2428
2429 /* Add Closure */
2430
2431 AddClosureNode::AddClosureNode()
2432 : ShaderNode("add_closure")
2433 {
2434         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2435         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2436         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2437 }
2438
2439 void AddClosureNode::compile(SVMCompiler& compiler)
2440 {
2441         /* handled in the SVM compiler */
2442 }
2443
2444 void AddClosureNode::compile(OSLCompiler& compiler)
2445 {
2446         compiler.add(this, "node_add_closure");
2447 }
2448
2449 /* Mix Closure */
2450
2451 MixClosureNode::MixClosureNode()
2452 : ShaderNode("mix_closure")
2453 {
2454         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2455         
2456         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2457         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2458         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2459         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2460 }
2461
2462 void MixClosureNode::compile(SVMCompiler& compiler)
2463 {
2464         /* handled in the SVM compiler */
2465 }
2466
2467 void MixClosureNode::compile(OSLCompiler& compiler)
2468 {
2469         compiler.add(this, "node_mix_closure");
2470 }
2471
2472 /* Mix Closure */
2473
2474 MixClosureWeightNode::MixClosureWeightNode()
2475 : ShaderNode("mix_closure_weight")
2476 {
2477         add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2478         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2479         add_output("Weight1", SHADER_SOCKET_FLOAT);
2480         add_output("Weight2", SHADER_SOCKET_FLOAT);
2481 }
2482
2483 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2484 {
2485         ShaderInput *weight_in = input("Weight");
2486         ShaderInput *fac_in = input("Fac");
2487         ShaderOutput *weight1_out = output("Weight1");
2488         ShaderOutput *weight2_out = output("Weight2");
2489
2490         compiler.stack_assign(weight_in);
2491         compiler.stack_assign(fac_in);
2492         compiler.stack_assign(weight1_out);
2493         compiler.stack_assign(weight2_out);
2494
2495         compiler.add_node(NODE_MIX_CLOSURE,
2496                 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2497                         weight1_out->stack_offset, weight2_out->stack_offset));
2498 }
2499
2500 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2501 {
2502         assert(0);
2503 }
2504
2505 /* Invert */
2506
2507 InvertNode::InvertNode()
2508 : ShaderNode("invert")
2509 {
2510         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2511         add_input("Color", SHADER_SOCKET_COLOR);
2512         add_output("Color",  SHADER_SOCKET_COLOR);
2513 }
2514
2515 void InvertNode::compile(SVMCompiler& compiler)
2516 {
2517         ShaderInput *fac_in = input("Fac");
2518         ShaderInput *color_in = input("Color");
2519         ShaderOutput *color_out = output("Color");
2520
2521         compiler.stack_assign(fac_in);
2522         compiler.stack_assign(color_in);
2523         compiler.stack_assign(color_out);
2524
2525         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2526 }
2527
2528 void InvertNode::compile(OSLCompiler& compiler)
2529 {
2530         compiler.add(this, "node_invert");
2531 }
2532
2533 /* Mix */
2534
2535 MixNode::MixNode()
2536 : ShaderNode("mix")
2537 {
2538         type = ustring("Mix");
2539
2540         use_clamp = false;
2541
2542         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2543         add_input("Color1", SHADER_SOCKET_COLOR);
2544         add_input("Color2", SHADER_SOCKET_COLOR);
2545         add_output("Color",  SHADER_SOCKET_COLOR);
2546 }
2547
2548 static ShaderEnum mix_type_init()
2549 {
2550         ShaderEnum enm;
2551
2552         enm.insert("Mix", NODE_MIX_BLEND);
2553         enm.insert("Add", NODE_MIX_ADD);
2554         enm.insert("Multiply", NODE_MIX_MUL);
2555         enm.insert("Screen", NODE_MIX_SCREEN);
2556         enm.insert("Overlay", NODE_MIX_OVERLAY);
2557         enm.insert("Subtract", NODE_MIX_SUB);
2558         enm.insert("Divide", NODE_MIX_DIV);
2559         enm.insert("Difference", NODE_MIX_DIFF);
2560         enm.insert("Darken", NODE_MIX_DARK);
2561         enm.insert("Lighten", NODE_MIX_LIGHT);
2562         enm.insert("Dodge", NODE_MIX_DODGE);
2563         enm.insert("Burn", NODE_MIX_BURN);
2564         enm.insert("Hue", NODE_MIX_HUE);
2565         enm.insert("Saturation", NODE_MIX_SAT);
2566         enm.insert("Value", NODE_MIX_VAL);
2567         enm.insert("Color", NODE_MIX_COLOR);
2568         enm.insert("Soft Light", NODE_MIX_SOFT);
2569         enm.insert("Linear Light", NODE_MIX_LINEAR);
2570
2571         return enm;
2572 }
2573
2574 ShaderEnum MixNode::type_enum = mix_type_init();
2575
2576 void MixNode::compile(SVMCompiler& compiler)
2577 {
2578         ShaderInput *fac_in = input("Fac");
2579         ShaderInput *color1_in = input("Color1");
2580         ShaderInput *color2_in = input("Color2");
2581         ShaderOutput *color_out = output("Color");
2582
2583         compiler.stack_assign(fac_in);
2584         compiler.stack_assign(color1_in);
2585         compiler.stack_assign(color2_in);
2586         compiler.stack_assign(color_out);
2587
2588         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2589         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2590
2591         if(use_clamp) {
2592                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2593                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2594         }
2595 }
2596
2597 void MixNode::compile(OSLCompiler& compiler)
2598 {
2599         compiler.parameter("type", type);
2600         compiler.parameter("Clamp", use_clamp);
2601         compiler.add(this, "node_mix");
2602 }
2603
2604 /* Combine RGB */
2605 CombineRGBNode::CombineRGBNode()
2606 : ShaderNode("combine_rgb")
2607 {
2608         add_input("R", SHADER_SOCKET_FLOAT);
2609         add_input("G", SHADER_SOCKET_FLOAT);
2610         add_input("B", SHADER_SOCKET_FLOAT);
2611         add_output("Image", SHADER_SOCKET_COLOR);
2612 }
2613
2614 void CombineRGBNode::compile(SVMCompiler& compiler)
2615 {
2616         ShaderInput *red_in = input("R");
2617         ShaderInput *green_in = input("G");
2618         ShaderInput *blue_in = input("B");
2619         ShaderOutput *color_out = output("Image");
2620
2621         compiler.stack_assign(color_out);
2622
2623         compiler.stack_assign(red_in);
2624         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2625
2626         compiler.stack_assign(green_in);
2627         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2628
2629         compiler.stack_assign(blue_in);
2630         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2631 }
2632
2633 void CombineRGBNode::compile(OSLCompiler& compiler)
2634 {
2635         compiler.add(this, "node_combine_rgb");
2636 }
2637
2638 /* Gamma */
2639 GammaNode::GammaNode()
2640 : ShaderNode("gamma")
2641 {
2642         add_input("Color", SHADER_SOCKET_COLOR);
2643         add_input("Gamma", SHADER_SOCKET_FLOAT);
2644         add_output("Color", SHADER_SOCKET_COLOR);
2645 }
2646
2647 void GammaNode::compile(SVMCompiler& compiler)
2648 {
2649         ShaderInput *color_in = input("Color");
2650         ShaderInput *gamma_in = input("Gamma");
2651         ShaderOutput *color_out = output("Color");
2652
2653         compiler.stack_assign(color_in);
2654         compiler.stack_assign(gamma_in);
2655         compiler.stack_assign(color_out);
2656
2657         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2658 }
2659
2660 void GammaNode::compile(OSLCompiler& compiler)
2661 {
2662         compiler.add(this, "node_gamma");
2663 }
2664
2665 /* Bright Contrast */
2666 BrightContrastNode::BrightContrastNode()
2667 : ShaderNode("brightness")
2668 {
2669         add_input("Color", SHADER_SOCKET_COLOR);
2670         add_input("Bright", SHADER_SOCKET_FLOAT);
2671         add_input("Contrast", SHADER_SOCKET_FLOAT);
2672         add_output("Color", SHADER_SOCKET_COLOR);
2673 }
2674
2675 void BrightContrastNode::compile(SVMCompiler& compiler)
2676 {
2677         ShaderInput *color_in = input("Color");
2678         ShaderInput *bright_in = input("Bright");
2679         ShaderInput *contrast_in = input("Contrast");
2680         ShaderOutput *color_out = output("Color");
2681
2682         compiler.stack_assign(color_in);
2683         compiler.stack_assign(bright_in);
2684         compiler.stack_assign(contrast_in);
2685         compiler.stack_assign(color_out);
2686
2687         compiler.add_node(NODE_BRIGHTCONTRAST,
2688                 color_in->stack_offset, color_out->stack_offset,
2689                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2690 }
2691
2692 void BrightContrastNode::compile(OSLCompiler& compiler)
2693 {
2694         compiler.add(this, "node_brightness");
2695 }
2696
2697 /* Separate RGB */
2698 SeparateRGBNode::SeparateRGBNode()
2699 : ShaderNode("separate_rgb")
2700 {
2701         add_input("Image", SHADER_SOCKET_COLOR);
2702         add_output("R", SHADER_SOCKET_FLOAT);
2703         add_output("G", SHADER_SOCKET_FLOAT);
2704         add_output("B", SHADER_SOCKET_FLOAT);
2705 }
2706
2707 void SeparateRGBNode::compile(SVMCompiler& compiler)
2708 {
2709         ShaderInput *color_in = input("Image");
2710         ShaderOutput *red_out = output("R");
2711         ShaderOutput *green_out = output("G");
2712         ShaderOutput *blue_out = output("B");
2713
2714         compiler.stack_assign(color_in);
2715
2716         compiler.stack_assign(red_out);
2717         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2718
2719         compiler.stack_assign(green_out);
2720         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2721
2722         compiler.stack_assign(blue_out);
2723         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2724 }
2725
2726 void SeparateRGBNode::compile(OSLCompiler& compiler)
2727 {
2728         compiler.add(this, "node_separate_rgb");
2729 }
2730
2731 /* Separate RGB */
2732 HSVNode::HSVNode()
2733 : ShaderNode("hsv")
2734 {
2735         add_input("Hue", SHADER_SOCKET_FLOAT);
2736         add_input("Saturation", SHADER_SOCKET_FLOAT);
2737         add_input("Value", SHADER_SOCKET_FLOAT);
2738         add_input("Fac", SHADER_SOCKET_FLOAT);
2739         add_input("Color", SHADER_SOCKET_COLOR);
2740         add_output("Color", SHADER_SOCKET_COLOR);
2741 }
2742
2743 void HSVNode::compile(SVMCompiler& compiler)
2744 {
2745         ShaderInput *hue_in = input("Hue");
2746         ShaderInput *saturation_in = input("Saturation");
2747         ShaderInput *value_in = input("Value");
2748         ShaderInput *fac_in = input("Fac");
2749         ShaderInput *color_in = input("Color");
2750         ShaderOutput *color_out = output("Color");
2751
2752         compiler.stack_assign(hue_in);
2753         compiler.stack_assign(saturation_in);
2754         compiler.stack_assign(value_in);
2755         compiler.stack_assign(fac_in);
2756         compiler.stack_assign(color_in);
2757         compiler.stack_assign(color_out);
2758
2759         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2760         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2761 }
2762
2763 void HSVNode::compile(OSLCompiler& compiler)
2764 {
2765         compiler.add(this, "node_hsv");
2766 }
2767
2768 /* Attribute */
2769
2770 AttributeNode::AttributeNode()
2771 : ShaderNode("attribute")
2772 {
2773         attribute = "";
2774
2775         add_output("Color",  SHADER_SOCKET_COLOR);
2776         add_output("Vector",  SHADER_SOCKET_VECTOR);
2777         add_output("Fac",  SHADER_SOCKET_FLOAT);
2778 }
2779
2780 void AttributeNode::attributes(AttributeRequestSet *attributes)
2781 {
2782         ShaderOutput *color_out = output("Color");
2783         ShaderOutput *vector_out = output("Vector");
2784         ShaderOutput *fac_out = output("Fac");
2785
2786         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2787                 attributes->add(attribute);
2788         
2789         ShaderNode::attributes(attributes);
2790 }
2791
2792 void AttributeNode::compile(SVMCompiler& compiler)
2793 {
2794         ShaderOutput *color_out = output("Color");
2795         ShaderOutput *vector_out = output("Vector");
2796         ShaderOutput *fac_out = output("Fac");
2797         NodeType attr_node = NODE_ATTR;
2798
2799         if(bump == SHADER_BUMP_DX)
2800                 attr_node = NODE_ATTR_BUMP_DX;
2801         else if(bump == SHADER_BUMP_DY)
2802                 attr_node = NODE_ATTR_BUMP_DY;
2803
2804         if(!color_out->links.empty() || !vector_out->links.empty()) {
2805                 int attr = compiler.attribute(attribute);
2806
2807                 if(!color_out->links.empty()) {
2808                         compiler.stack_assign(color_out);
2809                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2810                 }
2811                 if(!vector_out->links.empty()) {
2812                         compiler.stack_assign(vector_out);
2813                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2814                 }
2815         }
2816
2817         if(!fac_out->links.empty()) {
2818                 int attr = compiler.attribute(attribute);
2819
2820                 compiler.stack_assign(fac_out);
2821                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2822         }
2823 }
2824
2825 void AttributeNode::compile(OSLCompiler& compiler)
2826 {
2827         if(bump == SHADER_BUMP_DX)
2828                 compiler.parameter("bump_offset", "dx");
2829         else if(bump == SHADER_BUMP_DY)
2830                 compiler.parameter("bump_offset", "dy");
2831         else
2832                 compiler.parameter("bump_offset", "center");
2833
2834         compiler.parameter("name", attribute.c_str());
2835         compiler.add(this, "node_attribute");
2836 }
2837
2838 /* Camera */
2839
2840 CameraNode::CameraNode()
2841 : ShaderNode("camera")
2842 {
2843         add_output("View Vector",  SHADER_SOCKET_VECTOR);
2844         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
2845         add_output("View Distance",  SHADER_SOCKET_FLOAT);
2846 }
2847
2848 void CameraNode::compile(SVMCompiler& compiler)
2849 {
2850         ShaderOutput *vector_out = output("View Vector");
2851         ShaderOutput *z_depth_out = output("View Z Depth");
2852         ShaderOutput *distance_out = output("View Distance");
2853
2854         compiler.stack_assign(vector_out);
2855         compiler.stack_assign(z_depth_out);
2856         compiler.stack_assign(distance_out);
2857         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2858 }
2859
2860 void CameraNode::compile(OSLCompiler& compiler)
2861 {
2862         compiler.add(this, "node_camera");
2863 }
2864
2865 /* Fresnel */
2866
2867 FresnelNode::FresnelNode()
2868 : ShaderNode("Fresnel")
2869 {
2870         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2871         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2872         add_output("Fac", SHADER_SOCKET_FLOAT);
2873 }
2874
2875 void FresnelNode::compile(SVMCompiler& compiler)
2876 {
2877         ShaderInput *ior_in = input("IOR");
2878         ShaderOutput *fac_out = output("Fac");
2879
2880         compiler.stack_assign(ior_in);
2881         compiler.stack_assign(fac_out);
2882         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2883 }
2884
2885 void FresnelNode::compile(OSLCompiler& compiler)
2886 {
2887         compiler.add(this, "node_fresnel");
2888 }
2889
2890 /* Blend Weight */
2891
2892 LayerWeightNode::LayerWeightNode()
2893 : ShaderNode("LayerWeight")
2894 {
2895         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2896         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2897
2898         add_output("Fresnel", SHADER_SOCKET_FLOAT);
2899         add_output("Facing", SHADER_SOCKET_FLOAT);
2900 }
2901
2902 void LayerWeightNode::compile(SVMCompiler& compiler)
2903 {
2904         ShaderInput *blend_in = input("Blend");
2905
2906         if(blend_in->link)
2907                 compiler.stack_assign(blend_in);
2908
2909         ShaderOutput *fresnel_out = output("Fresnel");
2910         if(!fresnel_out->links.empty()) {
2911                 compiler.stack_assign(fresnel_out);
2912                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2913                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
2914         }
2915
2916         ShaderOutput *facing_out = output("Facing");
2917         if(!facing_out->links.empty()) {
2918                 compiler.stack_assign(facing_out);
2919                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2920                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
2921         }
2922 }
2923
2924 void LayerWeightNode::compile(OSLCompiler& compiler)
2925 {
2926         compiler.add(this, "node_layer_weight");
2927 }
2928
2929 /* Wireframe */
2930
2931 WireframeNode::WireframeNode()
2932 : ShaderNode("Wireframe")
2933 {
2934         add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
2935         add_output("Fac", SHADER_SOCKET_FLOAT);
2936         
2937         use_pixel_size = false;
2938 }
2939
2940 void WireframeNode::compile(SVMCompiler& compiler)
2941 {
2942         ShaderInput *size_in = input("Size");
2943         ShaderOutput *fac_out = output("Fac");
2944
2945         compiler.stack_assign(size_in);
2946         compiler.stack_assign(fac_out);
2947         compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
2948 }
2949
2950 void WireframeNode::compile(OSLCompiler& compiler)
2951 {
2952         compiler.parameter("use_pixel_size", use_pixel_size);
2953         compiler.add(this, "node_wireframe");
2954 }
2955
2956 /* Wavelength */
2957
2958 WavelengthNode::WavelengthNode()
2959 : ShaderNode("Wavelength")
2960 {
2961         add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
2962         add_output("Color", SHADER_SOCKET_COLOR);
2963 }
2964
2965 void WavelengthNode::compile(SVMCompiler& compiler)
2966 {
2967         ShaderInput *wavelength_in = input("Wavelength");
2968         ShaderOutput *color_out = output("Color");
2969
2970         compiler.stack_assign(wavelength_in);
2971         compiler.stack_assign(color_out);
2972         compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset, NULL);
2973 }
2974
2975 void WavelengthNode::compile(OSLCompiler& compiler)
2976 {
2977         compiler.add(this, "node_wavelength");
2978 }
2979
2980 /* Output */
2981
2982 OutputNode::OutputNode()
2983 : ShaderNode("output")
2984 {
2985         add_input("Surface", SHADER_SOCKET_CLOSURE);
2986         add_input("Volume", SHADER_SOCKET_CLOSURE);
2987         add_input("Displacement", SHADER_SOCKET_FLOAT);
2988         add_input("Normal", SHADER_SOCKET_NORMAL);
2989 }
2990
2991 void OutputNode::compile(SVMCompiler& compiler)
2992 {
2993         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
2994                 ShaderInput *displacement_in = input("Displacement");
2995
2996                 if(displacement_in->link) {
2997                         compiler.stack_assign(displacement_in);
2998                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
2999                 }
3000         }
3001 }
3002
3003 void OutputNode::compile(OSLCompiler& compiler)
3004 {
3005         if(compiler.output_type() == SHADER_TYPE_SURFACE)
3006                 compiler.add(this, "node_output_surface");
3007         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3008                 compiler.add(this, "node_output_volume");
3009         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3010                 compiler.add(this, "node_output_displacement");
3011 }
3012
3013 /* Math */
3014
3015 MathNode::MathNode()
3016 : ShaderNode("math")
3017 {
3018         type = ustring("Add");
3019
3020         use_clamp = false;
3021
3022         add_input("Value1", SHADER_SOCKET_FLOAT);
3023         add_input("Value2", SHADER_SOCKET_FLOAT);
3024         add_output("Value",  SHADER_SOCKET_FLOAT);
3025 }
3026
3027 static ShaderEnum math_type_init()
3028 {
3029         ShaderEnum enm;
3030
3031         enm.insert("Add", NODE_MATH_ADD);
3032         enm.insert("Subtract", NODE_MATH_SUBTRACT);
3033         enm.insert("Multiply", NODE_MATH_MULTIPLY);
3034         enm.insert("Divide", NODE_MATH_DIVIDE);
3035         enm.insert("Sine", NODE_MATH_SINE);
3036         enm.insert("Cosine", NODE_MATH_COSINE);
3037         enm.insert("Tangent", NODE_MATH_TANGENT);
3038         enm.insert("Arcsine", NODE_MATH_ARCSINE);
3039         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3040         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3041         enm.insert("Power", NODE_MATH_POWER);
3042         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3043         enm.insert("Minimum", NODE_MATH_MINIMUM);
3044         enm.insert("Maximum", NODE_MATH_MAXIMUM);
3045         enm.insert("Round", NODE_MATH_ROUND);
3046         enm.insert("Less Than", NODE_MATH_LESS_THAN);
3047         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3048         enm.insert("Modulo", NODE_MATH_MODULO);
3049
3050         return enm;
3051 }
3052
3053 ShaderEnum MathNode::type_enum = math_type_init();
3054
3055 void MathNode::compile(SVMCompiler& compiler)
3056 {
3057         ShaderInput *value1_in = input("Value1");
3058         ShaderInput *value2_in = input("Value2");
3059         ShaderOutput *value_out = output("Value");
3060
3061         compiler.stack_assign(value1_in);
3062         compiler.stack_assign(value2_in);
3063         compiler.stack_assign(value_out);
3064
3065         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3066         compiler.add_node(NODE_MATH, value_out->stack_offset);
3067
3068         if(use_clamp) {
3069                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3070                 compiler.add_node(NODE_MATH, value_out->stack_offset);
3071         }
3072 }
3073
3074 void MathNode::compile(OSLCompiler& compiler)
3075 {
3076         compiler.parameter("type", type);
3077         compiler.parameter("Clamp", use_clamp);
3078         compiler.add(this, "node_math");
3079 }
3080
3081 /* VectorMath */
3082
3083 VectorMathNode::VectorMathNode()
3084 : ShaderNode("vector_math")
3085 {
3086         type = ustring("Add");
3087
3088         add_input("Vector1", SHADER_SOCKET_VECTOR);
3089         add_input("Vector2", SHADER_SOCKET_VECTOR);
3090         add_output("Value",  SHADER_SOCKET_FLOAT);
3091         add_output("Vector",  SHADER_SOCKET_VECTOR);
3092 }
3093
3094 static ShaderEnum vector_math_type_init()
3095 {
3096         ShaderEnum enm;
3097
3098         enm.insert("Add", NODE_VECTOR_MATH_ADD);
3099         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3100         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3101         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3102         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3103         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3104
3105         return enm;
3106 }
3107
3108 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3109
3110 void VectorMathNode::compile(SVMCompiler& compiler)
3111 {
3112         ShaderInput *vector1_in = input("Vector1");
3113         ShaderInput *vector2_in = input("Vector2");
3114         ShaderOutput *value_out = output("Value");
3115         ShaderOutput *vector_out = output("Vector");
3116
3117         compiler.stack_assign(vector1_in);
3118         compiler.stack_assign(vector2_in);
3119         compiler.stack_assign(value_out);
3120         compiler.stack_assign(vector_out);
3121
3122         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
3123         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
3124 }
3125
3126 void VectorMathNode::compile(OSLCompiler& compiler)
3127 {
3128         compiler.parameter("type", type);
3129         compiler.add(this, "node_vector_math");
3130 }
3131
3132 /* BumpNode */
3133
3134 BumpNode::BumpNode()
3135 : ShaderNode("bump")
3136 {
3137         invert = false;
3138
3139         /* this input is used by the user, but after graph transform it is no longer
3140          * used and moved to sampler center/x/y instead */
3141         add_input("Height", SHADER_SOCKET_FLOAT);
3142
3143         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
3144         add_input("SampleX", SHADER_SOCKET_FLOAT);
3145         add_input("SampleY", SHADER_SOCKET_FLOAT);
3146         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
3147         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3148         add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
3149
3150         add_output("Normal", SHADER_SOCKET_NORMAL);
3151 }
3152
3153 void BumpNode::compile(SVMCompiler& compiler)
3154 {
3155         ShaderInput *center_in = input("SampleCenter");
3156         ShaderInput *dx_in = input("SampleX");
3157         ShaderInput *dy_in = input("SampleY");
3158         ShaderInput *normal_in = input("Normal");
3159         ShaderInput *strength_in = input("Strength");
3160         ShaderInput *distance_in = input("Distance");
3161         ShaderOutput *normal_out = output("Normal");
3162
3163         compiler.stack_assign(center_in);
3164         compiler.stack_assign(dx_in);
3165         compiler.stack_assign(dy_in);
3166         compiler.stack_assign(strength_in);
3167         compiler.stack_assign(distance_in);
3168         compiler.stack_assign(normal_out);
3169
3170         if(normal_in->link)
3171                 compiler.stack_assign(normal_in);
3172         
3173         /* pack all parameters in the node */
3174         compiler.add_node(NODE_SET_BUMP,
3175                 compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
3176                 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
3177                         dy_in->stack_offset, strength_in->stack_offset),
3178                 normal_out->stack_offset);
3179 }
3180
3181 void BumpNode::compile(OSLCompiler& compiler)
3182 {
3183         compiler.parameter("invert", invert);
3184         compiler.add(this, "node_bump");
3185 }
3186
3187 /* RGBCurvesNode */
3188
3189 RGBCurvesNode::RGBCurvesNode()
3190 : ShaderNode("rgb_curves")
3191 {
3192         add_input("Fac", SHADER_SOCKET_FLOAT);
3193         add_input("Color", SHADER_SOCKET_COLOR);
3194         add_output("Color", SHADER_SOCKET_COLOR);
3195 }
3196
3197 void RGBCurvesNode::compile(SVMCompiler& compiler)
3198 {
3199         ShaderInput *fac_in = input("Fac");
3200         ShaderInput *color_in = input("Color");
3201         ShaderOutput *color_out = output("Color");
3202
3203         compiler.stack_assign(fac_in);
3204         compiler.stack_assign(color_in);
3205         compiler.stack_assign(color_out);
3206
3207         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3208         compiler.add_array(curves, RAMP_TABLE_SIZE);
3209 }
3210
3211 void RGBCurvesNode::compile(OSLCompiler& compiler)
3212 {
3213         float ramp[RAMP_TABLE_SIZE][3];
3214
3215         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3216                 ramp[i][0] = curves[i].x;
3217                 ramp[i][1] = curves[i].y;
3218                 ramp[i][2] = curves[i].z;
3219         }
3220
3221         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3222         compiler.add(this, "node_rgb_curves");
3223 }
3224
3225 /* VectorCurvesNode */
3226
3227 VectorCurvesNode::VectorCurvesNode()
3228 : ShaderNode("rgb_curves")
3229 {
3230         add_input("Fac", SHADER_SOCKET_FLOAT);
3231         add_input("Vector", SHADER_SOCKET_VECTOR);
3232         add_output("Vector", SHADER_SOCKET_VECTOR);
3233 }
3234
3235 void VectorCurvesNode::compile(SVMCompiler& compiler)
3236 {
3237         ShaderInput *fac_in = input("Fac");
3238         ShaderInput *vector_in = input("Vector");
3239         ShaderOutput *vector_out = output("Vector");
3240
3241         compiler.stack_assign(fac_in);
3242         compiler.stack_assign(vector_in);
3243         compiler.stack_assign(vector_out);
3244
3245         compiler.add_node(NODE_VECTOR_CURVES, fac_in->stack_offset, vector_in->stack_offset, vector_out->stack_offset);
3246         compiler.add_array(curves, RAMP_TABLE_SIZE);
3247 }
3248
3249 void VectorCurvesNode::compile(OSLCompiler& compiler)
3250 {
3251         float ramp[RAMP_TABLE_SIZE][3];
3252
3253         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3254                 ramp[i][0] = curves[i].x;
3255                 ramp[i][1] = curves[i].y;
3256                 ramp[i][2] = curves[i].z;
3257         }
3258
3259         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3260         compiler.add(this, "node_vector_curves");
3261 }
3262
3263 /* RGBRampNode */
3264
3265 RGBRampNode::RGBRampNode()
3266 : ShaderNode("rgb_ramp")
3267 {
3268         add_input("Fac", SHADER_SOCKET_FLOAT);
3269         add_output("Color", SHADER_SOCKET_COLOR);
3270         add_output("Alpha", SHADER_SOCKET_FLOAT);
3271
3272         interpolate = true;
3273 }
3274
3275 void RGBRampNode::compile(SVMCompiler& compiler)
3276 {
3277         ShaderInput *fac_in = input("Fac");
3278         ShaderOutput *color_out = output("Color");
3279         ShaderOutput *alpha_out = output("Alpha");
3280
3281         compiler.stack_assign(fac_in);
3282         if(!color_out->links.empty())
3283                 compiler.stack_assign(color_out);
3284         if(!alpha_out->links.empty())
3285                 compiler.stack_assign(alpha_out);
3286
3287         compiler.add_node(NODE_RGB_RAMP,
3288                 compiler.encode_uchar4(
3289                         fac_in->stack_offset,
3290                         color_out->stack_offset,
3291                         alpha_out->stack_offset),
3292                 interpolate);
3293         compiler.add_array(ramp, RAMP_TABLE_SIZE);
3294 }
3295
3296 void RGBRampNode::compile(OSLCompiler& compiler)
3297 {
3298         /* OSL shader only takes separate RGB and A array, split the RGBA base array */
3299         /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
3300         float ramp_color[RAMP_TABLE_SIZE][3];
3301         float ramp_alpha[RAMP_TABLE_SIZE];
3302
3303         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3304                 ramp_color[i][0] = ramp[i].x;
3305                 ramp_color[i][1] = ramp[i].y;
3306                 ramp_color[i][2] = ramp[i].z;
3307                 ramp_alpha[i] = ramp[i].w;
3308         }
3309
3310         compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
3311         compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
3312         compiler.parameter("ramp_interpolate", interpolate);
3313         
3314         compiler.add(this, "node_rgb_ramp");
3315 }
3316
3317 /* Set Normal Node */
3318
3319 SetNormalNode::SetNormalNode()
3320 : ShaderNode("set_normal")
3321 {
3322         add_input("Direction", SHADER_SOCKET_VECTOR);
3323         add_output("Normal", SHADER_SOCKET_NORMAL);
3324 }
3325
3326 void SetNormalNode::compile(SVMCompiler& compiler)
3327 {
3328         ShaderInput  *direction_in = input("Direction");
3329         ShaderOutput *normal_out = output("Normal");
3330
3331         compiler.stack_assign(direction_in);
3332         compiler.stack_assign(normal_out);
3333
3334         compiler.add_node(NODE_CLOSURE_SET_NORMAL, direction_in->stack_offset, normal_out->stack_offset);
3335 }
3336
3337 void SetNormalNode::compile(OSLCompiler& compiler)
3338 {
3339         compiler.add(this, "node_set_normal"); 
3340 }
3341
3342 /* OSLScriptNode */
3343
3344 OSLScriptNode::OSLScriptNode()
3345 : ShaderNode("osl_script")
3346 {
3347 }
3348
3349 void OSLScriptNode::compile(SVMCompiler& compiler)
3350 {
3351         /* doesn't work for SVM, obviously ... */
3352 }
3353
3354 void OSLScriptNode::compile(OSLCompiler& compiler)
3355 {
3356         if(!filepath.empty())
3357                 compiler.add(this, filepath.c_str(), true);
3358         else
3359                 compiler.add(this, bytecode_hash.c_str(), false);
3360 }
3361
3362 /* Normal Map */
3363
3364 static ShaderEnum normal_map_space_init()
3365 {
3366         ShaderEnum enm;
3367
3368         enm.insert("Tangent", NODE_NORMAL_MAP_TANGENT);
3369         enm.insert("Object", NODE_NORMAL_MAP_OBJECT);
3370         enm.insert("World", NODE_NORMAL_MAP_WORLD);
3371         enm.insert("Blender Object", NODE_NORMAL_MAP_BLENDER_OBJECT);
3372         enm.insert("Blender World", NODE_NORMAL_MAP_BLENDER_WORLD);
3373
3374         return enm;
3375 }
3376
3377 ShaderEnum NormalMapNode::space_enum = normal_map_space_init();
3378
3379 NormalMapNode::NormalMapNode()
3380 : ShaderNode("normal_map")
3381 {
3382         space = ustring("Tangent");
3383         attribute = ustring("");
3384
3385         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3386         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3387         add_input("Color", SHADER_SOCKET_COLOR);
3388
3389         add_output("Normal", SHADER_SOCKET_NORMAL);
3390 }
3391
3392 void NormalMapNode::attributes(AttributeRequestSet *attributes)
3393 {
3394         if(space == ustring("Tangent")) {
3395                 if(attribute == ustring("")) {
3396                         attributes->add(ATTR_STD_UV_TANGENT);
3397                         attributes->add(ATTR_STD_UV_TANGENT_SIGN);
3398                 }
3399                 else {
3400                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3401                         attributes->add(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3402                 }
3403
3404                 attributes->add(ATTR_STD_VERTEX_NORMAL);
3405         }
3406         
3407         ShaderNode::attributes(attributes);
3408 }
3409
3410 void NormalMapNode::compile(SVMCompiler& compiler)
3411 {
3412         ShaderInput  *color_in = input("Color");
3413         ShaderInput  *strength_in = input("Strength");
3414         ShaderOutput *normal_out = output("Normal");
3415         int attr = 0, attr_sign = 0;
3416
3417         if(space == ustring("Tangent")) {
3418                 if(attribute == ustring("")) {
3419                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3420                         attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
3421                 }
3422                 else {
3423                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3424                         attr_sign = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3425                 }
3426         }
3427
3428         compiler.stack_assign(color_in);
3429         compiler.stack_assign(strength_in);
3430         compiler.stack_assign(normal_out);
3431
3432         compiler.add_node(NODE_NORMAL_MAP,
3433                 compiler.encode_uchar4(
3434                         color_in->stack_offset,
3435                         strength_in->stack_offset,
3436                         normal_out->stack_offset,
3437                         space_enum[space]),
3438                 attr, attr_sign);
3439 }
3440
3441 void NormalMapNode::compile(OSLCompiler& compiler)
3442 {
3443         if(space == ustring("Tangent")) {
3444                 if(attribute == ustring("")) {
3445                         compiler.parameter("attr_name", ustring("geom:tangent"));
3446                         compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
3447                 }
3448                 else {
3449                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3450                         compiler.parameter("attr_sign_name", ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3451                 }
3452         }
3453
3454         compiler.parameter("space", space);
3455
3456         compiler.add(this, "node_normal_map"); 
3457 }
3458
3459 /* Tangent */
3460
3461 static ShaderEnum tangent_direction_type_init()
3462 {
3463         ShaderEnum enm;
3464
3465         enm.insert("Radial", NODE_TANGENT_RADIAL);
3466         enm.insert("UV Map", NODE_TANGENT_UVMAP);
3467
3468         return enm;
3469 }
3470
3471 static ShaderEnum tangent_axis_init()
3472 {
3473         ShaderEnum enm;
3474
3475         enm.insert("X", NODE_TANGENT_AXIS_X);
3476         enm.insert("Y", NODE_TANGENT_AXIS_Y);
3477         enm.insert("Z", NODE_TANGENT_AXIS_Z);
3478
3479         return enm;
3480 }
3481
3482 ShaderEnum TangentNode::direction_type_enum = tangent_direction_type_init();
3483 ShaderEnum TangentNode::axis_enum = tangent_axis_init();
3484
3485 TangentNode::TangentNode()
3486 : ShaderNode("normal_map")
3487 {
3488         direction_type = ustring("Radial");
3489         axis = ustring("X");
3490         attribute = ustring("");
3491
3492         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3493         add_output("Tangent", SHADER_SOCKET_NORMAL);
3494 }
3495
3496 void TangentNode::attributes(AttributeRequestSet *attributes)
3497 {
3498         if(direction_type == ustring("UV Map")) {
3499                 if(attribute == ustring(""))
3500                         attributes->add(ATTR_STD_UV_TANGENT);
3501                 else
3502                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3503         }
3504         else
3505                 attributes->add(ATTR_STD_GENERATED);
3506         
3507         ShaderNode::attributes(attributes);
3508 }
3509
3510 void TangentNode::compile(SVMCompiler& compiler)
3511 {
3512         ShaderOutput *tangent_out = output("Tangent");
3513         int attr;
3514
3515         if(direction_type == ustring("UV Map")) {
3516                 if(attribute == ustring(""))
3517                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3518                 else
3519                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3520         }
3521         else
3522                 attr = compiler.attribute(ATTR_STD_GENERATED);
3523
3524         compiler.stack_assign(tangent_out);
3525
3526         compiler.add_node(NODE_TANGENT,
3527                 compiler.encode_uchar4(
3528                         tangent_out->stack_offset,
3529                         direction_type_enum[direction_type],
3530                         axis_enum[axis]), attr);
3531 }
3532
3533 void TangentNode::compile(OSLCompiler& compiler)
3534 {
3535         if(direction_type == ustring("UV Map")) {
3536                 if(attribute == ustring(""))
3537                         compiler.parameter("attr_name", ustring("geom:tangent"));
3538                 else
3539                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3540         }
3541
3542         compiler.parameter("direction_type", direction_type);
3543         compiler.parameter("axis", axis);
3544         compiler.add(this, "node_tangent"); 
3545 }
3546
3547 CCL_NAMESPACE_END
3548