Cycles: glossy and anisotropic BSDF changes
[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("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1593         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID);
1594         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
1595         enm.insert("Ashikhmin-Shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID);
1596
1597         return enm;
1598 }
1599
1600 ShaderEnum AnisotropicBsdfNode::distribution_enum = aniso_distribution_init();
1601
1602 AnisotropicBsdfNode::AnisotropicBsdfNode()
1603 {
1604         distribution = ustring("GGX");
1605
1606         add_input("Tangent", SHADER_SOCKET_VECTOR, ShaderInput::TANGENT);
1607
1608         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1609         add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.5f);
1610         add_input("Rotation", SHADER_SOCKET_FLOAT, 0.0f);
1611 }
1612
1613 void AnisotropicBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
1614 {
1615         if(shader->has_surface) {
1616                 ShaderInput *tangent_in = input("Tangent");
1617
1618                 if(!tangent_in->link)
1619                         attributes->add(ATTR_STD_GENERATED);
1620         }
1621
1622         ShaderNode::attributes(shader, attributes);
1623 }
1624
1625 void AnisotropicBsdfNode::compile(SVMCompiler& compiler)
1626 {
1627         closure = (ClosureType)distribution_enum[distribution];
1628
1629         BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1630 }
1631
1632 void AnisotropicBsdfNode::compile(OSLCompiler& compiler)
1633 {
1634         compiler.parameter("distribution", distribution);
1635         compiler.add(this, "node_anisotropic_bsdf");
1636 }
1637
1638 /* Glossy BSDF Closure */
1639
1640 static ShaderEnum glossy_distribution_init()
1641 {
1642         ShaderEnum enm;
1643
1644         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1645         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1646         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1647         enm.insert("Ashikhmin-Shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
1648
1649         return enm;
1650 }
1651
1652 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1653
1654 GlossyBsdfNode::GlossyBsdfNode()
1655 {
1656         distribution = ustring("GGX");
1657
1658         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1659 }
1660
1661 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1662 {
1663         closure = (ClosureType)distribution_enum[distribution];
1664
1665         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1666                 BsdfNode::compile(compiler, NULL, NULL);
1667         else
1668                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1669 }
1670
1671 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1672 {
1673         compiler.parameter("distribution", distribution);
1674         compiler.add(this, "node_glossy_bsdf");
1675 }
1676
1677 /* Glass BSDF Closure */
1678
1679 static ShaderEnum glass_distribution_init()
1680 {
1681         ShaderEnum enm;
1682
1683         enm.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1684         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1685         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1686
1687         return enm;
1688 }
1689
1690 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1691
1692 GlassBsdfNode::GlassBsdfNode()
1693 {
1694         distribution = ustring("Sharp");
1695
1696         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1697         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1698 }
1699
1700 void GlassBsdfNode::compile(SVMCompiler& compiler)
1701 {
1702         closure = (ClosureType)distribution_enum[distribution];
1703
1704         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
1705                 BsdfNode::compile(compiler, NULL, input("IOR"));
1706         else
1707                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1708 }
1709
1710 void GlassBsdfNode::compile(OSLCompiler& compiler)
1711 {
1712         compiler.parameter("distribution", distribution);
1713         compiler.add(this, "node_glass_bsdf");
1714 }
1715
1716 /* Refraction BSDF Closure */
1717
1718 static ShaderEnum refraction_distribution_init()
1719 {
1720         ShaderEnum enm;
1721
1722         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1723         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1724         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1725
1726         return enm;
1727 }
1728
1729 ShaderEnum RefractionBsdfNode::distribution_enum = refraction_distribution_init();
1730
1731 RefractionBsdfNode::RefractionBsdfNode()
1732 {
1733         distribution = ustring("Sharp");
1734
1735         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1736         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1737 }
1738
1739 void RefractionBsdfNode::compile(SVMCompiler& compiler)
1740 {
1741         closure = (ClosureType)distribution_enum[distribution];
1742
1743         if(closure == CLOSURE_BSDF_REFRACTION_ID)
1744                 BsdfNode::compile(compiler, NULL, input("IOR"));
1745         else
1746                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1747 }
1748
1749 void RefractionBsdfNode::compile(OSLCompiler& compiler)
1750 {
1751         compiler.parameter("distribution", distribution);
1752         compiler.add(this, "node_refraction_bsdf");
1753 }
1754
1755 /* Toon BSDF Closure */
1756
1757 static ShaderEnum toon_component_init()
1758 {
1759         ShaderEnum enm;
1760
1761         enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
1762         enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
1763
1764         return enm;
1765 }
1766
1767 ShaderEnum ToonBsdfNode::component_enum = toon_component_init();
1768
1769 ToonBsdfNode::ToonBsdfNode()
1770 {
1771         component = ustring("Diffuse");
1772
1773         add_input("Size", SHADER_SOCKET_FLOAT, 0.5f);
1774         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1775 }
1776
1777 void ToonBsdfNode::compile(SVMCompiler& compiler)
1778 {
1779         closure = (ClosureType)component_enum[component];
1780         
1781         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
1782 }
1783
1784 void ToonBsdfNode::compile(OSLCompiler& compiler)
1785 {
1786         compiler.parameter("component", component);
1787         compiler.add(this, "node_toon_bsdf");
1788 }
1789
1790 /* Velvet BSDF Closure */
1791
1792 VelvetBsdfNode::VelvetBsdfNode()
1793 {
1794         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1795
1796         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1797 }
1798
1799 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1800 {
1801         BsdfNode::compile(compiler, input("Sigma"), NULL);
1802 }
1803
1804 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1805 {
1806         compiler.add(this, "node_velvet_bsdf");
1807 }
1808
1809 /* Diffuse BSDF Closure */
1810
1811 DiffuseBsdfNode::DiffuseBsdfNode()
1812 {
1813         closure = CLOSURE_BSDF_DIFFUSE_ID;
1814         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1815 }
1816
1817 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1818 {
1819         BsdfNode::compile(compiler, input("Roughness"), NULL);
1820 }
1821
1822 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1823 {
1824         compiler.add(this, "node_diffuse_bsdf");
1825 }
1826
1827 /* Translucent BSDF Closure */
1828
1829 TranslucentBsdfNode::TranslucentBsdfNode()
1830 {
1831         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1832 }
1833
1834 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1835 {
1836         BsdfNode::compile(compiler, NULL, NULL);
1837 }
1838
1839 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1840 {
1841         compiler.add(this, "node_translucent_bsdf");
1842 }
1843
1844 /* Transparent BSDF Closure */
1845
1846 TransparentBsdfNode::TransparentBsdfNode()
1847 {
1848         name = "transparent";
1849         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1850 }
1851
1852 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1853 {
1854         BsdfNode::compile(compiler, NULL, NULL);
1855 }
1856
1857 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1858 {
1859         compiler.add(this, "node_transparent_bsdf");
1860 }
1861
1862 /* Subsurface Scattering Closure */
1863
1864 static ShaderEnum subsurface_falloff_init()
1865 {
1866         ShaderEnum enm;
1867
1868         enm.insert("Cubic", CLOSURE_BSSRDF_CUBIC_ID);
1869         enm.insert("Gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
1870
1871         return enm;
1872 }
1873
1874 ShaderEnum SubsurfaceScatteringNode::falloff_enum = subsurface_falloff_init();
1875
1876 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
1877 : BsdfNode(true)
1878 {
1879         name = "subsurface_scattering";
1880         closure = CLOSURE_BSSRDF_CUBIC_ID;
1881
1882         add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
1883         add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
1884         add_input("Sharpness", SHADER_SOCKET_FLOAT, 0.0f);
1885         add_input("Texture Blur", SHADER_SOCKET_FLOAT, 1.0f);
1886 }
1887
1888 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
1889 {
1890         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
1891 }
1892
1893 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
1894 {
1895         compiler.parameter("Falloff", falloff_enum[closure]);
1896         compiler.add(this, "node_subsurface_scattering");
1897 }
1898
1899 bool SubsurfaceScatteringNode::has_bssrdf_bump()
1900 {
1901         /* detect if anything is plugged into the normal input besides the default */
1902         ShaderInput *normal_in = input("Normal");
1903         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
1904 }
1905
1906 /* Emissive Closure */
1907
1908 EmissionNode::EmissionNode()
1909 : ShaderNode("emission")
1910 {
1911         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1912         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1913         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1914
1915         add_output("Emission", SHADER_SOCKET_CLOSURE);
1916 }
1917
1918 void EmissionNode::compile(SVMCompiler& compiler)
1919 {
1920         ShaderInput *color_in = input("Color");
1921         ShaderInput *strength_in = input("Strength");
1922
1923         if(color_in->link || strength_in->link) {
1924                 compiler.stack_assign(color_in);
1925                 compiler.stack_assign(strength_in);
1926                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1927         }
1928         else
1929                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1930
1931         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1932 }
1933
1934 void EmissionNode::compile(OSLCompiler& compiler)
1935 {
1936         compiler.add(this, "node_emission");
1937 }
1938
1939 /* Background Closure */
1940
1941 BackgroundNode::BackgroundNode()
1942 : ShaderNode("background")
1943 {
1944         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1945         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1946         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1947
1948         add_output("Background", SHADER_SOCKET_CLOSURE);
1949 }
1950
1951 void BackgroundNode::compile(SVMCompiler& compiler)
1952 {
1953         ShaderInput *color_in = input("Color");
1954         ShaderInput *strength_in = input("Strength");
1955
1956         if(color_in->link || strength_in->link) {
1957                 compiler.stack_assign(color_in);
1958                 compiler.stack_assign(strength_in);
1959                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1960         }
1961         else
1962                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1963
1964         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1965 }
1966
1967 void BackgroundNode::compile(OSLCompiler& compiler)
1968 {
1969         compiler.add(this, "node_background");
1970 }
1971
1972 /* Holdout Closure */
1973
1974 HoldoutNode::HoldoutNode()
1975 : ShaderNode("holdout")
1976 {
1977         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1978         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1979
1980         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1981 }
1982
1983 void HoldoutNode::compile(SVMCompiler& compiler)
1984 {
1985         float3 value = make_float3(1.0f, 1.0f, 1.0f);
1986
1987         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
1988         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1989 }
1990
1991 void HoldoutNode::compile(OSLCompiler& compiler)
1992 {
1993         compiler.add(this, "node_holdout");
1994 }
1995
1996 /* Ambient Occlusion */
1997
1998 AmbientOcclusionNode::AmbientOcclusionNode()
1999 : ShaderNode("ambient_occlusion")
2000 {
2001         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2002         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
2003         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
2004
2005         add_output("AO", SHADER_SOCKET_CLOSURE);
2006 }
2007
2008 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
2009 {
2010         ShaderInput *color_in = input("Color");
2011
2012         if(color_in->link) {
2013                 compiler.stack_assign(color_in);
2014                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
2015         }
2016         else
2017                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
2018
2019         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
2020 }
2021
2022 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
2023 {
2024         compiler.add(this, "node_ambient_occlusion");
2025 }
2026
2027 /* Volume Closure */
2028
2029 VolumeNode::VolumeNode()
2030 : ShaderNode("volume")
2031 {
2032         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2033
2034         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
2035         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
2036         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
2037
2038         add_output("Volume", SHADER_SOCKET_CLOSURE);
2039 }
2040
2041 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
2042 {
2043         ShaderInput *color_in = input("Color");
2044
2045         if(color_in->link) {
2046                 compiler.stack_assign(color_in);
2047                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
2048         }
2049         else
2050                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
2051         
2052         if(param1)
2053                 compiler.stack_assign(param1);
2054         if(param2)
2055                 compiler.stack_assign(param2);
2056
2057         compiler.add_node(NODE_CLOSURE_VOLUME,
2058                 compiler.encode_uchar4(closure,
2059                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
2060                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
2061                         compiler.closure_mix_weight_offset()),
2062                 __float_as_int((param1)? param1->value.x: 0.0f),
2063                 __float_as_int((param2)? param2->value.x: 0.0f));
2064 }
2065
2066 void VolumeNode::compile(SVMCompiler& compiler)
2067 {
2068         compile(compiler, NULL, NULL);
2069 }
2070
2071 void VolumeNode::compile(OSLCompiler& compiler)
2072 {
2073         assert(0);
2074 }
2075
2076 /* Absorption Volume Closure */
2077
2078 AbsorptionVolumeNode::AbsorptionVolumeNode()
2079 {
2080         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2081 }
2082
2083 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2084 {
2085         VolumeNode::compile(compiler, input("Density"), NULL);
2086 }
2087
2088 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2089 {
2090         compiler.add(this, "node_absorption_volume");
2091 }
2092
2093 /* Scatter Volume Closure */
2094
2095 ScatterVolumeNode::ScatterVolumeNode()
2096 {
2097         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2098         
2099         add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.0f);
2100 }
2101
2102 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2103 {
2104         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2105 }
2106
2107 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2108 {
2109         compiler.add(this, "node_scatter_volume");
2110 }
2111
2112 /* Hair BSDF Closure */
2113
2114 static ShaderEnum hair_component_init()
2115 {
2116         ShaderEnum enm;
2117
2118         enm.insert("Reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2119         enm.insert("Transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2120
2121         return enm;
2122 }
2123
2124 ShaderEnum HairBsdfNode::component_enum = hair_component_init();
2125
2126 HairBsdfNode::HairBsdfNode()
2127 {
2128         component = ustring("Reflection");
2129
2130         add_input("Offset", SHADER_SOCKET_FLOAT);
2131         add_input("RoughnessU", SHADER_SOCKET_FLOAT);
2132         add_input("RoughnessV", SHADER_SOCKET_FLOAT);
2133 }
2134
2135 void HairBsdfNode::compile(SVMCompiler& compiler)
2136 {
2137         closure = (ClosureType)component_enum[component];
2138
2139         BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
2140 }
2141
2142 void HairBsdfNode::compile(OSLCompiler& compiler)
2143 {
2144         compiler.parameter("component", component);
2145
2146         compiler.add(this, "node_hair_bsdf");
2147 }
2148
2149 /* Geometry */
2150
2151 GeometryNode::GeometryNode()
2152 : ShaderNode("geometry")
2153 {
2154         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2155
2156         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2157         add_output("Position", SHADER_SOCKET_POINT);
2158         add_output("Normal", SHADER_SOCKET_NORMAL);
2159         add_output("Tangent", SHADER_SOCKET_NORMAL);
2160         add_output("True Normal", SHADER_SOCKET_NORMAL);
2161         add_output("Incoming", SHADER_SOCKET_VECTOR);
2162         add_output("Parametric", SHADER_SOCKET_POINT);
2163         add_output("Backfacing", SHADER_SOCKET_FLOAT);
2164 }
2165
2166 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2167 {
2168         if(shader->has_surface) {
2169                 if(!output("Tangent")->links.empty())
2170                         attributes->add(ATTR_STD_GENERATED);
2171         }
2172
2173         ShaderNode::attributes(shader, attributes);
2174 }
2175
2176 void GeometryNode::compile(SVMCompiler& compiler)
2177 {
2178         ShaderOutput *out;
2179         NodeType geom_node = NODE_GEOMETRY;
2180
2181         if(bump == SHADER_BUMP_DX)
2182                 geom_node = NODE_GEOMETRY_BUMP_DX;
2183         else if(bump == SHADER_BUMP_DY)
2184                 geom_node = NODE_GEOMETRY_BUMP_DY;
2185         
2186         out = output("Position");
2187         if(!out->links.empty()) {
2188                 compiler.stack_assign(out);
2189                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2190         }
2191
2192         out = output("Normal");
2193         if(!out->links.empty()) {
2194                 compiler.stack_assign(out);
2195                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
2196         }
2197
2198         out = output("Tangent");
2199         if(!out->links.empty()) {
2200                 compiler.stack_assign(out);
2201                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
2202         }
2203
2204         out = output("True Normal");
2205         if(!out->links.empty()) {
2206                 compiler.stack_assign(out);
2207                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
2208         }
2209
2210         out = output("Incoming");
2211         if(!out->links.empty()) {
2212                 compiler.stack_assign(out);
2213                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2214         }
2215
2216         out = output("Parametric");
2217         if(!out->links.empty()) {
2218                 compiler.stack_assign(out);
2219                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
2220         }
2221
2222         out = output("Backfacing");
2223         if(!out->links.empty()) {
2224                 compiler.stack_assign(out);
2225                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
2226         }
2227 }
2228
2229 void GeometryNode::compile(OSLCompiler& compiler)
2230 {
2231         if(bump == SHADER_BUMP_DX)
2232                 compiler.parameter("bump_offset", "dx");
2233         else if(bump == SHADER_BUMP_DY)
2234                 compiler.parameter("bump_offset", "dy");
2235         else
2236                 compiler.parameter("bump_offset", "center");
2237
2238         compiler.add(this, "node_geometry");
2239 }
2240
2241 /* TextureCoordinate */
2242
2243 TextureCoordinateNode::TextureCoordinateNode()
2244 : ShaderNode("texture_coordinate")
2245 {
2246         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2247         add_output("Generated", SHADER_SOCKET_POINT);
2248         add_output("Normal", SHADER_SOCKET_NORMAL);
2249         add_output("UV", SHADER_SOCKET_POINT);
2250         add_output("Object", SHADER_SOCKET_POINT);
2251         add_output("Camera", SHADER_SOCKET_POINT);
2252         add_output("Window", SHADER_SOCKET_POINT);
2253         add_output("Reflection", SHADER_SOCKET_NORMAL);
2254
2255         from_dupli = false;
2256 }
2257
2258 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2259 {
2260         if(shader->has_surface) {
2261                 if(!from_dupli) {
2262                         if(!output("Generated")->links.empty())
2263                                 attributes->add(ATTR_STD_GENERATED);
2264                         if(!output("UV")->links.empty())
2265                                 attributes->add(ATTR_STD_UV);
2266                 }
2267         }
2268
2269         if(shader->has_volume) {
2270                 if(!from_dupli) {
2271                         if(!output("Generated")->links.empty()) {
2272                                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
2273                         }
2274                 }
2275         }
2276
2277         ShaderNode::attributes(shader, attributes);
2278 }
2279
2280 void TextureCoordinateNode::compile(SVMCompiler& compiler)
2281 {
2282         ShaderOutput *out;
2283         NodeType texco_node = NODE_TEX_COORD;
2284         NodeType attr_node = NODE_ATTR;
2285         NodeType geom_node = NODE_GEOMETRY;
2286
2287         if(bump == SHADER_BUMP_DX) {
2288                 texco_node = NODE_TEX_COORD_BUMP_DX;
2289                 attr_node = NODE_ATTR_BUMP_DX;
2290                 geom_node = NODE_GEOMETRY_BUMP_DX;
2291         }
2292         else if(bump == SHADER_BUMP_DY) {
2293                 texco_node = NODE_TEX_COORD_BUMP_DY;
2294                 attr_node = NODE_ATTR_BUMP_DY;
2295                 geom_node = NODE_GEOMETRY_BUMP_DY;
2296         }
2297         
2298         out = output("Generated");
2299         if(!out->links.empty()) {
2300                 if(compiler.background) {
2301                         compiler.stack_assign(out);
2302                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2303                 }
2304                 else {
2305                         if(from_dupli) {
2306                                 compiler.stack_assign(out);
2307                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
2308                         }
2309                         else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
2310                                 compiler.stack_assign(out);
2311                                 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, out->stack_offset);
2312                         }
2313                         else {
2314                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
2315                                 compiler.stack_assign(out);
2316                                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2317                         }
2318                 }
2319         }
2320
2321         out = output("Normal");
2322         if(!out->links.empty()) {
2323                 compiler.stack_assign(out);
2324                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
2325         }
2326
2327         out = output("UV");
2328         if(!out->links.empty()) {
2329                 if(from_dupli) {
2330                         compiler.stack_assign(out);
2331                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
2332                 }
2333                 else {
2334                         int attr = compiler.attribute(ATTR_STD_UV);
2335                         compiler.stack_assign(out);
2336                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2337                 }
2338         }
2339
2340         out = output("Object");
2341         if(!out->links.empty()) {
2342                 compiler.stack_assign(out);
2343                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
2344         }
2345
2346         out = output("Camera");
2347         if(!out->links.empty()) {
2348                 compiler.stack_assign(out);
2349                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
2350         }
2351
2352         out = output("Window");
2353         if(!out->links.empty()) {
2354                 compiler.stack_assign(out);
2355                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
2356         }
2357
2358         out = output("Reflection");
2359         if(!out->links.empty()) {
2360                 if(compiler.background) {
2361                         compiler.stack_assign(out);
2362                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2363                 }
2364                 else {
2365                         compiler.stack_assign(out);
2366                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
2367                 }
2368         }
2369 }
2370
2371 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2372 {
2373         if(bump == SHADER_BUMP_DX)
2374                 compiler.parameter("bump_offset", "dx");
2375         else if(bump == SHADER_BUMP_DY)
2376                 compiler.parameter("bump_offset", "dy");
2377         else
2378                 compiler.parameter("bump_offset", "center");
2379         
2380         if(compiler.background)
2381                 compiler.parameter("is_background", true);
2382         if(compiler.output_type() == SHADER_TYPE_VOLUME)
2383                 compiler.parameter("is_volume", true);
2384         
2385         compiler.parameter("from_dupli", from_dupli);
2386
2387         compiler.add(this, "node_texture_coordinate");
2388 }
2389
2390 UVMapNode::UVMapNode()
2391 : ShaderNode("uvmap")
2392 {
2393         attribute = "";
2394         from_dupli = false;
2395
2396         add_output("UV", SHADER_SOCKET_POINT);
2397 }
2398
2399 void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2400 {
2401         if(shader->has_surface) {
2402                 if(!from_dupli) {
2403                         if(!output("UV")->links.empty()) {
2404                                 if (attribute != "")
2405                                         attributes->add(attribute);
2406                                 else
2407                                         attributes->add(ATTR_STD_UV);
2408                         }
2409                 }
2410         }
2411
2412         ShaderNode::attributes(shader, attributes);
2413 }
2414
2415 void UVMapNode::compile(SVMCompiler& compiler)
2416 {
2417         ShaderOutput *out = output("UV");
2418         NodeType texco_node = NODE_TEX_COORD;
2419         NodeType attr_node = NODE_ATTR;
2420         int attr;
2421
2422         if(bump == SHADER_BUMP_DX) {
2423                 texco_node = NODE_TEX_COORD_BUMP_DX;
2424                 attr_node = NODE_ATTR_BUMP_DX;
2425         }
2426         else if(bump == SHADER_BUMP_DY) {
2427                 texco_node = NODE_TEX_COORD_BUMP_DY;
2428                 attr_node = NODE_ATTR_BUMP_DY;
2429         }
2430
2431         if(!out->links.empty()) {
2432                 if(from_dupli) {
2433                         compiler.stack_assign(out);
2434                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
2435                 }
2436                 else {
2437                         if (attribute != "")
2438                                 attr = compiler.attribute(attribute);
2439                         else
2440                                 attr = compiler.attribute(ATTR_STD_UV);
2441
2442                         compiler.stack_assign(out);
2443                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2444                 }
2445         }
2446 }
2447
2448 void UVMapNode::compile(OSLCompiler& compiler)
2449 {
2450         if(bump == SHADER_BUMP_DX)
2451                 compiler.parameter("bump_offset", "dx");
2452         else if(bump == SHADER_BUMP_DY)
2453                 compiler.parameter("bump_offset", "dy");
2454         else
2455                 compiler.parameter("bump_offset", "center");
2456
2457         compiler.parameter("from_dupli", from_dupli);
2458         compiler.parameter("name", attribute.c_str());
2459         compiler.add(this, "node_uv_map");
2460 }
2461
2462 /* Light Path */
2463
2464 LightPathNode::LightPathNode()
2465 : ShaderNode("light_path")
2466 {
2467         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
2468         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
2469         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
2470         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
2471         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
2472         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
2473         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
2474         add_output("Is Volume Scatter Ray", SHADER_SOCKET_FLOAT);
2475         add_output("Ray Length", SHADER_SOCKET_FLOAT);
2476         add_output("Ray Depth", SHADER_SOCKET_FLOAT);
2477         add_output("Transparent Depth", SHADER_SOCKET_FLOAT);
2478 }
2479
2480 void LightPathNode::compile(SVMCompiler& compiler)
2481 {
2482         ShaderOutput *out;
2483
2484         out = output("Is Camera Ray");
2485         if(!out->links.empty()) {
2486                 compiler.stack_assign(out);
2487                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2488         }
2489
2490         out = output("Is Shadow Ray");
2491         if(!out->links.empty()) {
2492                 compiler.stack_assign(out);
2493                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2494         }
2495
2496         out = output("Is Diffuse Ray");
2497         if(!out->links.empty()) {
2498                 compiler.stack_assign(out);
2499                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2500         }
2501
2502         out = output("Is Glossy Ray");
2503         if(!out->links.empty()) {
2504                 compiler.stack_assign(out);
2505                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2506         }
2507
2508         out = output("Is Singular Ray");
2509         if(!out->links.empty()) {
2510                 compiler.stack_assign(out);
2511                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2512         }
2513
2514         out = output("Is Reflection Ray");
2515         if(!out->links.empty()) {
2516                 compiler.stack_assign(out);
2517                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2518         }
2519
2520
2521         out = output("Is Transmission Ray");
2522         if(!out->links.empty()) {
2523                 compiler.stack_assign(out);
2524                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2525         }
2526         
2527         out = output("Is Volume Scatter Ray");
2528         if(!out->links.empty()) {
2529                 compiler.stack_assign(out);
2530                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, out->stack_offset);
2531         }
2532
2533         out = output("Ray Length");
2534         if(!out->links.empty()) {
2535                 compiler.stack_assign(out);
2536                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2537         }
2538         
2539         out = output("Ray Depth");
2540         if(!out->links.empty()) {
2541                 compiler.stack_assign(out);
2542                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, out->stack_offset);
2543         }
2544
2545         out = output("Transparent Depth");
2546         if(!out->links.empty()) {
2547                 compiler.stack_assign(out);
2548                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, out->stack_offset);
2549         }
2550 }
2551
2552 void LightPathNode::compile(OSLCompiler& compiler)
2553 {
2554         compiler.add(this, "node_light_path");
2555 }
2556
2557 /* Light Falloff */
2558
2559 LightFalloffNode::LightFalloffNode()
2560 : ShaderNode("light_fallof")
2561 {
2562         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2563         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2564         add_output("Quadratic", SHADER_SOCKET_FLOAT);
2565         add_output("Linear", SHADER_SOCKET_FLOAT);
2566         add_output("Constant", SHADER_SOCKET_FLOAT);
2567 }
2568
2569 void LightFalloffNode::compile(SVMCompiler& compiler)
2570 {
2571         ShaderInput *strength_in = input("Strength");
2572         ShaderInput *smooth_in = input("Smooth");
2573
2574         compiler.stack_assign(strength_in);
2575         compiler.stack_assign(smooth_in);
2576
2577         ShaderOutput *out = output("Quadratic");
2578         if(!out->links.empty()) {
2579                 compiler.stack_assign(out);
2580                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2581                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2582         }
2583
2584         out = output("Linear");
2585         if(!out->links.empty()) {
2586                 compiler.stack_assign(out);
2587                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2588                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2589         }
2590
2591         out = output("Constant");
2592         if(!out->links.empty()) {
2593                 compiler.stack_assign(out);
2594                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2595                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2596         }
2597 }
2598
2599 void LightFalloffNode::compile(OSLCompiler& compiler)
2600 {
2601         compiler.add(this, "node_light_falloff");
2602 }
2603
2604 /* Object Info */
2605
2606 ObjectInfoNode::ObjectInfoNode()
2607 : ShaderNode("object_info")
2608 {
2609         add_output("Location", SHADER_SOCKET_VECTOR);
2610         add_output("Object Index", SHADER_SOCKET_FLOAT);
2611         add_output("Material Index", SHADER_SOCKET_FLOAT);
2612         add_output("Random", SHADER_SOCKET_FLOAT);
2613 }
2614
2615 void ObjectInfoNode::compile(SVMCompiler& compiler)
2616 {
2617         ShaderOutput *out = output("Location");
2618         if(!out->links.empty()) {
2619                 compiler.stack_assign(out);
2620                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2621         }
2622
2623         out = output("Object Index");
2624         if(!out->links.empty()) {
2625                 compiler.stack_assign(out);
2626                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2627         }
2628
2629         out = output("Material Index");
2630         if(!out->links.empty()) {
2631                 compiler.stack_assign(out);
2632                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2633         }
2634
2635         out = output("Random");
2636         if(!out->links.empty()) {
2637                 compiler.stack_assign(out);
2638                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2639         }
2640 }
2641
2642 void ObjectInfoNode::compile(OSLCompiler& compiler)
2643 {
2644         compiler.add(this, "node_object_info");
2645 }
2646
2647 /* Particle Info */
2648
2649 ParticleInfoNode::ParticleInfoNode()
2650 : ShaderNode("particle_info")
2651 {
2652         add_output("Index", SHADER_SOCKET_FLOAT);
2653         add_output("Age", SHADER_SOCKET_FLOAT);
2654         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2655         add_output("Location", SHADER_SOCKET_POINT);
2656 #if 0   /* not yet supported */
2657         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2658 #endif
2659         add_output("Size", SHADER_SOCKET_FLOAT);
2660         add_output("Velocity", SHADER_SOCKET_VECTOR);
2661         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2662 }
2663
2664 void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2665 {
2666         if(!output("Index")->links.empty())
2667                 attributes->add(ATTR_STD_PARTICLE);
2668         if(!output("Age")->links.empty())
2669                 attributes->add(ATTR_STD_PARTICLE);
2670         if(!output("Lifetime")->links.empty())
2671                 attributes->add(ATTR_STD_PARTICLE);
2672         if(!output("Location")->links.empty())
2673                 attributes->add(ATTR_STD_PARTICLE);
2674 #if 0   /* not yet supported */
2675         if(!output("Rotation")->links.empty())
2676                 attributes->add(ATTR_STD_PARTICLE);
2677 #endif
2678         if(!output("Size")->links.empty())
2679                 attributes->add(ATTR_STD_PARTICLE);
2680         if(!output("Velocity")->links.empty())
2681                 attributes->add(ATTR_STD_PARTICLE);
2682         if(!output("Angular Velocity")->links.empty())
2683                 attributes->add(ATTR_STD_PARTICLE);
2684
2685         ShaderNode::attributes(shader, attributes);
2686 }
2687
2688 void ParticleInfoNode::compile(SVMCompiler& compiler)
2689 {
2690         ShaderOutput *out;
2691         
2692         out = output("Index");
2693         if(!out->links.empty()) {
2694                 compiler.stack_assign(out);
2695                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2696         }
2697         
2698         out = output("Age");
2699         if(!out->links.empty()) {
2700                 compiler.stack_assign(out);
2701                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2702         }
2703         
2704         out = output("Lifetime");
2705         if(!out->links.empty()) {
2706                 compiler.stack_assign(out);
2707                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2708         }
2709         
2710         out = output("Location");
2711         if(!out->links.empty()) {
2712                 compiler.stack_assign(out);
2713                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2714         }
2715         
2716         /* quaternion data is not yet supported by Cycles */
2717 #if 0
2718         out = output("Rotation");
2719         if(!out->links.empty()) {
2720                 compiler.stack_assign(out);
2721                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2722         }
2723 #endif
2724         
2725         out = output("Size");
2726         if(!out->links.empty()) {
2727                 compiler.stack_assign(out);
2728                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2729         }
2730         
2731         out = output("Velocity");
2732         if(!out->links.empty()) {
2733                 compiler.stack_assign(out);
2734                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2735         }
2736         
2737         out = output("Angular Velocity");
2738         if(!out->links.empty()) {
2739                 compiler.stack_assign(out);
2740                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2741         }
2742 }
2743
2744 void ParticleInfoNode::compile(OSLCompiler& compiler)
2745 {
2746         compiler.add(this, "node_particle_info");
2747 }
2748
2749 /* Hair Info */
2750
2751 HairInfoNode::HairInfoNode()
2752 : ShaderNode("hair_info")
2753 {
2754         add_output("Is Strand", SHADER_SOCKET_FLOAT);
2755         add_output("Intercept", SHADER_SOCKET_FLOAT);
2756         add_output("Thickness", SHADER_SOCKET_FLOAT);
2757         add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2758         /*output for minimum hair width transparency - deactivated*/
2759         /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2760 }
2761
2762 void HairInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2763 {
2764         if(shader->has_surface) {
2765                 ShaderOutput *intercept_out = output("Intercept");
2766
2767                 if(!intercept_out->links.empty())
2768                         attributes->add(ATTR_STD_CURVE_INTERCEPT);
2769         }
2770
2771         ShaderNode::attributes(shader, attributes);
2772 }
2773
2774 void HairInfoNode::compile(SVMCompiler& compiler)
2775 {
2776         ShaderOutput *out;
2777         
2778         out = output("Is Strand");
2779         if(!out->links.empty()) {
2780                 compiler.stack_assign(out);
2781                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2782         }
2783
2784         out = output("Intercept");
2785         if(!out->links.empty()) {
2786                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2787                 compiler.stack_assign(out);
2788                 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2789         }
2790
2791         out = output("Thickness");
2792         if(!out->links.empty()) {
2793                 compiler.stack_assign(out);
2794                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2795         }
2796
2797         out = output("Tangent Normal");
2798         if(!out->links.empty()) {
2799                 compiler.stack_assign(out);
2800                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2801         }
2802
2803         /*out = output("Fade");
2804         if(!out->links.empty()) {
2805                 compiler.stack_assign(out);
2806                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2807         }*/
2808
2809 }
2810
2811 void HairInfoNode::compile(OSLCompiler& compiler)
2812 {
2813         compiler.add(this, "node_hair_info");
2814 }
2815
2816 /* Value */
2817
2818 ValueNode::ValueNode()
2819 : ShaderNode("value")
2820 {
2821         value = 0.0f;
2822
2823         add_output("Value", SHADER_SOCKET_FLOAT);
2824 }
2825
2826 void ValueNode::compile(SVMCompiler& compiler)
2827 {
2828         ShaderOutput *val_out = output("Value");
2829
2830         compiler.stack_assign(val_out);
2831         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2832 }
2833
2834 void ValueNode::compile(OSLCompiler& compiler)
2835 {
2836         compiler.parameter("value_value", value);
2837         compiler.add(this, "node_value");
2838 }
2839
2840 /* Color */
2841
2842 ColorNode::ColorNode()
2843 : ShaderNode("color")
2844 {
2845         value = make_float3(0.0f, 0.0f, 0.0f);
2846
2847         add_output("Color", SHADER_SOCKET_COLOR);
2848 }
2849
2850 void ColorNode::compile(SVMCompiler& compiler)
2851 {
2852         ShaderOutput *color_out = output("Color");
2853
2854         if(color_out && !color_out->links.empty()) {
2855                 compiler.stack_assign(color_out);
2856                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2857                 compiler.add_node(NODE_VALUE_V, value);
2858         }
2859 }
2860
2861 void ColorNode::compile(OSLCompiler& compiler)
2862 {
2863         compiler.parameter_color("color_value", value);
2864
2865         compiler.add(this, "node_value");
2866 }
2867
2868 /* Add Closure */
2869
2870 AddClosureNode::AddClosureNode()
2871 : ShaderNode("add_closure")
2872 {
2873         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2874         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2875         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2876 }
2877
2878 void AddClosureNode::compile(SVMCompiler& compiler)
2879 {
2880         /* handled in the SVM compiler */
2881 }
2882
2883 void AddClosureNode::compile(OSLCompiler& compiler)
2884 {
2885         compiler.add(this, "node_add_closure");
2886 }
2887
2888 /* Mix Closure */
2889
2890 MixClosureNode::MixClosureNode()
2891 : ShaderNode("mix_closure")
2892 {
2893         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2894         
2895         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2896         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2897         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2898         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2899 }
2900
2901 void MixClosureNode::compile(SVMCompiler& compiler)
2902 {
2903         /* handled in the SVM compiler */
2904 }
2905
2906 void MixClosureNode::compile(OSLCompiler& compiler)
2907 {
2908         compiler.add(this, "node_mix_closure");
2909 }
2910
2911 /* Mix Closure */
2912
2913 MixClosureWeightNode::MixClosureWeightNode()
2914 : ShaderNode("mix_closure_weight")
2915 {
2916         add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2917         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2918         add_output("Weight1", SHADER_SOCKET_FLOAT);
2919         add_output("Weight2", SHADER_SOCKET_FLOAT);
2920 }
2921
2922 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2923 {
2924         ShaderInput *weight_in = input("Weight");
2925         ShaderInput *fac_in = input("Fac");
2926         ShaderOutput *weight1_out = output("Weight1");
2927         ShaderOutput *weight2_out = output("Weight2");
2928
2929         compiler.stack_assign(weight_in);
2930         compiler.stack_assign(fac_in);
2931         compiler.stack_assign(weight1_out);
2932         compiler.stack_assign(weight2_out);
2933
2934         compiler.add_node(NODE_MIX_CLOSURE,
2935                 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2936                         weight1_out->stack_offset, weight2_out->stack_offset));
2937 }
2938
2939 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2940 {
2941         assert(0);
2942 }
2943
2944 /* Invert */
2945
2946 InvertNode::InvertNode()
2947 : ShaderNode("invert")
2948 {
2949         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2950         add_input("Color", SHADER_SOCKET_COLOR);
2951         add_output("Color",  SHADER_SOCKET_COLOR);
2952 }
2953
2954 void InvertNode::compile(SVMCompiler& compiler)
2955 {
2956         ShaderInput *fac_in = input("Fac");
2957         ShaderInput *color_in = input("Color");
2958         ShaderOutput *color_out = output("Color");
2959
2960         compiler.stack_assign(fac_in);
2961         compiler.stack_assign(color_in);
2962         compiler.stack_assign(color_out);
2963
2964         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2965 }
2966
2967 void InvertNode::compile(OSLCompiler& compiler)
2968 {
2969         compiler.add(this, "node_invert");
2970 }
2971
2972 /* Mix */
2973
2974 MixNode::MixNode()
2975 : ShaderNode("mix")
2976 {
2977         type = ustring("Mix");
2978
2979         use_clamp = false;
2980
2981         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2982         add_input("Color1", SHADER_SOCKET_COLOR);
2983         add_input("Color2", SHADER_SOCKET_COLOR);
2984         add_output("Color",  SHADER_SOCKET_COLOR);
2985 }
2986
2987 static ShaderEnum mix_type_init()
2988 {
2989         ShaderEnum enm;
2990
2991         enm.insert("Mix", NODE_MIX_BLEND);
2992         enm.insert("Add", NODE_MIX_ADD);
2993         enm.insert("Multiply", NODE_MIX_MUL);
2994         enm.insert("Screen", NODE_MIX_SCREEN);
2995         enm.insert("Overlay", NODE_MIX_OVERLAY);
2996         enm.insert("Subtract", NODE_MIX_SUB);
2997         enm.insert("Divide", NODE_MIX_DIV);
2998         enm.insert("Difference", NODE_MIX_DIFF);
2999         enm.insert("Darken", NODE_MIX_DARK);
3000         enm.insert("Lighten", NODE_MIX_LIGHT);
3001         enm.insert("Dodge", NODE_MIX_DODGE);
3002         enm.insert("Burn", NODE_MIX_BURN);
3003         enm.insert("Hue", NODE_MIX_HUE);
3004         enm.insert("Saturation", NODE_MIX_SAT);
3005         enm.insert("Value", NODE_MIX_VAL);
3006         enm.insert("Color", NODE_MIX_COLOR);
3007         enm.insert("Soft Light", NODE_MIX_SOFT);
3008         enm.insert("Linear Light", NODE_MIX_LINEAR);
3009
3010         return enm;
3011 }
3012
3013 ShaderEnum MixNode::type_enum = mix_type_init();
3014
3015 void MixNode::compile(SVMCompiler& compiler)
3016 {
3017         ShaderInput *fac_in = input("Fac");
3018         ShaderInput *color1_in = input("Color1");
3019         ShaderInput *color2_in = input("Color2");
3020         ShaderOutput *color_out = output("Color");
3021
3022         compiler.stack_assign(fac_in);
3023         compiler.stack_assign(color1_in);
3024         compiler.stack_assign(color2_in);
3025         compiler.stack_assign(color_out);
3026
3027         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
3028         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
3029
3030         if(use_clamp) {
3031                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
3032                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
3033         }
3034 }
3035
3036 void MixNode::compile(OSLCompiler& compiler)
3037 {
3038         compiler.parameter("type", type);
3039         compiler.parameter("Clamp", use_clamp);
3040         compiler.add(this, "node_mix");
3041 }
3042
3043 /* Combine RGB */
3044 CombineRGBNode::CombineRGBNode()
3045 : ShaderNode("combine_rgb")
3046 {
3047         add_input("R", SHADER_SOCKET_FLOAT);
3048         add_input("G", SHADER_SOCKET_FLOAT);
3049         add_input("B", SHADER_SOCKET_FLOAT);
3050         add_output("Image", SHADER_SOCKET_COLOR);
3051 }
3052
3053 void CombineRGBNode::compile(SVMCompiler& compiler)
3054 {
3055         ShaderInput *red_in = input("R");
3056         ShaderInput *green_in = input("G");
3057         ShaderInput *blue_in = input("B");
3058         ShaderOutput *color_out = output("Image");
3059
3060         compiler.stack_assign(color_out);
3061
3062         compiler.stack_assign(red_in);
3063         compiler.add_node(NODE_COMBINE_VECTOR, red_in->stack_offset, 0, color_out->stack_offset);
3064
3065         compiler.stack_assign(green_in);
3066         compiler.add_node(NODE_COMBINE_VECTOR, green_in->stack_offset, 1, color_out->stack_offset);
3067
3068         compiler.stack_assign(blue_in);
3069         compiler.add_node(NODE_COMBINE_VECTOR, blue_in->stack_offset, 2, color_out->stack_offset);
3070 }
3071
3072 void CombineRGBNode::compile(OSLCompiler& compiler)
3073 {
3074         compiler.add(this, "node_combine_rgb");
3075 }
3076
3077 /* Combine XYZ */
3078 CombineXYZNode::CombineXYZNode()
3079 : ShaderNode("combine_xyz")
3080 {
3081         add_input("X", SHADER_SOCKET_FLOAT);
3082         add_input("Y", SHADER_SOCKET_FLOAT);
3083         add_input("Z", SHADER_SOCKET_FLOAT);
3084         add_output("Vector", SHADER_SOCKET_VECTOR);
3085 }
3086
3087 void CombineXYZNode::compile(SVMCompiler& compiler)
3088 {
3089         ShaderInput *x_in = input("X");
3090         ShaderInput *y_in = input("Y");
3091         ShaderInput *z_in = input("Z");
3092         ShaderOutput *vector_out = output("Vector");
3093
3094         compiler.stack_assign(vector_out);
3095
3096         compiler.stack_assign(x_in);
3097         compiler.add_node(NODE_COMBINE_VECTOR, x_in->stack_offset, 0, vector_out->stack_offset);
3098
3099         compiler.stack_assign(y_in);
3100         compiler.add_node(NODE_COMBINE_VECTOR, y_in->stack_offset, 1, vector_out->stack_offset);
3101
3102         compiler.stack_assign(z_in);
3103         compiler.add_node(NODE_COMBINE_VECTOR, z_in->stack_offset, 2, vector_out->stack_offset);
3104 }
3105
3106 void CombineXYZNode::compile(OSLCompiler& compiler)
3107 {
3108         compiler.add(this, "node_combine_xyz");
3109 }
3110
3111 /* Combine HSV */
3112 CombineHSVNode::CombineHSVNode()
3113 : ShaderNode("combine_hsv")
3114 {
3115         add_input("H", SHADER_SOCKET_FLOAT);
3116         add_input("S", SHADER_SOCKET_FLOAT);
3117         add_input("V", SHADER_SOCKET_FLOAT);
3118         add_output("Color", SHADER_SOCKET_COLOR);
3119 }
3120
3121 void CombineHSVNode::compile(SVMCompiler& compiler)
3122 {
3123         ShaderInput *hue_in = input("H");
3124         ShaderInput *saturation_in = input("S");
3125         ShaderInput *value_in = input("V");
3126         ShaderOutput *color_out = output("Color");
3127
3128         compiler.stack_assign(color_out);
3129         compiler.stack_assign(hue_in);
3130         compiler.stack_assign(saturation_in);
3131         compiler.stack_assign(value_in);
3132         
3133         compiler.add_node(NODE_COMBINE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3134         compiler.add_node(NODE_COMBINE_HSV, color_out->stack_offset);
3135 }
3136
3137 void CombineHSVNode::compile(OSLCompiler& compiler)
3138 {
3139         compiler.add(this, "node_combine_hsv");
3140 }
3141
3142 /* Gamma */
3143 GammaNode::GammaNode()
3144 : ShaderNode("gamma")
3145 {
3146         add_input("Color", SHADER_SOCKET_COLOR);
3147         add_input("Gamma", SHADER_SOCKET_FLOAT);
3148         add_output("Color", SHADER_SOCKET_COLOR);
3149 }
3150
3151 void GammaNode::compile(SVMCompiler& compiler)
3152 {
3153         ShaderInput *color_in = input("Color");
3154         ShaderInput *gamma_in = input("Gamma");
3155         ShaderOutput *color_out = output("Color");
3156
3157         compiler.stack_assign(color_in);
3158         compiler.stack_assign(gamma_in);
3159         compiler.stack_assign(color_out);
3160
3161         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3162 }
3163
3164 void GammaNode::compile(OSLCompiler& compiler)
3165 {
3166         compiler.add(this, "node_gamma");
3167 }
3168
3169 /* Bright Contrast */
3170 BrightContrastNode::BrightContrastNode()
3171 : ShaderNode("brightness")
3172 {
3173         add_input("Color", SHADER_SOCKET_COLOR);
3174         add_input("Bright", SHADER_SOCKET_FLOAT);
3175         add_input("Contrast", SHADER_SOCKET_FLOAT);
3176         add_output("Color", SHADER_SOCKET_COLOR);
3177 }
3178
3179 void BrightContrastNode::compile(SVMCompiler& compiler)
3180 {
3181         ShaderInput *color_in = input("Color");
3182         ShaderInput *bright_in = input("Bright");
3183         ShaderInput *contrast_in = input("Contrast");
3184         ShaderOutput *color_out = output("Color");
3185
3186         compiler.stack_assign(color_in);
3187         compiler.stack_assign(bright_in);
3188         compiler.stack_assign(contrast_in);
3189         compiler.stack_assign(color_out);
3190
3191         compiler.add_node(NODE_BRIGHTCONTRAST,
3192                 color_in->stack_offset, color_out->stack_offset,
3193                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
3194 }
3195
3196 void BrightContrastNode::compile(OSLCompiler& compiler)
3197 {
3198         compiler.add(this, "node_brightness");
3199 }
3200
3201 /* Separate RGB */
3202 SeparateRGBNode::SeparateRGBNode()
3203 : ShaderNode("separate_rgb")
3204 {
3205         add_input("Image", SHADER_SOCKET_COLOR);
3206         add_output("R", SHADER_SOCKET_FLOAT);
3207         add_output("G", SHADER_SOCKET_FLOAT);
3208         add_output("B", SHADER_SOCKET_FLOAT);
3209 }
3210
3211 void SeparateRGBNode::compile(SVMCompiler& compiler)
3212 {
3213         ShaderInput *color_in = input("Image");
3214         ShaderOutput *red_out = output("R");
3215         ShaderOutput *green_out = output("G");
3216         ShaderOutput *blue_out = output("B");
3217
3218         compiler.stack_assign(color_in);
3219
3220         compiler.stack_assign(red_out);
3221         compiler.add_node(NODE_SEPARATE_VECTOR, color_in->stack_offset, 0, red_out->stack_offset);
3222
3223         compiler.stack_assign(green_out);
3224         compiler.add_node(NODE_SEPARATE_VECTOR, color_in->stack_offset, 1, green_out->stack_offset);
3225
3226         compiler.stack_assign(blue_out);
3227         compiler.add_node(NODE_SEPARATE_VECTOR, color_in->stack_offset, 2, blue_out->stack_offset);
3228 }
3229
3230 void SeparateRGBNode::compile(OSLCompiler& compiler)
3231 {
3232         compiler.add(this, "node_separate_rgb");
3233 }
3234
3235 /* Separate XYZ */
3236 SeparateXYZNode::SeparateXYZNode()
3237 : ShaderNode("separate_xyz")
3238 {
3239         add_input("Vector", SHADER_SOCKET_VECTOR);
3240         add_output("X", SHADER_SOCKET_FLOAT);
3241         add_output("Y", SHADER_SOCKET_FLOAT);
3242         add_output("Z", SHADER_SOCKET_FLOAT);
3243 }
3244
3245 void SeparateXYZNode::compile(SVMCompiler& compiler)
3246 {
3247         ShaderInput *vector_in = input("Vector");
3248         ShaderOutput *x_out = output("X");
3249         ShaderOutput *y_out = output("Y");
3250         ShaderOutput *z_out = output("Z");
3251
3252         compiler.stack_assign(vector_in);
3253
3254         compiler.stack_assign(x_out);
3255         compiler.add_node(NODE_SEPARATE_VECTOR, vector_in->stack_offset, 0, x_out->stack_offset);
3256
3257         compiler.stack_assign(y_out);
3258         compiler.add_node(NODE_SEPARATE_VECTOR, vector_in->stack_offset, 1, y_out->stack_offset);
3259
3260         compiler.stack_assign(z_out);
3261         compiler.add_node(NODE_SEPARATE_VECTOR, vector_in->stack_offset, 2, z_out->stack_offset);
3262 }
3263
3264 void SeparateXYZNode::compile(OSLCompiler& compiler)
3265 {
3266         compiler.add(this, "node_separate_xyz");
3267 }
3268
3269 /* Separate HSV */
3270 SeparateHSVNode::SeparateHSVNode()
3271 : ShaderNode("separate_hsv")
3272 {
3273         add_input("Color", SHADER_SOCKET_COLOR);
3274         add_output("H", SHADER_SOCKET_FLOAT);
3275         add_output("S", SHADER_SOCKET_FLOAT);
3276         add_output("V", SHADER_SOCKET_FLOAT);
3277 }
3278
3279 void SeparateHSVNode::compile(SVMCompiler& compiler)
3280 {
3281         ShaderInput *color_in = input("Color");
3282         ShaderOutput *hue_out = output("H");
3283         ShaderOutput *saturation_out = output("S");
3284         ShaderOutput *value_out = output("V");
3285
3286         compiler.stack_assign(color_in);
3287         compiler.stack_assign(hue_out);
3288         compiler.stack_assign(saturation_out);
3289         compiler.stack_assign(value_out);
3290         
3291         compiler.add_node(NODE_SEPARATE_HSV, color_in->stack_offset, hue_out->stack_offset, saturation_out->stack_offset);
3292         compiler.add_node(NODE_SEPARATE_HSV, value_out->stack_offset);
3293
3294 }
3295
3296 void SeparateHSVNode::compile(OSLCompiler& compiler)
3297 {
3298         compiler.add(this, "node_separate_hsv");
3299 }
3300
3301 /* Hue Saturation Value */
3302 HSVNode::HSVNode()
3303 : ShaderNode("hsv")
3304 {
3305         add_input("Hue", SHADER_SOCKET_FLOAT);
3306         add_input("Saturation", SHADER_SOCKET_FLOAT);
3307         add_input("Value", SHADER_SOCKET_FLOAT);
3308         add_input("Fac", SHADER_SOCKET_FLOAT);
3309         add_input("Color", SHADER_SOCKET_COLOR);
3310         add_output("Color", SHADER_SOCKET_COLOR);
3311 }
3312
3313 void HSVNode::compile(SVMCompiler& compiler)
3314 {
3315         ShaderInput *hue_in = input("Hue");
3316         ShaderInput *saturation_in = input("Saturation");
3317         ShaderInput *value_in = input("Value");
3318         ShaderInput *fac_in = input("Fac");
3319         ShaderInput *color_in = input("Color");
3320         ShaderOutput *color_out = output("Color");
3321
3322         compiler.stack_assign(hue_in);
3323         compiler.stack_assign(saturation_in);
3324         compiler.stack_assign(value_in);
3325         compiler.stack_assign(fac_in);
3326         compiler.stack_assign(color_in);
3327         compiler.stack_assign(color_out);
3328
3329         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
3330         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3331 }
3332
3333 void HSVNode::compile(OSLCompiler& compiler)
3334 {
3335         compiler.add(this, "node_hsv");
3336 }
3337
3338 /* Attribute */
3339
3340 AttributeNode::AttributeNode()
3341 : ShaderNode("attribute")
3342 {
3343         attribute = "";
3344
3345         add_output("Color",  SHADER_SOCKET_COLOR);
3346         add_output("Vector",  SHADER_SOCKET_VECTOR);
3347         add_output("Fac",  SHADER_SOCKET_FLOAT);
3348 }
3349
3350 void AttributeNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3351 {
3352         ShaderOutput *color_out = output("Color");
3353         ShaderOutput *vector_out = output("Vector");
3354         ShaderOutput *fac_out = output("Fac");
3355
3356         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty()) {
3357                 AttributeStandard std = Attribute::name_standard(attribute.c_str());
3358
3359                 if(std != ATTR_STD_NONE)
3360                         attributes->add(std);
3361                 else
3362                         attributes->add(attribute);
3363         }
3364
3365         if(shader->has_volume)
3366                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
3367
3368         ShaderNode::attributes(shader, attributes);
3369 }
3370
3371 void AttributeNode::compile(SVMCompiler& compiler)
3372 {
3373         ShaderOutput *color_out = output("Color");
3374         ShaderOutput *vector_out = output("Vector");
3375         ShaderOutput *fac_out = output("Fac");
3376         NodeType attr_node = NODE_ATTR;
3377         AttributeStandard std = Attribute::name_standard(attribute.c_str());
3378         int attr;
3379
3380         if(std != ATTR_STD_NONE)
3381                 attr = compiler.attribute(std);
3382         else
3383                 attr = compiler.attribute(attribute);
3384
3385         if(bump == SHADER_BUMP_DX)
3386                 attr_node = NODE_ATTR_BUMP_DX;
3387         else if(bump == SHADER_BUMP_DY)
3388                 attr_node = NODE_ATTR_BUMP_DY;
3389
3390         if(!color_out->links.empty() || !vector_out->links.empty()) {
3391                 if(!color_out->links.empty()) {
3392                         compiler.stack_assign(color_out);
3393                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
3394                 }
3395                 if(!vector_out->links.empty()) {
3396                         compiler.stack_assign(vector_out);
3397                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
3398                 }
3399         }
3400
3401         if(!fac_out->links.empty()) {
3402                 compiler.stack_assign(fac_out);
3403                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
3404         }
3405 }
3406
3407 void AttributeNode::compile(OSLCompiler& compiler)
3408 {
3409         if(bump == SHADER_BUMP_DX)
3410                 compiler.parameter("bump_offset", "dx");
3411         else if(bump == SHADER_BUMP_DY)
3412                 compiler.parameter("bump_offset", "dy");
3413         else
3414                 compiler.parameter("bump_offset", "center");
3415         
3416         if(Attribute::name_standard(attribute.c_str()) != ATTR_STD_NONE)
3417                 compiler.parameter("name", (string("geom:") + attribute.c_str()).c_str());
3418         else
3419                 compiler.parameter("name", attribute.c_str());
3420
3421         compiler.add(this, "node_attribute");
3422 }
3423
3424 /* Camera */
3425
3426 CameraNode::CameraNode()
3427 : ShaderNode("camera")
3428 {
3429         add_output("View Vector",  SHADER_SOCKET_VECTOR);
3430         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
3431         add_output("View Distance",  SHADER_SOCKET_FLOAT);
3432 }
3433
3434 void CameraNode::compile(SVMCompiler& compiler)
3435 {
3436         ShaderOutput *vector_out = output("View Vector");
3437         ShaderOutput *z_depth_out = output("View Z Depth");
3438         ShaderOutput *distance_out = output("View Distance");
3439
3440         compiler.stack_assign(vector_out);
3441         compiler.stack_assign(z_depth_out);
3442         compiler.stack_assign(distance_out);
3443         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
3444 }
3445
3446 void CameraNode::compile(OSLCompiler& compiler)
3447 {
3448         compiler.add(this, "node_camera");
3449 }
3450
3451 /* Fresnel */
3452
3453 FresnelNode::FresnelNode()
3454 : ShaderNode("fresnel")
3455 {
3456         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3457         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
3458         add_output("Fac", SHADER_SOCKET_FLOAT);
3459 }
3460
3461 void FresnelNode::compile(SVMCompiler& compiler)
3462 {
3463         ShaderInput *normal_in = input("Normal");
3464         ShaderInput *ior_in = input("IOR");
3465         ShaderOutput *fac_out = output("Fac");
3466
3467         compiler.stack_assign(ior_in);
3468         compiler.stack_assign(fac_out);
3469         
3470         if(normal_in->link)
3471                 compiler.stack_assign(normal_in);
3472         
3473         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));
3474 }
3475
3476 void FresnelNode::compile(OSLCompiler& compiler)
3477 {
3478         compiler.add(this, "node_fresnel");
3479 }
3480
3481 /* Layer Weight */
3482
3483 LayerWeightNode::LayerWeightNode()
3484 : ShaderNode("layer_weight")
3485 {
3486         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3487         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
3488
3489         add_output("Fresnel", SHADER_SOCKET_FLOAT);
3490         add_output("Facing", SHADER_SOCKET_FLOAT);
3491 }
3492
3493 void LayerWeightNode::compile(SVMCompiler& compiler)
3494 {
3495         ShaderInput *normal_in = input("Normal");
3496         ShaderInput *blend_in = input("Blend");
3497
3498         if(normal_in->link)
3499                 compiler.stack_assign(normal_in);
3500
3501         if(blend_in->link)
3502                 compiler.stack_assign(blend_in);
3503
3504         ShaderOutput *fresnel_out = output("Fresnel");
3505         if(!fresnel_out->links.empty()) {
3506                 compiler.stack_assign(fresnel_out);
3507                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3508                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, normal_in->stack_offset, fresnel_out->stack_offset));
3509         }
3510
3511         ShaderOutput *facing_out = output("Facing");
3512         if(!facing_out->links.empty()) {
3513                 compiler.stack_assign(facing_out);
3514                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3515                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, normal_in->stack_offset, facing_out->stack_offset));
3516         }
3517 }
3518
3519 void LayerWeightNode::compile(OSLCompiler& compiler)
3520 {
3521         compiler.add(this, "node_layer_weight");
3522 }
3523
3524 /* Wireframe */
3525
3526 WireframeNode::WireframeNode()
3527 : ShaderNode("wireframe")
3528 {
3529         add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
3530         add_output("Fac", SHADER_SOCKET_FLOAT);
3531         
3532         use_pixel_size = false;
3533 }
3534
3535 void WireframeNode::compile(SVMCompiler& compiler)
3536 {
3537         ShaderInput *size_in = input("Size");
3538         ShaderOutput *fac_out = output("Fac");
3539
3540         compiler.stack_assign(size_in);
3541         compiler.stack_assign(fac_out);
3542         compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
3543 }
3544
3545 void WireframeNode::compile(OSLCompiler& compiler)
3546 {
3547         compiler.parameter("use_pixel_size", use_pixel_size);
3548         compiler.add(this, "node_wireframe");
3549 }
3550
3551 /* Wavelength */
3552
3553 WavelengthNode::WavelengthNode()
3554 : ShaderNode("wavelength")
3555 {
3556         add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
3557         add_output("Color", SHADER_SOCKET_COLOR);
3558 }
3559
3560 void WavelengthNode::compile(SVMCompiler& compiler)
3561 {
3562         ShaderInput *wavelength_in = input("Wavelength");
3563         ShaderOutput *color_out = output("Color");
3564
3565         compiler.stack_assign(wavelength_in);
3566         compiler.stack_assign(color_out);
3567         compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset);
3568 }
3569
3570 void WavelengthNode::compile(OSLCompiler& compiler)
3571 {
3572         compiler.add(this, "node_wavelength");
3573 }
3574
3575 /* Blackbody */
3576
3577 BlackbodyNode::BlackbodyNode()
3578 : ShaderNode("blackbody")
3579 {
3580         add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f);
3581         add_output("Color", SHADER_SOCKET_COLOR);
3582 }
3583
3584 void BlackbodyNode::compile(SVMCompiler& compiler)
3585 {
3586         ShaderInput *temperature_in = input("Temperature");
3587         ShaderOutput *color_out = output("Color");
3588
3589         compiler.stack_assign(temperature_in);
3590         compiler.stack_assign(color_out);
3591         compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset);
3592 }
3593
3594 void BlackbodyNode::compile(OSLCompiler& compiler)
3595 {
3596         compiler.add(this, "node_blackbody");
3597 }
3598
3599 /* Output */
3600
3601 OutputNode::OutputNode()
3602 : ShaderNode("output")
3603 {
3604         add_input("Surface", SHADER_SOCKET_CLOSURE);
3605         add_input("Volume", SHADER_SOCKET_CLOSURE);
3606         add_input("Displacement", SHADER_SOCKET_FLOAT);
3607         add_input("Normal", SHADER_SOCKET_NORMAL);
3608 }
3609
3610 void OutputNode::compile(SVMCompiler& compiler)
3611 {
3612         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
3613                 ShaderInput *displacement_in = input("Displacement");
3614
3615                 if(displacement_in->link) {
3616                         compiler.stack_assign(displacement_in);
3617                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
3618                 }
3619         }
3620 }
3621
3622 void OutputNode::compile(OSLCompiler& compiler)
3623 {
3624         if(compiler.output_type() == SHADER_TYPE_SURFACE)
3625                 compiler.add(this, "node_output_surface");
3626         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3627                 compiler.add(this, "node_output_volume");
3628         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3629                 compiler.add(this, "node_output_displacement");
3630 }
3631
3632 /* Math */
3633
3634 MathNode::MathNode()
3635 : ShaderNode("math")
3636 {
3637         type = ustring("Add");
3638
3639         use_clamp = false;
3640
3641         add_input("Value1", SHADER_SOCKET_FLOAT);
3642         add_input("Value2", SHADER_SOCKET_FLOAT);
3643         add_output("Value",  SHADER_SOCKET_FLOAT);
3644 }
3645
3646 static ShaderEnum math_type_init()
3647 {
3648         ShaderEnum enm;
3649
3650         enm.insert("Add", NODE_MATH_ADD);
3651         enm.insert("Subtract", NODE_MATH_SUBTRACT);
3652         enm.insert("Multiply", NODE_MATH_MULTIPLY);
3653         enm.insert("Divide", NODE_MATH_DIVIDE);
3654         enm.insert("Sine", NODE_MATH_SINE);
3655         enm.insert("Cosine", NODE_MATH_COSINE);
3656         enm.insert("Tangent", NODE_MATH_TANGENT);
3657         enm.insert("Arcsine", NODE_MATH_ARCSINE);
3658         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3659         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3660         enm.insert("Power", NODE_MATH_POWER);
3661         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3662         enm.insert("Minimum", NODE_MATH_MINIMUM);
3663         enm.insert("Maximum", NODE_MATH_MAXIMUM);
3664         enm.insert("Round", NODE_MATH_ROUND);
3665         enm.insert("Less Than", NODE_MATH_LESS_THAN);
3666         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3667         enm.insert("Modulo", NODE_MATH_MODULO);
3668     enm.insert("Absolute", NODE_MATH_ABSOLUTE);
3669
3670         return enm;
3671 }
3672
3673 ShaderEnum MathNode::type_enum = math_type_init();
3674
3675 void MathNode::compile(SVMCompiler& compiler)
3676 {
3677         ShaderInput *value1_in = input("Value1");
3678         ShaderInput *value2_in = input("Value2");
3679         ShaderOutput *value_out = output("Value");
3680
3681         compiler.stack_assign(value1_in);
3682         compiler.stack_assign(value2_in);
3683         compiler.stack_assign(value_out);
3684
3685         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3686         compiler.add_node(NODE_MATH, value_out->stack_offset);
3687
3688         if(use_clamp) {
3689                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3690                 compiler.add_node(NODE_MATH, value_out->stack_offset);
3691         }
3692 }
3693
3694 void MathNode::compile(OSLCompiler& compiler)
3695 {
3696         compiler.parameter("type", type);
3697         compiler.parameter("Clamp", use_clamp);
3698         compiler.add(this, "node_math");
3699 }
3700
3701 /* VectorMath */
3702
3703 VectorMathNode::VectorMathNode()
3704 : ShaderNode("vector_math")
3705 {
3706         type = ustring("Add");
3707
3708         add_input("Vector1", SHADER_SOCKET_VECTOR);
3709         add_input("Vector2", SHADER_SOCKET_VECTOR);
3710         add_output("Value",  SHADER_SOCKET_FLOAT);
3711         add_output("Vector",  SHADER_SOCKET_VECTOR);
3712 }
3713
3714 static ShaderEnum vector_math_type_init()
3715 {
3716         ShaderEnum enm;
3717
3718         enm.insert("Add", NODE_VECTOR_MATH_ADD);
3719         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3720         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3721         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3722         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3723         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3724
3725         return enm;
3726 }
3727
3728 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3729