Code cleanup: Add -Werror=float-conversion to Cycles
[blender.git] / intern / cycles / render / nodes.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16
17 #include "image.h"
18 #include "nodes.h"
19 #include "svm.h"
20 #include "osl.h"
21 #include "sky_model.h"
22
23 #include "util_foreach.h"
24 #include "util_transform.h"
25
26 CCL_NAMESPACE_BEGIN
27
28 /* Texture Mapping */
29
30 TextureMapping::TextureMapping()
31 {
32         translation = make_float3(0.0f, 0.0f, 0.0f);
33         rotation = make_float3(0.0f, 0.0f, 0.0f);
34         scale = make_float3(1.0f, 1.0f, 1.0f);
35
36         min = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
37         max = make_float3(FLT_MAX, FLT_MAX, FLT_MAX);
38
39         use_minmax = false;
40
41         x_mapping = X;
42         y_mapping = Y;
43         z_mapping = Z;
44
45         type = TEXTURE;
46
47         projection = FLAT;
48 }
49
50 Transform TextureMapping::compute_transform()
51 {
52         Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
53
54         if(x_mapping != NONE)
55                 mmat[0][x_mapping-1] = 1.0f;
56         if(y_mapping != NONE)
57                 mmat[1][y_mapping-1] = 1.0f;
58         if(z_mapping != NONE)
59                 mmat[2][z_mapping-1] = 1.0f;
60         
61         float3 scale_clamped = scale;
62
63         if(type == TEXTURE || type == NORMAL) {
64                 /* keep matrix invertible */
65                 if(fabsf(scale.x) < 1e-5f)
66                         scale_clamped.x = signf(scale.x)*1e-5f;
67                 if(fabsf(scale.y) < 1e-5f)
68                         scale_clamped.y = signf(scale.y)*1e-5f;
69                 if(fabsf(scale.z) < 1e-5f)
70                         scale_clamped.z = signf(scale.z)*1e-5f;
71         }
72         
73         Transform smat = transform_scale(scale_clamped);
74         Transform rmat = transform_euler(rotation);
75         Transform tmat = transform_translate(translation);
76
77         Transform mat;
78
79         switch(type) {
80                 case TEXTURE:
81                         /* inverse transform on texture coordinate gives
82                          * forward transform on texture */
83                         mat = tmat*rmat*smat;
84                         mat = transform_inverse(mat);
85                         break;
86                 case POINT:
87                         /* full transform */
88                         mat = tmat*rmat*smat;
89                         break;
90                 case VECTOR:
91                         /* no translation for vectors */
92                         mat = rmat*smat;
93                         break;
94                 case NORMAL:
95                         /* no translation for normals, and inverse transpose */
96                         mat = rmat*smat;
97                         mat = transform_inverse(mat);
98                         mat = transform_transpose(mat);
99                         break;
100         }
101
102         /* projection last */
103         mat = mat*mmat;
104
105         return mat;
106 }
107
108 bool TextureMapping::skip()
109 {
110         if(translation != make_float3(0.0f, 0.0f, 0.0f))
111                 return false;
112         if(rotation != make_float3(0.0f, 0.0f, 0.0f))
113                 return false;
114         if(scale != make_float3(1.0f, 1.0f, 1.0f))
115                 return false;
116         
117         if(x_mapping != X || y_mapping != Y || z_mapping != Z)
118                 return false;
119         if(use_minmax)
120                 return false;
121         
122         return true;
123 }
124
125 void TextureMapping::compile(SVMCompiler& compiler, int offset_in, int offset_out)
126 {
127         if(offset_in == SVM_STACK_INVALID || offset_out == SVM_STACK_INVALID)
128                 return;
129
130         compiler.add_node(NODE_MAPPING, offset_in, offset_out);
131
132         Transform tfm = compute_transform();
133         compiler.add_node(tfm.x);
134         compiler.add_node(tfm.y);
135         compiler.add_node(tfm.z);
136         compiler.add_node(tfm.w);
137
138         if(use_minmax) {
139                 compiler.add_node(NODE_MIN_MAX, offset_out, offset_out);
140                 compiler.add_node(float3_to_float4(min));
141                 compiler.add_node(float3_to_float4(max));
142         }
143
144         if(type == NORMAL) {
145                 compiler.add_node(NODE_VECTOR_MATH, NODE_VECTOR_MATH_NORMALIZE, offset_out, offset_out);
146                 compiler.add_node(NODE_VECTOR_MATH, SVM_STACK_INVALID, offset_out);
147         }
148 }
149
150 void TextureMapping::compile(OSLCompiler &compiler)
151 {
152         if(!skip()) {
153                 Transform tfm = transform_transpose(compute_transform());
154
155                 compiler.parameter("mapping", tfm);
156                 compiler.parameter("use_mapping", 1);
157         }
158 }
159
160 /* Image Texture */
161
162 static ShaderEnum color_space_init()
163 {
164         ShaderEnum enm;
165
166         enm.insert("None", 0);
167         enm.insert("Color", 1);
168
169         return enm;
170 }
171
172 static ShaderEnum image_projection_init()
173 {
174         ShaderEnum enm;
175
176         enm.insert("Flat", 0);
177         enm.insert("Box", 1);
178
179         return enm;
180 }
181
182 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
183 ShaderEnum ImageTextureNode::projection_enum = image_projection_init();
184
185 ImageTextureNode::ImageTextureNode()
186 : TextureNode("image_texture")
187 {
188         image_manager = NULL;
189         slot = -1;
190         is_float = -1;
191         is_linear = false;
192         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] = (float)sky_state->configs[0][i];
586                 sunsky->config_y[i] = (float)sky_state->configs[1][i];
587                 sunsky->config_z[i] = (float)sky_state->configs[2][i];
588         }
589         sunsky->radiance_x = (float)sky_state->radiances[0];
590         sunsky->radiance_y = (float)sky_state->radiances[1];
591         sunsky->radiance_z = (float)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         add_output("Transparent Depth", SHADER_SOCKET_FLOAT);
2423 }
2424
2425 void LightPathNode::compile(SVMCompiler& compiler)
2426 {
2427         ShaderOutput *out;
2428
2429         out = output("Is Camera Ray");
2430         if(!out->links.empty()) {
2431                 compiler.stack_assign(out);
2432                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2433         }
2434
2435         out = output("Is Shadow Ray");
2436         if(!out->links.empty()) {
2437                 compiler.stack_assign(out);
2438                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2439         }
2440
2441         out = output("Is Diffuse Ray");
2442         if(!out->links.empty()) {
2443                 compiler.stack_assign(out);
2444                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2445         }
2446
2447         out = output("Is Glossy Ray");
2448         if(!out->links.empty()) {
2449                 compiler.stack_assign(out);
2450                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2451         }
2452
2453         out = output("Is Singular Ray");
2454         if(!out->links.empty()) {
2455                 compiler.stack_assign(out);
2456                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2457         }
2458
2459         out = output("Is Reflection Ray");
2460         if(!out->links.empty()) {
2461                 compiler.stack_assign(out);
2462                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2463         }
2464
2465
2466         out = output("Is Transmission Ray");
2467         if(!out->links.empty()) {
2468                 compiler.stack_assign(out);
2469                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2470         }
2471         
2472         out = output("Is Volume Scatter Ray");
2473         if(!out->links.empty()) {
2474                 compiler.stack_assign(out);
2475                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, out->stack_offset);
2476         }
2477
2478         out = output("Ray Length");
2479         if(!out->links.empty()) {
2480                 compiler.stack_assign(out);
2481                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2482         }
2483         
2484         out = output("Ray Depth");
2485         if(!out->links.empty()) {
2486                 compiler.stack_assign(out);
2487                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, out->stack_offset);
2488         }
2489
2490         out = output("Transparent Depth");
2491         if(!out->links.empty()) {
2492                 compiler.stack_assign(out);
2493                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, out->stack_offset);
2494         }
2495 }
2496
2497 void LightPathNode::compile(OSLCompiler& compiler)
2498 {
2499         compiler.add(this, "node_light_path");
2500 }
2501
2502 /* Light Falloff */
2503
2504 LightFalloffNode::LightFalloffNode()
2505 : ShaderNode("light_fallof")
2506 {
2507         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2508         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2509         add_output("Quadratic", SHADER_SOCKET_FLOAT);
2510         add_output("Linear", SHADER_SOCKET_FLOAT);
2511         add_output("Constant", SHADER_SOCKET_FLOAT);
2512 }
2513
2514 void LightFalloffNode::compile(SVMCompiler& compiler)
2515 {
2516         ShaderInput *strength_in = input("Strength");
2517         ShaderInput *smooth_in = input("Smooth");
2518
2519         compiler.stack_assign(strength_in);
2520         compiler.stack_assign(smooth_in);
2521
2522         ShaderOutput *out = output("Quadratic");
2523         if(!out->links.empty()) {
2524                 compiler.stack_assign(out);
2525                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2526                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2527         }
2528
2529         out = output("Linear");
2530         if(!out->links.empty()) {
2531                 compiler.stack_assign(out);
2532                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2533                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2534         }
2535
2536         out = output("Constant");
2537         if(!out->links.empty()) {
2538                 compiler.stack_assign(out);
2539                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2540                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2541         }
2542 }
2543
2544 void LightFalloffNode::compile(OSLCompiler& compiler)
2545 {
2546         compiler.add(this, "node_light_falloff");
2547 }
2548
2549 /* Object Info */
2550
2551 ObjectInfoNode::ObjectInfoNode()
2552 : ShaderNode("object_info")
2553 {
2554         add_output("Location", SHADER_SOCKET_VECTOR);
2555         add_output("Object Index", SHADER_SOCKET_FLOAT);
2556         add_output("Material Index", SHADER_SOCKET_FLOAT);
2557         add_output("Random", SHADER_SOCKET_FLOAT);
2558 }
2559
2560 void ObjectInfoNode::compile(SVMCompiler& compiler)
2561 {
2562         ShaderOutput *out = output("Location");
2563         if(!out->links.empty()) {
2564                 compiler.stack_assign(out);
2565                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2566         }
2567
2568         out = output("Object Index");
2569         if(!out->links.empty()) {
2570                 compiler.stack_assign(out);
2571                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2572         }
2573
2574         out = output("Material Index");
2575         if(!out->links.empty()) {
2576                 compiler.stack_assign(out);
2577                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2578         }
2579
2580         out = output("Random");
2581         if(!out->links.empty()) {
2582                 compiler.stack_assign(out);
2583                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2584         }
2585 }
2586
2587 void ObjectInfoNode::compile(OSLCompiler& compiler)
2588 {
2589         compiler.add(this, "node_object_info");
2590 }
2591
2592 /* Particle Info */
2593
2594 ParticleInfoNode::ParticleInfoNode()
2595 : ShaderNode("particle_info")
2596 {
2597         add_output("Index", SHADER_SOCKET_FLOAT);
2598         add_output("Age", SHADER_SOCKET_FLOAT);
2599         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2600         add_output("Location", SHADER_SOCKET_POINT);
2601 #if 0   /* not yet supported */
2602         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2603 #endif
2604         add_output("Size", SHADER_SOCKET_FLOAT);
2605         add_output("Velocity", SHADER_SOCKET_VECTOR);
2606         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2607 }
2608
2609 void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2610 {
2611         if(!output("Index")->links.empty())
2612                 attributes->add(ATTR_STD_PARTICLE);
2613         if(!output("Age")->links.empty())
2614                 attributes->add(ATTR_STD_PARTICLE);
2615         if(!output("Lifetime")->links.empty())
2616                 attributes->add(ATTR_STD_PARTICLE);
2617         if(!output("Location")->links.empty())
2618                 attributes->add(ATTR_STD_PARTICLE);
2619 #if 0   /* not yet supported */
2620         if(!output("Rotation")->links.empty())
2621                 attributes->add(ATTR_STD_PARTICLE);
2622 #endif
2623         if(!output("Size")->links.empty())
2624                 attributes->add(ATTR_STD_PARTICLE);
2625         if(!output("Velocity")->links.empty())
2626                 attributes->add(ATTR_STD_PARTICLE);
2627         if(!output("Angular Velocity")->links.empty())
2628                 attributes->add(ATTR_STD_PARTICLE);
2629
2630         ShaderNode::attributes(shader, attributes);
2631 }
2632
2633 void ParticleInfoNode::compile(SVMCompiler& compiler)
2634 {
2635         ShaderOutput *out;
2636         
2637         out = output("Index");
2638         if(!out->links.empty()) {
2639                 compiler.stack_assign(out);
2640                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2641         }
2642         
2643         out = output("Age");
2644         if(!out->links.empty()) {
2645                 compiler.stack_assign(out);
2646                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2647         }
2648         
2649         out = output("Lifetime");
2650         if(!out->links.empty()) {
2651                 compiler.stack_assign(out);
2652                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2653         }
2654         
2655         out = output("Location");
2656         if(!out->links.empty()) {
2657                 compiler.stack_assign(out);
2658                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2659         }
2660         
2661         /* quaternion data is not yet supported by Cycles */
2662 #if 0
2663         out = output("Rotation");
2664         if(!out->links.empty()) {
2665                 compiler.stack_assign(out);
2666                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2667         }
2668 #endif
2669         
2670         out = output("Size");
2671         if(!out->links.empty()) {
2672                 compiler.stack_assign(out);
2673                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2674         }
2675         
2676         out = output("Velocity");
2677         if(!out->links.empty()) {
2678                 compiler.stack_assign(out);
2679                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2680         }
2681         
2682         out = output("Angular Velocity");
2683         if(!out->links.empty()) {
2684                 compiler.stack_assign(out);
2685                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2686         }
2687 }
2688
2689 void ParticleInfoNode::compile(OSLCompiler& compiler)
2690 {
2691         compiler.add(this, "node_particle_info");
2692 }
2693
2694 /* Hair Info */
2695
2696 HairInfoNode::HairInfoNode()
2697 : ShaderNode("hair_info")
2698 {
2699         add_output("Is Strand", SHADER_SOCKET_FLOAT);
2700         add_output("Intercept", SHADER_SOCKET_FLOAT);
2701         add_output("Thickness", SHADER_SOCKET_FLOAT);
2702         add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2703         /*output for minimum hair width transparency - deactivated*/
2704         /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2705 }
2706
2707 void HairInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2708 {
2709         if(shader->has_surface) {
2710                 ShaderOutput *intercept_out = output("Intercept");
2711
2712                 if(!intercept_out->links.empty())
2713                         attributes->add(ATTR_STD_CURVE_INTERCEPT);
2714         }
2715
2716         ShaderNode::attributes(shader, attributes);
2717 }
2718
2719 void HairInfoNode::compile(SVMCompiler& compiler)
2720 {
2721         ShaderOutput *out;
2722         
2723         out = output("Is Strand");
2724         if(!out->links.empty()) {
2725                 compiler.stack_assign(out);
2726                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2727         }
2728
2729         out = output("Intercept");
2730         if(!out->links.empty()) {
2731                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2732                 compiler.stack_assign(out);
2733                 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2734         }
2735
2736         out = output("Thickness");
2737         if(!out->links.empty()) {
2738                 compiler.stack_assign(out);
2739                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2740         }
2741
2742         out = output("Tangent Normal");
2743         if(!out->links.empty()) {
2744                 compiler.stack_assign(out);
2745                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2746         }
2747
2748         /*out = output("Fade");
2749         if(!out->links.empty()) {
2750                 compiler.stack_assign(out);
2751                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2752         }*/
2753
2754 }
2755
2756 void HairInfoNode::compile(OSLCompiler& compiler)
2757 {
2758         compiler.add(this, "node_hair_info");
2759 }
2760
2761 /* Value */
2762
2763 ValueNode::ValueNode()
2764 : ShaderNode("value")
2765 {
2766         value = 0.0f;
2767
2768         add_output("Value", SHADER_SOCKET_FLOAT);
2769 }
2770
2771 void ValueNode::compile(SVMCompiler& compiler)
2772 {
2773         ShaderOutput *val_out = output("Value");
2774
2775         compiler.stack_assign(val_out);
2776         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2777 }
2778
2779 void ValueNode::compile(OSLCompiler& compiler)
2780 {
2781         compiler.parameter("value_value", value);
2782         compiler.add(this, "node_value");
2783 }
2784
2785 /* Color */
2786
2787 ColorNode::ColorNode()
2788 : ShaderNode("color")
2789 {
2790         value = make_float3(0.0f, 0.0f, 0.0f);
2791
2792         add_output("Color", SHADER_SOCKET_COLOR);
2793 }
2794
2795 void ColorNode::compile(SVMCompiler& compiler)
2796 {
2797         ShaderOutput *color_out = output("Color");
2798
2799         if(color_out && !color_out->links.empty()) {
2800                 compiler.stack_assign(color_out);
2801                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2802                 compiler.add_node(NODE_VALUE_V, value);
2803         }
2804 }
2805
2806 void ColorNode::compile(OSLCompiler& compiler)
2807 {
2808         compiler.parameter_color("color_value", value);
2809
2810         compiler.add(this, "node_value");
2811 }
2812
2813 /* Add Closure */
2814
2815 AddClosureNode::AddClosureNode()
2816 : ShaderNode("add_closure")
2817 {
2818         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2819         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2820         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2821 }
2822
2823 void AddClosureNode::compile(SVMCompiler& compiler)
2824 {
2825         /* handled in the SVM compiler */
2826 }
2827
2828 void AddClosureNode::compile(OSLCompiler& compiler)
2829 {
2830         compiler.add(this, "node_add_closure");
2831 }
2832
2833 /* Mix Closure */
2834
2835 MixClosureNode::MixClosureNode()
2836 : ShaderNode("mix_closure")
2837 {
2838         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2839         
2840         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2841         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2842         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2843         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2844 }
2845
2846 void MixClosureNode::compile(SVMCompiler& compiler)
2847 {
2848         /* handled in the SVM compiler */
2849 }
2850
2851 void MixClosureNode::compile(OSLCompiler& compiler)
2852 {
2853         compiler.add(this, "node_mix_closure");
2854 }
2855
2856 /* Mix Closure */
2857
2858 MixClosureWeightNode::MixClosureWeightNode()
2859 : ShaderNode("mix_closure_weight")
2860 {
2861         add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2862         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2863         add_output("Weight1", SHADER_SOCKET_FLOAT);
2864         add_output("Weight2", SHADER_SOCKET_FLOAT);
2865 }
2866
2867 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2868 {
2869         ShaderInput *weight_in = input("Weight");
2870         ShaderInput *fac_in = input("Fac");
2871         ShaderOutput *weight1_out = output("Weight1");
2872         ShaderOutput *weight2_out = output("Weight2");
2873
2874         compiler.stack_assign(weight_in);
2875         compiler.stack_assign(fac_in);
2876         compiler.stack_assign(weight1_out);
2877         compiler.stack_assign(weight2_out);
2878
2879         compiler.add_node(NODE_MIX_CLOSURE,
2880                 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2881                         weight1_out->stack_offset, weight2_out->stack_offset));
2882 }
2883
2884 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2885 {
2886         assert(0);
2887 }
2888
2889 /* Invert */
2890
2891 InvertNode::InvertNode()
2892 : ShaderNode("invert")
2893 {
2894         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2895         add_input("Color", SHADER_SOCKET_COLOR);
2896         add_output("Color",  SHADER_SOCKET_COLOR);
2897 }
2898
2899 void InvertNode::compile(SVMCompiler& compiler)
2900 {
2901         ShaderInput *fac_in = input("Fac");
2902         ShaderInput *color_in = input("Color");
2903         ShaderOutput *color_out = output("Color");
2904
2905         compiler.stack_assign(fac_in);
2906         compiler.stack_assign(color_in);
2907         compiler.stack_assign(color_out);
2908
2909         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2910 }
2911
2912 void InvertNode::compile(OSLCompiler& compiler)
2913 {
2914         compiler.add(this, "node_invert");
2915 }
2916
2917 /* Mix */
2918
2919 MixNode::MixNode()
2920 : ShaderNode("mix")
2921 {
2922         type = ustring("Mix");
2923
2924         use_clamp = false;
2925
2926         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2927         add_input("Color1", SHADER_SOCKET_COLOR);
2928         add_input("Color2", SHADER_SOCKET_COLOR);
2929         add_output("Color",  SHADER_SOCKET_COLOR);
2930 }
2931
2932 static ShaderEnum mix_type_init()
2933 {
2934         ShaderEnum enm;
2935
2936         enm.insert("Mix", NODE_MIX_BLEND);
2937         enm.insert("Add", NODE_MIX_ADD);
2938         enm.insert("Multiply", NODE_MIX_MUL);
2939         enm.insert("Screen", NODE_MIX_SCREEN);
2940         enm.insert("Overlay", NODE_MIX_OVERLAY);
2941         enm.insert("Subtract", NODE_MIX_SUB);
2942         enm.insert("Divide", NODE_MIX_DIV);
2943         enm.insert("Difference", NODE_MIX_DIFF);
2944         enm.insert("Darken", NODE_MIX_DARK);
2945         enm.insert("Lighten", NODE_MIX_LIGHT);
2946         enm.insert("Dodge", NODE_MIX_DODGE);
2947         enm.insert("Burn", NODE_MIX_BURN);
2948         enm.insert("Hue", NODE_MIX_HUE);
2949         enm.insert("Saturation", NODE_MIX_SAT);
2950         enm.insert("Value", NODE_MIX_VAL);
2951         enm.insert("Color", NODE_MIX_COLOR);
2952         enm.insert("Soft Light", NODE_MIX_SOFT);
2953         enm.insert("Linear Light", NODE_MIX_LINEAR);
2954
2955         return enm;
2956 }
2957
2958 ShaderEnum MixNode::type_enum = mix_type_init();
2959
2960 void MixNode::compile(SVMCompiler& compiler)
2961 {
2962         ShaderInput *fac_in = input("Fac");
2963         ShaderInput *color1_in = input("Color1");
2964         ShaderInput *color2_in = input("Color2");
2965         ShaderOutput *color_out = output("Color");
2966
2967         compiler.stack_assign(fac_in);
2968         compiler.stack_assign(color1_in);
2969         compiler.stack_assign(color2_in);
2970         compiler.stack_assign(color_out);
2971
2972         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2973         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2974
2975         if(use_clamp) {
2976                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2977                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2978         }
2979 }
2980
2981 void MixNode::compile(OSLCompiler& compiler)
2982 {
2983         compiler.parameter("type", type);
2984         compiler.parameter("Clamp", use_clamp);
2985         compiler.add(this, "node_mix");
2986 }
2987
2988 /* Combine RGB */
2989 CombineRGBNode::CombineRGBNode()
2990 : ShaderNode("combine_rgb")
2991 {
2992         add_input("R", SHADER_SOCKET_FLOAT);
2993         add_input("G", SHADER_SOCKET_FLOAT);
2994         add_input("B", SHADER_SOCKET_FLOAT);
2995         add_output("Image", SHADER_SOCKET_COLOR);
2996 }
2997
2998 void CombineRGBNode::compile(SVMCompiler& compiler)
2999 {
3000         ShaderInput *red_in = input("R");
3001         ShaderInput *green_in = input("G");
3002         ShaderInput *blue_in = input("B");
3003         ShaderOutput *color_out = output("Image");
3004
3005         compiler.stack_assign(color_out);
3006
3007         compiler.stack_assign(red_in);
3008         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
3009
3010         compiler.stack_assign(green_in);
3011         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
3012
3013         compiler.stack_assign(blue_in);
3014         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
3015 }
3016
3017 void CombineRGBNode::compile(OSLCompiler& compiler)
3018 {
3019         compiler.add(this, "node_combine_rgb");
3020 }
3021
3022 /* Combine HSV */
3023 CombineHSVNode::CombineHSVNode()
3024 : ShaderNode("combine_hsv")
3025 {
3026         add_input("H", SHADER_SOCKET_FLOAT);
3027         add_input("S", SHADER_SOCKET_FLOAT);
3028         add_input("V", SHADER_SOCKET_FLOAT);
3029         add_output("Color", SHADER_SOCKET_COLOR);
3030 }
3031
3032 void CombineHSVNode::compile(SVMCompiler& compiler)
3033 {
3034         ShaderInput *hue_in = input("H");
3035         ShaderInput *saturation_in = input("S");
3036         ShaderInput *value_in = input("V");
3037         ShaderOutput *color_out = output("Color");
3038
3039         compiler.stack_assign(color_out);
3040         compiler.stack_assign(hue_in);
3041         compiler.stack_assign(saturation_in);
3042         compiler.stack_assign(value_in);
3043         
3044         compiler.add_node(NODE_COMBINE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3045         compiler.add_node(NODE_COMBINE_HSV, color_out->stack_offset);
3046 }
3047
3048 void CombineHSVNode::compile(OSLCompiler& compiler)
3049 {
3050         compiler.add(this, "node_combine_hsv");
3051 }
3052
3053 /* Gamma */
3054 GammaNode::GammaNode()
3055 : ShaderNode("gamma")
3056 {
3057         add_input("Color", SHADER_SOCKET_COLOR);
3058         add_input("Gamma", SHADER_SOCKET_FLOAT);
3059         add_output("Color", SHADER_SOCKET_COLOR);
3060 }
3061
3062 void GammaNode::compile(SVMCompiler& compiler)
3063 {
3064         ShaderInput *color_in = input("Color");
3065         ShaderInput *gamma_in = input("Gamma");
3066         ShaderOutput *color_out = output("Color");
3067
3068         compiler.stack_assign(color_in);
3069         compiler.stack_assign(gamma_in);
3070         compiler.stack_assign(color_out);
3071
3072         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3073 }
3074
3075 void GammaNode::compile(OSLCompiler& compiler)
3076 {
3077         compiler.add(this, "node_gamma");
3078 }
3079
3080 /* Bright Contrast */
3081 BrightContrastNode::BrightContrastNode()
3082 : ShaderNode("brightness")
3083 {
3084         add_input("Color", SHADER_SOCKET_COLOR);
3085         add_input("Bright", SHADER_SOCKET_FLOAT);
3086         add_input("Contrast", SHADER_SOCKET_FLOAT);
3087         add_output("Color", SHADER_SOCKET_COLOR);
3088 }
3089
3090 void BrightContrastNode::compile(SVMCompiler& compiler)
3091 {
3092         ShaderInput *color_in = input("Color");
3093         ShaderInput *bright_in = input("Bright");
3094         ShaderInput *contrast_in = input("Contrast");
3095         ShaderOutput *color_out = output("Color");
3096
3097         compiler.stack_assign(color_in);
3098         compiler.stack_assign(bright_in);
3099         compiler.stack_assign(contrast_in);
3100         compiler.stack_assign(color_out);
3101
3102         compiler.add_node(NODE_BRIGHTCONTRAST,
3103                 color_in->stack_offset, color_out->stack_offset,
3104                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
3105 }
3106
3107 void BrightContrastNode::compile(OSLCompiler& compiler)
3108 {
3109         compiler.add(this, "node_brightness");
3110 }
3111
3112 /* Separate RGB */
3113 SeparateRGBNode::SeparateRGBNode()
3114 : ShaderNode("separate_rgb")
3115 {
3116         add_input("Image", SHADER_SOCKET_COLOR);
3117         add_output("R", SHADER_SOCKET_FLOAT);
3118         add_output("G", SHADER_SOCKET_FLOAT);
3119         add_output("B", SHADER_SOCKET_FLOAT);
3120 }
3121
3122 void SeparateRGBNode::compile(SVMCompiler& compiler)
3123 {
3124         ShaderInput *color_in = input("Image");
3125         ShaderOutput *red_out = output("R");
3126         ShaderOutput *green_out = output("G");
3127         ShaderOutput *blue_out = output("B");
3128
3129         compiler.stack_assign(color_in);
3130
3131         compiler.stack_assign(red_out);
3132         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
3133
3134         compiler.stack_assign(green_out);
3135         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
3136
3137         compiler.stack_assign(blue_out);
3138         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
3139 }
3140
3141 void SeparateRGBNode::compile(OSLCompiler& compiler)
3142 {
3143         compiler.add(this, "node_separate_rgb");
3144 }
3145
3146 /* Separate HSV */
3147 SeparateHSVNode::SeparateHSVNode()
3148 : ShaderNode("separate_hsv")
3149 {
3150         add_input("Color", SHADER_SOCKET_COLOR);
3151         add_output("H", SHADER_SOCKET_FLOAT);
3152         add_output("S", SHADER_SOCKET_FLOAT);
3153         add_output("V", SHADER_SOCKET_FLOAT);
3154 }
3155
3156 void SeparateHSVNode::compile(SVMCompiler& compiler)
3157 {
3158         ShaderInput *color_in = input("Color");
3159         ShaderOutput *hue_out = output("H");
3160         ShaderOutput *saturation_out = output("S");
3161         ShaderOutput *value_out = output("V");
3162
3163         compiler.stack_assign(color_in);
3164         compiler.stack_assign(hue_out);
3165         compiler.stack_assign(saturation_out);
3166         compiler.stack_assign(value_out);
3167         
3168         compiler.add_node(NODE_SEPARATE_HSV, color_in->stack_offset, hue_out->stack_offset, saturation_out->stack_offset);
3169         compiler.add_node(NODE_SEPARATE_HSV, value_out->stack_offset);
3170
3171 }
3172
3173 void SeparateHSVNode::compile(OSLCompiler& compiler)
3174 {
3175         compiler.add(this, "node_separate_hsv");
3176 }
3177
3178 /* Hue Saturation Value */
3179 HSVNode::HSVNode()
3180 : ShaderNode("hsv")
3181 {
3182         add_input("Hue", SHADER_SOCKET_FLOAT);
3183         add_input("Saturation", SHADER_SOCKET_FLOAT);
3184         add_input("Value", SHADER_SOCKET_FLOAT);
3185         add_input("Fac", SHADER_SOCKET_FLOAT);
3186         add_input("Color", SHADER_SOCKET_COLOR);
3187         add_output("Color", SHADER_SOCKET_COLOR);
3188 }
3189
3190 void HSVNode::compile(SVMCompiler& compiler)
3191 {
3192         ShaderInput *hue_in = input("Hue");
3193         ShaderInput *saturation_in = input("Saturation");
3194         ShaderInput *value_in = input("Value");
3195         ShaderInput *fac_in = input("Fac");
3196         ShaderInput *color_in = input("Color");
3197         ShaderOutput *color_out = output("Color");
3198
3199         compiler.stack_assign(hue_in);
3200         compiler.stack_assign(saturation_in);
3201         compiler.stack_assign(value_in);
3202         compiler.stack_assign(fac_in);
3203         compiler.stack_assign(color_in);
3204         compiler.stack_assign(color_out);
3205
3206         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
3207         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3208 }
3209
3210 void HSVNode::compile(OSLCompiler& compiler)
3211 {
3212         compiler.add(this, "node_hsv");
3213 }
3214
3215 /* Attribute */
3216
3217 AttributeNode::AttributeNode()
3218 : ShaderNode("attribute")
3219 {
3220         attribute = "";
3221
3222         add_output("Color",  SHADER_SOCKET_COLOR);
3223         add_output("Vector",  SHADER_SOCKET_VECTOR);
3224         add_output("Fac",  SHADER_SOCKET_FLOAT);
3225 }
3226
3227 void AttributeNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3228 {
3229         ShaderOutput *color_out = output("Color");
3230         ShaderOutput *vector_out = output("Vector");
3231         ShaderOutput *fac_out = output("Fac");
3232
3233         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty()) {
3234                 AttributeStandard std = Attribute::name_standard(attribute.c_str());
3235
3236                 if(std != ATTR_STD_NONE)
3237                         attributes->add(std);
3238                 else
3239                         attributes->add(attribute);
3240         }
3241
3242         if(shader->has_volume)
3243                 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
3244
3245         ShaderNode::attributes(shader, attributes);
3246 }
3247
3248 void AttributeNode::compile(SVMCompiler& compiler)
3249 {
3250         ShaderOutput *color_out = output("Color");
3251         ShaderOutput *vector_out = output("Vector");
3252         ShaderOutput *fac_out = output("Fac");
3253         NodeType attr_node = NODE_ATTR;
3254         AttributeStandard std = Attribute::name_standard(attribute.c_str());
3255         int attr;
3256
3257         if(std != ATTR_STD_NONE)
3258                 attr = compiler.attribute(std);
3259         else
3260                 attr = compiler.attribute(attribute);
3261
3262         if(bump == SHADER_BUMP_DX)
3263                 attr_node = NODE_ATTR_BUMP_DX;
3264         else if(bump == SHADER_BUMP_DY)
3265                 attr_node = NODE_ATTR_BUMP_DY;
3266
3267         if(!color_out->links.empty() || !vector_out->links.empty()) {
3268                 if(!color_out->links.empty()) {
3269                         compiler.stack_assign(color_out);
3270                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
3271                 }
3272                 if(!vector_out->links.empty()) {
3273                         compiler.stack_assign(vector_out);
3274                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
3275                 }
3276         }
3277
3278         if(!fac_out->links.empty()) {
3279                 compiler.stack_assign(fac_out);
3280                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
3281         }
3282 }
3283
3284 void AttributeNode::compile(OSLCompiler& compiler)
3285 {
3286         if(bump == SHADER_BUMP_DX)
3287                 compiler.parameter("bump_offset", "dx");
3288         else if(bump == SHADER_BUMP_DY)
3289                 compiler.parameter("bump_offset", "dy");
3290         else
3291                 compiler.parameter("bump_offset", "center");
3292         
3293         if(Attribute::name_standard(attribute.c_str()) != ATTR_STD_NONE)
3294                 compiler.parameter("name", (string("geom:") + attribute.c_str()).c_str());
3295         else
3296                 compiler.parameter("name", attribute.c_str());
3297
3298         compiler.add(this, "node_attribute");
3299 }
3300
3301 /* Camera */
3302
3303 CameraNode::CameraNode()
3304 : ShaderNode("camera")
3305 {
3306         add_output("View Vector",  SHADER_SOCKET_VECTOR);
3307         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
3308         add_output("View Distance",  SHADER_SOCKET_FLOAT);
3309 }
3310
3311 void CameraNode::compile(SVMCompiler& compiler)
3312 {
3313         ShaderOutput *vector_out = output("View Vector");
3314         ShaderOutput *z_depth_out = output("View Z Depth");
3315         ShaderOutput *distance_out = output("View Distance");
3316
3317         compiler.stack_assign(vector_out);
3318         compiler.stack_assign(z_depth_out);
3319         compiler.stack_assign(distance_out);
3320         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
3321 }
3322
3323 void CameraNode::compile(OSLCompiler& compiler)
3324 {
3325         compiler.add(this, "node_camera");
3326 }
3327
3328 /* Fresnel */
3329
3330 FresnelNode::FresnelNode()
3331 : ShaderNode("fresnel")
3332 {
3333         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3334         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
3335         add_output("Fac", SHADER_SOCKET_FLOAT);
3336 }
3337
3338 void FresnelNode::compile(SVMCompiler& compiler)
3339 {
3340         ShaderInput *normal_in = input("Normal");
3341         ShaderInput *ior_in = input("IOR");
3342         ShaderOutput *fac_out = output("Fac");
3343
3344         compiler.stack_assign(ior_in);
3345         compiler.stack_assign(fac_out);
3346         
3347         if(normal_in->link)
3348                 compiler.stack_assign(normal_in);
3349         
3350         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));
3351 }
3352
3353 void FresnelNode::compile(OSLCompiler& compiler)
3354 {
3355         compiler.add(this, "node_fresnel");
3356 }
3357
3358 /* Layer Weight */
3359
3360 LayerWeightNode::LayerWeightNode()
3361 : ShaderNode("layer_weight")
3362 {
3363         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3364         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
3365
3366         add_output("Fresnel", SHADER_SOCKET_FLOAT);
3367         add_output("Facing", SHADER_SOCKET_FLOAT);
3368 }
3369
3370 void LayerWeightNode::compile(SVMCompiler& compiler)
3371 {
3372         ShaderInput *normal_in = input("Normal");
3373         ShaderInput *blend_in = input("Blend");
3374
3375         if(normal_in->link)
3376                 compiler.stack_assign(normal_in);
3377
3378         if(blend_in->link)
3379                 compiler.stack_assign(blend_in);
3380
3381         ShaderOutput *fresnel_out = output("Fresnel");
3382         if(!fresnel_out->links.empty()) {
3383                 compiler.stack_assign(fresnel_out);
3384                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3385                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, normal_in->stack_offset, fresnel_out->stack_offset));
3386         }
3387
3388         ShaderOutput *facing_out = output("Facing");
3389         if(!facing_out->links.empty()) {
3390                 compiler.stack_assign(facing_out);
3391                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3392                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, normal_in->stack_offset, facing_out->stack_offset));
3393         }
3394 }
3395
3396 void LayerWeightNode::compile(OSLCompiler& compiler)
3397 {
3398         compiler.add(this, "node_layer_weight");
3399 }
3400
3401 /* Wireframe */
3402
3403 WireframeNode::WireframeNode()
3404 : ShaderNode("wireframe")
3405 {
3406         add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
3407         add_output("Fac", SHADER_SOCKET_FLOAT);
3408         
3409         use_pixel_size = false;
3410 }
3411
3412 void WireframeNode::compile(SVMCompiler& compiler)
3413 {
3414         ShaderInput *size_in = input("Size");
3415         ShaderOutput *fac_out = output("Fac");
3416
3417         compiler.stack_assign(size_in);
3418         compiler.stack_assign(fac_out);
3419         compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
3420 }
3421
3422 void WireframeNode::compile(OSLCompiler& compiler)
3423 {
3424         compiler.parameter("use_pixel_size", use_pixel_size);
3425         compiler.add(this, "node_wireframe");
3426 }
3427
3428 /* Wavelength */
3429
3430 WavelengthNode::WavelengthNode()
3431 : ShaderNode("wavelength")
3432 {
3433         add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
3434         add_output("Color", SHADER_SOCKET_COLOR);
3435 }
3436
3437 void WavelengthNode::compile(SVMCompiler& compiler)
3438 {
3439         ShaderInput *wavelength_in = input("Wavelength");
3440         ShaderOutput *color_out = output("Color");
3441
3442         compiler.stack_assign(wavelength_in);
3443         compiler.stack_assign(color_out);
3444         compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset);
3445 }
3446
3447 void WavelengthNode::compile(OSLCompiler& compiler)
3448 {
3449         compiler.add(this, "node_wavelength");
3450 }
3451
3452 /* Blackbody */
3453
3454 BlackbodyNode::BlackbodyNode()
3455 : ShaderNode("blackbody")
3456 {
3457         add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f);
3458         add_output("Color", SHADER_SOCKET_COLOR);
3459 }
3460
3461 void BlackbodyNode::compile(SVMCompiler& compiler)
3462 {
3463         ShaderInput *temperature_in = input("Temperature");
3464         ShaderOutput *color_out = output("Color");
3465
3466         compiler.stack_assign(temperature_in);
3467         compiler.stack_assign(color_out);
3468         compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset);
3469 }
3470
3471 void BlackbodyNode::compile(OSLCompiler& compiler)
3472 {
3473         compiler.add(this, "node_blackbody");
3474 }
3475
3476 /* Output */
3477
3478 OutputNode::OutputNode()
3479 : ShaderNode("output")
3480 {
3481         add_input("Surface", SHADER_SOCKET_CLOSURE);
3482         add_input("Volume", SHADER_SOCKET_CLOSURE);
3483         add_input("Displacement", SHADER_SOCKET_FLOAT);
3484         add_input("Normal", SHADER_SOCKET_NORMAL);
3485 }
3486
3487 void OutputNode::compile(SVMCompiler& compiler)
3488 {
3489         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
3490                 ShaderInput *displacement_in = input("Displacement");
3491
3492                 if(displacement_in->link) {
3493                         compiler.stack_assign(displacement_in);
3494                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
3495                 }
3496         }
3497 }
3498
3499 void OutputNode::compile(OSLCompiler& compiler)
3500 {
3501         if(compiler.output_type() == SHADER_TYPE_SURFACE)
3502                 compiler.add(this, "node_output_surface");
3503         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3504                 compiler.add(this, "node_output_volume");
3505         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3506                 compiler.add(this, "node_output_displacement");
3507 }
3508
3509 /* Math */
3510
3511 MathNode::MathNode()
3512 : ShaderNode("math")
3513 {
3514         type = ustring("Add");
3515
3516         use_clamp = false;
3517
3518         add_input("Value1", SHADER_SOCKET_FLOAT);
3519         add_input("Value2", SHADER_SOCKET_FLOAT);
3520         add_output("Value",  SHADER_SOCKET_FLOAT);
3521 }
3522
3523 static ShaderEnum math_type_init()
3524 {
3525         ShaderEnum enm;
3526
3527         enm.insert("Add", NODE_MATH_ADD);
3528         enm.insert("Subtract", NODE_MATH_SUBTRACT);
3529         enm.insert("Multiply", NODE_MATH_MULTIPLY);
3530         enm.insert("Divide", NODE_MATH_DIVIDE);
3531         enm.insert("Sine", NODE_MATH_SINE);
3532         enm.insert("Cosine", NODE_MATH_COSINE);
3533         enm.insert("Tangent", NODE_MATH_TANGENT);
3534         enm.insert("Arcsine", NODE_MATH_ARCSINE);
3535         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3536         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3537         enm.insert("Power", NODE_MATH_POWER);
3538         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3539         enm.insert("Minimum", NODE_MATH_MINIMUM);
3540         enm.insert("Maximum", NODE_MATH_MAXIMUM);
3541         enm.insert("Round", NODE_MATH_ROUND);
3542         enm.insert("Less Than", NODE_MATH_LESS_THAN);
3543         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3544         enm.insert("Modulo", NODE_MATH_MODULO);
3545
3546         return enm;
3547 }
3548
3549 ShaderEnum MathNode::type_enum = math_type_init();
3550
3551 void MathNode::compile(SVMCompiler& compiler)
3552 {
3553         ShaderInput *value1_in = input("Value1");
3554         ShaderInput *value2_in = input("Value2");
3555         ShaderOutput *value_out = output("Value");
3556
3557         compiler.stack_assign(value1_in);
3558         compiler.stack_assign(value2_in);
3559         compiler.stack_assign(value_out);
3560
3561         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3562         compiler.add_node(NODE_MATH, value_out->stack_offset);
3563
3564         if(use_clamp) {
3565                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3566                 compiler.add_node(NODE_MATH, value_out->stack_offset);
3567         }
3568 }
3569
3570 void MathNode::compile(OSLCompiler& compiler)
3571 {
3572         compiler.parameter("type", type);
3573         compiler.parameter("Clamp", use_clamp);
3574         compiler.add(this, "node_math");
3575 }
3576
3577 /* VectorMath */
3578
3579 VectorMathNode::VectorMathNode()
3580 : ShaderNode("vector_math")
3581 {
3582         type = ustring("Add");
3583
3584         add_input("Vector1", SHADER_SOCKET_VECTOR);
3585         add_input("Vector2", SHADER_SOCKET_VECTOR);
3586         add_output("Value",  SHADER_SOCKET_FLOAT);
3587         add_output("Vector",  SHADER_SOCKET_VECTOR);
3588 }
3589
3590 static ShaderEnum vector_math_type_init()
3591 {
3592         ShaderEnum enm;
3593
3594         enm.insert("Add", NODE_VECTOR_MATH_ADD);
3595         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3596         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3597         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3598         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3599         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3600
3601         return enm;
3602 }
3603
3604 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3605
3606 void VectorMathNode::compile(SVMCompiler& compiler)
3607 {
3608         ShaderInput *vector1_in = input("Vector1");
3609         ShaderInput *vector2_in = input("Vector2");
3610         ShaderOutput *value_out = output("Value");
3611         ShaderOutput *vector_out = output("Vector");
3612
3613         compiler.stack_assign(vector1_in);
3614         compiler.stack_assign(vector2_in);
3615         compiler.stack_assign(value_out);
3616         compiler.stack_assign(vector_out);
3617
3618         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
3619         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
3620 }
3621
3622 void VectorMathNode::compile(OSLCompiler& compiler)
3623 {
3624         compiler.parameter("type", type);
3625         compiler.add(this, "node_vector_math");
3626 }
3627
3628 /* VectorTransform */
3629
3630 VectorTransformNode::VectorTransformNode()
3631 : ShaderNode("vector_transform")
3632 {
3633         type = ustring("Vector");
3634         convert_from = ustring("world");
3635         convert_to = ustring("object");
3636
3637         add_input("Vector", SHADER_SOCKET_VECTOR);
3638         add_output("Vector",  SHADER_SOCKET_VECTOR);
3639 }
3640
3641 static ShaderEnum vector_transform_type_init()
3642 {
3643         ShaderEnum enm;
3644
3645         enm.insert("Vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
3646         enm.insert("Point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
3647         enm.insert("Normal", NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
3648
3649         return enm;
3650 }
3651
3652 static ShaderEnum vector_transform_convert_space_init()
3653 {
3654         ShaderEnum enm;
3655
3656         enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
3657         enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
3658         enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA);
3659
3660         return enm;
3661 }
3662
3663 ShaderEnum VectorTransformNode::type_enum = vector_transform_type_init();
3664 ShaderEnum VectorTransformNode::convert_space_enum = vector_transform_convert_space_init();
3665
3666 void VectorTransformNode::compile(SVMCompiler& compiler)
3667 {
3668         ShaderInput *vector_in = input("Vector");
3669         ShaderOutput *vector_out = output("Vector");
3670
3671         compiler.stack_assign(vector_in);
3672         compiler.stack_assign(vector_out);
3673
3674         compiler.add_node(NODE_VECTOR_TRANSFORM,
3675                 compiler.encode_uchar4(type_enum[type], convert_space_enum[convert_from], convert_space_enum[convert_to]),
3676                 compiler.encode_uchar4(vector_in->stack_offset, vector_out->stack_offset));
3677 }
3678
3679 void VectorTransformNode::compile(OSLCompiler& compiler)
3680 {
3681         compiler.parameter("type", type);
3682         compiler.parameter("convert_from", convert_from);
3683         compiler.parameter("convert_to", convert_to);
3684         compiler.add(this, "node_vector_transform");
3685 }
3686
3687 /* BumpNode */
3688
3689 BumpNode::BumpNode()
3690 : ShaderNode("bump")
3691 {
3692         invert = false;
3693
3694         /* this input is used by the user, but after graph transform it is no longer
3695          * used and moved to sampler center/x/y instead */
3696         add_input("Height", SHADER_SOCKET_FLOAT);
3697
3698         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
3699         add_input("SampleX", SHADER_SOCKET_FLOAT);
3700         add_input("SampleY", SHADER_SOCKET_FLOAT);
3701         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
3702         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3703         add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
3704
3705         add_output("Normal", SHADER_SOCKET_NORMAL);
3706 }
3707
3708 void BumpNode::compile(SVMCompiler& compiler)
3709 {
3710         ShaderInput *center_in = input("SampleCenter");
3711         ShaderInput *dx_in = input("SampleX");
3712         ShaderInput *dy_in = input("SampleY");
3713         ShaderInput *normal_in = input("Normal");
3714         ShaderInput *strength_in = input("Strength");
3715         ShaderInput *distance_in = input("Distance");
3716         ShaderOutput *normal_out = output("Normal");
3717
3718         compiler.stack_assign(center_in);
3719         compiler.stack_assign(dx_in);
3720         compiler.stack_assign(dy_in);
3721         compiler.stack_assign(strength_in);
3722         compiler.stack_assign(distance_in);
3723         compiler.stack_assign(normal_out);
3724
3725         if(normal_in->link)
3726                 compiler.stack_assign(normal_in);
3727         
3728         /* pack all parameters in the node */
3729         compiler.add_node(NODE_SET_BUMP,
3730                 compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
3731                 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
3732                         dy_in->stack_offset, strength_in->stack_offset),
3733                 normal_out->stack_offset);
3734 }
3735
3736 void BumpNode::compile(OSLCompiler& compiler)
3737 {
3738         compiler.parameter("invert", invert);
3739         compiler.add(this, "node_bump");