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