Code refactor: make ShaderNode match Node a bit more, reusing types and enums.
[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("wrap", "clamp");
437                         break;
438                 case EXTENSION_CLIP:
439                         compiler.parameter("wrap", "black");
440                         break;
441                 case EXTENSION_REPEAT:
442                 default:
443                         compiler.parameter("wrap", "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("OffsetFrequency", offset_frequency);
1316         compiler.parameter("Squash", squash);
1317         compiler.parameter("SquashFrequency", 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 /* Convert */
1540
1541 ConvertNode::ConvertNode(SocketType::Type from_, SocketType::Type to_, bool autoconvert)
1542 : ShaderNode("convert")
1543 {
1544         from = from_;
1545         to = to_;
1546
1547         if(autoconvert) {
1548                 if(from == to)
1549                         special_type = SHADER_SPECIAL_TYPE_PROXY;
1550                 else
1551                         special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1552         }
1553
1554         if(from == SocketType::FLOAT)
1555                 add_input("Val", SocketType::FLOAT);
1556         else if(from == SocketType::INT)
1557                 add_input("ValInt", SocketType::INT);
1558         else if(from == SocketType::COLOR)
1559                 add_input("Color", SocketType::COLOR);
1560         else if(from == SocketType::VECTOR)
1561                 add_input("Vector", SocketType::VECTOR);
1562         else if(from == SocketType::POINT)
1563                 add_input("Point", SocketType::POINT);
1564         else if(from == SocketType::NORMAL)
1565                 add_input("Normal", SocketType::NORMAL);
1566         else if(from == SocketType::STRING)
1567                 add_input("String", SocketType::STRING);
1568         else if(from == SocketType::CLOSURE)
1569                 add_input("Closure", SocketType::CLOSURE);
1570         else
1571                 assert(0);
1572
1573         if(to == SocketType::FLOAT)
1574                 add_output("Val", SocketType::FLOAT);
1575         else if(to == SocketType::INT)
1576                 add_output("ValInt", SocketType::INT);
1577         else if(to == SocketType::COLOR)
1578                 add_output("Color", SocketType::COLOR);
1579         else if(to == SocketType::VECTOR)
1580                 add_output("Vector", SocketType::VECTOR);
1581         else if(to == SocketType::POINT)
1582                 add_output("Point", SocketType::POINT);
1583         else if(to == SocketType::NORMAL)
1584                 add_output("Normal", SocketType::NORMAL);
1585         else if(to == SocketType::STRING)
1586                 add_output("String", SocketType::STRING);
1587         else if(to == SocketType::CLOSURE)
1588                 add_output("Closure", SocketType::CLOSURE);
1589         else
1590                 assert(0);
1591 }
1592
1593 bool ConvertNode::constant_fold(ShaderGraph * /*graph*/,
1594                                 ShaderOutput * /*socket*/,
1595                                 float3 *optimized_value)
1596 {
1597         ShaderInput *in = inputs[0];
1598         float3 value = in->value();
1599
1600         /* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
1601
1602         if(in->link == NULL) {
1603                 if(from == SocketType::FLOAT) {
1604                         if(to == SocketType::INT)
1605                                 /* float to int */
1606                                 return false;
1607                         else
1608                                 /* float to float3 */
1609                                 *optimized_value = make_float3(value.x, value.x, value.x);
1610                 }
1611                 else if(from == SocketType::INT) {
1612                         if(to == SocketType::FLOAT)
1613                                 /* int to float */
1614                                 return false;
1615                         else
1616                                 /* int to vector/point/normal */
1617                                 return false;
1618                 }
1619                 else if(to == SocketType::FLOAT) {
1620                         if(from == SocketType::COLOR)
1621                                 /* color to float */
1622                                 optimized_value->x = linear_rgb_to_gray(value);
1623                         else
1624                                 /* vector/point/normal to float */
1625                                 optimized_value->x = average(value);
1626                 }
1627                 else if(to == SocketType::INT) {
1628                         if(from == SocketType::COLOR)
1629                                 /* color to int */
1630                                 return false;
1631                         else
1632                                 /* vector/point/normal to int */
1633                                 return false;
1634                 }
1635                 else {
1636                         *optimized_value = value;
1637                 }
1638
1639                 return true;
1640         }
1641
1642         return false;
1643 }
1644
1645 void ConvertNode::compile(SVMCompiler& compiler)
1646 {
1647         /* constant folding should eliminate proxy nodes */
1648         assert(from != to);
1649
1650         ShaderInput *in = inputs[0];
1651         ShaderOutput *out = outputs[0];
1652
1653         if(from == SocketType::FLOAT) {
1654                 if(to == SocketType::INT)
1655                         /* float to int */
1656                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, compiler.stack_assign(in), compiler.stack_assign(out));
1657                 else
1658                         /* float to float3 */
1659                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, compiler.stack_assign(in), compiler.stack_assign(out));
1660         }
1661         else if(from == SocketType::INT) {
1662                 if(to == SocketType::FLOAT)
1663                         /* int to float */
1664                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, compiler.stack_assign(in), compiler.stack_assign(out));
1665                 else
1666                         /* int to vector/point/normal */
1667                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, compiler.stack_assign(in), compiler.stack_assign(out));
1668         }
1669         else if(to == SocketType::FLOAT) {
1670                 if(from == SocketType::COLOR)
1671                         /* color to float */
1672                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, compiler.stack_assign(in), compiler.stack_assign(out));
1673                 else
1674                         /* vector/point/normal to float */
1675                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, compiler.stack_assign(in), compiler.stack_assign(out));
1676         }
1677         else if(to == SocketType::INT) {
1678                 if(from == SocketType::COLOR)
1679                         /* color to int */
1680                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, compiler.stack_assign(in), compiler.stack_assign(out));
1681                 else
1682                         /* vector/point/normal to int */
1683                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, compiler.stack_assign(in), compiler.stack_assign(out));
1684         }
1685         else {
1686                 /* float3 to float3 */
1687                 if(in->link) {
1688                         /* no op in SVM */
1689                         compiler.stack_link(in, out);
1690                 }
1691                 else {
1692                         /* set 0,0,0 value */
1693                         compiler.add_node(NODE_VALUE_V, compiler.stack_assign(out));
1694                         compiler.add_node(NODE_VALUE_V, in->value());
1695                 }
1696         }
1697 }
1698
1699 void ConvertNode::compile(OSLCompiler& compiler)
1700 {
1701         /* constant folding should eliminate proxy nodes */
1702         assert(from != to);
1703
1704         if(from == SocketType::FLOAT)
1705                 compiler.add(this, "node_convert_from_float");
1706         else if(from == SocketType::INT)
1707                 compiler.add(this, "node_convert_from_int");
1708         else if(from == SocketType::COLOR)
1709                 compiler.add(this, "node_convert_from_color");
1710         else if(from == SocketType::VECTOR)
1711                 compiler.add(this, "node_convert_from_vector");
1712         else if(from == SocketType::POINT)
1713                 compiler.add(this, "node_convert_from_point");
1714         else if(from == SocketType::NORMAL)
1715                 compiler.add(this, "node_convert_from_normal");
1716         else
1717                 assert(0);
1718 }
1719
1720 /* BSDF Closure */
1721
1722 BsdfNode::BsdfNode(bool scattering_)
1723 : ShaderNode("bsdf"), scattering(scattering_)
1724 {
1725         special_type = SHADER_SPECIAL_TYPE_CLOSURE;
1726
1727         add_input("Color", SocketType::COLOR, make_float3(0.8f, 0.8f, 0.8f));
1728         add_input("Normal", SocketType::NORMAL, make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
1729         add_input("SurfaceMixWeight", SocketType::FLOAT, 0.0f, SocketType::SVM_INTERNAL);
1730
1731         if(scattering) {
1732                 closure = CLOSURE_BSSRDF_CUBIC_ID;
1733                 add_output("BSSRDF", SocketType::CLOSURE);
1734         }
1735         else {
1736                 closure = CLOSURE_BSDF_DIFFUSE_ID;
1737                 add_output("BSDF", SocketType::CLOSURE);
1738         }
1739 }
1740
1741 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3, ShaderInput *param4)
1742 {
1743         ShaderInput *color_in = input("Color");
1744         ShaderInput *normal_in = input("Normal");
1745         ShaderInput *tangent_in = input("Tangent");
1746
1747         if(color_in->link)
1748                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
1749         else
1750                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value());
1751
1752         int normal_offset = compiler.stack_assign_if_linked(normal_in);
1753         int tangent_offset = (tangent_in) ? compiler.stack_assign_if_linked(tangent_in) : SVM_STACK_INVALID;
1754         int param3_offset = (param3) ? compiler.stack_assign(param3) : SVM_STACK_INVALID;
1755         int param4_offset = (param4) ? compiler.stack_assign(param4) : SVM_STACK_INVALID;
1756
1757         compiler.add_node(NODE_CLOSURE_BSDF,
1758                 compiler.encode_uchar4(closure,
1759                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
1760                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
1761                         compiler.closure_mix_weight_offset()),
1762                 __float_as_int((param1)? param1->value_float(): 0.0f),
1763                 __float_as_int((param2)? param2->value_float(): 0.0f));
1764
1765         compiler.add_node(normal_offset, tangent_offset, param3_offset, param4_offset);
1766 }
1767
1768 void BsdfNode::compile(SVMCompiler& compiler)
1769 {
1770         compile(compiler, NULL, NULL);
1771 }
1772
1773 void BsdfNode::compile(OSLCompiler& /*compiler*/)
1774 {
1775         assert(0);
1776 }
1777
1778 /* Anisotropic BSDF Closure */
1779
1780 static NodeEnum aniso_distribution_init()
1781 {
1782         NodeEnum enm;
1783
1784         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID);
1785         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1786         enm.insert("Ashikhmin-Shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID);
1787
1788         return enm;
1789 }
1790
1791 NodeEnum AnisotropicBsdfNode::distribution_enum = aniso_distribution_init();
1792
1793 AnisotropicBsdfNode::AnisotropicBsdfNode()
1794 {
1795         closure = CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID;
1796         distribution = ustring("GGX");
1797
1798         add_input("Tangent", SocketType::VECTOR, make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
1799
1800         add_input("Roughness", SocketType::FLOAT, 0.2f);
1801         add_input("Anisotropy", SocketType::FLOAT, 0.5f);
1802         add_input("Rotation", SocketType::FLOAT, 0.0f);
1803 }
1804
1805 void AnisotropicBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
1806 {
1807         if(shader->has_surface) {
1808                 ShaderInput *tangent_in = input("Tangent");
1809
1810                 if(!tangent_in->link)
1811                         attributes->add(ATTR_STD_GENERATED);
1812         }
1813
1814         ShaderNode::attributes(shader, attributes);
1815 }
1816
1817 void AnisotropicBsdfNode::compile(SVMCompiler& compiler)
1818 {
1819         closure = (ClosureType)distribution_enum[distribution];
1820
1821         BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1822 }
1823
1824 void AnisotropicBsdfNode::compile(OSLCompiler& compiler)
1825 {
1826         compiler.parameter("distribution", distribution);
1827         compiler.add(this, "node_anisotropic_bsdf");
1828 }
1829
1830 /* Glossy BSDF Closure */
1831
1832 static NodeEnum glossy_distribution_init()
1833 {
1834         NodeEnum enm;
1835
1836         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1837         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1838         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1839         enm.insert("Ashikhmin-Shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
1840
1841         return enm;
1842 }
1843
1844 NodeEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1845
1846 GlossyBsdfNode::GlossyBsdfNode()
1847 {
1848         closure = CLOSURE_BSDF_MICROFACET_GGX_ID;
1849         distribution = ustring("GGX");
1850         distribution_orig = ustring("");
1851
1852         add_input("Roughness", SocketType::FLOAT, 0.2f);
1853 }
1854
1855 void GlossyBsdfNode::simplify_settings(Scene *scene)
1856 {
1857         if(distribution_orig == "") {
1858                 distribution_orig = distribution;
1859         }
1860         Integrator *integrator = scene->integrator;
1861         if(integrator->filter_glossy == 0.0f) {
1862                 /* Fallback to Sharp closure for Roughness close to 0.
1863                  * Note: Keep the epsilon in sync with kernel!
1864                  */
1865                 ShaderInput *roughness_input = input("Roughness");
1866                 if(!roughness_input->link && roughness_input->value_float() <= 1e-4f) {
1867                         distribution = ustring("Sharp");
1868                 }
1869         }
1870         else {
1871                 /* Rollback to original distribution when filter glossy is used. */
1872                 distribution = distribution_orig;
1873         }
1874         closure = (ClosureType)distribution_enum[distribution];
1875 }
1876
1877 bool GlossyBsdfNode::has_integrator_dependency()
1878 {
1879         ShaderInput *roughness_input = input("Roughness");
1880         return !roughness_input->link && roughness_input->value_float() <= 1e-4f;
1881 }
1882
1883 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1884 {
1885         closure = (ClosureType)distribution_enum[distribution];
1886
1887         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1888                 BsdfNode::compile(compiler, NULL, NULL);
1889         else
1890                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1891 }
1892
1893 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1894 {
1895         compiler.parameter("distribution", distribution);
1896         compiler.add(this, "node_glossy_bsdf");
1897 }
1898
1899 /* Glass BSDF Closure */
1900
1901 static NodeEnum glass_distribution_init()
1902 {
1903         NodeEnum enm;
1904
1905         enm.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1906         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1907         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1908
1909         return enm;
1910 }
1911
1912 NodeEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1913
1914 GlassBsdfNode::GlassBsdfNode()
1915 {
1916         closure = CLOSURE_BSDF_SHARP_GLASS_ID;
1917         distribution = ustring("Sharp");
1918         distribution_orig = ustring("");
1919
1920         add_input("Roughness", SocketType::FLOAT, 0.0f);
1921         add_input("IOR", SocketType::FLOAT, 0.3f);
1922 }
1923
1924 void GlassBsdfNode::simplify_settings(Scene *scene)
1925 {
1926         if(distribution_orig == "") {
1927                 distribution_orig = distribution;
1928         }
1929         Integrator *integrator = scene->integrator;
1930         if(integrator->filter_glossy == 0.0f) {
1931                 /* Fallback to Sharp closure for Roughness close to 0.
1932                  * Note: Keep the epsilon in sync with kernel!
1933                  */
1934                 ShaderInput *roughness_input = input("Roughness");
1935                 if(!roughness_input->link && roughness_input->value_float() <= 1e-4f) {
1936                         distribution = ustring("Sharp");
1937                 }
1938         }
1939         else {
1940                 /* Rollback to original distribution when filter glossy is used. */
1941                 distribution = distribution_orig;
1942         }
1943         closure = (ClosureType)distribution_enum[distribution];
1944 }
1945
1946 bool GlassBsdfNode::has_integrator_dependency()
1947 {
1948         ShaderInput *roughness_input = input("Roughness");
1949         return !roughness_input->link && roughness_input->value_float() <= 1e-4f;
1950 }
1951
1952 void GlassBsdfNode::compile(SVMCompiler& compiler)
1953 {
1954         closure = (ClosureType)distribution_enum[distribution];
1955
1956         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
1957                 BsdfNode::compile(compiler, NULL, input("IOR"));
1958         else
1959                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1960 }
1961
1962 void GlassBsdfNode::compile(OSLCompiler& compiler)
1963 {
1964         compiler.parameter("distribution", distribution);
1965         compiler.add(this, "node_glass_bsdf");
1966 }
1967
1968 /* Refraction BSDF Closure */
1969
1970 static NodeEnum refraction_distribution_init()
1971 {
1972         NodeEnum enm;
1973
1974         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1975         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1976         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1977
1978         return enm;
1979 }
1980
1981 NodeEnum RefractionBsdfNode::distribution_enum = refraction_distribution_init();
1982
1983 RefractionBsdfNode::RefractionBsdfNode()
1984 {
1985         closure = CLOSURE_BSDF_REFRACTION_ID;
1986         distribution = ustring("Sharp");
1987         distribution_orig = ustring("");
1988
1989         add_input("Roughness", SocketType::FLOAT, 0.0f);
1990         add_input("IOR", SocketType::FLOAT, 0.3f);
1991 }
1992
1993 void RefractionBsdfNode::simplify_settings(Scene *scene)
1994 {
1995         if(distribution_orig == "") {
1996                 distribution_orig = distribution;
1997         }
1998         Integrator *integrator = scene->integrator;
1999         if(integrator->filter_glossy == 0.0f) {
2000                 /* Fallback to Sharp closure for Roughness close to 0.
2001                  * Note: Keep the epsilon in sync with kernel!
2002                  */
2003                 ShaderInput *roughness_input = input("Roughness");
2004                 if(!roughness_input->link && roughness_input->value_float() <= 1e-4f) {
2005                         distribution = ustring("Sharp");
2006                 }
2007         }
2008         else {
2009                 /* Rollback to original distribution when filter glossy is used. */
2010                 distribution = distribution_orig;
2011         }
2012         closure = (ClosureType)distribution_enum[distribution];
2013 }
2014
2015 bool RefractionBsdfNode::has_integrator_dependency()
2016 {
2017         ShaderInput *roughness_input = input("Roughness");
2018         return !roughness_input->link && roughness_input->value_float() <= 1e-4f;
2019 }
2020
2021 void RefractionBsdfNode::compile(SVMCompiler& compiler)
2022 {
2023         closure = (ClosureType)distribution_enum[distribution];
2024
2025         if(closure == CLOSURE_BSDF_REFRACTION_ID)
2026                 BsdfNode::compile(compiler, NULL, input("IOR"));
2027         else
2028                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2029 }
2030
2031 void RefractionBsdfNode::compile(OSLCompiler& compiler)
2032 {
2033         compiler.parameter("distribution", distribution);
2034         compiler.add(this, "node_refraction_bsdf");
2035 }
2036
2037 /* Toon BSDF Closure */
2038
2039 static NodeEnum toon_component_init()
2040 {
2041         NodeEnum enm;
2042
2043         enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
2044         enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
2045
2046         return enm;
2047 }
2048
2049 NodeEnum ToonBsdfNode::component_enum = toon_component_init();
2050
2051 ToonBsdfNode::ToonBsdfNode()
2052 {
2053         closure = CLOSURE_BSDF_DIFFUSE_TOON_ID;
2054         component = ustring("Diffuse");
2055
2056         add_input("Size", SocketType::FLOAT, 0.5f);
2057         add_input("Smooth", SocketType::FLOAT, 0.0f);
2058 }
2059
2060 void ToonBsdfNode::compile(SVMCompiler& compiler)
2061 {
2062         closure = (ClosureType)component_enum[component];
2063         
2064         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
2065 }
2066
2067 void ToonBsdfNode::compile(OSLCompiler& compiler)
2068 {
2069         compiler.parameter("component", component);
2070         compiler.add(this, "node_toon_bsdf");
2071 }
2072
2073 /* Velvet BSDF Closure */
2074
2075 VelvetBsdfNode::VelvetBsdfNode()
2076 {
2077         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
2078
2079         add_input("Sigma", SocketType::FLOAT, 1.0f);
2080 }
2081
2082 void VelvetBsdfNode::compile(SVMCompiler& compiler)
2083 {
2084         BsdfNode::compile(compiler, input("Sigma"), NULL);
2085 }
2086
2087 void VelvetBsdfNode::compile(OSLCompiler& compiler)
2088 {
2089         compiler.add(this, "node_velvet_bsdf");
2090 }
2091
2092 /* Diffuse BSDF Closure */
2093
2094 DiffuseBsdfNode::DiffuseBsdfNode()
2095 {
2096         closure = CLOSURE_BSDF_DIFFUSE_ID;
2097         add_input("Roughness", SocketType::FLOAT, 0.0f);
2098 }
2099
2100 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
2101 {
2102         BsdfNode::compile(compiler, input("Roughness"), NULL);
2103 }
2104
2105 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
2106 {
2107         compiler.add(this, "node_diffuse_bsdf");
2108 }
2109
2110 /* Translucent BSDF Closure */
2111
2112 TranslucentBsdfNode::TranslucentBsdfNode()
2113 {
2114         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
2115 }
2116
2117 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
2118 {
2119         BsdfNode::compile(compiler, NULL, NULL);
2120 }
2121
2122 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
2123 {
2124         compiler.add(this, "node_translucent_bsdf");
2125 }
2126
2127 /* Transparent BSDF Closure */
2128
2129 TransparentBsdfNode::TransparentBsdfNode()
2130 {
2131         name = "transparent";
2132         closure = CLOSURE_BSDF_TRANSPARENT_ID;
2133 }
2134
2135 void TransparentBsdfNode::compile(SVMCompiler& compiler)
2136 {
2137         BsdfNode::compile(compiler, NULL, NULL);
2138 }
2139
2140 void TransparentBsdfNode::compile(OSLCompiler& compiler)
2141 {
2142         compiler.add(this, "node_transparent_bsdf");
2143 }
2144
2145 /* Subsurface Scattering Closure */
2146
2147 static NodeEnum subsurface_falloff_init()
2148 {
2149         NodeEnum enm;
2150
2151         enm.insert("Cubic", CLOSURE_BSSRDF_CUBIC_ID);
2152         enm.insert("Gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
2153         enm.insert("Burley", CLOSURE_BSSRDF_BURLEY_ID);
2154
2155         return enm;
2156 }
2157
2158 NodeEnum SubsurfaceScatteringNode::falloff_enum = subsurface_falloff_init();
2159
2160 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
2161 : BsdfNode(true)
2162 {
2163         name = "subsurface_scattering";
2164         closure = CLOSURE_BSSRDF_CUBIC_ID;
2165
2166         add_input("Scale", SocketType::FLOAT, 0.01f);
2167         add_input("Radius", SocketType::VECTOR, make_float3(0.1f, 0.1f, 0.1f));
2168         add_input("Sharpness", SocketType::FLOAT, 0.0f);
2169         add_input("Texture Blur", SocketType::FLOAT, 1.0f);
2170 }
2171
2172 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
2173 {
2174         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
2175 }
2176
2177 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
2178 {
2179         compiler.parameter("Falloff", falloff_enum[closure]);
2180         compiler.add(this, "node_subsurface_scattering");
2181 }
2182
2183 bool SubsurfaceScatteringNode::has_bssrdf_bump()
2184 {
2185         /* detect if anything is plugged into the normal input besides the default */
2186         ShaderInput *normal_in = input("Normal");
2187         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
2188 }
2189
2190 /* Emissive Closure */
2191
2192 EmissionNode::EmissionNode()
2193 : ShaderNode("emission")
2194 {
2195         add_input("Color", SocketType::COLOR, make_float3(0.8f, 0.8f, 0.8f));
2196         add_input("Strength", SocketType::FLOAT, 10.0f);
2197         add_input("SurfaceMixWeight", SocketType::FLOAT, 0.0f, SocketType::SVM_INTERNAL);
2198
2199         add_output("Emission", SocketType::CLOSURE);
2200 }
2201
2202 void EmissionNode::compile(SVMCompiler& compiler)
2203 {
2204         ShaderInput *color_in = input("Color");
2205         ShaderInput *strength_in = input("Strength");
2206
2207         if(color_in->link || strength_in->link) {
2208                 compiler.add_node(NODE_EMISSION_WEIGHT,
2209                                   compiler.stack_assign(color_in),
2210                                                   compiler.stack_assign(strength_in));
2211         }
2212         else
2213                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value() * strength_in->value_float());
2214
2215         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
2216 }
2217
2218 void EmissionNode::compile(OSLCompiler& compiler)
2219 {
2220         compiler.add(this, "node_emission");
2221 }
2222
2223 bool EmissionNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, float3 * /*optimized_value*/)
2224 {
2225         ShaderInput *color_in = input("Color");
2226         ShaderInput *strength_in = input("Strength");
2227
2228         return ((!color_in->link && color_in->value() == make_float3(0.0f, 0.0f, 0.0f)) ||
2229                 (!strength_in->link && strength_in->value_float() == 0.0f));
2230 }
2231
2232 /* Background Closure */
2233
2234 BackgroundNode::BackgroundNode()
2235 : ShaderNode("background")
2236 {
2237         add_input("Color", SocketType::COLOR, make_float3(0.8f, 0.8f, 0.8f));
2238         add_input("Strength", SocketType::FLOAT, 1.0f);
2239         add_input("SurfaceMixWeight", SocketType::FLOAT, 0.0f, SocketType::SVM_INTERNAL);
2240
2241         add_output("Background", SocketType::CLOSURE);
2242 }
2243
2244 void BackgroundNode::compile(SVMCompiler& compiler)
2245 {
2246         ShaderInput *color_in = input("Color");
2247         ShaderInput *strength_in = input("Strength");
2248
2249         if(color_in->link || strength_in->link) {
2250                 compiler.add_node(NODE_EMISSION_WEIGHT,
2251                                   compiler.stack_assign(color_in),
2252                                                   compiler.stack_assign(strength_in));
2253         }
2254         else
2255                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value()*strength_in->value_float());
2256
2257         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
2258 }
2259
2260 void BackgroundNode::compile(OSLCompiler& compiler)
2261 {
2262         compiler.add(this, "node_background");
2263 }
2264
2265 bool BackgroundNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, float3 * /*optimized_value*/)
2266 {
2267         ShaderInput *color_in = input("Color");
2268         ShaderInput *strength_in = input("Strength");
2269
2270         return ((!color_in->link && color_in->value() == make_float3(0.0f, 0.0f, 0.0f)) ||
2271                 (!strength_in->link && strength_in->value_float() == 0.0f));
2272 }
2273
2274 /* Holdout Closure */
2275
2276 HoldoutNode::HoldoutNode()
2277 : ShaderNode("holdout")
2278 {
2279         add_input("SurfaceMixWeight", SocketType::FLOAT, 0.0f, SocketType::SVM_INTERNAL);
2280         add_input("VolumeMixWeight", SocketType::FLOAT, 0.0f, SocketType::SVM_INTERNAL);
2281
2282         add_output("Holdout", SocketType::CLOSURE);
2283 }
2284
2285 void HoldoutNode::compile(SVMCompiler& compiler)
2286 {
2287         float3 value = make_float3(1.0f, 1.0f, 1.0f);
2288
2289         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
2290         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
2291 }
2292
2293 void HoldoutNode::compile(OSLCompiler& compiler)
2294 {
2295         compiler.add(this, "node_holdout");
2296 }
2297
2298 /* Ambient Occlusion */
2299
2300 AmbientOcclusionNode::AmbientOcclusionNode()
2301 : ShaderNode("ambient_occlusion")
2302 {
2303         add_input("NormalIn", SocketType::NORMAL, make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2304         add_input("Color", SocketType::COLOR, make_float3(0.8f, 0.8f, 0.8f));
2305         add_input("SurfaceMixWeight", SocketType::FLOAT, 0.0f, SocketType::SVM_INTERNAL);
2306
2307         add_output("AO", SocketType::CLOSURE);
2308 }
2309
2310 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
2311 {
2312         ShaderInput *color_in = input("Color");
2313
2314         if(color_in->link)
2315                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2316         else
2317                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value());
2318
2319         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
2320 }
2321
2322 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
2323 {
2324         compiler.add(this, "node_ambient_occlusion");
2325 }
2326
2327 /* Volume Closure */
2328
2329 VolumeNode::VolumeNode()
2330 : ShaderNode("volume")
2331 {
2332         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2333
2334         add_input("Color", SocketType::COLOR, make_float3(0.8f, 0.8f, 0.8f));
2335         add_input("Density", SocketType::FLOAT, 1.0f);
2336         add_input("VolumeMixWeight", SocketType::FLOAT, 0.0f, SocketType::SVM_INTERNAL);
2337
2338         add_output("Volume", SocketType::CLOSURE);
2339 }
2340
2341 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
2342 {
2343         ShaderInput *color_in = input("Color");
2344
2345         if(color_in->link)
2346                 compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
2347         else
2348                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value());
2349         
2350         compiler.add_node(NODE_CLOSURE_VOLUME,
2351                 compiler.encode_uchar4(closure,
2352                         (param1)? compiler.stack_assign(param1): SVM_STACK_INVALID,
2353                         (param2)? compiler.stack_assign(param2): SVM_STACK_INVALID,
2354                         compiler.closure_mix_weight_offset()),
2355                 __float_as_int((param1)? param1->value_float(): 0.0f),
2356                 __float_as_int((param2)? param2->value_float(): 0.0f));
2357 }
2358
2359 void VolumeNode::compile(SVMCompiler& compiler)
2360 {
2361         compile(compiler, NULL, NULL);
2362 }
2363
2364 void VolumeNode::compile(OSLCompiler& /*compiler*/)
2365 {
2366         assert(0);
2367 }
2368
2369 /* Absorption Volume Closure */
2370
2371 AbsorptionVolumeNode::AbsorptionVolumeNode()
2372 {
2373         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2374 }
2375
2376 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2377 {
2378         VolumeNode::compile(compiler, input("Density"), NULL);
2379 }
2380
2381 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2382 {
2383         compiler.add(this, "node_absorption_volume");
2384 }
2385
2386 /* Scatter Volume Closure */
2387
2388 ScatterVolumeNode::ScatterVolumeNode()
2389 {
2390         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2391         
2392         add_input("Anisotropy", SocketType::FLOAT, 0.0f);
2393 }
2394
2395 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2396 {
2397         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2398 }
2399
2400 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2401 {
2402         compiler.add(this, "node_scatter_volume");
2403 }
2404
2405 /* Hair BSDF Closure */
2406
2407 static NodeEnum hair_component_init()
2408 {
2409         NodeEnum enm;
2410
2411         enm.insert("Reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2412         enm.insert("Transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2413
2414         return enm;
2415 }
2416
2417 NodeEnum HairBsdfNode::component_enum = hair_component_init();
2418
2419 HairBsdfNode::HairBsdfNode()
2420 {
2421         closure = CLOSURE_BSDF_HAIR_REFLECTION_ID;
2422         component = ustring("Reflection");
2423
2424         add_input("Offset", SocketType::FLOAT);
2425         add_input("RoughnessU", SocketType::FLOAT);
2426         add_input("RoughnessV", SocketType::FLOAT);
2427         add_input("Tangent", SocketType::VECTOR);
2428 }
2429
2430 void HairBsdfNode::compile(SVMCompiler& compiler)
2431 {
2432         closure = (ClosureType)component_enum[component];
2433
2434         BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
2435 }
2436
2437 void HairBsdfNode::compile(OSLCompiler& compiler)
2438 {
2439         compiler.parameter("component", component);
2440
2441         compiler.add(this, "node_hair_bsdf");
2442 }
2443
2444 /* Geometry */
2445
2446 GeometryNode::GeometryNode()
2447 : ShaderNode("geometry")
2448 {
2449         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2450
2451         add_input("NormalIn", SocketType::NORMAL, make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2452         add_output("Position", SocketType::POINT);
2453         add_output("Normal", SocketType::NORMAL);
2454         add_output("Tangent", SocketType::NORMAL);
2455         add_output("True Normal", SocketType::NORMAL);
2456         add_output("Incoming", SocketType::VECTOR);
2457         add_output("Parametric", SocketType::POINT);
2458         add_output("Backfacing", SocketType::FLOAT);
2459         add_output("Pointiness", SocketType::FLOAT);
2460 }
2461
2462 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2463 {
2464         if(shader->has_surface) {
2465                 if(!output("Tangent")->links.empty()) {
2466                         attributes->add(ATTR_STD_GENERATED);
2467                 }
2468                 if(!output("Pointiness")->links.empty()) {
2469                         attributes->add(ATTR_STD_POINTINESS);
2470                 }
2471         }
2472
2473         ShaderNode::attributes(shader, attributes);
2474 }
2475
2476 void GeometryNode::compile(SVMCompiler& compiler)
2477 {
2478         ShaderOutput *out;
2479         ShaderNodeType geom_node = NODE_GEOMETRY;
2480         ShaderNodeType attr_node = NODE_ATTR;
2481
2482         if(bump == SHADER_BUMP_DX) {
2483                 geom_node = NODE_GEOMETRY_BUMP_DX;
2484                 attr_node = NODE_ATTR_BUMP_DX;
2485         }
2486         else if(bump == SHADER_BUMP_DY) {
2487                 geom_node = NODE_GEOMETRY_BUMP_DY;
2488                 attr_node = NODE_ATTR_BUMP_DY;
2489         }
2490         
2491         out = output("Position");
2492         if(!out->links.empty()) {
2493                 compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
2494         }
2495
2496         out = output("Normal");
2497         if(!out->links.empty()) {
2498                 compiler.add_node(geom_node, NODE_GEOM_N, compiler.stack_assign(out));
2499         }
2500
2501         out = output("Tangent");
2502         if(!out->links.empty()) {
2503                 compiler.add_node(geom_node, NODE_GEOM_T, compiler.stack_assign(out));
2504         }
2505
2506         out = output("True Normal");
2507         if(!out->links.empty()) {
2508                 compiler.add_node(geom_node, NODE_GEOM_Ng, compiler.stack_assign(out));
2509         }
2510
2511         out = output("Incoming");
2512         if(!out->links.empty()) {
2513                 compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
2514         }
2515
2516         out = output("Parametric");
2517         if(!out->links.empty()) {
2518                 compiler.add_node(geom_node, NODE_GEOM_uv, compiler.stack_assign(out));
2519         }
2520
2521         out = output("Backfacing");
2522         if(!out->links.empty()) {
2523                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, compiler.stack_assign(out));
2524         }
2525
2526         out = output("Pointiness");
2527         if(!out->links.empty()) {
2528                 if(compiler.output_type() != SHADER_TYPE_VOLUME) {
2529                         compiler.add_node(attr_node,
2530                                           ATTR_STD_POINTINESS,
2531                                           compiler.stack_assign(out),
2532                                           NODE_ATTR_FLOAT);
2533                 }
2534                 else {
2535                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), compiler.stack_assign(out));
2536                 }
2537         }
2538 }
2539
2540 void GeometryNode::compile(OSLCompiler& compiler)
2541 {
2542         if(bump == SHADER_BUMP_DX)
2543                 compiler.parameter("bump_offset", "dx");
2544         else if(bump == SHADER_BUMP_DY)
2545                 compiler.parameter("bump_offset", "dy");
2546         else
2547                 compiler.parameter("bump_offset", "center");
2548
2549         compiler.add(this, "node_geometry");
2550 }
2551
2552 /* TextureCoordinate */
2553
2554 TextureCoordinateNode::TextureCoordinateNode()
2555 : ShaderNode("texture_coordinate")
2556 {
2557         add_input("NormalIn", SocketType::NORMAL, make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
2558         add_output("Generated", SocketType::POINT);
2559         add_output("Normal", SocketType::NORMAL);
2560         add_output("UV", SocketType::POINT);
2561         add_output("Object", SocketType::POINT);
2562         add_output("Camera", SocketType::POINT);
2563         add_output("Window", SocketType::POINT);
2564         add_output("Reflection", SocketType::NORMAL);
2565
2566         from_dupli = false;
2567         use_transform = false;
2568         ob_tfm = transform_identity();
2569 }
2570
2571 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2572 {
2573         if(shader->has_surface) {
2574                 if(!from_dupli) {
2575                         if(!output("Generated")->links.empty())
2576                                 attributes->add(ATTR_STD_GENERATED);
2577                         if(!output("UV")->links.empty())
2578                                 attributes->add(ATTR_STD_UV);
2579                 }
2580         }
2581
2582         if(shader->has_volume) {
2583                 if(!from_dupli) {
2584                         if(!output("Generated")->links.empty()) {
2585                                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
2586                         }
2587                 }
2588         }
2589
2590         ShaderNode::attributes(shader, attributes);
2591 }
2592
2593 void TextureCoordinateNode::compile(SVMCompiler& compiler)
2594 {
2595         ShaderOutput *out;
2596         ShaderNodeType texco_node = NODE_TEX_COORD;
2597         ShaderNodeType attr_node = NODE_ATTR;
2598         ShaderNodeType geom_node = NODE_GEOMETRY;
2599
2600         if(bump == SHADER_BUMP_DX) {
2601                 texco_node = NODE_TEX_COORD_BUMP_DX;
2602                 attr_node = NODE_ATTR_BUMP_DX;
2603                 geom_node = NODE_GEOMETRY_BUMP_DX;
2604         }
2605         else if(bump == SHADER_BUMP_DY) {
2606                 texco_node = NODE_TEX_COORD_BUMP_DY;
2607                 attr_node = NODE_ATTR_BUMP_DY;
2608                 geom_node = NODE_GEOMETRY_BUMP_DY;
2609         }
2610         
2611         out = output("Generated");
2612         if(!out->links.empty()) {
2613                 if(compiler.background) {
2614                         compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
2615                 }
2616                 else {
2617                         if(from_dupli) {
2618                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, compiler.stack_assign(out));
2619                         }
2620                         else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
2621                                 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, compiler.stack_assign(out));
2622                         }
2623                         else {
2624                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
2625                                 compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2626                         }
2627                 }
2628         }
2629
2630         out = output("Normal");
2631         if(!out->links.empty()) {
2632                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, compiler.stack_assign(out));
2633         }
2634
2635         out = output("UV");
2636         if(!out->links.empty()) {
2637                 if(from_dupli) {
2638                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
2639                 }
2640                 else {
2641                         int attr = compiler.attribute(ATTR_STD_UV);
2642                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2643                 }
2644         }
2645
2646         out = output("Object");
2647         if(!out->links.empty()) {
2648                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, compiler.stack_assign(out), use_transform);
2649                 if(use_transform) {
2650                         Transform ob_itfm = transform_inverse(ob_tfm);
2651                         compiler.add_node(ob_itfm.x);
2652                         compiler.add_node(ob_itfm.y);
2653                         compiler.add_node(ob_itfm.z);
2654                         compiler.add_node(ob_itfm.w);
2655                 }
2656         }
2657
2658         out = output("Camera");
2659         if(!out->links.empty()) {
2660                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, compiler.stack_assign(out));
2661         }
2662
2663         out = output("Window");
2664         if(!out->links.empty()) {
2665                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, compiler.stack_assign(out));
2666         }
2667
2668         out = output("Reflection");
2669         if(!out->links.empty()) {
2670                 if(compiler.background) {
2671                         compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
2672                 }
2673                 else {
2674                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, compiler.stack_assign(out));
2675                 }
2676         }
2677 }
2678
2679 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2680 {
2681         if(bump == SHADER_BUMP_DX)
2682                 compiler.parameter("bump_offset", "dx");
2683         else if(bump == SHADER_BUMP_DY)
2684                 compiler.parameter("bump_offset", "dy");
2685         else
2686                 compiler.parameter("bump_offset", "center");
2687         
2688         if(compiler.background)
2689                 compiler.parameter("is_background", true);
2690         if(compiler.output_type() == SHADER_TYPE_VOLUME)
2691                 compiler.parameter("is_volume", true);
2692         compiler.parameter("use_transform", use_transform);
2693         Transform ob_itfm = transform_transpose(transform_inverse(ob_tfm));
2694         compiler.parameter("object_itfm", ob_itfm);
2695
2696         compiler.parameter("from_dupli", from_dupli);
2697
2698         compiler.add(this, "node_texture_coordinate");
2699 }
2700
2701 UVMapNode::UVMapNode()
2702 : ShaderNode("uvmap")
2703 {
2704         attribute = "";
2705         from_dupli = false;
2706
2707         add_output("UV", SocketType::POINT);
2708 }
2709
2710 void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2711 {
2712         if(shader->has_surface) {
2713                 if(!from_dupli) {
2714                         if(!output("UV")->links.empty()) {
2715                                 if(attribute != "")
2716                                         attributes->add(attribute);
2717                                 else
2718                                         attributes->add(ATTR_STD_UV);
2719                         }
2720                 }
2721         }
2722
2723         ShaderNode::attributes(shader, attributes);
2724 }
2725
2726 void UVMapNode::compile(SVMCompiler& compiler)
2727 {
2728         ShaderOutput *out = output("UV");
2729         ShaderNodeType texco_node = NODE_TEX_COORD;
2730         ShaderNodeType attr_node = NODE_ATTR;
2731         int attr;
2732
2733         if(bump == SHADER_BUMP_DX) {
2734                 texco_node = NODE_TEX_COORD_BUMP_DX;
2735                 attr_node = NODE_ATTR_BUMP_DX;
2736         }
2737         else if(bump == SHADER_BUMP_DY) {
2738                 texco_node = NODE_TEX_COORD_BUMP_DY;
2739                 attr_node = NODE_ATTR_BUMP_DY;
2740         }
2741
2742         if(!out->links.empty()) {
2743                 if(from_dupli) {
2744                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
2745                 }
2746                 else {
2747                         if(attribute != "")
2748                                 attr = compiler.attribute(attribute);
2749                         else
2750                                 attr = compiler.attribute(ATTR_STD_UV);
2751
2752                         compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
2753                 }
2754         }
2755 }
2756
2757 void UVMapNode::compile(OSLCompiler& compiler)
2758 {
2759         if(bump == SHADER_BUMP_DX)
2760                 compiler.parameter("bump_offset", "dx");
2761         else if(bump == SHADER_BUMP_DY)
2762                 compiler.parameter("bump_offset", "dy");
2763         else
2764                 compiler.parameter("bump_offset", "center");
2765
2766         compiler.parameter("from_dupli", from_dupli);
2767         compiler.parameter("name", attribute.c_str());
2768         compiler.add(this, "node_uv_map");
2769 }
2770
2771 /* Light Path */
2772
2773 LightPathNode::LightPathNode()
2774 : ShaderNode("light_path")
2775 {
2776         add_output("Is Camera Ray", SocketType::FLOAT);
2777         add_output("Is Shadow Ray", SocketType::FLOAT);
2778         add_output("Is Diffuse Ray", SocketType::FLOAT);
2779         add_output("Is Glossy Ray", SocketType::FLOAT);
2780         add_output("Is Singular Ray", SocketType::FLOAT);
2781         add_output("Is Reflection Ray", SocketType::FLOAT);
2782         add_output("Is Transmission Ray", SocketType::FLOAT);
2783         add_output("Is Volume Scatter Ray", SocketType::FLOAT);
2784         add_output("Ray Length", SocketType::FLOAT);
2785         add_output("Ray Depth", SocketType::FLOAT);
2786         add_output("Transparent Depth", SocketType::FLOAT);
2787         add_output("Transmission Depth", SocketType::FLOAT);
2788 }
2789
2790 void LightPathNode::compile(SVMCompiler& compiler)
2791 {
2792         ShaderOutput *out;
2793
2794         out = output("Is Camera Ray");
2795         if(!out->links.empty()) {
2796                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, compiler.stack_assign(out));
2797         }
2798
2799         out = output("Is Shadow Ray");
2800         if(!out->links.empty()) {
2801                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, compiler.stack_assign(out));
2802         }
2803
2804         out = output("Is Diffuse Ray");
2805         if(!out->links.empty()) {
2806                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, compiler.stack_assign(out));
2807         }
2808
2809         out = output("Is Glossy Ray");
2810         if(!out->links.empty()) {
2811                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, compiler.stack_assign(out));
2812         }
2813
2814         out = output("Is Singular Ray");
2815         if(!out->links.empty()) {
2816                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, compiler.stack_assign(out));
2817         }
2818
2819         out = output("Is Reflection Ray");
2820         if(!out->links.empty()) {
2821                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, compiler.stack_assign(out));
2822         }
2823
2824
2825         out = output("Is Transmission Ray");
2826         if(!out->links.empty()) {
2827                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, compiler.stack_assign(out));
2828         }
2829         
2830         out = output("Is Volume Scatter Ray");
2831         if(!out->links.empty()) {
2832                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, compiler.stack_assign(out));
2833         }
2834
2835         out = output("Ray Length");
2836         if(!out->links.empty()) {
2837                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, compiler.stack_assign(out));
2838         }
2839         
2840         out = output("Ray Depth");
2841         if(!out->links.empty()) {
2842                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, compiler.stack_assign(out));
2843         }
2844
2845         out = output("Transparent Depth");
2846         if(!out->links.empty()) {
2847                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, compiler.stack_assign(out));
2848         }
2849
2850         out = output("Transmission Depth");
2851         if(!out->links.empty()) {
2852                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transmission, compiler.stack_assign(out));
2853         }
2854 }
2855
2856 void LightPathNode::compile(OSLCompiler& compiler)
2857 {
2858         compiler.add(this, "node_light_path");
2859 }
2860
2861 /* Light Falloff */
2862
2863 LightFalloffNode::LightFalloffNode()
2864 : ShaderNode("light_fallof")
2865 {
2866         add_input("Strength", SocketType::FLOAT, 100.0f);
2867         add_input("Smooth", SocketType::FLOAT, 0.0f);
2868         add_output("Quadratic", SocketType::FLOAT);
2869         add_output("Linear", SocketType::FLOAT);
2870         add_output("Constant", SocketType::FLOAT);
2871 }
2872
2873 void LightFalloffNode::compile(SVMCompiler& compiler)
2874 {
2875         ShaderInput *strength_in = input("Strength");
2876         ShaderInput *smooth_in = input("Smooth");
2877
2878         ShaderOutput *out = output("Quadratic");
2879         if(!out->links.empty()) {
2880                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2881                         compiler.encode_uchar4(
2882                                 compiler.stack_assign(strength_in),
2883                                 compiler.stack_assign(smooth_in),
2884                                 compiler.stack_assign(out)));
2885         }
2886
2887         out = output("Linear");
2888         if(!out->links.empty()) {
2889                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2890                         compiler.encode_uchar4(
2891                                 compiler.stack_assign(strength_in),
2892                                 compiler.stack_assign(smooth_in),
2893                                 compiler.stack_assign(out)));
2894         }
2895
2896         out = output("Constant");
2897         if(!out->links.empty()) {
2898                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2899                         compiler.encode_uchar4(
2900                                 compiler.stack_assign(strength_in),
2901                                 compiler.stack_assign(smooth_in),
2902                                 compiler.stack_assign(out)));
2903         }
2904 }
2905
2906 void LightFalloffNode::compile(OSLCompiler& compiler)
2907 {
2908         compiler.add(this, "node_light_falloff");
2909 }
2910
2911 /* Object Info */
2912
2913 ObjectInfoNode::ObjectInfoNode()
2914 : ShaderNode("object_info")
2915 {
2916         add_output("Location", SocketType::VECTOR);
2917         add_output("Object Index", SocketType::FLOAT);
2918         add_output("Material Index", SocketType::FLOAT);
2919         add_output("Random", SocketType::FLOAT);
2920 }
2921
2922 void ObjectInfoNode::compile(SVMCompiler& compiler)
2923 {
2924         ShaderOutput *out = output("Location");
2925         if(!out->links.empty()) {
2926                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, compiler.stack_assign(out));
2927         }
2928
2929         out = output("Object Index");
2930         if(!out->links.empty()) {
2931                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, compiler.stack_assign(out));
2932         }
2933
2934         out = output("Material Index");
2935         if(!out->links.empty()) {
2936                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, compiler.stack_assign(out));
2937         }
2938
2939         out = output("Random");
2940         if(!out->links.empty()) {
2941                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, compiler.stack_assign(out));
2942         }
2943 }
2944
2945 void ObjectInfoNode::compile(OSLCompiler& compiler)
2946 {
2947         compiler.add(this, "node_object_info");
2948 }
2949
2950 /* Particle Info */
2951
2952 ParticleInfoNode::ParticleInfoNode()
2953 : ShaderNode("particle_info")
2954 {
2955         add_output("Index", SocketType::FLOAT);
2956         add_output("Age", SocketType::FLOAT);
2957         add_output("Lifetime", SocketType::FLOAT);
2958         add_output("Location", SocketType::POINT);
2959 #if 0   /* not yet supported */
2960         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2961 #endif
2962         add_output("Size", SocketType::FLOAT);
2963         add_output("Velocity", SocketType::VECTOR);
2964         add_output("Angular Velocity", SocketType::VECTOR);
2965 }
2966
2967 void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2968 {
2969         if(!output("Index")->links.empty())
2970                 attributes->add(ATTR_STD_PARTICLE);
2971         if(!output("Age")->links.empty())
2972                 attributes->add(ATTR_STD_PARTICLE);
2973         if(!output("Lifetime")->links.empty())
2974                 attributes->add(ATTR_STD_PARTICLE);
2975         if(!output("Location")->links.empty())
2976                 attributes->add(ATTR_STD_PARTICLE);
2977 #if 0   /* not yet supported */
2978         if(!output("Rotation")->links.empty())
2979                 attributes->add(ATTR_STD_PARTICLE);
2980 #endif
2981         if(!output("Size")->links.empty())
2982                 attributes->add(ATTR_STD_PARTICLE);
2983         if(!output("Velocity")->links.empty())
2984                 attributes->add(ATTR_STD_PARTICLE);
2985         if(!output("Angular Velocity")->links.empty())
2986                 attributes->add(ATTR_STD_PARTICLE);
2987
2988         ShaderNode::attributes(shader, attributes);
2989 }
2990
2991 void ParticleInfoNode::compile(SVMCompiler& compiler)
2992 {
2993         ShaderOutput *out;
2994         
2995         out = output("Index");
2996         if(!out->links.empty()) {
2997                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, compiler.stack_assign(out));
2998         }
2999         
3000         out = output("Age");
3001         if(!out->links.empty()) {
3002                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, compiler.stack_assign(out));
3003         }
3004         
3005         out = output("Lifetime");
3006         if(!out->links.empty()) {
3007                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, compiler.stack_assign(out));
3008         }
3009         
3010         out = output("Location");
3011         if(!out->links.empty()) {
3012                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, compiler.stack_assign(out));
3013         }
3014         
3015         /* quaternion data is not yet supported by Cycles */
3016 #if 0
3017         out = output("Rotation");
3018         if(!out->links.empty()) {
3019                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, compiler.stack_assign(out));
3020         }
3021 #endif
3022         
3023         out = output("Size");
3024         if(!out->links.empty()) {
3025                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, compiler.stack_assign(out));
3026         }
3027         
3028         out = output("Velocity");
3029         if(!out->links.empty()) {
3030                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, compiler.stack_assign(out));
3031         }
3032         
3033         out = output("Angular Velocity");
3034         if(!out->links.empty()) {
3035                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, compiler.stack_assign(out));
3036         }
3037 }
3038
3039 void ParticleInfoNode::compile(OSLCompiler& compiler)
3040 {
3041         compiler.add(this, "node_particle_info");
3042 }
3043
3044 /* Hair Info */
3045
3046 HairInfoNode::HairInfoNode()
3047 : ShaderNode("hair_info")
3048 {
3049         add_output("Is Strand", SocketType::FLOAT);
3050         add_output("Intercept", SocketType::FLOAT);
3051         add_output("Thickness", SocketType::FLOAT);
3052         add_output("Tangent Normal", SocketType::NORMAL);
3053         /*output for minimum hair width transparency - deactivated*/
3054         /*add_output("Fade", SocketType::FLOAT);*/
3055 }
3056
3057 void HairInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3058 {
3059         if(shader->has_surface) {
3060                 ShaderOutput *intercept_out = output("Intercept");
3061
3062                 if(!intercept_out->links.empty())
3063                         attributes->add(ATTR_STD_CURVE_INTERCEPT);
3064         }
3065
3066         ShaderNode::attributes(shader, attributes);
3067 }
3068
3069 void HairInfoNode::compile(SVMCompiler& compiler)
3070 {
3071         ShaderOutput *out;
3072         
3073         out = output("Is Strand");
3074         if(!out->links.empty()) {
3075                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, compiler.stack_assign(out));
3076         }
3077
3078         out = output("Intercept");
3079         if(!out->links.empty()) {
3080                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
3081                 compiler.add_node(NODE_ATTR, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT);
3082         }
3083
3084         out = output("Thickness");
3085         if(!out->links.empty()) {
3086                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, compiler.stack_assign(out));
3087         }
3088
3089         out = output("Tangent Normal");
3090         if(!out->links.empty()) {
3091                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, compiler.stack_assign(out));
3092         }
3093
3094         /*out = output("Fade");
3095         if(!out->links.empty()) {
3096                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, compiler.stack_assign(out));
3097         }*/
3098
3099 }
3100
3101 void HairInfoNode::compile(OSLCompiler& compiler)
3102 {
3103         compiler.add(this, "node_hair_info");
3104 }
3105
3106 /* Value */
3107
3108 ValueNode::ValueNode()
3109 : ShaderNode("value")
3110 {
3111         value = 0.0f;
3112
3113         add_output("Value", SocketType::FLOAT);
3114 }
3115
3116 bool ValueNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/,
3117                               float3 *optimized_value)
3118 {
3119         *optimized_value = make_float3(value, value, value);
3120         return true;
3121 }
3122
3123 void ValueNode::compile(SVMCompiler& compiler)
3124 {
3125         ShaderOutput *val_out = output("Value");
3126
3127         compiler.add_node(NODE_VALUE_F, __float_as_int(value), compiler.stack_assign(val_out));
3128 }
3129
3130 void ValueNode::compile(OSLCompiler& compiler)
3131 {
3132         compiler.parameter("value_value", value);
3133         compiler.add(this, "node_value");
3134 }
3135
3136 /* Color */
3137
3138 ColorNode::ColorNode()
3139 : ShaderNode("color")
3140 {
3141         value = make_float3(0.0f, 0.0f, 0.0f);
3142
3143         add_output("Color", SocketType::COLOR);
3144 }
3145
3146 bool ColorNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/,
3147                               float3 *optimized_value)
3148 {
3149         *optimized_value = value;
3150         return true;
3151 }
3152
3153 void ColorNode::compile(SVMCompiler& compiler)
3154 {
3155         ShaderOutput *color_out = output("Color");
3156
3157         if(!color_out->links.empty()) {
3158                 compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
3159                 compiler.add_node(NODE_VALUE_V, value);
3160         }
3161 }
3162
3163 void ColorNode::compile(OSLCompiler& compiler)
3164 {
3165         compiler.parameter_color("color_value", value);
3166
3167         compiler.add(this, "node_value");
3168 }
3169
3170 /* Add Closure */
3171
3172 AddClosureNode::AddClosureNode()
3173 : ShaderNode("add_closure")
3174 {
3175         special_type = SHADER_SPECIAL_TYPE_COMBINE_CLOSURE;
3176
3177         add_input("Closure1", SocketType::CLOSURE);
3178         add_input("Closure2", SocketType::CLOSURE);
3179         add_output("Closure",  SocketType::CLOSURE);
3180 }
3181
3182 void AddClosureNode::compile(SVMCompiler& /*compiler*/)
3183 {
3184         /* handled in the SVM compiler */
3185 }
3186
3187 void AddClosureNode::compile(OSLCompiler& compiler)
3188 {
3189         compiler.add(this, "node_add_closure");
3190 }
3191
3192 /* Mix Closure */
3193
3194 MixClosureNode::MixClosureNode()
3195 : ShaderNode("mix_closure")
3196 {
3197         special_type = SHADER_SPECIAL_TYPE_COMBINE_CLOSURE;
3198
3199         add_input("Fac", SocketType::FLOAT, 0.5f);
3200         add_input("Closure1", SocketType::CLOSURE);
3201         add_input("Closure2", SocketType::CLOSURE);
3202         add_output("Closure",  SocketType::CLOSURE);
3203 }
3204
3205 void MixClosureNode::compile(SVMCompiler& /*compiler*/)
3206 {
3207         /* handled in the SVM compiler */
3208 }
3209
3210 void MixClosureNode::compile(OSLCompiler& compiler)
3211 {
3212         compiler.add(this, "node_mix_closure");
3213 }
3214
3215 bool MixClosureNode::constant_fold(ShaderGraph *graph, ShaderOutput * /*socket*/, float3 * /*optimized_value*/)
3216 {
3217         ShaderInput *fac_in = input("Fac");
3218         ShaderInput *closure1_in = input("Closure1");
3219         ShaderInput *closure2_in = input("Closure2");
3220         ShaderOutput *closure_out = output("Closure");
3221
3222         /* remove useless mix closures nodes */
3223         if(closure1_in->link == closure2_in->link) {
3224                 graph->relink(this, closure_out, closure1_in->link);
3225                 return true;
3226         }
3227
3228         /* remove unused mix closure input when factor is 0.0 or 1.0 */
3229         /* check for closure links and make sure factor link is disconnected */
3230         if(closure1_in->link && closure2_in->link && !fac_in->link) {
3231                 /* factor 0.0 */
3232                 if(fac_in->value_float() == 0.0f) {
3233                         graph->relink(this, closure_out, closure1_in->link);
3234                         return true;
3235                 }
3236                 /* factor 1.0 */
3237                 else if(fac_in->value_float() == 1.0f) {
3238                         graph->relink(this, closure_out, closure2_in->link);
3239                         return true;
3240                 }
3241         }
3242
3243         return false;
3244 }
3245
3246 /* Mix Closure */
3247
3248 MixClosureWeightNode::MixClosureWeightNode()
3249 : ShaderNode("mix_closure_weight")
3250 {
3251         add_input("Weight", SocketType::FLOAT, 1.0f);
3252         add_input("Fac", SocketType::FLOAT, 1.0f);
3253         add_output("Weight1", SocketType::FLOAT);
3254         add_output("Weight2", SocketType::FLOAT);
3255 }
3256
3257 void MixClosureWeightNode::compile(SVMCompiler& compiler)
3258 {
3259         ShaderInput *weight_in = input("Weight");
3260         ShaderInput *fac_in = input("Fac");
3261         ShaderOutput *weight1_out = output("Weight1");
3262         ShaderOutput *weight2_out = output("Weight2");
3263
3264         compiler.add_node(NODE_MIX_CLOSURE,
3265                 compiler.encode_uchar4(
3266                         compiler.stack_assign(fac_in),
3267                         compiler.stack_assign(weight_in),
3268                         compiler.stack_assign(weight1_out),
3269                         compiler.stack_assign(weight2_out)));
3270 }
3271
3272 void MixClosureWeightNode::compile(OSLCompiler& /*compiler*/)
3273 {
3274         assert(0);
3275 }
3276
3277 /* Invert */
3278
3279 InvertNode::InvertNode()
3280 : ShaderNode("invert")
3281 {
3282         add_input("Fac", SocketType::FLOAT, 1.0f);
3283         add_input("Color", SocketType::COLOR);
3284         add_output("Color",  SocketType::COLOR);
3285 }
3286
3287 void InvertNode::compile(SVMCompiler& compiler)
3288 {
3289         ShaderInput *fac_in = input("Fac");
3290         ShaderInput *color_in = input("Color");
3291         ShaderOutput *color_out = output("Color");
3292
3293         compiler.add_node(NODE_INVERT,
3294                 compiler.stack_assign(fac_in),
3295                 compiler.stack_assign(color_in),
3296                 compiler.stack_assign(color_out));
3297 }
3298
3299 void InvertNode::compile(OSLCompiler& compiler)
3300 {
3301         compiler.add(this, "node_invert");
3302 }
3303
3304 /* Mix */
3305
3306 MixNode::MixNode()
3307 : ShaderNode("mix")
3308 {
3309         type = ustring("Mix");
3310
3311         use_clamp = false;
3312
3313         add_input("Fac", SocketType::FLOAT, 0.5f);
3314         add_input("Color1", SocketType::COLOR);
3315         add_input("Color2", SocketType::COLOR);
3316         add_output("Color",  SocketType::COLOR);
3317 }
3318
3319 static NodeEnum mix_type_init()
3320 {
3321         NodeEnum enm;
3322
3323         enm.insert("Mix", NODE_MIX_BLEND);
3324         enm.insert("Add", NODE_MIX_ADD);
3325         enm.insert("Multiply", NODE_MIX_MUL);
3326         enm.insert("Screen", NODE_MIX_SCREEN);
3327         enm.insert("Overlay", NODE_MIX_OVERLAY);
3328         enm.insert("Subtract", NODE_MIX_SUB);
3329         enm.insert("Divide", NODE_MIX_DIV);
3330         enm.insert("Difference", NODE_MIX_DIFF);
3331         enm.insert("Darken", NODE_MIX_DARK);
3332         enm.insert("Lighten", NODE_MIX_LIGHT);
3333         enm.insert("Dodge", NODE_MIX_DODGE);
3334         enm.insert("Burn", NODE_MIX_BURN);
3335         enm.insert("Hue", NODE_MIX_HUE);
3336         enm.insert("Saturation", NODE_MIX_SAT);
3337         enm.insert("Value", NODE_MIX_VAL);
3338         enm.insert("Color", NODE_MIX_COLOR);
3339         enm.insert("Soft Light", NODE_MIX_SOFT);
3340         enm.insert("Linear Light", NODE_MIX_LINEAR);
3341
3342         return enm;
3343 }
3344
3345 NodeEnum MixNode::type_enum = mix_type_init();
3346
3347 void MixNode::compile(SVMCompiler& compiler)
3348 {
3349         ShaderInput *fac_in = input("Fac");
3350         ShaderInput *color1_in = input("Color1");
3351         ShaderInput *color2_in = input("Color2");
3352         ShaderOutput *color_out = output("Color");
3353
3354         compiler.add_node(NODE_MIX,
3355                 compiler.stack_assign(fac_in),
3356                 compiler.stack_assign(color1_in),
3357                 compiler.stack_assign(color2_in));
3358         compiler.add_node(NODE_MIX, type_enum[type], compiler.stack_assign(color_out));
3359
3360         if(use_clamp) {
3361                 compiler.add_node(NODE_MIX, 0, compiler.stack_assign(color_out));
3362                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, compiler.stack_assign(color_out));
3363         }
3364 }
3365
3366 void MixNode::compile(OSLCompiler& compiler)
3367 {
3368         compiler.parameter("type", type);
3369         compiler.parameter("Clamp", use_clamp);
3370         compiler.add(this, "node_mix");
3371 }
3372
3373 bool MixNode::constant_fold(ShaderGraph *graph, ShaderOutput * /*socket*/, float3 * optimized_value)
3374 {
3375         if(type != ustring("Mix")) {
3376                 return false;
3377         }
3378
3379         ShaderInput *fac_in = input("Fac");
3380         ShaderInput *color1_in = input("Color1");
3381         ShaderInput *color2_in = input("Color2");
3382         ShaderOutput *color_out = output("Color");
3383
3384         /* remove useless mix colors nodes */
3385         if(color1_in->link && color1_in->link == color2_in->link) {
3386                 graph->relink(this, color_out, color1_in->link);
3387                 return true;
3388         }
3389
3390         /* remove unused mix color input when factor is 0.0 or 1.0 */
3391         if(!fac_in->link) {
3392                 /* factor 0.0 */
3393                 if(fac_in->value_float() == 0.0f) {
3394                         if(color1_in->link)
3395                                 graph->relink(this, color_out, color1_in->link);
3396                         else
3397                                 *optimized_value = color1_in->value();
3398                         return true;
3399                 }
3400                 /* factor 1.0 */
3401                 else if(fac_in->value_float() == 1.0f) {
3402                         if(color2_in->link)
3403                                 graph->relink(this, color_out, color2_in->link);
3404                         else
3405                                 *optimized_value = color2_in->value();
3406                         return true;
3407                 }
3408         }
3409
3410         return false;
3411 }
3412
3413 /* Combine RGB */
3414 CombineRGBNode::CombineRGBNode()
3415 : ShaderNode("combine_rgb")
3416 {
3417         add_input("R", SocketType::FLOAT);
3418         add_input("G", SocketType::FLOAT);
3419         add_input("B", SocketType::FLOAT);
3420         add_output("Image", SocketType::COLOR);
3421 }
3422
3423 void CombineRGBNode::compile(SVMCompiler& compiler)
3424 {
3425         ShaderInput *red_in = input("R");
3426         ShaderInput *green_in = input("G");
3427         ShaderInput *blue_in = input("B");
3428         ShaderOutput *color_out = output("Image");
3429
3430         compiler.add_node(NODE_COMBINE_VECTOR,
3431                 compiler.stack_assign(red_in), 0,
3432                 compiler.stack_assign(color_out));
3433
3434         compiler.add_node(NODE_COMBINE_VECTOR,
3435                 compiler.stack_assign(green_in), 1,
3436                 compiler.stack_assign(color_out));
3437
3438         compiler.add_node(NODE_COMBINE_VECTOR,
3439                 compiler.stack_assign(blue_in), 2,
3440                 compiler.stack_assign(color_out));
3441 }
3442
3443 void CombineRGBNode::compile(OSLCompiler& compiler)
3444 {
3445         compiler.add(this, "node_combine_rgb");
3446 }
3447
3448 /* Combine XYZ */
3449 CombineXYZNode::CombineXYZNode()
3450 : ShaderNode("combine_xyz")
3451 {
3452         add_input("X", SocketType::FLOAT);
3453         add_input("Y", SocketType::FLOAT);
3454         add_input("Z", SocketType::FLOAT);
3455         add_output("Vector", SocketType::VECTOR);
3456 }
3457
3458 void CombineXYZNode::compile(SVMCompiler& compiler)
3459 {
3460         ShaderInput *x_in = input("X");
3461         ShaderInput *y_in = input("Y");
3462         ShaderInput *z_in = input("Z");
3463         ShaderOutput *vector_out = output("Vector");
3464
3465         compiler.add_node(NODE_COMBINE_VECTOR,
3466                 compiler.stack_assign(x_in), 0,
3467                 compiler.stack_assign(vector_out));
3468
3469         compiler.add_node(NODE_COMBINE_VECTOR,
3470                 compiler.stack_assign(y_in), 1,
3471                 compiler.stack_assign(vector_out));
3472
3473         compiler.add_node(NODE_COMBINE_VECTOR,
3474                 compiler.stack_assign(z_in), 2,
3475                 compiler.stack_assign(vector_out));
3476 }
3477
3478 void CombineXYZNode::compile(OSLCompiler& compiler)
3479 {
3480         compiler.add(this, "node_combine_xyz");
3481 }
3482
3483 /* Combine HSV */
3484 CombineHSVNode::CombineHSVNode()
3485 : ShaderNode("combine_hsv")
3486 {
3487         add_input("H", SocketType::FLOAT);
3488         add_input("S", SocketType::FLOAT);
3489         add_input("V", SocketType::FLOAT);
3490         add_output("Color", SocketType::COLOR);
3491 }
3492
3493 void CombineHSVNode::compile(SVMCompiler& compiler)
3494 {
3495         ShaderInput *hue_in = input("H");
3496         ShaderInput *saturation_in = input("S");
3497         ShaderInput *value_in = input("V");
3498         ShaderOutput *color_out = output("Color");
3499
3500         compiler.add_node(NODE_COMBINE_HSV,
3501                 compiler.stack_assign(hue_in),
3502                 compiler.stack_assign(saturation_in),
3503                 compiler.stack_assign(value_in));
3504         compiler.add_node(NODE_COMBINE_HSV,
3505                 compiler.stack_assign(color_out));
3506 }
3507
3508 void CombineHSVNode::compile(OSLCompiler& compiler)
3509 {
3510         compiler.add(this, "node_combine_hsv");
3511 }
3512
3513 /* Gamma */
3514 GammaNode::GammaNode()
3515 : ShaderNode("gamma")
3516 {
3517         add_input("Color", SocketType::COLOR);
3518         add_input("Gamma", SocketType::FLOAT);
3519         add_output("Color", SocketType::COLOR);
3520 }
3521
3522 bool GammaNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput *socket, float3 *optimized_value)
3523 {
3524         ShaderInput *color_in = input("Color");
3525         ShaderInput *gamma_in = input("Gamma");
3526
3527         if(socket == output("Color")) {
3528                 if(color_in->link == NULL && gamma_in->link == NULL) {
3529                         *optimized_value = svm_math_gamma_color(color_in->value(),
3530                                                                 gamma_in->value_float());
3531                         return true;
3532                 }
3533         }
3534
3535         return false;
3536 }
3537
3538 void GammaNode::compile(SVMCompiler& compiler)
3539 {
3540         ShaderInput *color_in = input("Color");
3541         ShaderInput *gamma_in = input("Gamma");
3542         ShaderOutput *color_out = output("Color");
3543
3544         compiler.add_node(NODE_GAMMA,
3545                 compiler.stack_assign(gamma_in),
3546                 compiler.stack_assign(color_in),
3547                 compiler.stack_assign(color_out));
3548 }
3549
3550 void GammaNode::compile(OSLCompiler& compiler)
3551 {
3552         compiler.add(this, "node_gamma");
3553 }
3554
3555 /* Bright Contrast */
3556 BrightContrastNode::BrightContrastNode()
3557 : ShaderNode("brightness")
3558 {
3559         add_input("Color", SocketType::COLOR);
3560         add_input("Bright", SocketType::FLOAT);
3561         add_input("Contrast", SocketType::FLOAT);
3562         add_output("Color", SocketType::COLOR);
3563 }
3564
3565 void BrightContrastNode::compile(SVMCompiler& compiler)
3566 {
3567         ShaderInput *color_in = input("Color");
3568         ShaderInput *bright_in = input("Bright");
3569         ShaderInput *contrast_in = input("Contrast");
3570         ShaderOutput *color_out = output("Color");
3571
3572         compiler.add_node(NODE_BRIGHTCONTRAST,
3573                 compiler.stack_assign(color_in),
3574                 compiler.stack_assign(color_out),
3575                 compiler.encode_uchar4(
3576                         compiler.stack_assign(bright_in),
3577                         compiler.stack_assign(contrast_in)));
3578 }
3579
3580 void BrightContrastNode::compile(OSLCompiler& compiler)
3581 {
3582         compiler.add(this, "node_brightness");
3583 }
3584
3585 /* Separate RGB */
3586 SeparateRGBNode::SeparateRGBNode()
3587 : ShaderNode("separate_rgb")
3588 {
3589         add_input("Image", SocketType::COLOR);
3590         add_output("R", SocketType::FLOAT);
3591         add_output("G", SocketType::FLOAT);
3592         add_output("B", SocketType::FLOAT);
3593 }
3594
3595 void SeparateRGBNode::compile(SVMCompiler& compiler)
3596 {
3597         ShaderInput *color_in = input("Image");
3598         ShaderOutput *red_out = output("R");
3599         ShaderOutput *green_out = output("G");
3600         ShaderOutput *blue_out = output("B");
3601