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