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