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