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