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