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