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