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