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