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