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