82afab4dc1a793a1fb4e7aff76327b2c77f3d167
[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("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1582         add_output("AO", SHADER_SOCKET_CLOSURE);
1583 }
1584
1585 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
1586 {
1587         ShaderInput *color_in = input("Color");
1588
1589         if(color_in->link) {
1590                 compiler.stack_assign(color_in);
1591                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1592         }
1593         else
1594                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1595
1596         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
1597 }
1598
1599 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
1600 {
1601         compiler.add(this, "node_ambient_occlusion");
1602 }
1603
1604 /* Volume Closure */
1605
1606 VolumeNode::VolumeNode()
1607 : ShaderNode("volume")
1608 {
1609         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1610
1611         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1612         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1613
1614         add_output("Volume", SHADER_SOCKET_CLOSURE);
1615 }
1616
1617 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1618 {
1619         ShaderInput *color_in = input("Color");
1620
1621         if(color_in->link) {
1622                 compiler.stack_assign(color_in);
1623                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1624         }
1625         else
1626                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1627         
1628         if(param1)
1629                 compiler.stack_assign(param1);
1630         if(param2)
1631                 compiler.stack_assign(param2);
1632
1633         compiler.add_node(NODE_CLOSURE_VOLUME,
1634                 compiler.encode_uchar4(closure,
1635                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1636                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1637                         compiler.closure_mix_weight_offset()),
1638                 __float_as_int((param1)? param1->value.x: 0.0f),
1639                 __float_as_int((param2)? param2->value.x: 0.0f));
1640 }
1641
1642 void VolumeNode::compile(SVMCompiler& compiler)
1643 {
1644         compile(compiler, NULL, NULL);
1645 }
1646
1647 void VolumeNode::compile(OSLCompiler& compiler)
1648 {
1649         assert(0);
1650 }
1651
1652 /* Transparent Volume Closure */
1653
1654 TransparentVolumeNode::TransparentVolumeNode()
1655 {
1656         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1657 }
1658
1659 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1660 {
1661         VolumeNode::compile(compiler, input("Density"), NULL);
1662 }
1663
1664 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1665 {
1666         compiler.add(this, "node_isotropic_volume");
1667 }
1668
1669 /* Isotropic Volume Closure */
1670
1671 IsotropicVolumeNode::IsotropicVolumeNode()
1672 {
1673         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1674 }
1675
1676 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1677 {
1678         VolumeNode::compile(compiler, input("Density"), NULL);
1679 }
1680
1681 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1682 {
1683         compiler.add(this, "node_isotropic_volume");
1684 }
1685
1686 /* Geometry */
1687
1688 GeometryNode::GeometryNode()
1689 : ShaderNode("geometry")
1690 {
1691         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1692         add_output("Position", SHADER_SOCKET_POINT);
1693         add_output("Normal", SHADER_SOCKET_NORMAL);
1694         add_output("Tangent", SHADER_SOCKET_NORMAL);
1695         add_output("True Normal", SHADER_SOCKET_NORMAL);
1696         add_output("Incoming", SHADER_SOCKET_VECTOR);
1697         add_output("Parametric", SHADER_SOCKET_POINT);
1698         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1699 }
1700
1701 void GeometryNode::attributes(AttributeRequestSet *attributes)
1702 {
1703         if(!output("Tangent")->links.empty())
1704                 attributes->add(ATTR_STD_GENERATED);
1705
1706         ShaderNode::attributes(attributes);
1707 }
1708
1709 void GeometryNode::compile(SVMCompiler& compiler)
1710 {
1711         ShaderOutput *out;
1712         NodeType geom_node = NODE_GEOMETRY;
1713
1714         if(bump == SHADER_BUMP_DX)
1715                 geom_node = NODE_GEOMETRY_BUMP_DX;
1716         else if(bump == SHADER_BUMP_DY)
1717                 geom_node = NODE_GEOMETRY_BUMP_DY;
1718         
1719         out = output("Position");
1720         if(!out->links.empty()) {
1721                 compiler.stack_assign(out);
1722                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1723         }
1724
1725         out = output("Normal");
1726         if(!out->links.empty()) {
1727                 compiler.stack_assign(out);
1728                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1729         }
1730
1731         out = output("Tangent");
1732         if(!out->links.empty()) {
1733                 compiler.stack_assign(out);
1734                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1735         }
1736
1737         out = output("True Normal");
1738         if(!out->links.empty()) {
1739                 compiler.stack_assign(out);
1740                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1741         }
1742
1743         out = output("Incoming");
1744         if(!out->links.empty()) {
1745                 compiler.stack_assign(out);
1746                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1747         }
1748
1749         out = output("Parametric");
1750         if(!out->links.empty()) {
1751                 compiler.stack_assign(out);
1752                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1753         }
1754
1755         out = output("Backfacing");
1756         if(!out->links.empty()) {
1757                 compiler.stack_assign(out);
1758                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1759         }
1760 }
1761
1762 void GeometryNode::compile(OSLCompiler& compiler)
1763 {
1764         if(bump == SHADER_BUMP_DX)
1765                 compiler.parameter("bump_offset", "dx");
1766         else if(bump == SHADER_BUMP_DY)
1767                 compiler.parameter("bump_offset", "dy");
1768         else
1769                 compiler.parameter("bump_offset", "center");
1770
1771         compiler.add(this, "node_geometry");
1772 }
1773
1774 /* TextureCoordinate */
1775
1776 TextureCoordinateNode::TextureCoordinateNode()
1777 : ShaderNode("texture_coordinate")
1778 {
1779         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1780         add_output("Generated", SHADER_SOCKET_POINT);
1781         add_output("Normal", SHADER_SOCKET_NORMAL);
1782         add_output("UV", SHADER_SOCKET_POINT);
1783         add_output("Object", SHADER_SOCKET_POINT);
1784         add_output("Camera", SHADER_SOCKET_POINT);
1785         add_output("Window", SHADER_SOCKET_POINT);
1786         add_output("Reflection", SHADER_SOCKET_NORMAL);
1787
1788         from_dupli = false;
1789 }
1790
1791 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1792 {
1793         if(!from_dupli) {
1794                 if(!output("Generated")->links.empty())
1795                         attributes->add(ATTR_STD_GENERATED);
1796                 if(!output("UV")->links.empty())
1797                         attributes->add(ATTR_STD_UV);
1798         }
1799
1800         ShaderNode::attributes(attributes);
1801 }
1802
1803 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1804 {
1805         ShaderOutput *out;
1806         NodeType texco_node = NODE_TEX_COORD;
1807         NodeType attr_node = NODE_ATTR;
1808         NodeType geom_node = NODE_GEOMETRY;
1809
1810         if(bump == SHADER_BUMP_DX) {
1811                 texco_node = NODE_TEX_COORD_BUMP_DX;
1812                 attr_node = NODE_ATTR_BUMP_DX;
1813                 geom_node = NODE_GEOMETRY_BUMP_DX;
1814         }
1815         else if(bump == SHADER_BUMP_DY) {
1816                 texco_node = NODE_TEX_COORD_BUMP_DY;
1817                 attr_node = NODE_ATTR_BUMP_DY;
1818                 geom_node = NODE_GEOMETRY_BUMP_DY;
1819         }
1820         
1821         out = output("Generated");
1822         if(!out->links.empty()) {
1823                 if(compiler.background) {
1824                         compiler.stack_assign(out);
1825                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1826                 }
1827                 else {
1828                         if(from_dupli) {
1829                                 compiler.stack_assign(out);
1830                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
1831                         }
1832                         else {
1833                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
1834                                 compiler.stack_assign(out);
1835                                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1836                         }
1837                 }
1838         }
1839
1840         out = output("Normal");
1841         if(!out->links.empty()) {
1842                 compiler.stack_assign(out);
1843                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
1844         }
1845
1846         out = output("UV");
1847         if(!out->links.empty()) {
1848                 if(from_dupli) {
1849                         compiler.stack_assign(out);
1850                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
1851                 }
1852                 else {
1853                         int attr = compiler.attribute(ATTR_STD_UV);
1854                         compiler.stack_assign(out);
1855                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1856                 }
1857         }
1858
1859         out = output("Object");
1860         if(!out->links.empty()) {
1861                 compiler.stack_assign(out);
1862                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
1863         }
1864
1865         out = output("Camera");
1866         if(!out->links.empty()) {
1867                 compiler.stack_assign(out);
1868                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
1869         }
1870
1871         out = output("Window");
1872         if(!out->links.empty()) {
1873                 compiler.stack_assign(out);
1874                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
1875         }
1876
1877         out = output("Reflection");
1878         if(!out->links.empty()) {
1879                 if(compiler.background) {
1880                         compiler.stack_assign(out);
1881                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1882                 }
1883                 else {
1884                         compiler.stack_assign(out);
1885                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
1886                 }
1887         }
1888 }
1889
1890 void TextureCoordinateNode::compile(OSLCompiler& compiler)
1891 {
1892         if(bump == SHADER_BUMP_DX)
1893                 compiler.parameter("bump_offset", "dx");
1894         else if(bump == SHADER_BUMP_DY)
1895                 compiler.parameter("bump_offset", "dy");
1896         else
1897                 compiler.parameter("bump_offset", "center");
1898         
1899         if(compiler.background)
1900                 compiler.parameter("is_background", true);
1901         
1902         compiler.parameter("from_dupli", from_dupli);
1903
1904         compiler.add(this, "node_texture_coordinate");
1905 }
1906
1907 /* Light Path */
1908
1909 LightPathNode::LightPathNode()
1910 : ShaderNode("light_path")
1911 {
1912         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
1913         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
1914         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
1915         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
1916         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
1917         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
1918         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
1919         add_output("Ray Length", SHADER_SOCKET_FLOAT);
1920 }
1921
1922 void LightPathNode::compile(SVMCompiler& compiler)
1923 {
1924         ShaderOutput *out;
1925
1926         out = output("Is Camera Ray");
1927         if(!out->links.empty()) {
1928                 compiler.stack_assign(out);
1929                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
1930         }
1931
1932         out = output("Is Shadow Ray");
1933         if(!out->links.empty()) {
1934                 compiler.stack_assign(out);
1935                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
1936         }
1937
1938         out = output("Is Diffuse Ray");
1939         if(!out->links.empty()) {
1940                 compiler.stack_assign(out);
1941                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
1942         }
1943
1944         out = output("Is Glossy Ray");
1945         if(!out->links.empty()) {
1946                 compiler.stack_assign(out);
1947                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
1948         }
1949
1950         out = output("Is Singular Ray");
1951         if(!out->links.empty()) {
1952                 compiler.stack_assign(out);
1953                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
1954         }
1955
1956         out = output("Is Reflection Ray");
1957         if(!out->links.empty()) {
1958                 compiler.stack_assign(out);
1959                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
1960         }
1961
1962
1963         out = output("Is Transmission Ray");
1964         if(!out->links.empty()) {
1965                 compiler.stack_assign(out);
1966                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
1967         }
1968         
1969         out = output("Ray Length");
1970         if(!out->links.empty()) {
1971                 compiler.stack_assign(out);
1972                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
1973         }
1974
1975 }
1976
1977 void LightPathNode::compile(OSLCompiler& compiler)
1978 {
1979         compiler.add(this, "node_light_path");
1980 }
1981
1982 /* Light Falloff */
1983
1984 LightFalloffNode::LightFalloffNode()
1985 : ShaderNode("light_path")
1986 {
1987         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
1988         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1989         add_output("Quadratic", SHADER_SOCKET_FLOAT);
1990         add_output("Linear", SHADER_SOCKET_FLOAT);
1991         add_output("Constant", SHADER_SOCKET_FLOAT);
1992 }
1993
1994 void LightFalloffNode::compile(SVMCompiler& compiler)
1995 {
1996         ShaderInput *strength_in = input("Strength");
1997         ShaderInput *smooth_in = input("Smooth");
1998
1999         compiler.stack_assign(strength_in);
2000         compiler.stack_assign(smooth_in);
2001
2002         ShaderOutput *out = output("Quadratic");
2003         if(!out->links.empty()) {
2004                 compiler.stack_assign(out);
2005                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2006                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2007         }
2008
2009         out = output("Linear");
2010         if(!out->links.empty()) {
2011                 compiler.stack_assign(out);
2012                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2013                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2014         }
2015
2016         out = output("Constant");
2017         if(!out->links.empty()) {
2018                 compiler.stack_assign(out);
2019                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2020                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2021         }
2022 }
2023
2024 void LightFalloffNode::compile(OSLCompiler& compiler)
2025 {
2026         compiler.add(this, "node_light_falloff");
2027 }
2028
2029 /* Object Info */
2030
2031 ObjectInfoNode::ObjectInfoNode()
2032 : ShaderNode("object_info")
2033 {
2034         add_output("Location", SHADER_SOCKET_VECTOR);
2035         add_output("Object Index", SHADER_SOCKET_FLOAT);
2036         add_output("Material Index", SHADER_SOCKET_FLOAT);
2037         add_output("Random", SHADER_SOCKET_FLOAT);
2038 }
2039
2040 void ObjectInfoNode::compile(SVMCompiler& compiler)
2041 {
2042         ShaderOutput *out = output("Location");
2043         if(!out->links.empty()) {
2044                 compiler.stack_assign(out);
2045                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2046         }
2047
2048         out = output("Object Index");
2049         if(!out->links.empty()) {
2050                 compiler.stack_assign(out);
2051                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2052         }
2053
2054         out = output("Material Index");
2055         if(!out->links.empty()) {
2056                 compiler.stack_assign(out);
2057                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2058         }
2059
2060         out = output("Random");
2061         if(!out->links.empty()) {
2062                 compiler.stack_assign(out);
2063                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2064         }
2065 }
2066
2067 void ObjectInfoNode::compile(OSLCompiler& compiler)
2068 {
2069         compiler.add(this, "node_object_info");
2070 }
2071
2072 /* Particle Info */
2073
2074 ParticleInfoNode::ParticleInfoNode()
2075 : ShaderNode("particle_info")
2076 {
2077         add_output("Index", SHADER_SOCKET_FLOAT);
2078         add_output("Age", SHADER_SOCKET_FLOAT);
2079         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2080         add_output("Location", SHADER_SOCKET_POINT);
2081         #if 0   /* not yet supported */
2082         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2083         #endif
2084         add_output("Size", SHADER_SOCKET_FLOAT);
2085         add_output("Velocity", SHADER_SOCKET_VECTOR);
2086         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2087 }
2088
2089 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
2090 {
2091         if(!output("Index")->links.empty())
2092                 attributes->add(ATTR_STD_PARTICLE);
2093         if(!output("Age")->links.empty())
2094                 attributes->add(ATTR_STD_PARTICLE);
2095         if(!output("Lifetime")->links.empty())
2096                 attributes->add(ATTR_STD_PARTICLE);
2097         if(!output("Location")->links.empty())
2098                 attributes->add(ATTR_STD_PARTICLE);
2099         #if 0   /* not yet supported */
2100         if(!output("Rotation")->links.empty())
2101                 attributes->add(ATTR_STD_PARTICLE);
2102         #endif
2103         if(!output("Size")->links.empty())
2104                 attributes->add(ATTR_STD_PARTICLE);
2105         if(!output("Velocity")->links.empty())
2106                 attributes->add(ATTR_STD_PARTICLE);
2107         if(!output("Angular Velocity")->links.empty())
2108                 attributes->add(ATTR_STD_PARTICLE);
2109
2110         ShaderNode::attributes(attributes);
2111 }
2112
2113 void ParticleInfoNode::compile(SVMCompiler& compiler)
2114 {
2115         ShaderOutput *out;
2116         
2117         out = output("Index");
2118         if(!out->links.empty()) {
2119                 compiler.stack_assign(out);
2120                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2121         }
2122         
2123         out = output("Age");
2124         if(!out->links.empty()) {
2125                 compiler.stack_assign(out);
2126                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2127         }
2128         
2129         out = output("Lifetime");
2130         if(!out->links.empty()) {
2131                 compiler.stack_assign(out);
2132                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2133         }
2134         
2135         out = output("Location");
2136         if(!out->links.empty()) {
2137                 compiler.stack_assign(out);
2138                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2139         }
2140         
2141         /* quaternion data is not yet supported by Cycles */
2142 #if 0
2143         out = output("Rotation");
2144         if(!out->links.empty()) {
2145                 compiler.stack_assign(out);
2146                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2147         }
2148 #endif
2149         
2150         out = output("Size");
2151         if(!out->links.empty()) {
2152                 compiler.stack_assign(out);
2153                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2154         }
2155         
2156         out = output("Velocity");
2157         if(!out->links.empty()) {
2158                 compiler.stack_assign(out);
2159                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2160         }
2161         
2162         out = output("Angular Velocity");
2163         if(!out->links.empty()) {
2164                 compiler.stack_assign(out);
2165                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2166         }
2167 }
2168
2169 void ParticleInfoNode::compile(OSLCompiler& compiler)
2170 {
2171         compiler.add(this, "node_particle_info");
2172 }
2173
2174 /* Value */
2175
2176 ValueNode::ValueNode()
2177 : ShaderNode("value")
2178 {
2179         value = 0.0f;
2180
2181         add_output("Value", SHADER_SOCKET_FLOAT);
2182 }
2183
2184 void ValueNode::compile(SVMCompiler& compiler)
2185 {
2186         ShaderOutput *val_out = output("Value");
2187
2188         compiler.stack_assign(val_out);
2189         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2190 }
2191
2192 void ValueNode::compile(OSLCompiler& compiler)
2193 {
2194         compiler.parameter("value_value", value);
2195         compiler.add(this, "node_value");
2196 }
2197
2198 /* Color */
2199
2200 ColorNode::ColorNode()
2201 : ShaderNode("color")
2202 {
2203         value = make_float3(0.0f, 0.0f, 0.0f);
2204
2205         add_output("Color", SHADER_SOCKET_COLOR);
2206 }
2207
2208 void ColorNode::compile(SVMCompiler& compiler)
2209 {
2210         ShaderOutput *color_out = output("Color");
2211
2212         if(color_out && !color_out->links.empty()) {
2213                 compiler.stack_assign(color_out);
2214                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2215                 compiler.add_node(NODE_VALUE_V, value);
2216         }
2217 }
2218
2219 void ColorNode::compile(OSLCompiler& compiler)
2220 {
2221         compiler.parameter_color("color_value", value);
2222
2223         compiler.add(this, "node_value");
2224 }
2225
2226 /* Add Closure */
2227
2228 AddClosureNode::AddClosureNode()
2229 : ShaderNode("add_closure")
2230 {
2231         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2232         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2233         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2234 }
2235
2236 void AddClosureNode::compile(SVMCompiler& compiler)
2237 {
2238         /* handled in the SVM compiler */
2239 }
2240
2241 void AddClosureNode::compile(OSLCompiler& compiler)
2242 {
2243         compiler.add(this, "node_add_closure");
2244 }
2245
2246 /* Mix Closure */
2247
2248 MixClosureNode::MixClosureNode()
2249 : ShaderNode("mix_closure")
2250 {
2251         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2252         
2253         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2254         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2255         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2256         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2257 }
2258
2259 void MixClosureNode::compile(SVMCompiler& compiler)
2260 {
2261         /* handled in the SVM compiler */
2262 }
2263
2264 void MixClosureNode::compile(OSLCompiler& compiler)
2265 {
2266         compiler.add(this, "node_mix_closure");
2267 }
2268
2269 /* Invert */
2270
2271 InvertNode::InvertNode()
2272 : ShaderNode("invert")
2273 {
2274         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2275         add_input("Color", SHADER_SOCKET_COLOR);
2276         add_output("Color",  SHADER_SOCKET_COLOR);
2277 }
2278
2279 void InvertNode::compile(SVMCompiler& compiler)
2280 {
2281         ShaderInput *fac_in = input("Fac");
2282         ShaderInput *color_in = input("Color");
2283         ShaderOutput *color_out = output("Color");
2284
2285         compiler.stack_assign(fac_in);
2286         compiler.stack_assign(color_in);
2287         compiler.stack_assign(color_out);
2288
2289         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2290 }
2291
2292 void InvertNode::compile(OSLCompiler& compiler)
2293 {
2294         compiler.add(this, "node_invert");
2295 }
2296
2297 /* Mix */
2298
2299 MixNode::MixNode()
2300 : ShaderNode("mix")
2301 {
2302         type = ustring("Mix");
2303
2304         use_clamp = false;
2305
2306         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2307         add_input("Color1", SHADER_SOCKET_COLOR);
2308         add_input("Color2", SHADER_SOCKET_COLOR);
2309         add_output("Color",  SHADER_SOCKET_COLOR);
2310 }
2311
2312 static ShaderEnum mix_type_init()
2313 {
2314         ShaderEnum enm;
2315
2316         enm.insert("Mix", NODE_MIX_BLEND);
2317         enm.insert("Add", NODE_MIX_ADD);
2318         enm.insert("Multiply", NODE_MIX_MUL);
2319         enm.insert("Screen", NODE_MIX_SCREEN);
2320         enm.insert("Overlay", NODE_MIX_OVERLAY);
2321         enm.insert("Subtract", NODE_MIX_SUB);
2322         enm.insert("Divide", NODE_MIX_DIV);
2323         enm.insert("Difference", NODE_MIX_DIFF);
2324         enm.insert("Darken", NODE_MIX_DARK);
2325         enm.insert("Lighten", NODE_MIX_LIGHT);
2326         enm.insert("Dodge", NODE_MIX_DODGE);
2327         enm.insert("Burn", NODE_MIX_BURN);
2328         enm.insert("Hue", NODE_MIX_HUE);
2329         enm.insert("Saturation", NODE_MIX_SAT);
2330         enm.insert("Value", NODE_MIX_VAL);
2331         enm.insert("Color", NODE_MIX_COLOR);
2332         enm.insert("Soft Light", NODE_MIX_SOFT);
2333         enm.insert("Linear Light", NODE_MIX_LINEAR);
2334
2335         return enm;
2336 }
2337
2338 ShaderEnum MixNode::type_enum = mix_type_init();
2339
2340 void MixNode::compile(SVMCompiler& compiler)
2341 {
2342         ShaderInput *fac_in = input("Fac");
2343         ShaderInput *color1_in = input("Color1");
2344         ShaderInput *color2_in = input("Color2");
2345         ShaderOutput *color_out = output("Color");
2346
2347         compiler.stack_assign(fac_in);
2348         compiler.stack_assign(color1_in);
2349         compiler.stack_assign(color2_in);
2350         compiler.stack_assign(color_out);
2351
2352         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2353         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2354
2355         if(use_clamp) {
2356                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2357                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2358         }
2359 }
2360
2361 void MixNode::compile(OSLCompiler& compiler)
2362 {
2363         compiler.parameter("type", type);
2364         compiler.parameter("Clamp", use_clamp);
2365         compiler.add(this, "node_mix");
2366 }
2367
2368 /* Combine RGB */
2369 CombineRGBNode::CombineRGBNode()
2370 : ShaderNode("combine_rgb")
2371 {
2372         add_input("R", SHADER_SOCKET_FLOAT);
2373         add_input("G", SHADER_SOCKET_FLOAT);
2374         add_input("B", SHADER_SOCKET_FLOAT);
2375         add_output("Image", SHADER_SOCKET_COLOR);
2376 }
2377
2378 void CombineRGBNode::compile(SVMCompiler& compiler)
2379 {
2380         ShaderInput *red_in = input("R");
2381         ShaderInput *green_in = input("G");
2382         ShaderInput *blue_in = input("B");
2383         ShaderOutput *color_out = output("Image");
2384
2385         compiler.stack_assign(color_out);
2386
2387         compiler.stack_assign(red_in);
2388         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2389
2390         compiler.stack_assign(green_in);
2391         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2392
2393         compiler.stack_assign(blue_in);
2394         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2395 }
2396
2397 void CombineRGBNode::compile(OSLCompiler& compiler)
2398 {
2399         compiler.add(this, "node_combine_rgb");
2400 }
2401
2402 /* Gamma */
2403 GammaNode::GammaNode()
2404 : ShaderNode("gamma")
2405 {
2406         add_input("Color", SHADER_SOCKET_COLOR);
2407         add_input("Gamma", SHADER_SOCKET_FLOAT);
2408         add_output("Color", SHADER_SOCKET_COLOR);
2409 }
2410
2411 void GammaNode::compile(SVMCompiler& compiler)
2412 {
2413         ShaderInput *color_in = input("Color");
2414         ShaderInput *gamma_in = input("Gamma");
2415         ShaderOutput *color_out = output("Color");
2416
2417         compiler.stack_assign(color_in);
2418         compiler.stack_assign(gamma_in);
2419         compiler.stack_assign(color_out);
2420
2421         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2422 }
2423
2424 void GammaNode::compile(OSLCompiler& compiler)
2425 {
2426         compiler.add(this, "node_gamma");
2427 }
2428
2429 /* Bright Contrast */
2430 BrightContrastNode::BrightContrastNode()
2431 : ShaderNode("brightness")
2432 {
2433         add_input("Color", SHADER_SOCKET_COLOR);
2434         add_input("Bright", SHADER_SOCKET_FLOAT);
2435         add_input("Contrast", SHADER_SOCKET_FLOAT);
2436         add_output("Color", SHADER_SOCKET_COLOR);
2437 }
2438
2439 void BrightContrastNode::compile(SVMCompiler& compiler)
2440 {
2441         ShaderInput *color_in = input("Color");
2442         ShaderInput *bright_in = input("Bright");
2443         ShaderInput *contrast_in = input("Contrast");
2444         ShaderOutput *color_out = output("Color");
2445
2446         compiler.stack_assign(color_in);
2447         compiler.stack_assign(bright_in);
2448         compiler.stack_assign(contrast_in);
2449         compiler.stack_assign(color_out);
2450
2451         compiler.add_node(NODE_BRIGHTCONTRAST,
2452                 color_in->stack_offset, color_out->stack_offset,
2453                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2454 }
2455
2456 void BrightContrastNode::compile(OSLCompiler& compiler)
2457 {
2458         compiler.add(this, "node_brightness");
2459 }
2460
2461 /* Separate RGB */
2462 SeparateRGBNode::SeparateRGBNode()
2463 : ShaderNode("separate_rgb")
2464 {
2465         add_input("Image", SHADER_SOCKET_COLOR);
2466         add_output("R", SHADER_SOCKET_FLOAT);
2467         add_output("G", SHADER_SOCKET_FLOAT);
2468         add_output("B", SHADER_SOCKET_FLOAT);
2469 }
2470
2471 void SeparateRGBNode::compile(SVMCompiler& compiler)
2472 {
2473         ShaderInput *color_in = input("Image");
2474         ShaderOutput *red_out = output("R");
2475         ShaderOutput *green_out = output("G");
2476         ShaderOutput *blue_out = output("B");
2477
2478         compiler.stack_assign(color_in);
2479
2480         compiler.stack_assign(red_out);
2481         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2482
2483         compiler.stack_assign(green_out);
2484         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2485
2486         compiler.stack_assign(blue_out);
2487         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2488 }
2489
2490 void SeparateRGBNode::compile(OSLCompiler& compiler)
2491 {
2492         compiler.add(this, "node_separate_rgb");
2493 }
2494
2495 /* Separate RGB */
2496 HSVNode::HSVNode()
2497 : ShaderNode("hsv")
2498 {
2499         add_input("Hue", SHADER_SOCKET_FLOAT);
2500         add_input("Saturation", SHADER_SOCKET_FLOAT);
2501         add_input("Value", SHADER_SOCKET_FLOAT);
2502         add_input("Fac", SHADER_SOCKET_FLOAT);
2503         add_input("Color", SHADER_SOCKET_COLOR);
2504         add_output("Color", SHADER_SOCKET_COLOR);
2505 }
2506
2507 void HSVNode::compile(SVMCompiler& compiler)
2508 {
2509         ShaderInput *hue_in = input("Hue");
2510         ShaderInput *saturation_in = input("Saturation");
2511         ShaderInput *value_in = input("Value");
2512         ShaderInput *fac_in = input("Fac");
2513         ShaderInput *color_in = input("Color");
2514         ShaderOutput *color_out = output("Color");
2515
2516         compiler.stack_assign(hue_in);
2517         compiler.stack_assign(saturation_in);
2518         compiler.stack_assign(value_in);
2519         compiler.stack_assign(fac_in);
2520         compiler.stack_assign(color_in);
2521         compiler.stack_assign(color_out);
2522
2523         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2524         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2525 }
2526
2527 void HSVNode::compile(OSLCompiler& compiler)
2528 {
2529         compiler.add(this, "node_hsv");
2530 }
2531
2532 /* Attribute */
2533
2534 AttributeNode::AttributeNode()
2535 : ShaderNode("attribute")
2536 {
2537         attribute = "";
2538
2539         add_output("Color",  SHADER_SOCKET_COLOR);
2540         add_output("Vector",  SHADER_SOCKET_VECTOR);
2541         add_output("Fac",  SHADER_SOCKET_FLOAT);
2542 }
2543
2544 void AttributeNode::attributes(AttributeRequestSet *attributes)
2545 {
2546         ShaderOutput *color_out = output("Color");
2547         ShaderOutput *vector_out = output("Vector");
2548         ShaderOutput *fac_out = output("Fac");
2549
2550         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2551                 attributes->add(attribute);
2552         
2553         ShaderNode::attributes(attributes);
2554 }
2555
2556 void AttributeNode::compile(SVMCompiler& compiler)
2557 {
2558         ShaderOutput *color_out = output("Color");
2559         ShaderOutput *vector_out = output("Vector");
2560         ShaderOutput *fac_out = output("Fac");
2561         NodeType attr_node = NODE_ATTR;
2562
2563         if(bump == SHADER_BUMP_DX)
2564                 attr_node = NODE_ATTR_BUMP_DX;
2565         else if(bump == SHADER_BUMP_DY)
2566                 attr_node = NODE_ATTR_BUMP_DY;
2567
2568         if(!color_out->links.empty() || !vector_out->links.empty()) {
2569                 int attr = compiler.attribute(attribute);
2570
2571                 if(!color_out->links.empty()) {
2572                         compiler.stack_assign(color_out);
2573                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2574                 }
2575                 if(!vector_out->links.empty()) {
2576                         compiler.stack_assign(vector_out);
2577                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2578                 }
2579         }
2580
2581         if(!fac_out->links.empty()) {
2582                 int attr = compiler.attribute(attribute);
2583
2584                 compiler.stack_assign(fac_out);
2585                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2586         }
2587 }
2588
2589 void AttributeNode::compile(OSLCompiler& compiler)
2590 {
2591         if(bump == SHADER_BUMP_DX)
2592                 compiler.parameter("bump_offset", "dx");
2593         else if(bump == SHADER_BUMP_DY)
2594                 compiler.parameter("bump_offset", "dy");
2595         else
2596                 compiler.parameter("bump_offset", "center");
2597
2598         compiler.parameter("name", attribute.c_str());
2599         compiler.add(this, "node_attribute");
2600 }
2601
2602 /* Camera */
2603
2604 CameraNode::CameraNode()
2605 : ShaderNode("camera")
2606 {
2607         add_output("View Vector",  SHADER_SOCKET_VECTOR);
2608         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
2609         add_output("View Distance",  SHADER_SOCKET_FLOAT);
2610 }
2611
2612 void CameraNode::compile(SVMCompiler& compiler)
2613 {
2614         ShaderOutput *vector_out = output("View Vector");
2615         ShaderOutput *z_depth_out = output("View Z Depth");
2616         ShaderOutput *distance_out = output("View Distance");
2617
2618         compiler.stack_assign(vector_out);
2619         compiler.stack_assign(z_depth_out);
2620         compiler.stack_assign(distance_out);
2621         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2622 }
2623
2624 void CameraNode::compile(OSLCompiler& compiler)
2625 {
2626         compiler.add(this, "node_camera");
2627 }
2628
2629 /* Fresnel */
2630
2631 FresnelNode::FresnelNode()
2632 : ShaderNode("Fresnel")
2633 {
2634         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2635         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2636         add_output("Fac", SHADER_SOCKET_FLOAT);
2637 }
2638
2639 void FresnelNode::compile(SVMCompiler& compiler)
2640 {
2641         ShaderInput *ior_in = input("IOR");
2642         ShaderOutput *fac_out = output("Fac");
2643
2644         compiler.stack_assign(ior_in);
2645         compiler.stack_assign(fac_out);
2646         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2647 }
2648
2649 void FresnelNode::compile(OSLCompiler& compiler)
2650 {
2651         compiler.add(this, "node_fresnel");
2652 }
2653
2654 /* Blend Weight */
2655
2656 LayerWeightNode::LayerWeightNode()
2657 : ShaderNode("LayerWeight")
2658 {
2659         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2660         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2661
2662         add_output("Fresnel", SHADER_SOCKET_FLOAT);
2663         add_output("Facing", SHADER_SOCKET_FLOAT);
2664 }
2665
2666 void LayerWeightNode::compile(SVMCompiler& compiler)
2667 {
2668         ShaderInput *blend_in = input("Blend");
2669
2670         if(blend_in->link)
2671                 compiler.stack_assign(blend_in);
2672
2673         ShaderOutput *fresnel_out = output("Fresnel");
2674         if(!fresnel_out->links.empty()) {
2675                 compiler.stack_assign(fresnel_out);
2676                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2677                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
2678         }
2679
2680         ShaderOutput *facing_out = output("Facing");
2681         if(!facing_out->links.empty()) {
2682                 compiler.stack_assign(facing_out);
2683                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2684                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
2685         }
2686 }
2687
2688 void LayerWeightNode::compile(OSLCompiler& compiler)
2689 {
2690         compiler.add(this, "node_layer_weight");
2691 }
2692
2693 /* Output */
2694
2695 OutputNode::OutputNode()
2696 : ShaderNode("output")
2697 {
2698         add_input("Surface", SHADER_SOCKET_CLOSURE);
2699         add_input("Volume", SHADER_SOCKET_CLOSURE);
2700         add_input("Displacement", SHADER_SOCKET_FLOAT);
2701         add_input("Normal", SHADER_SOCKET_NORMAL);
2702 }
2703
2704 void OutputNode::compile(SVMCompiler& compiler)
2705 {
2706         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
2707                 ShaderInput *displacement_in = input("Displacement");
2708
2709                 if(displacement_in->link) {
2710                         compiler.stack_assign(displacement_in);
2711                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
2712                 }
2713         }
2714 }
2715
2716 void OutputNode::compile(OSLCompiler& compiler)
2717 {
2718         if(compiler.output_type() == SHADER_TYPE_SURFACE)
2719                 compiler.add(this, "node_output_surface");
2720         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
2721                 compiler.add(this, "node_output_volume");
2722         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
2723                 compiler.add(this, "node_output_displacement");
2724 }
2725
2726 /* Math */
2727
2728 MathNode::MathNode()
2729 : ShaderNode("math")
2730 {
2731         type = ustring("Add");
2732
2733         use_clamp = false;
2734
2735         add_input("Value1", SHADER_SOCKET_FLOAT);
2736         add_input("Value2", SHADER_SOCKET_FLOAT);
2737         add_output("Value",  SHADER_SOCKET_FLOAT);
2738 }
2739
2740 static ShaderEnum math_type_init()
2741 {
2742         ShaderEnum enm;
2743
2744         enm.insert("Add", NODE_MATH_ADD);
2745         enm.insert("Subtract", NODE_MATH_SUBTRACT);
2746         enm.insert("Multiply", NODE_MATH_MULTIPLY);
2747         enm.insert("Divide", NODE_MATH_DIVIDE);
2748         enm.insert("Sine", NODE_MATH_SINE);
2749         enm.insert("Cosine", NODE_MATH_COSINE);
2750         enm.insert("Tangent", NODE_MATH_TANGENT);
2751         enm.insert("Arcsine", NODE_MATH_ARCSINE);
2752         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
2753         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
2754         enm.insert("Power", NODE_MATH_POWER);
2755         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
2756         enm.insert("Minimum", NODE_MATH_MINIMUM);
2757         enm.insert("Maximum", NODE_MATH_MAXIMUM);
2758         enm.insert("Round", NODE_MATH_ROUND);
2759         enm.insert("Less Than", NODE_MATH_LESS_THAN);
2760         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
2761
2762         return enm;
2763 }
2764
2765 ShaderEnum MathNode::type_enum = math_type_init();
2766
2767 void MathNode::compile(SVMCompiler& compiler)
2768 {
2769         ShaderInput *value1_in = input("Value1");
2770         ShaderInput *value2_in = input("Value2");
2771         ShaderOutput *value_out = output("Value");
2772
2773         compiler.stack_assign(value1_in);
2774         compiler.stack_assign(value2_in);
2775         compiler.stack_assign(value_out);
2776
2777         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
2778         compiler.add_node(NODE_MATH, value_out->stack_offset);
2779
2780         if(use_clamp) {
2781                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
2782                 compiler.add_node(NODE_MATH, value_out->stack_offset);
2783         }
2784 }
2785
2786 void MathNode::compile(OSLCompiler& compiler)
2787 {
2788         compiler.parameter("type", type);
2789         compiler.parameter("Clamp", use_clamp);
2790         compiler.add(this, "node_math");
2791 }
2792
2793 /* VectorMath */
2794
2795 VectorMathNode::VectorMathNode()
2796 : ShaderNode("vector_math")
2797 {
2798         type = ustring("Add");
2799
2800         add_input("Vector1", SHADER_SOCKET_VECTOR);
2801         add_input("Vector2", SHADER_SOCKET_VECTOR);
2802         add_output("Value",  SHADER_SOCKET_FLOAT);
2803         add_output("Vector",  SHADER_SOCKET_VECTOR);
2804 }
2805
2806 static ShaderEnum vector_math_type_init()
2807 {
2808         ShaderEnum enm;
2809
2810         enm.insert("Add", NODE_VECTOR_MATH_ADD);
2811         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
2812         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
2813         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
2814         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
2815         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
2816
2817         return enm;
2818 }
2819
2820 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
2821
2822 void VectorMathNode::compile(SVMCompiler& compiler)
2823 {
2824         ShaderInput *vector1_in = input("Vector1");
2825         ShaderInput *vector2_in = input("Vector2");
2826         ShaderOutput *value_out = output("Value");
2827         ShaderOutput *vector_out = output("Vector");
2828
2829         compiler.stack_assign(vector1_in);
2830         compiler.stack_assign(vector2_in);
2831         compiler.stack_assign(value_out);
2832         compiler.stack_assign(vector_out);
2833
2834         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
2835         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
2836 }
2837
2838 void VectorMathNode::compile(OSLCompiler& compiler)
2839 {
2840         compiler.parameter("type", type);
2841         compiler.add(this, "node_vector_math");
2842 }
2843
2844 /* BumpNode */
2845
2846 BumpNode::BumpNode()
2847 : ShaderNode("bump")
2848 {
2849         /* this input is used by the user, but after graph transform it is no longer
2850          * used and moved to sampler center/x/y instead */
2851         add_input("Height", SHADER_SOCKET_FLOAT);
2852
2853         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
2854         add_input("SampleX", SHADER_SOCKET_FLOAT);
2855         add_input("SampleY", SHADER_SOCKET_FLOAT);
2856         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
2857         add_input("Strength", SHADER_SOCKET_FLOAT, 0.1f);
2858
2859         add_output("Normal", SHADER_SOCKET_NORMAL);
2860 }
2861
2862 void BumpNode::compile(SVMCompiler& compiler)
2863 {
2864         ShaderInput *center_in = input("SampleCenter");
2865         ShaderInput *dx_in = input("SampleX");
2866         ShaderInput *dy_in = input("SampleY");
2867         ShaderInput *normal_in = input("NormalIn");
2868         ShaderInput *intensity_in = input("Strength");
2869         ShaderOutput *normal_out = output("Normal");
2870
2871         compiler.stack_assign(center_in);
2872         compiler.stack_assign(dx_in);
2873         compiler.stack_assign(dy_in);
2874         compiler.stack_assign(intensity_in);
2875         compiler.stack_assign(normal_out);
2876
2877         if(normal_in->link)
2878                 compiler.stack_assign(normal_in);
2879         
2880         /* pack all parameters in the node */
2881         compiler.add_node(NODE_SET_BUMP,
2882                 normal_in->stack_offset,
2883                 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
2884                         dy_in->stack_offset, intensity_in->stack_offset),
2885                 normal_out->stack_offset);
2886 }
2887
2888 void BumpNode::compile(OSLCompiler& compiler)
2889 {
2890         compiler.add(this, "node_bump");
2891 }
2892
2893 /* RGBCurvesNode */
2894
2895 RGBCurvesNode::RGBCurvesNode()
2896 : ShaderNode("rgb_curves")
2897 {
2898         add_input("Fac", SHADER_SOCKET_FLOAT);
2899         add_input("Color", SHADER_SOCKET_COLOR);
2900         add_output("Color", SHADER_SOCKET_COLOR);
2901 }
2902
2903 void RGBCurvesNode::compile(SVMCompiler& compiler)
2904 {
2905         ShaderInput *fac_in = input("Fac");
2906         ShaderInput *color_in = input("Color");
2907         ShaderOutput *color_out = output("Color");
2908
2909         compiler.stack_assign(fac_in);
2910         compiler.stack_assign(color_in);
2911         compiler.stack_assign(color_out);
2912
2913         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2914         compiler.add_array(curves, RAMP_TABLE_SIZE);
2915 }
2916
2917 void RGBCurvesNode::compile(OSLCompiler& compiler)
2918 {
2919         compiler.add(this, "node_rgb_curves");
2920 }
2921
2922 /* RGBRampNode */
2923
2924 RGBRampNode::RGBRampNode()
2925 : ShaderNode("rgb_ramp")
2926 {
2927         add_input("Fac", SHADER_SOCKET_FLOAT);
2928         add_output("Color", SHADER_SOCKET_COLOR);
2929         add_output("Alpha", SHADER_SOCKET_FLOAT);
2930 }
2931
2932 void RGBRampNode::compile(SVMCompiler& compiler)
2933 {
2934         ShaderInput *fac_in = input("Fac");
2935         ShaderOutput *color_out = output("Color");
2936         ShaderOutput *alpha_out = output("Alpha");
2937
2938         compiler.stack_assign(fac_in);
2939         if(!color_out->links.empty())
2940                 compiler.stack_assign(color_out);
2941         if(!alpha_out->links.empty())
2942                 compiler.stack_assign(alpha_out);
2943
2944         compiler.add_node(NODE_RGB_RAMP, fac_in->stack_offset, color_out->stack_offset, alpha_out->stack_offset);
2945         compiler.add_array(ramp, RAMP_TABLE_SIZE);
2946 }
2947
2948 void RGBRampNode::compile(OSLCompiler& compiler)
2949 {
2950         /* OSL shader only takes separate RGB and A array, split the RGBA base array */
2951         /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
2952         float ramp_color[RAMP_TABLE_SIZE][3];
2953         float ramp_alpha[RAMP_TABLE_SIZE];
2954
2955         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
2956                 ramp_color[i][0] = ramp[i].x;
2957                 ramp_color[i][1] = ramp[i].y;
2958                 ramp_color[i][2] = ramp[i].z;
2959                 ramp_alpha[i] = ramp[i].w;
2960         }
2961
2962         compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
2963         compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
2964         
2965         compiler.add(this, "node_rgb_ramp");
2966 }
2967
2968 /* Set Normal Node */
2969
2970 SetNormalNode::SetNormalNode()
2971 : ShaderNode("set_normal")
2972 {
2973         add_input("Direction", SHADER_SOCKET_VECTOR);
2974         add_output("Normal", SHADER_SOCKET_NORMAL);
2975 }
2976
2977 void SetNormalNode::compile(SVMCompiler& compiler)
2978 {
2979         ShaderInput  *direction_in = input("Direction");
2980         ShaderOutput *normal_out = output("Normal");
2981
2982         compiler.stack_assign(direction_in);
2983         compiler.stack_assign(normal_out);
2984
2985         compiler.add_node(NODE_CLOSURE_SET_NORMAL, direction_in->stack_offset, normal_out->stack_offset);
2986 }
2987
2988 void SetNormalNode::compile(OSLCompiler& compiler)
2989 {
2990         compiler.add(this, "node_set_normal"); 
2991 }
2992
2993 /* OSLScriptNode */
2994
2995 OSLScriptNode::OSLScriptNode()
2996 : ShaderNode("osl_script")
2997 {
2998 }
2999
3000 void OSLScriptNode::compile(SVMCompiler& compiler)
3001 {
3002         /* doesn't work for SVM, obviously ... */
3003 }
3004
3005 void OSLScriptNode::compile(OSLCompiler& compiler)
3006 {
3007         if(!filepath.empty())
3008                 compiler.add(this, filepath.c_str(), true);
3009         else
3010                 compiler.add(this, bytecode_hash.c_str(), false);
3011 }
3012
3013 /* Normal Map */
3014
3015 static ShaderEnum normal_map_space_init()
3016 {
3017         ShaderEnum enm;
3018
3019         enm.insert("Tangent", NODE_NORMAL_MAP_TANGENT);
3020         enm.insert("Object", NODE_NORMAL_MAP_OBJECT);
3021         enm.insert("World", NODE_NORMAL_MAP_WORLD);
3022
3023         return enm;
3024 }
3025
3026 ShaderEnum NormalMapNode::space_enum = normal_map_space_init();
3027
3028 NormalMapNode::NormalMapNode()
3029 : ShaderNode("normal_map")
3030 {
3031         space = ustring("Tangent");
3032         attribute = ustring("");
3033
3034         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
3035         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3036         add_input("Color", SHADER_SOCKET_COLOR);
3037
3038         add_output("Normal", SHADER_SOCKET_NORMAL);
3039 }
3040
3041 void NormalMapNode::attributes(AttributeRequestSet *attributes)
3042 {
3043         if(space == ustring("Tangent")) {
3044                 if(attribute == ustring("")) {
3045                         attributes->add(ATTR_STD_UV_TANGENT);
3046                         attributes->add(ATTR_STD_UV_TANGENT_SIGN);
3047                 }
3048                 else {
3049                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3050                         attributes->add(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3051                 }
3052         }
3053         
3054         ShaderNode::attributes(attributes);
3055 }
3056
3057 void NormalMapNode::compile(SVMCompiler& compiler)
3058 {
3059         ShaderInput  *color_in = input("Color");
3060         ShaderInput  *strength_in = input("Strength");
3061         ShaderOutput *normal_out = output("Normal");
3062         int attr = 0, attr_sign = 0;
3063
3064         if(space == ustring("Tangent")) {
3065                 if(attribute == ustring("")) {
3066                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3067                         attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
3068                 }
3069                 else {
3070                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3071                         attr_sign = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3072                 }
3073         }
3074
3075         compiler.stack_assign(color_in);
3076         compiler.stack_assign(strength_in);
3077         compiler.stack_assign(normal_out);
3078
3079         compiler.add_node(NODE_NORMAL_MAP,
3080                 compiler.encode_uchar4(
3081                         color_in->stack_offset,
3082                         strength_in->stack_offset,
3083                         normal_out->stack_offset,
3084                         space_enum[space]),
3085                 attr, attr_sign);
3086 }
3087
3088 void NormalMapNode::compile(OSLCompiler& compiler)
3089 {
3090         if(space == ustring("Tangent")) {
3091                 if(attribute == ustring("")) {
3092                         compiler.parameter("attr_name", ustring("geom:tangent"));
3093                         compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
3094                 }
3095                 else {
3096                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3097                         compiler.parameter("attr_sign_name", ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3098                 }
3099         }
3100
3101         compiler.parameter("space", space);
3102
3103         compiler.add(this, "node_normal_map"); 
3104 }
3105
3106 /* Tangent */
3107
3108 static ShaderEnum tangent_direction_type_init()
3109 {
3110         ShaderEnum enm;
3111
3112         enm.insert("Radial", NODE_TANGENT_RADIAL);
3113         enm.insert("UV Map", NODE_TANGENT_UVMAP);
3114
3115         return enm;
3116 }
3117
3118 static ShaderEnum tangent_axis_init()
3119 {
3120         ShaderEnum enm;
3121
3122         enm.insert("X", NODE_TANGENT_AXIS_X);
3123         enm.insert("Y", NODE_TANGENT_AXIS_Y);
3124         enm.insert("Z", NODE_TANGENT_AXIS_Z);
3125
3126         return enm;
3127 }
3128
3129 ShaderEnum TangentNode::direction_type_enum = tangent_direction_type_init();
3130 ShaderEnum TangentNode::axis_enum = tangent_axis_init();
3131
3132 TangentNode::TangentNode()
3133 : ShaderNode("normal_map")
3134 {
3135         direction_type = ustring("Radial");
3136         axis = ustring("X");
3137         attribute = ustring("");
3138
3139         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
3140         add_output("Tangent", SHADER_SOCKET_NORMAL);
3141 }
3142
3143 void TangentNode::attributes(AttributeRequestSet *attributes)
3144 {
3145         if(direction_type == ustring("UV Map")) {
3146                 if(attribute == ustring(""))
3147                         attributes->add(ATTR_STD_UV_TANGENT);
3148                 else
3149                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3150         }
3151         else
3152                 attributes->add(ATTR_STD_GENERATED);
3153         
3154         ShaderNode::attributes(attributes);
3155 }
3156
3157 void TangentNode::compile(SVMCompiler& compiler)
3158 {
3159         ShaderOutput *tangent_out = output("Tangent");
3160         int attr;
3161
3162         if(direction_type == ustring("UV Map")) {
3163                 if(attribute == ustring(""))
3164                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3165                 else
3166                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3167         }
3168         else
3169                 attr = compiler.attribute(ATTR_STD_GENERATED);
3170
3171         compiler.stack_assign(tangent_out);
3172
3173         compiler.add_node(NODE_TANGENT,
3174                 compiler.encode_uchar4(
3175                         tangent_out->stack_offset,
3176                         direction_type_enum[direction_type],
3177                         axis_enum[axis]), attr);
3178 }
3179
3180 void TangentNode::compile(OSLCompiler& compiler)
3181 {
3182         if(direction_type == ustring("UV Map")) {
3183                 if(attribute == ustring(""))
3184                         compiler.parameter("attr_name", ustring("geom:tangent"));
3185                 else
3186                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3187         }
3188
3189         compiler.parameter("direction_type", direction_type);
3190         compiler.parameter("axis", axis);
3191         compiler.add(this, "node_tangent"); 
3192 }
3193
3194 CCL_NAMESPACE_END
3195