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