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