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