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