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