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