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