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