Cleanup: comment blocks
[blender.git] / intern / cycles / render / nodes.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "image.h"
18 #include "integrator.h"
19 #include "nodes.h"
20 #include "scene.h"
21 #include "svm.h"
22 #include "svm_color_util.h"
23 #include "svm_math_util.h"
24 #include "osl.h"
25
26 #include "util_sky_model.h"
27 #include "util_foreach.h"
28 #include "util_transform.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 /* Texture Mapping */
33
34 #define TEXTURE_MAPPING_DEFINE(TextureNode) \
35         SOCKET_POINT(tex_mapping.translation, "Translation", make_float3(0.0f, 0.0f, 0.0f)); \
36         SOCKET_VECTOR(tex_mapping.rotation, "Rotation", make_float3(0.0f, 0.0f, 0.0f));      \
37         SOCKET_VECTOR(tex_mapping.scale, "Scale", make_float3(1.0f, 1.0f, 1.0f));            \
38         \
39         SOCKET_VECTOR(tex_mapping.min, "Min", make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX)); \
40         SOCKET_VECTOR(tex_mapping.max, "Max", make_float3(FLT_MAX, FLT_MAX, FLT_MAX));    \
41         SOCKET_BOOLEAN(tex_mapping.use_minmax, "Use Min Max", false);                     \
42         \
43         static NodeEnum mapping_axis_enum;                      \
44         mapping_axis_enum.insert("none", TextureMapping::NONE); \
45         mapping_axis_enum.insert("x", TextureMapping::X);       \
46         mapping_axis_enum.insert("y", TextureMapping::Y);       \
47         mapping_axis_enum.insert("z", TextureMapping::Z);       \
48         SOCKET_ENUM(tex_mapping.x_mapping, "x_mapping", mapping_axis_enum, TextureMapping::X); \
49         SOCKET_ENUM(tex_mapping.y_mapping, "y_mapping", mapping_axis_enum, TextureMapping::Y); \
50         SOCKET_ENUM(tex_mapping.z_mapping, "z_mapping", mapping_axis_enum, TextureMapping::Z); \
51         \
52         static NodeEnum mapping_type_enum;                            \
53         mapping_type_enum.insert("point", TextureMapping::POINT);     \
54         mapping_type_enum.insert("texture", TextureMapping::TEXTURE); \
55         mapping_type_enum.insert("vector", TextureMapping::VECTOR);   \
56         mapping_type_enum.insert("normal", TextureMapping::NORMAL);   \
57         SOCKET_ENUM(tex_mapping.type, "Type", mapping_type_enum, TextureMapping::TEXTURE); \
58         \
59         static NodeEnum mapping_projection_enum;                                                \
60         mapping_projection_enum.insert("flat", TextureMapping::FLAT);                           \
61         mapping_projection_enum.insert("cube", TextureMapping::CUBE);                           \
62         mapping_projection_enum.insert("tube", TextureMapping::TUBE);                           \
63         mapping_projection_enum.insert("sphere", TextureMapping::SPHERE);                       \
64         SOCKET_ENUM(tex_mapping.projection, "Projection", mapping_projection_enum, TextureMapping::FLAT);
65
66 TextureMapping::TextureMapping()
67 {
68 }
69
70 Transform TextureMapping::compute_transform()
71 {
72         Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
73
74         if(x_mapping != NONE)
75                 mmat[0][x_mapping-1] = 1.0f;
76         if(y_mapping != NONE)
77                 mmat[1][y_mapping-1] = 1.0f;
78         if(z_mapping != NONE)
79                 mmat[2][z_mapping-1] = 1.0f;
80         
81         float3 scale_clamped = scale;
82
83         if(type == TEXTURE || type == NORMAL) {
84                 /* keep matrix invertible */
85                 if(fabsf(scale.x) < 1e-5f)
86                         scale_clamped.x = signf(scale.x)*1e-5f;
87                 if(fabsf(scale.y) < 1e-5f)
88                         scale_clamped.y = signf(scale.y)*1e-5f;
89                 if(fabsf(scale.z) < 1e-5f)
90                         scale_clamped.z = signf(scale.z)*1e-5f;
91         }
92         
93         Transform smat = transform_scale(scale_clamped);
94         Transform rmat = transform_euler(rotation);
95         Transform tmat = transform_translate(translation);
96
97         Transform mat;
98
99         switch(type) {
100                 case TEXTURE:
101                         /* inverse transform on texture coordinate gives
102                          * forward transform on texture */
103                         mat = tmat*rmat*smat;
104                         mat = transform_inverse(mat);
105                         break;
106                 case POINT:
107                         /* full transform */
108                         mat = tmat*rmat*smat;
109                         break;
110                 case VECTOR:
111                         /* no translation for vectors */
112                         mat = rmat*smat;
113                         break;
114                 case NORMAL:
115                         /* no translation for normals, and inverse transpose */
116                         mat = rmat*smat;
117                         mat = transform_inverse(mat);
118                         mat = transform_transpose(mat);
119                         break;
120         }
121
122         /* projection last */
123         mat = mat*mmat;
124
125         return mat;
126 }
127
128 bool TextureMapping::skip()
129 {
130         if(translation != make_float3(0.0f, 0.0f, 0.0f))
131                 return false;
132         if(rotation != make_float3(0.0f, 0.0f, 0.0f))
133                 return false;
134         if(scale != make_float3(1.0f, 1.0f, 1.0f))
135                 return false;
136         
137         if(x_mapping != X || y_mapping != Y || z_mapping != Z)
138                 return false;
139         if(use_minmax)
140                 return false;
141         
142         return true;
143 }
144
145 void TextureMapping::compile(SVMCompiler& compiler, int offset_in, int offset_out)
146 {
147         compiler.add_node(NODE_MAPPING, offset_in, offset_out);
148
149         Transform tfm = compute_transform();
150         compiler.add_node(tfm.x);
151         compiler.add_node(tfm.y);
152         compiler.add_node(tfm.z);
153         compiler.add_node(tfm.w);
154
155         if(use_minmax) {
156                 compiler.add_node(NODE_MIN_MAX, offset_out, offset_out);
157                 compiler.add_node(float3_to_float4(min));
158                 compiler.add_node(float3_to_float4(max));
159         }
160
161         if(type == NORMAL) {
162                 compiler.add_node(NODE_VECTOR_MATH, NODE_VECTOR_MATH_NORMALIZE, offset_out, offset_out);
163                 compiler.add_node(NODE_VECTOR_MATH, SVM_STACK_INVALID, offset_out);
164         }
165 }
166
167 /* Convenience function for texture nodes, allocating stack space to output
168  * a modified vector and returning its offset */
169 int TextureMapping::compile_begin(SVMCompiler& compiler, ShaderInput *vector_in)
170 {
171         if(!skip()) {
172                 int offset_in = compiler.stack_assign(vector_in);
173                 int offset_out = compiler.stack_find_offset(SocketType::VECTOR);
174
175                 compile(compiler, offset_in, offset_out);
176
177                 return offset_out;
178         }
179
180         return compiler.stack_assign(vector_in);
181 }
182
183 void TextureMapping::compile_end(SVMCompiler& compiler, ShaderInput *vector_in, int vector_offset)
184 {
185         if(!skip()) {
186                 compiler.stack_clear_offset(vector_in->type(), vector_offset);
187         }
188 }
189
190 void TextureMapping::compile(OSLCompiler &compiler)
191 {
192         if(!skip()) {
193                 Transform tfm = transform_transpose(compute_transform());
194
195                 compiler.parameter("mapping", tfm);
196                 compiler.parameter("use_mapping", 1);
197         }
198 }
199
200 /* Image Texture */
201
202 NODE_DEFINE(ImageTextureNode)
203 {
204         NodeType* type = NodeType::add("image_texture", create, NodeType::SHADER);
205
206         TEXTURE_MAPPING_DEFINE(ImageTextureNode);
207
208         SOCKET_STRING(filename, "Filename", ustring(""));
209
210         static NodeEnum color_space_enum;
211         color_space_enum.insert("none", NODE_COLOR_SPACE_NONE);
212         color_space_enum.insert("color", NODE_COLOR_SPACE_COLOR);
213         SOCKET_ENUM(color_space, "Color Space", color_space_enum, NODE_COLOR_SPACE_COLOR);
214
215         SOCKET_BOOLEAN(use_alpha, "Use Alpha", true);
216
217         static NodeEnum interpolation_enum;
218         interpolation_enum.insert("closest", INTERPOLATION_CLOSEST);
219         interpolation_enum.insert("linear", INTERPOLATION_LINEAR);
220         interpolation_enum.insert("cubic", INTERPOLATION_CUBIC);
221         interpolation_enum.insert("smart", INTERPOLATION_SMART);
222         SOCKET_ENUM(interpolation, "Interpolation", interpolation_enum, INTERPOLATION_LINEAR);
223
224         static NodeEnum extension_enum;
225         extension_enum.insert("periodic", EXTENSION_REPEAT);
226         extension_enum.insert("clamp", EXTENSION_EXTEND);
227         extension_enum.insert("black", EXTENSION_CLIP);
228         SOCKET_ENUM(extension, "Extension", extension_enum, EXTENSION_REPEAT);
229
230         static NodeEnum projection_enum;
231         projection_enum.insert("flat", NODE_IMAGE_PROJ_FLAT);
232         projection_enum.insert("box", NODE_IMAGE_PROJ_BOX);
233         projection_enum.insert("sphere", NODE_IMAGE_PROJ_SPHERE);
234         projection_enum.insert("tube", NODE_IMAGE_PROJ_TUBE);
235         SOCKET_ENUM(projection, "Projection", projection_enum, NODE_IMAGE_PROJ_FLAT);
236
237         SOCKET_FLOAT(projection_blend, "Projection Blend", 0.0f);
238
239         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_UV);
240
241         SOCKET_OUT_COLOR(color, "Color");
242         SOCKET_OUT_FLOAT(alpha, "Alpha");
243
244         return type;
245 }
246
247 ImageTextureNode::ImageTextureNode()
248 : ImageSlotTextureNode(node_type)
249 {
250         image_manager = NULL;
251         slot = -1;
252         is_float = -1;
253         is_linear = false;
254         builtin_data = NULL;
255         animated = false;
256 }
257
258 ImageTextureNode::~ImageTextureNode()
259 {
260         if(image_manager) {
261                 image_manager->remove_image(filename.string(),
262                                             builtin_data,
263                                             interpolation,
264                                             extension);
265         }
266 }
267
268 ShaderNode *ImageTextureNode::clone() const
269 {
270         ImageTextureNode *node = new ImageTextureNode(*this);
271         node->image_manager = NULL;
272         node->slot = -1;
273         node->is_float = -1;
274         node->is_linear = false;
275         return node;
276 }
277
278 void ImageTextureNode::attributes(Shader *shader, AttributeRequestSet *attributes)
279 {
280 #ifdef WITH_PTEX
281         /* todo: avoid loading other texture coordinates when using ptex,
282          * and hide texture coordinate socket in the UI */
283         if(shader->has_surface && string_endswith(filename, ".ptx")) {
284                 /* ptex */
285                 attributes->add(ATTR_STD_PTEX_FACE_ID);
286                 attributes->add(ATTR_STD_PTEX_UV);
287         }
288 #endif
289
290         ShaderNode::attributes(shader, attributes);
291 }
292
293 void ImageTextureNode::compile(SVMCompiler& compiler)
294 {
295         ShaderInput *vector_in = input("Vector");
296         ShaderOutput *color_out = output("Color");
297         ShaderOutput *alpha_out = output("Alpha");
298
299         image_manager = compiler.image_manager;
300         if(is_float == -1) {
301                 bool is_float_bool;
302                 slot = image_manager->add_image(filename.string(),
303                                                 builtin_data,
304                                                 animated,
305                                                 0,
306                                                 is_float_bool,
307                                                 is_linear,
308                                                 interpolation,
309                                                 extension,
310                                                 use_alpha);
311                 is_float = (int)is_float_bool;
312         }
313
314         if(slot != -1) {
315                 int srgb = (is_linear || color_space != NODE_COLOR_SPACE_COLOR)? 0: 1;
316                 int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
317
318                 if(projection != NODE_IMAGE_PROJ_BOX) {
319                         compiler.add_node(NODE_TEX_IMAGE,
320                                 slot,
321                                 compiler.encode_uchar4(
322                                         vector_offset,
323                                         compiler.stack_assign_if_linked(color_out),
324                                         compiler.stack_assign_if_linked(alpha_out),
325                                         srgb),
326                                 projection);
327                 }
328                 else {
329                         compiler.add_node(NODE_TEX_IMAGE_BOX,
330                                 slot,
331                                 compiler.encode_uchar4(
332                                         vector_offset,
333                                         compiler.stack_assign_if_linked(color_out),
334                                         compiler.stack_assign_if_linked(alpha_out),
335                                         srgb),
336                                 __float_as_int(projection_blend));
337                 }
338
339                 tex_mapping.compile_end(compiler, vector_in, vector_offset);
340         }
341         else {
342                 /* image not found */
343                 if(!color_out->links.empty()) {
344                         compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
345                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
346                                                                     TEX_IMAGE_MISSING_G,
347                                                                     TEX_IMAGE_MISSING_B));
348                 }
349                 if(!alpha_out->links.empty())
350                         compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), compiler.stack_assign(alpha_out));
351         }
352 }
353
354 void ImageTextureNode::compile(OSLCompiler& compiler)
355 {
356         ShaderOutput *alpha_out = output("Alpha");
357
358         tex_mapping.compile(compiler);
359
360         image_manager = compiler.image_manager;
361         if(is_float == -1) {
362                 if(builtin_data == NULL) {
363                         ImageManager::ImageDataType type;
364                         type = image_manager->get_image_metadata(filename.string(), NULL, is_linear);
365                         if(type == ImageManager::IMAGE_DATA_TYPE_FLOAT || type == ImageManager::IMAGE_DATA_TYPE_FLOAT4)
366                                 is_float = 1;
367                 }
368                 else {
369                         bool is_float_bool;
370                         slot = image_manager->add_image(filename.string(),
371                                                         builtin_data,
372                                                         animated,
373                                                         0,
374                                                         is_float_bool,
375                                                         is_linear,
376                                                         interpolation,
377                                                         extension,
378                                                         use_alpha);
379                         is_float = (int)is_float_bool;
380                 }
381         }
382
383         if(slot == -1) {
384                 compiler.parameter(this, "filename");
385         }
386         else {
387                 /* TODO(sergey): It's not so simple to pass custom attribute
388                  * to the texture() function in order to make builtin images
389                  * support more clear. So we use special file name which is
390                  * "@<slot_number>" and check whether file name matches this
391                  * mask in the OSLRenderServices::texture().
392                  */
393                 compiler.parameter("filename", string_printf("@%d", slot).c_str());
394         }
395         if(is_linear || color_space != NODE_COLOR_SPACE_COLOR)
396                 compiler.parameter("color_space", "linear");
397         else
398                 compiler.parameter("color_space", "sRGB");
399         compiler.parameter(this, "projection");
400         compiler.parameter(this, "projection_blend");
401         compiler.parameter("is_float", is_float);
402         compiler.parameter("use_alpha", !alpha_out->links.empty());
403         compiler.parameter(this, "interpolation");
404         compiler.parameter(this, "extension");
405
406         compiler.add(this, "node_image_texture");
407 }
408
409 /* Environment Texture */
410
411 NODE_DEFINE(EnvironmentTextureNode)
412 {
413         NodeType* type = NodeType::add("environment_texture", create, NodeType::SHADER);
414
415         TEXTURE_MAPPING_DEFINE(EnvironmentTextureNode);
416
417         SOCKET_STRING(filename, "Filename", ustring(""));
418
419         static NodeEnum color_space_enum;
420         color_space_enum.insert("none", NODE_COLOR_SPACE_NONE);
421         color_space_enum.insert("color", NODE_COLOR_SPACE_COLOR);
422         SOCKET_ENUM(color_space, "Color Space", color_space_enum, NODE_COLOR_SPACE_COLOR);
423
424         SOCKET_BOOLEAN(use_alpha, "Use Alpha", true);
425
426         static NodeEnum interpolation_enum;
427         interpolation_enum.insert("closest", INTERPOLATION_CLOSEST);
428         interpolation_enum.insert("linear", INTERPOLATION_LINEAR);
429         interpolation_enum.insert("cubic", INTERPOLATION_CUBIC);
430         interpolation_enum.insert("smart", INTERPOLATION_SMART);
431         SOCKET_ENUM(interpolation, "Interpolation", interpolation_enum, INTERPOLATION_LINEAR);
432
433         static NodeEnum projection_enum;
434         projection_enum.insert("equirectangular", NODE_ENVIRONMENT_EQUIRECTANGULAR);
435         projection_enum.insert("mirror_ball", NODE_ENVIRONMENT_MIRROR_BALL);
436         SOCKET_ENUM(projection, "Projection", projection_enum, NODE_ENVIRONMENT_EQUIRECTANGULAR);
437
438         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_POSITION);
439
440         SOCKET_OUT_COLOR(color, "Color");
441         SOCKET_OUT_FLOAT(alpha, "Alpha");
442
443         return type;
444 }
445
446 EnvironmentTextureNode::EnvironmentTextureNode()
447 : ImageSlotTextureNode(node_type)
448 {
449         image_manager = NULL;
450         slot = -1;
451         is_float = -1;
452         is_linear = false;
453         builtin_data = NULL;
454         animated = false;
455 }
456
457 EnvironmentTextureNode::~EnvironmentTextureNode()
458 {
459         if(image_manager) {
460                 image_manager->remove_image(filename.string(),
461                                             builtin_data,
462                                             interpolation,
463                                             EXTENSION_REPEAT);
464         }
465 }
466
467 ShaderNode *EnvironmentTextureNode::clone() const
468 {
469         EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
470         node->image_manager = NULL;
471         node->slot = -1;
472         node->is_float = -1;
473         node->is_linear = false;
474         return node;
475 }
476
477 void EnvironmentTextureNode::attributes(Shader *shader, AttributeRequestSet *attributes)
478 {
479 #ifdef WITH_PTEX
480         if(shader->has_surface && string_endswith(filename, ".ptx")) {
481                 /* ptex */
482                 attributes->add(ATTR_STD_PTEX_FACE_ID);
483                 attributes->add(ATTR_STD_PTEX_UV);
484         }
485 #endif
486
487         ShaderNode::attributes(shader, attributes);
488 }
489
490 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
491 {
492         ShaderInput *vector_in = input("Vector");
493         ShaderOutput *color_out = output("Color");
494         ShaderOutput *alpha_out = output("Alpha");
495
496         image_manager = compiler.image_manager;
497         if(slot == -1) {
498                 bool is_float_bool;
499                 slot = image_manager->add_image(filename.string(),
500                                                 builtin_data,
501                                                 animated,
502                                                 0,
503                                                 is_float_bool,
504                                                 is_linear,
505                                                 interpolation,
506                                                 EXTENSION_REPEAT,
507                                                 use_alpha);
508                 is_float = (int)is_float_bool;
509         }
510
511         if(slot != -1) {
512                 int srgb = (is_linear || color_space != NODE_COLOR_SPACE_COLOR)? 0: 1;
513                 int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
514
515                 compiler.add_node(NODE_TEX_ENVIRONMENT,
516                         slot,
517                         compiler.encode_uchar4(
518                                 vector_offset,
519                                 compiler.stack_assign_if_linked(color_out),
520                                 compiler.stack_assign_if_linked(alpha_out),
521                                 srgb),
522                         projection);
523         
524                 tex_mapping.compile_end(compiler, vector_in, vector_offset);
525         }
526         else {
527                 /* image not found */
528                 if(!color_out->links.empty()) {
529                         compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
530                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
531                                                                     TEX_IMAGE_MISSING_G,
532                                                                     TEX_IMAGE_MISSING_B));
533                 }
534                 if(!alpha_out->links.empty())
535                         compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), compiler.stack_assign(alpha_out));
536         }
537 }
538
539 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
540 {
541         ShaderOutput *alpha_out = output("Alpha");
542
543         tex_mapping.compile(compiler);
544
545         /* See comments in ImageTextureNode::compile about support
546          * of builtin images.
547          */
548         image_manager = compiler.image_manager;
549         if(is_float == -1) {
550                 if(builtin_data == NULL) {
551                         ImageManager::ImageDataType type;
552                         type = image_manager->get_image_metadata(filename.string(), NULL, is_linear);
553                         if(type == ImageManager::IMAGE_DATA_TYPE_FLOAT || type == ImageManager::IMAGE_DATA_TYPE_FLOAT4)
554                                 is_float = 1;
555                 }
556                 else {
557                         bool is_float_bool;
558                         slot = image_manager->add_image(filename.string(),
559                                                         builtin_data,
560                                                         animated,
561                                                         0,
562                                                         is_float_bool,
563                                                         is_linear,
564                                                         interpolation,
565                                                         EXTENSION_REPEAT,
566                                                         use_alpha);
567                         is_float = (int)is_float_bool;
568                 }
569         }
570
571         if(slot == -1) {
572                 compiler.parameter(this, "filename");
573         }
574         else {
575                 compiler.parameter("filename", string_printf("@%d", slot).c_str());
576         }
577         compiler.parameter(this, "projection");
578         if(is_linear || color_space != NODE_COLOR_SPACE_COLOR)
579                 compiler.parameter("color_space", "linear");
580         else
581                 compiler.parameter("color_space", "sRGB");
582
583         compiler.parameter(this, "interpolation");
584         compiler.parameter("is_float", is_float);
585         compiler.parameter("use_alpha", !alpha_out->links.empty());
586         compiler.add(this, "node_environment_texture");
587 }
588
589 /* Sky Texture */
590
591 static float2 sky_spherical_coordinates(float3 dir)
592 {
593         return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
594 }
595
596 typedef struct SunSky {
597         /* sun direction in spherical and cartesian */
598         float theta, phi;
599
600         /* Parameter */
601         float radiance_x, radiance_y, radiance_z;
602         float config_x[9], config_y[9], config_z[9];
603 } SunSky;
604
605 /* Preetham model */
606 static float sky_perez_function(float lam[6], float theta, float gamma)
607 {
608         return (1.0f + lam[0]*expf(lam[1]/cosf(theta))) * (1.0f + lam[2]*expf(lam[3]*gamma)  + lam[4]*cosf(gamma)*cosf(gamma));
609 }
610
611 static void sky_texture_precompute_old(SunSky *sunsky, float3 dir, float turbidity)
612 {
613         /*
614          * We re-use the SunSky struct of the new model, to avoid extra variables
615          * zenith_Y/x/y is now radiance_x/y/z
616          * perez_Y/x/y is now config_x/y/z
617          */
618         
619         float2 spherical = sky_spherical_coordinates(dir);
620         float theta = spherical.x;
621         float phi = spherical.y;
622
623         sunsky->theta = theta;
624         sunsky->phi = phi;
625
626         float theta2 = theta*theta;
627         float theta3 = theta2*theta;
628         float T = turbidity;
629         float T2 = T * T;
630
631         float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
632         sunsky->radiance_x = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
633         sunsky->radiance_x *= 0.06f;
634
635         sunsky->radiance_y =
636         (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
637         (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
638         (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
639
640         sunsky->radiance_z =
641         (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
642         (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta  + 0.00516f) * T +
643         (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta  + 0.26688f);
644
645         sunsky->config_x[0] = (0.1787f * T  - 1.4630f);
646         sunsky->config_x[1] = (-0.3554f * T  + 0.4275f);
647         sunsky->config_x[2] = (-0.0227f * T  + 5.3251f);
648         sunsky->config_x[3] = (0.1206f * T  - 2.5771f);
649         sunsky->config_x[4] = (-0.0670f * T  + 0.3703f);
650
651         sunsky->config_y[0] = (-0.0193f * T  - 0.2592f);
652         sunsky->config_y[1] = (-0.0665f * T  + 0.0008f);
653         sunsky->config_y[2] = (-0.0004f * T  + 0.2125f);
654         sunsky->config_y[3] = (-0.0641f * T  - 0.8989f);
655         sunsky->config_y[4] = (-0.0033f * T  + 0.0452f);
656
657         sunsky->config_z[0] = (-0.0167f * T  - 0.2608f);
658         sunsky->config_z[1] = (-0.0950f * T  + 0.0092f);
659         sunsky->config_z[2] = (-0.0079f * T  + 0.2102f);
660         sunsky->config_z[3] = (-0.0441f * T  - 1.6537f);
661         sunsky->config_z[4] = (-0.0109f * T  + 0.0529f);
662
663         /* unused for old sky model */
664         for(int i = 5; i < 9; i++) {
665                 sunsky->config_x[i] = 0.0f;
666                 sunsky->config_y[i] = 0.0f;
667                 sunsky->config_z[i] = 0.0f;
668         }
669
670         sunsky->radiance_x /= sky_perez_function(sunsky->config_x, 0, theta);
671         sunsky->radiance_y /= sky_perez_function(sunsky->config_y, 0, theta);
672         sunsky->radiance_z /= sky_perez_function(sunsky->config_z, 0, theta);
673 }
674
675 /* Hosek / Wilkie */
676 static void sky_texture_precompute_new(SunSky *sunsky, float3 dir, float turbidity, float ground_albedo)
677 {
678         /* Calculate Sun Direction and save coordinates */
679         float2 spherical = sky_spherical_coordinates(dir);
680         float theta = spherical.x;
681         float phi = spherical.y;
682         
683         /* Clamp Turbidity */
684         turbidity = clamp(turbidity, 0.0f, 10.0f); 
685         
686         /* Clamp to Horizon */
687         theta = clamp(theta, 0.0f, M_PI_2_F); 
688
689         sunsky->theta = theta;
690         sunsky->phi = phi;
691
692         float solarElevation = M_PI_2_F - theta;
693
694         /* Initialize Sky Model */
695         ArHosekSkyModelState *sky_state;
696         sky_state = arhosek_xyz_skymodelstate_alloc_init((double)turbidity, (double)ground_albedo, (double)solarElevation);
697
698         /* Copy values from sky_state to SunSky */
699         for(int i = 0; i < 9; ++i) {
700                 sunsky->config_x[i] = (float)sky_state->configs[0][i];
701                 sunsky->config_y[i] = (float)sky_state->configs[1][i];
702                 sunsky->config_z[i] = (float)sky_state->configs[2][i];
703         }
704         sunsky->radiance_x = (float)sky_state->radiances[0];
705         sunsky->radiance_y = (float)sky_state->radiances[1];
706         sunsky->radiance_z = (float)sky_state->radiances[2];
707
708         /* Free sky_state */
709         arhosekskymodelstate_free(sky_state);
710 }
711
712 NODE_DEFINE(SkyTextureNode)
713 {
714         NodeType* type = NodeType::add("sky_texture", create, NodeType::SHADER);
715
716         TEXTURE_MAPPING_DEFINE(SkyTextureNode);
717
718         static NodeEnum type_enum;
719         type_enum.insert("preetham", NODE_SKY_OLD);
720         type_enum.insert("hosek_wilkie", NODE_SKY_NEW);
721         SOCKET_ENUM(type, "Type", type_enum, NODE_SKY_NEW);
722
723         SOCKET_VECTOR(sun_direction, "Sun Direction", make_float3(0.0f, 0.0f, 1.0f));
724         SOCKET_FLOAT(turbidity, "Turbidity", 2.2f);
725         SOCKET_FLOAT(ground_albedo, "Ground Albedo", 0.3f);
726
727         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
728
729         SOCKET_OUT_COLOR(color, "Color");
730
731         return type;
732 }
733
734 SkyTextureNode::SkyTextureNode()
735 : TextureNode(node_type)
736 {
737 }
738
739 void SkyTextureNode::compile(SVMCompiler& compiler)
740 {
741         ShaderInput *vector_in = input("Vector");
742         ShaderOutput *color_out = output("Color");
743
744         SunSky sunsky;
745         if(type == NODE_SKY_OLD)
746                 sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
747         else if(type == NODE_SKY_NEW)
748                 sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
749         else
750                 assert(false);
751
752         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
753
754         compiler.stack_assign(color_out);
755         compiler.add_node(NODE_TEX_SKY, vector_offset, compiler.stack_assign(color_out), type);
756         compiler.add_node(__float_as_uint(sunsky.phi), __float_as_uint(sunsky.theta), __float_as_uint(sunsky.radiance_x), __float_as_uint(sunsky.radiance_y));
757         compiler.add_node(__float_as_uint(sunsky.radiance_z), __float_as_uint(sunsky.config_x[0]), __float_as_uint(sunsky.config_x[1]), __float_as_uint(sunsky.config_x[2]));
758         compiler.add_node(__float_as_uint(sunsky.config_x[3]), __float_as_uint(sunsky.config_x[4]), __float_as_uint(sunsky.config_x[5]), __float_as_uint(sunsky.config_x[6]));
759         compiler.add_node(__float_as_uint(sunsky.config_x[7]), __float_as_uint(sunsky.config_x[8]), __float_as_uint(sunsky.config_y[0]), __float_as_uint(sunsky.config_y[1]));
760         compiler.add_node(__float_as_uint(sunsky.config_y[2]), __float_as_uint(sunsky.config_y[3]), __float_as_uint(sunsky.config_y[4]), __float_as_uint(sunsky.config_y[5]));
761         compiler.add_node(__float_as_uint(sunsky.config_y[6]), __float_as_uint(sunsky.config_y[7]), __float_as_uint(sunsky.config_y[8]), __float_as_uint(sunsky.config_z[0]));
762         compiler.add_node(__float_as_uint(sunsky.config_z[1]), __float_as_uint(sunsky.config_z[2]), __float_as_uint(sunsky.config_z[3]), __float_as_uint(sunsky.config_z[4]));
763         compiler.add_node(__float_as_uint(sunsky.config_z[5]), __float_as_uint(sunsky.config_z[6]), __float_as_uint(sunsky.config_z[7]), __float_as_uint(sunsky.config_z[8]));
764
765         tex_mapping.compile_end(compiler, vector_in, vector_offset);
766 }
767
768 void SkyTextureNode::compile(OSLCompiler& compiler)
769 {
770         tex_mapping.compile(compiler);
771
772         SunSky sunsky;
773         if(type == NODE_SKY_OLD)
774                 sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
775         else if(type == NODE_SKY_NEW)
776                 sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
777         else
778                 assert(false);
779                 
780         compiler.parameter(this, "type");
781         compiler.parameter("theta", sunsky.theta);
782         compiler.parameter("phi", sunsky.phi);
783         compiler.parameter_color("radiance", make_float3(sunsky.radiance_x, sunsky.radiance_y, sunsky.radiance_z));
784         compiler.parameter_array("config_x", sunsky.config_x, 9);
785         compiler.parameter_array("config_y", sunsky.config_y, 9);
786         compiler.parameter_array("config_z", sunsky.config_z, 9);
787         compiler.add(this, "node_sky_texture");
788 }
789
790 /* Gradient Texture */
791
792 NODE_DEFINE(GradientTextureNode)
793 {
794         NodeType* type = NodeType::add("gradient_texture", create, NodeType::SHADER);
795
796         TEXTURE_MAPPING_DEFINE(GradientTextureNode);
797
798         static NodeEnum type_enum;
799         type_enum.insert("linear", NODE_BLEND_LINEAR);
800         type_enum.insert("quadratic", NODE_BLEND_QUADRATIC);
801         type_enum.insert("easing", NODE_BLEND_EASING);
802         type_enum.insert("diagonal", NODE_BLEND_DIAGONAL);
803         type_enum.insert("radial", NODE_BLEND_RADIAL);
804         type_enum.insert("quadratic_sphere", NODE_BLEND_QUADRATIC_SPHERE);
805         type_enum.insert("spherical", NODE_BLEND_SPHERICAL);
806         SOCKET_ENUM(type, "Type", type_enum, NODE_BLEND_LINEAR);
807
808         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
809
810         SOCKET_OUT_COLOR(color, "Color");
811         SOCKET_OUT_FLOAT(fac, "Fac");
812
813         return type;
814 }
815
816 GradientTextureNode::GradientTextureNode()
817 : TextureNode(node_type)
818 {
819 }
820
821 void GradientTextureNode::compile(SVMCompiler& compiler)
822 {
823         ShaderInput *vector_in = input("Vector");
824         ShaderOutput *color_out = output("Color");
825         ShaderOutput *fac_out = output("Fac");
826
827         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
828
829         compiler.add_node(NODE_TEX_GRADIENT,
830                 compiler.encode_uchar4(
831                         type,
832                         vector_offset,
833                         compiler.stack_assign_if_linked(fac_out),
834                         compiler.stack_assign_if_linked(color_out)));
835
836         tex_mapping.compile_end(compiler, vector_in, vector_offset);
837 }
838
839 void GradientTextureNode::compile(OSLCompiler& compiler)
840 {
841         tex_mapping.compile(compiler);
842
843         compiler.parameter(this, "type");
844         compiler.add(this, "node_gradient_texture");
845 }
846
847 /* Noise Texture */
848
849 NODE_DEFINE(NoiseTextureNode)
850 {
851         NodeType* type = NodeType::add("noise_texture", create, NodeType::SHADER);
852
853         TEXTURE_MAPPING_DEFINE(NoiseTextureNode);
854
855         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
856         SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
857         SOCKET_IN_FLOAT(distortion, "Distortion", 0.0f);
858         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
859
860         SOCKET_OUT_COLOR(color, "Color");
861         SOCKET_OUT_FLOAT(fac, "Fac");
862
863         return type;
864 }
865
866 NoiseTextureNode::NoiseTextureNode()
867 : TextureNode(node_type)
868 {
869 }
870
871 void NoiseTextureNode::compile(SVMCompiler& compiler)
872 {
873         ShaderInput *distortion_in = input("Distortion");
874         ShaderInput *detail_in = input("Detail");
875         ShaderInput *scale_in = input("Scale");
876         ShaderInput *vector_in = input("Vector");
877         ShaderOutput *color_out = output("Color");
878         ShaderOutput *fac_out = output("Fac");
879
880         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
881
882         compiler.add_node(NODE_TEX_NOISE,
883                 compiler.encode_uchar4(
884                         vector_offset,
885                         compiler.stack_assign_if_linked(scale_in),
886                         compiler.stack_assign_if_linked(detail_in),
887                         compiler.stack_assign_if_linked(distortion_in)),
888                 compiler.encode_uchar4(
889                         compiler.stack_assign_if_linked(color_out),
890                         compiler.stack_assign_if_linked(fac_out)));
891         compiler.add_node(
892                 __float_as_int(scale),
893                 __float_as_int(detail),
894                 __float_as_int(distortion));
895
896         tex_mapping.compile_end(compiler, vector_in, vector_offset);
897 }
898
899 void NoiseTextureNode::compile(OSLCompiler& compiler)
900 {
901         tex_mapping.compile(compiler);
902
903         compiler.add(this, "node_noise_texture");
904 }
905
906 /* Voronoi Texture */
907
908 NODE_DEFINE(VoronoiTextureNode)
909 {
910         NodeType* type = NodeType::add("voronoi_texture", create, NodeType::SHADER);
911
912         TEXTURE_MAPPING_DEFINE(VoronoiTextureNode);
913
914         static NodeEnum coloring_enum;
915         coloring_enum.insert("intensity", NODE_VORONOI_INTENSITY);
916         coloring_enum.insert("cells", NODE_VORONOI_CELLS);
917         SOCKET_ENUM(coloring, "Coloring", coloring_enum, NODE_VORONOI_INTENSITY);
918
919         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
920         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
921
922         SOCKET_OUT_COLOR(color, "Color");
923         SOCKET_OUT_FLOAT(fac, "Fac");
924
925         return type;
926 }
927
928 VoronoiTextureNode::VoronoiTextureNode()
929 : TextureNode(node_type)
930 {
931 }
932
933 void VoronoiTextureNode::compile(SVMCompiler& compiler)
934 {
935         ShaderInput *scale_in = input("Scale");
936         ShaderInput *vector_in = input("Vector");
937         ShaderOutput *color_out = output("Color");
938         ShaderOutput *fac_out = output("Fac");
939
940         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
941
942         compiler.add_node(NODE_TEX_VORONOI,
943                 coloring,
944                 compiler.encode_uchar4(
945                         compiler.stack_assign_if_linked(scale_in),
946                         vector_offset,
947                         compiler.stack_assign(fac_out),
948                         compiler.stack_assign(color_out)),
949                 __float_as_int(scale));
950
951         tex_mapping.compile_end(compiler, vector_in, vector_offset);
952 }
953
954 void VoronoiTextureNode::compile(OSLCompiler& compiler)
955 {
956         tex_mapping.compile(compiler);
957
958         compiler.parameter(this, "coloring");
959         compiler.add(this, "node_voronoi_texture");
960 }
961
962 /* Musgrave Texture */
963
964 NODE_DEFINE(MusgraveTextureNode)
965 {
966         NodeType* type = NodeType::add("musgrave_texture", create, NodeType::SHADER);
967
968         TEXTURE_MAPPING_DEFINE(MusgraveTextureNode);
969
970         static NodeEnum type_enum;
971         type_enum.insert("multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
972         type_enum.insert("fBM", NODE_MUSGRAVE_FBM);
973         type_enum.insert("hybrid_multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
974         type_enum.insert("ridged_multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
975         type_enum.insert("hetero_terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
976         SOCKET_ENUM(type, "Type", type_enum, NODE_MUSGRAVE_FBM);
977
978         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
979         SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
980         SOCKET_IN_FLOAT(dimension, "Dimension", 2.0f);
981         SOCKET_IN_FLOAT(lacunarity, "Lacunarity", 1.0f);
982         SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
983         SOCKET_IN_FLOAT(gain, "Gain", 1.0f);
984         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
985
986         SOCKET_OUT_COLOR(color, "Color");
987         SOCKET_OUT_FLOAT(fac, "Fac");
988
989         return type;
990 }
991
992 MusgraveTextureNode::MusgraveTextureNode()
993 : TextureNode(node_type)
994 {
995 }
996
997 void MusgraveTextureNode::compile(SVMCompiler& compiler)
998 {
999         ShaderInput *vector_in = input("Vector");
1000         ShaderInput *scale_in = input("Scale");
1001         ShaderInput *dimension_in = input("Dimension");
1002         ShaderInput *lacunarity_in = input("Lacunarity");
1003         ShaderInput *detail_in = input("Detail");
1004         ShaderInput *offset_in = input("Offset");
1005         ShaderInput *gain_in = input("Gain");
1006         ShaderOutput *fac_out = output("Fac");
1007         ShaderOutput *color_out = output("Color");
1008
1009         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1010
1011         compiler.add_node(NODE_TEX_MUSGRAVE,
1012                 compiler.encode_uchar4(
1013                         type,
1014                         vector_offset,
1015                         compiler.stack_assign_if_linked(color_out),
1016                         compiler.stack_assign_if_linked(fac_out)),
1017                 compiler.encode_uchar4(
1018                         compiler.stack_assign_if_linked(dimension_in),
1019                         compiler.stack_assign_if_linked(lacunarity_in),
1020                         compiler.stack_assign_if_linked(detail_in),
1021                         compiler.stack_assign_if_linked(offset_in)),
1022                 compiler.encode_uchar4(
1023                         compiler.stack_assign_if_linked(gain_in),
1024                         compiler.stack_assign_if_linked(scale_in)));
1025         compiler.add_node(__float_as_int(dimension),
1026                 __float_as_int(lacunarity),
1027                 __float_as_int(detail),
1028                 __float_as_int(offset));
1029         compiler.add_node(__float_as_int(gain),
1030                 __float_as_int(scale));
1031
1032         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1033 }
1034
1035 void MusgraveTextureNode::compile(OSLCompiler& compiler)
1036 {
1037         tex_mapping.compile(compiler);
1038
1039         compiler.parameter(this, "type");
1040         compiler.add(this, "node_musgrave_texture");
1041 }
1042
1043 /* Wave Texture */
1044
1045 NODE_DEFINE(WaveTextureNode)
1046 {
1047         NodeType* type = NodeType::add("wave_texture", create, NodeType::SHADER);
1048
1049         TEXTURE_MAPPING_DEFINE(WaveTextureNode);
1050
1051         static NodeEnum type_enum;
1052         type_enum.insert("bands", NODE_WAVE_BANDS);
1053         type_enum.insert("rings", NODE_WAVE_RINGS);
1054         SOCKET_ENUM(type, "Type", type_enum, NODE_WAVE_BANDS);
1055
1056         static NodeEnum profile_enum;
1057         profile_enum.insert("sine", NODE_WAVE_PROFILE_SIN);
1058         profile_enum.insert("saw", NODE_WAVE_PROFILE_SAW);
1059         SOCKET_ENUM(profile, "Profile", profile_enum, NODE_WAVE_PROFILE_SIN);
1060
1061         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
1062         SOCKET_IN_FLOAT(distortion, "Distortion", 0.0f);
1063         SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
1064         SOCKET_IN_FLOAT(detail_scale, "Detail Scale", 0.0f);
1065         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
1066
1067         SOCKET_OUT_COLOR(color, "Color");
1068         SOCKET_OUT_FLOAT(fac, "Fac");
1069
1070         return type;
1071 }
1072
1073 WaveTextureNode::WaveTextureNode()
1074 : TextureNode(node_type)
1075 {
1076 }
1077
1078 void WaveTextureNode::compile(SVMCompiler& compiler)
1079 {
1080         ShaderInput *scale_in = input("Scale");
1081         ShaderInput *distortion_in = input("Distortion");
1082         ShaderInput *dscale_in = input("Detail Scale");
1083         ShaderInput *detail_in = input("Detail");
1084         ShaderInput *vector_in = input("Vector");
1085         ShaderOutput *fac_out = output("Fac");
1086         ShaderOutput *color_out = output("Color");
1087
1088         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1089
1090         compiler.add_node(NODE_TEX_WAVE,
1091                 compiler.encode_uchar4(
1092                         type,
1093                         compiler.stack_assign_if_linked(color_out),
1094                         compiler.stack_assign_if_linked(fac_out),
1095                         compiler.stack_assign_if_linked(dscale_in)),
1096                 compiler.encode_uchar4(
1097                         vector_offset,
1098                         compiler.stack_assign_if_linked(scale_in),
1099                         compiler.stack_assign_if_linked(detail_in),
1100                         compiler.stack_assign_if_linked(distortion_in)),
1101                 profile);
1102
1103         compiler.add_node(
1104                 __float_as_int(scale),
1105                 __float_as_int(detail),
1106                 __float_as_int(distortion),
1107                 __float_as_int(detail_scale));
1108
1109         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1110 }
1111
1112 void WaveTextureNode::compile(OSLCompiler& compiler)
1113 {
1114         tex_mapping.compile(compiler);
1115
1116         compiler.parameter(this, "type");
1117         compiler.parameter(this, "profile");
1118
1119         compiler.add(this, "node_wave_texture");
1120 }
1121
1122 /* Magic Texture */
1123
1124 NODE_DEFINE(MagicTextureNode)
1125 {
1126         NodeType* type = NodeType::add("magic_texture", create, NodeType::SHADER);
1127
1128         TEXTURE_MAPPING_DEFINE(MagicTextureNode);
1129
1130         SOCKET_INT(depth, "Depth", 2);
1131
1132         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
1133         SOCKET_IN_FLOAT(scale, "Scale", 5.0f);
1134         SOCKET_IN_FLOAT(distortion, "Distortion", 1.0f);
1135
1136         SOCKET_OUT_COLOR(color, "Color");
1137         SOCKET_OUT_FLOAT(fac, "Fac");
1138
1139         return type;
1140 }
1141
1142 MagicTextureNode::MagicTextureNode()
1143 : TextureNode(node_type)
1144 {
1145 }
1146
1147 void MagicTextureNode::compile(SVMCompiler& compiler)
1148 {
1149         ShaderInput *vector_in = input("Vector");
1150         ShaderInput *scale_in = input("Scale");
1151         ShaderInput *distortion_in = input("Distortion");
1152         ShaderOutput *color_out = output("Color");
1153         ShaderOutput *fac_out = output("Fac");
1154
1155         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1156
1157         compiler.add_node(NODE_TEX_MAGIC,
1158                 compiler.encode_uchar4(
1159                         depth,
1160                         compiler.stack_assign_if_linked(color_out),
1161                         compiler.stack_assign_if_linked(fac_out)),
1162                 compiler.encode_uchar4(
1163                         vector_offset,
1164                         compiler.stack_assign_if_linked(scale_in),
1165                         compiler.stack_assign_if_linked(distortion_in)));
1166         compiler.add_node(
1167                 __float_as_int(scale),
1168                 __float_as_int(distortion));
1169
1170         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1171 }
1172
1173 void MagicTextureNode::compile(OSLCompiler& compiler)
1174 {
1175         tex_mapping.compile(compiler);
1176
1177         compiler.parameter(this, "depth");
1178         compiler.add(this, "node_magic_texture");
1179 }
1180
1181 /* Checker Texture */
1182
1183 NODE_DEFINE(CheckerTextureNode)
1184 {
1185         NodeType* type = NodeType::add("checker_texture", create, NodeType::SHADER);
1186
1187         TEXTURE_MAPPING_DEFINE(CheckerTextureNode);
1188
1189         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
1190         SOCKET_IN_COLOR(color1, "Color1", make_float3(0.0f, 0.0f, 0.0f));
1191         SOCKET_IN_COLOR(color2, "Color2", make_float3(0.0f, 0.0f, 0.0f));
1192         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
1193
1194         SOCKET_OUT_COLOR(color, "Color");
1195         SOCKET_OUT_FLOAT(fac, "Fac");
1196
1197         return type;
1198 }
1199
1200 CheckerTextureNode::CheckerTextureNode()
1201 : TextureNode(node_type)
1202 {
1203 }
1204
1205 void CheckerTextureNode::compile(SVMCompiler& compiler)
1206 {
1207         ShaderInput *vector_in = input("Vector");
1208         ShaderInput *color1_in = input("Color1");
1209         ShaderInput *color2_in = input("Color2");
1210         ShaderInput *scale_in = input("Scale");
1211         
1212         ShaderOutput *color_out = output("Color");
1213         ShaderOutput *fac_out = output("Fac");
1214
1215         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1216
1217         compiler.add_node(NODE_TEX_CHECKER,
1218                 compiler.encode_uchar4(
1219                         vector_offset,
1220                         compiler.stack_assign(color1_in),
1221                         compiler.stack_assign(color2_in),
1222                         compiler.stack_assign_if_linked(scale_in)),
1223                 compiler.encode_uchar4(
1224                         compiler.stack_assign_if_linked(color_out),
1225                         compiler.stack_assign_if_linked(fac_out)),
1226                 __float_as_int(scale));
1227
1228         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1229 }
1230
1231 void CheckerTextureNode::compile(OSLCompiler& compiler)
1232 {
1233         tex_mapping.compile(compiler);
1234
1235         compiler.add(this, "node_checker_texture");
1236 }
1237
1238 /* Brick Texture */
1239
1240 NODE_DEFINE(BrickTextureNode)
1241 {
1242         NodeType* type = NodeType::add("brick_texture", create, NodeType::SHADER);
1243
1244         TEXTURE_MAPPING_DEFINE(BrickTextureNode);
1245
1246         SOCKET_FLOAT(offset, "Offset", 0.5f);
1247         SOCKET_INT(offset_frequency, "Offset Frequency", 2);
1248         SOCKET_FLOAT(squash, "Squash", 1.0f);
1249         SOCKET_INT(squash_frequency, "Squash Frequency", 2);
1250
1251         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
1252
1253         SOCKET_IN_COLOR(color1, "Color1", make_float3(0.0f, 0.0f, 0.0f));
1254         SOCKET_IN_COLOR(color2, "Color2", make_float3(0.0f, 0.0f, 0.0f));
1255         SOCKET_IN_COLOR(mortar, "Mortar", make_float3(0.0f, 0.0f, 0.0f));
1256         SOCKET_IN_FLOAT(scale, "Scale", 5.0f);
1257         SOCKET_IN_FLOAT(mortar_size, "Mortar Size", 0.02f);
1258         SOCKET_IN_FLOAT(bias, "Bias", 0.0f);
1259         SOCKET_IN_FLOAT(brick_width, "Brick Width", 0.5f);
1260         SOCKET_IN_FLOAT(row_height, "Row Height", 0.25f);
1261
1262         SOCKET_OUT_COLOR(color, "Color");
1263         SOCKET_OUT_FLOAT(fac, "Fac");
1264
1265         return type;
1266 }
1267
1268 BrickTextureNode::BrickTextureNode()
1269 : TextureNode(node_type)
1270 {
1271 }
1272
1273 void BrickTextureNode::compile(SVMCompiler& compiler)
1274 {
1275         ShaderInput *vector_in = input("Vector");
1276         ShaderInput *color1_in = input("Color1");
1277         ShaderInput *color2_in = input("Color2");
1278         ShaderInput *mortar_in = input("Mortar");
1279         ShaderInput *scale_in = input("Scale");
1280         ShaderInput *mortar_size_in = input("Mortar Size");
1281         ShaderInput *bias_in = input("Bias");
1282         ShaderInput *brick_width_in = input("Brick Width");
1283         ShaderInput *row_height_in = input("Row Height");
1284         
1285         ShaderOutput *color_out = output("Color");
1286         ShaderOutput *fac_out = output("Fac");
1287
1288         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1289
1290         compiler.add_node(NODE_TEX_BRICK,
1291                 compiler.encode_uchar4(
1292                         vector_offset,
1293                         compiler.stack_assign(color1_in),
1294                         compiler.stack_assign(color2_in),
1295                         compiler.stack_assign(mortar_in)),
1296                 compiler.encode_uchar4(
1297                         compiler.stack_assign_if_linked(scale_in),
1298                         compiler.stack_assign_if_linked(mortar_size_in),
1299                         compiler.stack_assign_if_linked(bias_in),
1300                         compiler.stack_assign_if_linked(brick_width_in)),
1301                 compiler.encode_uchar4(
1302                         compiler.stack_assign_if_linked(row_height_in),
1303                         compiler.stack_assign_if_linked(color_out),
1304                         compiler.stack_assign_if_linked(fac_out)));
1305                         
1306         compiler.add_node(compiler.encode_uchar4(offset_frequency, squash_frequency),
1307                 __float_as_int(scale),
1308                 __float_as_int(mortar_size),
1309                 __float_as_int(bias));
1310
1311         compiler.add_node(__float_as_int(brick_width),
1312                 __float_as_int(row_height),
1313                 __float_as_int(offset),
1314                 __float_as_int(squash));
1315
1316         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1317 }
1318
1319 void BrickTextureNode::compile(OSLCompiler& compiler)
1320 {
1321         tex_mapping.compile(compiler);
1322
1323         compiler.parameter(this, "offset");
1324         compiler.parameter(this, "offset_frequency");
1325         compiler.parameter(this, "squash");
1326         compiler.parameter(this, "squash_frequency");
1327         compiler.add(this, "node_brick_texture");
1328 }
1329
1330 /* Point Density Texture */
1331
1332 NODE_DEFINE(PointDensityTextureNode)
1333 {
1334         NodeType* type = NodeType::add("point_density_texture", create, NodeType::SHADER);
1335
1336         SOCKET_STRING(filename, "Filename", ustring(""));
1337
1338         static NodeEnum space_enum;
1339         space_enum.insert("object", NODE_TEX_VOXEL_SPACE_OBJECT);
1340         space_enum.insert("world", NODE_TEX_VOXEL_SPACE_WORLD);
1341         SOCKET_ENUM(space, "Space", space_enum, NODE_TEX_VOXEL_SPACE_OBJECT);
1342
1343         static NodeEnum interpolation_enum;
1344         interpolation_enum.insert("closest", INTERPOLATION_CLOSEST);
1345         interpolation_enum.insert("linear", INTERPOLATION_LINEAR);
1346         interpolation_enum.insert("cubic", INTERPOLATION_CUBIC);
1347         interpolation_enum.insert("smart", INTERPOLATION_SMART);
1348         SOCKET_ENUM(interpolation, "Interpolation", interpolation_enum, INTERPOLATION_LINEAR);
1349
1350         SOCKET_TRANSFORM(tfm, "Transform", transform_identity());
1351
1352         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_POSITION);
1353
1354         SOCKET_OUT_FLOAT(density, "Density");
1355         SOCKET_OUT_COLOR(color, "Color");
1356
1357         return type;
1358 }
1359
1360 PointDensityTextureNode::PointDensityTextureNode()
1361 : ShaderNode(node_type)
1362 {
1363         image_manager = NULL;
1364         slot = -1;
1365         builtin_data = NULL;
1366 }
1367
1368 PointDensityTextureNode::~PointDensityTextureNode()
1369 {
1370         if(image_manager) {
1371                 image_manager->remove_image(filename.string(),
1372                                             builtin_data,
1373                                             interpolation,
1374                                             EXTENSION_CLIP);
1375         }
1376 }
1377
1378 ShaderNode *PointDensityTextureNode::clone() const
1379 {
1380         PointDensityTextureNode *node = new PointDensityTextureNode(*this);
1381         node->image_manager = NULL;
1382         node->slot = -1;
1383         return node;
1384 }
1385
1386 void PointDensityTextureNode::attributes(Shader *shader,
1387                                          AttributeRequestSet *attributes)
1388 {
1389         if(shader->has_volume)
1390                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
1391
1392         ShaderNode::attributes(shader, attributes);
1393 }
1394
1395 void PointDensityTextureNode::compile(SVMCompiler& compiler)
1396 {
1397         ShaderInput *vector_in = input("Vector");
1398         ShaderOutput *density_out = output("Density");
1399         ShaderOutput *color_out = output("Color");
1400
1401         const bool use_density = !density_out->links.empty();
1402         const bool use_color = !color_out->links.empty();
1403
1404         image_manager = compiler.image_manager;
1405
1406         if(use_density || use_color) {
1407                 if(slot == -1) {
1408                         bool is_float, is_linear;
1409                         slot = image_manager->add_image(filename.string(), builtin_data,
1410                                                         false, 0,
1411                                                         is_float, is_linear,
1412                                                         interpolation,
1413                                                         EXTENSION_CLIP,
1414                                                         true);
1415                 }
1416
1417                 if(slot != -1) {
1418                         compiler.stack_assign(vector_in);
1419                         compiler.add_node(NODE_TEX_VOXEL,
1420                                           slot,
1421                                           compiler.encode_uchar4(compiler.stack_assign(vector_in),
1422                                                                  compiler.stack_assign_if_linked(density_out),
1423                                                                  compiler.stack_assign_if_linked(color_out),
1424                                                                  space));
1425                         if(space == NODE_TEX_VOXEL_SPACE_WORLD) {
1426                                 compiler.add_node(tfm.x);
1427                                 compiler.add_node(tfm.y);
1428                                 compiler.add_node(tfm.z);
1429                                 compiler.add_node(tfm.w);
1430                         }
1431                 }
1432                 else {
1433                         if(use_density) {
1434                                 compiler.add_node(NODE_VALUE_F,
1435                                                                   __float_as_int(0.0f),
1436                                                                   compiler.stack_assign(density_out));
1437                         }
1438                         if(use_color) {
1439                                 compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
1440                                 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
1441                                                                                                                         TEX_IMAGE_MISSING_G,
1442                                                                                                                         TEX_IMAGE_MISSING_B));
1443                         }
1444                 }
1445         }
1446 }
1447
1448 void PointDensityTextureNode::compile(OSLCompiler& compiler)
1449 {
1450         ShaderOutput *density_out = output("Density");
1451         ShaderOutput *color_out = output("Color");
1452
1453         const bool use_density = !density_out->links.empty();
1454         const bool use_color = !color_out->links.empty();
1455
1456         image_manager = compiler.image_manager;
1457
1458         if(use_density || use_color) {
1459                 if(slot == -1) {
1460                         bool is_float, is_linear;
1461                         slot = image_manager->add_image(filename.string(), builtin_data,
1462                                                         false, 0,
1463                                                         is_float, is_linear,
1464                                                         interpolation,
1465                                                         EXTENSION_CLIP,
1466                                                         true);
1467                 }
1468
1469                 if(slot != -1) {
1470                         compiler.parameter("filename", string_printf("@%d", slot).c_str());
1471                 }
1472                 if(space == NODE_TEX_VOXEL_SPACE_WORLD) {
1473                         compiler.parameter("mapping", transform_transpose(tfm));
1474                         compiler.parameter("use_mapping", 1);
1475                 }
1476                 compiler.parameter(this, "interpolation");
1477                 compiler.add(this, "node_voxel_texture");
1478         }
1479 }
1480
1481 /* Normal */
1482
1483 NODE_DEFINE(NormalNode)
1484 {
1485         NodeType* type = NodeType::add("normal", create, NodeType::SHADER);
1486
1487         SOCKET_VECTOR(direction, "direction", make_float3(0.0f, 0.0f, 0.0f));
1488
1489         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f));
1490
1491         SOCKET_OUT_NORMAL(normal, "Normal");
1492         SOCKET_OUT_FLOAT(dot, "Dot");
1493
1494         return type;
1495 }
1496
1497 NormalNode::NormalNode()
1498 : ShaderNode(node_type)
1499 {
1500 }
1501
1502 void NormalNode::compile(SVMCompiler& compiler)
1503 {
1504         ShaderInput *normal_in = input("Normal");
1505         ShaderOutput *normal_out = output("Normal");
1506         ShaderOutput *dot_out = output("Dot");
1507
1508         compiler.add_node(NODE_NORMAL,
1509                 compiler.stack_assign(normal_in),
1510                 compiler.stack_assign(normal_out),
1511                 compiler.stack_assign(dot_out));
1512         compiler.add_node(
1513                 __float_as_int(direction.x),
1514                 __float_as_int(direction.y),
1515                 __float_as_int(direction.z));
1516 }
1517
1518 void NormalNode::compile(OSLCompiler& compiler)
1519 {
1520         compiler.parameter(this, "direction");
1521         compiler.add(this, "node_normal");
1522 }
1523
1524 /* Mapping */
1525
1526 NODE_DEFINE(MappingNode)
1527 {
1528         NodeType* type = NodeType::add("mapping", create, NodeType::SHADER);
1529
1530         TEXTURE_MAPPING_DEFINE(MappingNode);
1531
1532         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
1533         SOCKET_OUT_POINT(vector, "Vector");
1534
1535         return type;
1536 }
1537
1538 MappingNode::MappingNode()
1539 : ShaderNode(node_type)
1540 {
1541 }
1542
1543 void MappingNode::compile(SVMCompiler& compiler)
1544 {
1545         ShaderInput *vector_in = input("Vector");
1546         ShaderOutput *vector_out = output("Vector");
1547
1548         tex_mapping.compile(compiler, compiler.stack_assign(vector_in), compiler.stack_assign(vector_out));
1549 }
1550
1551 void MappingNode::compile(OSLCompiler& compiler)
1552 {
1553         Transform tfm = transform_transpose(tex_mapping.compute_transform());
1554         compiler.parameter("Matrix", tfm);
1555         compiler.parameter_point("mapping_min", tex_mapping.min);
1556         compiler.parameter_point("mapping_max", tex_mapping.max);
1557         compiler.parameter("use_minmax", tex_mapping.use_minmax);
1558
1559         compiler.add(this, "node_mapping");
1560 }
1561
1562 /* RGBToBW */
1563
1564 NODE_DEFINE(RGBToBWNode)
1565 {
1566         NodeType* type = NodeType::add("rgb_to_bw", create, NodeType::SHADER);
1567
1568         SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
1569         SOCKET_OUT_FLOAT(val, "Val");
1570
1571         return type;
1572 }
1573
1574 RGBToBWNode::RGBToBWNode()
1575 : ShaderNode(node_type)
1576 {
1577 }
1578
1579 bool RGBToBWNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
1580 {
1581         if(all_inputs_constant()) {
1582                 optimized->set(linear_rgb_to_gray(color));
1583                 return true;
1584         }
1585
1586         return false;
1587 }
1588
1589 void RGBToBWNode::compile(SVMCompiler& compiler)
1590 {
1591         compiler.add_node(NODE_CONVERT,
1592                          NODE_CONVERT_CF,
1593                          compiler.stack_assign(inputs[0]),
1594                          compiler.stack_assign(outputs[0]));
1595 }
1596
1597 void RGBToBWNode::compile(OSLCompiler& compiler)
1598 {
1599         compiler.add(this, "node_convert_from_color");
1600 }
1601
1602 /* Convert */
1603
1604 const NodeType* ConvertNode::node_types[ConvertNode::MAX_TYPE][ConvertNode::MAX_TYPE];
1605 bool ConvertNode::initialized = ConvertNode::register_types();
1606
1607 Node* ConvertNode::create(const NodeType *type)
1608 {
1609         return new ConvertNode(type->inputs[0].type,  type->outputs[0].type);
1610 }
1611
1612 bool ConvertNode::register_types()
1613 {
1614         const int num_types = 8;
1615         SocketType::Type types[num_types] = {SocketType::FLOAT,
1616                                              SocketType::INT,
1617                                              SocketType::COLOR,
1618                                              SocketType::VECTOR,
1619                                              SocketType::POINT,
1620                                              SocketType::NORMAL,
1621                                              SocketType::STRING,
1622                                                                                  SocketType::CLOSURE};
1623
1624         for(size_t i = 0; i < num_types; i++) {
1625                 SocketType::Type from = types[i];
1626                 ustring from_name(SocketType::type_name(from));
1627                 ustring from_value_name("value_" + from_name.string());
1628
1629                 for(size_t j = 0; j < num_types; j++) {
1630                         SocketType::Type to = types[j];
1631                         ustring to_name(SocketType::type_name(to));
1632                         ustring to_value_name("value_" + to_name.string());
1633
1634                         string node_name = "convert_" + from_name.string() + "_to_" + to_name.string();
1635                         NodeType* type = NodeType::add(node_name.c_str(), create, NodeType::SHADER);
1636
1637                         type->register_input(from_value_name, from_value_name, from,
1638                                 SOCKET_OFFSETOF(ConvertNode, value_float), SocketType::zero_default_value(),
1639                                 NULL, NULL, SocketType::LINKABLE);
1640                         type->register_output(to_value_name, to_value_name, to);
1641
1642                         assert(from < MAX_TYPE);
1643                         assert(to < MAX_TYPE);
1644
1645                         node_types[from][to] = type;
1646                 }
1647         }
1648
1649         return true;
1650 }
1651
1652 ConvertNode::ConvertNode(SocketType::Type from_, SocketType::Type to_, bool autoconvert)
1653 : ShaderNode(node_types[from_][to_])
1654 {
1655         from = from_;
1656         to = to_;
1657
1658         if(from == to)
1659                 special_type = SHADER_SPECIAL_TYPE_PROXY;
1660         else if(autoconvert)
1661                 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1662 }
1663
1664 bool ConvertNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
1665 {
1666         /* proxy nodes should have been removed at this point */
1667         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1668
1669         /* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
1670
1671         if(all_inputs_constant()) {
1672                 if(from == SocketType::FLOAT) {
1673                         if(SocketType::is_float3(to)) {
1674                                 optimized->set(make_float3(value_float, value_float, value_float));
1675                                 return true;
1676                         }
1677                 }
1678                 else if(SocketType::is_float3(from)) {
1679                         if(to == SocketType::FLOAT) {
1680                                 if(from == SocketType::COLOR)
1681                                         /* color to float */
1682                                         optimized->set(linear_rgb_to_gray(value_color));
1683                                 else
1684                                         /* vector/point/normal to float */
1685                                         optimized->set(average(value_vector));
1686                                 return true;
1687                         }
1688                         else if(SocketType::is_float3(to)) {
1689                                 optimized->set(value_color);
1690                                 return true;
1691                         }
1692                 }
1693         }
1694
1695         return false;
1696 }
1697
1698 void ConvertNode::compile(SVMCompiler& compiler)
1699 {
1700         /* proxy nodes should have been removed at this point */
1701         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1702
1703         ShaderInput *in = inputs[0];
1704         ShaderOutput *out = outputs[0];
1705
1706         if(from == SocketType::FLOAT) {
1707                 if(to == SocketType::INT)
1708                         /* float to int */
1709                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, compiler.stack_assign(in), compiler.stack_assign(out));
1710                 else
1711                         /* float to float3 */
1712                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, compiler.stack_assign(in), compiler.stack_assign(out));
1713         }
1714         else if(from == SocketType::INT) {
1715                 if(to == SocketType::FLOAT)
1716                         /* int to float */
1717                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, compiler.stack_assign(in), compiler.stack_assign(out));
1718                 else
1719                         /* int to vector/point/normal */
1720                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, compiler.stack_assign(in), compiler.stack_assign(out));
1721         }
1722         else if(to == SocketType::FLOAT) {
1723                 if(from == SocketType::COLOR)
1724                         /* color to float */
1725                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, compiler.stack_assign(in), compiler.stack_assign(out));
1726                 else
1727                         /* vector/point/normal to float */
1728                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, compiler.stack_assign(in), compiler.stack_assign(out));
1729         }
1730         else if(to == SocketType::INT) {
1731                 if(from == SocketType::COLOR)
1732                         /* color to int */
1733                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, compiler.stack_assign(in), compiler.stack_assign(out));
1734                 else
1735                         /* vector/point/normal to int */
1736                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, compiler.stack_assign(in), compiler.stack_assign(out));
1737         }
1738         else {
1739                 /* float3 to float3 */
1740                 if(in->link) {
1741                         /* no op in SVM */
1742                         compiler.stack_link(in, out);
1743                 }
1744                 else {
1745                         /* set 0,0,0 value */
1746                         compiler.add_node(NODE_VALUE_V, compiler.stack_assign(out));
1747                         compiler.add_node(NODE_VALUE_V, value_color);
1748                 }
1749         }
1750 }
1751
1752 void ConvertNode::compile(OSLCompiler& compiler)
1753 {
1754         /* proxy nodes should have been removed at this point */
1755         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1756
1757         if(from == SocketType::FLOAT)
1758                 compiler.add(this, "node_convert_from_float");
1759         else if(from == SocketType::INT)
1760                 compiler.add(this, "node_convert_from_int");
1761         else if(from == SocketType::COLOR)
1762                 compiler.add(this, "node_convert_from_color");
1763         else if(from == SocketType::VECTOR)
1764                 compiler.add(this, "node_convert_from_vector");
1765         else if(from == SocketType::POINT)
1766                 compiler.add(this, "node_convert_from_point");
1767         else if(from == SocketType::NORMAL)
1768                 compiler.add(this, "node_convert_from_normal");
1769         else
1770                 assert(0);
1771 }
1772
1773 /* BSDF Closure */
1774
1775 BsdfNode::BsdfNode(const NodeType *node_type)
1776 : ShaderNode(node_type)
1777 {
1778         special_type = SHADER_SPECIAL_TYPE_CLOSURE;
1779 }
1780
1781 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3, ShaderInput *param4)
1782 {
1783         ShaderInput *color_in = input("Color");
1784         ShaderInput *normal_in = input("Normal");
1785         ShaderInput *tangent_in = input("Tangent");
1786
1787         if(color_in->link)
1788                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
1789         else
1790                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
1791
1792         int normal_offset = (normal_in) ? compiler.stack_assign_if_linked(normal_in) : SVM_STACK_INVALID;
1793         int tangent_offset = (tangent_in) ? compiler.stack_assign_if_linked(tangent_in) : SVM_STACK_INVALID;
1794         int param3_offset = (param3) ? compiler.stack_assign(param3) : SVM_STACK_INVALID;
1795         int param4_offset = (param4) ? compiler.stack_assign(param4) : SVM_STACK_INVALID;
1796
1797         compiler.add_node(NODE_CLOSURE_BSDF,
1798                 compiler.encode_uchar4(closure,
1799                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
1800                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
1801                         compiler.closure_mix_weight_offset()),
1802                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
1803                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
1804
1805         compiler.add_node(normal_offset, tangent_offset, param3_offset, param4_offset);
1806 }
1807
1808 void BsdfNode::compile(SVMCompiler& compiler)
1809 {
1810         compile(compiler, NULL, NULL);
1811 }
1812
1813 void BsdfNode::compile(OSLCompiler& /*compiler*/)
1814 {
1815         assert(0);
1816 }
1817
1818 /* Anisotropic BSDF Closure */
1819
1820 NODE_DEFINE(AnisotropicBsdfNode)
1821 {
1822         NodeType* type = NodeType::add("anisotropic_bsdf", create, NodeType::SHADER);
1823
1824         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
1825         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1826         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
1827
1828         static NodeEnum distribution_enum;
1829         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID);
1830         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1831         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_ANISO_ID);
1832         distribution_enum.insert("ashikhmin_shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID);
1833         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1834
1835         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
1836
1837         SOCKET_IN_FLOAT(roughness, "Roughness", 0.2f);
1838         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.5f);
1839         SOCKET_IN_FLOAT(rotation, "Rotation", 0.0f);
1840
1841         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
1842
1843         return type;
1844 }
1845
1846 AnisotropicBsdfNode::AnisotropicBsdfNode()
1847 : BsdfNode(node_type)
1848 {
1849         closure = CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID;
1850 }
1851
1852 void AnisotropicBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
1853 {
1854         if(shader->has_surface) {
1855                 ShaderInput *tangent_in = input("Tangent");
1856
1857                 if(!tangent_in->link)
1858                         attributes->add(ATTR_STD_GENERATED);
1859         }
1860
1861         ShaderNode::attributes(shader, attributes);
1862 }
1863
1864 void AnisotropicBsdfNode::compile(SVMCompiler& compiler)
1865 {
1866         closure = distribution;
1867
1868         if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ANISO_ID)
1869                 BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"), input("Color"));
1870         else
1871                 BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1872 }
1873
1874 void AnisotropicBsdfNode::compile(OSLCompiler& compiler)
1875 {
1876         compiler.parameter(this, "distribution");
1877         compiler.add(this, "node_anisotropic_bsdf");
1878 }
1879
1880 /* Glossy BSDF Closure */
1881
1882 NODE_DEFINE(GlossyBsdfNode)
1883 {
1884         NodeType* type = NodeType::add("glossy_bsdf", create, NodeType::SHADER);
1885
1886         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
1887         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1888         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
1889
1890         static NodeEnum distribution_enum;
1891         distribution_enum.insert("sharp", CLOSURE_BSDF_REFLECTION_ID);
1892         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1893         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1894         distribution_enum.insert("ashikhmin_shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
1895         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID);
1896         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ID);
1897         SOCKET_IN_FLOAT(roughness, "Roughness", 0.2f);
1898
1899         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
1900
1901         return type;
1902 }
1903
1904 GlossyBsdfNode::GlossyBsdfNode()
1905 : BsdfNode(node_type)
1906 {
1907         closure = CLOSURE_BSDF_MICROFACET_GGX_ID;
1908         distribution_orig = NBUILTIN_CLOSURES;
1909 }
1910
1911 void GlossyBsdfNode::simplify_settings(Scene *scene)
1912 {
1913         if(distribution_orig == NBUILTIN_CLOSURES) {
1914                 distribution_orig = distribution;
1915         }
1916         Integrator *integrator = scene->integrator;
1917         if(integrator->filter_glossy == 0.0f) {
1918                 /* Fallback to Sharp closure for Roughness close to 0.
1919                  * Note: Keep the epsilon in sync with kernel!
1920                  */
1921                 ShaderInput *roughness_input = input("Roughness");
1922                 if(!roughness_input->link && roughness <= 1e-4f) {
1923                         distribution = CLOSURE_BSDF_REFLECTION_ID;
1924                 }
1925         }
1926         else {
1927                 /* Rollback to original distribution when filter glossy is used. */
1928                 distribution = distribution_orig;
1929         }
1930         closure = distribution;
1931 }
1932
1933 bool GlossyBsdfNode::has_integrator_dependency()
1934 {
1935         ShaderInput *roughness_input = input("Roughness");
1936         return !roughness_input->link && roughness <= 1e-4f;
1937 }
1938
1939 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1940 {
1941         closure = distribution;
1942
1943         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1944                 BsdfNode::compile(compiler, NULL, NULL);
1945         else if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID)
1946                 BsdfNode::compile(compiler, input("Roughness"), NULL, input("Color"));
1947         else
1948                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1949 }
1950
1951 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1952 {
1953         compiler.parameter(this, "distribution");
1954         compiler.add(this, "node_glossy_bsdf");
1955 }
1956
1957 /* Glass BSDF Closure */
1958
1959 NODE_DEFINE(GlassBsdfNode)
1960 {
1961         NodeType* type = NodeType::add("glass_bsdf", create, NodeType::SHADER);
1962
1963         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
1964         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1965         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
1966
1967         static NodeEnum distribution_enum;
1968         distribution_enum.insert("sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1969         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1970         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1971         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
1972         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1973         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
1974         SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
1975
1976         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
1977
1978         return type;
1979 }
1980
1981 GlassBsdfNode::GlassBsdfNode()
1982 : BsdfNode(node_type)
1983 {
1984         closure = CLOSURE_BSDF_SHARP_GLASS_ID;
1985         distribution_orig = NBUILTIN_CLOSURES;
1986 }
1987
1988 void GlassBsdfNode::simplify_settings(Scene *scene)
1989 {
1990         if(distribution_orig == NBUILTIN_CLOSURES) {
1991                 distribution_orig = distribution;
1992         }
1993         Integrator *integrator = scene->integrator;
1994         if(integrator->filter_glossy == 0.0f) {
1995                 /* Fallback to Sharp closure for Roughness close to 0.
1996                  * Note: Keep the epsilon in sync with kernel!
1997                  */
1998                 ShaderInput *roughness_input = input("Roughness");
1999                 if(!roughness_input->link && roughness <= 1e-4f) {
2000                         distribution = CLOSURE_BSDF_SHARP_GLASS_ID;
2001                 }
2002         }
2003         else {
2004                 /* Rollback to original distribution when filter glossy is used. */
2005                 distribution = distribution_orig;
2006         }
2007         closure = distribution;
2008 }
2009
2010 bool GlassBsdfNode::has_integrator_dependency()
2011 {
2012         ShaderInput *roughness_input = input("Roughness");
2013         return !roughness_input->link && roughness <= 1e-4f;
2014 }
2015
2016 void GlassBsdfNode::compile(SVMCompiler& compiler)
2017 {
2018         closure = distribution;
2019
2020         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
2021                 BsdfNode::compile(compiler, NULL, input("IOR"));
2022         else if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID)
2023                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"), input("Color"));
2024         else
2025                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2026 }
2027
2028 void GlassBsdfNode::compile(OSLCompiler& compiler)
2029 {
2030         compiler.parameter(this, "distribution");
2031         compiler.add(this, "node_glass_bsdf");
2032 }
2033
2034 /* Refraction BSDF Closure */
2035
2036 NODE_DEFINE(RefractionBsdfNode)
2037 {
2038         NodeType* type = NodeType::add("refraction_bsdf", create, NodeType::SHADER);
2039
2040         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2041         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2042         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2043
2044         static NodeEnum distribution_enum;
2045         distribution_enum.insert("sharp", CLOSURE_BSDF_REFRACTION_ID);
2046         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
2047         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2048         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2049
2050         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2051         SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
2052
2053         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2054
2055         return type;
2056 }
2057
2058 RefractionBsdfNode::RefractionBsdfNode()
2059 : BsdfNode(node_type)
2060 {
2061         closure = CLOSURE_BSDF_REFRACTION_ID;
2062         distribution_orig = NBUILTIN_CLOSURES;
2063 }
2064
2065 void RefractionBsdfNode::simplify_settings(Scene *scene)
2066 {
2067         if(distribution_orig == NBUILTIN_CLOSURES) {
2068                 distribution_orig = distribution;
2069         }
2070         Integrator *integrator = scene->integrator;
2071         if(integrator->filter_glossy == 0.0f) {
2072                 /* Fallback to Sharp closure for Roughness close to 0.
2073                  * Note: Keep the epsilon in sync with kernel!
2074                  */
2075                 ShaderInput *roughness_input = input("Roughness");
2076                 if(!roughness_input->link && roughness <= 1e-4f) {
2077                         distribution = CLOSURE_BSDF_REFRACTION_ID;
2078                 }
2079         }
2080         else {
2081                 /* Rollback to original distribution when filter glossy is used. */
2082                 distribution = distribution_orig;
2083         }
2084         closure = distribution;
2085 }
2086
2087 bool RefractionBsdfNode::has_integrator_dependency()
2088 {
2089         ShaderInput *roughness_input = input("Roughness");
2090         return !roughness_input->link && roughness <= 1e-4f;
2091 }
2092
2093 void RefractionBsdfNode::compile(SVMCompiler& compiler)
2094 {
2095         closure = distribution;
2096
2097         if(closure == CLOSURE_BSDF_REFRACTION_ID)
2098                 BsdfNode::compile(compiler, NULL, input("IOR"));
2099         else
2100                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2101 }
2102
2103 void RefractionBsdfNode::compile(OSLCompiler& compiler)
2104 {
2105         compiler.parameter(this, "distribution");
2106         compiler.add(this, "node_refraction_bsdf");
2107 }
2108
2109 /* Toon BSDF Closure */
2110
2111 NODE_DEFINE(ToonBsdfNode)
2112 {
2113         NodeType* type = NodeType::add("toon_bsdf", create, NodeType::SHADER);
2114
2115         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2116         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2117         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2118
2119         static NodeEnum component_enum;
2120         component_enum.insert("diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
2121         component_enum.insert("glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
2122         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_DIFFUSE_TOON_ID);
2123         SOCKET_IN_FLOAT(size, "Size", 0.5f);
2124         SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
2125
2126         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2127
2128         return type;
2129 }
2130
2131 ToonBsdfNode::ToonBsdfNode()
2132 : BsdfNode(node_type)
2133 {
2134         closure = CLOSURE_BSDF_DIFFUSE_TOON_ID;
2135 }
2136
2137 void ToonBsdfNode::compile(SVMCompiler& compiler)
2138 {
2139         closure = component;
2140         
2141         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
2142 }
2143
2144 void ToonBsdfNode::compile(OSLCompiler& compiler)
2145 {
2146         compiler.parameter(this, "component");
2147         compiler.add(this, "node_toon_bsdf");
2148 }
2149
2150 /* Velvet BSDF Closure */
2151
2152 NODE_DEFINE(VelvetBsdfNode)
2153 {
2154         NodeType* type = NodeType::add("velvet_bsdf", create, NodeType::SHADER);
2155
2156         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2157         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2158         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2159         SOCKET_IN_FLOAT(sigma, "Sigma", 1.0f);
2160
2161         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2162
2163         return type;
2164 }
2165
2166 VelvetBsdfNode::VelvetBsdfNode()
2167 : BsdfNode(node_type)
2168 {
2169         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
2170 }
2171
2172 void VelvetBsdfNode::compile(SVMCompiler& compiler)
2173 {
2174         BsdfNode::compile(compiler, input("Sigma"), NULL);
2175 }
2176
2177 void VelvetBsdfNode::compile(OSLCompiler& compiler)
2178 {
2179         compiler.add(this, "node_velvet_bsdf");
2180 }
2181
2182 /* Diffuse BSDF Closure */
2183
2184 NODE_DEFINE(DiffuseBsdfNode)
2185 {
2186         NodeType* type = NodeType::add("diffuse_bsdf", create, NodeType::SHADER);
2187
2188         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2189         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2190         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2191         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2192
2193         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2194
2195         return type;
2196 }
2197
2198 DiffuseBsdfNode::DiffuseBsdfNode()
2199 : BsdfNode(node_type)
2200 {
2201         closure = CLOSURE_BSDF_DIFFUSE_ID;
2202 }
2203
2204 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
2205 {
2206         BsdfNode::compile(compiler, input("Roughness"), NULL);
2207 }
2208
2209 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
2210 {
2211         compiler.add(this, "node_diffuse_bsdf");
2212 }
2213
2214 /* Translucent BSDF Closure */
2215
2216 NODE_DEFINE(TranslucentBsdfNode)
2217 {
2218         NodeType* type = NodeType::add("translucent_bsdf", create, NodeType::SHADER);
2219
2220         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2221         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2222         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2223
2224         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2225
2226         return type;
2227 }
2228
2229 TranslucentBsdfNode::TranslucentBsdfNode()
2230 : BsdfNode(node_type)
2231 {
2232         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
2233 }
2234
2235 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
2236 {
2237         BsdfNode::compile(compiler, NULL, NULL);
2238 }
2239
2240 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
2241 {
2242         compiler.add(this, "node_translucent_bsdf");
2243 }
2244
2245 /* Transparent BSDF Closure */
2246
2247 NODE_DEFINE(TransparentBsdfNode)
2248 {
2249         NodeType* type = NodeType::add("transparent_bsdf", create, NodeType::SHADER);
2250
2251         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2252         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2253
2254         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2255
2256         return type;
2257 }
2258
2259 TransparentBsdfNode::TransparentBsdfNode()
2260 : BsdfNode(node_type)
2261 {
2262         closure = CLOSURE_BSDF_TRANSPARENT_ID;
2263 }
2264
2265 void TransparentBsdfNode::compile(SVMCompiler& compiler)
2266 {
2267         BsdfNode::compile(compiler, NULL, NULL);
2268 }
2269
2270 void TransparentBsdfNode::compile(OSLCompiler& compiler)
2271 {
2272         compiler.add(this, "node_transparent_bsdf");
2273 }
2274
2275 /* Subsurface Scattering Closure */
2276
2277 NODE_DEFINE(SubsurfaceScatteringNode)
2278 {
2279         NodeType* type = NodeType::add("subsurface_scattering", create, NodeType::SHADER);
2280
2281         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2282         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2283         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2284
2285         static NodeEnum falloff_enum;
2286         falloff_enum.insert("cubic", CLOSURE_BSSRDF_CUBIC_ID);
2287         falloff_enum.insert("gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
2288         falloff_enum.insert("burley", CLOSURE_BSSRDF_BURLEY_ID);
2289         SOCKET_ENUM(falloff, "Falloff", falloff_enum, CLOSURE_BSSRDF_BURLEY_ID);
2290         SOCKET_IN_FLOAT(scale, "Scale", 0.01f);
2291         SOCKET_IN_VECTOR(radius, "Radius", make_float3(0.1f, 0.1f, 0.1f));
2292         SOCKET_IN_FLOAT(sharpness, "Sharpness", 0.0f);
2293         SOCKET_IN_FLOAT(texture_blur, "Texture Blur", 1.0f);
2294
2295         SOCKET_OUT_CLOSURE(BSSRDF, "BSSRDF");
2296
2297         return type;
2298 }
2299
2300 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
2301 : BsdfNode(node_type)
2302 {
2303 }
2304
2305 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
2306 {
2307         closure = falloff;
2308         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
2309 }
2310
2311 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
2312 {
2313         compiler.parameter(this, "falloff");
2314         compiler.add(this, "node_subsurface_scattering");
2315 }
2316
2317 bool SubsurfaceScatteringNode::has_bssrdf_bump()
2318 {
2319         /* detect if anything is plugged into the normal input besides the default */
2320         ShaderInput *normal_in = input("Normal");
2321         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
2322 }
2323
2324 /* Emissive Closure */
2325
2326 NODE_DEFINE(EmissionNode)
2327 {
2328         NodeType* type = NodeType::add("emission", create, NodeType::SHADER);
2329
2330         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2331         SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
2332         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2333
2334         SOCKET_OUT_CLOSURE(emission, "Emission");
2335
2336         return type;
2337 }
2338
2339 EmissionNode::EmissionNode()
2340 : ShaderNode(node_type)
2341 {
2342 }
2343
2344 void EmissionNode::compile(SVMCompiler& compiler)
2345 {
2346         ShaderInput *color_in = input("Color");
2347         ShaderInput *strength_in = input("Strength");
2348
2349         if(color_in->link || strength_in->link) {
2350                 compiler.add_node(NODE_EMISSION_WEIGHT,
2351                                   compiler.stack_assign(color_in),
2352                                                   compiler.stack_assign(strength_in));
2353         }
2354         else
2355                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color * strength);
2356
2357         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
2358 }
2359
2360 void EmissionNode::compile(OSLCompiler& compiler)
2361 {
2362         compiler.add(this, "node_emission");
2363 }
2364
2365 bool EmissionNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *)
2366 {
2367         ShaderInput *color_in = input("Color");
2368         ShaderInput *strength_in = input("Strength");
2369
2370         return ((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2371                 (!strength_in->link && strength == 0.0f));
2372 }
2373
2374 /* Background Closure */
2375
2376 NODE_DEFINE(BackgroundNode)
2377 {
2378         NodeType* type = NodeType::add("background_shader", create, NodeType::SHADER);
2379
2380         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2381         SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
2382         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2383
2384         SOCKET_OUT_CLOSURE(background, "Background");
2385
2386         return type;
2387 }
2388
2389 BackgroundNode::BackgroundNode()
2390 : ShaderNode(node_type)
2391 {
2392 }
2393
2394 void BackgroundNode::compile(SVMCompiler& compiler)
2395 {
2396         ShaderInput *color_in = input("Color");
2397         ShaderInput *strength_in = input("Strength");
2398
2399         if(color_in->link || strength_in->link) {
2400                 compiler.add_node(NODE_EMISSION_WEIGHT,
2401                                   compiler.stack_assign(color_in),
2402                                                   compiler.stack_assign(strength_in));
2403         }
2404         else
2405                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color*strength);
2406
2407         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
2408 }
2409
2410 void BackgroundNode::compile(OSLCompiler& compiler)
2411 {
2412         compiler.add(this, "node_background");
2413 }
2414
2415 bool BackgroundNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *)
2416 {
2417         ShaderInput *color_in = input("Color");
2418         ShaderInput *strength_in = input("Strength");
2419
2420         return ((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2421                 (!strength_in->link && strength == 0.0f));
2422 }
2423
2424 /* Holdout Closure */
2425
2426 NODE_DEFINE(HoldoutNode)
2427 {
2428         NodeType* type = NodeType::add("holdout", create, NodeType::SHADER);
2429
2430         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2431         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2432
2433         SOCKET_OUT_CLOSURE(holdout, "Holdout");
2434
2435         return type;
2436 }
2437
2438 HoldoutNode::HoldoutNode()
2439 : ShaderNode(node_type)
2440 {
2441 }
2442
2443 void HoldoutNode::compile(SVMCompiler& compiler)
2444 {
2445         float3 value = make_float3(1.0f, 1.0f, 1.0f);
2446
2447         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
2448         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
2449 }
2450
2451 void HoldoutNode::compile(OSLCompiler& compiler)
2452 {
2453         compiler.add(this, "node_holdout");
2454 }
2455
2456 /* Ambient Occlusion */
2457
2458 NODE_DEFINE(AmbientOcclusionNode)
2459 {
2460         NodeType* type = NodeType::add("ambient_occlusion", create, NodeType::SHADER);
2461
2462         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2463         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2464         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2465
2466         SOCKET_OUT_CLOSURE(AO, "AO");
2467
2468         return type;
2469 }
2470
2471 AmbientOcclusionNode::AmbientOcclusionNode()
2472 : ShaderNode(node_type)
2473 {
2474 }
2475
2476 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
2477 {
2478         ShaderInput *color_in = input("Color");
2479
2480         if(color_in->link)
2481                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2482         else
2483                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2484
2485         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
2486 }
2487
2488 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
2489 {
2490         compiler.add(this, "node_ambient_occlusion");
2491 }
2492
2493 /* Volume Closure */
2494
2495 VolumeNode::VolumeNode(const NodeType *node_type)
2496 : ShaderNode(node_type)
2497 {
2498         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2499 }
2500
2501 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
2502 {
2503         ShaderInput *color_in = input("Color");
2504
2505         if(color_in->link)
2506                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2507         else
2508                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2509         
2510         compiler.add_node(NODE_CLOSURE_VOLUME,
2511                 compiler.encode_uchar4(closure,
2512                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
2513                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
2514                         compiler.closure_mix_weight_offset()),
2515                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
2516                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
2517 }
2518
2519 void VolumeNode::compile(SVMCompiler& compiler)
2520 {
2521         compile(compiler, NULL, NULL);
2522 }
2523
2524 void VolumeNode::compile(OSLCompiler& /*compiler*/)
2525 {
2526         assert(0);
2527 }
2528
2529 /* Absorption Volume Closure */
2530
2531 NODE_DEFINE(AbsorptionVolumeNode)
2532 {
2533         NodeType* type = NodeType::add("absorption_volume", create, NodeType::SHADER);
2534
2535         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2536         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2537         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2538
2539         SOCKET_OUT_CLOSURE(volume, "Volume");
2540
2541         return type;
2542 }
2543
2544 AbsorptionVolumeNode::AbsorptionVolumeNode()
2545 : VolumeNode(node_type)
2546 {
2547         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2548 }
2549
2550 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2551 {
2552         VolumeNode::compile(compiler, input("Density"), NULL);
2553 }
2554
2555 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2556 {
2557         compiler.add(this, "node_absorption_volume");
2558 }
2559
2560 /* Scatter Volume Closure */
2561
2562 NODE_DEFINE(ScatterVolumeNode)
2563 {
2564         NodeType* type = NodeType::add("scatter_volume", create, NodeType::SHADER);
2565
2566         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2567         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2568         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
2569         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2570
2571         SOCKET_OUT_CLOSURE(volume, "Volume");
2572
2573         return type;
2574 }
2575
2576 ScatterVolumeNode::ScatterVolumeNode()
2577 : VolumeNode(node_type)
2578 {
2579         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2580 }
2581
2582 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2583 {
2584         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2585 }
2586
2587 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2588 {
2589         compiler.add(this, "node_scatter_volume");
2590 }
2591
2592 /* Hair BSDF Closure */
2593
2594 NODE_DEFINE(HairBsdfNode)
2595 {
2596         NodeType* type = NodeType::add("hair_bsdf", create, NodeType::SHADER);
2597
2598         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2599         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2600         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2601
2602         static NodeEnum component_enum;
2603         component_enum.insert("reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2604         component_enum.insert("transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2605         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_HAIR_REFLECTION_ID);
2606         SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
2607         SOCKET_IN_FLOAT(roughness_u, "RoughnessU", 0.2f);
2608         SOCKET_IN_FLOAT(roughness_v, "RoughnessV", 0.2f);
2609         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f));
2610
2611         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2612
2613         return type;
2614 }
2615
2616 HairBsdfNode::HairBsdfNode()
2617 : BsdfNode(node_type)
2618 {
2619         closure = CLOSURE_BSDF_HAIR_REFLECTION_ID;
2620 }
2621
2622 void HairBsdfNode::compile(SVMCompiler& compiler)
2623 {
2624         closure = component;
2625
2626         BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
2627 }
2628
2629 void HairBsdfNode::compile(OSLCompiler& compiler)
2630 {
2631         compiler.parameter(this, "component");
2632         compiler.add(this, "node_hair_bsdf");
2633 }
2634
2635 /* Geometry */
2636
2637 NODE_DEFINE(GeometryNode)
2638 {
2639         NodeType* type = NodeType::add("geometry", create, NodeType::SHADER);
2640
2641         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2642
2643         SOCKET_OUT_POINT(position, "Position");
2644         SOCKET_OUT_NORMAL(normal, "Normal");
2645         SOCKET_OUT_NORMAL(tangent, "Tangent");
2646         SOCKET_OUT_NORMAL(true_normal, "True Normal");
2647         SOCKET_OUT_VECTOR(incoming, "Incoming");
2648         SOCKET_OUT_POINT(parametric, "Parametric");
2649         SOCKET_OUT_FLOAT(backfacing, "Backfacing");
2650         SOCKET_OUT_FLOAT(pointiness, "Pointiness");
2651
2652         return type;
2653 }
2654
2655 GeometryNode::GeometryNode()
2656 : ShaderNode(node_type)
2657 {
2658         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2659 }
2660
2661 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2662 {
2663         if(shader->has_surface) {
2664                 if(!output("Tangent")->links.empty()) {
2665                         attributes->add(ATTR_STD_GENERATED);
2666                 }
2667                 if(!output("Pointiness")->links.empty()) {
2668                         attributes->add(ATTR_STD_POINTINESS);
2669                 }
2670         }
2671
2672         ShaderNode::attributes(shader, attributes);
2673 }
2674
2675 void GeometryNode::compile(SVMCompiler& compiler)
2676 {
2677         ShaderOutput *out;
2678         ShaderNodeType geom_node = NODE_GEOMETRY;
2679         ShaderNodeType attr_node = NODE_ATTR;
2680
2681         if(bump == SHADER_BUMP_DX) {
2682                 geom_node = NODE_GEOMETRY_BUMP_DX;
2683                 attr_node = NODE_ATTR_BUMP_DX;
2684         }
2685         else if(bump == SHADER_BUMP_DY) {
2686                 geom_node = NODE_GEOMETRY_BUMP_DY;
2687                 attr_node = NODE_ATTR_BUMP_DY;
2688         }
2689         
2690         out = output("Position");
2691         if(!out->links.empty()) {
2692                 compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
2693         }
2694
2695         out = output("Normal");
2696         if(!out->links.empty()) {
2697                 compiler.add_node(geom_node, NODE_GEOM_N, compiler.stack_assign(out));
2698         }
2699
2700         out = output("Tangent");
2701         if(!out->links.empty()) {
2702                 compiler.add_node(geom_node, NODE_GEOM_T, compiler.stack_assign(out));
2703         }
2704
2705         out = output("True Normal");
2706         if(!out->links.empty()) {
2707                 compiler.add_node(geom_node, NODE_GEOM_Ng, compiler.stack_assign(out));
2708         }
2709
2710         out = output("Incoming");
2711         if(!out->links.empty()) {
2712                 compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
2713         }
2714
2715         out = output("Parametric");
2716         if(!out->links.empty()) {
2717                 compiler.add_node(geom_node, NODE_GEOM_uv, compiler.stack_assign(out));
2718         }
2719
2720         out = output("Backfacing");
2721         if(!out->links.empty()) {
2722                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, compiler.stack_assign(out));
2723         }
2724
2725         out = output("Pointiness");
2726         if(!out->links.empty()) {
2727                 if(compiler.output_type() != SHADER_TYPE_VOLUME) {
2728                         compiler.add_node(attr_node,
2729                                           ATTR_STD_POINTINESS,
2730                                           compiler.stack_assign(out),
2731                                           NODE_ATTR_FLOAT);
2732                 }
2733                 else {
2734                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), compiler.stack_assign(out));
2735                 }
2736         }
2737 }
2738
2739 void GeometryNode::compile(OSLCompiler& compiler)
2740 {
2741         if(bump == SHADER_BUMP_DX)
2742                 compiler.parameter("bump_offset", "dx");
2743         else if(bump == SHADER_BUMP_DY)
2744                 compiler.parameter("bump_offset", "dy");
2745         else
2746                 compiler.parameter("bump_offset", "center");
2747
2748         compiler.add(this, "node_geometry");
2749 }
2750
2751 /* TextureCoordinate */
2752
2753 NODE_DEFINE(TextureCoordinateNode)
2754 {
2755         NodeType* type = NodeType::add("texture_coordinate", create, NodeType::SHADER);
2756
2757         SOCKET_BOOLEAN(from_dupli, "From Dupli", false);
2758         SOCKET_BOOLEAN(use_transform, "Use Transform", false);
2759         SOCKET_TRANSFORM(ob_tfm, "Object Transform", transform_identity());
2760
2761         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2762
2763         SOCKET_OUT_POINT(generated, "Generated");
2764         SOCKET_OUT_NORMAL(normal, "Normal");
2765         SOCKET_OUT_POINT(UV, "UV");
2766         SOCKET_OUT_POINT(object, "Object");
2767         SOCKET_OUT_POINT(camera, "Camera");
2768         SOCKET_OUT_POINT(window, "Window");
2769         SOCKET_OUT_NORMAL(reflection, "Reflection");
2770
2771         return type;
2772 }
2773
2774 TextureCoordinateNode::TextureCoordinateNode()
2775 : ShaderNode(node_type)
2776 {
2777 }
2778
2779 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2780 {
2781         if(shader->has_surface) {
2782                 if(!from_dupli) {
2783                         if(!output("Generated")->links.empty())
2784                                 attributes->add(ATTR_STD_GENERATED);
2785                         if(!output("UV")->links.empty())
2786                                 attributes->add(ATTR_STD_UV);
2787                 }
2788         }
2789
2790         if(shader->has_volume) {
2791                 if(!from_dupli) {
2792                         if(!output("Generated")->links.empty()) {
2793                                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
2794                         }
2795                 }
2796         }
2797
2798         ShaderNode::attributes(shader, attributes);
2799 }
2800
2801 void TextureCoordinateNode::compile(SVMCompiler& compiler)
2802 {
2803         ShaderOutput *out;
2804         ShaderNodeType texco_node = NODE_TEX_COORD;
2805         ShaderNodeType attr_node = NODE_ATTR;
2806         ShaderNodeType geom_node = NODE_GEOMETRY;
2807
2808         if(bump == SHADER_BUMP_DX) {
2809                 texco_node = NODE_TEX_COORD_BUMP_DX;
2810                 attr_node = NODE_ATTR_BUMP_DX;
2811                 geom_node = NODE_GEOMETRY_BUMP_DX;
2812         }
2813         else if(bump == SHADER_BUMP_DY) {
2814                 texco_node = NODE_TEX_COORD_BUMP_DY;
2815                 attr_node = NODE_ATTR_BUMP_DY;
2816                 geom_node = NODE_GEOMETRY_BUMP_DY;
2817         }
2818         
2819         out = output("Generated");
2820         if(!out->links.empty()) {
2821                 if(compiler.background) {
2822                         compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
2823                 }
2824                 else {
2825                         if(from_dupli) {
2826                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, compiler.stack_assign(out));
2827                         }
2828                         else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
2829                                 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, compiler.stack_assign(out));
2830                         }
2831                         else {
2832                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
2833                                 compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2834                         }
2835                 }
2836         }
2837
2838         out = output("Normal");
2839         if(!out->links.empty()) {
2840                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, compiler.stack_assign(out));
2841         }
2842
2843         out = output("UV");
2844         if(!out->links.empty()) {
2845                 if(from_dupli) {
2846                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
2847                 }
2848                 else {
2849                         int attr = compiler.attribute(ATTR_STD_UV);
2850                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2851                 }
2852         }
2853
2854         out = output("Object");
2855         if(!out->links.empty()) {
2856                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, compiler.stack_assign(out), use_transform);
2857                 if(use_transform) {
2858                         Transform ob_itfm = transform_inverse(ob_tfm);
2859                         compiler.add_node(ob_itfm.x);
2860                         compiler.add_node(ob_itfm.y);
2861                         compiler.add_node(ob_itfm.z);
2862                         compiler.add_node(ob_itfm.w);
2863                 }
2864         }
2865
2866         out = output("Camera");
2867         if(!out->links.empty()) {
2868                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, compiler.stack_assign(out));
2869         }
2870
2871         out = output("Window");
2872         if(!out->links.empty()) {
2873                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, compiler.stack_assign(out));
2874         }
2875
2876         out = output("Reflection");
2877         if(!out->links.empty()) {
2878                 if(compiler.background) {
2879                         compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
2880                 }
2881                 else {
2882                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, compiler.stack_assign(out));
2883                 }
2884         }
2885 }
2886
2887 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2888 {
2889         if(bump == SHADER_BUMP_DX)
2890                 compiler.parameter("bump_offset", "dx");
2891         else if(bump == SHADER_BUMP_DY)
2892                 compiler.parameter("bump_offset", "dy");
2893         else
2894                 compiler.parameter("bump_offset", "center");
2895         
2896         if(compiler.background)
2897                 compiler.parameter("is_background", true);
2898         if(compiler.output_type() == SHADER_TYPE_VOLUME)
2899                 compiler.parameter("is_volume", true);
2900         compiler.parameter(this, "use_transform");
2901         Transform ob_itfm = transform_transpose(transform_inverse(ob_tfm));
2902         compiler.parameter("object_itfm", ob_itfm);
2903
2904         compiler.parameter(this, "from_dupli");
2905
2906         compiler.add(this, "node_texture_coordinate");
2907 }
2908
2909 /* UV Map */
2910
2911 NODE_DEFINE(UVMapNode)
2912 {
2913         NodeType* type = NodeType::add("uvmap", create, NodeType::SHADER);
2914
2915         SOCKET_IN_STRING(attribute, "attribute", ustring(""));
2916         SOCKET_IN_BOOLEAN(from_dupli, "from dupli", false);
2917
2918         SOCKET_OUT_POINT(UV, "UV");
2919
2920         return type;
2921 }
2922
2923 UVMapNode::UVMapNode()
2924 : ShaderNode(node_type)
2925 {
2926 }
2927
2928 void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2929 {
2930         if(shader->has_surface) {
2931                 if(!from_dupli) {
2932                         if(!output("UV")->links.empty()) {
2933                                 if(attribute != "")
2934                                         attributes->add(attribute);
2935                                 else
2936                                         attributes->add(ATTR_STD_UV);
2937                         }
2938                 }
2939         }
2940
2941         ShaderNode::attributes(shader, attributes);
2942 }
2943
2944 void UVMapNode::compile(SVMCompiler& compiler)
2945 {
2946         ShaderOutput *out = output("UV");
2947         ShaderNodeType texco_node = NODE_TEX_COORD;
2948         ShaderNodeType attr_node = NODE_ATTR;
2949         int attr;
2950
2951         if(bump == SHADER_BUMP_DX) {
2952                 texco_node = NODE_TEX_COORD_BUMP_DX;
2953                 attr_node = NODE_ATTR_BUMP_DX;
2954         }
2955         else if(bump == SHADER_BUMP_DY) {
2956                 texco_node = NODE_TEX_COORD_BUMP_DY;
2957                 attr_node = NODE_ATTR_BUMP_DY;
2958         }
2959
2960         if(!out->links.empty()) {
2961                 if(from_dupli) {
2962                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
2963                 }
2964                 else {
2965                         if(attribute != "")
2966                                 attr = compiler.attribute(attribute);
2967                         else
2968                                 attr = compiler.attribute(ATTR_STD_UV);
2969
2970                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2971                 }
2972         }
2973 }
2974
2975 void UVMapNode::compile(OSLCompiler& compiler)
2976 {
2977         if(bump == SHADER_BUMP_DX)
2978                 compiler.parameter("bump_offset", "dx");
2979         else if(bump == SHADER_BUMP_DY)
2980                 compiler.parameter("bump_offset", "dy");
2981         else
2982                 compiler.parameter("bump_offset", "center");
2983
2984         compiler.parameter(this, "from_dupli");
2985         compiler.parameter(this, "attribute");
2986         compiler.add(this, "node_uv_map");
2987 }
2988
2989 /* Light Path */
2990
2991 NODE_DEFINE(LightPathNode)
2992 {
2993         NodeType* type = NodeType::add("light_path", create, NodeType::SHADER);
2994
2995         SOCKET_OUT_FLOAT(is_camera_ray, "Is Camera Ray");
2996         SOCKET_OUT_FLOAT(is_shadow_ray, "Is Shadow Ray");
2997         SOCKET_OUT_FLOAT(is_diffuse_ray, "Is Diffuse Ray");
2998         SOCKET_OUT_FLOAT(is_glossy_ray, "Is Glossy Ray");
2999         SOCKET_OUT_FLOAT(is_singular_ray, "Is Singular Ray");
3000         SOCKET_OUT_FLOAT(is_reflection_ray, "Is Reflection Ray");
3001         SOCKET_OUT_FLOAT(is_transmission_ray, "Is Transmission Ray");
3002         SOCKET_OUT_FLOAT(is_volume_scatter_ray, "Is Volume Scatter Ray");
3003         SOCKET_OUT_FLOAT(ray_length, "Ray Length");
3004         SOCKET_OUT_FLOAT(ray_depth, "Ray Depth");
3005         SOCKET_OUT_FLOAT(transparent_depth, "Transparent Depth");
3006         SOCKET_OUT_FLOAT(transmission_depth, "Transmission Depth");
3007
3008         return type;
3009 }
3010
3011 LightPathNode::LightPathNode()
3012 : ShaderNode(node_type)
3013 {
3014 }
3015
3016 void LightPathNode::compile(SVMCompiler& compiler)
3017 {
3018         ShaderOutput *out;
3019
3020         out = output("Is Camera Ray");
3021         if(!out->links.empty()) {
3022                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, compiler.stack_assign(out));
3023         }
3024
3025         out = output("Is Shadow Ray");
3026         if(!out->links.empty()) {
3027                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, compiler.stack_assign(out));
3028         }
3029
3030         out = output("Is Diffuse Ray");
3031         if(!out->links.empty()) {
3032                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, compiler.stack_assign(out));
3033         }
3034
3035         out = output("Is Glossy Ray");
3036         if(!out->links.empty()) {
3037                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, compiler.stack_assign(out));
3038         }
3039
3040         out = output("Is Singular Ray");
3041         if(!out->links.empty()) {
3042                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, compiler.stack_assign(out));
3043         }
3044
3045         out = output("Is Reflection Ray");
3046         if(!out->links.empty()) {
3047                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, compiler.stack_assign(out));
3048         }
3049
3050
3051         out = output("Is Transmission Ray");
3052         if(!out->links.empty()) {
3053                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, compiler.stack_assign(out));
3054         }
3055         
3056         out = output("Is Volume Scatter Ray");
3057         if(!out->links.empty()) {
3058                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, compiler.stack_assign(out));
3059         }
3060
3061         out = output("Ray Length");
3062         if(!out->links.empty()) {
3063                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, compiler.stack_assign(out));
3064         }
3065         
3066         out = output("Ray Depth");
3067         if(!out->links.empty()) {
3068                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, compiler.stack_assign(out));
3069         }
3070
3071         out = output("Transparent Depth");
3072         if(!out->links.empty()) {
3073                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, compiler.stack_assign(out));
3074         }
3075
3076         out = output("Transmission Depth");
3077         if(!out->links.empty()) {
3078                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transmission, compiler.stack_assign(out));
3079         }
3080 }
3081
3082 void LightPathNode::compile(OSLCompiler& compiler)
3083 {
3084         compiler.add(this, "node_light_path");
3085 }
3086
3087 /* Light Falloff */
3088
3089 NODE_DEFINE(LightFalloffNode)
3090 {
3091         NodeType* type = NodeType::add("light_fallof", create, NodeType::SHADER);
3092
3093         SOCKET_IN_FLOAT(strength, "Strength", 100.0f);
3094         SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
3095
3096         SOCKET_OUT_FLOAT(quadratic, "Quadratic");
3097         SOCKET_OUT_FLOAT(linear, "Linear");
3098         SOCKET_OUT_FLOAT(constant, "Constant");
3099
3100         return type;
3101 }
3102
3103 LightFalloffNode::LightFalloffNode()
3104 : ShaderNode(node_type)
3105 {
3106 }
3107
3108 void LightFalloffNode::compile(SVMCompiler& compiler)
3109 {
3110         ShaderInput *strength_in = input("Strength");
3111         ShaderInput *smooth_in = input("Smooth");
3112
3113         ShaderOutput *out = output("Quadratic");
3114         if(!out->links.empty()) {
3115                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
3116                         compiler.encode_uchar4(
3117                                 compiler.stack_assign(strength_in),
3118                                 compiler.stack_assign(smooth_in),
3119                                 compiler.stack_assign(out)));
3120         }
3121
3122         out = output("Linear");
3123         if(!out->links.empty()) {
3124                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
3125                         compiler.encode_uchar4(
3126                                 compiler.stack_assign(strength_in),
3127                                 compiler.stack_assign(smooth_in),
3128                                 compiler.stack_assign(out)));
3129         }
3130
3131         out = output("Constant");
3132         if(!out->links.empty()) {
3133                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
3134                         compiler.encode_uchar4(
3135                                 compiler.stack_assign(strength_in),
3136                                 compiler.stack_assign(smooth_in),
3137                                 compiler.stack_assign(out)));
3138         }
3139 }
3140
3141 void LightFalloffNode::compile(OSLCompiler& compiler)
3142 {
3143         compiler.add(this, "node_light_falloff");
3144 }
3145
3146 /* Object Info */
3147
3148 NODE_DEFINE(ObjectInfoNode)
3149 {
3150         NodeType* type = NodeType::add("object_info", create, NodeType::SHADER);
3151
3152         SOCKET_OUT_VECTOR(location, "Location");
3153         SOCKET_OUT_FLOAT(object_index, "Object Index");
3154         SOCKET_OUT_FLOAT(material_index, "Material Index");
3155         SOCKET_OUT_FLOAT(random, "Random");
3156
3157         return type;
3158 }
3159
3160 ObjectInfoNode::ObjectInfoNode()
3161 : ShaderNode(node_type)
3162 {
3163 }
3164
3165 void ObjectInfoNode::compile(SVMCompiler& compiler)
3166 {
3167         ShaderOutput *out = output("Location");
3168         if(!out->links.empty()) {
3169                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, compiler.stack_assign(out));
3170         }
3171
3172         out = output("Object Index");
3173         if(!out->links.empty()) {
3174                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, compiler.stack_assign(out));
3175         }
3176
3177         out = output("Material Index");
3178         if(!out->links.empty()) {
3179                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, compiler.stack_assign(out));
3180         }
3181
3182         out = output("Random");
3183         if(!out->links.empty()) {
3184                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, compiler.stack_assign(out));
3185         }
3186 }
3187
3188 void ObjectInfoNode::compile(OSLCompiler& compiler)
3189 {
3190         compiler.add(this, "node_object_info");
3191 }
3192
3193 /* Particle Info */
3194
3195 NODE_DEFINE(ParticleInfoNode)
3196 {
3197         NodeType* type = NodeType::add("particle_info", create, NodeType::SHADER);
3198
3199         SOCKET_OUT_FLOAT(index, "Index");
3200         SOCKET_OUT_FLOAT(age, "Age");
3201         SOCKET_OUT_FLOAT(lifetime, "Lifetime");
3202         SOCKET_OUT_POINT(location, "Location");
3203 #if 0   /* not yet supported */
3204         SOCKET_OUT_QUATERNION(rotation, "Rotation");
3205 #endif
3206         SOCKET_OUT_FLOAT(size, "Size");
3207         SOCKET_OUT_VECTOR(velocity, "Velocity");
3208         SOCKET_OUT_VECTOR(angular_velocity, "Angular Velocity");
3209
3210         return type;
3211 }
3212
3213 ParticleInfoNode::ParticleInfoNode()
3214 : ShaderNode(node_type)
3215 {
3216 }
3217
3218 void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3219 {
3220         if(!output("Index")->links.empty())
3221                 attributes->add(ATTR_STD_PARTICLE);
3222         if(!output("Age")->links.empty())
3223                 attributes->add(ATTR_STD_PARTICLE);
3224         if(!output("Lifetime")->links.empty())
3225                 attributes->add(ATTR_STD_PARTICLE);
3226         if(!output("Location")->links.empty())
3227                 attributes->add(ATTR_STD_PARTICLE);
3228 #if 0   /* not yet supported */
3229         if(!output("Rotation")->links.empty())
3230                 attributes->add(ATTR_STD_PARTICLE);
3231 #endif
3232         if(!output("Size")->links.empty())
3233                 attributes->add(ATTR_STD_PARTICLE);
3234         if(!output("Velocity")->links.empty())
3235                 attributes->add(ATTR_STD_PARTICLE);
3236         if(!output("Angular Velocity")->links.empty())
3237                 attributes->add(ATTR_STD_PARTICLE);
3238
3239         ShaderNode::attributes(shader, attributes);
3240 }
3241
3242 void ParticleInfoNode::compile(SVMCompiler& compiler)
3243 {
3244         ShaderOutput *out;
3245         
3246         out = output("Index");
3247         if(!out->links.empty()) {
3248                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, compiler.stack_assign(out));
3249         }
3250         
3251         out = output("Age");
3252         if(!out->links.empty()) {
3253                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, compiler.stack_assign(out));
3254         }
3255         
3256         out = output("Lifetime");
3257         if(!out->links.empty()) {
3258                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, compiler.stack_assign(out));
3259         }
3260         
3261         out = output("Location");
3262         if(!out->links.empty()) {
3263                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, compiler.stack_assign(out));
3264         }
3265         
3266         /* quaternion data is not yet supported by Cycles */
3267 #if 0
3268         out = output("Rotation");
3269         if(!out->links.empty()) {
3270                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, compiler.stack_assign(out));
3271         }
3272 #endif
3273         
3274         out = output("Size");
3275         if(!out->links.empty()) {
3276                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, compiler.stack_assign(out));
3277         }
3278         
3279         out = output("Velocity");
3280         if(!out->links.empty()) {
3281                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, compiler.stack_assign(out));
3282         }
3283         
3284         out = output("Angular Velocity");
3285         if(!out->links.empty()) {
3286                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, compiler.stack_assign(out));
3287         }
3288 }
3289
3290 void ParticleInfoNode::compile(OSLCompiler& compiler)
3291 {
3292         compiler.add(this, "node_particle_info");
3293 }
3294
3295 /* Hair Info */
3296
3297 NODE_DEFINE(HairInfoNode)
3298 {
3299         NodeType* type = NodeType::add("hair_info", create, NodeType::SHADER);
3300
3301         SOCKET_OUT_FLOAT(is_strand, "Is Strand");
3302         SOCKET_OUT_FLOAT(intercept, "Intercept");
3303         SOCKET_OUT_FLOAT(thickness, "Thickness");
3304         SOCKET_OUT_NORMAL(tangent Normal, "Tangent Normal");
3305 #if 0 /*output for minimum hair width transparency - deactivated */
3306         SOCKET_OUT_FLOAT(fade, "Fade");
3307 #endif
3308
3309         return type;
3310 }
3311
3312 HairInfoNode::HairInfoNode()
3313 : ShaderNode(node_type)
3314 {
3315 }
3316
3317 void HairInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3318 {
3319         if(shader->has_surface) {
3320                 ShaderOutput *intercept_out = output("Intercept");
3321
3322                 if(!intercept_out->links.empty())
3323                         attributes->add(ATTR_STD_CURVE_INTERCEPT);
3324         }
3325
3326         ShaderNode::attributes(shader, attributes);
3327 }
3328
3329 void HairInfoNode::compile(SVMCompiler& compiler)
3330 {
3331         ShaderOutput *out;
3332         
3333         out = output("Is Strand");
3334         if(!out->links.empty()) {
3335                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, compiler.stack_assign(out));
3336         }
3337
3338         out = output("Intercept");
3339         if(!out->links.empty()) {
3340                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
3341                 compiler.add_node(NODE_ATTR, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT);
3342         }
3343
3344         out = output("Thickness");
3345         if(!out->links.empty()) {
3346                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, compiler.stack_assign(out));
3347         }
3348
3349         out = output("Tangent Normal");
3350         if(!out->links.empty()) {
3351                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, compiler.stack_assign(out));
3352         }
3353
3354         /*out = output("Fade");
3355         if(!out->links.empty()) {
3356                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, compiler.stack_assign(out));
3357         }*/
3358
3359 }
3360
3361 void HairInfoNode::compile(OSLCompiler& compiler)
3362 {
3363         compiler.add(this, "node_hair_info");
3364 }
3365
3366 /* Value */
3367
3368 NODE_DEFINE(ValueNode)
3369 {
3370         NodeType* type = NodeType::add("value", create, NodeType::SHADER);
3371
3372         SOCKET_FLOAT(value, "Value", 0.0f);
3373         SOCKET_OUT_FLOAT(value, "Value");
3374
3375         return type;
3376 }
3377
3378 ValueNode::ValueNode()
3379 : ShaderNode(node_type)
3380 {
3381 }
3382
3383 bool ValueNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
3384 {
3385         optimized->set(value);
3386         return true;
3387 }
3388
3389 void ValueNode::compile(SVMCompiler& compiler)
3390 {
3391         ShaderOutput *val_out = output("Value");
3392
3393         compiler.add_node(NODE_VALUE_F, __float_as_int(value), compiler.stack_assign(val_out));
3394 }
3395
3396 void ValueNode::compile(OSLCompiler& compiler)
3397 {
3398         compiler.parameter("value_value", value);
3399         compiler.add(this, "node_value");
3400 }
3401
3402 /* Color */
3403
3404 NODE_DEFINE(ColorNode)
3405 {
3406         NodeType* type = NodeType::add("color", create, NodeType::SHADER);
3407
3408         SOCKET_COLOR(value, "Value", make_float3(0.0f, 0.0f, 0.0f));
3409         SOCKET_OUT_COLOR(color, "Color");
3410
3411         return type;
3412 }
3413
3414 ColorNode::ColorNode()
3415 : ShaderNode(node_type)
3416 {
3417 }
3418
3419 bool ColorNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
3420 {
3421         optimized->set(value);
3422         return true;
3423 }
3424
3425 void ColorNode::compile(SVMCompiler& compiler)
3426 {
3427         ShaderOutput *color_out = output("Color");
3428
3429         if(!color_out->links.empty()) {
3430                 compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
3431                 compiler.add_node(NODE_VALUE_V, value);
3432         }
3433 }
3434
3435 void ColorNode::compile(OSLCompiler& compiler)
3436 {
3437         compiler.parameter_color("color_value", value);
3438
3439         compiler.add(this, "node_value");
3440 }
3441
3442 /* Add Closure */
3443
3444 NODE_DEFINE(AddClosureNode)
3445 {
3446         NodeType* type = NodeType::add("add_closure", create, NodeType::SHADER);
3447
3448         SOCKET_IN_CLOSURE(closure1, "Closure1");
3449         SOCKET_IN_CLOSURE(closure2, "Closure2");
3450         SOCKET_OUT_CLOSURE(closure, "Closure");
3451
3452         return type;
3453 }
3454
3455 AddClosureNode::AddClosureNode()
3456 : ShaderNode(node_type)
3457 {
3458         special_type = SHADER_SPECIAL_TYPE_COMBINE_CLOSURE;
3459 }
3460
3461 void AddClosureNode::compile(SVMCompiler& /*compiler*/)
3462 {
3463         /* handled in the SVM compiler */
3464 }
3465
3466 void AddClosureNode::compile(OSLCompiler& compiler)
3467 {
3468         compiler.add(this, "node_add_closure");
3469 }
3470
3471 /* Mix Closure */
3472
3473 NODE_DEFINE(MixClosureNode)
3474 {
3475         NodeType* type = NodeType::add("mix_closure", create, NodeType::SHADER);
3476
3477         SOCKET_IN_FLOAT(fac, "Fac", 0.5f);
3478         SOCKET_IN_CLOSURE(closure1, "Closure1");
3479         SOCKET_IN_CLOSURE(closure2, "Closure2");
3480
3481         SOCKET_OUT_CLOSURE(closure, "Closure");
3482
3483         return type;
3484 }
3485
3486 MixClosureNode::MixClosureNode()
3487 : ShaderNode(node_type)
3488 {
3489         special_type = SHADER_SPECIAL_TYPE_COMBINE_CLOSURE;
3490 }
3491
3492 void MixClosureNode::compile(SVMCompiler& /*compiler*/)
3493 {
3494         /* handled in the SVM compiler */
3495 }
3496
3497 void MixClosureNode::compile(OSLCompiler& compiler)
3498 {
3499         compiler.add(this, "node_mix_closure");
3500 }
3501
3502 bool MixClosureNode::constant_fold(ShaderGraph *graph, ShaderOutput *, ShaderInput *)
3503 {
3504         ShaderInput *fac_in = input("Fac");
3505         ShaderInput *closure1_in = input("Closure1");
3506         ShaderInput *closure2_in = input("Closure2");
3507         ShaderOutput *closure_out = output("Closure");
3508
3509         /* remove useless mix closures nodes */
3510         if(closure1_in->link == closure2_in->link) {
3511                 graph->relink(this, closure_out, closure1_in->link);
3512                 return true;
3513         }
3514
3515         /* remove unused mix closure input when factor is 0.0 or 1.0 */
3516         /* check for closure links and make sure factor link is disconnected */
3517         if(closure1_in->link && closure2_in->link && !fac_in->link) {
3518                 /* factor 0.0 */
3519                 if(fac == 0.0f) {
3520                         graph->relink(this, closure_out, closure1_in->link);
3521                         return true;
3522                 }
3523                 /* factor 1.0 */
3524                 else if(fac == 1.0f) {
3525                         graph->relink(this, closure_out, closure2_in->link);
3526                         return true;
3527                 }
3528         }
3529
3530         return false;
3531 }
3532
3533 /* Mix Closure */
3534
3535 NODE_DEFINE(MixClosureWeightNode)
3536 {
3537         NodeType* type = NodeType::add("mix_closure_weight", create, NodeType::SHADER);
3538
3539         SOCKET_IN_FLOAT(weight, "Weight", 1.0f);
3540         SOCKET_IN_FLOAT(fac, "Fac", 1.0f);
3541
3542         SOCKET_OUT_FLOAT(weight1, "Weight1");
3543         SOCKET_OUT_FLOAT(weight2, "Weight2");
3544
3545         return type;
3546 }
3547
3548 MixClosureWeightNode::MixClosureWeightNode()
3549 : ShaderNode(node_type)
3550 {
3551 }
3552
3553 void MixClosureWeightNode::compile(SVMCompiler& compiler)
3554 {
3555         ShaderInput *weight_in = input("Weight");
3556         ShaderInput *fac_in = input("Fac");
3557         ShaderOutput *weight1_out = output("Weight1");
3558         ShaderOutput *weight2_out = output("Weight2");
3559
3560         compiler.add_node(NODE_MIX_CLOSURE,
3561                 compiler.encode_uchar4(
3562                         compiler.stack_assign(fac_in),
3563                         compiler.stack_assign(weight_in),
3564                         compiler.stack_assign(weight1_out),
3565                         compiler.stack_assign(weight2_out)));
3566 }
3567
3568 void MixClosureWeightNode::compile(OSLCompiler& /*compiler*/)
3569 {
3570         assert(0);
3571 }
3572
3573 /* Invert */
3574
3575 NODE_DEFINE(InvertNode)
3576 {
3577         NodeType* type = NodeType::add("invert", create, NodeType::SHADER);
3578
3579         SOCKET_IN_FLOAT(fac, "Fac", 1.0f);
3580         SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
3581
3582         SOCKET_OUT_COLOR(color, "Color");
3583
3584         return type;
3585 }
3586
3587 InvertNode::InvertNode()
3588 : ShaderNode(node_type)
3589 {
3590 }
3591
3592 bool InvertNode::constant_fold(ShaderGraph *graph, ShaderOutput *, ShaderInput *optimized)
3593 {
3594         ShaderInput *fac_in = input("Fac");
3595         ShaderInput *color_in = input("Color");
3596         ShaderOutput *color_out = output("Color");
3597
3598         if(!fac_in->link) {
3599                 /* evaluate fully constant node */
3600                 if(!color_in->link) {
3601                         optimized->set(interp(color, make_float3(1.0f, 1.0f, 1.0f) - color, fac));
3602                         return true;
3603                 }
3604                 /* remove no-op node */
3605                 else if(fac == 0.0f) {
3606                         graph->relink(this, color_out, color_in->link);
3607                         return true;
3608                 }
3609         }
3610
3611         return false;
3612 }
3613
3614 void InvertNode::compile(SVMCompiler& compiler)
3615 {