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