Cycles: add random walk subsurface scattering to Principled BSDF.
[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
2316         static NodeEnum subsurface_method_enum;
2317         subsurface_method_enum.insert("burley", CLOSURE_BSSRDF_PRINCIPLED_ID);
2318         subsurface_method_enum.insert("random_walk", CLOSURE_BSSRDF_PRINCIPLED_RANDOM_WALK_ID);
2319         SOCKET_ENUM(subsurface_method, "Subsurface Method", subsurface_method_enum, CLOSURE_BSSRDF_PRINCIPLED_ID);
2320
2321         SOCKET_IN_COLOR(base_color, "Base Color", make_float3(0.8f, 0.8f, 0.8f));
2322         SOCKET_IN_COLOR(subsurface_color, "Subsurface Color", make_float3(0.8f, 0.8f, 0.8f));
2323         SOCKET_IN_FLOAT(metallic, "Metallic", 0.0f);
2324         SOCKET_IN_FLOAT(subsurface, "Subsurface", 0.0f);
2325         SOCKET_IN_VECTOR(subsurface_radius, "Subsurface Radius", make_float3(0.1f, 0.1f, 0.1f));
2326         SOCKET_IN_FLOAT(specular, "Specular", 0.0f);
2327         SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
2328         SOCKET_IN_FLOAT(specular_tint, "Specular Tint", 0.0f);
2329         SOCKET_IN_FLOAT(anisotropic, "Anisotropic", 0.0f);
2330         SOCKET_IN_FLOAT(sheen, "Sheen", 0.0f);
2331         SOCKET_IN_FLOAT(sheen_tint, "Sheen Tint", 0.0f);
2332         SOCKET_IN_FLOAT(clearcoat, "Clearcoat", 0.0f);
2333         SOCKET_IN_FLOAT(clearcoat_roughness, "Clearcoat Roughness", 0.03f);
2334         SOCKET_IN_FLOAT(ior, "IOR", 0.0f);
2335         SOCKET_IN_FLOAT(transmission, "Transmission", 0.0f);
2336         SOCKET_IN_FLOAT(transmission_roughness, "Transmission Roughness", 0.0f);
2337         SOCKET_IN_FLOAT(anisotropic_rotation, "Anisotropic Rotation", 0.0f);
2338         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2339         SOCKET_IN_NORMAL(clearcoat_normal, "Clearcoat Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2340         SOCKET_IN_NORMAL(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
2341         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2342
2343         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2344
2345         return type;
2346 }
2347
2348 PrincipledBsdfNode::PrincipledBsdfNode()
2349         : BsdfBaseNode(node_type)
2350 {
2351         closure = CLOSURE_BSDF_PRINCIPLED_ID;
2352         distribution = CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID;
2353         distribution_orig = NBUILTIN_CLOSURES;
2354 }
2355
2356 bool PrincipledBsdfNode::has_surface_bssrdf()
2357 {
2358         ShaderInput *subsurface_in = input("Subsurface");
2359         return (subsurface_in->link != NULL || subsurface > CLOSURE_WEIGHT_CUTOFF);
2360 }
2361
2362 void PrincipledBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2363 {
2364         if(shader->has_surface) {
2365                 ShaderInput *tangent_in = input("Tangent");
2366
2367                 if(!tangent_in->link)
2368                         attributes->add(ATTR_STD_GENERATED);
2369         }
2370
2371         ShaderNode::attributes(shader, attributes);
2372 }
2373
2374 void PrincipledBsdfNode::compile(SVMCompiler& compiler, ShaderInput *p_metallic, ShaderInput *p_subsurface, ShaderInput *p_subsurface_radius,
2375         ShaderInput *p_specular, ShaderInput *p_roughness, ShaderInput *p_specular_tint, ShaderInput *p_anisotropic,
2376         ShaderInput *p_sheen, ShaderInput *p_sheen_tint, ShaderInput *p_clearcoat, ShaderInput *p_clearcoat_roughness,
2377         ShaderInput *p_ior, ShaderInput *p_transmission, ShaderInput *p_anisotropic_rotation, ShaderInput *p_transmission_roughness)
2378 {
2379         ShaderInput *base_color_in = input("Base Color");
2380         ShaderInput *subsurface_color_in = input("Subsurface Color");
2381         ShaderInput *normal_in = input("Normal");
2382         ShaderInput *clearcoat_normal_in = input("Clearcoat Normal");
2383         ShaderInput *tangent_in = input("Tangent");
2384
2385         float3 weight = make_float3(1.0f, 1.0f, 1.0f);
2386
2387         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, weight);
2388
2389         int normal_offset = compiler.stack_assign_if_linked(normal_in);
2390         int clearcoat_normal_offset = compiler.stack_assign_if_linked(clearcoat_normal_in);
2391         int tangent_offset = compiler.stack_assign_if_linked(tangent_in);
2392         int specular_offset = compiler.stack_assign(p_specular);
2393         int roughness_offset = compiler.stack_assign(p_roughness);
2394         int specular_tint_offset = compiler.stack_assign(p_specular_tint);
2395         int anisotropic_offset = compiler.stack_assign(p_anisotropic);
2396         int sheen_offset = compiler.stack_assign(p_sheen);
2397         int sheen_tint_offset = compiler.stack_assign(p_sheen_tint);
2398         int clearcoat_offset = compiler.stack_assign(p_clearcoat);
2399         int clearcoat_roughness_offset = compiler.stack_assign(p_clearcoat_roughness);
2400         int ior_offset = compiler.stack_assign(p_ior);
2401         int transmission_offset = compiler.stack_assign(p_transmission);
2402         int transmission_roughness_offset = compiler.stack_assign(p_transmission_roughness);
2403         int anisotropic_rotation_offset = compiler.stack_assign(p_anisotropic_rotation);
2404         int subsurface_radius_offset = compiler.stack_assign(p_subsurface_radius);
2405
2406         compiler.add_node(NODE_CLOSURE_BSDF,
2407                 compiler.encode_uchar4(closure,
2408                 compiler.stack_assign(p_metallic),
2409                 compiler.stack_assign(p_subsurface),
2410                 compiler.closure_mix_weight_offset()),
2411                 __float_as_int((p_metallic) ? get_float(p_metallic->socket_type) : 0.0f),
2412                 __float_as_int((p_subsurface) ? get_float(p_subsurface->socket_type) : 0.0f));
2413
2414         compiler.add_node(normal_offset, tangent_offset,
2415                 compiler.encode_uchar4(specular_offset, roughness_offset, specular_tint_offset, anisotropic_offset),
2416                 compiler.encode_uchar4(sheen_offset, sheen_tint_offset, clearcoat_offset, clearcoat_roughness_offset));
2417
2418         compiler.add_node(compiler.encode_uchar4(ior_offset, transmission_offset, anisotropic_rotation_offset, transmission_roughness_offset),
2419                 distribution, subsurface_method, SVM_STACK_INVALID);
2420
2421         float3 bc_default = get_float3(base_color_in->socket_type);
2422
2423         compiler.add_node(((base_color_in->link) ? compiler.stack_assign(base_color_in) : SVM_STACK_INVALID),
2424                 __float_as_int(bc_default.x), __float_as_int(bc_default.y), __float_as_int(bc_default.z));
2425
2426         compiler.add_node(clearcoat_normal_offset, subsurface_radius_offset, SVM_STACK_INVALID, SVM_STACK_INVALID);
2427
2428         float3 ss_default = get_float3(subsurface_color_in->socket_type);
2429
2430         compiler.add_node(((subsurface_color_in->link) ? compiler.stack_assign(subsurface_color_in) : SVM_STACK_INVALID),
2431                 __float_as_int(ss_default.x), __float_as_int(ss_default.y), __float_as_int(ss_default.z));
2432 }
2433
2434 bool PrincipledBsdfNode::has_integrator_dependency()
2435 {
2436         ShaderInput *roughness_input = input("Roughness");
2437         return !roughness_input->link && roughness <= 1e-4f;
2438 }
2439
2440 void PrincipledBsdfNode::compile(SVMCompiler& compiler)
2441 {
2442         compile(compiler, input("Metallic"), input("Subsurface"), input("Subsurface Radius"), input("Specular"),
2443                 input("Roughness"), input("Specular Tint"), input("Anisotropic"), input("Sheen"), input("Sheen Tint"),
2444                 input("Clearcoat"), input("Clearcoat Roughness"), input("IOR"), input("Transmission"),
2445                 input("Anisotropic Rotation"), input("Transmission Roughness"));
2446 }
2447
2448 void PrincipledBsdfNode::compile(OSLCompiler& compiler)
2449 {
2450         compiler.parameter(this, "distribution");
2451         compiler.parameter(this, "subsurface_method");
2452         compiler.add(this, "node_principled_bsdf");
2453 }
2454
2455 bool PrincipledBsdfNode::has_bssrdf_bump()
2456 {
2457         return has_surface_bssrdf() && has_bump();
2458 }
2459
2460 /* Translucent BSDF Closure */
2461
2462 NODE_DEFINE(TranslucentBsdfNode)
2463 {
2464         NodeType* type = NodeType::add("translucent_bsdf", create, NodeType::SHADER);
2465
2466         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2467         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2468         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2469
2470         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2471
2472         return type;
2473 }
2474
2475 TranslucentBsdfNode::TranslucentBsdfNode()
2476 : BsdfNode(node_type)
2477 {
2478         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
2479 }
2480
2481 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
2482 {
2483         BsdfNode::compile(compiler, NULL, NULL);
2484 }
2485
2486 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
2487 {
2488         compiler.add(this, "node_translucent_bsdf");
2489 }
2490
2491 /* Transparent BSDF Closure */
2492
2493 NODE_DEFINE(TransparentBsdfNode)
2494 {
2495         NodeType* type = NodeType::add("transparent_bsdf", create, NodeType::SHADER);
2496
2497         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2498         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2499
2500         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2501
2502         return type;
2503 }
2504
2505 TransparentBsdfNode::TransparentBsdfNode()
2506 : BsdfNode(node_type)
2507 {
2508         closure = CLOSURE_BSDF_TRANSPARENT_ID;
2509 }
2510
2511 void TransparentBsdfNode::compile(SVMCompiler& compiler)
2512 {
2513         BsdfNode::compile(compiler, NULL, NULL);
2514 }
2515
2516 void TransparentBsdfNode::compile(OSLCompiler& compiler)
2517 {
2518         compiler.add(this, "node_transparent_bsdf");
2519 }
2520
2521 /* Subsurface Scattering Closure */
2522
2523 NODE_DEFINE(SubsurfaceScatteringNode)
2524 {
2525         NodeType* type = NodeType::add("subsurface_scattering", create, NodeType::SHADER);
2526
2527         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2528         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2529         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2530
2531         static NodeEnum falloff_enum;
2532         falloff_enum.insert("cubic", CLOSURE_BSSRDF_CUBIC_ID);
2533         falloff_enum.insert("gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
2534         falloff_enum.insert("burley", CLOSURE_BSSRDF_BURLEY_ID);
2535         falloff_enum.insert("random_walk", CLOSURE_BSSRDF_RANDOM_WALK_ID);
2536         SOCKET_ENUM(falloff, "Falloff", falloff_enum, CLOSURE_BSSRDF_BURLEY_ID);
2537         SOCKET_IN_FLOAT(scale, "Scale", 0.01f);
2538         SOCKET_IN_VECTOR(radius, "Radius", make_float3(0.1f, 0.1f, 0.1f));
2539         SOCKET_IN_FLOAT(sharpness, "Sharpness", 0.0f);
2540         SOCKET_IN_FLOAT(texture_blur, "Texture Blur", 1.0f);
2541
2542         SOCKET_OUT_CLOSURE(BSSRDF, "BSSRDF");
2543
2544         return type;
2545 }
2546
2547 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
2548 : BsdfNode(node_type)
2549 {
2550         closure = falloff;
2551 }
2552
2553 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
2554 {
2555         closure = falloff;
2556         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
2557 }
2558
2559 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
2560 {
2561         closure = falloff;
2562         compiler.parameter(this, "falloff");
2563         compiler.add(this, "node_subsurface_scattering");
2564 }
2565
2566 bool SubsurfaceScatteringNode::has_bssrdf_bump()
2567 {
2568         /* detect if anything is plugged into the normal input besides the default */
2569         ShaderInput *normal_in = input("Normal");
2570         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
2571 }
2572
2573 /* Emissive Closure */
2574
2575 NODE_DEFINE(EmissionNode)
2576 {
2577         NodeType* type = NodeType::add("emission", create, NodeType::SHADER);
2578
2579         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2580         SOCKET_IN_FLOAT(strength, "Strength", 10.0f);
2581         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2582
2583         SOCKET_OUT_CLOSURE(emission, "Emission");
2584
2585         return type;
2586 }
2587
2588 EmissionNode::EmissionNode()
2589 : ShaderNode(node_type)
2590 {
2591 }
2592
2593 void EmissionNode::compile(SVMCompiler& compiler)
2594 {
2595         ShaderInput *color_in = input("Color");
2596         ShaderInput *strength_in = input("Strength");
2597
2598         if(color_in->link || strength_in->link) {
2599                 compiler.add_node(NODE_EMISSION_WEIGHT,
2600                                   compiler.stack_assign(color_in),
2601                                   compiler.stack_assign(strength_in));
2602         }
2603         else
2604                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color * strength);
2605
2606         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
2607 }
2608
2609 void EmissionNode::compile(OSLCompiler& compiler)
2610 {
2611         compiler.add(this, "node_emission");
2612 }
2613
2614 void EmissionNode::constant_fold(const ConstantFolder& folder)
2615 {
2616         ShaderInput *color_in = input("Color");
2617         ShaderInput *strength_in = input("Strength");
2618
2619         if((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2620            (!strength_in->link && strength == 0.0f))
2621         {
2622                 folder.discard();
2623         }
2624 }
2625
2626 /* Background Closure */
2627
2628 NODE_DEFINE(BackgroundNode)
2629 {
2630         NodeType* type = NodeType::add("background_shader", create, NodeType::SHADER);
2631
2632         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2633         SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
2634         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2635
2636         SOCKET_OUT_CLOSURE(background, "Background");
2637
2638         return type;
2639 }
2640
2641 BackgroundNode::BackgroundNode()
2642 : ShaderNode(node_type)
2643 {
2644 }
2645
2646 void BackgroundNode::compile(SVMCompiler& compiler)
2647 {
2648         ShaderInput *color_in = input("Color");
2649         ShaderInput *strength_in = input("Strength");
2650
2651         if(color_in->link || strength_in->link) {
2652                 compiler.add_node(NODE_EMISSION_WEIGHT,
2653                                   compiler.stack_assign(color_in),
2654                                   compiler.stack_assign(strength_in));
2655         }
2656         else
2657                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color*strength);
2658
2659         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
2660 }
2661
2662 void BackgroundNode::compile(OSLCompiler& compiler)
2663 {
2664         compiler.add(this, "node_background");
2665 }
2666
2667 void BackgroundNode::constant_fold(const ConstantFolder& folder)
2668 {
2669         ShaderInput *color_in = input("Color");
2670         ShaderInput *strength_in = input("Strength");
2671
2672         if((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2673            (!strength_in->link && strength == 0.0f))
2674         {
2675                 folder.discard();
2676         }
2677 }
2678
2679 /* Holdout Closure */
2680
2681 NODE_DEFINE(HoldoutNode)
2682 {
2683         NodeType* type = NodeType::add("holdout", create, NodeType::SHADER);
2684
2685         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2686         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2687
2688         SOCKET_OUT_CLOSURE(holdout, "Holdout");
2689
2690         return type;
2691 }
2692
2693 HoldoutNode::HoldoutNode()
2694 : ShaderNode(node_type)
2695 {
2696 }
2697
2698 void HoldoutNode::compile(SVMCompiler& compiler)
2699 {
2700         float3 value = make_float3(1.0f, 1.0f, 1.0f);
2701
2702         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
2703         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
2704 }
2705
2706 void HoldoutNode::compile(OSLCompiler& compiler)
2707 {
2708         compiler.add(this, "node_holdout");
2709 }
2710
2711 /* Ambient Occlusion */
2712
2713 NODE_DEFINE(AmbientOcclusionNode)
2714 {
2715         NodeType* type = NodeType::add("ambient_occlusion", create, NodeType::SHADER);
2716
2717         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2718         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2719         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2720
2721         SOCKET_OUT_CLOSURE(AO, "AO");
2722
2723         return type;
2724 }
2725
2726 AmbientOcclusionNode::AmbientOcclusionNode()
2727 : ShaderNode(node_type)
2728 {
2729 }
2730
2731 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
2732 {
2733         ShaderInput *color_in = input("Color");
2734
2735         if(color_in->link)
2736                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2737         else
2738                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2739
2740         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
2741 }
2742
2743 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
2744 {
2745         compiler.add(this, "node_ambient_occlusion");
2746 }
2747
2748 /* Volume Closure */
2749
2750 VolumeNode::VolumeNode(const NodeType *node_type)
2751 : ShaderNode(node_type)
2752 {
2753         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2754 }
2755
2756 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
2757 {
2758         ShaderInput *color_in = input("Color");
2759
2760         if(color_in->link)
2761                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2762         else
2763                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2764         
2765         compiler.add_node(NODE_CLOSURE_VOLUME,
2766                 compiler.encode_uchar4(closure,
2767                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
2768                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
2769                         compiler.closure_mix_weight_offset()),
2770                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
2771                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
2772 }
2773
2774 void VolumeNode::compile(SVMCompiler& compiler)
2775 {
2776         compile(compiler, NULL, NULL);
2777 }
2778
2779 void VolumeNode::compile(OSLCompiler& /*compiler*/)
2780 {
2781         assert(0);
2782 }
2783
2784 /* Absorption Volume Closure */
2785
2786 NODE_DEFINE(AbsorptionVolumeNode)
2787 {
2788         NodeType* type = NodeType::add("absorption_volume", create, NodeType::SHADER);
2789
2790         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2791         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2792         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2793
2794         SOCKET_OUT_CLOSURE(volume, "Volume");
2795
2796         return type;
2797 }
2798
2799 AbsorptionVolumeNode::AbsorptionVolumeNode()
2800 : VolumeNode(node_type)
2801 {
2802         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2803 }
2804
2805 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2806 {
2807         VolumeNode::compile(compiler, input("Density"), NULL);
2808 }
2809
2810 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2811 {
2812         compiler.add(this, "node_absorption_volume");
2813 }
2814
2815 /* Scatter Volume Closure */
2816
2817 NODE_DEFINE(ScatterVolumeNode)
2818 {
2819         NodeType* type = NodeType::add("scatter_volume", create, NodeType::SHADER);
2820
2821         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2822         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2823         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
2824         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2825
2826         SOCKET_OUT_CLOSURE(volume, "Volume");
2827
2828         return type;
2829 }
2830
2831 ScatterVolumeNode::ScatterVolumeNode()
2832 : VolumeNode(node_type)
2833 {
2834         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2835 }
2836
2837 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2838 {
2839         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2840 }
2841
2842 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2843 {
2844         compiler.add(this, "node_scatter_volume");
2845 }
2846
2847 /* Hair BSDF Closure */
2848
2849 NODE_DEFINE(HairBsdfNode)
2850 {
2851         NodeType* type = NodeType::add("hair_bsdf", create, NodeType::SHADER);
2852
2853         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2854         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2855         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2856
2857         static NodeEnum component_enum;
2858         component_enum.insert("reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2859         component_enum.insert("transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2860         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_HAIR_REFLECTION_ID);
2861         SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
2862         SOCKET_IN_FLOAT(roughness_u, "RoughnessU", 0.2f);
2863         SOCKET_IN_FLOAT(roughness_v, "RoughnessV", 0.2f);
2864         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f));
2865
2866         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2867
2868         return type;
2869 }
2870
2871 HairBsdfNode::HairBsdfNode()
2872 : BsdfNode(node_type)
2873 {
2874         closure = CLOSURE_BSDF_HAIR_REFLECTION_ID;
2875 }
2876
2877 void HairBsdfNode::compile(SVMCompiler& compiler)
2878 {
2879         closure = component;
2880
2881         BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
2882 }
2883
2884 void HairBsdfNode::compile(OSLCompiler& compiler)
2885 {
2886         compiler.parameter(this, "component");
2887         compiler.add(this, "node_hair_bsdf");
2888 }
2889
2890 /* Geometry */
2891
2892 NODE_DEFINE(GeometryNode)
2893 {
2894         NodeType* type = NodeType::add("geometry", create, NodeType::SHADER);
2895
2896         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2897
2898         SOCKET_OUT_POINT(position, "Position");
2899         SOCKET_OUT_NORMAL(normal, "Normal");
2900         SOCKET_OUT_NORMAL(tangent, "Tangent");
2901         SOCKET_OUT_NORMAL(true_normal, "True Normal");
2902         SOCKET_OUT_VECTOR(incoming, "Incoming");
2903         SOCKET_OUT_POINT(parametric, "Parametric");
2904         SOCKET_OUT_FLOAT(backfacing, "Backfacing");
2905         SOCKET_OUT_FLOAT(pointiness, "Pointiness");
2906
2907         return type;
2908 }
2909
2910 GeometryNode::GeometryNode()
2911 : ShaderNode(node_type)
2912 {
2913         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2914 }
2915
2916 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2917 {
2918         if(shader->has_surface) {
2919                 if(!output("Tangent")->links.empty()) {
2920                         attributes->add(ATTR_STD_GENERATED);
2921                 }
2922                 if(!output("Pointiness")->links.empty()) {
2923                         attributes->add(ATTR_STD_POINTINESS);
2924                 }
2925         }
2926
2927         ShaderNode::attributes(shader, attributes);
2928 }
2929
2930 void GeometryNode::compile(SVMCompiler& compiler)
2931 {
2932         ShaderOutput *out;
2933         ShaderNodeType geom_node = NODE_GEOMETRY;
2934         ShaderNodeType attr_node = NODE_ATTR;
2935
2936         if(bump == SHADER_BUMP_DX) {
2937                 geom_node = NODE_GEOMETRY_BUMP_DX;
2938                 attr_node = NODE_ATTR_BUMP_DX;
2939         }
2940         else if(bump == SHADER_BUMP_DY) {
2941                 geom_node = NODE_GEOMETRY_BUMP_DY;
2942                 attr_node = NODE_ATTR_BUMP_DY;
2943         }
2944         
2945         out = output("Position");
2946         if(!out->links.empty()) {
2947                 compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
2948         }
2949
2950         out = output("Normal");
2951         if(!out->links.empty()) {
2952                 compiler.add_node(geom_node, NODE_GEOM_N, compiler.stack_assign(out));
2953         }
2954
2955         out = output("Tangent");
2956         if(!out->links.empty()) {
2957                 compiler.add_node(geom_node, NODE_GEOM_T, compiler.stack_assign(out));
2958         }
2959
2960         out = output("True Normal");
2961         if(!out->links.empty()) {
2962                 compiler.add_node(geom_node, NODE_GEOM_Ng, compiler.stack_assign(out));
2963         }
2964
2965         out = output("Incoming");
2966         if(!out->links.empty()) {
2967                 compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
2968         }
2969
2970         out = output("Parametric");
2971         if(!out->links.empty()) {
2972                 compiler.add_node(geom_node, NODE_GEOM_uv, compiler.stack_assign(out));
2973         }
2974
2975         out = output("Backfacing");
2976         if(!out->links.empty()) {
2977                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, compiler.stack_assign(out));
2978         }
2979
2980         out = output("Pointiness");
2981         if(!out->links.empty()) {
2982                 if(compiler.output_type() != SHADER_TYPE_VOLUME) {
2983                         compiler.add_node(attr_node,
2984                                           ATTR_STD_POINTINESS,
2985                                           compiler.stack_assign(out),
2986                                           NODE_ATTR_FLOAT);
2987                 }
2988                 else {
2989                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), compiler.stack_assign(out));
2990                 }
2991         }
2992 }
2993
2994 void GeometryNode::compile(OSLCompiler& compiler)
2995 {
2996         if(bump == SHADER_BUMP_DX)
2997                 compiler.parameter("bump_offset", "dx");
2998         else if(bump == SHADER_BUMP_DY)
2999                 compiler.parameter("bump_offset", "dy");
3000         else
3001                 compiler.parameter("bump_offset", "center");
3002
3003         compiler.add(this, "node_geometry");
3004 }
3005
3006 /* TextureCoordinate */
3007
3008 NODE_DEFINE(TextureCoordinateNode)
3009 {
3010         NodeType* type = NodeType::add("texture_coordinate", create, NodeType::SHADER);
3011
3012         SOCKET_BOOLEAN(from_dupli, "From Dupli", false);
3013         SOCKET_BOOLEAN(use_transform, "Use Transform", false);
3014         SOCKET_TRANSFORM(ob_tfm, "Object Transform", transform_identity());
3015
3016         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
3017
3018         SOCKET_OUT_POINT(generated, "Generated");
3019         SOCKET_OUT_NORMAL(normal, "Normal");
3020         SOCKET_OUT_POINT(UV, "UV");
3021         SOCKET_OUT_POINT(object, "Object");
3022         SOCKET_OUT_POINT(camera, "Camera");
3023         SOCKET_OUT_POINT(window, "Window");
3024         SOCKET_OUT_NORMAL(reflection, "Reflection");
3025
3026         return type;
3027 }
3028
3029 TextureCoordinateNode::TextureCoordinateNode()
3030 : ShaderNode(node_type)
3031 {
3032 }
3033
3034 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3035 {
3036         if(shader->has_surface) {
3037                 if(!from_dupli) {
3038                         if(!output("Generated")->links.empty())
3039                                 attributes->add(ATTR_STD_GENERATED);
3040                         if(!output("UV")->links.empty())
3041                                 attributes->add(ATTR_STD_UV);
3042                 }
3043         }
3044
3045         if(shader->has_volume) {
3046                 if(!from_dupli) {
3047                         if(!output("Generated")->links.empty()) {
3048                                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
3049                         }
3050                 }
3051         }
3052
3053         ShaderNode::attributes(shader, attributes);
3054 }
3055
3056 void TextureCoordinateNode::compile(SVMCompiler& compiler)
3057 {
3058         ShaderOutput *out;
3059         ShaderNodeType texco_node = NODE_TEX_COORD;
3060         ShaderNodeType attr_node = NODE_ATTR;
3061         ShaderNodeType geom_node = NODE_GEOMETRY;
3062
3063         if(bump == SHADER_BUMP_DX) {
3064                 texco_node = NODE_TEX_COORD_BUMP_DX;
3065                 attr_node = NODE_ATTR_BUMP_DX;
3066                 geom_node = NODE_GEOMETRY_BUMP_DX;
3067         }
3068         else if(bump == SHADER_BUMP_DY) {
3069                 texco_node = NODE_TEX_COORD_BUMP_DY;
3070                 attr_node = NODE_ATTR_BUMP_DY;
3071                 geom_node = NODE_GEOMETRY_BUMP_DY;
3072         }
3073         
3074         out = output("Generated");
3075         if(!out->links.empty()) {
3076                 if(compiler.background) {
3077                         compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
3078                 }
3079                 else {
3080                         if(from_dupli) {
3081                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, compiler.stack_assign(out));
3082                         }
3083                         else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
3084                                 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, compiler.stack_assign(out));
3085                         }
3086                         else {
3087                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
3088                                 compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
3089                         }
3090                 }
3091         }
3092
3093         out = output("Normal");
3094         if(!out->links.empty()) {
3095                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, compiler.stack_assign(out));
3096         }
3097
3098         out = output("UV");
3099         if(!out->links.empty()) {
3100                 if(from_dupli) {
3101                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
3102                 }
3103                 else {
3104                         int attr = compiler.attribute(ATTR_STD_UV);
3105                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
3106                 }
3107         }
3108
3109         out = output("Object");
3110         if(!out->links.empty()) {
3111                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, compiler.stack_assign(out), use_transform);
3112                 if(use_transform) {
3113                         Transform ob_itfm = transform_inverse(ob_tfm);
3114                         compiler.add_node(ob_itfm.x);
3115                         compiler.add_node(ob_itfm.y);
3116                         compiler.add_node(ob_itfm.z);
3117                         compiler.add_node(ob_itfm.w);
3118                 }
3119         }
3120
3121         out = output("Camera");
3122         if(!out->links.empty()) {
3123                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, compiler.stack_assign(out));
3124         }
3125
3126         out = output("Window");
3127         if(!out->links.empty()) {
3128                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, compiler.stack_assign(out));
3129         }
3130
3131         out = output("Reflection");
3132         if(!out->links.empty()) {
3133                 if(compiler.background) {
3134                         compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
3135                 }
3136                 else {
3137                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, compiler.stack_assign(out));
3138                 }
3139         }
3140 }
3141
3142 void TextureCoordinateNode::compile(OSLCompiler& compiler)
3143 {
3144         if(bump == SHADER_BUMP_DX)
3145                 compiler.parameter("bump_offset", "dx");
3146         else if(bump == SHADER_BUMP_DY)
3147                 compiler.parameter("bump_offset", "dy");
3148         else
3149                 compiler.parameter("bump_offset", "center");
3150         
3151         if(compiler.background)
3152                 compiler.parameter("is_background", true);
3153         if(compiler.output_type() == SHADER_TYPE_VOLUME)
3154                 compiler.parameter("is_volume", true);
3155         compiler.parameter(this, "use_transform");
3156         Transform ob_itfm = transform_transpose(transform_inverse(ob_tfm));
3157         compiler.parameter("object_itfm", ob_itfm);
3158
3159         compiler.parameter(this, "from_dupli");
3160
3161         compiler.add(this, "node_texture_coordinate");
3162 }
3163
3164 /* UV Map */
3165
3166 NODE_DEFINE(UVMapNode)
3167 {
3168         NodeType* type = NodeType::add("uvmap", create, NodeType::SHADER);
3169
3170         SOCKET_IN_STRING(attribute, "attribute", ustring(""));
3171         SOCKET_IN_BOOLEAN(from_dupli, "from dupli", false);
3172
3173         SOCKET_OUT_POINT(UV, "UV");
3174
3175         return type;
3176 }
3177
3178 UVMapNode::UVMapNode()
3179 : ShaderNode(node_type)
3180 {
3181 }
3182
3183 void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3184 {
3185         if(shader->has_surface) {
3186                 if(!from_dupli) {
3187                         if(!output("UV")->links.empty()) {
3188                                 if(attribute != "")
3189                                         attributes->add(attribute);
3190                                 else
3191                                         attributes->add(ATTR_STD_UV);
3192                         }
3193                 }
3194         }
3195
3196         ShaderNode::attributes(shader, attributes);
3197 }
3198
3199 void UVMapNode::compile(SVMCompiler& compiler)
3200 {
3201         ShaderOutput *out = output("UV");
3202         ShaderNodeType texco_node = NODE_TEX_COORD;
3203         ShaderNodeType attr_node = NODE_ATTR;
3204         int attr;
3205
3206         if(bump == SHADER_BUMP_DX) {
3207                 texco_node = NODE_TEX_COORD_BUMP_DX;
3208                 attr_node = NODE_ATTR_BUMP_DX;
3209         }
3210         else if(bump == SHADER_BUMP_DY) {
3211                 texco_node = NODE_TEX_COORD_BUMP_DY;
3212                 attr_node = NODE_ATTR_BUMP_DY;
3213         }
3214
3215         if(!out->links.empty()) {
3216                 if(from_dupli) {
3217                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
3218                 }
3219                 else {
3220                         if(attribute != "")
3221                                 attr = compiler.attribute(attribute);
3222                         else
3223                                 attr = compiler.attribute(ATTR_STD_UV);
3224
3225                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
3226                 }
3227         }
3228 }
3229
3230 void UVMapNode::compile(OSLCompiler& compiler)
3231 {
3232         if(bump == SHADER_BUMP_DX)
3233                 compiler.parameter("bump_offset", "dx");
3234         else if(bump == SHADER_BUMP_DY)
3235                 compiler.parameter("bump_offset", "dy");
3236         else
3237                 compiler.parameter("bump_offset", "center");
3238
3239         compiler.parameter(this, "from_dupli");
3240         compiler.parameter(this, "attribute");
3241         compiler.add(this, "node_uv_map");
3242 }
3243
3244 /* Light Path */
3245
3246 NODE_DEFINE(LightPathNode)
3247 {
3248         NodeType* type = NodeType::add("light_path", create, NodeType::SHADER);
3249
3250         SOCKET_OUT_FLOAT(is_camera_ray, "Is Camera Ray");
3251         SOCKET_OUT_FLOAT(is_shadow_ray, "Is Shadow Ray");
3252         SOCKET_OUT_FLOAT(is_diffuse_ray, "Is Diffuse Ray");
3253         SOCKET_OUT_FLOAT(is_glossy_ray, "Is Glossy Ray");
3254         SOCKET_OUT_FLOAT(is_singular_ray, "Is Singular Ray");
3255         SOCKET_OUT_FLOAT(is_reflection_ray, "Is Reflection Ray");
3256         SOCKET_OUT_FLOAT(is_transmission_ray, "Is Transmission Ray");
3257         SOCKET_OUT_FLOAT(is_volume_scatter_ray, "Is Volume Scatter Ray");
3258         SOCKET_OUT_FLOAT(ray_length, "Ray Length");
3259         SOCKET_OUT_FLOAT(ray_depth, "Ray Depth");
3260         SOCKET_OUT_FLOAT(diffuse_depth, "Diffuse Depth");
3261         SOCKET_OUT_FLOAT(glossy_depth, "Glossy Depth");
3262         SOCKET_OUT_FLOAT(transparent_depth, "Transparent Depth");
3263         SOCKET_OUT_FLOAT(transmission_depth, "Transmission Depth");
3264
3265         return type;
3266 }
3267
3268 LightPathNode::LightPathNode()
3269 : ShaderNode(node_type)
3270 {
3271 }
3272
3273 void LightPathNode::compile(SVMCompiler& compiler)
3274 {
3275         ShaderOutput *out;
3276
3277         out = output("Is Camera Ray");
3278         if(!out->links.empty()) {
3279                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, compiler.stack_assign(out));
3280         }
3281
3282         out = output("Is Shadow Ray");
3283         if(!out->links.empty()) {
3284                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, compiler.stack_assign(out));
3285         }
3286
3287         out = output("Is Diffuse Ray");
3288         if(!out->links.empty()) {
3289                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, compiler.stack_assign(out));
3290         }
3291
3292         out = output("Is Glossy Ray");
3293         if(!out->links.empty()) {
3294                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, compiler.stack_assign(out));
3295         }
3296
3297         out = output("Is Singular Ray");
3298         if(!out->links.empty()) {
3299                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, compiler.stack_assign(out));
3300         }
3301
3302         out = output("Is Reflection Ray");
3303         if(!out->links.empty()) {
3304                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, compiler.stack_assign(out));
3305         }
3306
3307
3308         out = output("Is Transmission Ray");
3309         if(!out->links.empty()) {
3310                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, compiler.stack_assign(out));
3311         }
3312         
3313         out = output("Is Volume Scatter Ray");
3314         if(!out->links.empty()) {
3315                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, compiler.stack_assign(out));
3316         }
3317
3318         out = output("Ray Length");
3319         if(!out->links.empty()) {
3320                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, compiler.stack_assign(out));
3321         }
3322         
3323         out = output("Ray Depth");
3324         if(!out->links.empty()) {
3325                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, compiler.stack_assign(out));
3326         }
3327
3328         out = output("Diffuse Depth");
3329         if(!out->links.empty()) {
3330                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_diffuse, compiler.stack_assign(out));
3331         }
3332
3333         out = output("Glossy Depth");
3334         if(!out->links.empty()) {
3335                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_glossy, compiler.stack_assign(out));
3336         }
3337
3338         out = output("Transparent Depth");
3339         if(!out->links.empty()) {
3340                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, compiler.stack_assign(out));
3341         }
3342
3343         out = output("Transmission Depth");
3344         if(!out->links.empty()) {
3345                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transmission, compiler.stack_assign(out));
3346         }
3347 }
3348
3349 void LightPathNode::compile(OSLCompiler& compiler)
3350 {
3351         compiler.add(this, "node_light_path");
3352 }
3353
3354 /* Light Falloff */
3355
3356 NODE_DEFINE(LightFalloffNode)
3357 {
3358         NodeType* type = NodeType::add("light_fallof", create, NodeType::SHADER);
3359
3360         SOCKET_IN_FLOAT(strength, "Strength", 100.0f);
3361         SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
3362
3363         SOCKET_OUT_FLOAT(quadratic, "Quadratic");
3364         SOCKET_OUT_FLOAT(linear, "Linear");
3365         SOCKET_OUT_FLOAT(constant, "Constant");
3366
3367         return type;
3368 }
3369
3370 LightFalloffNode::LightFalloffNode()
3371 : ShaderNode(node_type)
3372 {
3373 }
3374
3375 void LightFalloffNode::compile(SVMCompiler& compiler)
3376 {
3377         ShaderInput *strength_in = input("Strength");
3378         ShaderInput *smooth_in = input("Smooth");
3379
3380         ShaderOutput *out = output("Quadratic");
3381         if(!out->links.empty()) {
3382                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
3383                         compiler.encode_uchar4(
3384                                 compiler.stack_assign(strength_in),
3385                                 compiler.stack_assign(smooth_in),
3386                                 compiler.stack_assign(out)));
3387         }
3388
3389         out = output("Linear");
3390         if(!out->links.empty()) {
3391                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
3392                         compiler.encode_uchar4(
3393                                 compiler.stack_assign(strength_in),
3394                                 compiler.stack_assign(smooth_in),
3395                                 compiler.stack_assign(out)));
3396         }
3397
3398         out = output("Constant");
3399         if(!out->links.empty()) {
3400                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
3401                         compiler.encode_uchar4(
3402                                 compiler.stack_assign(strength_in),
3403                                 compiler.stack_assign(smooth_in),
3404                                 compiler.stack_assign(out)));
3405         }
3406 }
3407
3408 void LightFalloffNode::compile(OSLCompiler& compiler)
3409 {
3410         compiler.add(this, "node_light_falloff");
3411 }
3412
3413 /* Object Info */
3414
3415 NODE_DEFINE(ObjectInfoNode)
3416 {
3417         NodeType* type = NodeType::add("object_info", create, NodeType::SHADER);
3418
3419         SOCKET_OUT_VECTOR(location, "Location");
3420         SOCKET_OUT_FLOAT(object_index, "Object Index");
3421         SOCKET_OUT_FLOAT(material_index, "Material Index");
3422         SOCKET_OUT_FLOAT(random, "Random");
3423
3424         return type;
3425 }
3426
3427 ObjectInfoNode::ObjectInfoNode()
3428 : ShaderNode(node_type)
3429 {
3430 }
3431
3432 void ObjectInfoNode::compile(SVMCompiler& compiler)
3433 {
3434         ShaderOutput *out = output("Location");
3435         if(!out->links.empty()) {
3436                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, compiler.stack_assign(out));
3437         }
3438
3439         out = output("Object Index");
3440         if(!out->links.empty()) {
3441                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, compiler.stack_assign(out));
3442         }
3443
3444         out = output("Material Index");
3445         if(!out->links.empty()) {
3446                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, compiler.stack_assign(out));
3447         }
3448
3449         out = output("Random");
3450         if(!out->links.empty()) {
3451                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, compiler.stack_assign(out));
3452         }
3453 }
3454
3455 void ObjectInfoNode::compile(OSLCompiler& compiler)
3456 {
3457         compiler.add(this, "node_object_info");
3458 }
3459
3460 /* Particle Info */
3461
3462 NODE_DEFINE(ParticleInfoNode)
3463 {
3464         NodeType* type = NodeType::add("particle_info", create, NodeType::SHADER);
3465
3466         SOCKET_OUT_FLOAT(index, "Index");
3467         SOCKET_OUT_FLOAT(age, "Age");
3468         SOCKET_OUT_FLOAT(lifetime, "Lifetime");
3469         SOCKET_OUT_POINT(location, "Location");
3470 #if 0   /* not yet supported */
3471         SOCKET_OUT_QUATERNION(rotation, "Rotation");
3472 #endif
3473         SOCKET_OUT_FLOAT(size, "Size");
3474         SOCKET_OUT_VECTOR(velocity, "Velocity");
3475         SOCKET_OUT_VECTOR(angular_velocity, "Angular Velocity");
3476
3477         return type;
3478 }
3479
3480 ParticleInfoNode::ParticleInfoNode()
3481 : ShaderNode(node_type)
3482 {
3483 }
3484
3485 void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3486 {
3487         if(!output("Index")->links.empty())
3488                 attributes->add(ATTR_STD_PARTICLE);
3489         if(!output("Age")->links.empty())
3490                 attributes->add(ATTR_STD_PARTICLE);
3491         if(!output("Lifetime")->links.empty())
3492                 attributes->add(ATTR_STD_PARTICLE);
3493         if(!output("Location")->links.empty())
3494                 attributes->add(ATTR_STD_PARTICLE);
3495 #if 0   /* not yet supported */
3496         if(!output("Rotation")->links.empty())
3497                 attributes->add(ATTR_STD_PARTICLE);
3498 #endif
3499         if(!output("Size")->links.empty())
3500                 attributes->add(ATTR_STD_PARTICLE);
3501         if(!output("Velocity")->links.empty())
3502                 attributes->add(ATTR_STD_PARTICLE);
3503         if(!output("Angular Velocity")->links.empty())
3504                 attributes->add(ATTR_STD_PARTICLE);
3505
3506         ShaderNode::attributes(shader, attributes);
3507 }
3508
3509 void ParticleInfoNode::compile(SVMCompiler& compiler)
3510 {
3511         ShaderOutput *out;
3512         
3513         out = output("Index");
3514         if(!out->links.empty()) {
3515                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, compiler.stack_assign(out));
3516         }
3517         
3518         out = output("Age");
3519         if(!out->links.empty()) {
3520                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, compiler.stack_assign(out));
3521         }
3522         
3523         out = output("Lifetime");
3524         if(!out->links.empty()) {
3525                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, compiler.stack_assign(out));
3526         }
3527         
3528         out = output("Location");
3529         if(!out->links.empty()) {
3530                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, compiler.stack_assign(out));
3531         }
3532         
3533         /* quaternion data is not yet supported by Cycles */
3534 #if 0
3535         out = output("Rotation");
3536         if(!out->links.empty()) {
3537                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, compiler.stack_assign(out));
3538         }
3539 #endif
3540         
3541         out = output("Size");
3542         if(!out->links.empty()) {
3543                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, compiler.stack_assign(out));
3544         }
3545         
3546         out = output("Velocity");
3547         if(!out->links.empty()) {
3548                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, compiler.stack_assign(out));
3549         }
3550         
3551         out = output("Angular Velocity");
3552         if(!out->links.empty()) {
3553                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, compiler.stack_assign(out));
3554         }
3555 }
3556
3557 void ParticleInfoNode::compile(OSLCompiler& compiler)
3558 {
3559         compiler.add(this, "node_particle_info");
3560 }
3561
3562 /* Hair Info */
3563
3564 NODE_DEFINE(HairInfoNode)
3565 {
3566         NodeType* type = NodeType::add("hair_info", create, NodeType::SHADER);
3567
3568         SOCKET_OUT_FLOAT(is_strand, "Is Str