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