Fix Gradient Texture and OSL after refactor.
[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         SOCKET_ENUM(type, "Type", type_enum, NODE_BLEND_LINEAR);
808
809         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
810
811         SOCKET_OUT_COLOR(color, "Color");
812         SOCKET_OUT_FLOAT(fac, "Fac");
813
814         return type;
815 }
816
817 GradientTextureNode::GradientTextureNode()
818 : TextureNode(node_type)
819 {
820 }
821
822 void GradientTextureNode::compile(SVMCompiler& compiler)
823 {
824         ShaderInput *vector_in = input("Vector");
825         ShaderOutput *color_out = output("Color");
826         ShaderOutput *fac_out = output("Fac");
827
828         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
829
830         compiler.add_node(NODE_TEX_GRADIENT,
831                 compiler.encode_uchar4(
832                         type,
833                         vector_offset,
834                         compiler.stack_assign_if_linked(fac_out),
835                         compiler.stack_assign_if_linked(color_out)));
836
837         tex_mapping.compile_end(compiler, vector_in, vector_offset);
838 }
839
840 void GradientTextureNode::compile(OSLCompiler& compiler)
841 {
842         tex_mapping.compile(compiler);
843
844         compiler.parameter(this, "type");
845         compiler.add(this, "node_gradient_texture");
846 }
847
848 /* Noise Texture */
849
850 NODE_DEFINE(NoiseTextureNode)
851 {
852         NodeType* type = NodeType::add("noise_texture", create, NodeType::SHADER);
853
854         TEXTURE_MAPPING_DEFINE(NoiseTextureNode);
855
856         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
857         SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
858         SOCKET_IN_FLOAT(distortion, "Distortion", 0.0f);
859         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
860
861         SOCKET_OUT_COLOR(color, "Color");
862         SOCKET_OUT_FLOAT(fac, "Fac");
863
864         return type;
865 }
866
867 NoiseTextureNode::NoiseTextureNode()
868 : TextureNode(node_type)
869 {
870 }
871
872 void NoiseTextureNode::compile(SVMCompiler& compiler)
873 {
874         ShaderInput *distortion_in = input("Distortion");
875         ShaderInput *detail_in = input("Detail");
876         ShaderInput *scale_in = input("Scale");
877         ShaderInput *vector_in = input("Vector");
878         ShaderOutput *color_out = output("Color");
879         ShaderOutput *fac_out = output("Fac");
880
881         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
882
883         compiler.add_node(NODE_TEX_NOISE,
884                 compiler.encode_uchar4(
885                         vector_offset,
886                         compiler.stack_assign_if_linked(scale_in),
887                         compiler.stack_assign_if_linked(detail_in),
888                         compiler.stack_assign_if_linked(distortion_in)),
889                 compiler.encode_uchar4(
890                         compiler.stack_assign_if_linked(color_out),
891                         compiler.stack_assign_if_linked(fac_out)));
892         compiler.add_node(
893                 __float_as_int(scale),
894                 __float_as_int(detail),
895                 __float_as_int(distortion));
896
897         tex_mapping.compile_end(compiler, vector_in, vector_offset);
898 }
899
900 void NoiseTextureNode::compile(OSLCompiler& compiler)
901 {
902         tex_mapping.compile(compiler);
903
904         compiler.add(this, "node_noise_texture");
905 }
906
907 /* Voronoi Texture */
908
909 NODE_DEFINE(VoronoiTextureNode)
910 {
911         NodeType* type = NodeType::add("voronoi_texture", create, NodeType::SHADER);
912
913         TEXTURE_MAPPING_DEFINE(VoronoiTextureNode);
914
915         static NodeEnum coloring_enum;
916         coloring_enum.insert("Intensity", NODE_VORONOI_INTENSITY);
917         coloring_enum.insert("Cells", NODE_VORONOI_CELLS);
918         SOCKET_ENUM(coloring, "Coloring", coloring_enum, NODE_VORONOI_INTENSITY);
919
920         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
921         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
922
923         SOCKET_OUT_COLOR(color, "Color");
924         SOCKET_OUT_FLOAT(fac, "Fac");
925
926         return type;
927 }
928
929 VoronoiTextureNode::VoronoiTextureNode()
930 : TextureNode(node_type)
931 {
932 }
933
934 void VoronoiTextureNode::compile(SVMCompiler& compiler)
935 {
936         ShaderInput *scale_in = input("Scale");
937         ShaderInput *vector_in = input("Vector");
938         ShaderOutput *color_out = output("Color");
939         ShaderOutput *fac_out = output("Fac");
940
941         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
942
943         compiler.add_node(NODE_TEX_VORONOI,
944                 coloring,
945                 compiler.encode_uchar4(
946                         compiler.stack_assign_if_linked(scale_in),
947                         vector_offset,
948                         compiler.stack_assign(fac_out),
949                         compiler.stack_assign(color_out)),
950                 __float_as_int(scale));
951
952         tex_mapping.compile_end(compiler, vector_in, vector_offset);
953 }
954
955 void VoronoiTextureNode::compile(OSLCompiler& compiler)
956 {
957         tex_mapping.compile(compiler);
958
959         compiler.parameter(this, "coloring");
960         compiler.add(this, "node_voronoi_texture");
961 }
962
963 /* Musgrave Texture */
964
965 NODE_DEFINE(MusgraveTextureNode)
966 {
967         NodeType* type = NodeType::add("musgrave_texture", create, NodeType::SHADER);
968
969         TEXTURE_MAPPING_DEFINE(MusgraveTextureNode);
970
971         static NodeEnum type_enum;
972         type_enum.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
973         type_enum.insert("fBM", NODE_MUSGRAVE_FBM);
974         type_enum.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
975         type_enum.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
976         type_enum.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
977         SOCKET_ENUM(type, "Type", type_enum, NODE_MUSGRAVE_FBM);
978
979         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
980         SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
981         SOCKET_IN_FLOAT(dimension, "Dimension", 2.0f);
982         SOCKET_IN_FLOAT(lacunarity, "Lacunarity", 1.0f);
983         SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
984         SOCKET_IN_FLOAT(gain, "Gain", 1.0f);
985         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
986
987         SOCKET_OUT_COLOR(color, "Color");
988         SOCKET_OUT_FLOAT(fac, "Fac");
989
990         return type;
991 }
992
993 MusgraveTextureNode::MusgraveTextureNode()
994 : TextureNode(node_type)
995 {
996 }
997
998 void MusgraveTextureNode::compile(SVMCompiler& compiler)
999 {
1000         ShaderInput *vector_in = input("Vector");
1001         ShaderInput *scale_in = input("Scale");
1002         ShaderInput *dimension_in = input("Dimension");
1003         ShaderInput *lacunarity_in = input("Lacunarity");
1004         ShaderInput *detail_in = input("Detail");
1005         ShaderInput *offset_in = input("Offset");
1006         ShaderInput *gain_in = input("Gain");
1007         ShaderOutput *fac_out = output("Fac");
1008         ShaderOutput *color_out = output("Color");
1009
1010         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1011
1012         compiler.add_node(NODE_TEX_MUSGRAVE,
1013                 compiler.encode_uchar4(
1014                         type,
1015                         vector_offset,
1016                         compiler.stack_assign_if_linked(color_out),
1017                         compiler.stack_assign_if_linked(fac_out)),
1018                 compiler.encode_uchar4(
1019                         compiler.stack_assign_if_linked(dimension_in),
1020                         compiler.stack_assign_if_linked(lacunarity_in),
1021                         compiler.stack_assign_if_linked(detail_in),
1022                         compiler.stack_assign_if_linked(offset_in)),
1023                 compiler.encode_uchar4(
1024                         compiler.stack_assign_if_linked(gain_in),
1025                         compiler.stack_assign_if_linked(scale_in)));
1026         compiler.add_node(__float_as_int(dimension),
1027                 __float_as_int(lacunarity),
1028                 __float_as_int(detail),
1029                 __float_as_int(offset));
1030         compiler.add_node(__float_as_int(gain),
1031                 __float_as_int(scale));
1032
1033         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1034 }
1035
1036 void MusgraveTextureNode::compile(OSLCompiler& compiler)
1037 {
1038         tex_mapping.compile(compiler);
1039
1040         compiler.parameter(this, "type");
1041         compiler.add(this, "node_musgrave_texture");
1042 }
1043
1044 /* Wave Texture */
1045
1046 NODE_DEFINE(WaveTextureNode)
1047 {
1048         NodeType* type = NodeType::add("wave_texture", create, NodeType::SHADER);
1049
1050         TEXTURE_MAPPING_DEFINE(WaveTextureNode);
1051
1052         static NodeEnum type_enum;
1053         type_enum.insert("Bands", NODE_WAVE_BANDS);
1054         type_enum.insert("Rings", NODE_WAVE_RINGS);
1055         SOCKET_ENUM(type, "Type", type_enum, NODE_WAVE_BANDS);
1056
1057         static NodeEnum profile_enum;
1058         profile_enum.insert("Sine", NODE_WAVE_PROFILE_SIN);
1059         profile_enum.insert("Saw", NODE_WAVE_PROFILE_SAW);
1060         SOCKET_ENUM(profile, "Profile", profile_enum, NODE_WAVE_PROFILE_SIN);
1061
1062         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
1063         SOCKET_IN_FLOAT(distortion, "Distortion", 0.0f);
1064         SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
1065         SOCKET_IN_FLOAT(detail_scale, "Detail Scale", 0.0f);
1066         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
1067
1068         SOCKET_OUT_COLOR(color, "Color");
1069         SOCKET_OUT_FLOAT(fac, "Fac");
1070
1071         return type;
1072 }
1073
1074 WaveTextureNode::WaveTextureNode()
1075 : TextureNode(node_type)
1076 {
1077 }
1078
1079 void WaveTextureNode::compile(SVMCompiler& compiler)
1080 {
1081         ShaderInput *scale_in = input("Scale");
1082         ShaderInput *distortion_in = input("Distortion");
1083         ShaderInput *dscale_in = input("Detail Scale");
1084         ShaderInput *detail_in = input("Detail");
1085         ShaderInput *vector_in = input("Vector");
1086         ShaderOutput *fac_out = output("Fac");
1087         ShaderOutput *color_out = output("Color");
1088
1089         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1090
1091         compiler.add_node(NODE_TEX_WAVE,
1092                 compiler.encode_uchar4(
1093                         type,
1094                         compiler.stack_assign_if_linked(color_out),
1095                         compiler.stack_assign_if_linked(fac_out),
1096                         compiler.stack_assign_if_linked(dscale_in)),
1097                 compiler.encode_uchar4(
1098                         vector_offset,
1099                         compiler.stack_assign_if_linked(scale_in),
1100                         compiler.stack_assign_if_linked(detail_in),
1101                         compiler.stack_assign_if_linked(distortion_in)),
1102                 profile);
1103
1104         compiler.add_node(
1105                 __float_as_int(scale),
1106                 __float_as_int(detail),
1107                 __float_as_int(distortion),
1108                 __float_as_int(detail_scale));
1109
1110         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1111 }
1112
1113 void WaveTextureNode::compile(OSLCompiler& compiler)
1114 {
1115         tex_mapping.compile(compiler);
1116
1117         compiler.parameter(this, "type");
1118         compiler.parameter(this, "profile");
1119
1120         compiler.add(this, "node_wave_texture");
1121 }
1122
1123 /* Magic Texture */
1124
1125 NODE_DEFINE(MagicTextureNode)
1126 {
1127         NodeType* type = NodeType::add("magic_texture", create, NodeType::SHADER);
1128
1129         TEXTURE_MAPPING_DEFINE(MagicTextureNode);
1130
1131         SOCKET_INT(depth, "Depth", 2);
1132
1133         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
1134         SOCKET_IN_FLOAT(scale, "Scale", 5.0f);
1135         SOCKET_IN_FLOAT(distortion, "Distortion", 1.0f);
1136
1137         SOCKET_OUT_COLOR(color, "Color");
1138         SOCKET_OUT_FLOAT(fac, "Fac");
1139
1140         return type;
1141 }
1142
1143 MagicTextureNode::MagicTextureNode()
1144 : TextureNode(node_type)
1145 {
1146 }
1147
1148 void MagicTextureNode::compile(SVMCompiler& compiler)
1149 {
1150         ShaderInput *vector_in = input("Vector");
1151         ShaderInput *scale_in = input("Scale");
1152         ShaderInput *distortion_in = input("Distortion");
1153         ShaderOutput *color_out = output("Color");
1154         ShaderOutput *fac_out = output("Fac");
1155
1156         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1157
1158         compiler.add_node(NODE_TEX_MAGIC,
1159                 compiler.encode_uchar4(
1160                         depth,
1161                         compiler.stack_assign_if_linked(color_out),
1162                         compiler.stack_assign_if_linked(fac_out)),
1163                 compiler.encode_uchar4(
1164                         vector_offset,
1165                         compiler.stack_assign_if_linked(scale_in),
1166                         compiler.stack_assign_if_linked(distortion_in)));
1167         compiler.add_node(
1168                 __float_as_int(scale),
1169                 __float_as_int(distortion));
1170
1171         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1172 }
1173
1174 void MagicTextureNode::compile(OSLCompiler& compiler)
1175 {
1176         tex_mapping.compile(compiler);
1177
1178         compiler.parameter(this, "depth");
1179         compiler.add(this, "node_magic_texture");
1180 }
1181
1182 /* Checker Texture */
1183
1184 NODE_DEFINE(CheckerTextureNode)
1185 {
1186         NodeType* type = NodeType::add("checker_texture", create, NodeType::SHADER);
1187
1188         TEXTURE_MAPPING_DEFINE(CheckerTextureNode);
1189
1190         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
1191         SOCKET_IN_COLOR(color1, "Color1", make_float3(0.0f, 0.0f, 0.0f));
1192         SOCKET_IN_COLOR(color2, "Color2", make_float3(0.0f, 0.0f, 0.0f));
1193         SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
1194
1195         SOCKET_OUT_COLOR(color, "Color");
1196         SOCKET_OUT_FLOAT(fac, "Fac");
1197
1198         return type;
1199 }
1200
1201 CheckerTextureNode::CheckerTextureNode()
1202 : TextureNode(node_type)
1203 {
1204 }
1205
1206 void CheckerTextureNode::compile(SVMCompiler& compiler)
1207 {
1208         ShaderInput *vector_in = input("Vector");
1209         ShaderInput *color1_in = input("Color1");
1210         ShaderInput *color2_in = input("Color2");
1211         ShaderInput *scale_in = input("Scale");
1212         
1213         ShaderOutput *color_out = output("Color");
1214         ShaderOutput *fac_out = output("Fac");
1215
1216         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1217
1218         compiler.add_node(NODE_TEX_CHECKER,
1219                 compiler.encode_uchar4(
1220                         vector_offset,
1221                         compiler.stack_assign(color1_in),
1222                         compiler.stack_assign(color2_in),
1223                         compiler.stack_assign_if_linked(scale_in)),
1224                 compiler.encode_uchar4(
1225                         compiler.stack_assign_if_linked(color_out),
1226                         compiler.stack_assign_if_linked(fac_out)),
1227                 __float_as_int(scale));
1228
1229         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1230 }
1231
1232 void CheckerTextureNode::compile(OSLCompiler& compiler)
1233 {
1234         tex_mapping.compile(compiler);
1235
1236         compiler.add(this, "node_checker_texture");
1237 }
1238
1239 /* Brick Texture */
1240
1241 NODE_DEFINE(BrickTextureNode)
1242 {
1243         NodeType* type = NodeType::add("brick_texture", create, NodeType::SHADER);
1244
1245         TEXTURE_MAPPING_DEFINE(BrickTextureNode);
1246
1247         SOCKET_FLOAT(offset, "Offset", 0.5f);
1248         SOCKET_INT(offset_frequency, "Offset Frequency", 2);
1249         SOCKET_FLOAT(squash, "Squash", 1.0f);
1250         SOCKET_INT(squash_frequency, "Squash Frequency", 2);
1251
1252         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
1253
1254         SOCKET_IN_COLOR(color1, "Color1", make_float3(0.0f, 0.0f, 0.0f));
1255         SOCKET_IN_COLOR(color2, "Color2", make_float3(0.0f, 0.0f, 0.0f));
1256         SOCKET_IN_COLOR(mortar, "Mortar", make_float3(0.0f, 0.0f, 0.0f));
1257         SOCKET_IN_FLOAT(scale, "Scale", 5.0f);
1258         SOCKET_IN_FLOAT(mortar_size, "Mortar Size", 0.02f);
1259         SOCKET_IN_FLOAT(bias, "Bias", 0.0f);
1260         SOCKET_IN_FLOAT(brick_width, "Brick Width", 0.5f);
1261         SOCKET_IN_FLOAT(row_height, "Row Height", 0.25f);
1262
1263         SOCKET_OUT_COLOR(color, "Color");
1264         SOCKET_OUT_FLOAT(fac, "Fac");
1265
1266         return type;
1267 }
1268
1269 BrickTextureNode::BrickTextureNode()
1270 : TextureNode(node_type)
1271 {
1272 }
1273
1274 void BrickTextureNode::compile(SVMCompiler& compiler)
1275 {
1276         ShaderInput *vector_in = input("Vector");
1277         ShaderInput *color1_in = input("Color1");
1278         ShaderInput *color2_in = input("Color2");
1279         ShaderInput *mortar_in = input("Mortar");
1280         ShaderInput *scale_in = input("Scale");
1281         ShaderInput *mortar_size_in = input("Mortar Size");
1282         ShaderInput *bias_in = input("Bias");
1283         ShaderInput *brick_width_in = input("Brick Width");
1284         ShaderInput *row_height_in = input("Row Height");
1285         
1286         ShaderOutput *color_out = output("Color");
1287         ShaderOutput *fac_out = output("Fac");
1288
1289         int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
1290
1291         compiler.add_node(NODE_TEX_BRICK,
1292                 compiler.encode_uchar4(
1293                         vector_offset,
1294                         compiler.stack_assign(color1_in),
1295                         compiler.stack_assign(color2_in),
1296                         compiler.stack_assign(mortar_in)),
1297                 compiler.encode_uchar4(
1298                         compiler.stack_assign_if_linked(scale_in),
1299                         compiler.stack_assign_if_linked(mortar_size_in),
1300                         compiler.stack_assign_if_linked(bias_in),
1301                         compiler.stack_assign_if_linked(brick_width_in)),
1302                 compiler.encode_uchar4(
1303                         compiler.stack_assign_if_linked(row_height_in),
1304                         compiler.stack_assign_if_linked(color_out),
1305                         compiler.stack_assign_if_linked(fac_out)));
1306                         
1307         compiler.add_node(compiler.encode_uchar4(offset_frequency, squash_frequency),
1308                 __float_as_int(scale),
1309                 __float_as_int(mortar_size),
1310                 __float_as_int(bias));
1311
1312         compiler.add_node(__float_as_int(brick_width),
1313                 __float_as_int(row_height),
1314                 __float_as_int(offset),
1315                 __float_as_int(squash));
1316
1317         tex_mapping.compile_end(compiler, vector_in, vector_offset);
1318 }
1319
1320 void BrickTextureNode::compile(OSLCompiler& compiler)
1321 {
1322         tex_mapping.compile(compiler);
1323
1324         compiler.parameter(this, "offset");
1325         compiler.parameter(this, "offset_frequency");
1326         compiler.parameter(this, "squash");
1327         compiler.parameter(this, "squash_frequency");
1328         compiler.add(this, "node_brick_texture");
1329 }
1330
1331 /* Point Density Texture */
1332
1333 NODE_DEFINE(PointDensityTextureNode)
1334 {
1335         NodeType* type = NodeType::add("point_density_texture", create, NodeType::SHADER);
1336
1337         SOCKET_STRING(filename, "Filename", ustring(""));
1338
1339         static NodeEnum space_enum;
1340         space_enum.insert("Object", NODE_TEX_VOXEL_SPACE_OBJECT);
1341         space_enum.insert("World", NODE_TEX_VOXEL_SPACE_WORLD);
1342         SOCKET_ENUM(space, "Space", space_enum, NODE_TEX_VOXEL_SPACE_OBJECT);
1343
1344         static NodeEnum interpolation_enum;
1345         interpolation_enum.insert("closest", INTERPOLATION_CLOSEST);
1346         interpolation_enum.insert("linear", INTERPOLATION_LINEAR);
1347         interpolation_enum.insert("cubic", INTERPOLATION_CUBIC);
1348         interpolation_enum.insert("smart", INTERPOLATION_SMART);
1349         SOCKET_ENUM(interpolation, "Interpolation", interpolation_enum, INTERPOLATION_LINEAR);
1350
1351         SOCKET_TRANSFORM(tfm, "Transform", transform_identity());
1352
1353         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_POSITION);
1354
1355         SOCKET_OUT_FLOAT(density, "Density");
1356         SOCKET_OUT_COLOR(color, "Color");
1357
1358         return type;
1359 }
1360
1361 PointDensityTextureNode::PointDensityTextureNode()
1362 : ShaderNode(node_type)
1363 {
1364         image_manager = NULL;
1365         slot = -1;
1366         builtin_data = NULL;
1367 }
1368
1369 PointDensityTextureNode::~PointDensityTextureNode()
1370 {
1371         if(image_manager) {
1372                 image_manager->remove_image(filename.string(),
1373                                             builtin_data,
1374                                             interpolation,
1375                                             EXTENSION_CLIP);
1376         }
1377 }
1378
1379 ShaderNode *PointDensityTextureNode::clone() const
1380 {
1381         PointDensityTextureNode *node = new PointDensityTextureNode(*this);
1382         node->image_manager = NULL;
1383         node->slot = -1;
1384         return node;
1385 }
1386
1387 void PointDensityTextureNode::attributes(Shader *shader,
1388                                          AttributeRequestSet *attributes)
1389 {
1390         if(shader->has_volume)
1391                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
1392
1393         ShaderNode::attributes(shader, attributes);
1394 }
1395
1396 void PointDensityTextureNode::compile(SVMCompiler& compiler)
1397 {
1398         ShaderInput *vector_in = input("Vector");
1399         ShaderOutput *density_out = output("Density");
1400         ShaderOutput *color_out = output("Color");
1401
1402         const bool use_density = !density_out->links.empty();
1403         const bool use_color = !color_out->links.empty();
1404
1405         image_manager = compiler.image_manager;
1406
1407         if(use_density || use_color) {
1408                 if(slot == -1) {
1409                         bool is_float, is_linear;
1410                         slot = image_manager->add_image(filename.string(), builtin_data,
1411                                                         false, 0,
1412                                                         is_float, is_linear,
1413                                                         interpolation,
1414                                                         EXTENSION_CLIP,
1415                                                         true);
1416                 }
1417
1418                 if(slot != -1) {
1419                         compiler.stack_assign(vector_in);
1420                         compiler.add_node(NODE_TEX_VOXEL,
1421                                           slot,
1422                                           compiler.encode_uchar4(compiler.stack_assign(vector_in),
1423                                                                  compiler.stack_assign_if_linked(density_out),
1424                                                                  compiler.stack_assign_if_linked(color_out),
1425                                                                  space));
1426                         if(space == NODE_TEX_VOXEL_SPACE_WORLD) {
1427                                 compiler.add_node(tfm.x);
1428                                 compiler.add_node(tfm.y);
1429                                 compiler.add_node(tfm.z);
1430                                 compiler.add_node(tfm.w);
1431                         }
1432                 }
1433                 else {
1434                         if(use_density) {
1435                                 compiler.add_node(NODE_VALUE_F,
1436                                                                   __float_as_int(0.0f),
1437                                                                   compiler.stack_assign(density_out));
1438                         }
1439                         if(use_color) {
1440                                 compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
1441                                 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
1442                                                                                                                         TEX_IMAGE_MISSING_G,
1443                                                                                                                         TEX_IMAGE_MISSING_B));
1444                         }
1445                 }
1446         }
1447 }
1448
1449 void PointDensityTextureNode::compile(OSLCompiler& compiler)
1450 {
1451         ShaderOutput *density_out = output("Density");
1452         ShaderOutput *color_out = output("Color");
1453
1454         const bool use_density = !density_out->links.empty();
1455         const bool use_color = !color_out->links.empty();
1456
1457         image_manager = compiler.image_manager;
1458
1459         if(use_density || use_color) {
1460                 if(slot == -1) {
1461                         bool is_float, is_linear;
1462                         slot = image_manager->add_image(filename.string(), builtin_data,
1463                                                         false, 0,
1464                                                         is_float, is_linear,
1465                                                         interpolation,
1466                                                         EXTENSION_CLIP,
1467                                                         true);
1468                 }
1469
1470                 if(slot != -1) {
1471                         compiler.parameter("filename", string_printf("@%d", slot).c_str());
1472                 }
1473                 if(space == NODE_TEX_VOXEL_SPACE_WORLD) {
1474                         compiler.parameter("mapping", transform_transpose(tfm));
1475                         compiler.parameter("use_mapping", 1);
1476                 }
1477                 compiler.parameter(this, "interpolation");
1478                 compiler.add(this, "node_voxel_texture");
1479         }
1480 }
1481
1482 /* Normal */
1483
1484 NODE_DEFINE(NormalNode)
1485 {
1486         NodeType* type = NodeType::add("normal", create, NodeType::SHADER);
1487
1488         SOCKET_VECTOR(direction, "direction", make_float3(0.0f, 0.0f, 0.0f));
1489
1490         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f));
1491
1492         SOCKET_OUT_NORMAL(normal, "Normal");
1493         SOCKET_OUT_FLOAT(dot, "Dot");
1494
1495         return type;
1496 }
1497
1498 NormalNode::NormalNode()
1499 : ShaderNode(node_type)
1500 {
1501 }
1502
1503 void NormalNode::compile(SVMCompiler& compiler)
1504 {
1505         ShaderInput *normal_in = input("Normal");
1506         ShaderOutput *normal_out = output("Normal");
1507         ShaderOutput *dot_out = output("Dot");
1508
1509         compiler.add_node(NODE_NORMAL,
1510                 compiler.stack_assign(normal_in),
1511                 compiler.stack_assign(normal_out),
1512                 compiler.stack_assign(dot_out));
1513         compiler.add_node(
1514                 __float_as_int(direction.x),
1515                 __float_as_int(direction.y),
1516                 __float_as_int(direction.z));
1517 }
1518
1519 void NormalNode::compile(OSLCompiler& compiler)
1520 {
1521         compiler.parameter(this, "direction");
1522         compiler.add(this, "node_normal");
1523 }
1524
1525 /* Mapping */
1526
1527 NODE_DEFINE(MappingNode)
1528 {
1529         NodeType* type = NodeType::add("mapping", create, NodeType::SHADER);
1530
1531         TEXTURE_MAPPING_DEFINE(MappingNode);
1532
1533         SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
1534         SOCKET_OUT_POINT(vector, "Vector");
1535
1536         return type;
1537 }
1538
1539 MappingNode::MappingNode()
1540 : ShaderNode(node_type)
1541 {
1542 }
1543
1544 void MappingNode::compile(SVMCompiler& compiler)
1545 {
1546         ShaderInput *vector_in = input("Vector");
1547         ShaderOutput *vector_out = output("Vector");
1548
1549         tex_mapping.compile(compiler, compiler.stack_assign(vector_in), compiler.stack_assign(vector_out));
1550 }
1551
1552 void MappingNode::compile(OSLCompiler& compiler)
1553 {
1554         Transform tfm = transform_transpose(tex_mapping.compute_transform());
1555         compiler.parameter("Matrix", tfm);
1556         compiler.parameter_point("mapping_min", tex_mapping.min);
1557         compiler.parameter_point("mapping_max", tex_mapping.max);
1558         compiler.parameter("use_minmax", tex_mapping.use_minmax);
1559
1560         compiler.add(this, "node_mapping");
1561 }
1562
1563 /* RGBToBW */
1564
1565 NODE_DEFINE(RGBToBWNode)
1566 {
1567         NodeType* type = NodeType::add("rgb_to_bw", create, NodeType::SHADER);
1568
1569         SOCKET_IN_POINT(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
1570         SOCKET_OUT_POINT(val, "Val");
1571
1572         return type;
1573 }
1574
1575 RGBToBWNode::RGBToBWNode()
1576 : ShaderNode(node_type)
1577 {
1578 }
1579
1580 bool RGBToBWNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
1581 {
1582         if(inputs[0]->link == NULL) {
1583                 optimized->set(linear_rgb_to_gray(color));
1584                 return true;
1585         }
1586
1587         return false;
1588 }
1589
1590 void RGBToBWNode::compile(SVMCompiler& compiler)
1591 {
1592         compiler.add_node(NODE_CONVERT,
1593                          NODE_CONVERT_CF,
1594                          compiler.stack_assign(inputs[0]),
1595                          compiler.stack_assign(outputs[0]));
1596 }
1597
1598 void RGBToBWNode::compile(OSLCompiler& compiler)
1599 {
1600         compiler.add(this, "node_convert_from_color");
1601 }
1602
1603 /* Convert */
1604
1605 const NodeType* ConvertNode::node_types[ConvertNode::MAX_TYPE][ConvertNode::MAX_TYPE];
1606 bool ConvertNode::initialized = ConvertNode::register_types();
1607
1608 Node* ConvertNode::create(const NodeType *type)
1609 {
1610         return new ConvertNode(type->inputs[0].type,  type->outputs[0].type);
1611 }
1612
1613 bool ConvertNode::register_types()
1614 {
1615         const int num_types = 8;
1616         SocketType::Type types[num_types] = {SocketType::FLOAT,
1617                                              SocketType::INT,
1618                                              SocketType::COLOR,
1619                                              SocketType::VECTOR,
1620                                              SocketType::POINT,
1621                                              SocketType::NORMAL,
1622                                              SocketType::STRING,
1623                                                                                  SocketType::CLOSURE};
1624
1625         for(size_t i = 0; i < num_types; i++) {
1626                 SocketType::Type from = types[i];
1627                 ustring from_name(SocketType::type_name(from));
1628                 ustring from_value_name("value_" + from_name.string());
1629
1630                 for(size_t j = 0; j < num_types; j++) {
1631                         SocketType::Type to = types[j];
1632                         ustring to_name(SocketType::type_name(to));
1633                         ustring to_value_name("value_" + to_name.string());
1634
1635                         string node_name = "convert_" + from_name.string() + "_to_" + to_name.string();
1636                         NodeType* type = NodeType::add(node_name.c_str(), create, NodeType::SHADER);
1637
1638                         type->register_input(from_value_name, from_value_name, from,
1639                                 SOCKET_OFFSETOF(ConvertNode, value_float), SocketType::zero_default_value(),
1640                                 NULL, NULL, SocketType::LINKABLE);
1641                         type->register_output(to_value_name, to_value_name, to);
1642
1643                         assert(from < MAX_TYPE);
1644                         assert(to < MAX_TYPE);
1645
1646                         node_types[from][to] = type;
1647                 }
1648         }
1649
1650         return true;
1651 }
1652
1653 ConvertNode::ConvertNode(SocketType::Type from_, SocketType::Type to_, bool autoconvert)
1654 : ShaderNode(node_types[from_][to_])
1655 {
1656         from = from_;
1657         to = to_;
1658
1659         if(from == to)
1660                 special_type = SHADER_SPECIAL_TYPE_PROXY;
1661         else if(autoconvert)
1662                 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1663 }
1664
1665 bool ConvertNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
1666 {
1667         /* proxy nodes should have been removed at this point */
1668         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1669
1670         ShaderInput *in = inputs[0];
1671
1672         /* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
1673
1674         if(in->link == NULL) {
1675                 if(from == SocketType::FLOAT) {
1676                         if(SocketType::is_float3(to)) {
1677                                 optimized->set(make_float3(value_float, value_float, value_float));
1678                                 return true;
1679                         }
1680                 }
1681                 else if(SocketType::is_float3(from)) {
1682                         if(to == SocketType::FLOAT) {
1683                                 if(from == SocketType::COLOR)
1684                                         /* color to float */
1685                                         optimized->set(linear_rgb_to_gray(value_color));
1686                                 else
1687                                         /* vector/point/normal to float */
1688                                         optimized->set(average(value_vector));
1689                                 return true;
1690                         }
1691                         else if(SocketType::is_float3(to)) {
1692                                 optimized->set(value_color);
1693                                 return true;
1694                         }
1695                 }
1696         }
1697
1698         return false;
1699 }
1700
1701 void ConvertNode::compile(SVMCompiler& compiler)
1702 {
1703         /* proxy nodes should have been removed at this point */
1704         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1705
1706         ShaderInput *in = inputs[0];
1707         ShaderOutput *out = outputs[0];
1708
1709         if(from == SocketType::FLOAT) {
1710                 if(to == SocketType::INT)
1711                         /* float to int */
1712                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, compiler.stack_assign(in), compiler.stack_assign(out));
1713                 else
1714                         /* float to float3 */
1715                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, compiler.stack_assign(in), compiler.stack_assign(out));
1716         }
1717         else if(from == SocketType::INT) {
1718                 if(to == SocketType::FLOAT)
1719                         /* int to float */
1720                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, compiler.stack_assign(in), compiler.stack_assign(out));
1721                 else
1722                         /* int to vector/point/normal */
1723                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, compiler.stack_assign(in), compiler.stack_assign(out));
1724         }
1725         else if(to == SocketType::FLOAT) {
1726                 if(from == SocketType::COLOR)
1727                         /* color to float */
1728                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, compiler.stack_assign(in), compiler.stack_assign(out));
1729                 else
1730                         /* vector/point/normal to float */
1731                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, compiler.stack_assign(in), compiler.stack_assign(out));
1732         }
1733         else if(to == SocketType::INT) {
1734                 if(from == SocketType::COLOR)
1735                         /* color to int */
1736                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, compiler.stack_assign(in), compiler.stack_assign(out));
1737                 else
1738                         /* vector/point/normal to int */
1739                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, compiler.stack_assign(in), compiler.stack_assign(out));
1740         }
1741         else {
1742                 /* float3 to float3 */
1743                 if(in->link) {
1744                         /* no op in SVM */
1745                         compiler.stack_link(in, out);
1746                 }
1747                 else {
1748                         /* set 0,0,0 value */
1749                         compiler.add_node(NODE_VALUE_V, compiler.stack_assign(out));
1750                         compiler.add_node(NODE_VALUE_V, value_color);
1751                 }
1752         }
1753 }
1754
1755 void ConvertNode::compile(OSLCompiler& compiler)
1756 {
1757         /* proxy nodes should have been removed at this point */
1758         assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
1759
1760         if(from == SocketType::FLOAT)
1761                 compiler.add(this, "node_convert_from_float");
1762         else if(from == SocketType::INT)
1763                 compiler.add(this, "node_convert_from_int");
1764         else if(from == SocketType::COLOR)
1765                 compiler.add(this, "node_convert_from_color");
1766         else if(from == SocketType::VECTOR)
1767                 compiler.add(this, "node_convert_from_vector");
1768         else if(from == SocketType::POINT)
1769                 compiler.add(this, "node_convert_from_point");
1770         else if(from == SocketType::NORMAL)
1771                 compiler.add(this, "node_convert_from_normal");
1772         else
1773                 assert(0);
1774 }
1775
1776 /* BSDF Closure */
1777
1778 BsdfNode::BsdfNode(const NodeType *node_type)
1779 : ShaderNode(node_type)
1780 {
1781         special_type = SHADER_SPECIAL_TYPE_CLOSURE;
1782 }
1783
1784 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3, ShaderInput *param4)
1785 {
1786         ShaderInput *color_in = input("Color");
1787         ShaderInput *normal_in = input("Normal");
1788         ShaderInput *tangent_in = input("Tangent");
1789
1790         if(color_in->link)
1791                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
1792         else
1793                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
1794
1795         int normal_offset = (normal_in) ? compiler.stack_assign_if_linked(normal_in) : SVM_STACK_INVALID;
1796         int tangent_offset = (tangent_in) ? compiler.stack_assign_if_linked(tangent_in) : SVM_STACK_INVALID;
1797         int param3_offset = (param3) ? compiler.stack_assign(param3) : SVM_STACK_INVALID;
1798         int param4_offset = (param4) ? compiler.stack_assign(param4) : SVM_STACK_INVALID;
1799
1800         compiler.add_node(NODE_CLOSURE_BSDF,
1801                 compiler.encode_uchar4(closure,
1802                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
1803                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
1804                         compiler.closure_mix_weight_offset()),
1805                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
1806                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
1807
1808         compiler.add_node(normal_offset, tangent_offset, param3_offset, param4_offset);
1809 }
1810
1811 void BsdfNode::compile(SVMCompiler& compiler)
1812 {
1813         compile(compiler, NULL, NULL);
1814 }
1815
1816 void BsdfNode::compile(OSLCompiler& /*compiler*/)
1817 {
1818         assert(0);
1819 }
1820
1821 /* Anisotropic BSDF Closure */
1822
1823 NODE_DEFINE(AnisotropicBsdfNode)
1824 {
1825         NodeType* type = NodeType::add("anisotropic_bsdf", create, NodeType::SHADER);
1826
1827         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
1828         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1829         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
1830
1831         static NodeEnum distribution_enum;
1832         distribution_enum.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID);
1833         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1834         distribution_enum.insert("Ashikhmin-Shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID);
1835         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1836
1837         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
1838
1839         SOCKET_IN_FLOAT(roughness, "Roughness", 0.2f);
1840         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.5f);
1841         SOCKET_IN_FLOAT(rotation, "Rotation", 0.0f);
1842
1843         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
1844
1845         return type;
1846 }
1847
1848 AnisotropicBsdfNode::AnisotropicBsdfNode()
1849 : BsdfNode(node_type)
1850 {
1851         closure = CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID;
1852 }
1853
1854 void AnisotropicBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
1855 {
1856         if(shader->has_surface) {
1857                 ShaderInput *tangent_in = input("Tangent");
1858
1859                 if(!tangent_in->link)
1860                         attributes->add(ATTR_STD_GENERATED);
1861         }
1862
1863         ShaderNode::attributes(shader, attributes);
1864 }
1865
1866 void AnisotropicBsdfNode::compile(SVMCompiler& compiler)
1867 {
1868         closure = distribution;
1869
1870         BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1871 }
1872
1873 void AnisotropicBsdfNode::compile(OSLCompiler& compiler)
1874 {
1875         compiler.parameter(this, "distribution");
1876         compiler.add(this, "node_anisotropic_bsdf");
1877 }
1878
1879 /* Glossy BSDF Closure */
1880
1881 NODE_DEFINE(GlossyBsdfNode)
1882 {
1883         NodeType* type = NodeType::add("glossy_bsdf", create, NodeType::SHADER);
1884
1885         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
1886         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1887         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
1888
1889         static NodeEnum distribution_enum;
1890         distribution_enum.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1891         distribution_enum.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1892         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1893         distribution_enum.insert("Ashikhmin-Shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
1894         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ID);
1895         SOCKET_IN_FLOAT(roughness, "Roughness", 0.2f);
1896
1897         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
1898
1899         return type;
1900 }
1901
1902 GlossyBsdfNode::GlossyBsdfNode()
1903 : BsdfNode(node_type)
1904 {
1905         closure = CLOSURE_BSDF_MICROFACET_GGX_ID;
1906         distribution_orig = NBUILTIN_CLOSURES;
1907 }
1908
1909 void GlossyBsdfNode::simplify_settings(Scene *scene)
1910 {
1911         if(distribution_orig == NBUILTIN_CLOSURES) {
1912                 distribution_orig = distribution;
1913         }
1914         Integrator *integrator = scene->integrator;
1915         if(integrator->filter_glossy == 0.0f) {
1916                 /* Fallback to Sharp closure for Roughness close to 0.
1917                  * Note: Keep the epsilon in sync with kernel!
1918                  */
1919                 ShaderInput *roughness_input = input("Roughness");
1920                 if(!roughness_input->link && roughness <= 1e-4f) {
1921                         distribution = CLOSURE_BSDF_REFLECTION_ID;
1922                 }
1923         }
1924         else {
1925                 /* Rollback to original distribution when filter glossy is used. */
1926                 distribution = distribution_orig;
1927         }
1928         closure = distribution;
1929 }
1930
1931 bool GlossyBsdfNode::has_integrator_dependency()
1932 {
1933         ShaderInput *roughness_input = input("Roughness");
1934         return !roughness_input->link && roughness <= 1e-4f;
1935 }
1936
1937 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1938 {
1939         closure = distribution;
1940
1941         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1942                 BsdfNode::compile(compiler, NULL, NULL);
1943         else
1944                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1945 }
1946
1947 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1948 {
1949         compiler.parameter(this, "distribution");
1950         compiler.add(this, "node_glossy_bsdf");
1951 }
1952
1953 /* Glass BSDF Closure */
1954
1955 NODE_DEFINE(GlassBsdfNode)
1956 {
1957         NodeType* type = NodeType::add("glass_bsdf", create, NodeType::SHADER);
1958
1959         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
1960         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1961         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
1962
1963         static NodeEnum distribution_enum;
1964         distribution_enum.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1965         distribution_enum.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1966         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1967         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1968         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
1969         SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
1970
1971         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
1972
1973         return type;
1974 }
1975
1976 GlassBsdfNode::GlassBsdfNode()
1977 : BsdfNode(node_type)
1978 {
1979         closure = CLOSURE_BSDF_SHARP_GLASS_ID;
1980         distribution_orig = NBUILTIN_CLOSURES;
1981 }
1982
1983 void GlassBsdfNode::simplify_settings(Scene *scene)
1984 {
1985         if(distribution_orig == NBUILTIN_CLOSURES) {
1986                 distribution_orig = distribution;
1987         }
1988         Integrator *integrator = scene->integrator;
1989         if(integrator->filter_glossy == 0.0f) {
1990                 /* Fallback to Sharp closure for Roughness close to 0.
1991                  * Note: Keep the epsilon in sync with kernel!
1992                  */
1993                 ShaderInput *roughness_input = input("Roughness");
1994                 if(!roughness_input->link && roughness <= 1e-4f) {
1995                         distribution = CLOSURE_BSDF_SHARP_GLASS_ID;
1996                 }
1997         }
1998         else {
1999                 /* Rollback to original distribution when filter glossy is used. */
2000                 distribution = distribution_orig;
2001         }
2002         closure = distribution;
2003 }
2004
2005 bool GlassBsdfNode::has_integrator_dependency()
2006 {
2007         ShaderInput *roughness_input = input("Roughness");
2008         return !roughness_input->link && roughness <= 1e-4f;
2009 }
2010
2011 void GlassBsdfNode::compile(SVMCompiler& compiler)
2012 {
2013         closure = distribution;
2014
2015         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
2016                 BsdfNode::compile(compiler, NULL, input("IOR"));
2017         else
2018                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2019 }
2020
2021 void GlassBsdfNode::compile(OSLCompiler& compiler)
2022 {
2023         compiler.parameter(this, "distribution");
2024         compiler.add(this, "node_glass_bsdf");
2025 }
2026
2027 /* Refraction BSDF Closure */
2028
2029 NODE_DEFINE(RefractionBsdfNode)
2030 {
2031         NodeType* type = NodeType::add("refraction_bsdf", create, NodeType::SHADER);
2032
2033         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2034         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2035         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2036
2037         static NodeEnum distribution_enum;
2038         distribution_enum.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
2039         distribution_enum.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
2040         distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2041         SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2042
2043         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2044         SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
2045
2046         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2047
2048         return type;
2049 }
2050
2051 RefractionBsdfNode::RefractionBsdfNode()
2052 : BsdfNode(node_type)
2053 {
2054         closure = CLOSURE_BSDF_REFRACTION_ID;
2055         distribution_orig = NBUILTIN_CLOSURES;
2056 }
2057
2058 void RefractionBsdfNode::simplify_settings(Scene *scene)
2059 {
2060         if(distribution_orig == NBUILTIN_CLOSURES) {
2061                 distribution_orig = distribution;
2062         }
2063         Integrator *integrator = scene->integrator;
2064         if(integrator->filter_glossy == 0.0f) {
2065                 /* Fallback to Sharp closure for Roughness close to 0.
2066                  * Note: Keep the epsilon in sync with kernel!
2067                  */
2068                 ShaderInput *roughness_input = input("Roughness");
2069                 if(!roughness_input->link && roughness <= 1e-4f) {
2070                         distribution = CLOSURE_BSDF_REFRACTION_ID;
2071                 }
2072         }
2073         else {
2074                 /* Rollback to original distribution when filter glossy is used. */
2075                 distribution = distribution_orig;
2076         }
2077         closure = distribution;
2078 }
2079
2080 bool RefractionBsdfNode::has_integrator_dependency()
2081 {
2082         ShaderInput *roughness_input = input("Roughness");
2083         return !roughness_input->link && roughness <= 1e-4f;
2084 }
2085
2086 void RefractionBsdfNode::compile(SVMCompiler& compiler)
2087 {
2088         closure = distribution;
2089
2090         if(closure == CLOSURE_BSDF_REFRACTION_ID)
2091                 BsdfNode::compile(compiler, NULL, input("IOR"));
2092         else
2093                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2094 }
2095
2096 void RefractionBsdfNode::compile(OSLCompiler& compiler)
2097 {
2098         compiler.parameter(this, "distribution");
2099         compiler.add(this, "node_refraction_bsdf");
2100 }
2101
2102 /* Toon BSDF Closure */
2103
2104 NODE_DEFINE(ToonBsdfNode)
2105 {
2106         NodeType* type = NodeType::add("toon_bsdf", create, NodeType::SHADER);
2107
2108         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2109         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2110         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2111
2112         static NodeEnum component_enum;
2113         component_enum.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
2114         component_enum.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
2115         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_DIFFUSE_TOON_ID);
2116         SOCKET_IN_FLOAT(size, "Size", 0.5f);
2117         SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
2118
2119         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2120
2121         return type;
2122 }
2123
2124 ToonBsdfNode::ToonBsdfNode()
2125 : BsdfNode(node_type)
2126 {
2127         closure = CLOSURE_BSDF_DIFFUSE_TOON_ID;
2128 }
2129
2130 void ToonBsdfNode::compile(SVMCompiler& compiler)
2131 {
2132         closure = component;
2133         
2134         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
2135 }
2136
2137 void ToonBsdfNode::compile(OSLCompiler& compiler)
2138 {
2139         compiler.parameter(this, "component");
2140         compiler.add(this, "node_toon_bsdf");
2141 }
2142
2143 /* Velvet BSDF Closure */
2144
2145 NODE_DEFINE(VelvetBsdfNode)
2146 {
2147         NodeType* type = NodeType::add("velvet_bsdf", create, NodeType::SHADER);
2148
2149         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2150         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2151         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2152         SOCKET_IN_FLOAT(sigma, "Sigma", 1.0f);
2153
2154         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2155
2156         return type;
2157 }
2158
2159 VelvetBsdfNode::VelvetBsdfNode()
2160 : BsdfNode(node_type)
2161 {
2162         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
2163 }
2164
2165 void VelvetBsdfNode::compile(SVMCompiler& compiler)
2166 {
2167         BsdfNode::compile(compiler, input("Sigma"), NULL);
2168 }
2169
2170 void VelvetBsdfNode::compile(OSLCompiler& compiler)
2171 {
2172         compiler.add(this, "node_velvet_bsdf");
2173 }
2174
2175 /* Diffuse BSDF Closure */
2176
2177 NODE_DEFINE(DiffuseBsdfNode)
2178 {
2179         NodeType* type = NodeType::add("diffuse_bsdf", create, NodeType::SHADER);
2180
2181         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2182         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2183         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2184         SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
2185
2186         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2187
2188         return type;
2189 }
2190
2191 DiffuseBsdfNode::DiffuseBsdfNode()
2192 : BsdfNode(node_type)
2193 {
2194         closure = CLOSURE_BSDF_DIFFUSE_ID;
2195 }
2196
2197 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
2198 {
2199         BsdfNode::compile(compiler, input("Roughness"), NULL);
2200 }
2201
2202 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
2203 {
2204         compiler.add(this, "node_diffuse_bsdf");
2205 }
2206
2207 /* Translucent BSDF Closure */
2208
2209 NODE_DEFINE(TranslucentBsdfNode)
2210 {
2211         NodeType* type = NodeType::add("translucent_bsdf", create, NodeType::SHADER);
2212
2213         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2214         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2215         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2216
2217         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2218
2219         return type;
2220 }
2221
2222 TranslucentBsdfNode::TranslucentBsdfNode()
2223 : BsdfNode(node_type)
2224 {
2225         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
2226 }
2227
2228 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
2229 {
2230         BsdfNode::compile(compiler, NULL, NULL);
2231 }
2232
2233 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
2234 {
2235         compiler.add(this, "node_translucent_bsdf");
2236 }
2237
2238 /* Transparent BSDF Closure */
2239
2240 NODE_DEFINE(TransparentBsdfNode)
2241 {
2242         NodeType* type = NodeType::add("transparent_bsdf", create, NodeType::SHADER);
2243
2244         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2245         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2246
2247         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2248
2249         return type;
2250 }
2251
2252 TransparentBsdfNode::TransparentBsdfNode()
2253 : BsdfNode(node_type)
2254 {
2255         closure = CLOSURE_BSDF_TRANSPARENT_ID;
2256 }
2257
2258 void TransparentBsdfNode::compile(SVMCompiler& compiler)
2259 {
2260         BsdfNode::compile(compiler, NULL, NULL);
2261 }
2262
2263 void TransparentBsdfNode::compile(OSLCompiler& compiler)
2264 {
2265         compiler.add(this, "node_transparent_bsdf");
2266 }
2267
2268 /* Subsurface Scattering Closure */
2269
2270 NODE_DEFINE(SubsurfaceScatteringNode)
2271 {
2272         NodeType* type = NodeType::add("subsurface_scattering", create, NodeType::SHADER);
2273
2274         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2275         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2276         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2277
2278         static NodeEnum falloff_enum;
2279         falloff_enum.insert("Cubic", CLOSURE_BSSRDF_CUBIC_ID);
2280         falloff_enum.insert("Gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
2281         falloff_enum.insert("Burley", CLOSURE_BSSRDF_BURLEY_ID);
2282         SOCKET_ENUM(falloff, "Falloff", falloff_enum, CLOSURE_BSSRDF_BURLEY_ID);
2283         SOCKET_IN_FLOAT(scale, "Scale", 0.01f);
2284         SOCKET_IN_VECTOR(radius, "Radius", make_float3(0.1f, 0.1f, 0.1f));
2285         SOCKET_IN_FLOAT(sharpness, "Sharpness", 0.0f);
2286         SOCKET_IN_FLOAT(texture_blur, "Texture Blur", 1.0f);
2287
2288         SOCKET_OUT_CLOSURE(BSSRDF, "BSSRDF");
2289
2290         return type;
2291 }
2292
2293 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
2294 : BsdfNode(node_type)
2295 {
2296 }
2297
2298 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
2299 {
2300         closure = falloff;
2301         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
2302 }
2303
2304 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
2305 {
2306         compiler.parameter(this, "falloff");
2307         compiler.add(this, "node_subsurface_scattering");
2308 }
2309
2310 bool SubsurfaceScatteringNode::has_bssrdf_bump()
2311 {
2312         /* detect if anything is plugged into the normal input besides the default */
2313         ShaderInput *normal_in = input("Normal");
2314         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
2315 }
2316
2317 /* Emissive Closure */
2318
2319 NODE_DEFINE(EmissionNode)
2320 {
2321         NodeType* type = NodeType::add("emission", create, NodeType::SHADER);
2322
2323         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2324         SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
2325         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2326
2327         SOCKET_OUT_CLOSURE(emission, "Emission");
2328
2329         return type;
2330 }
2331
2332 EmissionNode::EmissionNode()
2333 : ShaderNode(node_type)
2334 {
2335 }
2336
2337 void EmissionNode::compile(SVMCompiler& compiler)
2338 {
2339         ShaderInput *color_in = input("Color");
2340         ShaderInput *strength_in = input("Strength");
2341
2342         if(color_in->link || strength_in->link) {
2343                 compiler.add_node(NODE_EMISSION_WEIGHT,
2344                                   compiler.stack_assign(color_in),
2345                                                   compiler.stack_assign(strength_in));
2346         }
2347         else
2348                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color * strength);
2349
2350         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
2351 }
2352
2353 void EmissionNode::compile(OSLCompiler& compiler)
2354 {
2355         compiler.add(this, "node_emission");
2356 }
2357
2358 bool EmissionNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *)
2359 {
2360         ShaderInput *color_in = input("Color");
2361         ShaderInput *strength_in = input("Strength");
2362
2363         return ((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2364                 (!strength_in->link && strength == 0.0f));
2365 }
2366
2367 /* Background Closure */
2368
2369 NODE_DEFINE(BackgroundNode)
2370 {
2371         NodeType* type = NodeType::add("background_shader", create, NodeType::SHADER);
2372
2373         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2374         SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
2375         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2376
2377         SOCKET_OUT_CLOSURE(background, "Background");
2378
2379         return type;
2380 }
2381
2382 BackgroundNode::BackgroundNode()
2383 : ShaderNode(node_type)
2384 {
2385 }
2386
2387 void BackgroundNode::compile(SVMCompiler& compiler)
2388 {
2389         ShaderInput *color_in = input("Color");
2390         ShaderInput *strength_in = input("Strength");
2391
2392         if(color_in->link || strength_in->link) {
2393                 compiler.add_node(NODE_EMISSION_WEIGHT,
2394                                   compiler.stack_assign(color_in),
2395                                                   compiler.stack_assign(strength_in));
2396         }
2397         else
2398                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color*strength);
2399
2400         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
2401 }
2402
2403 void BackgroundNode::compile(OSLCompiler& compiler)
2404 {
2405         compiler.add(this, "node_background");
2406 }
2407
2408 bool BackgroundNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *)
2409 {
2410         ShaderInput *color_in = input("Color");
2411         ShaderInput *strength_in = input("Strength");
2412
2413         return ((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
2414                 (!strength_in->link && strength == 0.0f));
2415 }
2416
2417 /* Holdout Closure */
2418
2419 NODE_DEFINE(HoldoutNode)
2420 {
2421         NodeType* type = NodeType::add("holdout", create, NodeType::SHADER);
2422
2423         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2424         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2425
2426         SOCKET_OUT_CLOSURE(holdout, "Holdout");
2427
2428         return type;
2429 }
2430
2431 HoldoutNode::HoldoutNode()
2432 : ShaderNode(node_type)
2433 {
2434 }
2435
2436 void HoldoutNode::compile(SVMCompiler& compiler)
2437 {
2438         float3 value = make_float3(1.0f, 1.0f, 1.0f);
2439
2440         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
2441         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
2442 }
2443
2444 void HoldoutNode::compile(OSLCompiler& compiler)
2445 {
2446         compiler.add(this, "node_holdout");
2447 }
2448
2449 /* Ambient Occlusion */
2450
2451 NODE_DEFINE(AmbientOcclusionNode)
2452 {
2453         NodeType* type = NodeType::add("ambient_occlusion", create, NodeType::SHADER);
2454
2455         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2456         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2457         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2458
2459         SOCKET_OUT_CLOSURE(AO, "AO");
2460
2461         return type;
2462 }
2463
2464 AmbientOcclusionNode::AmbientOcclusionNode()
2465 : ShaderNode(node_type)
2466 {
2467 }
2468
2469 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
2470 {
2471         ShaderInput *color_in = input("Color");
2472
2473         if(color_in->link)
2474                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2475         else
2476                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2477
2478         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
2479 }
2480
2481 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
2482 {
2483         compiler.add(this, "node_ambient_occlusion");
2484 }
2485
2486 /* Volume Closure */
2487
2488 VolumeNode::VolumeNode(const NodeType *node_type)
2489 : ShaderNode(node_type)
2490 {
2491         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2492 }
2493
2494 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
2495 {
2496         ShaderInput *color_in = input("Color");
2497
2498         if(color_in->link)
2499                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2500         else
2501                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
2502         
2503         compiler.add_node(NODE_CLOSURE_VOLUME,
2504                 compiler.encode_uchar4(closure,
2505                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
2506                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
2507                         compiler.closure_mix_weight_offset()),
2508                 __float_as_int((param1)? get_float(param1->socket_type): 0.0f),
2509                 __float_as_int((param2)? get_float(param2->socket_type): 0.0f));
2510 }
2511
2512 void VolumeNode::compile(SVMCompiler& compiler)
2513 {
2514         compile(compiler, NULL, NULL);
2515 }
2516
2517 void VolumeNode::compile(OSLCompiler& /*compiler*/)
2518 {
2519         assert(0);
2520 }
2521
2522 /* Absorption Volume Closure */
2523
2524 NODE_DEFINE(AbsorptionVolumeNode)
2525 {
2526         NodeType* type = NodeType::add("absorption_volume", create, NodeType::SHADER);
2527
2528         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2529         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2530         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2531
2532         SOCKET_OUT_CLOSURE(volume, "Volume");
2533
2534         return type;
2535 }
2536
2537 AbsorptionVolumeNode::AbsorptionVolumeNode()
2538 : VolumeNode(node_type)
2539 {
2540         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2541 }
2542
2543 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2544 {
2545         VolumeNode::compile(compiler, input("Density"), NULL);
2546 }
2547
2548 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2549 {
2550         compiler.add(this, "node_absorption_volume");
2551 }
2552
2553 /* Scatter Volume Closure */
2554
2555 NODE_DEFINE(ScatterVolumeNode)
2556 {
2557         NodeType* type = NodeType::add("scatter_volume", create, NodeType::SHADER);
2558
2559         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2560         SOCKET_IN_FLOAT(density, "Density", 1.0f);
2561         SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
2562         SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2563
2564         SOCKET_OUT_CLOSURE(volume, "Volume");
2565
2566         return type;
2567 }
2568
2569 ScatterVolumeNode::ScatterVolumeNode()
2570 : VolumeNode(node_type)
2571 {
2572         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2573 }
2574
2575 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2576 {
2577         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2578 }
2579
2580 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2581 {
2582         compiler.add(this, "node_scatter_volume");
2583 }
2584
2585 /* Hair BSDF Closure */
2586
2587 NODE_DEFINE(HairBsdfNode)
2588 {
2589         NodeType* type = NodeType::add("hair_bsdf", create, NodeType::SHADER);
2590
2591         SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
2592         SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
2593         SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
2594
2595         static NodeEnum component_enum;
2596         component_enum.insert("Reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2597         component_enum.insert("Transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2598         SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_HAIR_REFLECTION_ID);
2599         SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
2600         SOCKET_IN_FLOAT(roughness_u, "RoughnessU", 0.2f);
2601         SOCKET_IN_FLOAT(roughness_v, "RoughnessV", 0.2f);
2602         SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f));
2603
2604         SOCKET_OUT_CLOSURE(BSDF, "BSDF");
2605
2606         return type;
2607 }
2608
2609 HairBsdfNode::HairBsdfNode()
2610 : BsdfNode(node_type)
2611 {
2612         closure = CLOSURE_BSDF_HAIR_REFLECTION_ID;
2613 }
2614
2615 void HairBsdfNode::compile(SVMCompiler& compiler)
2616 {
2617         closure = component;
2618
2619         BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
2620 }
2621
2622 void HairBsdfNode::compile(OSLCompiler& compiler)
2623 {
2624         compiler.parameter(this, "component");
2625         compiler.add(this, "node_hair_bsdf");
2626 }
2627
2628 /* Geometry */
2629
2630 NODE_DEFINE(GeometryNode)
2631 {
2632         NodeType* type = NodeType::add("geometry", create, NodeType::SHADER);
2633
2634         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2635
2636         SOCKET_OUT_POINT(position, "Position");
2637         SOCKET_OUT_NORMAL(normal, "Normal");
2638         SOCKET_OUT_NORMAL(tangent, "Tangent");
2639         SOCKET_OUT_NORMAL(true_normal, "True Normal");
2640         SOCKET_OUT_VECTOR(incoming, "Incoming");
2641         SOCKET_OUT_POINT(parametric, "Parametric");
2642         SOCKET_OUT_FLOAT(backfacing, "Backfacing");
2643         SOCKET_OUT_FLOAT(pointiness, "Pointiness");
2644
2645         return type;
2646 }
2647
2648 GeometryNode::GeometryNode()
2649 : ShaderNode(node_type)
2650 {
2651         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2652 }
2653
2654 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2655 {
2656         if(shader->has_surface) {
2657                 if(!output("Tangent")->links.empty()) {
2658                         attributes->add(ATTR_STD_GENERATED);
2659                 }
2660                 if(!output("Pointiness")->links.empty()) {
2661                         attributes->add(ATTR_STD_POINTINESS);
2662                 }
2663         }
2664
2665         ShaderNode::attributes(shader, attributes);
2666 }
2667
2668 void GeometryNode::compile(SVMCompiler& compiler)
2669 {
2670         ShaderOutput *out;
2671         ShaderNodeType geom_node = NODE_GEOMETRY;
2672         ShaderNodeType attr_node = NODE_ATTR;
2673
2674         if(bump == SHADER_BUMP_DX) {
2675                 geom_node = NODE_GEOMETRY_BUMP_DX;
2676                 attr_node = NODE_ATTR_BUMP_DX;
2677         }
2678         else if(bump == SHADER_BUMP_DY) {
2679                 geom_node = NODE_GEOMETRY_BUMP_DY;
2680                 attr_node = NODE_ATTR_BUMP_DY;
2681         }
2682         
2683         out = output("Position");
2684         if(!out->links.empty()) {
2685                 compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
2686         }
2687
2688         out = output("Normal");
2689         if(!out->links.empty()) {
2690                 compiler.add_node(geom_node, NODE_GEOM_N, compiler.stack_assign(out));
2691         }
2692
2693         out = output("Tangent");
2694         if(!out->links.empty()) {
2695                 compiler.add_node(geom_node, NODE_GEOM_T, compiler.stack_assign(out));
2696         }
2697
2698         out = output("True Normal");
2699         if(!out->links.empty()) {
2700                 compiler.add_node(geom_node, NODE_GEOM_Ng, compiler.stack_assign(out));
2701         }
2702
2703         out = output("Incoming");
2704         if(!out->links.empty()) {
2705                 compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
2706         }
2707
2708         out = output("Parametric");
2709         if(!out->links.empty()) {
2710                 compiler.add_node(geom_node, NODE_GEOM_uv, compiler.stack_assign(out));
2711         }
2712
2713         out = output("Backfacing");
2714         if(!out->links.empty()) {
2715                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, compiler.stack_assign(out));
2716         }
2717
2718         out = output("Pointiness");
2719         if(!out->links.empty()) {
2720                 if(compiler.output_type() != SHADER_TYPE_VOLUME) {
2721                         compiler.add_node(attr_node,
2722                                           ATTR_STD_POINTINESS,
2723                                           compiler.stack_assign(out),
2724                                           NODE_ATTR_FLOAT);
2725                 }
2726                 else {
2727                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), compiler.stack_assign(out));
2728                 }
2729         }
2730 }
2731
2732 void GeometryNode::compile(OSLCompiler& compiler)
2733 {
2734         if(bump == SHADER_BUMP_DX)
2735                 compiler.parameter("bump_offset", "dx");
2736         else if(bump == SHADER_BUMP_DY)
2737                 compiler.parameter("bump_offset", "dy");
2738         else
2739                 compiler.parameter("bump_offset", "center");
2740
2741         compiler.add(this, "node_geometry");
2742 }
2743
2744 /* TextureCoordinate */
2745
2746 NODE_DEFINE(TextureCoordinateNode)
2747 {
2748         NodeType* type = NodeType::add("texture_coordinate", create, NodeType::SHADER);
2749
2750         SOCKET_BOOLEAN(from_dupli, "From Dupli", false);
2751         SOCKET_BOOLEAN(use_transform, "Use Transform", false);
2752         SOCKET_TRANSFORM(ob_tfm, "Object Transform", transform_identity());
2753
2754         SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2755
2756         SOCKET_OUT_POINT(generated, "Generated");
2757         SOCKET_OUT_NORMAL(normal, "Normal");
2758         SOCKET_OUT_POINT(UV, "UV");
2759         SOCKET_OUT_POINT(object, "Object");
2760         SOCKET_OUT_POINT(camera, "Camera");
2761         SOCKET_OUT_POINT(window, "Window");
2762         SOCKET_OUT_NORMAL(reflection, "Reflection");
2763
2764         return type;
2765 }
2766
2767 TextureCoordinateNode::TextureCoordinateNode()
2768 : ShaderNode(node_type)
2769 {
2770 }
2771
2772 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2773 {
2774         if(shader->has_surface) {
2775                 if(!from_dupli) {
2776                         if(!output("Generated")->links.empty())
2777                                 attributes->add(ATTR_STD_GENERATED);
2778                         if(!output("UV")->links.empty())
2779                                 attributes->add(ATTR_STD_UV);
2780                 }
2781         }
2782
2783         if(shader->has_volume) {
2784                 if(!from_dupli) {
2785                         if(!output("Generated")->links.empty()) {
2786                                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
2787                         }
2788                 }
2789         }
2790
2791         ShaderNode::attributes(shader, attributes);
2792 }
2793
2794 void TextureCoordinateNode::compile(SVMCompiler& compiler)
2795 {
2796         ShaderOutput *out;
2797         ShaderNodeType texco_node = NODE_TEX_COORD;
2798         ShaderNodeType attr_node = NODE_ATTR;
2799         ShaderNodeType geom_node = NODE_GEOMETRY;
2800
2801         if(bump == SHADER_BUMP_DX) {
2802                 texco_node = NODE_TEX_COORD_BUMP_DX;
2803                 attr_node = NODE_ATTR_BUMP_DX;
2804                 geom_node = NODE_GEOMETRY_BUMP_DX;
2805         }
2806         else if(bump == SHADER_BUMP_DY) {
2807                 texco_node = NODE_TEX_COORD_BUMP_DY;
2808                 attr_node = NODE_ATTR_BUMP_DY;
2809                 geom_node = NODE_GEOMETRY_BUMP_DY;
2810         }
2811         
2812         out = output("Generated");
2813         if(!out->links.empty()) {
2814                 if(compiler.background) {
2815                         compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
2816                 }
2817                 else {
2818                         if(from_dupli) {
2819                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, compiler.stack_assign(out));
2820                         }
2821                         else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
2822                                 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, compiler.stack_assign(out));
2823                         }
2824                         else {
2825                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
2826                                 compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2827                         }
2828                 }
2829         }
2830
2831         out = output("Normal");
2832         if(!out->links.empty()) {
2833                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, compiler.stack_assign(out));
2834         }
2835
2836         out = output("UV");
2837         if(!out->links.empty()) {
2838                 if(from_dupli) {
2839                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
2840                 }
2841                 else {
2842                         int attr = compiler.attribute(ATTR_STD_UV);
2843                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2844                 }
2845         }
2846
2847         out = output("Object");
2848         if(!out->links.empty()) {
2849                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, compiler.stack_assign(out), use_transform);
2850                 if(use_transform) {
2851                         Transform ob_itfm = transform_inverse(ob_tfm);
2852                         compiler.add_node(ob_itfm.x);
2853                         compiler.add_node(ob_itfm.y);
2854                         compiler.add_node(ob_itfm.z);
2855                         compiler.add_node(ob_itfm.w);
2856                 }
2857         }
2858
2859         out = output("Camera");
2860         if(!out->links.empty()) {
2861                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, compiler.stack_assign(out));
2862         }
2863
2864         out = output("Window");
2865         if(!out->links.empty()) {
2866                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, compiler.stack_assign(out));
2867         }
2868
2869         out = output("Reflection");
2870         if(!out->links.empty()) {
2871                 if(compiler.background) {
2872                         compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
2873                 }
2874                 else {
2875                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, compiler.stack_assign(out));
2876                 }
2877         }
2878 }
2879
2880 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2881 {
2882         if(bump == SHADER_BUMP_DX)
2883                 compiler.parameter("bump_offset", "dx");
2884         else if(bump == SHADER_BUMP_DY)
2885                 compiler.parameter("bump_offset", "dy");
2886         else
2887                 compiler.parameter("bump_offset", "center");
2888         
2889         if(compiler.background)
2890                 compiler.parameter("is_background", true);
2891         if(compiler.output_type() == SHADER_TYPE_VOLUME)
2892                 compiler.parameter("is_volume", true);
2893         compiler.parameter(this, "use_transform");
2894         Transform ob_itfm = transform_transpose(transform_inverse(ob_tfm));
2895         compiler.parameter("object_itfm", ob_itfm);
2896
2897         compiler.parameter(this, "from_dupli");
2898
2899         compiler.add(this, "node_texture_coordinate");
2900 }
2901
2902 /* UV Map */
2903
2904 NODE_DEFINE(UVMapNode)
2905 {
2906         NodeType* type = NodeType::add("uvmap", create, NodeType::SHADER);
2907
2908         SOCKET_IN_STRING(attribute, "attribute", ustring(""));
2909         SOCKET_IN_BOOLEAN(from_dupli, "from dupli", false);
2910
2911         SOCKET_OUT_POINT(UV, "UV");
2912
2913         return type;
2914 }
2915
2916 UVMapNode::UVMapNode()
2917 : ShaderNode(node_type)
2918 {
2919 }
2920
2921 void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2922 {
2923         if(shader->has_surface) {
2924                 if(!from_dupli) {
2925                         if(!output("UV")->links.empty()) {
2926                                 if(attribute != "")
2927                                         attributes->add(attribute);
2928                                 else
2929                                         attributes->add(ATTR_STD_UV);
2930                         }
2931                 }
2932         }
2933
2934         ShaderNode::attributes(shader, attributes);
2935 }
2936
2937 void UVMapNode::compile(SVMCompiler& compiler)
2938 {
2939         ShaderOutput *out = output("UV");
2940         ShaderNodeType texco_node = NODE_TEX_COORD;
2941         ShaderNodeType attr_node = NODE_ATTR;
2942         int attr;
2943
2944         if(bump == SHADER_BUMP_DX) {
2945                 texco_node = NODE_TEX_COORD_BUMP_DX;
2946                 attr_node = NODE_ATTR_BUMP_DX;
2947         }
2948         else if(bump == SHADER_BUMP_DY) {
2949                 texco_node = NODE_TEX_COORD_BUMP_DY;
2950                 attr_node = NODE_ATTR_BUMP_DY;
2951         }
2952
2953         if(!out->links.empty()) {
2954                 if(from_dupli) {
2955                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
2956                 }
2957                 else {
2958                         if(attribute != "")
2959                                 attr = compiler.attribute(attribute);
2960                         else
2961                                 attr = compiler.attribute(ATTR_STD_UV);
2962
2963                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2964                 }
2965         }
2966 }
2967
2968 void UVMapNode::compile(OSLCompiler& compiler)
2969 {
2970         if(bump == SHADER_BUMP_DX)
2971                 compiler.parameter("bump_offset", "dx");
2972         else if(bump == SHADER_BUMP_DY)
2973                 compiler.parameter("bump_offset", "dy");
2974         else
2975                 compiler.parameter("bump_offset", "center");
2976
2977         compiler.parameter(this, "from_dupli");
2978         compiler.parameter("name", attribute.c_str());
2979         compiler.add(this, "node_uv_map");
2980 }
2981
2982 /* Light Path */
2983
2984 NODE_DEFINE(LightPathNode)
2985 {
2986         NodeType* type = NodeType::add("light_path", create, NodeType::SHADER);
2987
2988         SOCKET_OUT_FLOAT(is_camera_ray, "Is Camera Ray");
2989         SOCKET_OUT_FLOAT(is_shadow_ray, "Is Shadow Ray");
2990         SOCKET_OUT_FLOAT(is_diffus_ray, "Is Diffus Ray");
2991         SOCKET_OUT_FLOAT(is_glossy_ray, "Is Glossy Ray");
2992         SOCKET_OUT_FLOAT(is_singular_ray, "Is Singular Ray");
2993         SOCKET_OUT_FLOAT(is_reflection_ray, "Is Reflection Ray");
2994         SOCKET_OUT_FLOAT(is_transmission_ray, "Is Transmission Ray");
2995         SOCKET_OUT_FLOAT(is_volume_scatter_ray, "Is Volume Scatter Ray");
2996         SOCKET_OUT_FLOAT(ray_length, "Ray Length");
2997         SOCKET_OUT_FLOAT(ray_depth, "Ray Depth");
2998         SOCKET_OUT_FLOAT(transparent_depth, "Transparent Depth");
2999         SOCKET_OUT_FLOAT(transmission_depth, "Transmission Depth");
3000
3001         return type;
3002 }
3003
3004 LightPathNode::LightPathNode()
3005 : ShaderNode(node_type)
3006 {
3007 }
3008
3009 void LightPathNode::compile(SVMCompiler& compiler)
3010 {
3011         ShaderOutput *out;
3012
3013         out = output("Is Camera Ray");
3014         if(!out->links.empty()) {
3015                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, compiler.stack_assign(out));
3016         }
3017
3018         out = output("Is Shadow Ray");
3019         if(!out->links.empty()) {
3020                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, compiler.stack_assign(out));
3021         }
3022
3023         out = output("Is Diffuse Ray");
3024         if(!out->links.empty()) {
3025                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, compiler.stack_assign(out));
3026         }
3027
3028         out = output("Is Glossy Ray");
3029         if(!out->links.empty()) {
3030                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, compiler.stack_assign(out));
3031         }
3032
3033         out = output("Is Singular Ray");
3034         if(!out->links.empty()) {
3035                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, compiler.stack_assign(out));
3036         }
3037
3038         out = output("Is Reflection Ray");
3039         if(!out->links.empty()) {
3040                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, compiler.stack_assign(out));
3041         }
3042
3043
3044         out = output("Is Transmission Ray");
3045         if(!out->links.empty()) {
3046                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, compiler.stack_assign(out));
3047         }
3048         
3049         out = output("Is Volume Scatter Ray");
3050         if(!out->links.empty()) {
3051                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, compiler.stack_assign(out));
3052         }
3053
3054         out = output("Ray Length");
3055         if(!out->links.empty()) {
3056                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, compiler.stack_assign(out));
3057         }
3058         
3059         out = output("Ray Depth");
3060         if(!out->links.empty()) {
3061                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, compiler.stack_assign(out));
3062         }
3063
3064         out = output("Transparent Depth");
3065         if(!out->links.empty()) {
3066                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, compiler.stack_assign(out));
3067         }
3068
3069         out = output("Transmission Depth");
3070         if(!out->links.empty()) {
3071                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transmission, compiler.stack_assign(out));
3072         }
3073 }
3074
3075 void LightPathNode::compile(OSLCompiler& compiler)
3076 {
3077         compiler.add(this, "node_light_path");
3078 }
3079
3080 /* Light Falloff */
3081
3082 NODE_DEFINE(LightFalloffNode)
3083 {
3084         NodeType* type = NodeType::add("light_fallof", create, NodeType::SHADER);
3085
3086         SOCKET_IN_FLOAT(strength, "Strength", 100.0f);
3087         SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
3088
3089         SOCKET_OUT_FLOAT(quadratic, "Quadratic");
3090         SOCKET_OUT_FLOAT(linear, "Linear");
3091         SOCKET_OUT_FLOAT(constant, "Constant");
3092
3093         return type;
3094 }
3095
3096 LightFalloffNode::LightFalloffNode()
3097 : ShaderNode(node_type)
3098 {
3099 }
3100
3101 void LightFalloffNode::compile(SVMCompiler& compiler)
3102 {
3103         ShaderInput *strength_in = input("Strength");
3104         ShaderInput *smooth_in = input("Smooth");
3105
3106         ShaderOutput *out = output("Quadratic");
3107         if(!out->links.empty()) {
3108                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
3109                         compiler.encode_uchar4(
3110                                 compiler.stack_assign(strength_in),
3111                                 compiler.stack_assign(smooth_in),
3112                                 compiler.stack_assign(out)));
3113         }
3114
3115         out = output("Linear");
3116         if(!out->links.empty()) {
3117                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
3118                         compiler.encode_uchar4(
3119                                 compiler.stack_assign(strength_in),
3120                                 compiler.stack_assign(smooth_in),
3121                                 compiler.stack_assign(out)));
3122         }
3123
3124         out = output("Constant");
3125         if(!out->links.empty()) {
3126                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
3127                         compiler.encode_uchar4(
3128                                 compiler.stack_assign(strength_in),
3129                                 compiler.stack_assign(smooth_in),
3130                                 compiler.stack_assign(out)));
3131         }
3132 }
3133
3134 void LightFalloffNode::compile(OSLCompiler& compiler)
3135 {
3136         compiler.add(this, "node_light_falloff");
3137 }
3138
3139 /* Object Info */
3140
3141 NODE_DEFINE(ObjectInfoNode)
3142 {
3143         NodeType* type = NodeType::add("object_info", create, NodeType::SHADER);
3144
3145         SOCKET_OUT_VECTOR(location, "Location");
3146         SOCKET_OUT_FLOAT(object_index, "Object Index");
3147         SOCKET_OUT_FLOAT(material_index, "Material Index");
3148         SOCKET_OUT_FLOAT(random, "Random");
3149
3150         return type;
3151 }
3152
3153 ObjectInfoNode::ObjectInfoNode()
3154 : ShaderNode(node_type)
3155 {
3156 }
3157
3158 void ObjectInfoNode::compile(SVMCompiler& compiler)
3159 {
3160         ShaderOutput *out = output("Location");
3161         if(!out->links.empty()) {
3162                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, compiler.stack_assign(out));
3163         }
3164
3165         out = output("Object Index");
3166         if(!out->links.empty()) {
3167                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, compiler.stack_assign(out));
3168         }
3169
3170         out = output("Material Index");
3171         if(!out->links.empty()) {
3172                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, compiler.stack_assign(out));
3173         }
3174
3175         out = output("Random");
3176         if(!out->links.empty()) {
3177                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, compiler.stack_assign(out));
3178         }
3179 }
3180
3181 void ObjectInfoNode::compile(OSLCompiler& compiler)
3182 {
3183         compiler.add(this, "node_object_info");
3184 }
3185
3186 /* Particle Info */
3187
3188 NODE_DEFINE(ParticleInfoNode)
3189 {
3190         NodeType* type = NodeType::add("particle_info", create, NodeType::SHADER);
3191
3192         SOCKET_OUT_FLOAT(index, "Index");
3193         SOCKET_OUT_FLOAT(age, "Age");
3194         SOCKET_OUT_FLOAT(lifetime, "Lifetime");
3195         SOCKET_OUT_POINT(location, "Location");
3196 #if 0   /* not yet supported */
3197         SOCKET_OUT_QUATERNION(rotation, "Rotation");
3198 #endif
3199         SOCKET_OUT_FLOAT(size, "Size");
3200         SOCKET_OUT_VECTOR(velocity, "Velocity");
3201         SOCKET_OUT_VECTOR(angular_velocity, "Angular Velocity");
3202
3203         return type;
3204 }
3205
3206 ParticleInfoNode::ParticleInfoNode()
3207 : ShaderNode(node_type)
3208 {
3209 }
3210
3211 void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3212 {
3213         if(!output("Index")->links.empty())
3214                 attributes->add(ATTR_STD_PARTICLE);
3215         if(!output("Age")->links.empty())
3216                 attributes->add(ATTR_STD_PARTICLE);
3217         if(!output("Lifetime")->links.empty())
3218                 attributes->add(ATTR_STD_PARTICLE);
3219         if(!output("Location")->links.empty())
3220                 attributes->add(ATTR_STD_PARTICLE);
3221 #if 0   /* not yet supported */
3222         if(!output("Rotation")->links.empty())
3223                 attributes->add(ATTR_STD_PARTICLE);
3224 #endif
3225         if(!output("Size")->links.empty())
3226                 attributes->add(ATTR_STD_PARTICLE);
3227         if(!output("Velocity")->links.empty())
3228                 attributes->add(ATTR_STD_PARTICLE);
3229         if(!output("Angular Velocity")->links.empty())
3230                 attributes->add(ATTR_STD_PARTICLE);
3231
3232         ShaderNode::attributes(shader, attributes);
3233 }
3234
3235 void ParticleInfoNode::compile(SVMCompiler& compiler)
3236 {
3237         ShaderOutput *out;
3238         
3239         out = output("Index");
3240         if(!out->links.empty()) {
3241                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, compiler.stack_assign(out));
3242         }
3243         
3244         out = output("Age");
3245         if(!out->links.empty()) {
3246                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, compiler.stack_assign(out));
3247         }
3248         
3249         out = output("Lifetime");
3250         if(!out->links.empty()) {
3251                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, compiler.stack_assign(out));
3252         }
3253         
3254         out = output("Location");
3255         if(!out->links.empty()) {
3256                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, compiler.stack_assign(out));
3257         }
3258         
3259         /* quaternion data is not yet supported by Cycles */
3260 #if 0
3261         out = output("Rotation");
3262         if(!out->links.empty()) {
3263                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, compiler.stack_assign(out));
3264         }
3265 #endif
3266         
3267         out = output("Size");
3268         if(!out->links.empty()) {
3269                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, compiler.stack_assign(out));
3270         }
3271         
3272         out = output("Velocity");
3273         if(!out->links.empty()) {
3274                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, compiler.stack_assign(out));
3275         }
3276         
3277         out = output("Angular Velocity");
3278         if(!out->links.empty()) {
3279                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, compiler.stack_assign(out));
3280         }
3281 }
3282
3283 void ParticleInfoNode::compile(OSLCompiler& compiler)
3284 {
3285         compiler.add(this, "node_particle_info");
3286 }
3287
3288 /* Hair Info */
3289
3290 NODE_DEFINE(HairInfoNode)
3291 {
3292         NodeType* type = NodeType::add("hair_info", create, NodeType::SHADER);
3293
3294         SOCKET_OUT_FLOAT(is_strand, "Is Strand");
3295         SOCKET_OUT_FLOAT(intercept, "Intercept");
3296         SOCKET_OUT_FLOAT(thickness, "Thickness");
3297         SOCKET_OUT_NORMAL(tangent Normal, "Tangent Normal");
3298 #if 0 /*output for minimum hair width transparency - deactivated */
3299         SOCKET_OUT_FLOAT(fade, "Fade");
3300 #endif
3301
3302         return type;
3303 }
3304
3305 HairInfoNode::HairInfoNode()
3306 : ShaderNode(node_type)
3307 {
3308 }
3309
3310 void HairInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3311 {
3312         if(shader->has_surface) {
3313                 ShaderOutput *intercept_out = output("Intercept");
3314
3315                 if(!intercept_out->links.empty())
3316                         attributes->add(ATTR_STD_CURVE_INTERCEPT);
3317         }
3318
3319         ShaderNode::attributes(shader, attributes);
3320 }
3321
3322 void HairInfoNode::compile(SVMCompiler& compiler)
3323 {
3324         ShaderOutput *out;
3325         
3326         out = output("Is Strand");
3327         if(!out->links.empty()) {
3328                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, compiler.stack_assign(out));
3329         }
3330
3331         out = output("Intercept");
3332         if(!out->links.empty()) {
3333                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
3334                 compiler.add_node(NODE_ATTR, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT);
3335         }
3336
3337         out = output("Thickness");
3338         if(!out->links.empty()) {
3339                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, compiler.stack_assign(out));
3340         }
3341
3342         out = output("Tangent Normal");
3343         if(!out->links.empty()) {
3344                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, compiler.stack_assign(out));
3345         }
3346
3347         /*out = output("Fade");
3348         if(!out->links.empty()) {
3349                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, compiler.stack_assign(out));
3350         }*/
3351
3352 }
3353
3354 void HairInfoNode::compile(OSLCompiler& compiler)
3355 {
3356         compiler.add(this, "node_hair_info");
3357 }
3358
3359 /* Value */
3360
3361 NODE_DEFINE(ValueNode)
3362 {
3363         NodeType* type = NodeType::add("value", create, NodeType::SHADER);
3364
3365         SOCKET_FLOAT(value, "Value", 0.0f);
3366         SOCKET_OUT_FLOAT(value, "Value");
3367
3368         return type;
3369 }
3370
3371 ValueNode::ValueNode()
3372 : ShaderNode(node_type)
3373 {
3374 }
3375
3376 bool ValueNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
3377 {
3378         optimized->set(value);
3379         return true;
3380 }
3381
3382 void ValueNode::compile(SVMCompiler& compiler)
3383 {
3384         ShaderOutput *val_out = output("Value");
3385
3386         compiler.add_node(NODE_VALUE_F, __float_as_int(value), compiler.stack_assign(val_out));
3387 }
3388
3389 void ValueNode::compile(OSLCompiler& compiler)
3390 {
3391         compiler.parameter("value_value", value);
3392         compiler.add(this, "node_value");
3393 }
3394
3395 /* Color */
3396
3397 NODE_DEFINE(ColorNode)
3398 {
3399         NodeType* type = NodeType::add("color", create, NodeType::SHADER);
3400
3401         SOCKET_COLOR(value, "Value", make_float3(0.0f, 0.0f, 0.0f));
3402         SOCKET_OUT_COLOR(color, "Color");
3403
3404         return type;
3405 }
3406
3407 ColorNode::ColorNode()
3408 : ShaderNode(node_type)
3409 {
3410 }
3411
3412 bool ColorNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
3413 {
3414         optimized->set(value);
3415         return true;
3416 }
3417
3418 void ColorNode::compile(SVMCompiler& compiler)
3419 {
3420         ShaderOutput *color_out = output("Color");
3421
3422         if(!color_out->links.empty()) {
3423                 compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
3424                 compiler.add_node(NODE_VALUE_V, value);
3425         }
3426 }
3427
3428 void ColorNode::compile(OSLCompiler& compiler)
3429 {
3430         compiler.parameter_color("color_value", value);
3431
3432         compiler.add(this, "node_value");
3433 }
3434
3435 /* Add Closure */
3436
3437 NODE_DEFINE(AddClosureNode)
3438 {
3439         NodeType* type = NodeType::add("add_closure", create, NodeType::SHADER);
3440
3441         SOCKET_IN_CLOSURE(closure1, "Closure1");
3442         SOCKET_IN_CLOSURE(closure2, "Closure2");
3443         SOCKET_OUT_CLOSURE(closure, "Closure");
3444
3445         return type;
3446 }
3447
3448 AddClosureNode::AddClosureNode()
3449 : ShaderNode(node_type)
3450 {
3451         special_type = SHADER_SPECIAL_TYPE_COMBINE_CLOSURE;
3452 }
3453
3454 void AddClosureNode::compile(SVMCompiler& /*compiler*/)
3455 {
3456         /* handled in the SVM compiler */
3457 }
3458
3459 void AddClosureNode::compile(OSLCompiler& compiler)
3460 {
3461         compiler.add(this, "node_add_closure");
3462 }
3463
3464 /* Mix Closure */
3465
3466 NODE_DEFINE(MixClosureNode)
3467 {
3468         NodeType* type = NodeType::add("mix_closure", create, NodeType::SHADER);
3469
3470         SOCKET_IN_FLOAT(fac, "Fac", 0.5f);
3471         SOCKET_IN_CLOSURE(closure1, "Closure1");
3472         SOCKET_IN_CLOSURE(closure2, "Closure2");
3473
3474         SOCKET_OUT_CLOSURE(closure, "Closure");
3475
3476         return type;
3477 }
3478
3479 MixClosureNode::MixClosureNode()
3480 : ShaderNode(node_type)
3481 {
3482         special_type = SHADER_SPECIAL_TYPE_COMBINE_CLOSURE;
3483 }
3484
3485 void MixClosureNode::compile(SVMCompiler& /*compiler*/)
3486 {
3487         /* handled in the SVM compiler */
3488 }
3489
3490 void MixClosureNode::compile(OSLCompiler& compiler)
3491 {
3492         compiler.add(this, "node_mix_closure");
3493 }
3494
3495 bool MixClosureNode::constant_fold(ShaderGraph *graph, ShaderOutput *, ShaderInput *)
3496 {
3497         ShaderInput *fac_in = input("Fac");
3498         ShaderInput *closure1_in = input("Closure1");
3499         ShaderInput *closure2_in = input("Closure2");
3500         ShaderOutput *closure_out = output("Closure");
3501
3502         /* remove useless mix closures nodes */
3503         if(closure1_in->link == closure2_in->link) {
3504                 graph->relink(this, closure_out, closure1_in->link);
3505                 return true;
3506         }
3507
3508         /* remove unused mix closure input when factor is 0.0 or 1.0 */
3509         /* check for closure links and make sure factor link is disconnected */
3510         if(closure1_in->link && closure2_in->link && !fac_in->link) {
3511                 /* factor 0.0 */
3512                 if(fac == 0.0f) {
3513                         graph->relink(this, closure_out, closure1_in->link);
3514                         return true;
3515                 }
3516                 /* factor 1.0 */
3517                 else if(fac == 1.0f) {
3518                         graph->relink(this, closure_out, closure2_in->link);
3519                         return true;
3520                 }
3521         }
3522
3523         return false;
3524 }
3525
3526 /* Mix Closure */
3527
3528 NODE_DEFINE(MixClosureWeightNode)
3529 {
3530         NodeType* type = NodeType::add("mix_closure_weight", create, NodeType::SHADER);
3531
3532         SOCKET_IN_FLOAT(weight, "Weight", 1.0f);
3533         SOCKET_IN_FLOAT(fac, "Fac", 1.0f);
3534
3535         SOCKET_OUT_FLOAT(weight1, "Weight1");
3536         SOCKET_OUT_FLOAT(weight2, "Weight2");
3537
3538         return type;
3539 }
3540
3541 MixClosureWeightNode::MixClosureWeightNode()
3542 : ShaderNode(node_type)
3543 {
3544 }
3545
3546 void MixClosureWeightNode::compile(SVMCompiler& compiler)
3547 {
3548         ShaderInput *weight_in = input("Weight");
3549         ShaderInput *fac_in = input("Fac");
3550         ShaderOutput *weight1_out = output("Weight1");
3551         ShaderOutput *weight2_out = output("Weight2");
3552
3553         compiler.add_node(NODE_MIX_CLOSURE,
3554                 compiler.encode_uchar4(
3555                         compiler.stack_assign(fac_in),
3556                         compiler.stack_assign(weight_in),
3557                         compiler.stack_assign(weight1_out),
3558                         compiler.stack_assign(weight2_out)));
3559 }
3560
3561 void MixClosureWeightNode::compile(OSLCompiler& /*compiler*/)
3562 {
3563         assert(0);
3564 }
3565
3566 /* Invert */
3567
3568 NODE_DEFINE(InvertNode)
3569 {
3570         NodeType* type = NodeType::add("invert", create, NodeType::SHADER);
3571
3572         SOCKET_IN_FLOAT(fac, "Fac", 1.0f);
3573         SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
3574
3575         SOCKET_OUT_COLOR(color, "Color");
3576
3577         return type;
3578 }
3579
3580 InvertNode::InvertNode()
3581 : ShaderNode(node_type)
3582 {
3583 }
3584
3585 void InvertNode::compile(SVMCompiler& compiler)
3586 {
3587         ShaderInput *fac_in = input("Fac");
3588         ShaderInput *color_in = input("Color");
3589         ShaderOutput *color_out = output("Color");
3590
3591         compiler.add_node(NODE_INVERT,
3592                 compiler.stack_assign(fac_in),
3593                 compiler.stack_assign(color_in),
3594                 compiler.stack_assign(color_out));
3595 }
3596
3597 void InvertNode::compile(OSLCompiler& compiler)
3598 {
3599         compiler.add(this, "node_invert");
3600 }
3601
3602 /* Mix */
3603
3604 NODE_DEFINE(MixNode)
3605 {
3606         NodeType* type = NodeType::add("mix", create, NodeType::SHADER);
3607
3608         static NodeEnum type_enum;
3609         type_enum.insert("Mix", NODE_MIX_BLEND);
3610         type_enum.insert("Add", NODE_MIX_ADD);
3611         type_enum.insert("Multiply", NODE_MIX_MUL);
3612         type_enum.insert("Screen", NODE_MIX_SCREEN);
3613         type_enum.insert("Overlay", NODE_MIX_OVERLAY);
3614         type_enum.insert("Subtract", NODE_MIX_SUB);
3615         type_enum.insert("Divide", NODE_MIX_DIV);
3616         type_enum.insert("Difference", NODE_MIX_DIFF);
3617         type_enum.insert("Darken", NODE_MIX_DARK);
3618         type_enum.insert("Lighten", NODE_MIX_LIGHT);
3619         type_enum.insert("Dodge", NODE_MIX_DODGE);
3620         type_enum.insert("Burn", NODE_MIX_BURN);
3621         t