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