Cycles: Query XYZ to/from Scene Linear conversion from OCIO instead of assuming sRGB
[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::compile(SVMCompiler& compiler)
1493 {
1494         ShaderInput *vector_in = input("Vector");
1495         ShaderOutput *density_out = output("Density");
1496         ShaderOutput *color_out = output("Color");
1497
1498         const bool use_density = !density_out->links.empty();
1499         const bool use_color = !color_out->links.empty();
1500
1501         image_manager = compiler.image_manager;
1502
1503         if(use_density || use_color) {
1504                 if(slot == -1) {
1505                         ImageMetaData metadata;
1506                         slot = image_manager->add_image(filename.string(), builtin_data,
1507                                                         false, 0,
1508                                                         interpolation,
1509                                                         EXTENSION_CLIP,
1510                                                         true,
1511                                                         metadata);
1512                 }
1513
1514                 if(slot != -1) {
1515                         compiler.stack_assign(vector_in);
1516                         compiler.add_node(NODE_TEX_VOXEL,
1517                                           slot,
1518                                           compiler.encode_uchar4(compiler.stack_assign(vector_in),
1519                                                                  compiler.stack_assign_if_linked(density_out),
1520                                                                  compiler.stack_assign_if_linked(color_out),
1521                                                                  space));
1522                         if(space == NODE_TEX_VOXEL_SPACE_WORLD) {
1523                                 compiler.add_node(tfm.x);
1524                                 compiler.add_node(tfm.y);
1525                                 compiler.add_node(tfm.z);
1526                         }
1527                 }
1528                 else {
1529                         if(use_density) {
1530                                 compiler.add_node(NODE_VALUE_F,
1531                                                   __float_as_int(0.0f),
1532                                                   compiler.stack_assign(density_out));
1533                         }
1534                         if(use_color) {
1535                                 compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
1536                                 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
1537                                                                             TEX_IMAGE_MISSING_G,
1538                                                                             TEX_IMAGE_MISSING_B));
1539                         }
1540                 }
1541         }
1542 }
1543
1544 void PointDensityTextureNode::compile(OSLCompiler& compiler)
1545 {
1546         ShaderOutput *density_out = output("Density");
1547         ShaderOutput *color_out = output("Color");
1548
1549         const bool use_density = !density_out->links.empty();
1550         const bool use_color = !color_out->links.empty();
1551
1552         image_manager = compiler.image_manager;
1553
1554         if(use_density || use_color) {
1555                 if(slot == -1) {
1556                         ImageMetaData metadata;
1557                         slot = image_manager->add_image(filename.string(), builtin_data,
1558                                                         false, 0,
1559                                                         interpolation,
1560                                                         EXTENSION_CLIP,
1561                                                         true,
1562                                                         metadata);
1563                 }
1564
1565                 if(slot != -1) {
1566                         compiler.parameter("filename", string_printf("@i%d", slot).c_str());
1567                 }
1568                 if(space == NODE_TEX_VOXEL_SPACE_WORLD) {
1569                         compiler.parameter("mapping", tfm);
1570                         compiler.parameter("use_mapping", 1);
1571                 }
1572                 compiler.parameter(this, "interpolation");
1573                 compiler.add(this, "node_voxel_texture");
1574         }
1575 }
1576
1577 /* Normal */
1578
1579 NODE_DEFINE(NormalNode)
1580 {
1581         NodeType* type = NodeType::add("normal", create, NodeType::SHADER);
1582
1583         SOCKET_VECTOR(direction, "direction", make_float3(0.0f, 0.0f, 0.0f));
1584
1585         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f));
1586
1587         SOCKET_OUT_NORMAL(normal, "Normal");
1588         SOCKET_OUT_FLOAT(dot, "Dot");
1589
1590         return type;
1591 }
1592
1593 NormalNode::NormalNode()
1594 : ShaderNode(node_type)
1595 {
1596 }
1597
1598 void NormalNode::compile(SVMCompiler& compiler)
1599 {
1600         ShaderInput *normal_in = input("Normal");
1601         ShaderOutput *normal_out = output("Normal");
1602         ShaderOutput *dot_out = output("Dot");
1603
1604         compiler.add_node(NODE_NORMAL,
1605                 compiler.stack_assign(normal_in),
1606                 compiler.stack_assign(normal_out),
1607                 compiler.stack_assign(dot_out));
1608         compiler.add_node(
1609                 __float_as_int(direction.x),
1610                 __float_as_int(direction.y),
1611                 __float_as_int(direction.z));
1612 }
1613
1614 void NormalNode::compile(OSLCompiler& compiler)
1615 {
1616         compiler.parameter(this, "direction");
1617         compiler.add(this, "node_normal");
1618 }
1619
1620 /* Mapping */
1621
1622 NODE_DEFINE(MappingNode)
1623 {
1624         NodeType* type = NodeType::add("mapping", create, NodeType::SHADER);
1625
1626         TEXTURE_MAPPING_DEFINE(MappingNode);
1627
1628         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
1629         SOCKET_OUT_POINT(vector, "Vector");
1630
1631         return type;
1632 }
1633
1634 MappingNode::MappingNode()
1635 : ShaderNode(node_type)
1636 {
1637 }
1638
1639 void MappingNode::compile(SVMCompiler& compiler)
1640 {
1641         ShaderInput *vector_in = input("Vector");
1642         ShaderOutput *vector_out = output("Vector");
1643
1644         tex_mapping.compile(compiler, compiler.stack_assign(vector_in), compiler.stack_assign(vector_out));
1645 }
1646
1647 void MappingNode::compile(OSLCompiler& compiler)
1648 {
1649         compiler.parameter("Matrix", tex_mapping.compute_transform());
1650         compiler.parameter_point("mapping_min", tex_mapping.min);
1651         compiler.parameter_point("mapping_max", tex_mapping.max);
1652         compiler.parameter("use_minmax", tex_mapping.use_minmax);
1653
1654         compiler.add(this, "node_mapping");
1655 }
1656
1657 /* RGBToBW */
1658
1659 NODE_DEFINE(RGBToBWNode)
1660 {
1661         NodeType* type = NodeType::add("rgb_to_bw", create, NodeType::SHADER);
1662
1663         SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
1664         SOCKET_OUT_FLOAT(val, "Val");
1665
1666         return type;
1667 }
1668
1669 RGBToBWNode::RGBToBWNode()
1670 : ShaderNode(node_type)
1671 {
1672 }
1673
1674 void RGBToBWNode::constant_fold(const ConstantFolder& folder)
1675 {
1676         if(folder.all_inputs_constant()) {
1677                 float val = folder.scene->shader_manager->linear_rgb_to_gray(color);
1678                 folder.make_constant(val);
1679         }
1680 }
1681
1682 void RGBToBWNode::compile(SVMCompiler& compiler)
1683 {
1684         compiler.add_node(NODE_CONVERT,
1685                          NODE_CONVERT_CF,
1686                          compiler.stack_assign(inputs[0]),
1687                          compiler.stack_assign(outputs[0]));
1688 }
1689
1690 void RGBToBWNode::compile(OSLCompiler& compiler)
1691 {
1692         compiler.add(this, "node_rgb_to_bw");
1693 }
1694
1695 /* Convert */
1696
1697 const NodeType* ConvertNode::node_types[ConvertNode::MAX_TYPE][ConvertNode::MAX_TYPE];
1698 bool ConvertNode::initialized = ConvertNode::register_types();
1699
1700 Node* ConvertNode::create(const NodeType *type)
1701 {
1702         return new ConvertNode(type->inputs[0].type,  type->outputs[0].type);
1703 }
1704
1705 bool ConvertNode::register_types()
1706 {
1707         const int num_types = 8;
1708         SocketType::Type types[num_types] = {SocketType::FLOAT,
1709                                              SocketType::INT,
1710                                              SocketType::COLOR,
1711                                              SocketType::VECTOR,
1712                                              SocketType::POINT,
1713                                              SocketType::NORMAL,
1714                                              SocketType::STRING,
1715                                                                                  SocketType::CLOSURE};
1716
1717         for(size_t i = 0; i < num_types; i++) {
1718                 SocketType::Type from = types[i];
1719                 ustring from_name(SocketType::type_name(from));
1720                 ustring from_value_name("value_" + from_name.string());
1721
1722                 for(size_t j = 0; j < num_types; j++) {
1723                         SocketType::Type to = types[j];
1724                         ustring to_name(SocketType::type_name(to));
1725                         ustring to_value_name("value_" + to_name.string());
1726
1727                         string node_name = "convert_" + from_name.string() + "_to_" + to_name.string();
1728                         NodeType* type = NodeType::add(node_name.c_str(), create, NodeType::SHADER);
1729
1730                         type->register_input(from_value_name, from_value_name, from,
1731                                 SOCKET_OFFSETOF(ConvertNode, value_float), SocketType::zero_default_value(),
1732                                 NULL, NULL, SocketType::LINKABLE);
1733                         type->register_output(to_value_name, to_value_name, to);
1734
1735                         assert(from < MAX_TYPE);
1736                         assert(to < MAX_TYPE);
1737
1738                         node_types[from][to] = type;
1739                 }
1740         }
1741
1742         return true;
1743 }
1744
1745 ConvertNode::ConvertNode(SocketType::Type from_, SocketType::Type to_, bool autoconvert)
1746 : ShaderNode(node_types[from_][to_])
1747 {
1748         from = from_;
1749         to = to_;
1750
1751         if(from == to)
1752                 special_type = SHADER_SPECIAL_TYPE_PROXY;
1753         else if(autoconvert)
1754                 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1755 }
1756
1757 void ConvertNode::constant_fold(const ConstantFolder& folder)
1758 {
1759         /* proxy nodes should have been removed at this point */
1760         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1761
1762         /* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
1763
1764         if(folder.all_inputs_constant()) {
1765                 if(from == SocketType::FLOAT) {
1766                         if(SocketType::is_float3(to)) {
1767                                 folder.make_constant(make_float3(value_float, value_float, value_float));
1768                         }
1769                 }
1770                 else if(SocketType::is_float3(from)) {
1771                         if(to == SocketType::FLOAT) {
1772                                 if(from == SocketType::COLOR) {
1773                                         /* color to float */
1774                                         float val = folder.scene->shader_manager->linear_rgb_to_gray(value_color);
1775                                         folder.make_constant(val);
1776                                 }
1777                                 else {
1778                                         /* vector/point/normal to float */
1779                                         folder.make_constant(average(value_vector));
1780                                 }
1781                         }
1782                         else if(SocketType::is_float3(to)) {
1783                                 folder.make_constant(value_color);
1784                         }
1785                 }
1786         }
1787         else {
1788                 ShaderInput *in = inputs[0];
1789                 ShaderNode *prev = in->link->parent;
1790
1791                 /* no-op conversion of A to B to A */
1792                 if(prev->type == node_types[to][from]) {
1793                         ShaderInput *prev_in = prev->inputs[0];
1794
1795                         if(SocketType::is_float3(from) && (to == SocketType::FLOAT || SocketType::is_float3(to)) && prev_in->link) {
1796                                 folder.bypass(prev_in->link);
1797                         }
1798                 }
1799         }
1800 }
1801
1802 void ConvertNode::compile(SVMCompiler& compiler)
1803 {
1804         /* proxy nodes should have been removed at this point */
1805         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1806
1807         ShaderInput *in = inputs[0];
1808         ShaderOutput *out = outputs[0];
1809
1810         if(from == SocketType::FLOAT) {
1811                 if(to == SocketType::INT)
1812                         /* float to int */
1813                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, compiler.stack_assign(in), compiler.stack_assign(out));
1814                 else
1815                         /* float to float3 */
1816                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, compiler.stack_assign(in), compiler.stack_assign(out));
1817         }
1818         else if(from == SocketType::INT) {
1819                 if(to == SocketType::FLOAT)
1820                         /* int to float */
1821                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, compiler.stack_assign(in), compiler.stack_assign(out));
1822                 else
1823                         /* int to vector/point/normal */
1824                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, compiler.stack_assign(in), compiler.stack_assign(out));
1825         }
1826         else if(to == SocketType::FLOAT) {
1827                 if(from == SocketType::COLOR)
1828                         /* color to float */
1829                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, compiler.stack_assign(in), compiler.stack_assign(out));
1830                 else
1831                         /* vector/point/normal to float */
1832                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, compiler.stack_assign(in), compiler.stack_assign(out));
1833         }
1834         else if(to == SocketType::INT) {
1835                 if(from == SocketType::COLOR)
1836                         /* color to int */
1837                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, compiler.stack_assign(in), compiler.stack_assign(out));
1838                 else
1839                         /* vector/point/normal to int */
1840                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, compiler.stack_assign(in), compiler.stack_assign(out));
1841         }
1842         else {
1843                 /* float3 to float3 */
1844                 if(in->link) {
1845                         /* no op in SVM */
1846                         compiler.stack_link(in, out);
1847                 }
1848                 else {
1849                         /* set 0,0,0 value */
1850                         compiler.add_node(NODE_VALUE_V, compiler.stack_assign(out));
1851                         compiler.add_node(NODE_VALUE_V, value_color);
1852                 }
1853         }
1854 }
1855
1856 void ConvertNode::compile(OSLCompiler& compiler)
1857 {
1858         /* proxy nodes should have been removed at this point */
1859         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1860
1861         if(from == SocketType::FLOAT)
1862                 compiler.add(this, "node_convert_from_float");
1863         else if(from == SocketType::INT)
1864                 compiler.add(this, "node_convert_from_int");
1865         else if(from == SocketType::COLOR)
1866                 compiler.add(this, "node_convert_from_color");
1867         else if(from == SocketType::VECTOR)
1868                 compiler.add(this, "node_convert_from_vector");
1869         else if(from == SocketType::POINT)
1870                 compiler.add(this, "node_convert_from_point");
1871         else if(from == SocketType::NORMAL)
1872                 compiler.add(this, "node_convert_from_normal");
1873         else
1874                 assert(0);
1875 }
1876
1877 /* Base type for all closure-type nodes */
1878
1879 BsdfBaseNode::BsdfBaseNode(const NodeType *node_type)
1880         : ShaderNode(node_type)
1881 {
1882         special_type = SHADER_SPECIAL_TYPE_CLOSURE;
1883 }
1884
1885 bool BsdfBaseNode::has_bump()
1886 {
1887         /* detect if anything is plugged into the normal input besides the default */
1888         ShaderInput *normal_in = input("Normal");
1889         return (normal_in && normal_in->link &&
1890                 normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
1891 }
1892
1893 /* BSDF Closure */
1894
1895 BsdfNode::BsdfNode(const NodeType *node_type)
1896 : BsdfBaseNode(node_type)
1897 {
1898 }
1899
1900 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3, ShaderInput *param4)
1901 {
1902         ShaderInput *color_in = input("Color");
1903         ShaderInput *normal_in = input("Normal");
1904         ShaderInput *tangent_in = input("Tangent");
1905
1906         if(color_in->link)
1907                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
1908         else
1909                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
1910
1911         int normal_offset = (normal_in) ? compiler.stack_assign_if_linked(normal_in) : SVM_STACK_INVALID;
1912         int tangent_offset = (tangent_in) ? compiler.stack_assign_if_linked(tangent_in) : SVM_STACK_INVALID;
1913         int param3_offset = (param3) ? compiler.stack_assign(param3) : SVM_STACK_INVALID;
1914         int param4_offset = (param4) ? compiler.stack_assign(param4) : SVM_STACK_INVALID;
1915
1916         compiler.add_node(NODE_CLOSURE_BSDF,
1917                 compiler.encode_uchar4(closure,
1918                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
1919                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
1920                         compiler.closure_mix_weight_offset()),
1921                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
1922                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
1923
1924         compiler.add_node(normal_offset, tangent_offset, param3_offset, param4_offset);
1925 }
1926
1927 void BsdfNode::compile(SVMCompiler& compiler)
1928 {
1929         compile(compiler, NULL, NULL);
1930 }
1931
1932 void BsdfNode::compile(OSLCompiler& /*compiler*/)
1933 {
1934         assert(0);
1935 }
1936
1937 /* Anisotropic BSDF Closure */
1938
1939 NODE_DEFINE(AnisotropicBsdfNode)
1940 {
1941         NodeType* type = NodeType::add("anisotropic_bsdf", create, NodeType::SHADER);
1942
1943         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
1944         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1945         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
1946
1947         static NodeEnum distribution_enum;
1948         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID);
1949         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1950         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_ANISO_ID);
1951         distribution_enum.insert("ashikhmin_shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID);
1952         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1953
1954         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
1955
1956         SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
1957         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.5f);
1958         SOCKET_IN_FLOAT(rotation, "Rotation", 0.0f);
1959
1960         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
1961
1962         return type;
1963 }
1964
1965 AnisotropicBsdfNode::AnisotropicBsdfNode()
1966 : BsdfNode(node_type)
1967 {
1968         closure = CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID;
1969 }
1970
1971 void AnisotropicBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
1972 {
1973         if(shader->has_surface) {
1974                 ShaderInput *tangent_in = input("Tangent");
1975
1976                 if(!tangent_in->link)
1977                         attributes->add(ATTR_STD_GENERATED);
1978         }
1979
1980         ShaderNode::attributes(shader, attributes);
1981 }
1982
1983 void AnisotropicBsdfNode::compile(SVMCompiler& compiler)
1984 {
1985         closure = distribution;
1986
1987         if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ANISO_ID)
1988                 BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"), input("Color"));
1989         else
1990                 BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1991 }
1992
1993 void AnisotropicBsdfNode::compile(OSLCompiler& compiler)
1994 {
1995         compiler.parameter(this, "distribution");
1996         compiler.add(this, "node_anisotropic_bsdf");
1997 }
1998
1999 /* Glossy BSDF Closure */
2000
2001 NODE_DEFINE(GlossyBsdfNode)
2002 {
2003         NodeType* type = NodeType::add("glossy_bsdf", create, NodeType::SHADER);
2004
2005         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2006         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2007         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2008
2009         static NodeEnum distribution_enum;
2010         distribution_enum.insert("sharp", CLOSURE_BSDF_REFLECTION_ID);
2011         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
2012         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
2013         distribution_enum.insert("ashikhmin_shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
2014         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID);
2015         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ID);
2016         SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
2017
2018         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2019
2020         return type;
2021 }
2022
2023 GlossyBsdfNode::GlossyBsdfNode()
2024 : BsdfNode(node_type)
2025 {
2026         closure = CLOSURE_BSDF_MICROFACET_GGX_ID;
2027         distribution_orig = NBUILTIN_CLOSURES;
2028 }
2029
2030 void GlossyBsdfNode::simplify_settings(Scene *scene)
2031 {
2032         if(distribution_orig == NBUILTIN_CLOSURES) {
2033                 roughness_orig = roughness;
2034                 distribution_orig = distribution;
2035         }
2036         else {
2037                 /* By default we use original values, so we don't worry about restoring
2038                  * defaults later one and can only do override when needed.
2039                  */
2040                 roughness = roughness_orig;
2041                 distribution = distribution_orig;
2042         }
2043         Integrator *integrator = scene->integrator;
2044         ShaderInput *roughness_input = input("Roughness");
2045         if(integrator->filter_glossy == 0.0f) {
2046                 /* Fallback to Sharp closure for Roughness close to 0.
2047                  * Note: Keep the epsilon in sync with kernel!
2048                  */
2049                 if(!roughness_input->link && roughness <= 1e-4f) {
2050                         VLOG(1) << "Using sharp glossy BSDF.";
2051                         distribution = CLOSURE_BSDF_REFLECTION_ID;
2052                 }
2053         }
2054         else {
2055                 /* If filter glossy is used we replace Sharp glossy with GGX so we can
2056                  * benefit from closure blur to remove unwanted noise.
2057                  */
2058                 if(roughness_input->link == NULL &&
2059                    distribution == CLOSURE_BSDF_REFLECTION_ID)
2060                 {
2061                         VLOG(1) << "Using GGX glossy with filter glossy.";
2062                         distribution = CLOSURE_BSDF_MICROFACET_GGX_ID;
2063                         roughness = 0.0f;
2064                 }
2065         }
2066         closure = distribution;
2067 }
2068
2069 bool GlossyBsdfNode::has_integrator_dependency()
2070 {
2071         ShaderInput *roughness_input = input("Roughness");
2072         return !roughness_input->link &&
2073                (distribution == CLOSURE_BSDF_REFLECTION_ID || roughness <= 1e-4f);
2074 }
2075
2076 void GlossyBsdfNode::compile(SVMCompiler& compiler)
2077 {
2078         closure = distribution;
2079
2080         if(closure == CLOSURE_BSDF_REFLECTION_ID)
2081                 BsdfNode::compile(compiler, NULL, NULL);
2082         else if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID)
2083                 BsdfNode::compile(compiler, input("Roughness"), NULL, input("Color"));
2084         else
2085                 BsdfNode::compile(compiler, input("Roughness"), NULL);
2086 }
2087
2088 void GlossyBsdfNode::compile(OSLCompiler& compiler)
2089 {
2090         compiler.parameter(this, "distribution");
2091         compiler.add(this, "node_glossy_bsdf");
2092 }
2093
2094 /* Glass BSDF Closure */
2095
2096 NODE_DEFINE(GlassBsdfNode)
2097 {
2098         NodeType* type = NodeType::add("glass_bsdf", create, NodeType::SHADER);
2099
2100         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2101         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2102         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2103
2104         static NodeEnum distribution_enum;
2105         distribution_enum.insert("sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
2106         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
2107         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
2108         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
2109         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
2110         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2111         SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
2112
2113         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2114
2115         return type;
2116 }
2117
2118 GlassBsdfNode::GlassBsdfNode()
2119 : BsdfNode(node_type)
2120 {
2121         closure = CLOSURE_BSDF_SHARP_GLASS_ID;
2122         distribution_orig = NBUILTIN_CLOSURES;
2123 }
2124
2125 void GlassBsdfNode::simplify_settings(Scene *scene)
2126 {
2127         if(distribution_orig == NBUILTIN_CLOSURES) {
2128                 roughness_orig = roughness;
2129                 distribution_orig = distribution;
2130         }
2131         else {
2132                 /* By default we use original values, so we don't worry about restoring
2133                  * defaults later one and can only do override when needed.
2134                  */
2135                 roughness = roughness_orig;
2136                 distribution = distribution_orig;
2137         }
2138         Integrator *integrator = scene->integrator;
2139         ShaderInput *roughness_input = input("Roughness");
2140         if(integrator->filter_glossy == 0.0f) {
2141                 /* Fallback to Sharp closure for Roughness close to 0.
2142                  * Note: Keep the epsilon in sync with kernel!
2143                  */
2144                 if(!roughness_input->link && roughness <= 1e-4f) {
2145                         VLOG(1) << "Using sharp glass BSDF.";
2146                         distribution = CLOSURE_BSDF_SHARP_GLASS_ID;
2147                 }
2148         }
2149         else {
2150                 /* If filter glossy is used we replace Sharp glossy with GGX so we can
2151                  * benefit from closure blur to remove unwanted noise.
2152                  */
2153                 if(roughness_input->link == NULL &&
2154                    distribution == CLOSURE_BSDF_SHARP_GLASS_ID)
2155                 {
2156                         VLOG(1) << "Using GGX glass with filter glossy.";
2157                         distribution = CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID;
2158                         roughness = 0.0f;
2159                 }
2160         }
2161         closure = distribution;
2162 }
2163
2164 bool GlassBsdfNode::has_integrator_dependency()
2165 {
2166         ShaderInput *roughness_input = input("Roughness");
2167         return !roughness_input->link &&
2168                (distribution == CLOSURE_BSDF_SHARP_GLASS_ID || roughness <= 1e-4f);
2169 }
2170
2171 void GlassBsdfNode::compile(SVMCompiler& compiler)
2172 {
2173         closure = distribution;
2174
2175         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
2176                 BsdfNode::compile(compiler, NULL, input("IOR"));
2177         else if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID)
2178                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"), input("Color"));
2179         else
2180                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2181 }
2182
2183 void GlassBsdfNode::compile(OSLCompiler& compiler)
2184 {
2185         compiler.parameter(this, "distribution");
2186         compiler.add(this, "node_glass_bsdf");
2187 }
2188
2189 /* Refraction BSDF Closure */
2190
2191 NODE_DEFINE(RefractionBsdfNode)
2192 {
2193         NodeType* type = NodeType::add("refraction_bsdf", create, NodeType::SHADER);
2194
2195         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2196         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2197         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2198
2199         static NodeEnum distribution_enum;
2200         distribution_enum.insert("sharp", CLOSURE_BSDF_REFRACTION_ID);
2201         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
2202         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2203         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2204
2205         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2206         SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
2207
2208         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2209
2210         return type;
2211 }
2212
2213 RefractionBsdfNode::RefractionBsdfNode()
2214 : BsdfNode(node_type)
2215 {
2216         closure = CLOSURE_BSDF_REFRACTION_ID;
2217         distribution_orig = NBUILTIN_CLOSURES;
2218 }
2219
2220 void RefractionBsdfNode::simplify_settings(Scene *scene)
2221 {
2222         if(distribution_orig == NBUILTIN_CLOSURES) {
2223                 roughness_orig = roughness;
2224                 distribution_orig = distribution;
2225         }
2226         else {
2227                 /* By default we use original values, so we don't worry about restoring
2228                  * defaults later one and can only do override when needed.
2229                  */
2230                 roughness = roughness_orig;
2231                 distribution = distribution_orig;
2232         }
2233         Integrator *integrator = scene->integrator;
2234         ShaderInput *roughness_input = input("Roughness");
2235         if(integrator->filter_glossy == 0.0f) {
2236                 /* Fallback to Sharp closure for Roughness close to 0.
2237                  * Note: Keep the epsilon in sync with kernel!
2238                  */
2239                 if(!roughness_input->link && roughness <= 1e-4f) {
2240                         VLOG(1) << "Using sharp refraction BSDF.";
2241                         distribution = CLOSURE_BSDF_REFRACTION_ID;
2242                 }
2243         }
2244         else {
2245                 /* If filter glossy is used we replace Sharp glossy with GGX so we can
2246                  * benefit from closure blur to remove unwanted noise.
2247                  */
2248                 if(roughness_input->link == NULL &&
2249                    distribution == CLOSURE_BSDF_REFRACTION_ID)
2250                 {
2251                         VLOG(1) << "Using GGX refraction with filter glossy.";
2252                         distribution = CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID;
2253                         roughness = 0.0f;
2254                 }
2255         }
2256         closure = distribution;
2257 }
2258
2259 bool RefractionBsdfNode::has_integrator_dependency()
2260 {
2261         ShaderInput *roughness_input = input("Roughness");
2262         return !roughness_input->link &&
2263                (distribution == CLOSURE_BSDF_REFRACTION_ID || roughness <= 1e-4f);
2264 }
2265
2266 void RefractionBsdfNode::compile(SVMCompiler& compiler)
2267 {
2268         closure = distribution;
2269
2270         if(closure == CLOSURE_BSDF_REFRACTION_ID)
2271                 BsdfNode::compile(compiler, NULL, input("IOR"));
2272         else
2273                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2274 }
2275
2276 void RefractionBsdfNode::compile(OSLCompiler& compiler)
2277 {
2278         compiler.parameter(this, "distribution");
2279         compiler.add(this, "node_refraction_bsdf");
2280 }
2281
2282 /* Toon BSDF Closure */
2283
2284 NODE_DEFINE(ToonBsdfNode)
2285 {
2286         NodeType* type = NodeType::add("toon_bsdf", create, NodeType::SHADER);
2287
2288         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2289         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2290         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2291
2292         static NodeEnum component_enum;
2293         component_enum.insert("diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
2294         component_enum.insert("glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
2295         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_DIFFUSE_TOON_ID);
2296         SOCKET_IN_FLOAT(size, "Size", 0.5f);
2297         SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
2298
2299         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2300
2301         return type;
2302 }
2303
2304 ToonBsdfNode::ToonBsdfNode()
2305 : BsdfNode(node_type)
2306 {
2307         closure = CLOSURE_BSDF_DIFFUSE_TOON_ID;
2308 }
2309
2310 void ToonBsdfNode::compile(SVMCompiler& compiler)
2311 {
2312         closure = component;
2313         
2314         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
2315 }
2316
2317 void ToonBsdfNode::compile(OSLCompiler& compiler)
2318 {
2319         compiler.parameter(this, "component");
2320         compiler.add(this, "node_toon_bsdf");
2321 }
2322
2323 /* Velvet BSDF Closure */
2324
2325 NODE_DEFINE(VelvetBsdfNode)
2326 {
2327         NodeType* type = NodeType::add("velvet_bsdf", create, NodeType::SHADER);
2328
2329         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2330         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2331         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2332         SOCKET_IN_FLOAT(sigma, "Sigma", 1.0f);
2333
2334         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2335
2336         return type;
2337 }
2338
2339 VelvetBsdfNode::VelvetBsdfNode()
2340 : BsdfNode(node_type)
2341 {
2342         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
2343 }
2344
2345 void VelvetBsdfNode::compile(SVMCompiler& compiler)
2346 {
2347         BsdfNode::compile(compiler, input("Sigma"), NULL);
2348 }
2349
2350 void VelvetBsdfNode::compile(OSLCompiler& compiler)
2351 {
2352         compiler.add(this, "node_velvet_bsdf");
2353 }
2354
2355 /* Diffuse BSDF Closure */
2356
2357 NODE_DEFINE(DiffuseBsdfNode)
2358 {
2359         NodeType* type = NodeType::add("diffuse_bsdf", create, NodeType::SHADER);
2360
2361         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2362         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2363         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2364         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2365
2366         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2367
2368         return type;
2369 }
2370
2371 DiffuseBsdfNode::DiffuseBsdfNode()
2372 : BsdfNode(node_type)
2373 {
2374         closure = CLOSURE_BSDF_DIFFUSE_ID;
2375 }
2376
2377 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
2378 {
2379         BsdfNode::compile(compiler, input("Roughness"), NULL);
2380 }
2381
2382 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
2383 {
2384         compiler.add(this, "node_diffuse_bsdf");
2385 }
2386
2387 /* Disney principled BSDF Closure */
2388 NODE_DEFINE(PrincipledBsdfNode)
2389 {
2390         NodeType* type = NodeType::add("principled_bsdf", create, NodeType::SHADER);
2391
2392         static NodeEnum distribution_enum;
2393         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
2394         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
2395         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
2396
2397         static NodeEnum subsurface_method_enum;
2398         subsurface_method_enum.insert("burley", CLOSURE_BSSRDF_PRINCIPLED_ID);
2399         subsurface_method_enum.insert("random_walk", CLOSURE_BSSRDF_PRINCIPLED_RANDOM_WALK_ID);
2400         SOCKET_ENUM(subsurface_method, "Subsurface Method", subsurface_method_enum, CLOSURE_BSSRDF_PRINCIPLED_ID);
2401
2402         SOCKET_IN_COLOR(base_color, "Base Color", make_float3(0.8f, 0.8f, 0.8f));
2403         SOCKET_IN_COLOR(subsurface_color, "Subsurface Color", make_float3(0.8f, 0.8f, 0.8f));
2404         SOCKET_IN_FLOAT(metallic, "Metallic", 0.0f);
2405         SOCKET_IN_FLOAT(subsurface, "Subsurface", 0.0f);
2406         SOCKET_IN_VECTOR(subsurface_radius, "Subsurface Radius", make_float3(0.1f, 0.1f, 0.1f));
2407         SOCKET_IN_FLOAT(specular, "Specular", 0.0f);
2408         SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
2409         SOCKET_IN_FLOAT(specular_tint, "Specular Tint", 0.0f);
2410         SOCKET_IN_FLOAT(anisotropic, "Anisotropic", 0.0f);
2411         SOCKET_IN_FLOAT(sheen, "Sheen", 0.0f);
2412         SOCKET_IN_FLOAT(sheen_tint, "Sheen Tint", 0.0f);
2413         SOCKET_IN_FLOAT(clearcoat, "Clearcoat", 0.0f);
2414         SOCKET_IN_FLOAT(clearcoat_roughness, "Clearcoat Roughness", 0.03f);
2415         SOCKET_IN_FLOAT(ior, "IOR", 0.0f);
2416         SOCKET_IN_FLOAT(transmission, "Transmission", 0.0f);
2417         SOCKET_IN_FLOAT(transmission_roughness, "Transmission Roughness", 0.0f);
2418         SOCKET_IN_FLOAT(anisotropic_rotation, "Anisotropic Rotation", 0.0f);
2419         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2420         SOCKET_IN_NORMAL(clearcoat_normal, "Clearcoat Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2421         SOCKET_IN_NORMAL(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
2422         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2423
2424         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2425
2426         return type;
2427 }
2428
2429 PrincipledBsdfNode::PrincipledBsdfNode()
2430         : BsdfBaseNode(node_type)
2431 {
2432         closure = CLOSURE_BSDF_PRINCIPLED_ID;
2433         distribution = CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID;
2434         distribution_orig = NBUILTIN_CLOSURES;
2435 }
2436
2437 bool PrincipledBsdfNode::has_surface_bssrdf()
2438 {
2439         ShaderInput *subsurface_in = input("Subsurface");
2440         return (subsurface_in->link != NULL || subsurface > CLOSURE_WEIGHT_CUTOFF);
2441 }
2442
2443 void PrincipledBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2444 {
2445         if(shader->has_surface) {
2446                 ShaderInput *tangent_in = input("Tangent");
2447
2448                 if(!tangent_in->link)
2449                         attributes->add(ATTR_STD_GENERATED);
2450         }
2451
2452         ShaderNode::attributes(shader, attributes);
2453 }
2454
2455 void PrincipledBsdfNode::compile(SVMCompiler& compiler, ShaderInput *p_metallic, ShaderInput *p_subsurface, ShaderInput *p_subsurface_radius,
2456         ShaderInput *p_specular, ShaderInput *p_roughness, ShaderInput *p_specular_tint, ShaderInput *p_anisotropic,
2457         ShaderInput *p_sheen, ShaderInput *p_sheen_tint, ShaderInput *p_clearcoat, ShaderInput *p_clearcoat_roughness,
2458         ShaderInput *p_ior, ShaderInput *p_transmission, ShaderInput *p_anisotropic_rotation, ShaderInput *p_transmission_roughness)
2459 {
2460         ShaderInput *base_color_in = input("Base Color");
2461         ShaderInput *subsurface_color_in = input("Subsurface Color");
2462         ShaderInput *normal_in = input("Normal");
2463         ShaderInput *clearcoat_normal_in = input("Clearcoat Normal");
2464         ShaderInput *tangent_in = input("Tangent");
2465
2466         float3 weight = make_float3(1.0f, 1.0f, 1.0f);
2467
2468         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, weight);
2469
2470         int normal_offset = compiler.stack_assign_if_linked(normal_in);
2471         int clearcoat_normal_offset = compiler.stack_assign_if_linked(clearcoat_normal_in);
2472         int tangent_offset = compiler.stack_assign_if_linked(tangent_in);
2473         int specular_offset = compiler.stack_assign(p_specular);
2474         int roughness_offset = compiler.stack_assign(p_roughness);
2475         int specular_tint_offset = compiler.stack_assign(p_specular_tint);
2476         int anisotropic_offset = compiler.stack_assign(p_anisotropic);
2477         int sheen_offset = compiler.stack_assign(p_sheen);
2478         int sheen_tint_offset = compiler.stack_assign(p_sheen_tint);
2479         int clearcoat_offset = compiler.stack_assign(p_clearcoat);
2480         int clearcoat_roughness_offset = compiler.stack_assign(p_clearcoat_roughness);
2481         int ior_offset = compiler.stack_assign(p_ior);
2482         int transmission_offset = compiler.stack_assign(p_transmission);
2483         int transmission_roughness_offset = compiler.stack_assign(p_transmission_roughness);
2484         int anisotropic_rotation_offset = compiler.stack_assign(p_anisotropic_rotation);
2485         int subsurface_radius_offset = compiler.stack_assign(p_subsurface_radius);
2486
2487         compiler.add_node(NODE_CLOSURE_BSDF,
2488                 compiler.encode_uchar4(closure,
2489                 compiler.stack_assign(p_metallic),
2490                 compiler.stack_assign(p_subsurface),
2491                 compiler.closure_mix_weight_offset()),
2492                 __float_as_int((p_metallic) ? get_float(p_metallic->socket_type) : 0.0f),
2493                 __float_as_int((p_subsurface) ? get_float(p_subsurface->socket_type) : 0.0f));
2494
2495         compiler.add_node(normal_offset, tangent_offset,
2496                 compiler.encode_uchar4(specular_offset, roughness_offset, specular_tint_offset, anisotropic_offset),
2497                 compiler.encode_uchar4(sheen_offset, sheen_tint_offset, clearcoat_offset, clearcoat_roughness_offset));
2498
2499         compiler.add_node(compiler.encode_uchar4(ior_offset, transmission_offset, anisotropic_rotation_offset, transmission_roughness_offset),
2500                 distribution, subsurface_method, SVM_STACK_INVALID);
2501
2502         float3 bc_default = get_float3(base_color_in->socket_type);
2503
2504         compiler.add_node(((base_color_in->link) ? compiler.stack_assign(base_color_in) : SVM_STACK_INVALID),
2505                 __float_as_int(bc_default.x), __float_as_int(bc_default.y), __float_as_int(bc_default.z));
2506
2507         compiler.add_node(clearcoat_normal_offset, subsurface_radius_offset, SVM_STACK_INVALID, SVM_STACK_INVALID);
2508
2509         float3 ss_default = get_float3(subsurface_color_in->socket_type);
2510
2511         compiler.add_node(((subsurface_color_in->link) ? compiler.stack_assign(subsurface_color_in) : SVM_STACK_INVALID),
2512                 __float_as_int(ss_default.x), __float_as_int(ss_default.y), __float_as_int(ss_default.z));
2513 }
2514
2515 bool PrincipledBsdfNode::has_integrator_dependency()
2516 {
2517         ShaderInput *roughness_input = input("Roughness");
2518         return !roughness_input->link && roughness <= 1e-4f;
2519 }
2520
2521 void PrincipledBsdfNode::compile(SVMCompiler& compiler)
2522 {
2523         compile(compiler, input("Metallic"), input("Subsurface"), input("Subsurface Radius"), input("Specular"),
2524                 input("Roughness"), input("Specular Tint"), input("Anisotropic"), input("Sheen"), input("Sheen Tint"),
2525                 input("Clearcoat"), input("Clearcoat Roughness"), input("IOR"), input("Transmission"),
2526                 input("Anisotropic Rotation"), input("Transmission Roughness"));
2527 }
2528
2529 void PrincipledBsdfNode::compile(OSLCompiler& compiler)
2530 {
2531         compiler.parameter(this, "distribution");
2532         compiler.parameter(this, "subsurface_method");
2533         compiler.add(this, "node_principled_bsdf");
2534 }
2535
2536 bool PrincipledBsdfNode::has_bssrdf_bump()
2537 {
2538         return has_surface_bssrdf() && has_bump();
2539 }
2540
2541 /* Translucent BSDF Closure */
2542
2543 NODE_DEFINE(TranslucentBsdfNode)
2544 {
2545         NodeType* type = NodeType::add("translucent_bsdf", create, NodeType::SHADER);
2546
2547         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2548         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2549         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2550
2551         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2552
2553         return type;
2554 }
2555
2556 TranslucentBsdfNode::TranslucentBsdfNode()
2557 : BsdfNode(node_type)
2558 {
2559         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
2560 }
2561
2562 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
2563 {
2564         BsdfNode::compile(compiler, NULL, NULL);
2565 }
2566
2567 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
2568 {
2569         compiler.add(this, "node_translucent_bsdf");
2570 }
2571
2572 /* Transparent BSDF Closure */
2573
2574 NODE_DEFINE(TransparentBsdfNode)
2575 {
2576         NodeType* type = NodeType::add("transparent_bsdf", create, NodeType::SHADER);
2577
2578         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2579         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2580
2581         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2582
2583         return type;
2584 }
2585
2586 TransparentBsdfNode::TransparentBsdfNode()
2587 : BsdfNode(node_type)
2588 {
2589         closure = CLOSURE_BSDF_TRANSPARENT_ID;
2590 }
2591
2592 void TransparentBsdfNode::compile(SVMCompiler& compiler)
2593 {
2594         BsdfNode::compile(compiler, NULL, NULL);
2595 }
2596
2597 void TransparentBsdfNode::compile(OSLCompiler& compiler)
2598 {
2599         compiler.add(this, "node_transparent_bsdf");
2600 }
2601
2602 /* Subsurface Scattering Closure */
2603
2604 NODE_DEFINE(SubsurfaceScatteringNode)
2605 {
2606         NodeType* type = NodeType::add("subsurface_scattering", create, NodeType::SHADER);
2607
2608         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2609         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2610         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2611
2612         static NodeEnum falloff_enum;
2613         falloff_enum.insert("cubic", CLOSURE_BSSRDF_CUBIC_ID);
2614         falloff_enum.insert("gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
2615         falloff_enum.insert("burley", CLOSURE_BSSRDF_BURLEY_ID);
2616         falloff_enum.insert("random_walk", CLOSURE_BSSRDF_RANDOM_WALK_ID);
2617         SOCKET_ENUM(falloff, "Falloff", falloff_enum, CLOSURE_BSSRDF_BURLEY_ID);
2618         SOCKET_IN_FLOAT(scale, "Scale", 0.01f);
2619         SOCKET_IN_VECTOR(radius, "Radius", make_float3(0.1f, 0.1f, 0.1f));
2620         SOCKET_IN_FLOAT(sharpness, "Sharpness", 0.0f);
2621         SOCKET_IN_FLOAT(texture_blur, "Texture Blur", 1.0f);
2622
2623         SOCKET_OUT_CLOSURE(BSSRDF, "BSSRDF");
2624
2625         return type;
2626 }
2627
2628 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
2629 : BsdfNode(node_type)
2630 {
2631         closure = falloff;
2632 }
2633
2634 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
2635 {
2636         closure = falloff;
2637         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
2638 }
2639
2640 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
2641 {
2642         closure = falloff;
2643         compiler.parameter(this, "falloff");
2644         compiler.add(this, "node_subsurface_scattering");
2645 }
2646
2647 bool SubsurfaceScatteringNode::has_bssrdf_bump()
2648 {
2649         /* detect if anything is plugged into the normal input besides the default */
2650         ShaderInput *normal_in = input("Normal");
2651         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
2652 }
2653
2654 /* Emissive Closure */
2655
2656 NODE_DEFINE(EmissionNode)
2657 {
2658         NodeType* type = NodeType::add("emission", create, NodeType::SHADER);
2659
2660         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2661         SOCKET_IN_FLOAT(strength, "Strength", 10.0f);
2662         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2663
2664         SOCKET_OUT_CLOSURE(emission, "Emission");
2665
2666         return type;
2667 }
2668
2669 EmissionNode::EmissionNode()
2670 : ShaderNode(node_type)
2671 {
2672 }
2673
2674 void EmissionNode::compile(SVMCompiler& compiler)
2675 {
2676         ShaderInput *color_in = input("Color");
2677         ShaderInput *strength_in = input("Strength");
2678
2679         if(color_in->link || strength_in->link) {
2680                 compiler.add_node(NODE_EMISSION_WEIGHT,
2681                                   compiler.stack_assign(color_in),
2682                                   compiler.stack_assign(strength_in));
2683         }
2684         else
2685                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color * strength);
2686
2687         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
2688 }
2689
2690 void EmissionNode::compile(OSLCompiler& compiler)
2691 {
2692         compiler.add(this, "node_emission");
2693 }
2694
2695 void EmissionNode::constant_fold(const ConstantFolder& folder)
2696 {
2697         ShaderInput *color_in = input("Color");
2698         ShaderInput *strength_in = input("Strength");
2699
2700         if((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2701            (!strength_in->link && strength == 0.0f))
2702         {
2703                 folder.discard();
2704         }
2705 }
2706
2707 /* Background Closure */
2708
2709 NODE_DEFINE(BackgroundNode)
2710 {
2711         NodeType* type = NodeType::add("background_shader", create, NodeType::SHADER);
2712
2713         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2714         SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
2715         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2716
2717         SOCKET_OUT_CLOSURE(background, "Background");
2718
2719         return type;
2720 }
2721
2722 BackgroundNode::BackgroundNode()
2723 : ShaderNode(node_type)
2724 {
2725 }
2726
2727 void BackgroundNode::compile(SVMCompiler& compiler)
2728 {
2729         ShaderInput *color_in = input("Color");
2730         ShaderInput *strength_in = input("Strength");
2731
2732         if(color_in->link || strength_in->link) {
2733                 compiler.add_node(NODE_EMISSION_WEIGHT,
2734                                   compiler.stack_assign(color_in),
2735                                   compiler.stack_assign(strength_in));
2736         }
2737         else
2738                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color*strength);
2739
2740         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
2741 }
2742
2743 void BackgroundNode::compile(OSLCompiler& compiler)
2744 {
2745         compiler.add(this, "node_background");
2746 }
2747
2748 void BackgroundNode::constant_fold(const ConstantFolder& folder)
2749 {
2750         ShaderInput *color_in = input("Color");
2751         ShaderInput *strength_in = input("Strength");
2752
2753         if((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2754            (!strength_in->link && strength == 0.0f))
2755         {
2756                 folder.discard();
2757         }
2758 }
2759
2760 /* Holdout Closure */
2761
2762 NODE_DEFINE(HoldoutNode)
2763 {
2764         NodeType* type = NodeType::add("holdout", create, NodeType::SHADER);
2765
2766         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2767         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2768
2769         SOCKET_OUT_CLOSURE(holdout, "Holdout");
2770
2771         return type;
2772 }
2773
2774 HoldoutNode::HoldoutNode()
2775 : ShaderNode(node_type)
2776 {
2777 }
2778
2779 void HoldoutNode::compile(SVMCompiler& compiler)
2780 {
2781         float3 value = make_float3(1.0f, 1.0f, 1.0f);
2782
2783         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
2784         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
2785 }
2786
2787 void HoldoutNode::compile(OSLCompiler& compiler)
2788 {
2789         compiler.add(this, "node_holdout");
2790 }
2791
2792 /* Ambient Occlusion */
2793
2794 NODE_DEFINE(AmbientOcclusionNode)
2795 {
2796         NodeType* type = NodeType::add("ambient_occlusion", create, NodeType::SHADER);
2797
2798         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2799         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2800         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2801
2802         SOCKET_OUT_CLOSURE(AO, "AO");
2803
2804         return type;
2805 }
2806
2807 AmbientOcclusionNode::AmbientOcclusionNode()
2808 : ShaderNode(node_type)
2809 {
2810 }
2811
2812 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
2813 {
2814         ShaderInput *color_in = input("Color");
2815
2816         if(color_in->link)
2817                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2818         else
2819                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2820
2821         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
2822 }
2823
2824 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
2825 {
2826         compiler.add(this, "node_ambient_occlusion");
2827 }
2828
2829 /* Volume Closure */
2830
2831 VolumeNode::VolumeNode(const NodeType *node_type)
2832 : ShaderNode(node_type)
2833 {
2834         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2835 }
2836
2837 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
2838 {
2839         ShaderInput *color_in = input("Color");
2840
2841         if(color_in->link)
2842                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2843         else
2844                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2845         
2846         compiler.add_node(NODE_CLOSURE_VOLUME,
2847                 compiler.encode_uchar4(closure,
2848                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
2849                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
2850                         compiler.closure_mix_weight_offset()),
2851                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
2852                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
2853 }
2854
2855 void VolumeNode::compile(SVMCompiler& compiler)
2856 {
2857         compile(compiler, NULL, NULL);
2858 }
2859
2860 void VolumeNode::compile(OSLCompiler& /*compiler*/)
2861 {
2862         assert(0);
2863 }
2864
2865 /* Absorption Volume Closure */
2866
2867 NODE_DEFINE(AbsorptionVolumeNode)
2868 {
2869         NodeType* type = NodeType::add("absorption_volume", create, NodeType::SHADER);
2870
2871         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2872         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2873         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2874
2875         SOCKET_OUT_CLOSURE(volume, "Volume");
2876
2877         return type;
2878 }
2879
2880 AbsorptionVolumeNode::AbsorptionVolumeNode()
2881 : VolumeNode(node_type)
2882 {
2883         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2884 }
2885
2886 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2887 {
2888         VolumeNode::compile(compiler, input("Density"), NULL);
2889 }
2890
2891 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2892 {
2893         compiler.add(this, "node_absorption_volume");
2894 }
2895
2896 /* Scatter Volume Closure */
2897
2898 NODE_DEFINE(ScatterVolumeNode)
2899 {
2900         NodeType* type = NodeType::add("scatter_volume", create, NodeType::SHADER);
2901
2902         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2903         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2904         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
2905         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2906
2907         SOCKET_OUT_CLOSURE(volume, "Volume");
2908
2909         return type;
2910 }
2911
2912 ScatterVolumeNode::ScatterVolumeNode()
2913 : VolumeNode(node_type)
2914 {
2915         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2916 }
2917
2918 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2919 {
2920         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2921 }
2922
2923 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2924 {
2925         compiler.add(this, "node_scatter_volume");
2926 }
2927
2928 /* Principled Volume Closure */
2929
2930 NODE_DEFINE(PrincipledVolumeNode)
2931 {
2932         NodeType* type = NodeType::add("principled_volume", create, NodeType::SHADER);
2933
2934         SOCKET_IN_STRING(density_attribute, "Density Attribute", ustring());
2935         SOCKET_IN_STRING(color_attribute, "Color Attribute", ustring());
2936         SOCKET_IN_STRING(temperature_attribute, "Temperature Attribute", ustring());
2937
2938         SOCKET_IN_COLOR(color, "Color", make_float3(0.5f, 0.5f, 0.5f));
2939         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2940         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
2941         SOCKET_IN_COLOR(absorption_color, "Absorption Color", make_float3(0.0f, 0.0f, 0.0f));
2942         SOCKET_IN_FLOAT(emission_strength, "Emission Strength", 0.0f);
2943         SOCKET_IN_COLOR(emission_color, "Emission Color", make_float3(1.0f, 1.0f, 1.0f));
2944         SOCKET_IN_FLOAT(blackbody_intensity, "Blackbody Intensity", 0.0f);
2945         SOCKET_IN_COLOR(blackbody_tint, "Blackbody Tint", make_float3(1.0f, 1.0f, 1.0f));
2946         SOCKET_IN_FLOAT(temperature, "Temperature", 1500.0f);
2947         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2948
2949         SOCKET_OUT_CLOSURE(volume, "Volume");
2950
2951         return type;
2952 }
2953
2954 PrincipledVolumeNode::PrincipledVolumeNode()
2955 : VolumeNode(node_type)
2956 {
2957         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2958 }
2959
2960 void PrincipledVolumeNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2961 {
2962         if(shader->has_volume) {
2963                 ShaderInput *density_in = input("Density");
2964                 ShaderInput *blackbody_in = input("Blackbody Intensity");
2965
2966                 if(density_in->link || density > 0.0f) {
2967                         attributes->add_standard(density_attribute);
2968                         attributes->add_standard(color_attribute);
2969                 }
2970
2971                 if(blackbody_in->link || blackbody_intensity > 0.0f) {
2972                         attributes->add_standard(temperature_attribute);
2973                 }
2974
2975                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
2976         }
2977
2978         ShaderNode::attributes(shader, attributes);
2979 }
2980
2981 void PrincipledVolumeNode::compile(SVMCompiler& compiler)
2982 {
2983         ShaderInput *color_in = input("Color");
2984         ShaderInput *density_in = input("Density");
2985         ShaderInput *anisotropy_in = input("Anisotropy");
2986         ShaderInput *absorption_color_in = input("Absorption Color");
2987         ShaderInput *emission_in = input("Emission Strength");
2988         ShaderInput *emission_color_in = input("Emission Color");
2989         ShaderInput *blackbody_in = input("Blackbody Intensity");
2990         ShaderInput *blackbody_tint_in = input("Blackbody Tint");
2991         ShaderInput *temperature_in = input("Temperature");
2992
2993         if(color_in->link)
2994                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2995         else
2996                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2997
2998         compiler.add_node(NODE_PRINCIPLED_VOLUME,
2999                 compiler.encode_uchar4(
3000                         compiler.stack_assign_if_linked(density_in),
3001                         compiler.stack_assign_if_linked(anisotropy_in),
3002                         compiler.stack_assign(absorption_color_in),
3003                         compiler.closure_mix_weight_offset()),
3004                 compiler.encode_uchar4(
3005                         compiler.stack_assign_if_linked(emission_in),
3006                         compiler.stack_assign(emission_color_in),
3007                         compiler.stack_assign_if_linked(blackbody_in),
3008                         compiler.stack_assign(temperature_in)),
3009                 compiler.stack_assign(blackbody_tint_in));
3010
3011         int attr_density = compiler.attribute_standard(density_attribute);
3012         int attr_color = compiler.attribute_standard(color_attribute);
3013         int attr_temperature = compiler.attribute_standard(temperature_attribute);
3014
3015         compiler.add_node(
3016                 __float_as_int(density),
3017                 __float_as_int(anisotropy),
3018                 __float_as_int(emission_strength),
3019                 __float_as_int(blackbody_intensity));
3020
3021         compiler.add_node(
3022                 attr_density,
3023                 attr_color,
3024                 attr_temperature);
3025 }
3026
3027 void PrincipledVolumeNode::compile(OSLCompiler& compiler)
3028 {
3029         if(Attribute::name_standard(density_attribute.c_str())) {
3030                 density_attribute = ustring("geom:" + density_attribute.string());
3031         }
3032         if(Attribute::name_standard(color_attribute.c_str())) {
3033                 color_attribute = ustring("geom:" + color_attribute.string());
3034         }
3035         if(Attribute::name_standard(temperature_attribute.c_str())) {
3036                 temperature_attribute = ustring("geom:" + temperature_attribute.string());
3037         }
3038
3039         compiler.add(this, "node_principled_volume");
3040 }
3041
3042 /* Hair BSDF Closure */
3043
3044 NODE_DEFINE(HairBsdfNode)
3045 {
3046         NodeType* type = NodeType::add("hair_bsdf", create, NodeType::SHADER);
3047
3048         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
3049         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
3050         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
3051
3052         static NodeEnum component_enum;
3053         component_enum.insert("reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
3054         component_enum.insert("transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
3055         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_HAIR_REFLECTION_ID);
3056         SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
3057         SOCKET_IN_FLOAT(roughness_u, "RoughnessU", 0.2f);
3058         SOCKET_IN_FLOAT(roughness_v, "RoughnessV", 0.2f);
3059         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f));
3060
3061         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
3062
3063         return type;
3064 }
3065
3066 HairBsdfNode::HairBsdfNode()
3067 : BsdfNode(node_type)
3068 {
3069         closure = CLOSURE_BSDF_HAIR_REFLECTION_ID;
3070 }
3071
3072 void HairBsdfNode::compile(SVMCompiler& compiler)
3073 {
3074         closure = component;
3075
3076         BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
3077 }
3078
3079 void HairBsdfNode::compile(OSLCompiler& compiler)
3080 {
3081         compiler.parameter(this, "component");
3082         compiler.add(this, "node_hair_bsdf");
3083 }
3084
3085 /* Geometry */
3086
3087 NODE_DEFINE(GeometryNode)
3088 {
3089         NodeType* type = NodeType::add("geometry", create, NodeType::SHADER);
3090
3091         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
3092
3093         SOCKET_OUT_POINT(position, "Position");
3094         SOCKET_OUT_NORMAL(normal, "Normal");
3095         SOCKET_OUT_NORMAL(tangent, "Tangent");
3096         SOCKET_OUT_NORMAL(true_normal, "True Normal");
3097         SOCKET_OUT_VECTOR(incoming, "Incoming");
3098         SOCKET_OUT_POINT(parametric, "Parametric");
3099         SOCKET_OUT_FLOAT(backfacing, "Backfacing");
3100         SOCKET_OUT_FLOAT(pointiness, "Pointiness");
3101
3102         return type;
3103 }
3104
3105 GeometryNode::GeometryNode()
3106 : ShaderNode(node_type)
3107 {
3108         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
3109 }
3110
3111 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3112 {
3113         if(shader->has_surface) {
3114                 if(!output("Tangent")->links.empty()) {
3115                         attributes->add(ATTR_STD_GENERATED);
3116                 }
3117                 if(!output("Pointiness")->links.empty()) {
3118                         attributes->add(ATTR_STD_POINTINESS);
3119                 }
3120         }
3121
3122         ShaderNode::attributes(shader, attributes);
3123 }
3124
3125 void GeometryNode::compile(SVMCompiler& compiler)
3126 {
3127         ShaderOutput *out;
3128         ShaderNodeType geom_node = NODE_GEOMETRY;
3129         ShaderNodeType attr_node = NODE_ATTR;
3130
3131         if(bump == SHADER_BUMP_DX) {
3132                 geom_node = NODE_GEOMETRY_BUMP_DX;
3133                 attr_node = NODE_ATTR_BUMP_DX;
3134         }
3135         else if(bump == SHADER_BUMP_DY) {
3136                 geom_node = NODE_GEOMETRY_BUMP_DY;
3137                 attr_node = NODE_ATTR_BUMP_DY;
3138         }
3139         
3140         out = output("Position");
3141         if(!out->links.empty()) {
3142                 compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
3143         }
3144
3145         out = output("Normal");
3146         if(!out->links.empty()) {
3147                 compiler.add_node(geom_node, NODE_GEOM_N, compiler.stack_assign(out));
3148         }
3149
3150         out = output("Tangent");
3151         if(!out->links.empty()) {
3152                 compiler.add_node(geom_node, NODE_GEOM_T, compiler.stack_assign(out));
3153         }
3154
3155         out = output("True Normal");
3156         if(!out->links.empty()) {
3157                 compiler.add_node(geom_node, NODE_GEOM_Ng, compiler.stack_assign(out));
3158         }
3159
3160         out = output("Incoming");
3161         if(!out->links.empty()) {
3162                 compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
3163         }
3164
3165         out = output("Parametric");
3166         if(!out->links.empty()) {
3167                 compiler.add_node(geom_node, NODE_GEOM_uv, compiler.stack_assign(out));
3168         }
3169
3170         out = output("Backfacing");
3171         if(!out->links.empty()) {
3172                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, compiler.stack_assign(out));
3173         }
3174
3175         out = output("Pointiness");
3176         if(!out->links.empty()) {
3177                 if(compiler.output_type() != SHADER_TYPE_VOLUME) {
3178                         compiler.add_node(attr_node,
3179                                           ATTR_STD_POINTINESS,
3180                                           compiler.stack_assign(out),
3181                                           NODE_ATTR_FLOAT);
3182                 }
3183                 else {
3184                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), compiler.stack_assign(out));
3185                 }
3186         }
3187 }
3188
3189 void GeometryNode::compile(OSLCompiler& compiler)
3190 {
3191         if(bump == SHADER_BUMP_DX)
3192                 compiler.parameter("bump_offset", "dx");
3193         else if(bump == SHADER_BUMP_DY)
3194                 compiler.parameter("bump_offset", "dy");
3195         else
3196                 compiler.parameter("bump_offset", "center");
3197
3198         compiler.add(this, "node_geometry");
3199 }
3200
3201 /* TextureCoordinate */
3202
3203 NODE_DEFINE(TextureCoordinateNode)
3204 {
3205         NodeType* type = NodeType::add("texture_coordinate", create, NodeType::SHADER);
3206
3207         SOCKET_BOOLEAN(from_dupli, "From Dupli", false);
3208         SOCKET_BOOLEAN(use_transform, "Use Transform", false);
3209         SOCKET_TRANSFORM(ob_tfm, "Object Transform", transform_identity());
3210
3211         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
3212
3213         SOCKET_OUT_POINT(generated, "Generated");
3214         SOCKET_OUT_NORMAL(normal, "Normal");
3215         SOCKET_OUT_POINT(UV, "UV");
3216         SOCKET_OUT_POINT(object, "Object");
3217         SOCKET_OUT_POINT(camera, "Camera");
3218         SOCKET_OUT_POINT(window, "Window");
3219         SOCKET_OUT_NORMAL(reflection, "Reflection");
3220
3221         return type;
3222 }
3223
3224 TextureCoordinateNode::TextureCoordinateNode()
3225 : ShaderNode(node_type)
3226 {
3227 }
3228
3229 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3230 {
3231         if(shader->has_surface) {
3232                 if(!from_dupli) {
3233                         if(!output("Generated")->links.empty())
3234                                 attributes->add(ATTR_STD_GENERATED);
3235                         if(!output("UV")->links.empty())
3236                                 attributes->add(ATTR_STD_UV);
3237                 }
3238         }
3239
3240         if(shader->has_volume) {
3241                 if(!from_dupli) {
3242                         if(!output("Generated")->links.empty()) {
3243                                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
3244                         }
3245                 }
3246         }
3247
3248         ShaderNode::attributes(shader, attributes);
3249 }
3250
3251 void TextureCoordinateNode::compile(SVMCompiler& compiler)
3252 {
3253         ShaderOutput *out;
3254         ShaderNodeType texco_node = NODE_TEX_COORD;
3255         ShaderNodeType attr_node = NODE_ATTR;
3256         ShaderNodeType geom_node = NODE_GEOMETRY;
3257
3258         if(bump == SHADER_BUMP_DX) {
3259                 texco_node = NODE_TEX_COORD_BUMP_DX;
3260                 attr_node = NODE_ATTR_BUMP_DX;
3261                 geom_node = NODE_GEOMETRY_BUMP_DX;
3262         }
3263         else if(bump == SHADER_BUMP_DY) {
3264                 texco_node = NODE_TEX_COORD_BUMP_DY;
3265                 attr_node = NODE_ATTR_BUMP_DY;
3266                 geom_node = NODE_GEOMETRY_BUMP_DY;
3267         }
3268         
3269         out = output("Generated");
3270         if(!out->links.empty()) {
3271                 if(compiler.background) {
3272                         compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
3273                 }
3274                 else {
3275                         if(from_dupli) {
3276                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, compiler.stack_assign(out));
3277                         }
3278                         else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
3279                                 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, compiler.stack_assign(out));
3280                         }
3281                         else {
3282                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
3283                                 compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
3284                         }
3285                 }
3286         }
3287
3288         out = output("Normal");
3289         if(!out->links.empty()) {
3290                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, compiler.stack_assign(out));
3291         }
3292
3293         out = output("UV");
3294         if(!out->links.empty()) {
3295                 if(from_dupli) {
3296                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
3297                 }
3298                 else {
3299                         int attr = compiler.attribute(ATTR_STD_UV);
3300                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
3301                 }
3302         }
3303
3304         out = output("Object");
3305         if(!out->links.empty()) {
3306                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, compiler.stack_assign(out), use_transform);
3307                 if(use_transform) {
3308                         Transform ob_itfm = transform_inverse(ob_tfm);
3309                         compiler.add_node(ob_itfm.x);
3310                         compiler.add_node(ob_itfm.y);
3311                         compiler.add_node(ob_itfm.z);
3312                 }
3313         }
3314
3315         out = output("Camera");
3316         if(!out->links.empty()) {
3317                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, compiler.stack_assign(out));
3318         }
3319
3320         out = output("Window");
3321         if(!out->links.empty()) {
3322                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, compiler.stack_assign(out));
3323         }
3324
3325         out = output("Reflection");
3326         if(!out->links.empty()) {
3327                 if(compiler.background) {
3328                         compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
3329                 }
3330                 else {
3331                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, compiler.stack_assign(out));
3332                 }
3333         }
3334 }
3335
3336 void TextureCoordinateNode::compile(OSLCompiler& compiler)
3337 {
3338         if(bump == SHADER_BUMP_DX)
3339                 compiler.parameter("bump_offset", "dx");
3340         else if(bump == SHADER_BUMP_DY)
3341                 compiler.parameter("bump_offset", "dy");
3342         else
3343                 compiler.parameter("bump_offset", "center");
3344         
3345         if(compiler.background)
3346                 compiler.parameter("is_background", true);
3347         if(compiler.output_type() == SHADER_TYPE_VOLUME)
3348                 compiler.parameter("is_volume", true);
3349         compiler.parameter(this, "use_transform");
3350         Transform ob_itfm = transform_transposed_inverse(ob_tfm);
3351         compiler.parameter("object_itfm", ob_itfm);
3352
3353         compiler.parameter(this, "from_dupli");
3354
3355         compiler.add(this, "node_texture_coordinate");
3356 }
3357
3358 /* UV Map */
3359
3360 NODE_DEFINE(UVMapNode)
3361 {
3362         NodeType* type = NodeType::add("uvmap", create, NodeType::SHADER);
3363
3364         SOCKET_STRING(attribute, "attribute", ustring());
3365         SOCKET_IN_BOOLEAN(from_dupli, "from dupli", false);
3366
3367         SOCKET_OUT_POINT(UV, "UV");
3368
3369         return type;
3370 }
3371
3372 UVMapNode::UVMapNode()
3373 : ShaderNode(node_type)
3374 {
3375 }
3376
3377 void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3378 {
3379         if(shader->has_surface) {
3380                 if(!from_dupli) {
3381                         if(!output("UV")->links.empty()) {
3382                                 if(attribute != "")
3383                                         attributes->add(attribute);
3384                                 else
3385                                         attributes->add(ATTR_STD_UV);
3386                         }
3387                 }
3388         }
3389
3390         ShaderNode::attributes(shader, attributes);
3391 }
3392
3393 void UVMapNode::compile(SVMCompiler& compiler)
3394 {
3395         ShaderOutput *out = output("UV");
3396         ShaderNodeType texco_node = NODE_TEX_COORD;
3397         ShaderNodeType attr_node = NODE_ATTR;
3398         int attr;
3399
3400         if(bump == SHADER_BUMP_DX) {
3401                 texco_node = NODE_TEX_COORD_BUMP_DX;
3402                 attr_node = NODE_ATTR_BUMP_DX;
3403         }
3404         else if(bump == SHADER_BUMP_DY) {
3405                 texco_node = NODE_TEX_COORD_BUMP_DY;
3406                 attr_node = NODE_ATTR_BUMP_DY;
3407         }
3408
3409         if(!out->links.empty()) {
3410                 if(from_dupli) {
3411                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
3412                 }
3413                 else {
3414                         if(attribute != "")
3415                                 attr = compiler.attribute(attribute);
3416                         else
3417                                 attr = compiler.attribute(ATTR_STD_UV);
3418
3419                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
3420                 }
3421         }
3422 }
3423
3424 void UVMapNode::compile(OSLCompiler& compiler)
3425 {
3426         if(bump == SHADER_BUMP_DX)
3427                 compiler.parameter("bump_offset", "dx");
3428         else if(bump == SHADER_BUMP_DY)
3429                 compiler.parameter("bump_offset", "dy");
3430         else
3431                 compiler.parameter("bump_offset", "center");
3432
3433         compiler.parameter(this, "from_dupli");
3434         compiler.parameter(this, "attribute");
3435         compiler.add(this, "node_uv_map");
3436 }
3437
3438 /* Light Path */
3439
3440 NODE_DEFINE(LightPathNode)
3441 {
3442         NodeType* type = NodeType::add("light_path", create, NodeType::SHADER);
3443
3444         SOCKET_OUT_FLOAT(is_camera_ray, "Is Camera Ray");
3445         SOCKET_OUT_FLOAT(is_shadow_ray, "Is Shadow Ray");
3446         SOCKET_OUT_FLOAT(is_diffuse_ray, "Is Diffuse Ray");
3447         SOCKET_OUT_FLOAT(is_glossy_ray, "Is Glossy Ray");
3448         SOCKET_OUT_FLOAT(is_singular_ray, "Is Singular Ray");
3449         SOCKET_OUT_FLOAT(is_reflection_ray, "Is Reflection Ray");
3450         SOCKET_OUT_FLOAT(is_transmission_ray, "Is Transmission Ray");
3451         SOCKET_OUT_FLOAT(is_volume_scatter_ray, "Is Volume Scatter Ray");
3452         SOCKET_OUT_FLOAT(ray_length, "Ray Length");
3453         SOCKET_OUT_FLOAT(ray_depth, "Ray Depth");
3454         SOCKET_OUT_FLOAT(diffuse_depth, "Diffuse Depth");
3455         SOCKET_OUT_FLOAT(glossy_depth, "Glossy Depth");
3456         SOCKET_OUT_FLOAT(transparent_depth, "Transparent Depth");
3457         SOCKET_OUT_FLOAT(transmission_depth, "Transmission Depth");
3458
3459         return type;
3460 }
3461
3462 LightPathNode::LightPathNode()
3463 : ShaderNode(node_type)
3464 {
3465 }
3466
3467 void LightPathNode::compile(SVMCompiler& compiler)
3468 {
3469         ShaderOutput *out;
3470
3471         out = output("Is Camera Ray");
3472         if(!out->links.empty()) {
3473                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, compiler.stack_assign(out));
3474         }
3475
3476         out = output("Is Shadow Ray");
3477         if(!out->links.empty()) {
3478                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, compiler.stack_assign(out));
3479         }
3480
3481         out = output("Is Diffuse Ray");
3482         if(!out->links.empty()) {
3483                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, compiler.stack_assign(out));
3484         }
3485
3486         out = output("Is Glossy Ray");
3487         if(!out->links.empty()) {
3488                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, compiler.stack_assign(out));
3489         }
3490
3491         out = output("Is Singular Ray");
3492         if(!out->links.empty()) {
3493                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, compiler.stack_assign(out));
3494         }
3495
3496         out = output("Is Reflection Ray");
3497         if(!out->links.empty()) {
3498                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, compiler.stack_assign(out));
3499         }
3500
3501
3502         out = output("Is Transmission Ray");
3503         if(!out->links.empty()) {
3504                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, compiler.stack_assign(out));
3505         }
3506         
3507         out = output("Is Volume Scatter Ray");
3508         if(!out->links.empty()) {
3509                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, compiler.stack_assign(out));
3510         }
3511
3512         out = output("Ray Length");
3513         if(!out->links.empty()) {
3514                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, compiler.stack_assign(out));
3515         }
3516         
3517         out = output("Ray Depth");
3518         if(!out->links.empty()) {
3519                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, compiler.stack_assign(out));
3520         }
3521
3522         out = output("Diffuse Depth");
3523         if(!out->links.empty()) {
3524                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_diffuse, compiler.stack_assign(out));
3525         }
3526
3527         out = output("Glossy Depth");
3528         if(!out->links.empty()) {
3529                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_glossy, compiler.stack_assign(out));
3530         }
3531
3532         out = output("Transparent Depth");
3533         if(!out->links.empty()) {
3534                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, compiler.stack_assign(out));
3535         }
3536
3537         out = output("Transmission Depth");
3538         if(!out->links.empty()) {
3539                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transmission, compiler.stack_assign(out));
3540         }
3541 }
3542
3543 void LightPathNode::compile(OSLCompiler& compiler)
3544 {
3545         compiler.add(this, "node_light_path");
3546 }
3547
3548 /* Light Falloff */
3549
3550 NODE_DEFINE(LightFalloffNode)
3551 {
3552         NodeType* type = NodeType::add("light_fallof", create, NodeType::SHADER);
3553
3554         SOCKET_IN_FLOAT(strength, "Strength", 100.0f);
3555         SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
3556
3557         SOCKET_OUT_FLOAT(quadratic, "Quadratic");
3558         SOCKET_OUT_FLOAT(linear, "Linear");
3559         SOCKET_OUT_FLOAT(constant, "Constant");
3560
3561         return type;
3562 }
3563
3564 LightFalloffNode::LightFalloffNode()
3565 : ShaderNode(node_type)
3566 {
3567 }
3568
3569 void LightFalloffNode::compile(SVMCompiler& compiler)
3570 {
3571         ShaderInput *strength_in = input("Strength");
3572         ShaderInput *smooth_in = input("Smooth");
3573
3574         ShaderOutput *out = output("Quadratic");
3575         if(!out->links.empty()) {