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