Cycles: Cleanup, indentation and braces
[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 static ShaderEnum wave_profile_init()
1077 {
1078         ShaderEnum enm;
1079
1080         enm.insert("Sine", NODE_WAVE_PROFILE_SIN);
1081         enm.insert("Saw", NODE_WAVE_PROFILE_SAW);
1082
1083         return enm;
1084 }
1085
1086 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
1087 ShaderEnum WaveTextureNode::profile_enum = wave_profile_init();
1088
1089 WaveTextureNode::WaveTextureNode()
1090 : TextureNode("wave_texture")
1091 {
1092         type = ustring("Bands");
1093         profile = ustring("Sine");
1094
1095         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
1096         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
1097         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
1098         add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
1099         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1100
1101         add_output("Color", SHADER_SOCKET_COLOR);
1102         add_output("Fac", SHADER_SOCKET_FLOAT);
1103 }
1104
1105 void WaveTextureNode::compile(SVMCompiler& compiler)
1106 {
1107         ShaderInput *scale_in = input("Scale");
1108         ShaderInput *distortion_in = input("Distortion");
1109         ShaderInput *dscale_in = input("Detail Scale");
1110         ShaderInput *detail_in = input("Detail");
1111         ShaderInput *vector_in = input("Vector");
1112         ShaderOutput *fac_out = output("Fac");
1113         ShaderOutput *color_out = output("Color");
1114
1115         if(scale_in->link) compiler.stack_assign(scale_in);
1116         if(detail_in->link) compiler.stack_assign(detail_in);
1117         if(distortion_in->link) compiler.stack_assign(distortion_in);
1118         if(dscale_in->link) compiler.stack_assign(dscale_in);
1119         if(vector_in->link) compiler.stack_assign(vector_in);
1120
1121         int vector_offset = vector_in->stack_offset;
1122
1123         if(!tex_mapping.skip()) {
1124                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1125                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1126         }
1127
1128         if(!fac_out->links.empty())
1129                 compiler.stack_assign(fac_out);
1130         if(!color_out->links.empty())
1131                 compiler.stack_assign(color_out);
1132
1133         compiler.add_node(NODE_TEX_WAVE,
1134                 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
1135                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
1136                 profile_enum[profile]);
1137
1138         compiler.add_node(
1139                 __float_as_int(scale_in->value.x),
1140                 __float_as_int(detail_in->value.x),
1141                 __float_as_int(distortion_in->value.x),
1142                 __float_as_int(dscale_in->value.x));
1143
1144         if(vector_offset != vector_in->stack_offset)
1145                 compiler.stack_clear_offset(vector_in->type, vector_offset);
1146 }
1147
1148 void WaveTextureNode::compile(OSLCompiler& compiler)
1149 {
1150         tex_mapping.compile(compiler);
1151
1152         compiler.parameter("Type", type);
1153         compiler.parameter("Profile", profile);
1154
1155         compiler.add(this, "node_wave_texture");
1156 }
1157
1158 /* Magic Texture */
1159
1160 MagicTextureNode::MagicTextureNode()
1161 : TextureNode("magic_texture")
1162 {
1163         depth = 2;
1164
1165         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1166         add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
1167         add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
1168
1169         add_output("Color", SHADER_SOCKET_COLOR);
1170         add_output("Fac", SHADER_SOCKET_FLOAT);
1171 }
1172
1173 void MagicTextureNode::compile(SVMCompiler& compiler)
1174 {
1175         ShaderInput *vector_in = input("Vector");
1176         ShaderInput *scale_in = input("Scale");
1177         ShaderInput *distortion_in = input("Distortion");
1178         ShaderOutput *color_out = output("Color");
1179         ShaderOutput *fac_out = output("Fac");
1180
1181         if(vector_in->link) compiler.stack_assign(vector_in);
1182         if(distortion_in->link) compiler.stack_assign(distortion_in);
1183         if(scale_in->link) compiler.stack_assign(scale_in);
1184
1185         int vector_offset = vector_in->stack_offset;
1186
1187         if(!tex_mapping.skip()) {
1188                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1189                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1190         }
1191
1192         if(!fac_out->links.empty())
1193                 compiler.stack_assign(fac_out);
1194         if(!color_out->links.empty())
1195                 compiler.stack_assign(color_out);
1196
1197         compiler.add_node(NODE_TEX_MAGIC,
1198                 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
1199                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, distortion_in->stack_offset));
1200         compiler.add_node(
1201                 __float_as_int(scale_in->value.x),
1202                 __float_as_int(distortion_in->value.x));
1203
1204         if(vector_offset != vector_in->stack_offset)
1205                 compiler.stack_clear_offset(vector_in->type, vector_offset);
1206 }
1207
1208 void MagicTextureNode::compile(OSLCompiler& compiler)
1209 {
1210         tex_mapping.compile(compiler);
1211
1212         compiler.parameter("Depth", depth);
1213         compiler.add(this, "node_magic_texture");
1214 }
1215
1216 /* Checker Texture */
1217
1218 CheckerTextureNode::CheckerTextureNode()
1219 : TextureNode("checker_texture")
1220 {
1221         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1222         add_input("Color1", SHADER_SOCKET_COLOR);
1223         add_input("Color2", SHADER_SOCKET_COLOR);
1224         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
1225
1226         add_output("Color", SHADER_SOCKET_COLOR);
1227         add_output("Fac", SHADER_SOCKET_FLOAT);
1228 }
1229
1230 void CheckerTextureNode::compile(SVMCompiler& compiler)
1231 {
1232         ShaderInput *vector_in = input("Vector");
1233         ShaderInput *color1_in = input("Color1");
1234         ShaderInput *color2_in = input("Color2");
1235         ShaderInput *scale_in = input("Scale");
1236         
1237         ShaderOutput *color_out = output("Color");
1238         ShaderOutput *fac_out = output("Fac");
1239
1240         compiler.stack_assign(vector_in);
1241         compiler.stack_assign(color1_in);
1242         compiler.stack_assign(color2_in);
1243         if(scale_in->link) compiler.stack_assign(scale_in);
1244
1245         int vector_offset = vector_in->stack_offset;
1246
1247         if(!tex_mapping.skip()) {
1248                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1249                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1250         }
1251
1252         if(!color_out->links.empty())
1253                 compiler.stack_assign(color_out);
1254         if(!fac_out->links.empty())
1255                 compiler.stack_assign(fac_out);
1256
1257         compiler.add_node(NODE_TEX_CHECKER,
1258                 compiler.encode_uchar4(vector_offset, color1_in->stack_offset, color2_in->stack_offset, scale_in->stack_offset),
1259                 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset),
1260                 __float_as_int(scale_in->value.x));
1261
1262         if(vector_offset != vector_in->stack_offset)
1263                 compiler.stack_clear_offset(vector_in->type, vector_offset);
1264 }
1265
1266 void CheckerTextureNode::compile(OSLCompiler& compiler)
1267 {
1268         tex_mapping.compile(compiler);
1269
1270         compiler.add(this, "node_checker_texture");
1271 }
1272
1273 /* Brick Texture */
1274
1275 BrickTextureNode::BrickTextureNode()
1276 : TextureNode("brick_texture")
1277 {
1278         offset = 0.5f;
1279         offset_frequency = 2;
1280         squash = 1.0f;
1281         squash_frequency = 2;
1282         
1283         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1284         add_input("Color1", SHADER_SOCKET_COLOR);
1285         add_input("Color2", SHADER_SOCKET_COLOR);
1286         add_input("Mortar", SHADER_SOCKET_COLOR);
1287         add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
1288         add_input("Mortar Size", SHADER_SOCKET_FLOAT, 0.02f);
1289         add_input("Bias", SHADER_SOCKET_FLOAT, 0.0f);
1290         add_input("Brick Width", SHADER_SOCKET_FLOAT, 0.5f);
1291         add_input("Row Height", SHADER_SOCKET_FLOAT, 0.25f);
1292
1293         add_output("Color", SHADER_SOCKET_COLOR);
1294         add_output("Fac", SHADER_SOCKET_FLOAT);
1295 }
1296
1297 void BrickTextureNode::compile(SVMCompiler& compiler)
1298 {
1299         ShaderInput *vector_in = input("Vector");
1300         ShaderInput *color1_in = input("Color1");
1301         ShaderInput *color2_in = input("Color2");
1302         ShaderInput *mortar_in = input("Mortar");
1303         ShaderInput *scale_in = input("Scale");
1304         ShaderInput *mortar_size_in = input("Mortar Size");
1305         ShaderInput *bias_in = input("Bias");
1306         ShaderInput *brick_width_in = input("Brick Width");
1307         ShaderInput *row_height_in = input("Row Height");
1308         
1309         ShaderOutput *color_out = output("Color");
1310         ShaderOutput *fac_out = output("Fac");
1311
1312         compiler.stack_assign(vector_in);
1313         compiler.stack_assign(color1_in);
1314         compiler.stack_assign(color2_in);
1315         compiler.stack_assign(mortar_in);
1316         if(scale_in->link) compiler.stack_assign(scale_in);
1317         if(mortar_size_in->link) compiler.stack_assign(mortar_size_in);
1318         if(bias_in->link) compiler.stack_assign(bias_in);
1319         if(brick_width_in->link) compiler.stack_assign(brick_width_in);
1320         if(row_height_in->link) compiler.stack_assign(row_height_in);
1321
1322         int vector_offset = vector_in->stack_offset;
1323
1324         if(!tex_mapping.skip()) {
1325                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1326                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1327         }
1328
1329         if(!color_out->links.empty())
1330                 compiler.stack_assign(color_out);
1331         if(!fac_out->links.empty())
1332                 compiler.stack_assign(fac_out);
1333
1334         compiler.add_node(NODE_TEX_BRICK,
1335                 compiler.encode_uchar4(vector_offset,
1336                         color1_in->stack_offset, color2_in->stack_offset, mortar_in->stack_offset),
1337                 compiler.encode_uchar4(scale_in->stack_offset,
1338                         mortar_size_in->stack_offset, bias_in->stack_offset, brick_width_in->stack_offset),
1339                 compiler.encode_uchar4(row_height_in->stack_offset,
1340                         color_out->stack_offset, fac_out->stack_offset));
1341                         
1342         compiler.add_node(compiler.encode_uchar4(offset_frequency, squash_frequency),
1343                 __float_as_int(scale_in->value.x),
1344                 __float_as_int(mortar_size_in->value.x),
1345                 __float_as_int(bias_in->value.x));
1346
1347         compiler.add_node(__float_as_int(brick_width_in->value.x),
1348                 __float_as_int(row_height_in->value.x),
1349                 __float_as_int(offset),
1350                 __float_as_int(squash));
1351
1352         if(vector_offset != vector_in->stack_offset)
1353                 compiler.stack_clear_offset(vector_in->type, vector_offset);
1354 }
1355
1356 void BrickTextureNode::compile(OSLCompiler& compiler)
1357 {
1358         tex_mapping.compile(compiler);
1359
1360         compiler.parameter("Offset", offset);
1361         compiler.parameter("OffsetFrequency", offset_frequency);
1362         compiler.parameter("Squash", squash);
1363         compiler.parameter("SquashFrequency", squash_frequency);
1364         compiler.add(this, "node_brick_texture");
1365 }
1366
1367 /* Point Density Texture */
1368
1369 static ShaderEnum point_density_space_init()
1370 {
1371         ShaderEnum enm;
1372
1373         enm.insert("Object", NODE_TEX_VOXEL_SPACE_OBJECT);
1374         enm.insert("World", NODE_TEX_VOXEL_SPACE_WORLD);
1375
1376         return enm;
1377 }
1378
1379 ShaderEnum PointDensityTextureNode::space_enum = point_density_space_init();
1380
1381 PointDensityTextureNode::PointDensityTextureNode()
1382 : ShaderNode("point_density")
1383 {
1384         image_manager = NULL;
1385         slot = -1;
1386         filename = "";
1387         space = ustring("Object");
1388         builtin_data = NULL;
1389         interpolation = INTERPOLATION_LINEAR;
1390
1391         tfm = transform_identity();
1392
1393         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::POSITION);
1394         add_output("Density", SHADER_SOCKET_FLOAT);
1395         add_output("Color", SHADER_SOCKET_COLOR);
1396 }
1397
1398 PointDensityTextureNode::~PointDensityTextureNode()
1399 {
1400         if(image_manager) {
1401                 image_manager->remove_image(filename,
1402                                             builtin_data,
1403                                             interpolation,
1404                                             EXTENSION_CLIP);
1405         }
1406 }
1407
1408 ShaderNode *PointDensityTextureNode::clone() const
1409 {
1410         PointDensityTextureNode *node = new PointDensityTextureNode(*this);
1411         node->image_manager = NULL;
1412         node->slot = -1;
1413         return node;
1414 }
1415
1416 void PointDensityTextureNode::attributes(Shader *shader,
1417                                          AttributeRequestSet *attributes)
1418 {
1419         if(shader->has_volume)
1420                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
1421
1422         ShaderNode::attributes(shader, attributes);
1423 }
1424
1425 void PointDensityTextureNode::compile(SVMCompiler& compiler)
1426 {
1427         ShaderInput *vector_in = input("Vector");
1428         ShaderOutput *density_out = output("Density");
1429         ShaderOutput *color_out = output("Color");
1430
1431         const bool use_density = !density_out->links.empty();
1432         const bool use_color = !color_out->links.empty();
1433
1434         image_manager = compiler.image_manager;
1435
1436         if(use_density || use_color) {
1437                 if(use_density)
1438                         compiler.stack_assign(density_out);
1439                 if(use_color)
1440                         compiler.stack_assign(color_out);
1441
1442                 if(slot == -1) {
1443                         bool is_float, is_linear;
1444                         slot = image_manager->add_image(filename, builtin_data,
1445                                                         false, 0,
1446                                                         is_float, is_linear,
1447                                                         interpolation,
1448                                                         EXTENSION_CLIP,
1449                                                         true);
1450                 }
1451
1452                 if(slot != -1) {
1453                         compiler.stack_assign(vector_in);
1454                         compiler.add_node(NODE_TEX_VOXEL,
1455                                           slot,
1456                                           compiler.encode_uchar4(vector_in->stack_offset,
1457                                                                  density_out->stack_offset,
1458                                                                  color_out->stack_offset,
1459                                                                  space_enum[space]));
1460                         if(space == "World") {
1461                                 compiler.add_node(tfm.x);
1462                                 compiler.add_node(tfm.y);
1463                                 compiler.add_node(tfm.z);
1464                                 compiler.add_node(tfm.w);
1465                         }
1466                 }
1467                 else {
1468                         compiler.add_node(NODE_VALUE_F,
1469                                           __float_as_int(0.0f),
1470                                           density_out->stack_offset);
1471                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
1472                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
1473                                                                     TEX_IMAGE_MISSING_G,
1474                                                                     TEX_IMAGE_MISSING_B));
1475                 }
1476         }
1477 }
1478
1479 void PointDensityTextureNode::compile(OSLCompiler& compiler)
1480 {
1481         ShaderOutput *density_out = output("Density");
1482         ShaderOutput *color_out = output("Color");
1483
1484         const bool use_density = !density_out->links.empty();
1485         const bool use_color = !color_out->links.empty();
1486
1487         image_manager = compiler.image_manager;
1488
1489         if(use_density || use_color) {
1490                 if(slot == -1) {
1491                         bool is_float, is_linear;
1492                         slot = image_manager->add_image(filename, builtin_data,
1493                                                         false, 0,
1494                                                         is_float, is_linear,
1495                                                         interpolation,
1496                                                         EXTENSION_CLIP,
1497                                                         true);
1498                 }
1499
1500                 if(slot != -1) {
1501                         compiler.parameter("filename", string_printf("@%d", slot).c_str());
1502                 }
1503                 if(space == "World") {
1504                         compiler.parameter("mapping", transform_transpose(tfm));
1505                         compiler.parameter("use_mapping", 1);
1506                 }
1507                 switch(interpolation) {
1508                         case INTERPOLATION_CLOSEST:
1509                                 compiler.parameter("interpolation", "closest");
1510                                 break;
1511                         case INTERPOLATION_CUBIC:
1512                                 compiler.parameter("interpolation", "cubic");
1513                                 break;
1514                         case INTERPOLATION_LINEAR:
1515                         default:
1516                                 compiler.parameter("interpolation", "linear");
1517                                 break;
1518                 }
1519
1520                 compiler.add(this, "node_voxel_texture");
1521         }
1522 }
1523
1524 /* Normal */
1525
1526 NormalNode::NormalNode()
1527 : ShaderNode("normal")
1528 {
1529         direction = make_float3(0.0f, 0.0f, 1.0f);
1530
1531         add_input("Normal", SHADER_SOCKET_NORMAL);
1532         add_output("Normal", SHADER_SOCKET_NORMAL);
1533         add_output("Dot",  SHADER_SOCKET_FLOAT);
1534 }
1535
1536 void NormalNode::compile(SVMCompiler& compiler)
1537 {
1538         ShaderInput *normal_in = input("Normal");
1539         ShaderOutput *normal_out = output("Normal");
1540         ShaderOutput *dot_out = output("Dot");
1541
1542         compiler.stack_assign(normal_in);
1543         compiler.stack_assign(normal_out);
1544         compiler.stack_assign(dot_out);
1545
1546         compiler.add_node(NODE_NORMAL, normal_in->stack_offset, normal_out->stack_offset, dot_out->stack_offset);
1547         compiler.add_node(
1548                 __float_as_int(direction.x),
1549                 __float_as_int(direction.y),
1550                 __float_as_int(direction.z));
1551 }
1552
1553 void NormalNode::compile(OSLCompiler& compiler)
1554 {
1555         compiler.parameter_normal("Direction", direction);
1556         compiler.add(this, "node_normal");
1557 }
1558
1559 /* Mapping */
1560
1561 MappingNode::MappingNode()
1562 : ShaderNode("mapping")
1563 {
1564         add_input("Vector", SHADER_SOCKET_POINT);
1565         add_output("Vector", SHADER_SOCKET_POINT);
1566 }
1567
1568 void MappingNode::compile(SVMCompiler& compiler)
1569 {
1570         ShaderInput *vector_in = input("Vector");
1571         ShaderOutput *vector_out = output("Vector");
1572
1573         compiler.stack_assign(vector_in);
1574         compiler.stack_assign(vector_out);
1575
1576         tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
1577 }
1578
1579 void MappingNode::compile(OSLCompiler& compiler)
1580 {
1581         Transform tfm = transform_transpose(tex_mapping.compute_transform());
1582         compiler.parameter("Matrix", tfm);
1583         compiler.parameter_point("mapping_min", tex_mapping.min);
1584         compiler.parameter_point("mapping_max", tex_mapping.max);
1585         compiler.parameter("use_minmax", tex_mapping.use_minmax);
1586
1587         compiler.add(this, "node_mapping");
1588 }
1589
1590 /* Convert */
1591
1592 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_, bool autoconvert)
1593 : ShaderNode("convert")
1594 {
1595         from = from_;
1596         to = to_;
1597
1598         if(autoconvert)
1599                 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1600
1601         assert(from != to);
1602
1603         if(from == SHADER_SOCKET_FLOAT)
1604                 add_input("Val", SHADER_SOCKET_FLOAT);
1605         else if(from == SHADER_SOCKET_INT)
1606                 add_input("ValInt", SHADER_SOCKET_INT);
1607         else if(from == SHADER_SOCKET_COLOR)
1608                 add_input("Color", SHADER_SOCKET_COLOR);
1609         else if(from == SHADER_SOCKET_VECTOR)
1610                 add_input("Vector", SHADER_SOCKET_VECTOR);
1611         else if(from == SHADER_SOCKET_POINT)
1612                 add_input("Point", SHADER_SOCKET_POINT);
1613         else if(from == SHADER_SOCKET_NORMAL)
1614                 add_input("Normal", SHADER_SOCKET_NORMAL);
1615         else if(from == SHADER_SOCKET_STRING)
1616                 add_input("String", SHADER_SOCKET_STRING);
1617         else
1618                 assert(0);
1619
1620         if(to == SHADER_SOCKET_FLOAT)
1621                 add_output("Val", SHADER_SOCKET_FLOAT);
1622         else if(to == SHADER_SOCKET_INT)
1623                 add_output("ValInt", SHADER_SOCKET_INT);
1624         else if(to == SHADER_SOCKET_COLOR)
1625                 add_output("Color", SHADER_SOCKET_COLOR);
1626         else if(to == SHADER_SOCKET_VECTOR)
1627                 add_output("Vector", SHADER_SOCKET_VECTOR);
1628         else if(to == SHADER_SOCKET_POINT)
1629                 add_output("Point", SHADER_SOCKET_POINT);
1630         else if(to == SHADER_SOCKET_NORMAL)
1631                 add_output("Normal", SHADER_SOCKET_NORMAL);
1632         else if(to == SHADER_SOCKET_STRING)
1633                 add_output("String", SHADER_SOCKET_STRING);
1634         else
1635                 assert(0);
1636 }
1637
1638 bool ConvertNode::constant_fold(ShaderOutput *socket, float3 *optimized_value)
1639 {
1640         ShaderInput *in = inputs[0];
1641         float3 value = in->value;
1642
1643         /* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
1644
1645         if(socket == outputs[0] && in->link == NULL) {
1646                 if(from == SHADER_SOCKET_FLOAT) {
1647                         if(to == SHADER_SOCKET_INT)
1648                         /* float to int */
1649                                 return false;
1650                         else
1651                         /* float to float3 */
1652                                 *optimized_value = make_float3(value.x, value.x, value.x);
1653                 }
1654                 else if(from == SHADER_SOCKET_INT) {
1655                         if(to == SHADER_SOCKET_FLOAT)
1656                         /* int to float */
1657                                 return false;
1658                         else
1659                         /* int to vector/point/normal */
1660                                 return false;
1661                 }
1662                 else if(to == SHADER_SOCKET_FLOAT) {
1663                         if(from == SHADER_SOCKET_COLOR)
1664                         /* color to float */
1665                                 optimized_value->x = linear_rgb_to_gray(value);
1666                         else
1667                         /* vector/point/normal to float */
1668                                 optimized_value->x = average(value);
1669                 }
1670                 else if(to == SHADER_SOCKET_INT) {
1671                         if(from == SHADER_SOCKET_COLOR)
1672                         /* color to int */
1673                                 return false;
1674                         else
1675                         /* vector/point/normal to int */
1676                                 return false;
1677                 }
1678                 else {
1679                         *optimized_value = value;
1680                 }
1681
1682                 return true;
1683         }
1684
1685         return false;
1686 }
1687
1688 void ConvertNode::compile(SVMCompiler& compiler)
1689 {
1690         ShaderInput *in = inputs[0];
1691         ShaderOutput *out = outputs[0];
1692
1693         if(from == SHADER_SOCKET_FLOAT) {
1694                 compiler.stack_assign(in);
1695                 compiler.stack_assign(out);
1696
1697                 if(to == SHADER_SOCKET_INT)
1698                         /* float to int */
1699                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, in->stack_offset, out->stack_offset);
1700                 else
1701                         /* float to float3 */
1702                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
1703         }
1704         else if(from == SHADER_SOCKET_INT) {
1705                 compiler.stack_assign(in);
1706                 compiler.stack_assign(out);
1707
1708                 if(to == SHADER_SOCKET_FLOAT)
1709                         /* int to float */
1710                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, in->stack_offset, out->stack_offset);
1711                 else
1712                         /* int to vector/point/normal */
1713                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, in->stack_offset, out->stack_offset);
1714         }
1715         else if(to == SHADER_SOCKET_FLOAT) {
1716                 compiler.stack_assign(in);
1717                 compiler.stack_assign(out);
1718
1719                 if(from == SHADER_SOCKET_COLOR)
1720                         /* color to float */
1721                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
1722                 else
1723                         /* vector/point/normal to float */
1724                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
1725         }
1726         else if(to == SHADER_SOCKET_INT) {
1727                 compiler.stack_assign(in);
1728                 compiler.stack_assign(out);
1729
1730                 if(from == SHADER_SOCKET_COLOR)
1731                         /* color to int */
1732                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, in->stack_offset, out->stack_offset);
1733                 else
1734                         /* vector/point/normal to int */
1735                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, in->stack_offset, out->stack_offset);
1736         }
1737         else {
1738                 /* float3 to float3 */
1739                 if(in->link) {
1740                         /* no op in SVM */
1741                         compiler.stack_link(in, out);
1742                 }
1743                 else {
1744                         /* set 0,0,0 value */
1745                         compiler.stack_assign(in);
1746                         compiler.stack_assign(out);
1747
1748                         compiler.add_node(NODE_VALUE_V, out->stack_offset);
1749                         compiler.add_node(NODE_VALUE_V, in->value);
1750                 }
1751         }
1752 }
1753
1754 void ConvertNode::compile(OSLCompiler& compiler)
1755 {
1756         if(from == SHADER_SOCKET_FLOAT)
1757                 compiler.add(this, "node_convert_from_float");
1758         else if(from == SHADER_SOCKET_INT)
1759                 compiler.add(this, "node_convert_from_int");
1760         else if(from == SHADER_SOCKET_COLOR)
1761                 compiler.add(this, "node_convert_from_color");
1762         else if(from == SHADER_SOCKET_VECTOR)
1763                 compiler.add(this, "node_convert_from_vector");
1764         else if(from == SHADER_SOCKET_POINT)
1765                 compiler.add(this, "node_convert_from_point");
1766         else if(from == SHADER_SOCKET_NORMAL)
1767                 compiler.add(this, "node_convert_from_normal");
1768         else
1769                 assert(0);
1770 }
1771
1772 /* Proxy */
1773
1774 ProxyNode::ProxyNode(ShaderSocketType type_)
1775 : ShaderNode("proxy")
1776 {
1777         type = type_;
1778         special_type = SHADER_SPECIAL_TYPE_PROXY;
1779
1780         add_input("Input", type);
1781         add_output("Output", type);
1782 }
1783
1784 void ProxyNode::compile(SVMCompiler& /*compiler*/)
1785 {
1786 }
1787
1788 void ProxyNode::compile(OSLCompiler& /*compiler*/)
1789 {
1790 }
1791
1792 /* BSDF Closure */
1793
1794 BsdfNode::BsdfNode(bool scattering_)
1795 : ShaderNode("bsdf"), scattering(scattering_)
1796 {
1797         special_type = SHADER_SPECIAL_TYPE_CLOSURE;
1798
1799         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1800         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
1801         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1802
1803         if(scattering) {
1804                 closure = CLOSURE_BSSRDF_CUBIC_ID;
1805                 add_output("BSSRDF", SHADER_SOCKET_CLOSURE);
1806         }
1807         else {
1808                 closure = CLOSURE_BSDF_DIFFUSE_ID;
1809                 add_output("BSDF", SHADER_SOCKET_CLOSURE);
1810         }
1811 }
1812
1813 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3, ShaderInput *param4)
1814 {
1815         ShaderInput *color_in = input("Color");
1816         ShaderInput *normal_in = input("Normal");
1817         ShaderInput *tangent_in = input("Tangent");
1818
1819         if(color_in->link) {
1820                 compiler.stack_assign(color_in);
1821                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1822         }
1823         else
1824                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1825         
1826         if(param1)
1827                 compiler.stack_assign(param1);
1828         if(param2)
1829                 compiler.stack_assign(param2);
1830         if(param3)
1831                 compiler.stack_assign(param3);
1832         if(param4)
1833                 compiler.stack_assign(param4);
1834
1835         if(normal_in->link)
1836                 compiler.stack_assign(normal_in);
1837
1838         if(tangent_in && tangent_in->link)
1839                 compiler.stack_assign(tangent_in);
1840
1841         compiler.add_node(NODE_CLOSURE_BSDF,
1842                 compiler.encode_uchar4(closure,
1843                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1844                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1845                         compiler.closure_mix_weight_offset()),
1846                 __float_as_int((param1)? param1->value.x: 0.0f),
1847                 __float_as_int((param2)? param2->value.x: 0.0f));
1848
1849         if(tangent_in) {
1850                 compiler.add_node(normal_in->stack_offset, tangent_in->stack_offset,
1851                         (param3)? param3->stack_offset: SVM_STACK_INVALID,
1852                         (param4)? param4->stack_offset: SVM_STACK_INVALID);
1853         }
1854         else {
1855                 compiler.add_node(normal_in->stack_offset, SVM_STACK_INVALID,
1856                         (param3)? param3->stack_offset: SVM_STACK_INVALID,
1857                         (param4)? param4->stack_offset: SVM_STACK_INVALID);
1858         }
1859 }
1860
1861 void BsdfNode::compile(SVMCompiler& compiler)
1862 {
1863         compile(compiler, NULL, NULL);
1864 }
1865
1866 void BsdfNode::compile(OSLCompiler& /*compiler*/)
1867 {
1868         assert(0);
1869 }
1870
1871 /* Anisotropic BSDF Closure */
1872
1873 static ShaderEnum aniso_distribution_init()
1874 {
1875         ShaderEnum enm;
1876
1877         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID);
1878         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1879         enm.insert("Ashikhmin-Shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID);
1880
1881         return enm;
1882 }
1883
1884 ShaderEnum AnisotropicBsdfNode::distribution_enum = aniso_distribution_init();
1885
1886 AnisotropicBsdfNode::AnisotropicBsdfNode()
1887 {
1888         closure = CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID;
1889         distribution = ustring("GGX");
1890
1891         add_input("Tangent", SHADER_SOCKET_VECTOR, ShaderInput::TANGENT);
1892
1893         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1894         add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.5f);
1895         add_input("Rotation", SHADER_SOCKET_FLOAT, 0.0f);
1896 }
1897
1898 void AnisotropicBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
1899 {
1900         if(shader->has_surface) {
1901                 ShaderInput *tangent_in = input("Tangent");
1902
1903                 if(!tangent_in->link)
1904                         attributes->add(ATTR_STD_GENERATED);
1905         }
1906
1907         ShaderNode::attributes(shader, attributes);
1908 }
1909
1910 void AnisotropicBsdfNode::compile(SVMCompiler& compiler)
1911 {
1912         closure = (ClosureType)distribution_enum[distribution];
1913
1914         BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1915 }
1916
1917 void AnisotropicBsdfNode::compile(OSLCompiler& compiler)
1918 {
1919         compiler.parameter("distribution", distribution);
1920         compiler.add(this, "node_anisotropic_bsdf");
1921 }
1922
1923 /* Glossy BSDF Closure */
1924
1925 static ShaderEnum glossy_distribution_init()
1926 {
1927         ShaderEnum enm;
1928
1929         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1930         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1931         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1932         enm.insert("Ashikhmin-Shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
1933
1934         return enm;
1935 }
1936
1937 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1938
1939 GlossyBsdfNode::GlossyBsdfNode()
1940 {
1941         closure = CLOSURE_BSDF_MICROFACET_GGX_ID;
1942         distribution = ustring("GGX");
1943         distribution_orig = ustring("");
1944
1945         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1946 }
1947
1948 void GlossyBsdfNode::simplify_settings(Scene *scene)
1949 {
1950         if(distribution_orig == "") {
1951                 distribution_orig = distribution;
1952         }
1953         Integrator *integrator = scene->integrator;
1954         if(integrator->filter_glossy == 0.0f) {
1955                 /* Fallback to Sharp closure for Roughness close to 0.
1956                  * Note: Keep the epsilon in sync with kernel!
1957                  */
1958                 ShaderInput *roughness_input = input("Roughness");
1959                 if(!roughness_input->link && roughness_input->value.x <= 1e-4f) {
1960                         distribution = ustring("Sharp");
1961                 }
1962         }
1963         else {
1964                 /* Rollback to original distribution when filter glossy is used. */
1965                 distribution = distribution_orig;
1966         }
1967         closure = (ClosureType)distribution_enum[distribution];
1968 }
1969
1970 bool GlossyBsdfNode::has_integrator_dependency()
1971 {
1972         ShaderInput *roughness_input = input("Roughness");
1973         return !roughness_input->link && roughness_input->value.x <= 1e-4f;
1974 }
1975
1976 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1977 {
1978         closure = (ClosureType)distribution_enum[distribution];
1979
1980         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1981                 BsdfNode::compile(compiler, NULL, NULL);
1982         else
1983                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1984 }
1985
1986 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1987 {
1988         compiler.parameter("distribution", distribution);
1989         compiler.add(this, "node_glossy_bsdf");
1990 }
1991
1992 /* Glass BSDF Closure */
1993
1994 static ShaderEnum glass_distribution_init()
1995 {
1996         ShaderEnum enm;
1997
1998         enm.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1999         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
2000         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
2001
2002         return enm;
2003 }
2004
2005 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
2006
2007 GlassBsdfNode::GlassBsdfNode()
2008 {
2009         closure = CLOSURE_BSDF_SHARP_GLASS_ID;
2010         distribution = ustring("Sharp");
2011         distribution_orig = ustring("");
2012
2013         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
2014         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
2015 }
2016
2017 void GlassBsdfNode::simplify_settings(Scene *scene)
2018 {
2019         if(distribution_orig == "") {
2020                 distribution_orig = distribution;
2021         }
2022         Integrator *integrator = scene->integrator;
2023         if(integrator->filter_glossy == 0.0f) {
2024                 /* Fallback to Sharp closure for Roughness close to 0.
2025                  * Note: Keep the epsilon in sync with kernel!
2026                  */
2027                 ShaderInput *roughness_input = input("Roughness");
2028                 if(!roughness_input->link && roughness_input->value.x <= 1e-4f) {
2029                         distribution = ustring("Sharp");
2030                 }
2031         }
2032         else {
2033                 /* Rollback to original distribution when filter glossy is used. */
2034                 distribution = distribution_orig;
2035         }
2036         closure = (ClosureType)distribution_enum[distribution];
2037 }
2038
2039 bool GlassBsdfNode::has_integrator_dependency()
2040 {
2041         ShaderInput *roughness_input = input("Roughness");
2042         return !roughness_input->link && roughness_input->value.x <= 1e-4f;
2043 }
2044
2045 void GlassBsdfNode::compile(SVMCompiler& compiler)
2046 {
2047         closure = (ClosureType)distribution_enum[distribution];
2048
2049         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
2050                 BsdfNode::compile(compiler, NULL, input("IOR"));
2051         else
2052                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2053 }
2054
2055 void GlassBsdfNode::compile(OSLCompiler& compiler)
2056 {
2057         compiler.parameter("distribution", distribution);
2058         compiler.add(this, "node_glass_bsdf");
2059 }
2060
2061 /* Refraction BSDF Closure */
2062
2063 static ShaderEnum refraction_distribution_init()
2064 {
2065         ShaderEnum enm;
2066
2067         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
2068         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
2069         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
2070
2071         return enm;
2072 }
2073
2074 ShaderEnum RefractionBsdfNode::distribution_enum = refraction_distribution_init();
2075
2076 RefractionBsdfNode::RefractionBsdfNode()
2077 {
2078         closure = CLOSURE_BSDF_REFRACTION_ID;
2079         distribution = ustring("Sharp");
2080         distribution_orig = ustring("");
2081
2082         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
2083         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
2084 }
2085
2086 void RefractionBsdfNode::simplify_settings(Scene *scene)
2087 {
2088         if(distribution_orig == "") {
2089                 distribution_orig = distribution;
2090         }
2091         Integrator *integrator = scene->integrator;
2092         if(integrator->filter_glossy == 0.0f) {
2093                 /* Fallback to Sharp closure for Roughness close to 0.
2094                  * Note: Keep the epsilon in sync with kernel!
2095                  */
2096                 ShaderInput *roughness_input = input("Roughness");
2097                 if(!roughness_input->link && roughness_input->value.x <= 1e-4f) {
2098                         distribution = ustring("Sharp");
2099                 }
2100         }
2101         else {
2102                 /* Rollback to original distribution when filter glossy is used. */
2103                 distribution = distribution_orig;
2104         }
2105         closure = (ClosureType)distribution_enum[distribution];
2106 }
2107
2108 bool RefractionBsdfNode::has_integrator_dependency()
2109 {
2110         ShaderInput *roughness_input = input("Roughness");
2111         return !roughness_input->link && roughness_input->value.x <= 1e-4f;
2112 }
2113
2114 void RefractionBsdfNode::compile(SVMCompiler& compiler)
2115 {
2116         closure = (ClosureType)distribution_enum[distribution];
2117
2118         if(closure == CLOSURE_BSDF_REFRACTION_ID)
2119                 BsdfNode::compile(compiler, NULL, input("IOR"));
2120         else
2121                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
2122 }
2123
2124 void RefractionBsdfNode::compile(OSLCompiler& compiler)
2125 {
2126         compiler.parameter("distribution", distribution);
2127         compiler.add(this, "node_refraction_bsdf");
2128 }
2129
2130 /* Toon BSDF Closure */
2131
2132 static ShaderEnum toon_component_init()
2133 {
2134         ShaderEnum enm;
2135
2136         enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
2137         enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
2138
2139         return enm;
2140 }
2141
2142 ShaderEnum ToonBsdfNode::component_enum = toon_component_init();
2143
2144 ToonBsdfNode::ToonBsdfNode()
2145 {
2146         closure = CLOSURE_BSDF_DIFFUSE_TOON_ID;
2147         component = ustring("Diffuse");
2148
2149         add_input("Size", SHADER_SOCKET_FLOAT, 0.5f);
2150         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2151 }
2152
2153 void ToonBsdfNode::compile(SVMCompiler& compiler)
2154 {
2155         closure = (ClosureType)component_enum[component];
2156         
2157         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
2158 }
2159
2160 void ToonBsdfNode::compile(OSLCompiler& compiler)
2161 {
2162         compiler.parameter("component", component);
2163         compiler.add(this, "node_toon_bsdf");
2164 }
2165
2166 /* Velvet BSDF Closure */
2167
2168 VelvetBsdfNode::VelvetBsdfNode()
2169 {
2170         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
2171
2172         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
2173 }
2174
2175 void VelvetBsdfNode::compile(SVMCompiler& compiler)
2176 {
2177         BsdfNode::compile(compiler, input("Sigma"), NULL);
2178 }
2179
2180 void VelvetBsdfNode::compile(OSLCompiler& compiler)
2181 {
2182         compiler.add(this, "node_velvet_bsdf");
2183 }
2184
2185 /* Diffuse BSDF Closure */
2186
2187 DiffuseBsdfNode::DiffuseBsdfNode()
2188 {
2189         closure = CLOSURE_BSDF_DIFFUSE_ID;
2190         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
2191 }
2192
2193 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
2194 {
2195         BsdfNode::compile(compiler, input("Roughness"), NULL);
2196 }
2197
2198 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
2199 {
2200         compiler.add(this, "node_diffuse_bsdf");
2201 }
2202
2203 /* Translucent BSDF Closure */
2204
2205 TranslucentBsdfNode::TranslucentBsdfNode()
2206 {
2207         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
2208 }
2209
2210 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
2211 {
2212         BsdfNode::compile(compiler, NULL, NULL);
2213 }
2214
2215 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
2216 {
2217         compiler.add(this, "node_translucent_bsdf");
2218 }
2219
2220 /* Transparent BSDF Closure */
2221
2222 TransparentBsdfNode::TransparentBsdfNode()
2223 {
2224         name = "transparent";
2225         closure = CLOSURE_BSDF_TRANSPARENT_ID;
2226 }
2227
2228 void TransparentBsdfNode::compile(SVMCompiler& compiler)
2229 {
2230         BsdfNode::compile(compiler, NULL, NULL);
2231 }
2232
2233 void TransparentBsdfNode::compile(OSLCompiler& compiler)
2234 {
2235         compiler.add(this, "node_transparent_bsdf");
2236 }
2237
2238 /* Subsurface Scattering Closure */
2239
2240 static ShaderEnum subsurface_falloff_init()
2241 {
2242         ShaderEnum enm;
2243
2244         enm.insert("Cubic", CLOSURE_BSSRDF_CUBIC_ID);
2245         enm.insert("Gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
2246
2247         return enm;
2248 }
2249
2250 ShaderEnum SubsurfaceScatteringNode::falloff_enum = subsurface_falloff_init();
2251
2252 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
2253 : BsdfNode(true)
2254 {
2255         name = "subsurface_scattering";
2256         closure = CLOSURE_BSSRDF_CUBIC_ID;
2257
2258         add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
2259         add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
2260         add_input("Sharpness", SHADER_SOCKET_FLOAT, 0.0f);
2261         add_input("Texture Blur", SHADER_SOCKET_FLOAT, 1.0f);
2262 }
2263
2264 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
2265 {
2266         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
2267 }
2268
2269 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
2270 {
2271         compiler.parameter("Falloff", falloff_enum[closure]);
2272         compiler.add(this, "node_subsurface_scattering");
2273 }
2274
2275 bool SubsurfaceScatteringNode::has_bssrdf_bump()
2276 {
2277         /* detect if anything is plugged into the normal input besides the default */
2278         ShaderInput *normal_in = input("Normal");
2279         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
2280 }
2281
2282 /* Emissive Closure */
2283
2284 EmissionNode::EmissionNode()
2285 : ShaderNode("emission")
2286 {
2287         special_type = SHADER_SPECIAL_TYPE_EMISSION;
2288
2289         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
2290         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
2291         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
2292
2293         add_output("Emission", SHADER_SOCKET_CLOSURE);
2294 }
2295
2296 void EmissionNode::compile(SVMCompiler& compiler)
2297 {
2298         ShaderInput *color_in = input("Color");
2299         ShaderInput *strength_in = input("Strength");
2300
2301         if(color_in->link || strength_in->link) {
2302                 compiler.stack_assign(color_in);
2303                 compiler.stack_assign(strength_in);
2304                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
2305         }
2306         else
2307                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
2308
2309         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
2310 }
2311
2312 void EmissionNode::compile(OSLCompiler& compiler)
2313 {
2314         compiler.add(this, "node_emission");
2315 }
2316
2317 /* Background Closure */
2318
2319 BackgroundNode::BackgroundNode()
2320 : ShaderNode("background")
2321 {
2322         special_type = SHADER_SPECIAL_TYPE_BACKGROUND;
2323
2324         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
2325         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
2326         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
2327
2328         add_output("Background", SHADER_SOCKET_CLOSURE);
2329 }
2330
2331 void BackgroundNode::compile(SVMCompiler& compiler)
2332 {
2333         ShaderInput *color_in = input("Color");
2334         ShaderInput *strength_in = input("Strength");
2335
2336         if(color_in->link || strength_in->link) {
2337                 compiler.stack_assign(color_in);
2338                 compiler.stack_assign(strength_in);
2339                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
2340         }
2341         else
2342                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
2343
2344         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
2345 }
2346
2347 void BackgroundNode::compile(OSLCompiler& compiler)
2348 {
2349         compiler.add(this, "node_background");
2350 }
2351
2352 /* Holdout Closure */
2353
2354 HoldoutNode::HoldoutNode()
2355 : ShaderNode("holdout")
2356 {
2357         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
2358         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
2359
2360         add_output("Holdout", SHADER_SOCKET_CLOSURE);
2361 }
2362
2363 void HoldoutNode::compile(SVMCompiler& compiler)
2364 {
2365         float3 value = make_float3(1.0f, 1.0f, 1.0f);
2366
2367         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
2368         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
2369 }
2370
2371 void HoldoutNode::compile(OSLCompiler& compiler)
2372 {
2373         compiler.add(this, "node_holdout");
2374 }
2375
2376 /* Ambient Occlusion */
2377
2378 AmbientOcclusionNode::AmbientOcclusionNode()
2379 : ShaderNode("ambient_occlusion")
2380 {
2381         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2382         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
2383         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
2384
2385         add_output("AO", SHADER_SOCKET_CLOSURE);
2386 }
2387
2388 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
2389 {
2390         ShaderInput *color_in = input("Color");
2391
2392         if(color_in->link) {
2393                 compiler.stack_assign(color_in);
2394                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
2395         }
2396         else
2397                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
2398
2399         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
2400 }
2401
2402 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
2403 {
2404         compiler.add(this, "node_ambient_occlusion");
2405 }
2406
2407 /* Volume Closure */
2408
2409 VolumeNode::VolumeNode()
2410 : ShaderNode("volume")
2411 {
2412         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2413
2414         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
2415         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
2416         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
2417
2418         add_output("Volume", SHADER_SOCKET_CLOSURE);
2419 }
2420
2421 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
2422 {
2423         ShaderInput *color_in = input("Color");
2424
2425         if(color_in->link) {
2426                 compiler.stack_assign(color_in);
2427                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
2428         }
2429         else
2430                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
2431         
2432         if(param1)
2433                 compiler.stack_assign(param1);
2434         if(param2)
2435                 compiler.stack_assign(param2);
2436
2437         compiler.add_node(NODE_CLOSURE_VOLUME,
2438                 compiler.encode_uchar4(closure,
2439                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
2440                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
2441                         compiler.closure_mix_weight_offset()),
2442                 __float_as_int((param1)? param1->value.x: 0.0f),
2443                 __float_as_int((param2)? param2->value.x: 0.0f));
2444 }
2445
2446 void VolumeNode::compile(SVMCompiler& compiler)
2447 {
2448         compile(compiler, NULL, NULL);
2449 }
2450
2451 void VolumeNode::compile(OSLCompiler& /*compiler*/)
2452 {
2453         assert(0);
2454 }
2455
2456 /* Absorption Volume Closure */
2457
2458 AbsorptionVolumeNode::AbsorptionVolumeNode()
2459 {
2460         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2461 }
2462
2463 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2464 {
2465         VolumeNode::compile(compiler, input("Density"), NULL);
2466 }
2467
2468 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2469 {
2470         compiler.add(this, "node_absorption_volume");
2471 }
2472
2473 /* Scatter Volume Closure */
2474
2475 ScatterVolumeNode::ScatterVolumeNode()
2476 {
2477         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2478         
2479         add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.0f);
2480 }
2481
2482 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2483 {
2484         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2485 }
2486
2487 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2488 {
2489         compiler.add(this, "node_scatter_volume");
2490 }
2491
2492 /* Hair BSDF Closure */
2493
2494 static ShaderEnum hair_component_init()
2495 {
2496         ShaderEnum enm;
2497
2498         enm.insert("Reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2499         enm.insert("Transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2500
2501         return enm;
2502 }
2503
2504 ShaderEnum HairBsdfNode::component_enum = hair_component_init();
2505
2506 HairBsdfNode::HairBsdfNode()
2507 {
2508         closure = CLOSURE_BSDF_HAIR_REFLECTION_ID;
2509         component = ustring("Reflection");
2510
2511         add_input("Offset", SHADER_SOCKET_FLOAT);
2512         add_input("RoughnessU", SHADER_SOCKET_FLOAT);
2513         add_input("RoughnessV", SHADER_SOCKET_FLOAT);
2514         add_input("Tangent", SHADER_SOCKET_VECTOR);
2515 }
2516
2517 void HairBsdfNode::compile(SVMCompiler& compiler)
2518 {
2519         closure = (ClosureType)component_enum[component];
2520
2521         BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
2522 }
2523
2524 void HairBsdfNode::compile(OSLCompiler& compiler)
2525 {
2526         compiler.parameter("component", component);
2527
2528         compiler.add(this, "node_hair_bsdf");
2529 }
2530
2531 /* Geometry */
2532
2533 GeometryNode::GeometryNode()
2534 : ShaderNode("geometry")
2535 {
2536         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2537
2538         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2539         add_output("Position", SHADER_SOCKET_POINT);
2540         add_output("Normal", SHADER_SOCKET_NORMAL);
2541         add_output("Tangent", SHADER_SOCKET_NORMAL);
2542         add_output("True Normal", SHADER_SOCKET_NORMAL);
2543         add_output("Incoming", SHADER_SOCKET_VECTOR);
2544         add_output("Parametric", SHADER_SOCKET_POINT);
2545         add_output("Backfacing", SHADER_SOCKET_FLOAT);
2546         add_output("Pointiness", SHADER_SOCKET_FLOAT);
2547 }
2548
2549 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2550 {
2551         if(shader->has_surface) {
2552                 if(!output("Tangent")->links.empty()) {
2553                         attributes->add(ATTR_STD_GENERATED);
2554                 }
2555                 if(!output("Pointiness")->links.empty()) {
2556                         attributes->add(ATTR_STD_POINTINESS);
2557                 }
2558         }
2559
2560         ShaderNode::attributes(shader, attributes);
2561 }
2562
2563 void GeometryNode::compile(SVMCompiler& compiler)
2564 {
2565         ShaderOutput *out;
2566         NodeType geom_node = NODE_GEOMETRY;
2567         NodeType attr_node = NODE_ATTR;
2568
2569         if(bump == SHADER_BUMP_DX) {
2570                 geom_node = NODE_GEOMETRY_BUMP_DX;
2571                 attr_node = NODE_ATTR_BUMP_DX;
2572         }
2573         else if(bump == SHADER_BUMP_DY) {
2574                 geom_node = NODE_GEOMETRY_BUMP_DY;
2575                 attr_node = NODE_ATTR_BUMP_DY;
2576         }
2577         
2578         out = output("Position");
2579         if(!out->links.empty()) {
2580                 compiler.stack_assign(out);
2581                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2582         }
2583
2584         out = output("Normal");
2585         if(!out->links.empty()) {
2586                 compiler.stack_assign(out);
2587                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
2588         }
2589
2590         out = output("Tangent");
2591         if(!out->links.empty()) {
2592                 compiler.stack_assign(out);
2593                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
2594         }
2595
2596         out = output("True Normal");
2597         if(!out->links.empty()) {
2598                 compiler.stack_assign(out);
2599                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
2600         }
2601
2602         out = output("Incoming");
2603         if(!out->links.empty()) {
2604                 compiler.stack_assign(out);
2605                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2606         }
2607
2608         out = output("Parametric");
2609         if(!out->links.empty()) {
2610                 compiler.stack_assign(out);
2611                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
2612         }
2613
2614         out = output("Backfacing");
2615         if(!out->links.empty()) {
2616                 compiler.stack_assign(out);
2617                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
2618         }
2619
2620         out = output("Pointiness");
2621         if(!out->links.empty()) {
2622                 compiler.stack_assign(out);
2623                 if(compiler.output_type() != SHADER_TYPE_VOLUME) {
2624                         compiler.add_node(attr_node,
2625                                           ATTR_STD_POINTINESS,
2626                                           out->stack_offset,
2627                                           NODE_ATTR_FLOAT);
2628                 }
2629                 else {
2630                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), out->stack_offset);
2631                 }
2632         }
2633 }
2634
2635 void GeometryNode::compile(OSLCompiler& compiler)
2636 {
2637         if(bump == SHADER_BUMP_DX)
2638                 compiler.parameter("bump_offset", "dx");
2639         else if(bump == SHADER_BUMP_DY)
2640                 compiler.parameter("bump_offset", "dy");
2641         else
2642                 compiler.parameter("bump_offset", "center");
2643
2644         compiler.add(this, "node_geometry");
2645 }
2646
2647 /* TextureCoordinate */
2648
2649 TextureCoordinateNode::TextureCoordinateNode()
2650 : ShaderNode("texture_coordinate")
2651 {
2652         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2653         add_output("Generated", SHADER_SOCKET_POINT);
2654         add_output("Normal", SHADER_SOCKET_NORMAL);
2655         add_output("UV", SHADER_SOCKET_POINT);
2656         add_output("Object", SHADER_SOCKET_POINT);
2657         add_output("Camera", SHADER_SOCKET_POINT);
2658         add_output("Window", SHADER_SOCKET_POINT);
2659         add_output("Reflection", SHADER_SOCKET_NORMAL);
2660
2661         from_dupli = false;
2662         use_transform = false;
2663         ob_tfm = transform_identity();
2664 }
2665
2666 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2667 {
2668         if(shader->has_surface) {
2669                 if(!from_dupli) {
2670                         if(!output("Generated")->links.empty())
2671                                 attributes->add(ATTR_STD_GENERATED);
2672                         if(!output("UV")->links.empty())
2673                                 attributes->add(ATTR_STD_UV);
2674                 }
2675         }
2676
2677         if(shader->has_volume) {
2678                 if(!from_dupli) {
2679                         if(!output("Generated")->links.empty()) {
2680                                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
2681                         }
2682                 }
2683         }
2684
2685         ShaderNode::attributes(shader, attributes);
2686 }
2687
2688 void TextureCoordinateNode::compile(SVMCompiler& compiler)
2689 {
2690         ShaderOutput *out;
2691         NodeType texco_node = NODE_TEX_COORD;
2692         NodeType attr_node = NODE_ATTR;
2693         NodeType geom_node = NODE_GEOMETRY;
2694
2695         if(bump == SHADER_BUMP_DX) {
2696                 texco_node = NODE_TEX_COORD_BUMP_DX;
2697                 attr_node = NODE_ATTR_BUMP_DX;
2698                 geom_node = NODE_GEOMETRY_BUMP_DX;
2699         }
2700         else if(bump == SHADER_BUMP_DY) {
2701                 texco_node = NODE_TEX_COORD_BUMP_DY;
2702                 attr_node = NODE_ATTR_BUMP_DY;
2703                 geom_node = NODE_GEOMETRY_BUMP_DY;
2704         }
2705         
2706         out = output("Generated");
2707         if(!out->links.empty()) {
2708                 if(compiler.background) {
2709                         compiler.stack_assign(out);
2710                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2711                 }
2712                 else {
2713                         if(from_dupli) {
2714                                 compiler.stack_assign(out);
2715                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
2716                         }
2717                         else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
2718                                 compiler.stack_assign(out);
2719                                 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, out->stack_offset);
2720                         }
2721                         else {
2722                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
2723                                 compiler.stack_assign(out);
2724                                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2725                         }
2726                 }
2727         }
2728
2729         out = output("Normal");
2730         if(!out->links.empty()) {
2731                 compiler.stack_assign(out);
2732                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
2733         }
2734
2735         out = output("UV");
2736         if(!out->links.empty()) {
2737                 if(from_dupli) {
2738                         compiler.stack_assign(out);
2739                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
2740                 }
2741                 else {
2742                         int attr = compiler.attribute(ATTR_STD_UV);
2743                         compiler.stack_assign(out);
2744                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2745                 }
2746         }
2747
2748         out = output("Object");
2749         if(!out->links.empty()) {
2750                 compiler.stack_assign(out);
2751                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset, use_transform);
2752                 if(use_transform) {
2753                         Transform ob_itfm = transform_inverse(ob_tfm);
2754                         compiler.add_node(ob_itfm.x);
2755                         compiler.add_node(ob_itfm.y);
2756                         compiler.add_node(ob_itfm.z);
2757                         compiler.add_node(ob_itfm.w);
2758                 }
2759         }
2760
2761         out = output("Camera");
2762         if(!out->links.empty()) {
2763                 compiler.stack_assign(out);
2764                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
2765         }
2766
2767         out = output("Window");
2768         if(!out->links.empty()) {
2769                 compiler.stack_assign(out);
2770                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
2771         }
2772
2773         out = output("Reflection");
2774         if(!out->links.empty()) {
2775                 if(compiler.background) {
2776                         compiler.stack_assign(out);
2777                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2778                 }
2779                 else {
2780                         compiler.stack_assign(out);
2781                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
2782                 }
2783         }
2784 }
2785
2786 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2787 {
2788         if(bump == SHADER_BUMP_DX)
2789                 compiler.parameter("bump_offset", "dx");
2790         else if(bump == SHADER_BUMP_DY)
2791                 compiler.parameter("bump_offset", "dy");
2792         else
2793                 compiler.parameter("bump_offset", "center");
2794         
2795         if(compiler.background)
2796                 compiler.parameter("is_background", true);
2797         if(compiler.output_type() == SHADER_TYPE_VOLUME)
2798                 compiler.parameter("is_volume", true);
2799         compiler.parameter("use_transform", use_transform);
2800         Transform ob_itfm = transform_transpose(transform_inverse(ob_tfm));
2801         compiler.parameter("object_itfm", ob_itfm);
2802
2803         compiler.parameter("from_dupli", from_dupli);
2804
2805         compiler.add(this, "node_texture_coordinate");
2806 }
2807
2808 UVMapNode::UVMapNode()
2809 : ShaderNode("uvmap")
2810 {
2811         attribute = "";
2812         from_dupli = false;
2813
2814         add_output("UV", SHADER_SOCKET_POINT);
2815 }
2816
2817 void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2818 {
2819         if(shader->has_surface) {
2820                 if(!from_dupli) {
2821                         if(!output("UV")->links.empty()) {
2822                                 if(attribute != "")
2823                                         attributes->add(attribute);
2824                                 else
2825                                         attributes->add(ATTR_STD_UV);
2826                         }
2827                 }
2828         }
2829
2830         ShaderNode::attributes(shader, attributes);
2831 }
2832
2833 void UVMapNode::compile(SVMCompiler& compiler)
2834 {
2835         ShaderOutput *out = output("UV");
2836         NodeType texco_node = NODE_TEX_COORD;
2837         NodeType attr_node = NODE_ATTR;
2838         int attr;
2839
2840         if(bump == SHADER_BUMP_DX) {
2841                 texco_node = NODE_TEX_COORD_BUMP_DX;
2842                 attr_node = NODE_ATTR_BUMP_DX;
2843         }
2844         else if(bump == SHADER_BUMP_DY) {
2845                 texco_node = NODE_TEX_COORD_BUMP_DY;
2846                 attr_node = NODE_ATTR_BUMP_DY;
2847         }
2848
2849         if(!out->links.empty()) {
2850                 if(from_dupli) {
2851                         compiler.stack_assign(out);
2852                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
2853                 }
2854                 else {
2855                         if(attribute != "")
2856                                 attr = compiler.attribute(attribute);
2857                         else
2858                                 attr = compiler.attribute(ATTR_STD_UV);
2859
2860                         compiler.stack_assign(out);
2861                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2862                 }
2863         }
2864 }
2865
2866 void UVMapNode::compile(OSLCompiler& compiler)
2867 {
2868         if(bump == SHADER_BUMP_DX)
2869                 compiler.parameter("bump_offset", "dx");
2870         else if(bump == SHADER_BUMP_DY)
2871                 compiler.parameter("bump_offset", "dy");
2872         else
2873                 compiler.parameter("bump_offset", "center");
2874
2875         compiler.parameter("from_dupli", from_dupli);
2876         compiler.parameter("name", attribute.c_str());
2877         compiler.add(this, "node_uv_map");
2878 }
2879
2880 /* Light Path */
2881
2882 LightPathNode::LightPathNode()
2883 : ShaderNode("light_path")
2884 {
2885         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
2886         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
2887         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
2888         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
2889         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
2890         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
2891         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
2892         add_output("Is Volume Scatter Ray", SHADER_SOCKET_FLOAT);
2893         add_output("Ray Length", SHADER_SOCKET_FLOAT);
2894         add_output("Ray Depth", SHADER_SOCKET_FLOAT);
2895         add_output("Transparent Depth", SHADER_SOCKET_FLOAT);
2896         add_output("Transmission Depth", SHADER_SOCKET_FLOAT);
2897 }
2898
2899 void LightPathNode::compile(SVMCompiler& compiler)
2900 {
2901         ShaderOutput *out;
2902
2903         out = output("Is Camera Ray");
2904         if(!out->links.empty()) {
2905                 compiler.stack_assign(out);
2906                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2907         }
2908
2909         out = output("Is Shadow Ray");
2910         if(!out->links.empty()) {
2911                 compiler.stack_assign(out);
2912                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2913         }
2914
2915         out = output("Is Diffuse Ray");
2916         if(!out->links.empty()) {
2917                 compiler.stack_assign(out);
2918                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2919         }
2920
2921         out = output("Is Glossy Ray");
2922         if(!out->links.empty()) {
2923                 compiler.stack_assign(out);
2924                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2925         }
2926
2927         out = output("Is Singular Ray");
2928         if(!out->links.empty()) {
2929                 compiler.stack_assign(out);
2930                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2931         }
2932
2933         out = output("Is Reflection Ray");
2934         if(!out->links.empty()) {
2935                 compiler.stack_assign(out);
2936                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2937         }
2938
2939
2940         out = output("Is Transmission Ray");
2941         if(!out->links.empty()) {
2942                 compiler.stack_assign(out);
2943                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2944         }
2945         
2946         out = output("Is Volume Scatter Ray");
2947         if(!out->links.empty()) {
2948                 compiler.stack_assign(out);
2949                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, out->stack_offset);
2950         }
2951
2952         out = output("Ray Length");
2953         if(!out->links.empty()) {
2954                 compiler.stack_assign(out);
2955                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2956         }
2957         
2958         out = output("Ray Depth");
2959         if(!out->links.empty()) {
2960                 compiler.stack_assign(out);
2961                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, out->stack_offset);
2962         }
2963
2964         out = output("Transparent Depth");
2965         if(!out->links.empty()) {
2966                 compiler.stack_assign(out);
2967                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, out->stack_offset);
2968         }
2969
2970         out = output("Transmission Depth");
2971         if(!out->links.empty()) {
2972                 compiler.stack_assign(out);
2973                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transmission, out->stack_offset);
2974         }
2975 }
2976
2977 void LightPathNode::compile(OSLCompiler& compiler)
2978 {
2979         compiler.add(this, "node_light_path");
2980 }
2981
2982 /* Light Falloff */
2983
2984 LightFalloffNode::LightFalloffNode()
2985 : ShaderNode("light_fallof")
2986 {
2987         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2988         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2989         add_output("Quadratic", SHADER_SOCKET_FLOAT);
2990         add_output("Linear", SHADER_SOCKET_FLOAT);
2991         add_output("Constant", SHADER_SOCKET_FLOAT);
2992 }
2993
2994 void LightFalloffNode::compile(SVMCompiler& compiler)
2995 {
2996         ShaderInput *strength_in = input("Strength");
2997         ShaderInput *smooth_in = input("Smooth");
2998
2999         compiler.stack_assign(strength_in);
3000         compiler.stack_assign(smooth_in);
3001
3002         ShaderOutput *out = output("Quadratic");
3003         if(!out->links.empty()) {
3004                 compiler.stack_assign(out);
3005                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
3006                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
3007         }
3008
3009         out = output("Linear");
3010         if(!out->links.empty()) {
3011                 compiler.stack_assign(out);
3012                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
3013                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
3014         }
3015
3016         out = output("Constant");
3017         if(!out->links.empty()) {
3018                 compiler.stack_assign(out);
3019                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
3020                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
3021         }
3022 }
3023
3024 void LightFalloffNode::compile(OSLCompiler& compiler)
3025 {
3026         compiler.add(this, "node_light_falloff");
3027 }
3028
3029 /* Object Info */
3030
3031 ObjectInfoNode::ObjectInfoNode()
3032 : ShaderNode("object_info")
3033 {
3034         add_output("Location", SHADER_SOCKET_VECTOR);
3035         add_output("Object Index", SHADER_SOCKET_FLOAT);
3036         add_output("Material Index", SHADER_SOCKET_FLOAT);
3037         add_output("Random", SHADER_SOCKET_FLOAT);
3038 }
3039
3040 void ObjectInfoNode::compile(SVMCompiler& compiler)
3041 {
3042         ShaderOutput *out = output("Location");
3043         if(!out->links.empty()) {
3044                 compiler.stack_assign(out);
3045                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
3046         }
3047
3048         out = output("Object Index");
3049         if(!out->links.empty()) {
3050                 compiler.stack_assign(out);
3051                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
3052         }
3053
3054         out = output("Material Index");
3055         if(!out->links.empty()) {
3056                 compiler.stack_assign(out);
3057                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
3058         }
3059
3060         out = output("Random");
3061         if(!out->links.empty()) {
3062                 compiler.stack_assign(out);
3063                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
3064         }
3065 }
3066
3067 void ObjectInfoNode::compile(OSLCompiler& compiler)
3068 {
3069         compiler.add(this, "node_object_info");
3070 }
3071
3072 /* Particle Info */
3073
3074 ParticleInfoNode::ParticleInfoNode()
3075 : ShaderNode("particle_info")
3076 {
3077         add_output("Index", SHADER_SOCKET_FLOAT);
3078         add_output("Age", SHADER_SOCKET_FLOAT);
3079         add_output("Lifetime", SHADER_SOCKET_FLOAT);
3080         add_output("Location", SHADER_SOCKET_POINT);
3081 #if 0   /* not yet supported */
3082         add_output("Rotation", SHADER_SOCKET_QUATERNION);
3083 #endif
3084         add_output("Size", SHADER_SOCKET_FLOAT);
3085         add_output("Velocity", SHADER_SOCKET_VECTOR);
3086         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
3087 }
3088
3089 void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3090 {
3091         if(!output("Index")->links.empty())
3092                 attributes->add(ATTR_STD_PARTICLE);
3093         if(!output("Age")->links.empty())
3094                 attributes->add(ATTR_STD_PARTICLE);
3095         if(!output("Lifetime")->links.empty())
3096                 attributes->add(ATTR_STD_PARTICLE);
3097         if(!output("Location")->links.empty())
3098                 attributes->add(ATTR_STD_PARTICLE);
3099 #if 0   /* not yet supported */
3100         if(!output("Rotation")->links.empty())
3101                 attributes->add(ATTR_STD_PARTICLE);
3102 #endif
3103         if(!output("Size")->links.empty())
3104                 attributes->add(ATTR_STD_PARTICLE);
3105         if(!output("Velocity")->links.empty())
3106                 attributes->add(ATTR_STD_PARTICLE);
3107         if(!output("Angular Velocity")->links.empty())
3108                 attributes->add(ATTR_STD_PARTICLE);
3109
3110         ShaderNode::attributes(shader, attributes);
3111 }
3112
3113 void ParticleInfoNode::compile(SVMCompiler& compiler)
3114 {
3115         ShaderOutput *out;
3116         
3117         out = output("Index");
3118         if(!out->links.empty()) {
3119                 compiler.stack_assign(out);
3120                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
3121         }
3122         
3123         out = output("Age");
3124         if(!out->links.empty()) {
3125                 compiler.stack_assign(out);
3126                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
3127         }
3128         
3129         out = output("Lifetime");
3130         if(!out->links.empty()) {
3131                 compiler.stack_assign(out);
3132                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
3133         }
3134         
3135         out = output("Location");
3136         if(!out->links.empty()) {
3137                 compiler.stack_assign(out);
3138                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
3139         }
3140         
3141         /* quaternion data is not yet supported by Cycles */
3142 #if 0
3143         out = output("Rotation");
3144         if(!out->links.empty()) {
3145                 compiler.stack_assign(out);
3146                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
3147         }
3148 #endif
3149         
3150         out = output("Size");
3151         if(!out->links.empty()) {
3152                 compiler.stack_assign(out);
3153                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
3154         }
3155         
3156         out = output("Velocity");
3157         if(!out->links.empty()) {
3158                 compiler.stack_assign(out);
3159                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
3160         }
3161         
3162         out = output("Angular Velocity");
3163         if(!out->links.empty()) {
3164                 compiler.stack_assign(out);
3165                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
3166         }
3167 }
3168
3169 void ParticleInfoNode::compile(OSLCompiler& compiler)
3170 {
3171         compiler.add(this, "node_particle_info");
3172 }
3173
3174 /* Hair Info */
3175
3176 HairInfoNode::HairInfoNode()
3177 : ShaderNode("hair_info")
3178 {
3179         add_output("Is Strand", SHADER_SOCKET_FLOAT);
3180         add_output("Intercept", SHADER_SOCKET_FLOAT);
3181         add_output("Thickness", SHADER_SOCKET_FLOAT);
3182         add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
3183         /*output for minimum hair width transparency - deactivated*/
3184         /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
3185 }
3186
3187 void HairInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3188 {
3189         if(shader->has_surface) {
3190                 ShaderOutput *intercept_out = output("Intercept");
3191
3192                 if(!intercept_out->links.empty())
3193                         attributes->add(ATTR_STD_CURVE_INTERCEPT);
3194         }
3195
3196         ShaderNode::attributes(shader, attributes);
3197 }
3198
3199 void HairInfoNode::compile(SVMCompiler& compiler)
3200 {
3201         ShaderOutput *out;
3202         
3203         out = output("Is Strand");
3204         if(!out->links.empty()) {
3205                 compiler.stack_assign(out);
3206                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
3207         }
3208
3209         out = output("Intercept");
3210         if(!out->links.empty()) {
3211                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
3212                 compiler.stack_assign(out);
3213                 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
3214         }
3215
3216         out = output("Thickness");
3217         if(!out->links.empty()) {
3218                 compiler.stack_assign(out);
3219                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
3220         }
3221
3222         out = output("Tangent Normal");
3223         if(!out->links.empty()) {
3224                 compiler.stack_assign(out);
3225                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
3226         }
3227
3228         /*out = output("Fade");
3229         if(!out->links.empty()) {
3230                 compiler.stack_assign(out);
3231                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
3232         }*/
3233
3234 }
3235
3236 void HairInfoNode::compile(OSLCompiler& compiler)
3237 {
3238         compiler.add(this, "node_hair_info");
3239 }
3240
3241 /* Value */
3242
3243 ValueNode::ValueNode()
3244 : ShaderNode("value")
3245 {
3246         value = 0.0f;
3247
3248         add_output("Value", SHADER_SOCKET_FLOAT);
3249 }
3250
3251 bool ValueNode::constant_fold(ShaderOutput * /*socket*/,
3252                               float3 *optimized_value)
3253 {
3254         *optimized_value = make_float3(value, value, value);
3255         return true;
3256 }
3257
3258 void ValueNode::compile(SVMCompiler& compiler)
3259 {
3260         ShaderOutput *val_out = output("Value");
3261
3262         compiler.stack_assign(val_out);
3263         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
3264 }
3265
3266 void ValueNode::compile(OSLCompiler& compiler)
3267 {
3268         compiler.parameter("value_value", value);
3269         compiler.add(this, "node_value");
3270 }
3271
3272 /* Color */
3273
3274 ColorNode::ColorNode()
3275 : ShaderNode("color")
3276 {
3277         value = make_float3(0.0f, 0.0f, 0.0f);
3278
3279         add_output("Color", SHADER_SOCKET_COLOR);
3280 }
3281
3282 bool ColorNode::constant_fold(ShaderOutput * /*socket*/,
3283                               float3 *optimized_value)
3284 {
3285         *optimized_value = value;
3286         return true;
3287 }
3288
3289 void ColorNode::compile(SVMCompiler& compiler)
3290 {
3291         ShaderOutput *color_out = output("Color");
3292
3293         if(color_out && !color_out->links.empty()) {
3294                 compiler.stack_assign(color_out);
3295                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
3296                 compiler.add_node(NODE_VALUE_V, value);
3297         }
3298 }
3299
3300 void ColorNode::compile(OSLCompiler& compiler)
3301 {
3302         compiler.parameter_color("color_value", value);
3303
3304         compiler.add(this, "node_value");
3305 }
3306
3307 /* Add Closure */
3308
3309 AddClosureNode::AddClosureNode()
3310 : ShaderNode("add_closure")
3311 {
3312         add_input("Closure1", SHADER_SOCKET_CLOSURE);
3313         add_input("Closure2", SHADER_SOCKET_CLOSURE);
3314         add_output("Closure",  SHADER_SOCKET_CLOSURE);
3315 }
3316
3317 void AddClosureNode::compile(SVMCompiler& /*compiler*/)
3318 {
3319         /* handled in the SVM compiler */
3320 }
3321
3322 void AddClosureNode::compile(OSLCompiler& compiler)
3323 {
3324         compiler.add(this, "node_add_closure");
3325 }
3326
3327 /* Mix Closure */
3328
3329 MixClosureNode::MixClosureNode()
3330 : ShaderNode("mix_closure")
3331 {
3332         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
3333         
3334         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
3335         add_input("Closure1", SHADER_SOCKET_CLOSURE);
3336         add_input("Closure2", SHADER_SOCKET_CLOSURE);
3337         add_output("Closure",  SHADER_SOCKET_CLOSURE);
3338 }
3339
3340 void MixClosureNode::compile(SVMCompiler& /*compiler*/)
3341 {
3342         /* handled in the SVM compiler */
3343 }
3344
3345 void MixClosureNode::compile(OSLCompiler& compiler)
3346 {
3347         compiler.add(this, "node_mix_closure");
3348 }
3349
3350 /* Mix Closure */
3351
3352 MixClosureWeightNode::MixClosureWeightNode()
3353 : ShaderNode("mix_closure_weight")
3354 {
3355         add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
3356         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
3357         add_output("Weight1", SHADER_SOCKET_FLOAT);
3358         add_output("Weight2", SHADER_SOCKET_FLOAT);
3359 }
3360
3361 void MixClosureWeightNode::compile(SVMCompiler& compiler)
3362 {
3363         ShaderInput *weight_in = input("Weight");
3364         ShaderInput *fac_in = input("Fac");
3365         ShaderOutput *weight1_out = output("Weight1");
3366         ShaderOutput *weight2_out = output("Weight2");
3367
3368         compiler.stack_assign(weight_in);
3369         compiler.stack_assign(fac_in);
3370         compiler.stack_assign(weight1_out);
3371         compiler.stack_assign(weight2_out);
3372
3373         compiler.add_node(NODE_MIX_CLOSURE,
3374                 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
3375                         weight1_out->stack_offset, weight2_out->stack_offset));
3376 }
3377
3378 void MixClosureWeightNode::compile(OSLCompiler& /*compiler*/)
3379 {
3380         assert(0);
3381 }
3382
3383 /* Invert */
3384
3385 InvertNode::InvertNode()
3386 : ShaderNode("invert")
3387 {
3388         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
3389         add_input("Color", SHADER_SOCKET_COLOR);
3390         add_output("Color",  SHADER_SOCKET_COLOR);
3391 }
3392
3393 void InvertNode::compile(SVMCompiler& compiler)
3394 {
3395         ShaderInput *fac_in = input("Fac");
3396         ShaderInput *color_in = input("Color");
3397         ShaderOutput *color_out = output("Color");
3398
3399         compiler.stack_assign(fac_in);
3400         compiler.stack_assign(color_in);
3401         compiler.stack_assign(color_out);
3402
3403         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3404 }
3405
3406 void InvertNode::compile(OSLCompiler& compiler)
3407 {
3408         compiler.add(this, "node_invert");
3409 }
3410
3411 /* Mix */
3412
3413 MixNode::MixNode()
3414 : ShaderNode("mix")
3415 {
3416         type = ustring("Mix");
3417
3418         use_clamp = false;
3419
3420         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
3421         add_input("Color1", SHADER_SOCKET_COLOR);
3422         add_input("Color2", SHADER_SOCKET_COLOR);
3423         add_output("Color",  SHADER_SOCKET_COLOR);
3424 }
3425
3426 static ShaderEnum mix_type_init()
3427 {
3428         ShaderEnum enm;
3429
3430         enm.insert("Mix", NODE_MIX_BLEND);
3431         enm.insert("Add", NODE_MIX_ADD);
3432         enm.insert("Multiply", NODE_MIX_MUL);
3433         enm.insert("Screen", NODE_MIX_SCREEN);
3434         enm.insert("Overlay", NODE_MIX_OVERLAY);
3435         enm.insert("Subtract", NODE_MIX_SUB);
3436         enm.insert("Divide", NODE_MIX_DIV);
3437         enm.insert("Difference", NODE_MIX_DIFF);
3438         enm.insert("Darken", NODE_MIX_DARK);
3439         enm.insert("Lighten", NODE_MIX_LIGHT);
3440         enm.insert("Dodge", NODE_MIX_DODGE);
3441         enm.insert("Burn", NODE_MIX_BURN);
3442         enm.insert("Hue", NODE_MIX_HUE);
3443         enm.insert("Saturation", NODE_MIX_SAT);
3444         enm.insert("Value", NODE_MIX_VAL);
3445         enm.insert("Color", NODE_MIX_COLOR);
3446         enm.insert("Soft Light", NODE_MIX_SOFT);
3447         enm.insert("Linear Light", NODE_MIX_LINEAR);
3448
3449         return enm;
3450 }
3451
3452 ShaderEnum MixNode::type_enum = mix_type_init();
3453
3454 void MixNode::compile(SVMCompiler& compiler)
3455 {
3456         ShaderInput *fac_in = input("Fac");
3457         ShaderInput *color1_in = input("Color1");
3458         ShaderInput *color2_in = input("Color2");
3459         ShaderOutput *color_out = output("Color");
3460
3461         compiler.stack_assign(fac_in);
3462         compiler.stack_assign(color1_in);
3463         compiler.stack_assign(color2_in);
3464         compiler.stack_assign(color_out);
3465
3466         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
3467         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
3468
3469         if(use_clamp) {
3470                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
3471                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
3472         }
3473 }
3474
3475 void MixNode::compile(OSLCompiler& compiler)
3476 {
3477         compiler.parameter("type", type);
3478         compiler.parameter("Clamp", use_clamp);
3479         compiler.add(this, "node_mix");
3480 }
3481
3482 /* Combine RGB */
3483 CombineRGBNode::CombineRGBNode()
3484 : ShaderNode("combine_rgb")
3485 {
3486         add_input("R", SHADER_SOCKET_FLOAT);
3487         add_input("G", SHADER_SOCKET_FLOAT);
3488         add_input("B", SHADER_SOCKET_FLOAT);
3489         add_output("Image", SHADER_SOCKET_COLOR);
3490 }
3491
3492 void CombineRGBNode::compile(SVMCompiler& compiler)
3493 {
3494         ShaderInput *red_in = input("R");
3495         ShaderInput *green_in = input("G");
3496         ShaderInput *blue_in = input("B");
3497         ShaderOutput *color_out = output("Image");
3498
3499         compiler.stack_assign(color_out);
3500
3501         compiler.stack_assign(red_in);
3502         compiler.add_node(NODE_COMBINE_VECTOR, red_in->stack_offset, 0, color_out->stack_offset);
3503
3504         compiler.stack_assign(green_in);
3505         compiler.add_node(NODE_COMBINE_VECTOR, green_in->stack_offset, 1, color_out->stack_offset);
3506
3507         compiler.stack_assign(blue_in);
3508         compiler.add_node(NODE_COMBINE_VECTOR, blue_in->stack_offset, 2, color_out->stack_offset);
3509 }
3510
3511 void CombineRGBNode::compile(OSLCompiler& compiler)
3512 {
3513         compiler.add(this, "node_combine_rgb");
3514 }
3515
3516 /* Combine XYZ */
3517 CombineXYZNode::CombineXYZNode()
3518 : ShaderNode("combine_xyz")
3519 {
3520         add_input("X", SHADER_SOCKET_FLOAT);
3521         add_input("Y", SHADER_SOCKET_FLOAT);
3522         add_input("Z", SHADER_SOCKET_FLOAT);
3523         add_output("Vector", SHADER_SOCKET_VECTOR);
3524 }
3525
3526 void CombineXYZNode::compile(SVMCompiler& compiler)
3527 {
3528         ShaderInput *x_in = input("X");
3529         ShaderInput *y_in = input("Y");
3530         ShaderInput *z_in = input("Z");
3531         ShaderOutput *vector_out = output("Vector");
3532
3533         compiler.stack_assign(vector_out);
3534
3535         compiler.stack_assign(x_in);
3536         compiler.add_node(NODE_COMBINE_VECTOR, x_in->stack_offset, 0, vector_out->stack_offset);
3537
3538         compiler.stack_assign(y_in);
3539         compiler.add_node(NODE_COMBINE_VECTOR, y_in->stack_offset, 1, vector_out->stack_offset);
3540
3541         compiler.stack_assign(z_in);
3542         compiler.add_node(NODE_COMBINE_VECTOR, z_in->stack_offset, 2, vector_out->stack_offset);
3543 }
3544
3545 void CombineXYZNode::compile(OSLCompiler& compiler)
3546 {
3547         compiler.add(this, "node_combine_xyz");
3548 }
3549
3550 /* Combine HSV */
3551 CombineHSVNode::CombineHSVNode()
3552 : ShaderNode("combine_hsv")
3553 {
3554         add_input("H", SHADER_SOCKET_FLOAT);
3555         add_input("S", SHADER_SOCKET_FLOAT);
3556         add_input("V", SHADER_SOCKET_FLOAT);
3557         add_output("Color", SHADER_SOCKET_COLOR);
3558 }
3559
3560 void CombineHSVNode::compile(SVMCompiler& compiler)
3561 {
3562         ShaderInput *hue_in = input("H");
3563         ShaderInput *saturation_in = input("S");
3564         ShaderInput *value_in = input("V");
3565         ShaderOutput *color_out = output("Color");
3566
3567         compiler.stack_assign(color_out);
3568         compiler.stack_assign(hue_in);
3569         compiler.stack_assign(saturation_in);
3570         compiler.stack_assign(value_in);
3571         
3572         compiler.add_node(NODE_COMBINE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3573         compiler.add_node(NODE_COMBINE_HSV, color_out->stack_offset);
3574 }
3575
3576 void CombineHSVNode::compile(OSLCompiler& compiler)
3577 {
3578         compiler.add(this, "node_combine_hsv");
3579 }
3580
3581 /* Gamma */
3582 GammaNode::GammaNode()
3583 : ShaderNode("gamma")
3584 {
3585         add_input("Color", SHADER_SOCKET_COLOR);
3586         add_input("Gamma", SHADER_SOCKET_FLOAT);
3587         add_output("Color", SHADER_SOCKET_COLOR);
3588 }
3589
3590 bool GammaNode::constant_fold(ShaderOutput *socket, float3 *optimized_value)
3591 {
3592         ShaderInput *color_in = input("Color");
3593         ShaderInput *gamma_in = input("Gamma");
3594
3595         if(socket == output("Color")) {
3596                 if(color_in->link == NULL && gamma_in->link == NULL) {
3597                         *optimized_value = svm_math_gamma_color(color_in->value,
3598                                                                 gamma_in->value.x);
3599
3600                         return true;
3601                 }
3602         }
3603
3604         return false;
3605 }
3606
3607 void GammaNode::compile(SVMCompiler& compiler)
3608 {
3609         ShaderInput *color_in = input("Color");
3610         ShaderInput *gamma_in = input("Gamma");
3611         ShaderOutput *color_out = output("Color");
3612
3613         compiler.stack_assign(color_in);
3614         compiler.stack_assign(gamma_in);
3615         compiler.stack_assign(color_out);
3616
3617         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3618 }
3619
3620 void GammaNode::compile(OSLCompiler& compiler)
3621 {
3622         compiler.add(this, "node_gamma");
3623 }
3624
3625 /* Bright Contrast */