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