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