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