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