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