Fix #32947: cycles color to float conversion issue after integer socket commit.
[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)
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
1235         compiler.add_node(NODE_CLOSURE_BSDF,
1236                 compiler.encode_uchar4(closure,
1237                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1238                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1239                         compiler.closure_mix_weight_offset()),
1240                 __float_as_int((param1)? param1->value.x: 0.0f),
1241                 __float_as_int((param2)? param2->value.x: 0.0f));
1242
1243         if(normal_in->link)
1244                 compiler.stack_assign(normal_in);
1245
1246         if(tangent_in) {
1247                 if(tangent_in->link)
1248                         compiler.stack_assign(tangent_in);
1249
1250                 compiler.add_node(NODE_CLOSURE_BSDF, normal_in->stack_offset, tangent_in->stack_offset);
1251         }
1252         else {
1253                 compiler.add_node(NODE_CLOSURE_BSDF, normal_in->stack_offset);
1254         }
1255 }
1256
1257 void BsdfNode::compile(SVMCompiler& compiler)
1258 {
1259         compile(compiler, NULL, NULL);
1260 }
1261
1262 void BsdfNode::compile(OSLCompiler& compiler)
1263 {
1264         assert(0);
1265 }
1266
1267 /* Ward BSDF Closure */
1268
1269 WardBsdfNode::WardBsdfNode()
1270 {
1271         closure = CLOSURE_BSDF_WARD_ID;
1272
1273         add_input("Tangent", SHADER_SOCKET_VECTOR, ShaderInput::TANGENT);
1274
1275         add_input("Roughness U", SHADER_SOCKET_FLOAT, 0.2f);
1276         add_input("Roughness V", SHADER_SOCKET_FLOAT, 0.2f);
1277 }
1278
1279 void WardBsdfNode::attributes(AttributeRequestSet *attributes)
1280 {
1281         ShaderInput *tangent_in = input("Tangent");
1282
1283         if(!tangent_in->link) {
1284                 attributes->add(ATTR_STD_TANGENT);
1285                 attributes->add(ATTR_STD_GENERATED);
1286         }
1287
1288         ShaderNode::attributes(attributes);
1289 }
1290
1291 void WardBsdfNode::compile(SVMCompiler& compiler)
1292 {
1293         BsdfNode::compile(compiler, input("Roughness U"), input("Roughness V"));
1294 }
1295
1296 void WardBsdfNode::compile(OSLCompiler& compiler)
1297 {
1298         compiler.add(this, "node_ward_bsdf");
1299 }
1300
1301 /* Glossy BSDF Closure */
1302
1303 static ShaderEnum glossy_distribution_init()
1304 {
1305         ShaderEnum enm;
1306
1307         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1308         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1309         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1310
1311         return enm;
1312 }
1313
1314 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1315
1316 GlossyBsdfNode::GlossyBsdfNode()
1317 {
1318         distribution = ustring("Beckmann");
1319
1320         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1321 }
1322
1323 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1324 {
1325         closure = (ClosureType)distribution_enum[distribution];
1326
1327         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1328                 BsdfNode::compile(compiler, NULL, NULL);
1329         else
1330                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1331 }
1332
1333 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1334 {
1335         compiler.parameter("distribution", distribution);
1336         compiler.add(this, "node_glossy_bsdf");
1337 }
1338
1339 /* Glass BSDF Closure */
1340
1341 static ShaderEnum glass_distribution_init()
1342 {
1343         ShaderEnum enm;
1344
1345         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1346         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1347         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1348
1349         return enm;
1350 }
1351
1352 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1353
1354 GlassBsdfNode::GlassBsdfNode()
1355 {
1356         distribution = ustring("Sharp");
1357
1358         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1359         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1360 }
1361
1362 void GlassBsdfNode::compile(SVMCompiler& compiler)
1363 {
1364         closure = (ClosureType)distribution_enum[distribution];
1365
1366         if(closure == CLOSURE_BSDF_REFRACTION_ID)
1367                 BsdfNode::compile(compiler, NULL, input("IOR"));
1368         else
1369                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1370 }
1371
1372 void GlassBsdfNode::compile(OSLCompiler& compiler)
1373 {
1374         compiler.parameter("distribution", distribution);
1375         compiler.add(this, "node_glass_bsdf");
1376 }
1377
1378 /* Velvet BSDF Closure */
1379
1380 VelvetBsdfNode::VelvetBsdfNode()
1381 {
1382         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1383
1384         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1385 }
1386
1387 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1388 {
1389         BsdfNode::compile(compiler, input("Sigma"), NULL);
1390 }
1391
1392 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1393 {
1394         compiler.add(this, "node_velvet_bsdf");
1395 }
1396
1397 /* Diffuse BSDF Closure */
1398
1399 DiffuseBsdfNode::DiffuseBsdfNode()
1400 {
1401         closure = CLOSURE_BSDF_DIFFUSE_ID;
1402         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1403 }
1404
1405 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1406 {
1407         BsdfNode::compile(compiler, input("Roughness"), NULL);
1408 }
1409
1410 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1411 {
1412         compiler.add(this, "node_diffuse_bsdf");
1413 }
1414
1415 /* Translucent BSDF Closure */
1416
1417 TranslucentBsdfNode::TranslucentBsdfNode()
1418 {
1419         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1420 }
1421
1422 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1423 {
1424         BsdfNode::compile(compiler, NULL, NULL);
1425 }
1426
1427 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1428 {
1429         compiler.add(this, "node_translucent_bsdf");
1430 }
1431
1432 /* Transparent BSDF Closure */
1433
1434 TransparentBsdfNode::TransparentBsdfNode()
1435 {
1436         name = "transparent";
1437         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1438 }
1439
1440 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1441 {
1442         BsdfNode::compile(compiler, NULL, NULL);
1443 }
1444
1445 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1446 {
1447         compiler.add(this, "node_transparent_bsdf");
1448 }
1449
1450 /* Emissive Closure */
1451
1452 EmissionNode::EmissionNode()
1453 : ShaderNode("emission")
1454 {
1455         total_power = false;
1456
1457         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1458         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1459         add_output("Emission", SHADER_SOCKET_CLOSURE);
1460 }
1461
1462 void EmissionNode::compile(SVMCompiler& compiler)
1463 {
1464         ShaderInput *color_in = input("Color");
1465         ShaderInput *strength_in = input("Strength");
1466
1467         if(color_in->link || strength_in->link) {
1468                 compiler.stack_assign(color_in);
1469                 compiler.stack_assign(strength_in);
1470                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1471         }
1472         else if(total_power)
1473                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1474         else
1475                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1476
1477         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1478 }
1479
1480 void EmissionNode::compile(OSLCompiler& compiler)
1481 {
1482         compiler.parameter("TotalPower", (total_power)? 1: 0);
1483         compiler.add(this, "node_emission");
1484 }
1485
1486 /* Background Closure */
1487
1488 BackgroundNode::BackgroundNode()
1489 : ShaderNode("background")
1490 {
1491         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1492         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1493         add_output("Background", SHADER_SOCKET_CLOSURE);
1494 }
1495
1496 void BackgroundNode::compile(SVMCompiler& compiler)
1497 {
1498         ShaderInput *color_in = input("Color");
1499         ShaderInput *strength_in = input("Strength");
1500
1501         if(color_in->link || strength_in->link) {
1502                 compiler.stack_assign(color_in);
1503                 compiler.stack_assign(strength_in);
1504                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1505         }
1506         else
1507                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1508
1509         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1510 }
1511
1512 void BackgroundNode::compile(OSLCompiler& compiler)
1513 {
1514         compiler.add(this, "node_background");
1515 }
1516
1517 /* Holdout Closure */
1518
1519 HoldoutNode::HoldoutNode()
1520 : ShaderNode("holdout")
1521 {
1522         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1523 }
1524
1525 void HoldoutNode::compile(SVMCompiler& compiler)
1526 {
1527         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1528 }
1529
1530 void HoldoutNode::compile(OSLCompiler& compiler)
1531 {
1532         compiler.add(this, "node_holdout");
1533 }
1534
1535 /* Volume Closure */
1536
1537 VolumeNode::VolumeNode()
1538 : ShaderNode("volume")
1539 {
1540         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1541
1542         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1543         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1544
1545         add_output("Volume", SHADER_SOCKET_CLOSURE);
1546 }
1547
1548 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1549 {
1550         ShaderInput *color_in = input("Color");
1551
1552         if(color_in->link) {
1553                 compiler.stack_assign(color_in);
1554                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1555         }
1556         else
1557                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1558         
1559         if(param1)
1560                 compiler.stack_assign(param1);
1561         if(param2)
1562                 compiler.stack_assign(param2);
1563
1564         compiler.add_node(NODE_CLOSURE_VOLUME,
1565                 compiler.encode_uchar4(closure,
1566                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1567                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1568                         compiler.closure_mix_weight_offset()),
1569                 __float_as_int((param1)? param1->value.x: 0.0f),
1570                 __float_as_int((param2)? param2->value.x: 0.0f));
1571 }
1572
1573 void VolumeNode::compile(SVMCompiler& compiler)
1574 {
1575         compile(compiler, NULL, NULL);
1576 }
1577
1578 void VolumeNode::compile(OSLCompiler& compiler)
1579 {
1580         assert(0);
1581 }
1582
1583 /* Transparent Volume Closure */
1584
1585 TransparentVolumeNode::TransparentVolumeNode()
1586 {
1587         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1588 }
1589
1590 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1591 {
1592         VolumeNode::compile(compiler, input("Density"), NULL);
1593 }
1594
1595 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1596 {
1597         compiler.add(this, "node_isotropic_volume");
1598 }
1599
1600 /* Isotropic Volume Closure */
1601
1602 IsotropicVolumeNode::IsotropicVolumeNode()
1603 {
1604         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1605 }
1606
1607 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1608 {
1609         VolumeNode::compile(compiler, input("Density"), NULL);
1610 }
1611
1612 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1613 {
1614         compiler.add(this, "node_isotropic_volume");
1615 }
1616
1617 /* Geometry */
1618
1619 GeometryNode::GeometryNode()
1620 : ShaderNode("geometry")
1621 {
1622         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1623         add_output("Position", SHADER_SOCKET_POINT);
1624         add_output("Normal", SHADER_SOCKET_NORMAL);
1625         add_output("Tangent", SHADER_SOCKET_NORMAL);
1626         add_output("True Normal", SHADER_SOCKET_NORMAL);
1627         add_output("Incoming", SHADER_SOCKET_VECTOR);
1628         add_output("Parametric", SHADER_SOCKET_POINT);
1629         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1630 }
1631
1632 void GeometryNode::attributes(AttributeRequestSet *attributes)
1633 {
1634         if(!output("Tangent")->links.empty()) {
1635                 attributes->add(ATTR_STD_TANGENT);
1636                 attributes->add(ATTR_STD_GENERATED);
1637         }
1638
1639         ShaderNode::attributes(attributes);
1640 }
1641
1642 void GeometryNode::compile(SVMCompiler& compiler)
1643 {
1644         ShaderOutput *out;
1645         NodeType geom_node = NODE_GEOMETRY;
1646
1647         if(bump == SHADER_BUMP_DX)
1648                 geom_node = NODE_GEOMETRY_BUMP_DX;
1649         else if(bump == SHADER_BUMP_DY)
1650                 geom_node = NODE_GEOMETRY_BUMP_DY;
1651         
1652         out = output("Position");
1653         if(!out->links.empty()) {
1654                 compiler.stack_assign(out);
1655                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1656         }
1657
1658         out = output("Normal");
1659         if(!out->links.empty()) {
1660                 compiler.stack_assign(out);
1661                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1662         }
1663
1664         out = output("Tangent");
1665         if(!out->links.empty()) {
1666                 compiler.stack_assign(out);
1667                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1668         }
1669
1670         out = output("True Normal");
1671         if(!out->links.empty()) {
1672                 compiler.stack_assign(out);
1673                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1674         }
1675
1676         out = output("Incoming");
1677         if(!out->links.empty()) {
1678                 compiler.stack_assign(out);
1679                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1680         }
1681
1682         out = output("Parametric");
1683         if(!out->links.empty()) {
1684                 compiler.stack_assign(out);
1685                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1686         }
1687
1688         out = output("Backfacing");
1689         if(!out->links.empty()) {
1690                 compiler.stack_assign(out);
1691                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1692         }
1693 }
1694
1695 void GeometryNode::compile(OSLCompiler& compiler)
1696 {
1697         if(bump == SHADER_BUMP_DX)
1698                 compiler.parameter("bump_offset", "dx");
1699         else if(bump == SHADER_BUMP_DY)
1700                 compiler.parameter("bump_offset", "dy");
1701         else
1702                 compiler.parameter("bump_offset", "center");
1703
1704         compiler.add(this, "node_geometry");
1705 }
1706
1707 /* TextureCoordinate */
1708
1709 TextureCoordinateNode::TextureCoordinateNode()
1710 : ShaderNode("texture_coordinate")
1711 {
1712         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1713         add_output("Generated", SHADER_SOCKET_POINT);
1714         add_output("Normal", SHADER_SOCKET_NORMAL);
1715         add_output("UV", SHADER_SOCKET_POINT);
1716         add_output("Object", SHADER_SOCKET_POINT);
1717         add_output("Camera", SHADER_SOCKET_POINT);
1718         add_output("Window", SHADER_SOCKET_POINT);
1719         add_output("Reflection", SHADER_SOCKET_NORMAL);
1720
1721         from_dupli = false;
1722 }
1723
1724 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1725 {
1726         if(!from_dupli) {
1727                 if(!output("Generated")->links.empty())
1728                         attributes->add(ATTR_STD_GENERATED);
1729                 if(!output("UV")->links.empty())
1730                         attributes->add(ATTR_STD_UV);
1731         }
1732
1733         ShaderNode::attributes(attributes);
1734 }
1735
1736 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1737 {
1738         ShaderOutput *out;
1739         NodeType texco_node = NODE_TEX_COORD;
1740         NodeType attr_node = NODE_ATTR;
1741         NodeType geom_node = NODE_GEOMETRY;
1742
1743         if(bump == SHADER_BUMP_DX) {
1744                 texco_node = NODE_TEX_COORD_BUMP_DX;
1745                 attr_node = NODE_ATTR_BUMP_DX;
1746                 geom_node = NODE_GEOMETRY_BUMP_DX;
1747         }
1748         else if(bump == SHADER_BUMP_DY) {
1749                 texco_node = NODE_TEX_COORD_BUMP_DY;
1750                 attr_node = NODE_ATTR_BUMP_DY;
1751                 geom_node = NODE_GEOMETRY_BUMP_DY;
1752         }
1753         
1754         out = output("Generated");
1755         if(!out->links.empty()) {
1756                 if(compiler.background) {
1757                         compiler.stack_assign(out);
1758                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1759                 }
1760                 else {
1761                         if(from_dupli) {
1762                                 compiler.stack_assign(out);
1763                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
1764                         }
1765                         else {
1766                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
1767                                 compiler.stack_assign(out);
1768                                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1769                         }
1770                 }
1771         }
1772
1773         out = output("Normal");
1774         if(!out->links.empty()) {
1775                 compiler.stack_assign(out);
1776                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
1777         }
1778
1779         out = output("UV");
1780         if(!out->links.empty()) {
1781                 if(from_dupli) {
1782                         compiler.stack_assign(out);
1783                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
1784                 }
1785                 else {
1786                         int attr = compiler.attribute(ATTR_STD_UV);
1787                         compiler.stack_assign(out);
1788                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1789                 }
1790         }
1791
1792         out = output("Object");
1793         if(!out->links.empty()) {
1794                 compiler.stack_assign(out);
1795                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
1796         }
1797
1798         out = output("Camera");
1799         if(!out->links.empty()) {
1800                 compiler.stack_assign(out);
1801                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
1802         }
1803
1804         out = output("Window");
1805         if(!out->links.empty()) {
1806                 compiler.stack_assign(out);
1807                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
1808         }
1809
1810         out = output("Reflection");
1811         if(!out->links.empty()) {
1812                 if(compiler.background) {
1813                         compiler.stack_assign(out);
1814                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1815                 }
1816                 else {
1817                         compiler.stack_assign(out);
1818                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
1819                 }
1820         }
1821 }
1822
1823 void TextureCoordinateNode::compile(OSLCompiler& compiler)
1824 {
1825         if(bump == SHADER_BUMP_DX)
1826                 compiler.parameter("bump_offset", "dx");
1827         else if(bump == SHADER_BUMP_DY)
1828                 compiler.parameter("bump_offset", "dy");
1829         else
1830                 compiler.parameter("bump_offset", "center");
1831         
1832         if(compiler.background)
1833                 compiler.parameter("is_background", true);
1834         
1835         compiler.parameter("from_dupli", from_dupli);
1836
1837         compiler.add(this, "node_texture_coordinate");
1838 }
1839
1840 /* Light Path */
1841
1842 LightPathNode::LightPathNode()
1843 : ShaderNode("light_path")
1844 {
1845         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
1846         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
1847         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
1848         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
1849         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
1850         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
1851         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
1852         add_output("Ray Length", SHADER_SOCKET_FLOAT);
1853 }
1854
1855 void LightPathNode::compile(SVMCompiler& compiler)
1856 {
1857         ShaderOutput *out;
1858
1859         out = output("Is Camera Ray");
1860         if(!out->links.empty()) {
1861                 compiler.stack_assign(out);
1862                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
1863         }
1864
1865         out = output("Is Shadow Ray");
1866         if(!out->links.empty()) {
1867                 compiler.stack_assign(out);
1868                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
1869         }
1870
1871         out = output("Is Diffuse Ray");
1872         if(!out->links.empty()) {
1873                 compiler.stack_assign(out);
1874                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
1875         }
1876
1877         out = output("Is Glossy Ray");
1878         if(!out->links.empty()) {
1879                 compiler.stack_assign(out);
1880                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
1881         }
1882
1883         out = output("Is Singular Ray");
1884         if(!out->links.empty()) {
1885                 compiler.stack_assign(out);
1886                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
1887         }
1888
1889         out = output("Is Reflection Ray");
1890         if(!out->links.empty()) {
1891                 compiler.stack_assign(out);
1892                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
1893         }
1894
1895
1896         out = output("Is Transmission Ray");
1897         if(!out->links.empty()) {
1898                 compiler.stack_assign(out);
1899                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
1900         }
1901         
1902         out = output("Ray Length");
1903         if(!out->links.empty()) {
1904                 compiler.stack_assign(out);
1905                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
1906         }
1907
1908 }
1909
1910 void LightPathNode::compile(OSLCompiler& compiler)
1911 {
1912         compiler.add(this, "node_light_path");
1913 }
1914
1915 /* Light Falloff */
1916
1917 LightFalloffNode::LightFalloffNode()
1918 : ShaderNode("light_path")
1919 {
1920         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
1921         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1922         add_output("Quadratic", SHADER_SOCKET_FLOAT);
1923         add_output("Linear", SHADER_SOCKET_FLOAT);
1924         add_output("Constant", SHADER_SOCKET_FLOAT);
1925 }
1926
1927 void LightFalloffNode::compile(SVMCompiler& compiler)
1928 {
1929         ShaderInput *strength_in = input("Strength");
1930         ShaderInput *smooth_in = input("Smooth");
1931
1932         compiler.stack_assign(strength_in);
1933         compiler.stack_assign(smooth_in);
1934
1935         ShaderOutput *out = output("Quadratic");
1936         if(!out->links.empty()) {
1937                 compiler.stack_assign(out);
1938                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
1939                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1940         }
1941
1942         out = output("Linear");
1943         if(!out->links.empty()) {
1944                 compiler.stack_assign(out);
1945                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
1946                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1947         }
1948
1949         out = output("Constant");
1950         if(!out->links.empty()) {
1951                 compiler.stack_assign(out);
1952                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
1953                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1954         }
1955 }
1956
1957 void LightFalloffNode::compile(OSLCompiler& compiler)
1958 {
1959         compiler.add(this, "node_light_falloff");
1960 }
1961
1962 /* Object Info */
1963
1964 ObjectInfoNode::ObjectInfoNode()
1965 : ShaderNode("object_info")
1966 {
1967         add_output("Location", SHADER_SOCKET_VECTOR);
1968         add_output("Object Index", SHADER_SOCKET_FLOAT);
1969         add_output("Material Index", SHADER_SOCKET_FLOAT);
1970         add_output("Random", SHADER_SOCKET_FLOAT);
1971 }
1972
1973 void ObjectInfoNode::compile(SVMCompiler& compiler)
1974 {
1975         ShaderOutput *out = output("Location");
1976         if(!out->links.empty()) {
1977                 compiler.stack_assign(out);
1978                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
1979         }
1980
1981         out = output("Object Index");
1982         if(!out->links.empty()) {
1983                 compiler.stack_assign(out);
1984                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
1985         }
1986
1987         out = output("Material Index");
1988         if(!out->links.empty()) {
1989                 compiler.stack_assign(out);
1990                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
1991         }
1992
1993         out = output("Random");
1994         if(!out->links.empty()) {
1995                 compiler.stack_assign(out);
1996                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
1997         }
1998 }
1999
2000 void ObjectInfoNode::compile(OSLCompiler& compiler)
2001 {
2002         compiler.add(this, "node_object_info");
2003 }
2004
2005 /* Particle Info */
2006
2007 ParticleInfoNode::ParticleInfoNode()
2008 : ShaderNode("particle_info")
2009 {
2010         add_output("Index", SHADER_SOCKET_FLOAT);
2011         add_output("Age", SHADER_SOCKET_FLOAT);
2012         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2013         add_output("Location", SHADER_SOCKET_POINT);
2014         #if 0   /* not yet supported */
2015         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2016         #endif
2017         add_output("Size", SHADER_SOCKET_FLOAT);
2018         add_output("Velocity", SHADER_SOCKET_VECTOR);
2019         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2020 }
2021
2022 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
2023 {
2024         if(!output("Index")->links.empty())
2025                 attributes->add(ATTR_STD_PARTICLE);
2026         if(!output("Age")->links.empty())
2027                 attributes->add(ATTR_STD_PARTICLE);
2028         if(!output("Lifetime")->links.empty())
2029                 attributes->add(ATTR_STD_PARTICLE);
2030         if(!output("Location")->links.empty())
2031                 attributes->add(ATTR_STD_PARTICLE);
2032         #if 0   /* not yet supported */
2033         if(!output("Rotation")->links.empty())
2034                 attributes->add(ATTR_STD_PARTICLE);
2035         #endif
2036         if(!output("Size")->links.empty())
2037                 attributes->add(ATTR_STD_PARTICLE);
2038         if(!output("Velocity")->links.empty())
2039                 attributes->add(ATTR_STD_PARTICLE);
2040         if(!output("Angular Velocity")->links.empty())
2041                 attributes->add(ATTR_STD_PARTICLE);
2042
2043         ShaderNode::attributes(attributes);
2044 }
2045
2046 void ParticleInfoNode::compile(SVMCompiler& compiler)
2047 {
2048         ShaderOutput *out;
2049         
2050         out = output("Index");
2051         if(!out->links.empty()) {
2052                 compiler.stack_assign(out);
2053                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2054         }
2055         
2056         out = output("Age");
2057         if(!out->links.empty()) {
2058                 compiler.stack_assign(out);
2059                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2060         }
2061         
2062         out = output("Lifetime");
2063         if(!out->links.empty()) {
2064                 compiler.stack_assign(out);
2065                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2066         }
2067         
2068         out = output("Location");
2069         if(!out->links.empty()) {
2070                 compiler.stack_assign(out);
2071                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2072         }
2073         
2074         #if 0   /* XXX Quaternion data is not yet supported by Cycles */
2075         out = output("Rotation");
2076         if(!out->links.empty()) {
2077                 compiler.stack_assign(out);
2078                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2079         }
2080         #endif
2081         
2082         out = output("Size");
2083         if(!out->links.empty()) {
2084                 compiler.stack_assign(out);
2085                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2086         }
2087         
2088         out = output("Velocity");
2089         if(!out->links.empty()) {
2090                 compiler.stack_assign(out);
2091                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2092         }
2093         
2094         out = output("Angular Velocity");
2095         if(!out->links.empty()) {
2096                 compiler.stack_assign(out);
2097                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2098         }
2099 }
2100
2101 void ParticleInfoNode::compile(OSLCompiler& compiler)
2102 {
2103         compiler.add(this, "node_particle_info");
2104 }
2105
2106 /* Value */
2107
2108 ValueNode::ValueNode()
2109 : ShaderNode("value")
2110 {
2111         value = 0.0f;
2112
2113         add_output("Value", SHADER_SOCKET_FLOAT);
2114 }
2115
2116 void ValueNode::compile(SVMCompiler& compiler)
2117 {
2118         ShaderOutput *val_out = output("Value");
2119
2120         compiler.stack_assign(val_out);
2121         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2122 }
2123
2124 void ValueNode::compile(OSLCompiler& compiler)
2125 {
2126         compiler.parameter("value_value", value);
2127         compiler.add(this, "node_value");
2128 }
2129
2130 /* Color */
2131
2132 ColorNode::ColorNode()
2133 : ShaderNode("color")
2134 {
2135         value = make_float3(0.0f, 0.0f, 0.0f);
2136
2137         add_output("Color", SHADER_SOCKET_COLOR);
2138 }
2139
2140 void ColorNode::compile(SVMCompiler& compiler)
2141 {
2142         ShaderOutput *color_out = output("Color");
2143
2144         if(color_out && !color_out->links.empty()) {
2145                 compiler.stack_assign(color_out);
2146                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2147                 compiler.add_node(NODE_VALUE_V, value);
2148         }
2149 }
2150
2151 void ColorNode::compile(OSLCompiler& compiler)
2152 {
2153         compiler.parameter_color("color_value", value);
2154
2155         compiler.add(this, "node_value");
2156 }
2157
2158 /* Add Closure */
2159
2160 AddClosureNode::AddClosureNode()
2161 : ShaderNode("add_closure")
2162 {
2163         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2164         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2165         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2166 }
2167
2168 void AddClosureNode::compile(SVMCompiler& compiler)
2169 {
2170         /* handled in the SVM compiler */
2171 }
2172
2173 void AddClosureNode::compile(OSLCompiler& compiler)
2174 {
2175         compiler.add(this, "node_add_closure");
2176 }
2177
2178 /* Mix Closure */
2179
2180 MixClosureNode::MixClosureNode()
2181 : ShaderNode("mix_closure")
2182 {
2183         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2184         
2185         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2186         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2187         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2188         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2189 }
2190
2191 void MixClosureNode::compile(SVMCompiler& compiler)
2192 {
2193         /* handled in the SVM compiler */
2194 }
2195
2196 void MixClosureNode::compile(OSLCompiler& compiler)
2197 {
2198         compiler.add(this, "node_mix_closure");
2199 }
2200
2201 /* Invert */
2202
2203 InvertNode::InvertNode()
2204 : ShaderNode("invert")
2205 {
2206         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2207         add_input("Color", SHADER_SOCKET_COLOR);
2208         add_output("Color",  SHADER_SOCKET_COLOR);
2209 }
2210
2211 void InvertNode::compile(SVMCompiler& compiler)
2212 {
2213         ShaderInput *fac_in = input("Fac");
2214         ShaderInput *color_in = input("Color");
2215         ShaderOutput *color_out = output("Color");
2216
2217         compiler.stack_assign(fac_in);
2218         compiler.stack_assign(color_in);
2219         compiler.stack_assign(color_out);
2220
2221         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2222 }
2223
2224 void InvertNode::compile(OSLCompiler& compiler)
2225 {
2226         compiler.add(this, "node_invert");
2227 }
2228
2229 /* Mix */
2230
2231 MixNode::MixNode()
2232 : ShaderNode("mix")
2233 {
2234         type = ustring("Mix");
2235
2236         use_clamp = false;
2237
2238         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2239         add_input("Color1", SHADER_SOCKET_COLOR);
2240         add_input("Color2", SHADER_SOCKET_COLOR);
2241         add_output("Color",  SHADER_SOCKET_COLOR);
2242 }
2243
2244 static ShaderEnum mix_type_init()
2245 {
2246         ShaderEnum enm;
2247
2248         enm.insert("Mix", NODE_MIX_BLEND);
2249         enm.insert("Add", NODE_MIX_ADD);
2250         enm.insert("Multiply", NODE_MIX_MUL);
2251         enm.insert("Screen", NODE_MIX_SCREEN);
2252         enm.insert("Overlay", NODE_MIX_OVERLAY);
2253         enm.insert("Subtract", NODE_MIX_SUB);
2254         enm.insert("Divide", NODE_MIX_DIV);
2255         enm.insert("Difference", NODE_MIX_DIFF);
2256         enm.insert("Darken", NODE_MIX_DARK);
2257         enm.insert("Lighten", NODE_MIX_LIGHT);
2258         enm.insert("Dodge", NODE_MIX_DODGE);
2259         enm.insert("Burn", NODE_MIX_BURN);
2260         enm.insert("Hue", NODE_MIX_HUE);
2261         enm.insert("Saturation", NODE_MIX_SAT);
2262         enm.insert("Value", NODE_MIX_VAL);
2263         enm.insert("Color", NODE_MIX_COLOR);
2264         enm.insert("Soft Light", NODE_MIX_SOFT);
2265         enm.insert("Linear Light", NODE_MIX_LINEAR);
2266
2267         return enm;
2268 }
2269
2270 ShaderEnum MixNode::type_enum = mix_type_init();
2271
2272 void MixNode::compile(SVMCompiler& compiler)
2273 {
2274         ShaderInput *fac_in = input("Fac");
2275         ShaderInput *color1_in = input("Color1");
2276         ShaderInput *color2_in = input("Color2");
2277         ShaderOutput *color_out = output("Color");
2278
2279         compiler.stack_assign(fac_in);
2280         compiler.stack_assign(color1_in);
2281         compiler.stack_assign(color2_in);
2282         compiler.stack_assign(color_out);
2283
2284         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2285         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2286
2287         if(use_clamp) {
2288                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2289                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2290         }
2291 }
2292
2293 void MixNode::compile(OSLCompiler& compiler)
2294 {
2295         compiler.parameter("type", type);
2296         compiler.parameter("Clamp", use_clamp);
2297         compiler.add(this, "node_mix");
2298 }
2299
2300 /* Combine RGB */
2301 CombineRGBNode::CombineRGBNode()
2302 : ShaderNode("combine_rgb")
2303 {
2304         add_input("R", SHADER_SOCKET_FLOAT);
2305         add_input("G", SHADER_SOCKET_FLOAT);
2306         add_input("B", SHADER_SOCKET_FLOAT);
2307         add_output("Image", SHADER_SOCKET_COLOR);
2308 }
2309
2310 void CombineRGBNode::compile(SVMCompiler& compiler)
2311 {
2312         ShaderInput *red_in = input("R");
2313         ShaderInput *green_in = input("G");
2314         ShaderInput *blue_in = input("B");
2315         ShaderOutput *color_out = output("Image");
2316
2317         compiler.stack_assign(color_out);
2318
2319         compiler.stack_assign(red_in);
2320         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2321
2322         compiler.stack_assign(green_in);
2323         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2324
2325         compiler.stack_assign(blue_in);
2326         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2327 }
2328
2329 void CombineRGBNode::compile(OSLCompiler& compiler)
2330 {
2331         compiler.add(this, "node_combine_rgb");
2332 }
2333
2334 /* Gamma */
2335 GammaNode::GammaNode()
2336 : ShaderNode("gamma")
2337 {
2338         add_input("Color", SHADER_SOCKET_COLOR);
2339         add_input("Gamma", SHADER_SOCKET_FLOAT);
2340         add_output("Color", SHADER_SOCKET_COLOR);
2341 }
2342
2343 void GammaNode::compile(SVMCompiler& compiler)
2344 {
2345         ShaderInput *color_in = input("Color");
2346         ShaderInput *gamma_in = input("Gamma");
2347         ShaderOutput *color_out = output("Color");
2348
2349         compiler.stack_assign(color_in);
2350         compiler.stack_assign(gamma_in);
2351         compiler.stack_assign(color_out);
2352
2353         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2354 }
2355
2356 void GammaNode::compile(OSLCompiler& compiler)
2357 {
2358         compiler.add(this, "node_gamma");
2359 }
2360
2361 /* Bright Contrast */
2362 BrightContrastNode::BrightContrastNode()
2363 : ShaderNode("brightness")
2364 {
2365         add_input("Color", SHADER_SOCKET_COLOR);
2366         add_input("Bright", SHADER_SOCKET_FLOAT);
2367         add_input("Contrast", SHADER_SOCKET_FLOAT);
2368         add_output("Color", SHADER_SOCKET_COLOR);
2369 }
2370
2371 void BrightContrastNode::compile(SVMCompiler& compiler)
2372 {
2373         ShaderInput *color_in = input("Color");
2374         ShaderInput *bright_in = input("Bright");
2375         ShaderInput *contrast_in = input("Contrast");
2376         ShaderOutput *color_out = output("Color");
2377
2378         compiler.stack_assign(color_in);
2379         compiler.stack_assign(bright_in);
2380         compiler.stack_assign(contrast_in);
2381         compiler.stack_assign(color_out);
2382
2383         compiler.add_node(NODE_BRIGHTCONTRAST,
2384                 color_in->stack_offset, color_out->stack_offset,
2385                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2386 }
2387
2388 void BrightContrastNode::compile(OSLCompiler& compiler)
2389 {
2390         compiler.add(this, "node_brightness");
2391 }
2392
2393 /* Separate RGB */
2394 SeparateRGBNode::SeparateRGBNode()
2395 : ShaderNode("separate_rgb")
2396 {
2397         add_input("Image", SHADER_SOCKET_COLOR);
2398         add_output("R", SHADER_SOCKET_FLOAT);
2399         add_output("G", SHADER_SOCKET_FLOAT);
2400         add_output("B", SHADER_SOCKET_FLOAT);
2401 }
2402
2403 void SeparateRGBNode::compile(SVMCompiler& compiler)
2404 {
2405         ShaderInput *color_in = input("Image");
2406         ShaderOutput *red_out = output("R");
2407         ShaderOutput *green_out = output("G");
2408         ShaderOutput *blue_out = output("B");
2409
2410         compiler.stack_assign(color_in);
2411
2412         compiler.stack_assign(red_out);
2413         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2414
2415         compiler.stack_assign(green_out);
2416         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2417
2418         compiler.stack_assign(blue_out);
2419         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2420 }
2421
2422 void SeparateRGBNode::compile(OSLCompiler& compiler)
2423 {
2424         compiler.add(this, "node_separate_rgb");
2425 }
2426
2427 /* Separate RGB */
2428 HSVNode::HSVNode()
2429 : ShaderNode("hsv")
2430 {
2431         add_input("Hue", SHADER_SOCKET_FLOAT);
2432         add_input("Saturation", SHADER_SOCKET_FLOAT);
2433         add_input("Value", SHADER_SOCKET_FLOAT);
2434         add_input("Fac", SHADER_SOCKET_FLOAT);
2435         add_input("Color", SHADER_SOCKET_COLOR);
2436         add_output("Color", SHADER_SOCKET_COLOR);
2437 }
2438
2439 void HSVNode::compile(SVMCompiler& compiler)
2440 {
2441         ShaderInput *hue_in = input("Hue");
2442         ShaderInput *saturation_in = input("Saturation");
2443         ShaderInput *value_in = input("Value");
2444         ShaderInput *fac_in = input("Fac");
2445         ShaderInput *color_in = input("Color");
2446         ShaderOutput *color_out = output("Color");
2447
2448         compiler.stack_assign(hue_in);
2449         compiler.stack_assign(saturation_in);
2450         compiler.stack_assign(value_in);
2451         compiler.stack_assign(fac_in);
2452         compiler.stack_assign(color_in);
2453         compiler.stack_assign(color_out);
2454
2455         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2456         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2457 }
2458
2459 void HSVNode::compile(OSLCompiler& compiler)
2460 {
2461         compiler.add(this, "node_hsv");
2462 }
2463
2464 /* Attribute */
2465
2466 AttributeNode::AttributeNode()
2467 : ShaderNode("attribute")
2468 {
2469         attribute = "";
2470
2471         add_output("Color",  SHADER_SOCKET_COLOR);
2472         add_output("Vector",  SHADER_SOCKET_VECTOR);
2473         add_output("Fac",  SHADER_SOCKET_FLOAT);
2474 }
2475
2476 void AttributeNode::attributes(AttributeRequestSet *attributes)
2477 {
2478         ShaderOutput *color_out = output("Color");
2479         ShaderOutput *vector_out = output("Vector");
2480         ShaderOutput *fac_out = output("Fac");
2481
2482         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2483                 attributes->add(attribute);
2484         
2485         ShaderNode::attributes(attributes);
2486 }
2487
2488 void AttributeNode::compile(SVMCompiler& compiler)
2489 {
2490         ShaderOutput *color_out = output("Color");
2491         ShaderOutput *vector_out = output("Vector");
2492         ShaderOutput *fac_out = output("Fac");
2493         NodeType attr_node = NODE_ATTR;
2494
2495         if(bump == SHADER_BUMP_DX)
2496                 attr_node = NODE_ATTR_BUMP_DX;
2497         else if(bump == SHADER_BUMP_DY)
2498                 attr_node = NODE_ATTR_BUMP_DY;
2499
2500         if(!color_out->links.empty() || !vector_out->links.empty()) {
2501                 int attr = compiler.attribute(attribute);
2502
2503                 if(!color_out->links.empty()) {
2504                         compiler.stack_assign(color_out);
2505                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2506                 }
2507                 if(!vector_out->links.empty()) {
2508                         compiler.stack_assign(vector_out);
2509                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2510                 }
2511         }
2512
2513         if(!fac_out->links.empty()) {
2514                 int attr = compiler.attribute(attribute);
2515
2516                 compiler.stack_assign(fac_out);
2517                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2518         }
2519 }
2520
2521 void AttributeNode::compile(OSLCompiler& compiler)
2522 {
2523         if(bump == SHADER_BUMP_DX)
2524                 compiler.parameter("bump_offset", "dx");
2525         else if(bump == SHADER_BUMP_DY)
2526                 compiler.parameter("bump_offset", "dy");
2527         else
2528                 compiler.parameter("bump_offset", "center");
2529
2530         compiler.parameter("name", attribute.c_str());
2531         compiler.add(this, "node_attribute");
2532 }
2533
2534 /* Camera */
2535
2536 CameraNode::CameraNode()
2537 : ShaderNode("camera")
2538 {
2539         add_output("View Vector",  SHADER_SOCKET_VECTOR);
2540         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
2541         add_output("View Distance",  SHADER_SOCKET_FLOAT);
2542 }
2543
2544 void CameraNode::compile(SVMCompiler& compiler)
2545 {
2546         ShaderOutput *vector_out = output("View Vector");
2547         ShaderOutput *z_depth_out = output("View Z Depth");
2548         ShaderOutput *distance_out = output("View Distance");
2549
2550         compiler.stack_assign(vector_out);
2551         compiler.stack_assign(z_depth_out);
2552         compiler.stack_assign(distance_out);
2553         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2554 }
2555
2556 void CameraNode::compile(OSLCompiler& compiler)
2557 {
2558         compiler.add(this, "node_camera");
2559 }
2560
2561 /* Fresnel */
2562
2563 FresnelNode::FresnelNode()
2564 : ShaderNode("Fresnel")
2565 {
2566         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2567         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2568         add_output("Fac", SHADER_SOCKET_FLOAT);
2569 }
2570
2571 void FresnelNode::compile(SVMCompiler& compiler)
2572 {
2573         ShaderInput *ior_in = input("IOR");
2574         ShaderOutput *fac_out = output("Fac");
2575
2576         compiler.stack_assign(ior_in);
2577         compiler.stack_assign(fac_out);
2578         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2579 }
2580
2581 void FresnelNode::compile(OSLCompiler& compiler)
2582 {
2583         compiler.add(this, "node_fresnel");
2584 }
2585
2586 /* Blend Weight */
2587
2588 LayerWeightNode::LayerWeightNode()
2589 : ShaderNode("LayerWeight")
2590 {
2591         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2592         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2593
2594         add_output("Fresnel", SHADER_SOCKET_FLOAT);
2595         add_output("Facing", SHADER_SOCKET_FLOAT);
2596 }
2597
2598 void LayerWeightNode::compile(SVMCompiler& compiler)
2599 {
2600         ShaderInput *blend_in = input("Blend");
2601
2602         if(blend_in->link)
2603                 compiler.stack_assign(blend_in);
2604
2605         ShaderOutput *fresnel_out = output("Fresnel");
2606         if(!fresnel_out->links.empty()) {
2607                 compiler.stack_assign(fresnel_out);
2608                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2609                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
2610         }
2611
2612         ShaderOutput *facing_out = output("Facing");
2613         if(!facing_out->links.empty()) {
2614                 compiler.stack_assign(facing_out);
2615                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2616                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
2617         }
2618 }
2619
2620 void LayerWeightNode::compile(OSLCompiler& compiler)
2621 {
2622         compiler.add(this, "node_layer_weight");
2623 }
2624
2625 /* Output */
2626
2627 OutputNode::OutputNode()
2628 : ShaderNode("output")
2629 {
2630         add_input("Surface", SHADER_SOCKET_CLOSURE);
2631         add_input("Volume", SHADER_SOCKET_CLOSURE);
2632         add_input("Displacement", SHADER_SOCKET_FLOAT);
2633         add_input("Normal", SHADER_SOCKET_NORMAL);
2634 }
2635
2636 void OutputNode::compile(SVMCompiler& compiler)
2637 {
2638         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
2639                 ShaderInput *displacement_in = input("Displacement");
2640
2641                 if(displacement_in->link) {
2642                         compiler.stack_assign(displacement_in);
2643                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
2644                 }
2645         }
2646 }
2647
2648 void OutputNode::compile(OSLCompiler& compiler)
2649 {
2650         if(compiler.output_type() == SHADER_TYPE_SURFACE)
2651                 compiler.add(this, "node_output_surface");
2652         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
2653                 compiler.add(this, "node_output_volume");
2654         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
2655                 compiler.add(this, "node_output_displacement");
2656 }
2657
2658 /* Math */
2659
2660 MathNode::MathNode()
2661 : ShaderNode("math")
2662 {
2663         type = ustring("Add");
2664
2665         use_clamp = false;
2666
2667         add_input("Value1", SHADER_SOCKET_FLOAT);
2668         add_input("Value2", SHADER_SOCKET_FLOAT);
2669         add_output("Value",  SHADER_SOCKET_FLOAT);
2670 }
2671
2672 static ShaderEnum math_type_init()
2673 {
2674         ShaderEnum enm;
2675
2676         enm.insert("Add", NODE_MATH_ADD);
2677         enm.insert("Subtract", NODE_MATH_SUBTRACT);
2678         enm.insert("Multiply", NODE_MATH_MULTIPLY);
2679         enm.insert("Divide", NODE_MATH_DIVIDE);
2680         enm.insert("Sine", NODE_MATH_SINE);
2681         enm.insert("Cosine", NODE_MATH_COSINE);
2682         enm.insert("Tangent", NODE_MATH_TANGENT);
2683         enm.insert("Arcsine", NODE_MATH_ARCSINE);
2684         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
2685         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
2686         enm.insert("Power", NODE_MATH_POWER);
2687         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
2688         enm.insert("Minimum", NODE_MATH_MINIMUM);
2689         enm.insert("Maximum", NODE_MATH_MAXIMUM);
2690         enm.insert("Round", NODE_MATH_ROUND);
2691         enm.insert("Less Than", NODE_MATH_LESS_THAN);
2692         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
2693
2694         return enm;
2695 }
2696
2697 ShaderEnum MathNode::type_enum = math_type_init();
2698
2699 void MathNode::compile(SVMCompiler& compiler)
2700 {
2701         ShaderInput *value1_in = input("Value1");
2702         ShaderInput *value2_in = input("Value2");
2703         ShaderOutput *value_out = output("Value");
2704
2705         compiler.stack_assign(value1_in);
2706         compiler.stack_assign(value2_in);
2707         compiler.stack_assign(value_out);
2708
2709         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
2710         compiler.add_node(NODE_MATH, value_out->stack_offset);
2711
2712         if(use_clamp) {
2713                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
2714                 compiler.add_node(NODE_MATH, value_out->stack_offset);
2715         }
2716 }
2717
2718 void MathNode::compile(OSLCompiler& compiler)
2719 {
2720         compiler.parameter("type", type);
2721         compiler.parameter("Clamp", use_clamp);
2722         compiler.add(this, "node_math");
2723 }
2724
2725 /* VectorMath */
2726
2727 VectorMathNode::VectorMathNode()
2728 : ShaderNode("vector_math")
2729 {
2730         type = ustring("Add");
2731
2732         add_input("Vector1", SHADER_SOCKET_VECTOR);
2733         add_input("Vector2", SHADER_SOCKET_VECTOR);
2734         add_output("Value",  SHADER_SOCKET_FLOAT);
2735         add_output("Vector",  SHADER_SOCKET_VECTOR);
2736 }
2737
2738 static ShaderEnum vector_math_type_init()
2739 {
2740         ShaderEnum enm;
2741
2742         enm.insert("Add", NODE_VECTOR_MATH_ADD);
2743         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
2744         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
2745         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
2746         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
2747         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
2748
2749         return enm;
2750 }
2751
2752 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
2753
2754 void VectorMathNode::compile(SVMCompiler& compiler)
2755 {
2756         ShaderInput *vector1_in = input("Vector1");
2757         ShaderInput *vector2_in = input("Vector2");
2758         ShaderOutput *value_out = output("Value");
2759         ShaderOutput *vector_out = output("Vector");
2760
2761         compiler.stack_assign(vector1_in);
2762         compiler.stack_assign(vector2_in);
2763         compiler.stack_assign(value_out);
2764         compiler.stack_assign(vector_out);
2765
2766         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
2767         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
2768 }
2769
2770 void VectorMathNode::compile(OSLCompiler& compiler)
2771 {
2772         compiler.parameter("type", type);
2773         compiler.add(this, "node_vector_math");
2774 }
2775
2776 /* BumpNode */
2777
2778 BumpNode::BumpNode()
2779 : ShaderNode("bump")
2780 {
2781         /* this input is used by the user, but after graph transform it is no longer
2782          * used and moved to sampler center/x/y instead */
2783         add_input("Height", SHADER_SOCKET_FLOAT);
2784
2785         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
2786         add_input("SampleX", SHADER_SOCKET_FLOAT);
2787         add_input("SampleY", SHADER_SOCKET_FLOAT);
2788         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
2789         add_input("Strength", SHADER_SOCKET_FLOAT, 0.1f);
2790
2791         add_output("Normal", SHADER_SOCKET_NORMAL);
2792 }
2793
2794 void BumpNode::compile(SVMCompiler& compiler)
2795 {
2796         ShaderInput *center_in = input("SampleCenter");
2797         ShaderInput *dx_in = input("SampleX");
2798         ShaderInput *dy_in = input("SampleY");
2799         ShaderInput *normal_in = input("NormalIn");
2800         ShaderInput *intensity_in = input("Strength");
2801         ShaderOutput *normal_out = output("Normal");
2802
2803         compiler.stack_assign(center_in);
2804         compiler.stack_assign(dx_in);
2805         compiler.stack_assign(dy_in);
2806         compiler.stack_assign(intensity_in);
2807         compiler.stack_assign(normal_out);
2808
2809         if(normal_in->link)
2810                 compiler.stack_assign(normal_in);
2811         
2812         /* pack all parameters in the node */
2813         compiler.add_node(NODE_SET_BUMP,
2814                 normal_in->stack_offset,
2815                 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
2816                         dy_in->stack_offset, intensity_in->stack_offset),
2817                 normal_out->stack_offset);
2818 }
2819
2820 void BumpNode::compile(OSLCompiler& compiler)
2821 {
2822         compiler.add(this, "node_bump");
2823 }
2824
2825 /* RGBCurvesNode */
2826
2827 RGBCurvesNode::RGBCurvesNode()
2828 : ShaderNode("rgb_curves")
2829 {
2830         add_input("Fac", SHADER_SOCKET_FLOAT);
2831         add_input("Color", SHADER_SOCKET_COLOR);
2832         add_output("Color", SHADER_SOCKET_COLOR);
2833 }
2834
2835 void RGBCurvesNode::compile(SVMCompiler& compiler)
2836 {
2837         ShaderInput *fac_in = input("Fac");
2838         ShaderInput *color_in = input("Color");
2839         ShaderOutput *color_out = output("Color");
2840
2841         compiler.stack_assign(fac_in);
2842         compiler.stack_assign(color_in);
2843         compiler.stack_assign(color_out);
2844
2845         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2846         compiler.add_array(curves, RAMP_TABLE_SIZE);
2847 }
2848
2849 void RGBCurvesNode::compile(OSLCompiler& compiler)
2850 {
2851         compiler.add(this, "node_rgb_curves");
2852 }
2853
2854 /* RGBRampNode */
2855
2856 RGBRampNode::RGBRampNode()
2857 : ShaderNode("rgb_ramp")
2858 {
2859         add_input("Fac", SHADER_SOCKET_FLOAT);
2860         add_output("Color", SHADER_SOCKET_COLOR);
2861         add_output("Alpha", SHADER_SOCKET_FLOAT);
2862 }
2863
2864 void RGBRampNode::compile(SVMCompiler& compiler)
2865 {
2866         ShaderInput *fac_in = input("Fac");
2867         ShaderOutput *color_out = output("Color");
2868         ShaderOutput *alpha_out = output("Alpha");
2869
2870         compiler.stack_assign(fac_in);
2871         if(!color_out->links.empty())
2872                 compiler.stack_assign(color_out);
2873         if(!alpha_out->links.empty())
2874                 compiler.stack_assign(alpha_out);
2875
2876         compiler.add_node(NODE_RGB_RAMP, fac_in->stack_offset, color_out->stack_offset, alpha_out->stack_offset);
2877         compiler.add_array(ramp, RAMP_TABLE_SIZE);
2878 }
2879
2880 void RGBRampNode::compile(OSLCompiler& compiler)
2881 {
2882         /* OSL shader only takes separate RGB and A array, split the RGBA base array */
2883         /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
2884         float ramp_color[RAMP_TABLE_SIZE][3];
2885         float ramp_alpha[RAMP_TABLE_SIZE];
2886
2887         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
2888                 ramp_color[i][0] = ramp[i].x;
2889                 ramp_color[i][1] = ramp[i].y;
2890                 ramp_color[i][2] = ramp[i].z;
2891                 ramp_alpha[i] = ramp[i].w;
2892         }
2893
2894         compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
2895         compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
2896         
2897         compiler.add(this, "node_rgb_ramp");
2898 }
2899
2900 /* Set Normal Node */
2901
2902 SetNormalNode::SetNormalNode()
2903 : ShaderNode("set_normal")
2904 {
2905         add_input("Direction", SHADER_SOCKET_VECTOR);
2906         add_output("Normal", SHADER_SOCKET_NORMAL);
2907 }
2908
2909 void SetNormalNode::compile(SVMCompiler& compiler)
2910 {
2911         ShaderInput  *direction_in = input("Direction");
2912         ShaderOutput *normal_out = output("Normal");
2913
2914         compiler.stack_assign(direction_in);
2915         compiler.stack_assign(normal_out);
2916
2917         compiler.add_node(NODE_CLOSURE_SET_NORMAL, direction_in->stack_offset, normal_out->stack_offset);
2918 }
2919
2920 void SetNormalNode::compile(OSLCompiler& compiler)
2921 {
2922         compiler.add(this, "node_set_normal"); 
2923 }
2924
2925 CCL_NAMESPACE_END
2926