Fix Cycles node definition typos and incorrect enum value.
[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::compile(SVMCompiler& compiler)
1524 {
1525         ShaderInput *vector_in = input("Vector");
1526         ShaderOutput *density_out = output("Density");
1527         ShaderOutput *color_out = output("Color");
1528
1529         const bool use_density = !density_out->links.empty();
1530         const bool use_color = !color_out->links.empty();
1531
1532         image_manager = compiler.image_manager;
1533
1534         if(use_density || use_color) {
1535                 if(slot == -1) {
1536                         ImageMetaData metadata;
1537                         slot = image_manager->add_image(filename.string(), builtin_data,
1538                                                         false, 0,
1539                                                         interpolation,
1540                                                         EXTENSION_CLIP,
1541                                                         true,
1542                                                         metadata);
1543                 }
1544
1545                 if(slot != -1) {
1546                         compiler.stack_assign(vector_in);
1547                         compiler.add_node(NODE_TEX_VOXEL,
1548                                           slot,
1549                                           compiler.encode_uchar4(compiler.stack_assign(vector_in),
1550                                                                  compiler.stack_assign_if_linked(density_out),
1551                                                                  compiler.stack_assign_if_linked(color_out),
1552                                                                  space));
1553                         if(space == NODE_TEX_VOXEL_SPACE_WORLD) {
1554                                 compiler.add_node(tfm.x);
1555                                 compiler.add_node(tfm.y);
1556                                 compiler.add_node(tfm.z);
1557                         }
1558                 }
1559                 else {
1560                         if(use_density) {
1561                                 compiler.add_node(NODE_VALUE_F,
1562                                                   __float_as_int(0.0f),
1563                                                   compiler.stack_assign(density_out));
1564                         }
1565                         if(use_color) {
1566                                 compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
1567                                 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
1568                                                                             TEX_IMAGE_MISSING_G,
1569                                                                             TEX_IMAGE_MISSING_B));
1570                         }
1571                 }
1572         }
1573 }
1574
1575 void PointDensityTextureNode::compile(OSLCompiler& compiler)
1576 {
1577         ShaderOutput *density_out = output("Density");
1578         ShaderOutput *color_out = output("Color");
1579
1580         const bool use_density = !density_out->links.empty();
1581         const bool use_color = !color_out->links.empty();
1582
1583         image_manager = compiler.image_manager;
1584
1585         if(use_density || use_color) {
1586                 if(slot == -1) {
1587                         ImageMetaData metadata;
1588                         slot = image_manager->add_image(filename.string(), builtin_data,
1589                                                         false, 0,
1590                                                         interpolation,
1591                                                         EXTENSION_CLIP,
1592                                                         true,
1593                                                         metadata);
1594                 }
1595
1596                 if(slot != -1) {
1597                         compiler.parameter("filename", string_printf("@i%d", slot).c_str());
1598                 }
1599                 if(space == NODE_TEX_VOXEL_SPACE_WORLD) {
1600                         compiler.parameter("mapping", tfm);
1601                         compiler.parameter("use_mapping", 1);
1602                 }
1603                 compiler.parameter(this, "interpolation");
1604                 compiler.add(this, "node_voxel_texture");
1605         }
1606 }
1607
1608 /* Normal */
1609
1610 NODE_DEFINE(NormalNode)
1611 {
1612         NodeType* type = NodeType::add("normal", create, NodeType::SHADER);
1613
1614         SOCKET_VECTOR(direction, "direction", make_float3(0.0f, 0.0f, 0.0f));
1615
1616         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f));
1617
1618         SOCKET_OUT_NORMAL(normal, "Normal");
1619         SOCKET_OUT_FLOAT(dot, "Dot");
1620
1621         return type;
1622 }
1623
1624 NormalNode::NormalNode()
1625 : ShaderNode(node_type)
1626 {
1627 }
1628
1629 void NormalNode::compile(SVMCompiler& compiler)
1630 {
1631         ShaderInput *normal_in = input("Normal");
1632         ShaderOutput *normal_out = output("Normal");
1633         ShaderOutput *dot_out = output("Dot");
1634
1635         compiler.add_node(NODE_NORMAL,
1636                 compiler.stack_assign(normal_in),
1637                 compiler.stack_assign(normal_out),
1638                 compiler.stack_assign(dot_out));
1639         compiler.add_node(
1640                 __float_as_int(direction.x),
1641                 __float_as_int(direction.y),
1642                 __float_as_int(direction.z));
1643 }
1644
1645 void NormalNode::compile(OSLCompiler& compiler)
1646 {
1647         compiler.parameter(this, "direction");
1648         compiler.add(this, "node_normal");
1649 }
1650
1651 /* Mapping */
1652
1653 NODE_DEFINE(MappingNode)
1654 {
1655         NodeType* type = NodeType::add("mapping", create, NodeType::SHADER);
1656
1657         TEXTURE_MAPPING_DEFINE(MappingNode);
1658
1659         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
1660         SOCKET_OUT_POINT(vector, "Vector");
1661
1662         return type;
1663 }
1664
1665 MappingNode::MappingNode()
1666 : ShaderNode(node_type)
1667 {
1668 }
1669
1670 void MappingNode::compile(SVMCompiler& compiler)
1671 {
1672         ShaderInput *vector_in = input("Vector");
1673         ShaderOutput *vector_out = output("Vector");
1674
1675         tex_mapping.compile(compiler, compiler.stack_assign(vector_in), compiler.stack_assign(vector_out));
1676 }
1677
1678 void MappingNode::compile(OSLCompiler& compiler)
1679 {
1680         compiler.parameter("Matrix", tex_mapping.compute_transform());
1681         compiler.parameter_point("mapping_min", tex_mapping.min);
1682         compiler.parameter_point("mapping_max", tex_mapping.max);
1683         compiler.parameter("use_minmax", tex_mapping.use_minmax);
1684
1685         compiler.add(this, "node_mapping");
1686 }
1687
1688 /* RGBToBW */
1689
1690 NODE_DEFINE(RGBToBWNode)
1691 {
1692         NodeType* type = NodeType::add("rgb_to_bw", create, NodeType::SHADER);
1693
1694         SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
1695         SOCKET_OUT_FLOAT(val, "Val");
1696
1697         return type;
1698 }
1699
1700 RGBToBWNode::RGBToBWNode()
1701 : ShaderNode(node_type)
1702 {
1703 }
1704
1705 void RGBToBWNode::constant_fold(const ConstantFolder& folder)
1706 {
1707         if(folder.all_inputs_constant()) {
1708                 float val = folder.scene->shader_manager->linear_rgb_to_gray(color);
1709                 folder.make_constant(val);
1710         }
1711 }
1712
1713 void RGBToBWNode::compile(SVMCompiler& compiler)
1714 {
1715         compiler.add_node(NODE_CONVERT,
1716                           NODE_CONVERT_CF,
1717                           compiler.stack_assign(inputs[0]),
1718                           compiler.stack_assign(outputs[0]));
1719 }
1720
1721 void RGBToBWNode::compile(OSLCompiler& compiler)
1722 {
1723         compiler.add(this, "node_rgb_to_bw");
1724 }
1725
1726 /* Convert */
1727
1728 const NodeType* ConvertNode::node_types[ConvertNode::MAX_TYPE][ConvertNode::MAX_TYPE];
1729 bool ConvertNode::initialized = ConvertNode::register_types();
1730
1731 Node* ConvertNode::create(const NodeType *type)
1732 {
1733         return new ConvertNode(type->inputs[0].type,  type->outputs[0].type);
1734 }
1735
1736 bool ConvertNode::register_types()
1737 {
1738         const int num_types = 8;
1739         SocketType::Type types[num_types] = {SocketType::FLOAT,
1740                                              SocketType::INT,
1741                                              SocketType::COLOR,
1742                                              SocketType::VECTOR,
1743                                              SocketType::POINT,
1744                                              SocketType::NORMAL,
1745                                              SocketType::STRING,
1746                                                                                  SocketType::CLOSURE};
1747
1748         for(size_t i = 0; i < num_types; i++) {
1749                 SocketType::Type from = types[i];
1750                 ustring from_name(SocketType::type_name(from));
1751                 ustring from_value_name("value_" + from_name.string());
1752
1753                 for(size_t j = 0; j < num_types; j++) {
1754                         SocketType::Type to = types[j];
1755                         ustring to_name(SocketType::type_name(to));
1756                         ustring to_value_name("value_" + to_name.string());
1757
1758                         string node_name = "convert_" + from_name.string() + "_to_" + to_name.string();
1759                         NodeType* type = NodeType::add(node_name.c_str(), create, NodeType::SHADER);
1760
1761                         type->register_input(from_value_name, from_value_name, from,
1762                                 SOCKET_OFFSETOF(ConvertNode, value_float), SocketType::zero_default_value(),
1763                                 NULL, NULL, SocketType::LINKABLE);
1764                         type->register_output(to_value_name, to_value_name, to);
1765
1766                         assert(from < MAX_TYPE);
1767                         assert(to < MAX_TYPE);
1768
1769                         node_types[from][to] = type;
1770                 }
1771         }
1772
1773         return true;
1774 }
1775
1776 ConvertNode::ConvertNode(SocketType::Type from_, SocketType::Type to_, bool autoconvert)
1777 : ShaderNode(node_types[from_][to_])
1778 {
1779         from = from_;
1780         to = to_;
1781
1782         if(from == to)
1783                 special_type = SHADER_SPECIAL_TYPE_PROXY;
1784         else if(autoconvert)
1785                 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1786 }
1787
1788 void ConvertNode::constant_fold(const ConstantFolder& folder)
1789 {
1790         /* proxy nodes should have been removed at this point */
1791         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1792
1793         /* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
1794
1795         if(folder.all_inputs_constant()) {
1796                 if(from == SocketType::FLOAT) {
1797                         if(SocketType::is_float3(to)) {
1798                                 folder.make_constant(make_float3(value_float, value_float, value_float));
1799                         }
1800                 }
1801                 else if(SocketType::is_float3(from)) {
1802                         if(to == SocketType::FLOAT) {
1803                                 if(from == SocketType::COLOR) {
1804                                         /* color to float */
1805                                         float val = folder.scene->shader_manager->linear_rgb_to_gray(value_color);
1806                                         folder.make_constant(val);
1807                                 }
1808                                 else {
1809                                         /* vector/point/normal to float */
1810                                         folder.make_constant(average(value_vector));
1811                                 }
1812                         }
1813                         else if(SocketType::is_float3(to)) {
1814                                 folder.make_constant(value_color);
1815                         }
1816                 }
1817         }
1818         else {
1819                 ShaderInput *in = inputs[0];
1820                 ShaderNode *prev = in->link->parent;
1821
1822                 /* no-op conversion of A to B to A */
1823                 if(prev->type == node_types[to][from]) {
1824                         ShaderInput *prev_in = prev->inputs[0];
1825
1826                         if(SocketType::is_float3(from) && (to == SocketType::FLOAT || SocketType::is_float3(to)) && prev_in->link) {
1827                                 folder.bypass(prev_in->link);
1828                         }
1829                 }
1830         }
1831 }
1832
1833 void ConvertNode::compile(SVMCompiler& compiler)
1834 {
1835         /* proxy nodes should have been removed at this point */
1836         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1837
1838         ShaderInput *in = inputs[0];
1839         ShaderOutput *out = outputs[0];
1840
1841         if(from == SocketType::FLOAT) {
1842                 if(to == SocketType::INT)
1843                         /* float to int */
1844                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, compiler.stack_assign(in), compiler.stack_assign(out));
1845                 else
1846                         /* float to float3 */
1847                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, compiler.stack_assign(in), compiler.stack_assign(out));
1848         }
1849         else if(from == SocketType::INT) {
1850                 if(to == SocketType::FLOAT)
1851                         /* int to float */
1852                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, compiler.stack_assign(in), compiler.stack_assign(out));
1853                 else
1854                         /* int to vector/point/normal */
1855                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, compiler.stack_assign(in), compiler.stack_assign(out));
1856         }
1857         else if(to == SocketType::FLOAT) {
1858                 if(from == SocketType::COLOR)
1859                         /* color to float */
1860                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, compiler.stack_assign(in), compiler.stack_assign(out));
1861                 else
1862                         /* vector/point/normal to float */
1863                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, compiler.stack_assign(in), compiler.stack_assign(out));
1864         }
1865         else if(to == SocketType::INT) {
1866                 if(from == SocketType::COLOR)
1867                         /* color to int */
1868                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, compiler.stack_assign(in), compiler.stack_assign(out));
1869                 else
1870                         /* vector/point/normal to int */
1871                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, compiler.stack_assign(in), compiler.stack_assign(out));
1872         }
1873         else {
1874                 /* float3 to float3 */
1875                 if(in->link) {
1876                         /* no op in SVM */
1877                         compiler.stack_link(in, out);
1878                 }
1879                 else {
1880                         /* set 0,0,0 value */
1881                         compiler.add_node(NODE_VALUE_V, compiler.stack_assign(out));
1882                         compiler.add_node(NODE_VALUE_V, value_color);
1883                 }
1884         }
1885 }
1886
1887 void ConvertNode::compile(OSLCompiler& compiler)
1888 {
1889         /* proxy nodes should have been removed at this point */
1890         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1891
1892         if(from == SocketType::FLOAT)
1893                 compiler.add(this, "node_convert_from_float");
1894         else if(from == SocketType::INT)
1895                 compiler.add(this, "node_convert_from_int");
1896         else if(from == SocketType::COLOR)
1897                 compiler.add(this, "node_convert_from_color");
1898         else if(from == SocketType::VECTOR)
1899                 compiler.add(this, "node_convert_from_vector");
1900         else if(from == SocketType::POINT)
1901                 compiler.add(this, "node_convert_from_point");
1902         else if(from == SocketType::NORMAL)
1903                 compiler.add(this, "node_convert_from_normal");
1904         else
1905                 assert(0);
1906 }
1907
1908 /* Base type for all closure-type nodes */
1909
1910 BsdfBaseNode::BsdfBaseNode(const NodeType *node_type)
1911         : ShaderNode(node_type)
1912 {
1913         special_type = SHADER_SPECIAL_TYPE_CLOSURE;
1914 }
1915
1916 bool BsdfBaseNode::has_bump()
1917 {
1918         /* detect if anything is plugged into the normal input besides the default */
1919         ShaderInput *normal_in = input("Normal");
1920         return (normal_in && normal_in->link &&
1921                 normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
1922 }
1923
1924 /* BSDF Closure */
1925
1926 BsdfNode::BsdfNode(const NodeType *node_type)
1927 : BsdfBaseNode(node_type)
1928 {
1929 }
1930
1931 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3, ShaderInput *param4)
1932 {
1933         ShaderInput *color_in = input("Color");
1934         ShaderInput *normal_in = input("Normal");
1935         ShaderInput *tangent_in = input("Tangent");
1936
1937         if(color_in->link)
1938                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
1939         else
1940                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
1941
1942         int normal_offset = (normal_in) ? compiler.stack_assign_if_linked(normal_in) : SVM_STACK_INVALID;
1943         int tangent_offset = (tangent_in) ? compiler.stack_assign_if_linked(tangent_in) : SVM_STACK_INVALID;
1944         int param3_offset = (param3) ? compiler.stack_assign(param3) : SVM_STACK_INVALID;
1945         int param4_offset = (param4) ? compiler.stack_assign(param4) : SVM_STACK_INVALID;
1946
1947         compiler.add_node(NODE_CLOSURE_BSDF,
1948                 compiler.encode_uchar4(closure,
1949                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
1950                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
1951                         compiler.closure_mix_weight_offset()),
1952                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
1953                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
1954
1955         compiler.add_node(normal_offset, tangent_offset, param3_offset, param4_offset);
1956 }
1957
1958 void BsdfNode::compile(SVMCompiler& compiler)
1959 {
1960         compile(compiler, NULL, NULL);
1961 }
1962
1963 void BsdfNode::compile(OSLCompiler& /*compiler*/)
1964 {
1965         assert(0);
1966 }
1967
1968 /* Anisotropic BSDF Closure */
1969
1970 NODE_DEFINE(AnisotropicBsdfNode)
1971 {
1972         NodeType* type = NodeType::add("anisotropic_bsdf", create, NodeType::SHADER);
1973
1974         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
1975         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1976         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
1977
1978         static NodeEnum distribution_enum;
1979         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID);
1980         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1981         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_ANISO_ID);
1982         distribution_enum.insert("ashikhmin_shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID);
1983         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1984
1985         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
1986
1987         SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
1988         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.5f);
1989         SOCKET_IN_FLOAT(rotation, "Rotation", 0.0f);
1990
1991         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
1992
1993         return type;
1994 }
1995
1996 AnisotropicBsdfNode::AnisotropicBsdfNode()
1997 : BsdfNode(node_type)
1998 {
1999         closure = CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID;
2000 }
2001
2002 void AnisotropicBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2003 {
2004         if(shader->has_surface) {
2005                 ShaderInput *tangent_in = input("Tangent");
2006
2007                 if(!tangent_in->link)
2008                         attributes->add(ATTR_STD_GENERATED);
2009         }
2010
2011         ShaderNode::attributes(shader, attributes);
2012 }
2013
2014 void AnisotropicBsdfNode::compile(SVMCompiler& compiler)
2015 {
2016         closure = distribution;
2017
2018         if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ANISO_ID)
2019                 BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"), input("Color"));
2020         else
2021                 BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
2022 }
2023
2024 void AnisotropicBsdfNode::compile(OSLCompiler& compiler)
2025 {
2026         compiler.parameter(this, "distribution");
2027         compiler.add(this, "node_anisotropic_bsdf");
2028 }
2029
2030 /* Glossy BSDF Closure */
2031
2032 NODE_DEFINE(GlossyBsdfNode)
2033 {
2034         NodeType* type = NodeType::add("glossy_bsdf", create, NodeType::SHADER);
2035
2036         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2037         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2038         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2039
2040         static NodeEnum distribution_enum;
2041         distribution_enum.insert("sharp", CLOSURE_BSDF_REFLECTION_ID);
2042         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
2043         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
2044         distribution_enum.insert("ashikhmin_shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
2045         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID);
2046         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ID);
2047         SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
2048
2049         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2050
2051         return type;
2052 }
2053
2054 GlossyBsdfNode::GlossyBsdfNode()
2055 : BsdfNode(node_type)
2056 {
2057         closure = CLOSURE_BSDF_MICROFACET_GGX_ID;
2058         distribution_orig = NBUILTIN_CLOSURES;
2059 }
2060
2061 void GlossyBsdfNode::simplify_settings(Scene *scene)
2062 {
2063         if(distribution_orig == NBUILTIN_CLOSURES) {
2064                 roughness_orig = roughness;
2065                 distribution_orig = distribution;
2066         }
2067         else {
2068                 /* By default we use original values, so we don't worry about restoring
2069                  * defaults later one and can only do override when needed.
2070                  */
2071                 roughness = roughness_orig;
2072                 distribution = distribution_orig;
2073         }
2074         Integrator *integrator = scene->integrator;
2075         ShaderInput *roughness_input = input("Roughness");
2076         if(integrator->filter_glossy == 0.0f) {
2077                 /* Fallback to Sharp closure for Roughness close to 0.
2078                  * Note: Keep the epsilon in sync with kernel!
2079                  */
2080                 if(!roughness_input->link && roughness <= 1e-4f) {
2081                         VLOG(1) << "Using sharp glossy BSDF.";
2082                         distribution = CLOSURE_BSDF_REFLECTION_ID;
2083                 }
2084         }
2085         else {
2086                 /* If filter glossy is used we replace Sharp glossy with GGX so we can
2087                  * benefit from closure blur to remove unwanted noise.
2088                  */
2089                 if(roughness_input->link == NULL &&
2090                    distribution == CLOSURE_BSDF_REFLECTION_ID)
2091                 {
2092                         VLOG(1) << "Using GGX glossy with filter glossy.";
2093                         distribution = CLOSURE_BSDF_MICROFACET_GGX_ID;
2094                         roughness = 0.0f;
2095                 }
2096         }
2097         closure = distribution;
2098 }
2099
2100 bool GlossyBsdfNode::has_integrator_dependency()
2101 {
2102         ShaderInput *roughness_input = input("Roughness");
2103         return !roughness_input->link &&
2104                (distribution == CLOSURE_BSDF_REFLECTION_ID || roughness <= 1e-4f);
2105 }
2106
2107 void GlossyBsdfNode::compile(SVMCompiler& compiler)
2108 {
2109         closure = distribution;
2110
2111         if(closure == CLOSURE_BSDF_REFLECTION_ID)
2112                 BsdfNode::compile(compiler, NULL, NULL);
2113         else if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID)
2114                 BsdfNode::compile(compiler, input("Roughness"), NULL, input("Color"));
2115         else
2116                 BsdfNode::compile(compiler, input("Roughness"), NULL);
2117 }
2118
2119 void GlossyBsdfNode::compile(OSLCompiler& compiler)
2120 {
2121         compiler.parameter(this, "distribution");
2122         compiler.add(this, "node_glossy_bsdf");
2123 }
2124
2125 /* Glass BSDF Closure */
2126
2127 NODE_DEFINE(GlassBsdfNode)
2128 {
2129         NodeType* type = NodeType::add("glass_bsdf", create, NodeType::SHADER);
2130
2131         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2132         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2133         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2134
2135         static NodeEnum distribution_enum;
2136         distribution_enum.insert("sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
2137         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
2138         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
2139         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
2140         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
2141         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2142         SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
2143
2144         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2145
2146         return type;
2147 }
2148
2149 GlassBsdfNode::GlassBsdfNode()
2150 : BsdfNode(node_type)
2151 {
2152         closure = CLOSURE_BSDF_SHARP_GLASS_ID;
2153         distribution_orig = NBUILTIN_CLOSURES;
2154 }
2155
2156 void GlassBsdfNode::simplify_settings(Scene *scene)
2157 {
2158         if(distribution_orig == NBUILTIN_CLOSURES) {
2159                 roughness_orig = roughness;
2160                 distribution_orig = distribution;
2161         }
2162         else {
2163                 /* By default we use original values, so we don't worry about restoring
2164                  * defaults later one and can only do override when needed.
2165                  */
2166                 roughness = roughness_orig;
2167                 distribution = distribution_orig;
2168         }
2169         Integrator *integrator = scene->integrator;
2170         ShaderInput *roughness_input = input("Roughness");
2171         if(integrator->filter_glossy == 0.0f) {
2172                 /* Fallback to Sharp closure for Roughness close to 0.
2173                  * Note: Keep the epsilon in sync with kernel!
2174                  */
2175                 if(!roughness_input->link && roughness <= 1e-4f) {
2176                         VLOG(1) << "Using sharp glass BSDF.";
2177                         distribution = CLOSURE_BSDF_SHARP_GLASS_ID;
2178                 }
2179         }
2180         else {
2181                 /* If filter glossy is used we replace Sharp glossy with GGX so we can
2182                  * benefit from closure blur to remove unwanted noise.
2183                  */
2184                 if(roughness_input->link == NULL &&
2185                    distribution == CLOSURE_BSDF_SHARP_GLASS_ID)
2186                 {
2187                         VLOG(1) << "Using GGX glass with filter glossy.";
2188                         distribution = CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID;
2189                         roughness = 0.0f;
2190                 }
2191         }
2192         closure = distribution;
2193 }
2194
2195 bool GlassBsdfNode::has_integrator_dependency()
2196 {
2197         ShaderInput *roughness_input = input("Roughness");
2198         return !roughness_input->link &&
2199                (distribution == CLOSURE_BSDF_SHARP_GLASS_ID || roughness <= 1e-4f);
2200 }
2201
2202 void GlassBsdfNode::compile(SVMCompiler& compiler)
2203 {
2204         closure = distribution;
2205
2206         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
2207                 BsdfNode::compile(compiler, NULL, input("IOR"));
2208         else if(closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID)
2209                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"), input("Color"));
2210         else
2211                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2212 }
2213
2214 void GlassBsdfNode::compile(OSLCompiler& compiler)
2215 {
2216         compiler.parameter(this, "distribution");
2217         compiler.add(this, "node_glass_bsdf");
2218 }
2219
2220 /* Refraction BSDF Closure */
2221
2222 NODE_DEFINE(RefractionBsdfNode)
2223 {
2224         NodeType* type = NodeType::add("refraction_bsdf", create, NodeType::SHADER);
2225
2226         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2227         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2228         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2229
2230         static NodeEnum distribution_enum;
2231         distribution_enum.insert("sharp", CLOSURE_BSDF_REFRACTION_ID);
2232         distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
2233         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2234         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2235
2236         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2237         SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
2238
2239         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2240
2241         return type;
2242 }
2243
2244 RefractionBsdfNode::RefractionBsdfNode()
2245 : BsdfNode(node_type)
2246 {
2247         closure = CLOSURE_BSDF_REFRACTION_ID;
2248         distribution_orig = NBUILTIN_CLOSURES;
2249 }
2250
2251 void RefractionBsdfNode::simplify_settings(Scene *scene)
2252 {
2253         if(distribution_orig == NBUILTIN_CLOSURES) {
2254                 roughness_orig = roughness;
2255                 distribution_orig = distribution;
2256         }
2257         else {
2258                 /* By default we use original values, so we don't worry about restoring
2259                  * defaults later one and can only do override when needed.
2260                  */
2261                 roughness = roughness_orig;
2262                 distribution = distribution_orig;
2263         }
2264         Integrator *integrator = scene->integrator;
2265         ShaderInput *roughness_input = input("Roughness");
2266         if(integrator->filter_glossy == 0.0f) {
2267                 /* Fallback to Sharp closure for Roughness close to 0.
2268                  * Note: Keep the epsilon in sync with kernel!
2269                  */
2270                 if(!roughness_input->link && roughness <= 1e-4f) {
2271                         VLOG(1) << "Using sharp refraction BSDF.";
2272                         distribution = CLOSURE_BSDF_REFRACTION_ID;
2273                 }
2274         }
2275         else {
2276                 /* If filter glossy is used we replace Sharp glossy with GGX so we can
2277                  * benefit from closure blur to remove unwanted noise.
2278                  */
2279                 if(roughness_input->link == NULL &&
2280                    distribution == CLOSURE_BSDF_REFRACTION_ID)
2281                 {
2282                         VLOG(1) << "Using GGX refraction with filter glossy.";
2283                         distribution = CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID;
2284                         roughness = 0.0f;
2285                 }
2286         }
2287         closure = distribution;
2288 }
2289
2290 bool RefractionBsdfNode::has_integrator_dependency()
2291 {
2292         ShaderInput *roughness_input = input("Roughness");
2293         return !roughness_input->link &&
2294                (distribution == CLOSURE_BSDF_REFRACTION_ID || roughness <= 1e-4f);
2295 }
2296
2297 void RefractionBsdfNode::compile(SVMCompiler& compiler)
2298 {
2299         closure = distribution;
2300
2301         if(closure == CLOSURE_BSDF_REFRACTION_ID)
2302                 BsdfNode::compile(compiler, NULL, input("IOR"));
2303         else
2304                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2305 }
2306
2307 void RefractionBsdfNode::compile(OSLCompiler& compiler)
2308 {
2309         compiler.parameter(this, "distribution");
2310         compiler.add(this, "node_refraction_bsdf");
2311 }
2312
2313 /* Toon BSDF Closure */
2314
2315 NODE_DEFINE(ToonBsdfNode)
2316 {
2317         NodeType* type = NodeType::add("toon_bsdf", create, NodeType::SHADER);
2318
2319         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2320         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2321         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2322
2323         static NodeEnum component_enum;
2324         component_enum.insert("diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
2325         component_enum.insert("glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
2326         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_DIFFUSE_TOON_ID);
2327         SOCKET_IN_FLOAT(size, "Size", 0.5f);
2328         SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
2329
2330         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2331
2332         return type;
2333 }
2334
2335 ToonBsdfNode::ToonBsdfNode()
2336 : BsdfNode(node_type)
2337 {
2338         closure = CLOSURE_BSDF_DIFFUSE_TOON_ID;
2339 }
2340
2341 void ToonBsdfNode::compile(SVMCompiler& compiler)
2342 {
2343         closure = component;
2344
2345         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
2346 }
2347
2348 void ToonBsdfNode::compile(OSLCompiler& compiler)
2349 {
2350         compiler.parameter(this, "component");
2351         compiler.add(this, "node_toon_bsdf");
2352 }
2353
2354 /* Velvet BSDF Closure */
2355
2356 NODE_DEFINE(VelvetBsdfNode)
2357 {
2358         NodeType* type = NodeType::add("velvet_bsdf", create, NodeType::SHADER);
2359
2360         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2361         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2362         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2363         SOCKET_IN_FLOAT(sigma, "Sigma", 1.0f);
2364
2365         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2366
2367         return type;
2368 }
2369
2370 VelvetBsdfNode::VelvetBsdfNode()
2371 : BsdfNode(node_type)
2372 {
2373         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
2374 }
2375
2376 void VelvetBsdfNode::compile(SVMCompiler& compiler)
2377 {
2378         BsdfNode::compile(compiler, input("Sigma"), NULL);
2379 }
2380
2381 void VelvetBsdfNode::compile(OSLCompiler& compiler)
2382 {
2383         compiler.add(this, "node_velvet_bsdf");
2384 }
2385
2386 /* Diffuse BSDF Closure */
2387
2388 NODE_DEFINE(DiffuseBsdfNode)
2389 {
2390         NodeType* type = NodeType::add("diffuse_bsdf", create, NodeType::SHADER);
2391
2392         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2393         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2394         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2395         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2396
2397         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2398
2399         return type;
2400 }
2401
2402 DiffuseBsdfNode::DiffuseBsdfNode()
2403 : BsdfNode(node_type)
2404 {
2405         closure = CLOSURE_BSDF_DIFFUSE_ID;
2406 }
2407
2408 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
2409 {
2410         BsdfNode::compile(compiler, input("Roughness"), NULL);
2411 }
2412
2413 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
2414 {
2415         compiler.add(this, "node_diffuse_bsdf");
2416 }
2417
2418 /* Disney principled BSDF Closure */
2419 NODE_DEFINE(PrincipledBsdfNode)
2420 {
2421         NodeType* type = NodeType::add("principled_bsdf", create, NodeType::SHADER);
2422
2423         static NodeEnum distribution_enum;
2424         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
2425         distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
2426         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
2427
2428         static NodeEnum subsurface_method_enum;
2429         subsurface_method_enum.insert("burley", CLOSURE_BSSRDF_PRINCIPLED_ID);
2430         subsurface_method_enum.insert("random_walk", CLOSURE_BSSRDF_PRINCIPLED_RANDOM_WALK_ID);
2431         SOCKET_ENUM(subsurface_method, "Subsurface Method", subsurface_method_enum, CLOSURE_BSSRDF_PRINCIPLED_ID);
2432
2433         SOCKET_IN_COLOR(base_color, "Base Color", make_float3(0.8f, 0.8f, 0.8f));
2434         SOCKET_IN_COLOR(subsurface_color, "Subsurface Color", make_float3(0.8f, 0.8f, 0.8f));
2435         SOCKET_IN_FLOAT(metallic, "Metallic", 0.0f);
2436         SOCKET_IN_FLOAT(subsurface, "Subsurface", 0.0f);
2437         SOCKET_IN_VECTOR(subsurface_radius, "Subsurface Radius", make_float3(0.1f, 0.1f, 0.1f));
2438         SOCKET_IN_FLOAT(specular, "Specular", 0.0f);
2439         SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
2440         SOCKET_IN_FLOAT(specular_tint, "Specular Tint", 0.0f);
2441         SOCKET_IN_FLOAT(anisotropic, "Anisotropic", 0.0f);
2442         SOCKET_IN_FLOAT(sheen, "Sheen", 0.0f);
2443         SOCKET_IN_FLOAT(sheen_tint, "Sheen Tint", 0.0f);
2444         SOCKET_IN_FLOAT(clearcoat, "Clearcoat", 0.0f);
2445         SOCKET_IN_FLOAT(clearcoat_roughness, "Clearcoat Roughness", 0.03f);
2446         SOCKET_IN_FLOAT(ior, "IOR", 0.0f);
2447         SOCKET_IN_FLOAT(transmission, "Transmission", 0.0f);
2448         SOCKET_IN_FLOAT(transmission_roughness, "Transmission Roughness", 0.0f);
2449         SOCKET_IN_FLOAT(anisotropic_rotation, "Anisotropic Rotation", 0.0f);
2450         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2451         SOCKET_IN_NORMAL(clearcoat_normal, "Clearcoat Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2452         SOCKET_IN_NORMAL(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
2453         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2454
2455         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2456
2457         return type;
2458 }
2459
2460 PrincipledBsdfNode::PrincipledBsdfNode()
2461         : BsdfBaseNode(node_type)
2462 {
2463         closure = CLOSURE_BSDF_PRINCIPLED_ID;
2464         distribution = CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID;
2465         distribution_orig = NBUILTIN_CLOSURES;
2466 }
2467
2468 bool PrincipledBsdfNode::has_surface_bssrdf()
2469 {
2470         ShaderInput *subsurface_in = input("Subsurface");
2471         return (subsurface_in->link != NULL || subsurface > CLOSURE_WEIGHT_CUTOFF);
2472 }
2473
2474 void PrincipledBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2475 {
2476         if(shader->has_surface) {
2477                 ShaderInput *tangent_in = input("Tangent");
2478
2479                 if(!tangent_in->link)
2480                         attributes->add(ATTR_STD_GENERATED);
2481         }
2482
2483         ShaderNode::attributes(shader, attributes);
2484 }
2485
2486 void PrincipledBsdfNode::compile(SVMCompiler& compiler, ShaderInput *p_metallic, ShaderInput *p_subsurface, ShaderInput *p_subsurface_radius,
2487         ShaderInput *p_specular, ShaderInput *p_roughness, ShaderInput *p_specular_tint, ShaderInput *p_anisotropic,
2488         ShaderInput *p_sheen, ShaderInput *p_sheen_tint, ShaderInput *p_clearcoat, ShaderInput *p_clearcoat_roughness,
2489         ShaderInput *p_ior, ShaderInput *p_transmission, ShaderInput *p_anisotropic_rotation, ShaderInput *p_transmission_roughness)
2490 {
2491         ShaderInput *base_color_in = input("Base Color");
2492         ShaderInput *subsurface_color_in = input("Subsurface Color");
2493         ShaderInput *normal_in = input("Normal");
2494         ShaderInput *clearcoat_normal_in = input("Clearcoat Normal");
2495         ShaderInput *tangent_in = input("Tangent");
2496
2497         float3 weight = make_float3(1.0f, 1.0f, 1.0f);
2498
2499         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, weight);
2500
2501         int normal_offset = compiler.stack_assign_if_linked(normal_in);
2502         int clearcoat_normal_offset = compiler.stack_assign_if_linked(clearcoat_normal_in);
2503         int tangent_offset = compiler.stack_assign_if_linked(tangent_in);
2504         int specular_offset = compiler.stack_assign(p_specular);
2505         int roughness_offset = compiler.stack_assign(p_roughness);
2506         int specular_tint_offset = compiler.stack_assign(p_specular_tint);
2507         int anisotropic_offset = compiler.stack_assign(p_anisotropic);
2508         int sheen_offset = compiler.stack_assign(p_sheen);
2509         int sheen_tint_offset = compiler.stack_assign(p_sheen_tint);
2510         int clearcoat_offset = compiler.stack_assign(p_clearcoat);
2511         int clearcoat_roughness_offset = compiler.stack_assign(p_clearcoat_roughness);
2512         int ior_offset = compiler.stack_assign(p_ior);
2513         int transmission_offset = compiler.stack_assign(p_transmission);
2514         int transmission_roughness_offset = compiler.stack_assign(p_transmission_roughness);
2515         int anisotropic_rotation_offset = compiler.stack_assign(p_anisotropic_rotation);
2516         int subsurface_radius_offset = compiler.stack_assign(p_subsurface_radius);
2517
2518         compiler.add_node(NODE_CLOSURE_BSDF,
2519                 compiler.encode_uchar4(closure,
2520                 compiler.stack_assign(p_metallic),
2521                 compiler.stack_assign(p_subsurface),
2522                 compiler.closure_mix_weight_offset()),
2523                 __float_as_int((p_metallic) ? get_float(p_metallic->socket_type) : 0.0f),
2524                 __float_as_int((p_subsurface) ? get_float(p_subsurface->socket_type) : 0.0f));
2525
2526         compiler.add_node(normal_offset, tangent_offset,
2527                 compiler.encode_uchar4(specular_offset, roughness_offset, specular_tint_offset, anisotropic_offset),
2528                 compiler.encode_uchar4(sheen_offset, sheen_tint_offset, clearcoat_offset, clearcoat_roughness_offset));
2529
2530         compiler.add_node(compiler.encode_uchar4(ior_offset, transmission_offset, anisotropic_rotation_offset, transmission_roughness_offset),
2531                 distribution, subsurface_method, SVM_STACK_INVALID);
2532
2533         float3 bc_default = get_float3(base_color_in->socket_type);
2534
2535         compiler.add_node(((base_color_in->link) ? compiler.stack_assign(base_color_in) : SVM_STACK_INVALID),
2536                 __float_as_int(bc_default.x), __float_as_int(bc_default.y), __float_as_int(bc_default.z));
2537
2538         compiler.add_node(clearcoat_normal_offset, subsurface_radius_offset, SVM_STACK_INVALID, SVM_STACK_INVALID);
2539
2540         float3 ss_default = get_float3(subsurface_color_in->socket_type);
2541
2542         compiler.add_node(((subsurface_color_in->link) ? compiler.stack_assign(subsurface_color_in) : SVM_STACK_INVALID),
2543                 __float_as_int(ss_default.x), __float_as_int(ss_default.y), __float_as_int(ss_default.z));
2544 }
2545
2546 bool PrincipledBsdfNode::has_integrator_dependency()
2547 {
2548         ShaderInput *roughness_input = input("Roughness");
2549         return !roughness_input->link && roughness <= 1e-4f;
2550 }
2551
2552 void PrincipledBsdfNode::compile(SVMCompiler& compiler)
2553 {
2554         compile(compiler, input("Metallic"), input("Subsurface"), input("Subsurface Radius"), input("Specular"),
2555                 input("Roughness"), input("Specular Tint"), input("Anisotropic"), input("Sheen"), input("Sheen Tint"),
2556                 input("Clearcoat"), input("Clearcoat Roughness"), input("IOR"), input("Transmission"),
2557                 input("Anisotropic Rotation"), input("Transmission Roughness"));
2558 }
2559
2560 void PrincipledBsdfNode::compile(OSLCompiler& compiler)
2561 {
2562         compiler.parameter(this, "distribution");
2563         compiler.parameter(this, "subsurface_method");
2564         compiler.add(this, "node_principled_bsdf");
2565 }
2566
2567 bool PrincipledBsdfNode::has_bssrdf_bump()
2568 {
2569         return has_surface_bssrdf() && has_bump();
2570 }
2571
2572 /* Translucent BSDF Closure */
2573
2574 NODE_DEFINE(TranslucentBsdfNode)
2575 {
2576         NodeType* type = NodeType::add("translucent_bsdf", create, NodeType::SHADER);
2577
2578         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2579         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2580         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2581
2582         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2583
2584         return type;
2585 }
2586
2587 TranslucentBsdfNode::TranslucentBsdfNode()
2588 : BsdfNode(node_type)
2589 {
2590         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
2591 }
2592
2593 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
2594 {
2595         BsdfNode::compile(compiler, NULL, NULL);
2596 }
2597
2598 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
2599 {
2600         compiler.add(this, "node_translucent_bsdf");
2601 }
2602
2603 /* Transparent BSDF Closure */
2604
2605 NODE_DEFINE(TransparentBsdfNode)
2606 {
2607         NodeType* type = NodeType::add("transparent_bsdf", create, NodeType::SHADER);
2608
2609         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2610         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2611
2612         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2613
2614         return type;
2615 }
2616
2617 TransparentBsdfNode::TransparentBsdfNode()
2618 : BsdfNode(node_type)
2619 {
2620         closure = CLOSURE_BSDF_TRANSPARENT_ID;
2621 }
2622
2623 void TransparentBsdfNode::compile(SVMCompiler& compiler)
2624 {
2625         BsdfNode::compile(compiler, NULL, NULL);
2626 }
2627
2628 void TransparentBsdfNode::compile(OSLCompiler& compiler)
2629 {
2630         compiler.add(this, "node_transparent_bsdf");
2631 }
2632
2633 /* Subsurface Scattering Closure */
2634
2635 NODE_DEFINE(SubsurfaceScatteringNode)
2636 {
2637         NodeType* type = NodeType::add("subsurface_scattering", create, NodeType::SHADER);
2638
2639         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2640         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2641         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2642
2643         static NodeEnum falloff_enum;
2644         falloff_enum.insert("cubic", CLOSURE_BSSRDF_CUBIC_ID);
2645         falloff_enum.insert("gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
2646         falloff_enum.insert("burley", CLOSURE_BSSRDF_BURLEY_ID);
2647         falloff_enum.insert("random_walk", CLOSURE_BSSRDF_RANDOM_WALK_ID);
2648         SOCKET_ENUM(falloff, "Falloff", falloff_enum, CLOSURE_BSSRDF_BURLEY_ID);
2649         SOCKET_IN_FLOAT(scale, "Scale", 0.01f);
2650         SOCKET_IN_VECTOR(radius, "Radius", make_float3(0.1f, 0.1f, 0.1f));
2651         SOCKET_IN_FLOAT(sharpness, "Sharpness", 0.0f);
2652         SOCKET_IN_FLOAT(texture_blur, "Texture Blur", 1.0f);
2653
2654         SOCKET_OUT_CLOSURE(BSSRDF, "BSSRDF");
2655
2656         return type;
2657 }
2658
2659 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
2660 : BsdfNode(node_type)
2661 {
2662         closure = falloff;
2663 }
2664
2665 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
2666 {
2667         closure = falloff;
2668         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
2669 }
2670
2671 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
2672 {
2673         closure = falloff;
2674         compiler.parameter(this, "falloff");
2675         compiler.add(this, "node_subsurface_scattering");
2676 }
2677
2678 bool SubsurfaceScatteringNode::has_bssrdf_bump()
2679 {
2680         /* detect if anything is plugged into the normal input besides the default */
2681         ShaderInput *normal_in = input("Normal");
2682         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
2683 }
2684
2685 /* Emissive Closure */
2686
2687 NODE_DEFINE(EmissionNode)
2688 {
2689         NodeType* type = NodeType::add("emission", create, NodeType::SHADER);
2690
2691         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2692         SOCKET_IN_FLOAT(strength, "Strength", 10.0f);
2693         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2694
2695         SOCKET_OUT_CLOSURE(emission, "Emission");
2696
2697         return type;
2698 }
2699
2700 EmissionNode::EmissionNode()
2701 : ShaderNode(node_type)
2702 {
2703 }
2704
2705 void EmissionNode::compile(SVMCompiler& compiler)
2706 {
2707         ShaderInput *color_in = input("Color");
2708         ShaderInput *strength_in = input("Strength");
2709
2710         if(color_in->link || strength_in->link) {
2711                 compiler.add_node(NODE_EMISSION_WEIGHT,
2712                                   compiler.stack_assign(color_in),
2713                                   compiler.stack_assign(strength_in));
2714         }
2715         else
2716                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color * strength);
2717
2718         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
2719 }
2720
2721 void EmissionNode::compile(OSLCompiler& compiler)
2722 {
2723         compiler.add(this, "node_emission");
2724 }
2725
2726 void EmissionNode::constant_fold(const ConstantFolder& folder)
2727 {
2728         ShaderInput *color_in = input("Color");
2729         ShaderInput *strength_in = input("Strength");
2730
2731         if((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2732            (!strength_in->link && strength == 0.0f))
2733         {
2734                 folder.discard();
2735         }
2736 }
2737
2738 /* Background Closure */
2739
2740 NODE_DEFINE(BackgroundNode)
2741 {
2742         NodeType* type = NodeType::add("background_shader", create, NodeType::SHADER);
2743
2744         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2745         SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
2746         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2747
2748         SOCKET_OUT_CLOSURE(background, "Background");
2749
2750         return type;
2751 }
2752
2753 BackgroundNode::BackgroundNode()
2754 : ShaderNode(node_type)
2755 {
2756 }
2757
2758 void BackgroundNode::compile(SVMCompiler& compiler)
2759 {
2760         ShaderInput *color_in = input("Color");
2761         ShaderInput *strength_in = input("Strength");
2762
2763         if(color_in->link || strength_in->link) {
2764                 compiler.add_node(NODE_EMISSION_WEIGHT,
2765                                   compiler.stack_assign(color_in),
2766                                   compiler.stack_assign(strength_in));
2767         }
2768         else
2769                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color*strength);
2770
2771         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
2772 }
2773
2774 void BackgroundNode::compile(OSLCompiler& compiler)
2775 {
2776         compiler.add(this, "node_background");
2777 }
2778
2779 void BackgroundNode::constant_fold(const ConstantFolder& folder)
2780 {
2781         ShaderInput *color_in = input("Color");
2782         ShaderInput *strength_in = input("Strength");
2783
2784         if((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2785            (!strength_in->link && strength == 0.0f))
2786         {
2787                 folder.discard();
2788         }
2789 }
2790
2791 /* Holdout Closure */
2792
2793 NODE_DEFINE(HoldoutNode)
2794 {
2795         NodeType* type = NodeType::add("holdout", create, NodeType::SHADER);
2796
2797         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2798         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2799
2800         SOCKET_OUT_CLOSURE(holdout, "Holdout");
2801
2802         return type;
2803 }
2804
2805 HoldoutNode::HoldoutNode()
2806 : ShaderNode(node_type)
2807 {
2808 }
2809
2810 void HoldoutNode::compile(SVMCompiler& compiler)
2811 {
2812         float3 value = make_float3(1.0f, 1.0f, 1.0f);
2813
2814         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
2815         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
2816 }
2817
2818 void HoldoutNode::compile(OSLCompiler& compiler)
2819 {
2820         compiler.add(this, "node_holdout");
2821 }
2822
2823 /* Ambient Occlusion */
2824
2825 NODE_DEFINE(AmbientOcclusionNode)
2826 {
2827         NodeType* type = NodeType::add("ambient_occlusion", create, NodeType::SHADER);
2828
2829         SOCKET_INT(samples, "Samples", 16);
2830
2831         SOCKET_IN_COLOR(color, "Color", make_float3(1.0f, 1.0f, 1.0f));
2832         SOCKET_IN_FLOAT(distance, "Distance", 1.0f);
2833         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2834
2835         SOCKET_BOOLEAN(inside, "Inside", false);
2836         SOCKET_BOOLEAN(only_local, "Only Local", false);
2837
2838         SOCKET_OUT_COLOR(color, "Color");
2839         SOCKET_OUT_FLOAT(ao, "AO");
2840
2841         return type;
2842 }
2843
2844 AmbientOcclusionNode::AmbientOcclusionNode()
2845 : ShaderNode(node_type)
2846 {
2847 }
2848
2849 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
2850 {
2851         ShaderInput *color_in = input("Color");
2852         ShaderInput *distance_in = input("Distance");
2853         ShaderInput *normal_in = input("Normal");
2854         ShaderOutput *color_out = output("Color");
2855         ShaderOutput *ao_out = output("AO");
2856
2857         int flags = (inside? NODE_AO_INSIDE : 0) | (only_local? NODE_AO_ONLY_LOCAL : 0);
2858
2859         if(!distance_in->link && distance == 0.0f) {
2860                 flags |= NODE_AO_GLOBAL_RADIUS;
2861         }
2862
2863         compiler.add_node(NODE_AMBIENT_OCCLUSION,
2864                 compiler.encode_uchar4(flags,
2865                                        compiler.stack_assign_if_linked(distance_in),
2866                                        compiler.stack_assign_if_linked(normal_in),
2867                                        compiler.stack_assign(ao_out)),
2868                 compiler.encode_uchar4(compiler.stack_assign(color_in),
2869                                        compiler.stack_assign(color_out),
2870                                        samples),
2871                 __float_as_uint(distance));
2872 }
2873
2874 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
2875 {
2876         compiler.parameter(this, "samples");
2877         compiler.parameter(this, "inside");
2878         compiler.parameter(this, "only_local");
2879         compiler.add(this, "node_ambient_occlusion");
2880 }
2881
2882 /* Volume Closure */
2883
2884 VolumeNode::VolumeNode(const NodeType *node_type)
2885 : ShaderNode(node_type)
2886 {
2887         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2888 }
2889
2890 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
2891 {
2892         ShaderInput *color_in = input("Color");
2893
2894         if(color_in->link)
2895                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2896         else
2897                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2898
2899         compiler.add_node(NODE_CLOSURE_VOLUME,
2900                 compiler.encode_uchar4(closure,
2901                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
2902                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
2903                         compiler.closure_mix_weight_offset()),
2904                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
2905                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
2906 }
2907
2908 void VolumeNode::compile(SVMCompiler& compiler)
2909 {
2910         compile(compiler, NULL, NULL);
2911 }
2912
2913 void VolumeNode::compile(OSLCompiler& /*compiler*/)
2914 {
2915         assert(0);
2916 }
2917
2918 /* Absorption Volume Closure */
2919
2920 NODE_DEFINE(AbsorptionVolumeNode)
2921 {
2922         NodeType* type = NodeType::add("absorption_volume", create, NodeType::SHADER);
2923
2924         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2925         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2926         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2927
2928         SOCKET_OUT_CLOSURE(volume, "Volume");
2929
2930         return type;
2931 }
2932
2933 AbsorptionVolumeNode::AbsorptionVolumeNode()
2934 : VolumeNode(node_type)
2935 {
2936         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2937 }
2938
2939 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2940 {
2941         VolumeNode::compile(compiler, input("Density"), NULL);
2942 }
2943
2944 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2945 {
2946         compiler.add(this, "node_absorption_volume");
2947 }
2948
2949 /* Scatter Volume Closure */
2950
2951 NODE_DEFINE(ScatterVolumeNode)
2952 {
2953         NodeType* type = NodeType::add("scatter_volume", create, NodeType::SHADER);
2954
2955         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2956         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2957         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
2958         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2959
2960         SOCKET_OUT_CLOSURE(volume, "Volume");
2961
2962         return type;
2963 }
2964
2965 ScatterVolumeNode::ScatterVolumeNode()
2966 : VolumeNode(node_type)
2967 {
2968         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2969 }
2970
2971 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2972 {
2973         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2974 }
2975
2976 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2977 {
2978         compiler.add(this, "node_scatter_volume");
2979 }
2980
2981 /* Principled Volume Closure */
2982
2983 NODE_DEFINE(PrincipledVolumeNode)
2984 {
2985         NodeType* type = NodeType::add("principled_volume", create, NodeType::SHADER);
2986
2987         SOCKET_IN_STRING(density_attribute, "Density Attribute", ustring());
2988         SOCKET_IN_STRING(color_attribute, "Color Attribute", ustring());
2989         SOCKET_IN_STRING(temperature_attribute, "Temperature Attribute", ustring());
2990
2991         SOCKET_IN_COLOR(color, "Color", make_float3(0.5f, 0.5f, 0.5f));
2992         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2993         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
2994         SOCKET_IN_COLOR(absorption_color, "Absorption Color", make_float3(0.0f, 0.0f, 0.0f));
2995         SOCKET_IN_FLOAT(emission_strength, "Emission Strength", 0.0f);
2996         SOCKET_IN_COLOR(emission_color, "Emission Color", make_float3(1.0f, 1.0f, 1.0f));
2997         SOCKET_IN_FLOAT(blackbody_intensity, "Blackbody Intensity", 0.0f);
2998         SOCKET_IN_COLOR(blackbody_tint, "Blackbody Tint", make_float3(1.0f, 1.0f, 1.0f));
2999         SOCKET_IN_FLOAT(temperature, "Temperature", 1500.0f);
3000         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
3001
3002         SOCKET_OUT_CLOSURE(volume, "Volume");
3003
3004         return type;
3005 }
3006
3007 PrincipledVolumeNode::PrincipledVolumeNode()
3008 : VolumeNode(node_type)
3009 {
3010         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
3011 }
3012
3013 void PrincipledVolumeNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3014 {
3015         if(shader->has_volume) {
3016                 ShaderInput *density_in = input("Density");
3017                 ShaderInput *blackbody_in = input("Blackbody Intensity");
3018
3019                 if(density_in->link || density > 0.0f) {
3020                         attributes->add_standard(density_attribute);
3021                         attributes->add_standard(color_attribute);
3022                 }
3023
3024                 if(blackbody_in->link || blackbody_intensity > 0.0f) {
3025                         attributes->add_standard(temperature_attribute);
3026                 }
3027
3028                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
3029         }
3030
3031         ShaderNode::attributes(shader, attributes);
3032 }
3033
3034 void PrincipledVolumeNode::compile(SVMCompiler& compiler)
3035 {
3036         ShaderInput *color_in = input("Color");
3037         ShaderInput *density_in = input("Density");
3038         ShaderInput *anisotropy_in = input("Anisotropy");
3039         ShaderInput *absorption_color_in = input("Absorption Color");
3040         ShaderInput *emission_in = input("Emission Strength");
3041         ShaderInput *emission_color_in = input("Emission Color");
3042         ShaderInput *blackbody_in = input("Blackbody Intensity");
3043         ShaderInput *blackbody_tint_in = input("Blackbody Tint");
3044         ShaderInput *temperature_in = input("Temperature");
3045
3046         if(color_in->link)
3047                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
3048         else
3049                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
3050
3051         compiler.add_node(NODE_PRINCIPLED_VOLUME,
3052                 compiler.encode_uchar4(
3053                         compiler.stack_assign_if_linked(density_in),
3054                         compiler.stack_assign_if_linked(anisotropy_in),
3055                         compiler.stack_assign(absorption_color_in),
3056                         compiler.closure_mix_weight_offset()),
3057                 compiler.encode_uchar4(
3058                         compiler.stack_assign_if_linked(emission_in),
3059                         compiler.stack_assign(emission_color_in),
3060                         compiler.stack_assign_if_linked(blackbody_in),
3061                         compiler.stack_assign(temperature_in)),
3062                 compiler.stack_assign(blackbody_tint_in));
3063
3064         int attr_density = compiler.attribute_standard(density_attribute);
3065         int attr_color = compiler.attribute_standard(color_attribute);
3066         int attr_temperature = compiler.attribute_standard(temperature_attribute);
3067
3068         compiler.add_node(
3069                 __float_as_int(density),
3070                 __float_as_int(anisotropy),
3071                 __float_as_int(emission_strength),
3072                 __float_as_int(blackbody_intensity));
3073
3074         compiler.add_node(
3075                 attr_density,
3076                 attr_color,
3077                 attr_temperature);
3078 }
3079
3080 void PrincipledVolumeNode::compile(OSLCompiler& compiler)
3081 {
3082         if(Attribute::name_standard(density_attribute.c_str())) {
3083                 density_attribute = ustring("geom:" + density_attribute.string());
3084         }
3085         if(Attribute::name_standard(color_attribute.c_str())) {
3086                 color_attribute = ustring("geom:" + color_attribute.string());
3087         }
3088         if(Attribute::name_standard(temperature_attribute.c_str())) {
3089                 temperature_attribute = ustring("geom:" + temperature_attribute.string());
3090         }
3091
3092         compiler.add(this, "node_principled_volume");
3093 }
3094
3095 /* Principled Hair BSDF Closure */
3096
3097 NODE_DEFINE(PrincipledHairBsdfNode)
3098 {
3099         NodeType* type = NodeType::add("principled_hair_bsdf", create, NodeType::SHADER);
3100
3101         /* Color parametrization specified as enum. */
3102         static NodeEnum parametrization_enum;
3103         parametrization_enum.insert("Direct coloring", NODE_PRINCIPLED_HAIR_REFLECTANCE);
3104         parametrization_enum.insert("Melanin concentration", NODE_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION);
3105         parametrization_enum.insert("Absorption coefficient", NODE_PRINCIPLED_HAIR_DIRECT_ABSORPTION);
3106         SOCKET_ENUM(parametrization, "Parametrization", parametrization_enum, NODE_PRINCIPLED_HAIR_REFLECTANCE);
3107
3108         /* Initialize sockets to their default values. */
3109         SOCKET_IN_COLOR(color, "Color", make_float3(0.017513f, 0.005763f, 0.002059f));
3110         SOCKET_IN_FLOAT(melanin, "Melanin", 0.8f);
3111         SOCKET_IN_FLOAT(melanin_redness, "Melanin Redness", 1.0f);
3112         SOCKET_IN_COLOR(tint, "Tint", make_float3(1.f, 1.f, 1.f));
3113         SOCKET_IN_VECTOR(absorption_coefficient, "Absorption Coefficient", make_float3(0.245531f, 0.52f, 1.365f), SocketType::VECTOR);
3114
3115         SOCKET_IN_FLOAT(offset, "Offset", 2.f*M_PI_F/180.f);
3116         SOCKET_IN_FLOAT(roughness, "Roughness", 0.3f);
3117         SOCKET_IN_FLOAT(radial_roughness, "Radial Roughness", 0.3f);
3118         SOCKET_IN_FLOAT(coat, "Coat", 0.0f);
3119         SOCKET_IN_FLOAT(ior, "IOR", 1.55f);
3120
3121         SOCKET_IN_FLOAT(random_roughness, "Random Roughness", 0.0f);
3122         SOCKET_IN_FLOAT(random_color, "Random Color", 0.0f);
3123         SOCKET_IN_FLOAT(random, "Random", 0.0f);
3124
3125         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
3126         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
3127
3128         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
3129
3130         return type;
3131 }
3132
3133 PrincipledHairBsdfNode::PrincipledHairBsdfNode()
3134 : BsdfBaseNode(node_type)
3135 {
3136         closure = CLOSURE_BSDF_HAIR_PRINCIPLED_ID;
3137 }
3138
3139 /* Enable retrieving Hair Info -> Random if Random isn't linked. */
3140 void PrincipledHairBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3141 {
3142         if(!input("Random")->link) {
3143                 attributes->add(ATTR_STD_CURVE_RANDOM);
3144         }
3145         ShaderNode::attributes(shader, attributes);
3146 }
3147
3148 /* Prepares the input data for the SVM shader. */
3149 void PrincipledHairBsdfNode::compile(SVMCompiler& compiler)
3150 {
3151         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, make_float3(1.0f, 1.0f, 1.0f));
3152
3153         ShaderInput *roughness_in = input("Roughness");
3154         ShaderInput *radial_roughness_in = input("Radial Roughness");
3155         ShaderInput *random_roughness_in = input("Random Roughness");
3156         ShaderInput *offset_in = input("Offset");
3157         ShaderInput *coat_in = input("Coat");
3158         ShaderInput *ior_in = input("IOR");
3159         ShaderInput *melanin_in =  input("Melanin");
3160         ShaderInput *melanin_redness_in = input("Melanin Redness");
3161         ShaderInput *random_color_in = input("Random Color");
3162
3163         int color_ofs = compiler.stack_assign(input("Color"));
3164         int tint_ofs = compiler.stack_assign(input("Tint"));
3165         int absorption_coefficient_ofs = compiler.stack_assign(input("Absorption Coefficient"));
3166
3167         ShaderInput *random_in = input("Random");
3168         int attr_random = random_in->link ? SVM_STACK_INVALID : compiler.attribute(ATTR_STD_CURVE_RANDOM);
3169
3170         /* Encode all parameters into data nodes. */
3171         compiler.add_node(NODE_CLOSURE_BSDF,
3172                 /* Socket IDs can be packed 4 at a time into a single data packet */
3173                 compiler.encode_uchar4(closure,
3174                         compiler.stack_assign_if_linked(roughness_in),
3175                         compiler.stack_assign_if_linked(radial_roughness_in),
3176                         compiler.closure_mix_weight_offset()),
3177                 /* The rest are stored as unsigned integers */
3178                 __float_as_uint(roughness),
3179                 __float_as_uint(radial_roughness));
3180
3181         compiler.add_node(compiler.stack_assign_if_linked(input("Normal")),
3182                 compiler.encode_uchar4(
3183                         compiler.stack_assign_if_linked(offset_in),
3184                         compiler.stack_assign_if_linked(ior_in),
3185                         color_ofs,
3186                         parametrization),
3187                 __float_as_uint(offset),
3188                 __float_as_uint(ior));
3189
3190         compiler.add_node(
3191                 compiler.encode_uchar4(
3192                         compiler.stack_assign_if_linked(coat_in),
3193                         compiler.stack_assign_if_linked(melanin_in),
3194                         compiler.stack_assign_if_linked(melanin_redness_in),
3195                         absorption_coefficient_ofs),
3196                 __float_as_uint(coat),
3197                 __float_as_uint(melanin),
3198                 __float_as_uint(melanin_redness));
3199
3200         compiler.add_node(
3201                 compiler.encode_uchar4(
3202                         tint_ofs,
3203                         compiler.stack_assign_if_linked(random_in),
3204                         compiler.stack_assign_if_linked(random_color_in),
3205                         compiler.stack_assign_if_linked(random_roughness_in)),
3206                 __float_as_uint(random),
3207                 __float_as_uint(random_color),
3208                 __float_as_uint(random_roughness));
3209
3210         compiler.add_node(
3211                 compiler.encode_uchar4(
3212                         SVM_STACK_INVALID,
3213                         SVM_STACK_INVALID,
3214                         SVM_STACK_INVALID,
3215                         SVM_STACK_INVALID),
3216                 attr_random,
3217                 SVM_STACK_INVALID,
3218                 SVM_STACK_INVALID);
3219 }
3220
3221 /* Prepares the input data for the OSL shader. */
3222 void PrincipledHairBsdfNode::compile(OSLCompiler& compiler)
3223 {
3224         compiler.parameter(this, "parametrization");
3225         compiler.add(this, "node_principled_hair_bsdf");
3226 }
3227
3228 /* Hair BSDF Closure */
3229
3230 NODE_DEFINE(HairBsdfNode)
3231 {
3232         NodeType* type = NodeType::add("hair_bsdf", create, NodeType::SHADER);
3233
3234         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
3235         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
3236         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
3237
3238         static NodeEnum component_enum;
3239         component_enum.insert("reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
3240         component_enum.insert("transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
3241         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_HAIR_REFLECTION_ID);
3242         SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
3243         SOCKET_IN_FLOAT(roughness_u, "RoughnessU", 0.2f);
3244         SOCKET_IN_FLOAT(roughness_v, "RoughnessV", 0.2f);
3245         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f));
3246
3247         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
3248
3249         return type;
3250 }
3251
3252 HairBsdfNode::HairBsdfNode()
3253 : BsdfNode(node_type)
3254 {
3255         closure = CLOSURE_BSDF_HAIR_REFLECTION_ID;
3256 }
3257
3258 void HairBsdfNode::compile(SVMCompiler& compiler)
3259 {
3260         closure = component;
3261
3262         BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
3263 }
3264
3265 void HairBsdfNode::compile(OSLCompiler& compiler)
3266 {
3267         compiler.parameter(this, "component");
3268         compiler.add(this, "node_hair_bsdf");
3269 }
3270
3271 /* Geometry */
3272
3273 NODE_DEFINE(GeometryNode)
3274 {
3275         NodeType* type = NodeType::add("geometry", create, NodeType::SHADER);
3276
3277         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
3278
3279         SOCKET_OUT_POINT(position, "Position");
3280         SOCKET_OUT_NORMAL(normal, "Normal");
3281         SOCKET_OUT_NORMAL(tangent, "Tangent");
3282         SOCKET_OUT_NORMAL(true_normal, "True Normal");
3283         SOCKET_OUT_VECTOR(incoming, "Incoming");
3284         SOCKET_OUT_POINT(parametric, "Parametric");
3285         SOCKET_OUT_FLOAT(backfacing, "Backfacing");
3286         SOCKET_OUT_FLOAT(pointiness, "Pointiness");
3287
3288         return type;
3289 }
3290
3291 GeometryNode::GeometryNode()
3292 : ShaderNode(node_type)
3293 {
3294         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
3295 }
3296
3297 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3298 {
3299         if(shader->has_surface) {
3300                 if(!output("Tangent")->links.empty()) {
3301                         attributes->add(ATTR_STD_GENERATED);
3302                 }
3303                 if(!output("Pointiness")->links.empty()) {
3304                         attributes->add(ATTR_STD_POINTINESS);
3305                 }
3306         }
3307
3308         ShaderNode::attributes(shader, attributes);
3309 }
3310
3311 void GeometryNode::compile(SVMCompiler& compiler)
3312 {
3313         ShaderOutput *out;
3314         ShaderNodeType geom_node = NODE_GEOMETRY;
3315         ShaderNodeType attr_node = NODE_ATTR;
3316
3317         if(bump == SHADER_BUMP_DX) {
3318                 geom_node = NODE_GEOMETRY_BUMP_DX;
3319                 attr_node = NODE_ATTR_BUMP_DX;
3320         }
3321         else if(bump == SHADER_BUMP_DY) {
3322                 geom_node = NODE_GEOMETRY_BUMP_DY;
3323                 attr_node = NODE_ATTR_BUMP_DY;
3324         }
3325
3326         out = output("Position");
3327         if(!out->links.empty()) {
3328                 compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
3329         }
3330
3331         out = output("Normal");
3332         if(!out->links.empty()) {
3333                 compiler.add_node(geom_node, NODE_GEOM_N, compiler.stack_assign(out));
3334         }
3335
3336         out = output("Tangent");
3337         if(!out->links.empty()) {
3338                 compiler.add_node(geom_node, NODE_GEOM_T, compiler.stack_assign(out));
3339         }
3340
3341         out = output("True Normal");
3342         if(!out->links.empty()) {
3343                 compiler.add_node(geom_node, NODE_GEOM_Ng, compiler.stack_assign(out));
3344         }
3345
3346         out = output("Incoming");
3347         if(!out->links.empty()) {
3348                 compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
3349         }
3350
3351         out = output("Parametric");
3352         if(!out->links.empty()) {
3353                 compiler.add_node(geom_node, NODE_GEOM_uv, compiler.stack_assign(out));
3354         }
3355
3356         out = output("Backfacing");
3357         if(!out->links.empty()) {
3358                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, compiler.stack_assign(out));
3359         }
3360
3361         out = output("Pointiness");
3362         if(!out->links.empty()) {
3363                 if(compiler.output_type() != SHADER_TYPE_VOLUME) {
3364                         compiler.add_node(attr_node,
3365                                           ATTR_STD_POINTINESS,
3366                                           compiler.stack_assign(out),
3367                                           NODE_ATTR_FLOAT);
3368                 }
3369                 else {
3370                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), compiler.stack_assign(out));
3371                 }
3372         }
3373 }
3374
3375 void GeometryNode::compile(OSLCompiler& compiler)
3376 {
3377         if(bump == SHADER_BUMP_DX)
3378                 compiler.parameter("bump_offset", "dx");
3379         else if(bump == SHADER_BUMP_DY)
3380                 compiler.parameter("bump_offset", "dy");
3381         else
3382                 compiler.parameter("bump_offset", "center");
3383
3384         compiler.add(this, "node_geometry");
3385 }
3386
3387 /* TextureCoordinate */
3388
3389 NODE_DEFINE(TextureCoordinateNode)
3390 {
3391         NodeType* type = NodeType::add("texture_coordinate", create, NodeType::SHADER);
3392
3393         SOCKET_BOOLEAN(from_dupli, "From Dupli", false);
3394         SOCKET_BOOLEAN(use_transform, "Use Transform", false);
3395         SOCKET_TRANSFORM(ob_tfm, "Object Transform", transform_identity());
3396
3397         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
3398
3399         SOCKET_OUT_POINT(generated, "Generated");
3400         SOCKET_OUT_NORMAL(normal, "Normal");
3401         SOCKET_OUT_POINT(UV, "UV");
3402         SOCKET_OUT_POINT(object, "Object");
3403         SOCKET_OUT_POINT(camera, "Camera");
3404         SOCKET_OUT_POINT(window, "Window");
3405         SOCKET_OUT_NORMAL(reflection, "Reflection");
3406
3407         return type;
3408 }
3409
3410 TextureCoordinateNode::TextureCoordinateNode()
3411 : ShaderNode(node_type)
3412 {
3413 }
3414
3415 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3416 {
3417         if(shader->has_surface) {
3418                 if(!from_dupli) {
3419                         if(!output("Generated")->links.empty())
3420                                 attributes->add(ATTR_STD_GENERATED);
3421                         if(!output("UV")->links.empty())
3422                                 attributes->add(ATTR_STD_UV);
3423                 }
3424         }
3425
3426         if(shader->has_volume) {
3427                 if(!from_dupli) {
3428                         if(!output("Generated")->links.empty()) {
3429                                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
3430                         }
3431                 }
3432         }
3433
3434         ShaderNode::attributes(shader, attributes);
3435 }
3436
3437 void TextureCoordinateNode::compile(SVMCompiler& compiler)
3438 {
3439         ShaderOutput *out;
3440         ShaderNodeType texco_node = NODE_TEX_COORD;
3441         ShaderNodeType attr_node = NODE_ATTR;
3442         ShaderNodeType geom_node = NODE_GEOMETRY;
3443
3444         if(bump == SHADER_BUMP_DX) {
3445                 texco_node = NODE_TEX_COORD_BUMP_DX;
3446                 attr_node = NODE_ATTR_BUMP_DX;
3447                 geom_node = NODE_GEOMETRY_BUMP_DX;
3448         }
3449         else if(bump == SHADER_BUMP_DY) {
3450                 texco_node = NODE_TEX_COORD_BUMP_DY;
3451                 attr_node = NODE_ATTR_BUMP_DY;
3452                 geom_node = NODE_GEOMETRY_BUMP_DY;
3453         }
3454
3455         out = output("Generated");
3456         if(!out->links.empty()) {
3457                 if(compiler.background) {
3458                         compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
3459                 }
3460                 else {
3461                         if(from_dupli) {
3462                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, compiler.stack_assign(out));
3463                         }
3464                         else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
3465                                 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, compiler.stack_assign(out));
3466                         }
3467                         else {
3468                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
3469                                 compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
3470                         }
3471                 }
3472         }
3473
3474         out = output("Normal");
3475         if(!out->links.empty()) {
3476                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, compiler.stack_assign(out));
3477         }
3478
3479         out = output("UV");
3480         if(!out->links.empty()) {
3481                 if(from_dupli) {
3482                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
3483                 }
3484                 else {
3485                         int attr = compiler.attribute(ATTR_STD_UV);
3486                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
3487                 }
3488         }
3489
3490         out = output("Object");
3491         if(!out->links.empty()) {
3492                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, compiler.stack_assign(out), use_transform);
3493                 if(use_transform) {
3494                         Transform ob_itfm = transform_inverse(ob_tfm);
3495                         compiler.add_node(ob_itfm.x);
3496                         compiler.add_node(ob_itfm.y);
3497                         compiler.add_node(ob_itfm.z);
3498                 }
3499         }
3500
3501         out = output("Camera");
3502         if(!out->links.empty()) {
3503                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, compiler.stack_assign(out));
3504         }
3505
3506         out = output("Window");
3507         if(!out->links.empty()) {
3508                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, compiler.stack_assign(out));
3509         }
3510
3511         out = output("Reflection");
3512         if(!out->links.empty()) {
3513                 if(compiler.background) {
3514                         compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
3515                 }
3516                 else {
3517                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, compiler.stack_assign(out));
3518                 }
3519         }
3520 }
3521
3522 void TextureCoordinateNode::compile(OSLCompiler& compiler)
3523 {
3524         if(bump == SHADER_BUMP_DX)
3525                 compiler.parameter("bump_offset", "dx");
3526         else if(bump == SHADER_BUMP_DY)
3527                 compiler.parameter("bump_offset", "dy");
3528         else
3529                 compiler.parameter("bump_offset", "center");
3530
3531         if(compiler.background)
3532                 compiler.parameter("is_background", true);
3533         if(compiler.output_type() == SHADER_TYPE_VOLUME)
3534                 compiler.parameter("is_volume", true);
3535         compiler.parameter(this, "use_transform");
3536         Transform ob_itfm = transform_transposed_inverse(ob_tfm);
3537         compiler.parameter("object_itfm", ob_itfm);
3538
3539         compiler.parameter(this, "from_dupli");
3540
3541         compiler.add(this, "node_texture_coordinate");
3542 }
3543
3544 /* UV Map */
3545
3546 NODE_DEFINE(UVMapNode)
3547 {
3548         NodeType* type = NodeType::add("uvmap", create, NodeType::SHADER);
3549
3550         SOCKET_STRING(attribute, "attribute", ustring());
3551         SOCKET_IN_BOOLEAN(from_dupli, "from dupli", false);
3552
3553         SOCKET_OUT_POINT(UV, "UV");
3554
3555         return type;
3556 }
3557
3558 UVMapNode::UVMapNode()
3559 : ShaderNode(node_type)
3560 {
3561 }
3562