Cycles Volume Render: work on nodes and closures.
[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(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 (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(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(AttributeRequestSet *attributes)
371 {
372 #ifdef WITH_PTEX
373         if (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(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(AttributeRequestSet *attributes)
1537 {
1538         ShaderInput *tangent_in = input("Tangent");
1539
1540         if(!tangent_in->link)
1541                 attributes->add(ATTR_STD_GENERATED);
1542
1543         ShaderNode::attributes(attributes);
1544 }
1545
1546 void WardBsdfNode::compile(SVMCompiler& compiler)
1547 {
1548         BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1549 }
1550
1551 void WardBsdfNode::compile(OSLCompiler& compiler)
1552 {
1553         compiler.add(this, "node_ward_bsdf");
1554 }
1555
1556 /* Glossy BSDF Closure */
1557
1558 static ShaderEnum glossy_distribution_init()
1559 {
1560         ShaderEnum enm;
1561
1562         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1563         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1564         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1565
1566         return enm;
1567 }
1568
1569 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1570
1571 GlossyBsdfNode::GlossyBsdfNode()
1572 {
1573         distribution = ustring("Beckmann");
1574
1575         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1576 }
1577
1578 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1579 {
1580         closure = (ClosureType)distribution_enum[distribution];
1581
1582         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1583                 BsdfNode::compile(compiler, NULL, NULL);
1584         else
1585                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1586 }
1587
1588 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1589 {
1590         compiler.parameter("distribution", distribution);
1591         compiler.add(this, "node_glossy_bsdf");
1592 }
1593
1594 /* Glass BSDF Closure */
1595
1596 static ShaderEnum glass_distribution_init()
1597 {
1598         ShaderEnum enm;
1599
1600         enm.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1601         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1602         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1603
1604         return enm;
1605 }
1606
1607 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1608
1609 GlassBsdfNode::GlassBsdfNode()
1610 {
1611         distribution = ustring("Sharp");
1612
1613         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1614         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1615 }
1616
1617 void GlassBsdfNode::compile(SVMCompiler& compiler)
1618 {
1619         closure = (ClosureType)distribution_enum[distribution];
1620
1621         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
1622                 BsdfNode::compile(compiler, NULL, input("IOR"));
1623         else
1624                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1625 }
1626
1627 void GlassBsdfNode::compile(OSLCompiler& compiler)
1628 {
1629         compiler.parameter("distribution", distribution);
1630         compiler.add(this, "node_glass_bsdf");
1631 }
1632
1633 /* Refraction BSDF Closure */
1634
1635 static ShaderEnum refraction_distribution_init()
1636 {
1637         ShaderEnum enm;
1638
1639         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1640         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1641         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1642
1643         return enm;
1644 }
1645
1646 ShaderEnum RefractionBsdfNode::distribution_enum = refraction_distribution_init();
1647
1648 RefractionBsdfNode::RefractionBsdfNode()
1649 {
1650         distribution = ustring("Sharp");
1651
1652         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1653         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1654 }
1655
1656 void RefractionBsdfNode::compile(SVMCompiler& compiler)
1657 {
1658         closure = (ClosureType)distribution_enum[distribution];
1659
1660         if(closure == CLOSURE_BSDF_REFRACTION_ID)
1661                 BsdfNode::compile(compiler, NULL, input("IOR"));
1662         else
1663                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1664 }
1665
1666 void RefractionBsdfNode::compile(OSLCompiler& compiler)
1667 {
1668         compiler.parameter("distribution", distribution);
1669         compiler.add(this, "node_refraction_bsdf");
1670 }
1671
1672 /* Toon BSDF Closure */
1673
1674 static ShaderEnum toon_component_init()
1675 {
1676         ShaderEnum enm;
1677
1678         enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
1679         enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
1680
1681         return enm;
1682 }
1683
1684 ShaderEnum ToonBsdfNode::component_enum = toon_component_init();
1685
1686 ToonBsdfNode::ToonBsdfNode()
1687 {
1688         component = ustring("Diffuse");
1689
1690         add_input("Size", SHADER_SOCKET_FLOAT, 0.5f);
1691         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1692 }
1693
1694 void ToonBsdfNode::compile(SVMCompiler& compiler)
1695 {
1696         closure = (ClosureType)component_enum[component];
1697         
1698         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
1699 }
1700
1701 void ToonBsdfNode::compile(OSLCompiler& compiler)
1702 {
1703         compiler.parameter("component", component);
1704         compiler.add(this, "node_toon_bsdf");
1705 }
1706
1707 /* Velvet BSDF Closure */
1708
1709 VelvetBsdfNode::VelvetBsdfNode()
1710 {
1711         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1712
1713         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1714 }
1715
1716 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1717 {
1718         BsdfNode::compile(compiler, input("Sigma"), NULL);
1719 }
1720
1721 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1722 {
1723         compiler.add(this, "node_velvet_bsdf");
1724 }
1725
1726 /* Diffuse BSDF Closure */
1727
1728 DiffuseBsdfNode::DiffuseBsdfNode()
1729 {
1730         closure = CLOSURE_BSDF_DIFFUSE_ID;
1731         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1732 }
1733
1734 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1735 {
1736         BsdfNode::compile(compiler, input("Roughness"), NULL);
1737 }
1738
1739 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1740 {
1741         compiler.add(this, "node_diffuse_bsdf");
1742 }
1743
1744 /* Translucent BSDF Closure */
1745
1746 TranslucentBsdfNode::TranslucentBsdfNode()
1747 {
1748         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1749 }
1750
1751 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1752 {
1753         BsdfNode::compile(compiler, NULL, NULL);
1754 }
1755
1756 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1757 {
1758         compiler.add(this, "node_translucent_bsdf");
1759 }
1760
1761 /* Transparent BSDF Closure */
1762
1763 TransparentBsdfNode::TransparentBsdfNode()
1764 {
1765         name = "transparent";
1766         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1767 }
1768
1769 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1770 {
1771         BsdfNode::compile(compiler, NULL, NULL);
1772 }
1773
1774 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1775 {
1776         compiler.add(this, "node_transparent_bsdf");
1777 }
1778
1779 /* Subsurface Scattering Closure */
1780
1781 static ShaderEnum subsurface_falloff_init()
1782 {
1783         ShaderEnum enm;
1784
1785         enm.insert("Cubic", CLOSURE_BSSRDF_CUBIC_ID);
1786         enm.insert("Gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
1787
1788         return enm;
1789 }
1790
1791 ShaderEnum SubsurfaceScatteringNode::falloff_enum = subsurface_falloff_init();
1792
1793 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
1794 : BsdfNode(true)
1795 {
1796         name = "subsurface_scattering";
1797         closure = CLOSURE_BSSRDF_CUBIC_ID;
1798
1799         add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
1800         add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
1801         add_input("Sharpness", SHADER_SOCKET_FLOAT, 0.0f);
1802         add_input("Texture Blur", SHADER_SOCKET_FLOAT, 1.0f);
1803 }
1804
1805 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
1806 {
1807         BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
1808 }
1809
1810 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
1811 {
1812         compiler.parameter("Falloff", falloff_enum[closure]);
1813         compiler.add(this, "node_subsurface_scattering");
1814 }
1815
1816 bool SubsurfaceScatteringNode::has_bssrdf_bump()
1817 {
1818         /* detect if anything is plugged into the normal input besides the default */
1819         ShaderInput *normal_in = input("Normal");
1820         return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
1821 }
1822
1823 /* Emissive Closure */
1824
1825 EmissionNode::EmissionNode()
1826 : ShaderNode("emission")
1827 {
1828         total_power = false;
1829
1830         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1831         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1832         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1833
1834         add_output("Emission", SHADER_SOCKET_CLOSURE);
1835 }
1836
1837 void EmissionNode::compile(SVMCompiler& compiler)
1838 {
1839         ShaderInput *color_in = input("Color");
1840         ShaderInput *strength_in = input("Strength");
1841
1842         if(color_in->link || strength_in->link) {
1843                 compiler.stack_assign(color_in);
1844                 compiler.stack_assign(strength_in);
1845                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1846         }
1847         else if(total_power)
1848                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1849         else
1850                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1851
1852         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1853 }
1854
1855 void EmissionNode::compile(OSLCompiler& compiler)
1856 {
1857         compiler.parameter("TotalPower", (total_power)? 1: 0);
1858         compiler.add(this, "node_emission");
1859 }
1860
1861 /* Background Closure */
1862
1863 BackgroundNode::BackgroundNode()
1864 : ShaderNode("background")
1865 {
1866         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1867         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1868         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1869
1870         add_output("Background", SHADER_SOCKET_CLOSURE);
1871 }
1872
1873 void BackgroundNode::compile(SVMCompiler& compiler)
1874 {
1875         ShaderInput *color_in = input("Color");
1876         ShaderInput *strength_in = input("Strength");
1877
1878         if(color_in->link || strength_in->link) {
1879                 compiler.stack_assign(color_in);
1880                 compiler.stack_assign(strength_in);
1881                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1882         }
1883         else
1884                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1885
1886         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1887 }
1888
1889 void BackgroundNode::compile(OSLCompiler& compiler)
1890 {
1891         compiler.add(this, "node_background");
1892 }
1893
1894 /* Holdout Closure */
1895
1896 HoldoutNode::HoldoutNode()
1897 : ShaderNode("holdout")
1898 {
1899         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1900         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1901
1902         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1903 }
1904
1905 void HoldoutNode::compile(SVMCompiler& compiler)
1906 {
1907         float3 value = make_float3(1.0f, 1.0f, 1.0f);
1908
1909         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
1910         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1911 }
1912
1913 void HoldoutNode::compile(OSLCompiler& compiler)
1914 {
1915         compiler.add(this, "node_holdout");
1916 }
1917
1918 /* Ambient Occlusion */
1919
1920 AmbientOcclusionNode::AmbientOcclusionNode()
1921 : ShaderNode("ambient_occlusion")
1922 {
1923         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1924         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1925         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1926
1927         add_output("AO", SHADER_SOCKET_CLOSURE);
1928 }
1929
1930 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
1931 {
1932         ShaderInput *color_in = input("Color");
1933
1934         if(color_in->link) {
1935                 compiler.stack_assign(color_in);
1936                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1937         }
1938         else
1939                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1940
1941         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
1942 }
1943
1944 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
1945 {
1946         compiler.add(this, "node_ambient_occlusion");
1947 }
1948
1949 /* Volume Closure */
1950
1951 VolumeNode::VolumeNode()
1952 : ShaderNode("volume")
1953 {
1954         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
1955
1956         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1957         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1958         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1959
1960         add_output("Volume", SHADER_SOCKET_CLOSURE);
1961 }
1962
1963 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1964 {
1965         ShaderInput *color_in = input("Color");
1966
1967         if(color_in->link) {
1968                 compiler.stack_assign(color_in);
1969                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1970         }
1971         else
1972                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1973         
1974         if(param1)
1975                 compiler.stack_assign(param1);
1976         if(param2)
1977                 compiler.stack_assign(param2);
1978
1979         compiler.add_node(NODE_CLOSURE_VOLUME,
1980                 compiler.encode_uchar4(closure,
1981                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1982                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1983                         compiler.closure_mix_weight_offset()),
1984                 __float_as_int((param1)? param1->value.x: 0.0f),
1985                 __float_as_int((param2)? param2->value.x: 0.0f));
1986 }
1987
1988 void VolumeNode::compile(SVMCompiler& compiler)
1989 {
1990         compile(compiler, NULL, NULL);
1991 }
1992
1993 void VolumeNode::compile(OSLCompiler& compiler)
1994 {
1995         assert(0);
1996 }
1997
1998 /* Absorption Volume Closure */
1999
2000 AbsorptionVolumeNode::AbsorptionVolumeNode()
2001 {
2002         closure = CLOSURE_VOLUME_ABSORPTION_ID;
2003 }
2004
2005 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2006 {
2007         VolumeNode::compile(compiler, input("Density"), NULL);
2008 }
2009
2010 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2011 {
2012         compiler.add(this, "node_absorption_volume");
2013 }
2014
2015 /* Scatter Volume Closure */
2016
2017 ScatterVolumeNode::ScatterVolumeNode()
2018 {
2019         closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2020         
2021         add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.0f);
2022 }
2023
2024 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2025 {
2026         VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2027 }
2028
2029 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2030 {
2031         compiler.add(this, "node_scatter_volume");
2032 }
2033
2034 /* Hair BSDF Closure */
2035
2036 static ShaderEnum hair_component_init()
2037 {
2038         ShaderEnum enm;
2039
2040         enm.insert("Reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2041         enm.insert("Transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2042         
2043
2044         return enm;
2045 }
2046
2047 ShaderEnum HairBsdfNode::component_enum = hair_component_init();
2048
2049 HairBsdfNode::HairBsdfNode()
2050 {
2051         component = ustring("Reflection");
2052
2053         add_input("Offset", SHADER_SOCKET_FLOAT);
2054         add_input("RoughnessU", SHADER_SOCKET_FLOAT);
2055         add_input("RoughnessV", SHADER_SOCKET_FLOAT);
2056
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
2074 /* Geometry */
2075
2076 GeometryNode::GeometryNode()
2077 : ShaderNode("geometry")
2078 {
2079         special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2080
2081         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2082         add_output("Position", SHADER_SOCKET_POINT);
2083         add_output("Normal", SHADER_SOCKET_NORMAL);
2084         add_output("Tangent", SHADER_SOCKET_NORMAL);
2085         add_output("True Normal", SHADER_SOCKET_NORMAL);
2086         add_output("Incoming", SHADER_SOCKET_VECTOR);
2087         add_output("Parametric", SHADER_SOCKET_POINT);
2088         add_output("Backfacing", SHADER_SOCKET_FLOAT);
2089 }
2090
2091 void GeometryNode::attributes(AttributeRequestSet *attributes)
2092 {
2093         if(!output("Tangent")->links.empty())
2094                 attributes->add(ATTR_STD_GENERATED);
2095
2096         ShaderNode::attributes(attributes);
2097 }
2098
2099 void GeometryNode::compile(SVMCompiler& compiler)
2100 {
2101         ShaderOutput *out;
2102         NodeType geom_node = NODE_GEOMETRY;
2103
2104         if(bump == SHADER_BUMP_DX)
2105                 geom_node = NODE_GEOMETRY_BUMP_DX;
2106         else if(bump == SHADER_BUMP_DY)
2107                 geom_node = NODE_GEOMETRY_BUMP_DY;
2108         
2109         out = output("Position");
2110         if(!out->links.empty()) {
2111                 compiler.stack_assign(out);
2112                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2113         }
2114
2115         out = output("Normal");
2116         if(!out->links.empty()) {
2117                 compiler.stack_assign(out);
2118                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
2119         }
2120
2121         out = output("Tangent");
2122         if(!out->links.empty()) {
2123                 compiler.stack_assign(out);
2124                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
2125         }
2126
2127         out = output("True Normal");
2128         if(!out->links.empty()) {
2129                 compiler.stack_assign(out);
2130                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
2131         }
2132
2133         out = output("Incoming");
2134         if(!out->links.empty()) {
2135                 compiler.stack_assign(out);
2136                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2137         }
2138
2139         out = output("Parametric");
2140         if(!out->links.empty()) {
2141                 compiler.stack_assign(out);
2142                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
2143         }
2144
2145         out = output("Backfacing");
2146         if(!out->links.empty()) {
2147                 compiler.stack_assign(out);
2148                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
2149         }
2150 }
2151
2152 void GeometryNode::compile(OSLCompiler& compiler)
2153 {
2154         if(bump == SHADER_BUMP_DX)
2155                 compiler.parameter("bump_offset", "dx");
2156         else if(bump == SHADER_BUMP_DY)
2157                 compiler.parameter("bump_offset", "dy");
2158         else
2159                 compiler.parameter("bump_offset", "center");
2160
2161         compiler.add(this, "node_geometry");
2162 }
2163
2164 /* TextureCoordinate */
2165
2166 TextureCoordinateNode::TextureCoordinateNode()
2167 : ShaderNode("texture_coordinate")
2168 {
2169         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2170         add_output("Generated", SHADER_SOCKET_POINT);
2171         add_output("Normal", SHADER_SOCKET_NORMAL);
2172         add_output("UV", SHADER_SOCKET_POINT);
2173         add_output("Object", SHADER_SOCKET_POINT);
2174         add_output("Camera", SHADER_SOCKET_POINT);
2175         add_output("Window", SHADER_SOCKET_POINT);
2176         add_output("Reflection", SHADER_SOCKET_NORMAL);
2177
2178         from_dupli = false;
2179 }
2180
2181 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
2182 {
2183         if(!from_dupli) {
2184                 if(!output("Generated")->links.empty())
2185                         attributes->add(ATTR_STD_GENERATED);
2186                 if(!output("UV")->links.empty())
2187                         attributes->add(ATTR_STD_UV);
2188         }
2189
2190         ShaderNode::attributes(attributes);
2191 }
2192
2193 void TextureCoordinateNode::compile(SVMCompiler& compiler)
2194 {
2195         ShaderOutput *out;
2196         NodeType texco_node = NODE_TEX_COORD;
2197         NodeType attr_node = NODE_ATTR;
2198         NodeType geom_node = NODE_GEOMETRY;
2199
2200         if(bump == SHADER_BUMP_DX) {
2201                 texco_node = NODE_TEX_COORD_BUMP_DX;
2202                 attr_node = NODE_ATTR_BUMP_DX;
2203                 geom_node = NODE_GEOMETRY_BUMP_DX;
2204         }
2205         else if(bump == SHADER_BUMP_DY) {
2206                 texco_node = NODE_TEX_COORD_BUMP_DY;
2207                 attr_node = NODE_ATTR_BUMP_DY;
2208                 geom_node = NODE_GEOMETRY_BUMP_DY;
2209         }
2210         
2211         out = output("Generated");
2212         if(!out->links.empty()) {
2213                 if(compiler.background) {
2214                         compiler.stack_assign(out);
2215                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2216                 }
2217                 else {
2218                         if(from_dupli) {
2219                                 compiler.stack_assign(out);
2220                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
2221                         }
2222                         else {
2223                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
2224                                 compiler.stack_assign(out);
2225                                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2226                         }
2227                 }
2228         }
2229
2230         out = output("Normal");
2231         if(!out->links.empty()) {
2232                 compiler.stack_assign(out);
2233                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
2234         }
2235
2236         out = output("UV");
2237         if(!out->links.empty()) {
2238                 if(from_dupli) {
2239                         compiler.stack_assign(out);
2240                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
2241                 }
2242                 else {
2243                         int attr = compiler.attribute(ATTR_STD_UV);
2244                         compiler.stack_assign(out);
2245                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2246                 }
2247         }
2248
2249         out = output("Object");
2250         if(!out->links.empty()) {
2251                 compiler.stack_assign(out);
2252                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
2253         }
2254
2255         out = output("Camera");
2256         if(!out->links.empty()) {
2257                 compiler.stack_assign(out);
2258                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
2259         }
2260
2261         out = output("Window");
2262         if(!out->links.empty()) {
2263                 compiler.stack_assign(out);
2264                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
2265         }
2266
2267         out = output("Reflection");
2268         if(!out->links.empty()) {
2269                 if(compiler.background) {
2270                         compiler.stack_assign(out);
2271                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2272                 }
2273                 else {
2274                         compiler.stack_assign(out);
2275                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
2276                 }
2277         }
2278 }
2279
2280 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2281 {
2282         if(bump == SHADER_BUMP_DX)
2283                 compiler.parameter("bump_offset", "dx");
2284         else if(bump == SHADER_BUMP_DY)
2285                 compiler.parameter("bump_offset", "dy");
2286         else
2287                 compiler.parameter("bump_offset", "center");
2288         
2289         if(compiler.background)
2290                 compiler.parameter("is_background", true);
2291         
2292         compiler.parameter("from_dupli", from_dupli);
2293
2294         compiler.add(this, "node_texture_coordinate");
2295 }
2296
2297 /* Light Path */
2298
2299 LightPathNode::LightPathNode()
2300 : ShaderNode("light_path")
2301 {
2302         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
2303         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
2304         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
2305         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
2306         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
2307         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
2308         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
2309         add_output("Is Volume Scatter Ray", SHADER_SOCKET_FLOAT);
2310         add_output("Ray Length", SHADER_SOCKET_FLOAT);
2311         add_output("Ray Depth", SHADER_SOCKET_FLOAT);
2312 }
2313
2314 void LightPathNode::compile(SVMCompiler& compiler)
2315 {
2316         ShaderOutput *out;
2317
2318         out = output("Is Camera Ray");
2319         if(!out->links.empty()) {
2320                 compiler.stack_assign(out);
2321                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2322         }
2323
2324         out = output("Is Shadow Ray");
2325         if(!out->links.empty()) {
2326                 compiler.stack_assign(out);
2327                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2328         }
2329
2330         out = output("Is Diffuse Ray");
2331         if(!out->links.empty()) {
2332                 compiler.stack_assign(out);
2333                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2334         }
2335
2336         out = output("Is Glossy Ray");
2337         if(!out->links.empty()) {
2338                 compiler.stack_assign(out);
2339                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2340         }
2341
2342         out = output("Is Singular Ray");
2343         if(!out->links.empty()) {
2344                 compiler.stack_assign(out);
2345                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2346         }
2347
2348         out = output("Is Reflection Ray");
2349         if(!out->links.empty()) {
2350                 compiler.stack_assign(out);
2351                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2352         }
2353
2354
2355         out = output("Is Transmission Ray");
2356         if(!out->links.empty()) {
2357                 compiler.stack_assign(out);
2358                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2359         }
2360         
2361         out = output("Is Volume Scatter Ray");
2362         if(!out->links.empty()) {
2363                 compiler.stack_assign(out);
2364                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, out->stack_offset);
2365         }
2366
2367         out = output("Ray Length");
2368         if(!out->links.empty()) {
2369                 compiler.stack_assign(out);
2370                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2371         }
2372         
2373         out = output("Ray Depth");
2374         if(!out->links.empty()) {
2375                 compiler.stack_assign(out);
2376                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, out->stack_offset);
2377         }
2378
2379 }
2380
2381 void LightPathNode::compile(OSLCompiler& compiler)
2382 {
2383         compiler.add(this, "node_light_path");
2384 }
2385
2386 /* Light Falloff */
2387
2388 LightFalloffNode::LightFalloffNode()
2389 : ShaderNode("light_fallof")
2390 {
2391         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2392         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2393         add_output("Quadratic", SHADER_SOCKET_FLOAT);
2394         add_output("Linear", SHADER_SOCKET_FLOAT);
2395         add_output("Constant", SHADER_SOCKET_FLOAT);
2396 }
2397
2398 void LightFalloffNode::compile(SVMCompiler& compiler)
2399 {
2400         ShaderInput *strength_in = input("Strength");
2401         ShaderInput *smooth_in = input("Smooth");
2402
2403         compiler.stack_assign(strength_in);
2404         compiler.stack_assign(smooth_in);
2405
2406         ShaderOutput *out = output("Quadratic");
2407         if(!out->links.empty()) {
2408                 compiler.stack_assign(out);
2409                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2410                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2411         }
2412
2413         out = output("Linear");
2414         if(!out->links.empty()) {
2415                 compiler.stack_assign(out);
2416                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2417                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2418         }
2419
2420         out = output("Constant");
2421         if(!out->links.empty()) {
2422                 compiler.stack_assign(out);
2423                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2424                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2425         }
2426 }
2427
2428 void LightFalloffNode::compile(OSLCompiler& compiler)
2429 {
2430         compiler.add(this, "node_light_falloff");
2431 }
2432
2433 /* Object Info */
2434
2435 ObjectInfoNode::ObjectInfoNode()
2436 : ShaderNode("object_info")
2437 {
2438         add_output("Location", SHADER_SOCKET_VECTOR);
2439         add_output("Object Index", SHADER_SOCKET_FLOAT);
2440         add_output("Material Index", SHADER_SOCKET_FLOAT);
2441         add_output("Random", SHADER_SOCKET_FLOAT);
2442 }
2443
2444 void ObjectInfoNode::compile(SVMCompiler& compiler)
2445 {
2446         ShaderOutput *out = output("Location");
2447         if(!out->links.empty()) {
2448                 compiler.stack_assign(out);
2449                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2450         }
2451
2452         out = output("Object Index");
2453         if(!out->links.empty()) {
2454                 compiler.stack_assign(out);
2455                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2456         }
2457
2458         out = output("Material Index");
2459         if(!out->links.empty()) {
2460                 compiler.stack_assign(out);
2461                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2462         }
2463
2464         out = output("Random");
2465         if(!out->links.empty()) {
2466                 compiler.stack_assign(out);
2467                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2468         }
2469 }
2470
2471 void ObjectInfoNode::compile(OSLCompiler& compiler)
2472 {
2473         compiler.add(this, "node_object_info");
2474 }
2475
2476 /* Particle Info */
2477
2478 ParticleInfoNode::ParticleInfoNode()
2479 : ShaderNode("particle_info")
2480 {
2481         add_output("Index", SHADER_SOCKET_FLOAT);
2482         add_output("Age", SHADER_SOCKET_FLOAT);
2483         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2484         add_output("Location", SHADER_SOCKET_POINT);
2485 #if 0   /* not yet supported */
2486         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2487 #endif
2488         add_output("Size", SHADER_SOCKET_FLOAT);
2489         add_output("Velocity", SHADER_SOCKET_VECTOR);
2490         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2491 }
2492
2493 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
2494 {
2495         if(!output("Index")->links.empty())
2496                 attributes->add(ATTR_STD_PARTICLE);
2497         if(!output("Age")->links.empty())
2498                 attributes->add(ATTR_STD_PARTICLE);
2499         if(!output("Lifetime")->links.empty())
2500                 attributes->add(ATTR_STD_PARTICLE);
2501         if(!output("Location")->links.empty())
2502                 attributes->add(ATTR_STD_PARTICLE);
2503 #if 0   /* not yet supported */
2504         if(!output("Rotation")->links.empty())
2505                 attributes->add(ATTR_STD_PARTICLE);
2506 #endif
2507         if(!output("Size")->links.empty())
2508                 attributes->add(ATTR_STD_PARTICLE);
2509         if(!output("Velocity")->links.empty())
2510                 attributes->add(ATTR_STD_PARTICLE);
2511         if(!output("Angular Velocity")->links.empty())
2512                 attributes->add(ATTR_STD_PARTICLE);
2513
2514         ShaderNode::attributes(attributes);
2515 }
2516
2517 void ParticleInfoNode::compile(SVMCompiler& compiler)
2518 {
2519         ShaderOutput *out;
2520         
2521         out = output("Index");
2522         if(!out->links.empty()) {
2523                 compiler.stack_assign(out);
2524                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2525         }
2526         
2527         out = output("Age");
2528         if(!out->links.empty()) {
2529                 compiler.stack_assign(out);
2530                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2531         }
2532         
2533         out = output("Lifetime");
2534         if(!out->links.empty()) {
2535                 compiler.stack_assign(out);
2536                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2537         }
2538         
2539         out = output("Location");
2540         if(!out->links.empty()) {
2541                 compiler.stack_assign(out);
2542                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2543         }
2544         
2545         /* quaternion data is not yet supported by Cycles */
2546 #if 0
2547         out = output("Rotation");
2548         if(!out->links.empty()) {
2549                 compiler.stack_assign(out);
2550                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2551         }
2552 #endif
2553         
2554         out = output("Size");
2555         if(!out->links.empty()) {
2556                 compiler.stack_assign(out);
2557                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2558         }
2559         
2560         out = output("Velocity");
2561         if(!out->links.empty()) {
2562                 compiler.stack_assign(out);
2563                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2564         }
2565         
2566         out = output("Angular Velocity");
2567         if(!out->links.empty()) {
2568                 compiler.stack_assign(out);
2569                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2570         }
2571 }
2572
2573 void ParticleInfoNode::compile(OSLCompiler& compiler)
2574 {
2575         compiler.add(this, "node_particle_info");
2576 }
2577
2578 /* Hair Info */
2579
2580 HairInfoNode::HairInfoNode()
2581 : ShaderNode("hair_info")
2582 {
2583         add_output("Is Strand", SHADER_SOCKET_FLOAT);
2584         add_output("Intercept", SHADER_SOCKET_FLOAT);
2585         add_output("Thickness", SHADER_SOCKET_FLOAT);
2586         add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2587         /*output for minimum hair width transparency - deactivated*/
2588         /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2589 }
2590
2591 void HairInfoNode::attributes(AttributeRequestSet *attributes)
2592 {
2593         ShaderOutput *intercept_out = output("Intercept");
2594
2595         if(!intercept_out->links.empty())
2596                 attributes->add(ATTR_STD_CURVE_INTERCEPT);
2597         
2598         ShaderNode::attributes(attributes);
2599 }
2600
2601 void HairInfoNode::compile(SVMCompiler& compiler)
2602 {
2603         ShaderOutput *out;
2604         
2605         out = output("Is Strand");
2606         if(!out->links.empty()) {
2607                 compiler.stack_assign(out);
2608                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2609         }
2610
2611         out = output("Intercept");
2612         if(!out->links.empty()) {
2613                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2614                 compiler.stack_assign(out);
2615                 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2616         }
2617
2618         out = output("Thickness");
2619         if(!out->links.empty()) {
2620                 compiler.stack_assign(out);
2621                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2622         }
2623
2624         out = output("Tangent Normal");
2625         if(!out->links.empty()) {
2626                 compiler.stack_assign(out);
2627                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2628         }
2629
2630         /*out = output("Fade");
2631         if(!out->links.empty()) {
2632                 compiler.stack_assign(out);
2633                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2634         }*/
2635
2636 }
2637
2638 void HairInfoNode::compile(OSLCompiler& compiler)
2639 {
2640         compiler.add(this, "node_hair_info");
2641 }
2642
2643 /* Value */
2644
2645 ValueNode::ValueNode()
2646 : ShaderNode("value")
2647 {
2648         value = 0.0f;
2649
2650         add_output("Value", SHADER_SOCKET_FLOAT);
2651 }
2652
2653 void ValueNode::compile(SVMCompiler& compiler)
2654 {
2655         ShaderOutput *val_out = output("Value");
2656
2657         compiler.stack_assign(val_out);
2658         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2659 }
2660
2661 void ValueNode::compile(OSLCompiler& compiler)
2662 {
2663         compiler.parameter("value_value", value);
2664         compiler.add(this, "node_value");
2665 }
2666
2667 /* Color */
2668
2669 ColorNode::ColorNode()
2670 : ShaderNode("color")
2671 {
2672         value = make_float3(0.0f, 0.0f, 0.0f);
2673
2674         add_output("Color", SHADER_SOCKET_COLOR);
2675 }
2676
2677 void ColorNode::compile(SVMCompiler& compiler)
2678 {
2679         ShaderOutput *color_out = output("Color");
2680
2681         if(color_out && !color_out->links.empty()) {
2682                 compiler.stack_assign(color_out);
2683                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2684                 compiler.add_node(NODE_VALUE_V, value);
2685         }
2686 }
2687
2688 void ColorNode::compile(OSLCompiler& compiler)
2689 {
2690         compiler.parameter_color("color_value", value);
2691
2692         compiler.add(this, "node_value");
2693 }
2694
2695 /* Add Closure */
2696
2697 AddClosureNode::AddClosureNode()
2698 : ShaderNode("add_closure")
2699 {
2700         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2701         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2702         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2703 }
2704
2705 void AddClosureNode::compile(SVMCompiler& compiler)
2706 {
2707         /* handled in the SVM compiler */
2708 }
2709
2710 void AddClosureNode::compile(OSLCompiler& compiler)
2711 {
2712         compiler.add(this, "node_add_closure");
2713 }
2714
2715 /* Mix Closure */
2716
2717 MixClosureNode::MixClosureNode()
2718 : ShaderNode("mix_closure")
2719 {
2720         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2721         
2722         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2723         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2724         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2725         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2726 }
2727
2728 void MixClosureNode::compile(SVMCompiler& compiler)
2729 {
2730         /* handled in the SVM compiler */
2731 }
2732
2733 void MixClosureNode::compile(OSLCompiler& compiler)
2734 {
2735         compiler.add(this, "node_mix_closure");
2736 }
2737
2738 /* Mix Closure */
2739
2740 MixClosureWeightNode::MixClosureWeightNode()
2741 : ShaderNode("mix_closure_weight")
2742 {
2743         add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2744         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2745         add_output("Weight1", SHADER_SOCKET_FLOAT);
2746         add_output("Weight2", SHADER_SOCKET_FLOAT);
2747 }
2748
2749 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2750 {
2751         ShaderInput *weight_in = input("Weight");
2752         ShaderInput *fac_in = input("Fac");
2753         ShaderOutput *weight1_out = output("Weight1");
2754         ShaderOutput *weight2_out = output("Weight2");
2755
2756         compiler.stack_assign(weight_in);
2757         compiler.stack_assign(fac_in);
2758         compiler.stack_assign(weight1_out);
2759         compiler.stack_assign(weight2_out);
2760
2761         compiler.add_node(NODE_MIX_CLOSURE,
2762                 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2763                         weight1_out->stack_offset, weight2_out->stack_offset));
2764 }
2765
2766 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2767 {
2768         assert(0);
2769 }
2770
2771 /* Invert */
2772
2773 InvertNode::InvertNode()
2774 : ShaderNode("invert")
2775 {
2776         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2777         add_input("Color", SHADER_SOCKET_COLOR);
2778         add_output("Color",  SHADER_SOCKET_COLOR);
2779 }
2780
2781 void InvertNode::compile(SVMCompiler& compiler)
2782 {
2783         ShaderInput *fac_in = input("Fac");
2784         ShaderInput *color_in = input("Color");
2785         ShaderOutput *color_out = output("Color");
2786
2787         compiler.stack_assign(fac_in);
2788         compiler.stack_assign(color_in);
2789         compiler.stack_assign(color_out);
2790
2791         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2792 }
2793
2794 void InvertNode::compile(OSLCompiler& compiler)
2795 {
2796         compiler.add(this, "node_invert");
2797 }
2798
2799 /* Mix */
2800
2801 MixNode::MixNode()
2802 : ShaderNode("mix")
2803 {
2804         type = ustring("Mix");
2805
2806         use_clamp = false;
2807
2808         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2809         add_input("Color1", SHADER_SOCKET_COLOR);
2810         add_input("Color2", SHADER_SOCKET_COLOR);
2811         add_output("Color",  SHADER_SOCKET_COLOR);
2812 }
2813
2814 static ShaderEnum mix_type_init()
2815 {
2816         ShaderEnum enm;
2817
2818         enm.insert("Mix", NODE_MIX_BLEND);
2819         enm.insert("Add", NODE_MIX_ADD);
2820         enm.insert("Multiply", NODE_MIX_MUL);
2821         enm.insert("Screen", NODE_MIX_SCREEN);
2822         enm.insert("Overlay", NODE_MIX_OVERLAY);
2823         enm.insert("Subtract", NODE_MIX_SUB);
2824         enm.insert("Divide", NODE_MIX_DIV);
2825         enm.insert("Difference", NODE_MIX_DIFF);
2826         enm.insert("Darken", NODE_MIX_DARK);
2827         enm.insert("Lighten", NODE_MIX_LIGHT);
2828         enm.insert("Dodge", NODE_MIX_DODGE);
2829         enm.insert("Burn", NODE_MIX_BURN);
2830         enm.insert("Hue", NODE_MIX_HUE);
2831         enm.insert("Saturation", NODE_MIX_SAT);
2832         enm.insert("Value", NODE_MIX_VAL);
2833         enm.insert("Color", NODE_MIX_COLOR);
2834         enm.insert("Soft Light", NODE_MIX_SOFT);
2835         enm.insert("Linear Light", NODE_MIX_LINEAR);
2836
2837         return enm;
2838 }
2839
2840 ShaderEnum MixNode::type_enum = mix_type_init();
2841
2842 void MixNode::compile(SVMCompiler& compiler)
2843 {
2844         ShaderInput *fac_in = input("Fac");
2845         ShaderInput *color1_in = input("Color1");
2846         ShaderInput *color2_in = input("Color2");
2847         ShaderOutput *color_out = output("Color");
2848
2849         compiler.stack_assign(fac_in);
2850         compiler.stack_assign(color1_in);
2851         compiler.stack_assign(color2_in);
2852         compiler.stack_assign(color_out);
2853
2854         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2855         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2856
2857         if(use_clamp) {
2858                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2859                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2860         }
2861 }
2862
2863 void MixNode::compile(OSLCompiler& compiler)
2864 {
2865         compiler.parameter("type", type);
2866         compiler.parameter("Clamp", use_clamp);
2867         compiler.add(this, "node_mix");
2868 }
2869
2870 /* Combine RGB */
2871 CombineRGBNode::CombineRGBNode()
2872 : ShaderNode("combine_rgb")
2873 {
2874         add_input("R", SHADER_SOCKET_FLOAT);
2875         add_input("G", SHADER_SOCKET_FLOAT);
2876         add_input("B", SHADER_SOCKET_FLOAT);
2877         add_output("Image", SHADER_SOCKET_COLOR);
2878 }
2879
2880 void CombineRGBNode::compile(SVMCompiler& compiler)
2881 {
2882         ShaderInput *red_in = input("R");
2883         ShaderInput *green_in = input("G");
2884         ShaderInput *blue_in = input("B");
2885         ShaderOutput *color_out = output("Image");
2886
2887         compiler.stack_assign(color_out);
2888
2889         compiler.stack_assign(red_in);
2890         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2891
2892         compiler.stack_assign(green_in);
2893         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2894
2895         compiler.stack_assign(blue_in);
2896         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2897 }
2898
2899 void CombineRGBNode::compile(OSLCompiler& compiler)
2900 {
2901         compiler.add(this, "node_combine_rgb");
2902 }
2903
2904 /* Combine HSV */
2905 CombineHSVNode::CombineHSVNode()
2906 : ShaderNode("combine_hsv")
2907 {
2908         add_input("H", SHADER_SOCKET_FLOAT);
2909         add_input("S", SHADER_SOCKET_FLOAT);
2910         add_input("V", SHADER_SOCKET_FLOAT);
2911         add_output("Color", SHADER_SOCKET_COLOR);
2912 }
2913
2914 void CombineHSVNode::compile(SVMCompiler& compiler)
2915 {
2916         ShaderInput *hue_in = input("H");
2917         ShaderInput *saturation_in = input("S");
2918         ShaderInput *value_in = input("V");
2919         ShaderOutput *color_out = output("Color");
2920
2921         compiler.stack_assign(color_out);
2922         compiler.stack_assign(hue_in);
2923         compiler.stack_assign(saturation_in);
2924         compiler.stack_assign(value_in);
2925         
2926         compiler.add_node(NODE_COMBINE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2927         compiler.add_node(NODE_COMBINE_HSV, color_out->stack_offset);
2928 }
2929
2930 void CombineHSVNode::compile(OSLCompiler& compiler)
2931 {
2932         compiler.add(this, "node_combine_hsv");
2933 }
2934
2935 /* Gamma */
2936 GammaNode::GammaNode()
2937 : ShaderNode("gamma")
2938 {
2939         add_input("Color", SHADER_SOCKET_COLOR);
2940         add_input("Gamma", SHADER_SOCKET_FLOAT);
2941         add_output("Color", SHADER_SOCKET_COLOR);
2942 }
2943
2944 void GammaNode::compile(SVMCompiler& compiler)
2945 {
2946         ShaderInput *color_in = input("Color");
2947         ShaderInput *gamma_in = input("Gamma");
2948         ShaderOutput *color_out = output("Color");
2949
2950         compiler.stack_assign(color_in);
2951         compiler.stack_assign(gamma_in);
2952         compiler.stack_assign(color_out);
2953
2954         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2955 }
2956
2957 void GammaNode::compile(OSLCompiler& compiler)
2958 {
2959         compiler.add(this, "node_gamma");
2960 }
2961
2962 /* Bright Contrast */
2963 BrightContrastNode::BrightContrastNode()
2964 : ShaderNode("brightness")
2965 {
2966         add_input("Color", SHADER_SOCKET_COLOR);
2967         add_input("Bright", SHADER_SOCKET_FLOAT);
2968         add_input("Contrast", SHADER_SOCKET_FLOAT);
2969         add_output("Color", SHADER_SOCKET_COLOR);
2970 }
2971
2972 void BrightContrastNode::compile(SVMCompiler& compiler)
2973 {
2974         ShaderInput *color_in = input("Color");
2975         ShaderInput *bright_in = input("Bright");
2976         ShaderInput *contrast_in = input("Contrast");
2977         ShaderOutput *color_out = output("Color");
2978
2979         compiler.stack_assign(color_in);
2980         compiler.stack_assign(bright_in);
2981         compiler.stack_assign(contrast_in);
2982         compiler.stack_assign(color_out);
2983
2984         compiler.add_node(NODE_BRIGHTCONTRAST,
2985                 color_in->stack_offset, color_out->stack_offset,
2986                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2987 }
2988
2989 void BrightContrastNode::compile(OSLCompiler& compiler)
2990 {
2991         compiler.add(this, "node_brightness");
2992 }
2993
2994 /* Separate RGB */
2995 SeparateRGBNode::SeparateRGBNode()
2996 : ShaderNode("separate_rgb")
2997 {
2998         add_input("Image", SHADER_SOCKET_COLOR);
2999         add_output("R", SHADER_SOCKET_FLOAT);
3000         add_output("G", SHADER_SOCKET_FLOAT);
3001         add_output("B", SHADER_SOCKET_FLOAT);
3002 }
3003
3004 void SeparateRGBNode::compile(SVMCompiler& compiler)
3005 {
3006         ShaderInput *color_in = input("Image");
3007         ShaderOutput *red_out = output("R");
3008         ShaderOutput *green_out = output("G");
3009         ShaderOutput *blue_out = output("B");
3010
3011         compiler.stack_assign(color_in);
3012
3013         compiler.stack_assign(red_out);
3014         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
3015
3016         compiler.stack_assign(green_out);
3017         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
3018
3019         compiler.stack_assign(blue_out);
3020         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
3021 }
3022
3023 void SeparateRGBNode::compile(OSLCompiler& compiler)
3024 {
3025         compiler.add(this, "node_separate_rgb");
3026 }
3027
3028 /* Separate HSV */
3029 SeparateHSVNode::SeparateHSVNode()
3030 : ShaderNode("separate_hsv")
3031 {
3032         add_input("Color", SHADER_SOCKET_COLOR);
3033         add_output("H", SHADER_SOCKET_FLOAT);
3034         add_output("S", SHADER_SOCKET_FLOAT);
3035         add_output("V", SHADER_SOCKET_FLOAT);
3036 }
3037
3038 void SeparateHSVNode::compile(SVMCompiler& compiler)
3039 {
3040         ShaderInput *color_in = input("Color");
3041         ShaderOutput *hue_out = output("H");
3042         ShaderOutput *saturation_out = output("S");
3043         ShaderOutput *value_out = output("V");
3044
3045         compiler.stack_assign(color_in);
3046         compiler.stack_assign(hue_out);
3047         compiler.stack_assign(saturation_out);
3048         compiler.stack_assign(value_out);
3049         
3050         compiler.add_node(NODE_SEPARATE_HSV, color_in->stack_offset, hue_out->stack_offset, saturation_out->stack_offset);
3051         compiler.add_node(NODE_SEPARATE_HSV, value_out->stack_offset);
3052
3053 }
3054
3055 void SeparateHSVNode::compile(OSLCompiler& compiler)
3056 {
3057         compiler.add(this, "node_separate_hsv");
3058 }
3059
3060 /* Hue Saturation Value */
3061 HSVNode::HSVNode()
3062 : ShaderNode("hsv")
3063 {
3064         add_input("Hue", SHADER_SOCKET_FLOAT);
3065         add_input("Saturation", SHADER_SOCKET_FLOAT);
3066         add_input("Value", SHADER_SOCKET_FLOAT);
3067         add_input("Fac", SHADER_SOCKET_FLOAT);
3068         add_input("Color", SHADER_SOCKET_COLOR);
3069         add_output("Color", SHADER_SOCKET_COLOR);
3070 }
3071
3072 void HSVNode::compile(SVMCompiler& compiler)
3073 {
3074         ShaderInput *hue_in = input("Hue");
3075         ShaderInput *saturation_in = input("Saturation");
3076         ShaderInput *value_in = input("Value");
3077         ShaderInput *fac_in = input("Fac");
3078         ShaderInput *color_in = input("Color");
3079         ShaderOutput *color_out = output("Color");
3080
3081         compiler.stack_assign(hue_in);
3082         compiler.stack_assign(saturation_in);
3083         compiler.stack_assign(value_in);
3084         compiler.stack_assign(fac_in);
3085         compiler.stack_assign(color_in);
3086         compiler.stack_assign(color_out);
3087
3088         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
3089         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3090 }
3091
3092 void HSVNode::compile(OSLCompiler& compiler)
3093 {
3094         compiler.add(this, "node_hsv");
3095 }
3096
3097 /* Attribute */
3098
3099 AttributeNode::AttributeNode()
3100 : ShaderNode("attribute")
3101 {
3102         attribute = "";
3103
3104         add_output("Color",  SHADER_SOCKET_COLOR);
3105         add_output("Vector",  SHADER_SOCKET_VECTOR);
3106         add_output("Fac",  SHADER_SOCKET_FLOAT);
3107 }
3108
3109 void AttributeNode::attributes(AttributeRequestSet *attributes)
3110 {
3111         ShaderOutput *color_out = output("Color");
3112         ShaderOutput *vector_out = output("Vector");
3113         ShaderOutput *fac_out = output("Fac");
3114
3115         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
3116                 attributes->add(attribute);
3117         
3118         ShaderNode::attributes(attributes);
3119 }
3120
3121 void AttributeNode::compile(SVMCompiler& compiler)
3122 {
3123         ShaderOutput *color_out = output("Color");
3124         ShaderOutput *vector_out = output("Vector");
3125         ShaderOutput *fac_out = output("Fac");
3126         NodeType attr_node = NODE_ATTR;
3127
3128         if(bump == SHADER_BUMP_DX)
3129                 attr_node = NODE_ATTR_BUMP_DX;
3130         else if(bump == SHADER_BUMP_DY)
3131                 attr_node = NODE_ATTR_BUMP_DY;
3132
3133         if(!color_out->links.empty() || !vector_out->links.empty()) {
3134                 int attr = compiler.attribute(attribute);
3135
3136                 if(!color_out->links.empty()) {
3137                         compiler.stack_assign(color_out);
3138                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
3139                 }
3140                 if(!vector_out->links.empty()) {
3141                         compiler.stack_assign(vector_out);
3142                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
3143                 }
3144         }
3145
3146         if(!fac_out->links.empty()) {
3147                 int attr = compiler.attribute(attribute);
3148
3149                 compiler.stack_assign(fac_out);
3150                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
3151         }
3152 }
3153
3154 void AttributeNode::compile(OSLCompiler& compiler)
3155 {
3156         if(bump == SHADER_BUMP_DX)
3157                 compiler.parameter("bump_offset", "dx");
3158         else if(bump == SHADER_BUMP_DY)
3159                 compiler.parameter("bump_offset", "dy");
3160         else
3161                 compiler.parameter("bump_offset", "center");
3162
3163         compiler.parameter("name", attribute.c_str());
3164         compiler.add(this, "node_attribute");
3165 }
3166
3167 /* Camera */
3168
3169 CameraNode::CameraNode()
3170 : ShaderNode("camera")
3171 {
3172         add_output("View Vector",  SHADER_SOCKET_VECTOR);
3173         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
3174         add_output("View Distance",  SHADER_SOCKET_FLOAT);
3175 }
3176
3177 void CameraNode::compile(SVMCompiler& compiler)
3178 {
3179         ShaderOutput *vector_out = output("View Vector");
3180         ShaderOutput *z_depth_out = output("View Z Depth");
3181         ShaderOutput *distance_out = output("View Distance");
3182
3183         compiler.stack_assign(vector_out);
3184         compiler.stack_assign(z_depth_out);
3185         compiler.stack_assign(distance_out);
3186         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
3187 }
3188
3189 void CameraNode::compile(OSLCompiler& compiler)
3190 {
3191         compiler.add(this, "node_camera");
3192 }
3193
3194 /* Fresnel */
3195
3196 FresnelNode::FresnelNode()
3197 : ShaderNode("fresnel")
3198 {
3199         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3200         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
3201         add_output("Fac", SHADER_SOCKET_FLOAT);
3202 }
3203
3204 void FresnelNode::compile(SVMCompiler& compiler)
3205 {
3206         ShaderInput *normal_in = input("Normal");
3207         ShaderInput *ior_in = input("IOR");
3208         ShaderOutput *fac_out = output("Fac");
3209
3210         compiler.stack_assign(ior_in);
3211         compiler.stack_assign(fac_out);
3212         
3213         if(normal_in->link)
3214                 compiler.stack_assign(normal_in);
3215         
3216         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));
3217 }
3218
3219 void FresnelNode::compile(OSLCompiler& compiler)
3220 {
3221         compiler.add(this, "node_fresnel");
3222 }
3223
3224 /* Layer Weight */
3225
3226 LayerWeightNode::LayerWeightNode()
3227 : ShaderNode("layer_weight")
3228 {
3229         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3230         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
3231
3232         add_output("Fresnel", SHADER_SOCKET_FLOAT);
3233         add_output("Facing", SHADER_SOCKET_FLOAT);
3234 }
3235
3236 void LayerWeightNode::compile(SVMCompiler& compiler)
3237 {
3238         ShaderInput *normal_in = input("Normal");
3239         ShaderInput *blend_in = input("Blend");
3240
3241         if(normal_in->link)
3242                 compiler.stack_assign(normal_in);
3243
3244         if(blend_in->link)
3245                 compiler.stack_assign(blend_in);
3246
3247         ShaderOutput *fresnel_out = output("Fresnel");
3248         if(!fresnel_out->links.empty()) {
3249                 compiler.stack_assign(fresnel_out);
3250                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3251                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, normal_in->stack_offset, fresnel_out->stack_offset));
3252         }
3253
3254         ShaderOutput *facing_out = output("Facing");
3255         if(!facing_out->links.empty()) {
3256                 compiler.stack_assign(facing_out);
3257                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3258                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, normal_in->stack_offset, facing_out->stack_offset));
3259         }
3260 }
3261
3262 void LayerWeightNode::compile(OSLCompiler& compiler)
3263 {
3264         compiler.add(this, "node_layer_weight");
3265 }
3266
3267 /* Wireframe */
3268
3269 WireframeNode::WireframeNode()
3270 : ShaderNode("wireframe")
3271 {
3272         add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
3273         add_output("Fac", SHADER_SOCKET_FLOAT);
3274         
3275         use_pixel_size = false;
3276 }
3277
3278 void WireframeNode::compile(SVMCompiler& compiler)
3279 {
3280         ShaderInput *size_in = input("Size");
3281         ShaderOutput *fac_out = output("Fac");
3282
3283         compiler.stack_assign(size_in);
3284         compiler.stack_assign(fac_out);
3285         compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
3286 }
3287
3288 void WireframeNode::compile(OSLCompiler& compiler)
3289 {
3290         compiler.parameter("use_pixel_size", use_pixel_size);
3291         compiler.add(this, "node_wireframe");
3292 }
3293
3294 /* Wavelength */
3295
3296 WavelengthNode::WavelengthNode()
3297 : ShaderNode("wavelength")
3298 {
3299         add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
3300         add_output("Color", SHADER_SOCKET_COLOR);
3301 }
3302
3303 void WavelengthNode::compile(SVMCompiler& compiler)
3304 {
3305         ShaderInput *wavelength_in = input("Wavelength");
3306         ShaderOutput *color_out = output("Color");
3307
3308         compiler.stack_assign(wavelength_in);
3309         compiler.stack_assign(color_out);
3310         compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset);
3311 }
3312
3313 void WavelengthNode::compile(OSLCompiler& compiler)
3314 {
3315         compiler.add(this, "node_wavelength");
3316 }
3317
3318 /* Blackbody */
3319
3320 BlackbodyNode::BlackbodyNode()
3321 : ShaderNode("blackbody")
3322 {
3323         add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f);
3324         add_output("Color", SHADER_SOCKET_COLOR);
3325 }
3326
3327 void BlackbodyNode::compile(SVMCompiler& compiler)
3328 {
3329         ShaderInput *temperature_in = input("Temperature");
3330         ShaderOutput *color_out = output("Color");
3331
3332         compiler.stack_assign(temperature_in);
3333         compiler.stack_assign(color_out);
3334         compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset);
3335 }
3336
3337 void BlackbodyNode::compile(OSLCompiler& compiler)
3338 {
3339         compiler.add(this, "node_blackbody");
3340 }
3341
3342 /* Output */
3343
3344 OutputNode::OutputNode()
3345 : ShaderNode("output")
3346 {
3347         add_input("Surface", SHADER_SOCKET_CLOSURE);
3348         add_input("Volume", SHADER_SOCKET_CLOSURE);
3349         add_input("Displacement", SHADER_SOCKET_FLOAT);
3350         add_input("Normal", SHADER_SOCKET_NORMAL);
3351 }
3352
3353 void OutputNode::compile(SVMCompiler& compiler)
3354 {
3355         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
3356                 ShaderInput *displacement_in = input("Displacement");
3357
3358                 if(displacement_in->link) {
3359                         compiler.stack_assign(displacement_in);
3360                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
3361                 }
3362         }
3363 }
3364
3365 void OutputNode::compile(OSLCompiler& compiler)
3366 {
3367         if(compiler.output_type() == SHADER_TYPE_SURFACE)
3368                 compiler.add(this, "node_output_surface");
3369         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3370                 compiler.add(this, "node_output_volume");
3371         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3372                 compiler.add(this, "node_output_displacement");
3373 }
3374
3375 /* Math */
3376
3377 MathNode::MathNode()
3378 : ShaderNode("math")
3379 {
3380         type = ustring("Add");
3381
3382         use_clamp = false;
3383
3384         add_input("Value1", SHADER_SOCKET_FLOAT);
3385         add_input("Value2", SHADER_SOCKET_FLOAT);
3386         add_output("Value",  SHADER_SOCKET_FLOAT);
3387 }
3388
3389 static ShaderEnum math_type_init()
3390 {
3391         ShaderEnum enm;
3392
3393         enm.insert("Add", NODE_MATH_ADD);
3394         enm.insert("Subtract", NODE_MATH_SUBTRACT);
3395         enm.insert("Multiply", NODE_MATH_MULTIPLY);
3396         enm.insert("Divide", NODE_MATH_DIVIDE);
3397         enm.insert("Sine", NODE_MATH_SINE);
3398         enm.insert("Cosine", NODE_MATH_COSINE);
3399         enm.insert("Tangent", NODE_MATH_TANGENT);
3400         enm.insert("Arcsine", NODE_MATH_ARCSINE);
3401         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3402         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3403         enm.insert("Power", NODE_MATH_POWER);
3404         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3405         enm.insert("Minimum", NODE_MATH_MINIMUM);
3406         enm.insert("Maximum", NODE_MATH_MAXIMUM);
3407         enm.insert("Round", NODE_MATH_ROUND);
3408         enm.insert("Less Than", NODE_MATH_LESS_THAN);
3409         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3410         enm.insert("Modulo", NODE_MATH_MODULO);
3411
3412         return enm;
3413 }
3414
3415 ShaderEnum MathNode::type_enum = math_type_init();
3416
3417 void MathNode::compile(SVMCompiler& compiler)
3418 {
3419         ShaderInput *value1_in = input("Value1");
3420         ShaderInput *value2_in = input("Value2");
3421         ShaderOutput *value_out = output("Value");
3422
3423         compiler.stack_assign(value1_in);
3424         compiler.stack_assign(value2_in);
3425         compiler.stack_assign(value_out);
3426
3427         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3428         compiler.add_node(NODE_MATH, value_out->stack_offset);
3429
3430         if(use_clamp) {
3431                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3432                 compiler.add_node(NODE_MATH, value_out->stack_offset);
3433         }
3434 }
3435
3436 void MathNode::compile(OSLCompiler& compiler)
3437 {
3438         compiler.parameter("type", type);
3439         compiler.parameter("Clamp", use_clamp);
3440         compiler.add(this, "node_math");
3441 }
3442
3443 /* VectorMath */
3444
3445 VectorMathNode::VectorMathNode()
3446 : ShaderNode("vector_math")
3447 {
3448         type = ustring("Add");
3449
3450         add_input("Vector1", SHADER_SOCKET_VECTOR);
3451         add_input("Vector2", SHADER_SOCKET_VECTOR);
3452         add_output("Value",  SHADER_SOCKET_FLOAT);
3453         add_output("Vector",  SHADER_SOCKET_VECTOR);
3454 }
3455
3456 static ShaderEnum vector_math_type_init()
3457 {
3458         ShaderEnum enm;
3459
3460         enm.insert("Add", NODE_VECTOR_MATH_ADD);
3461         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3462         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3463         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3464         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3465         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3466
3467         return enm;
3468 }
3469
3470 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3471
3472 void VectorMathNode::compile(SVMCompiler& compiler)
3473 {
3474         ShaderInput *vector1_in = input("Vector1");
3475         ShaderInput *vector2_in = input("Vector2");
3476         ShaderOutput *value_out = output("Value");
3477         ShaderOutput *vector_out = output("Vector");
3478
3479         compiler.stack_assign(vector1_in);
3480         compiler.stack_assign(vector2_in);
3481         compiler.stack_assign(value_out);
3482         compiler.stack_assign(vector_out);
3483
3484         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
3485         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
3486 }
3487
3488 void VectorMathNode::compile(OSLCompiler& compiler)
3489 {
3490         compiler.parameter("type", type);
3491         compiler.add(this, "node_vector_math");
3492 }
3493
3494 /* VectorTransform */
3495
3496 VectorTransformNode::VectorTransformNode()
3497 : ShaderNode("vector_transform")
3498 {
3499         type = ustring("Vector");
3500         convert_from = ustring("world");
3501         convert_to = ustring("object");
3502
3503         add_input("Vector", SHADER_SOCKET_VECTOR);
3504         add_output("Vector",  SHADER_SOCKET_VECTOR);
3505 }
3506
3507 static ShaderEnum vector_transform_type_init()
3508 {
3509         ShaderEnum enm;
3510
3511         enm.insert("Vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
3512         enm.insert("Point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
3513         enm.insert("Normal", NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
3514
3515         return enm;
3516 }
3517
3518 static ShaderEnum vector_transform_convert_space_init()
3519 {
3520         ShaderEnum enm;
3521
3522         enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
3523         enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
3524         enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA);
3525
3526         return enm;
3527 }
3528
3529 ShaderEnum VectorTransformNode::type_enum = vector_transform_type_init();
3530 ShaderEnum VectorTransformNode::convert_space_enum = vector_transform_convert_space_init();
3531
3532 void VectorTransformNode::compile(SVMCompiler& compiler)
3533 {
3534         ShaderInput *vector_in = input("Vector");
3535         ShaderOutput *vector_out = output("Vector");
3536
3537         compiler.stack_assign(vector_in);
3538         compiler.stack_assign(vector_out);
3539
3540         compiler.add_node(NODE_VECTOR_TRANSFORM,
3541                 compiler.encode_uchar4(type_enum[type], convert_space_enum[convert_from], convert_space_enum[convert_to]),
3542                 compiler.encode_uchar4(vector_in->stack_offset, vector_out->stack_offset));
3543 }
3544
3545 void VectorTransformNode::compile(OSLCompiler& compiler)
3546 {
3547         compiler.parameter("type", type);
3548         compiler.parameter("convert_from", convert_from);
3549         compiler.parameter("convert_to", convert_to);
3550         compiler.add(this, "node_vector_transform");
3551 }
3552
3553 /* BumpNode */
3554
3555 BumpNode::BumpNode()
3556 : ShaderNode("bump")
3557 {
3558         invert = false;
3559
3560         /* this input is used by the user, but after graph transform it is no longer
3561          * used and moved to sampler center/x/y instead */
3562         add_input("Height", SHADER_SOCKET_FLOAT);
3563
3564         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
3565         add_input("SampleX", SHADER_SOCKET_FLOAT);
3566         add_input("SampleY", SHADER_SOCKET_FLOAT);
3567         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
3568         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3569         add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
3570
3571         add_output("Normal", SHADER_SOCKET_NORMAL);
3572 }
3573
3574 void BumpNode::compile(SVMCompiler& compiler)
3575 {
3576         ShaderInput *center_in = input("SampleCenter");
3577         ShaderInput *dx_in = input("SampleX");
3578         ShaderInput *dy_in = input("SampleY");
3579         ShaderInput *normal_in = input("Normal");
3580         ShaderInput *strength_in = input("Strength");
3581         ShaderInput *distance_in = input("Distance");
3582         ShaderOutput *normal_out = output("Normal");
3583
3584         compiler.stack_assign(center_in);
3585         compiler.stack_assign(dx_in);
3586         compiler.stack_assign(dy_in);
3587         compiler.stack_assign(strength_in);
3588         compiler.stack_assign(distance_in);
3589         compiler.stack_assign(normal_out);
3590
3591         if(normal_in->link)
3592                 compiler.stack_assign(normal_in);
3593         
3594         /* pack all parameters in the node */
3595         compiler.add_node(NODE_SET_BUMP,
3596                 compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
3597                 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
3598                         dy_in->stack_offset, strength_in->stack_offset),
3599                 normal_out->stack_offset);
3600 }
3601
3602 void BumpNode::compile(OSLCompiler& compiler)
3603 {
3604         compiler.parameter("invert", invert);
3605         compiler.add(this, "node_bump");
3606 }
3607
3608 /* RGBCurvesNode */
3609
3610 RGBCurvesNode::RGBCurvesNode()
3611 : ShaderNode("rgb_curves")
3612 {
3613         add_input("Fac", SHADER_SOCKET_FLOAT);
3614         add_input("Color", SHADER_SOCKET_COLOR);
3615         add_output("Color", SHADER_SOCKET_COLOR);
3616 }
3617
3618 void RGBCurvesNode::compile(SVMCompiler& compiler)
3619 {
3620         ShaderInput *fac_in = input("Fac");
3621         ShaderInput *color_in = input("Color");
3622         ShaderOutput *color_out = output("Color");
3623
3624         compiler.stack_assign(fac_in);
3625         compiler.stack_assign(color_in);
3626         compiler.stack_assign(color_out);
3627
3628         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3629         compiler.add_array(curves, RAMP_TABLE_SIZE);
3630 }
3631
3632 void RGBCurvesNode::compile(OSLCompiler& compiler)
3633 {
3634         float ramp[RAMP_TABLE_SIZE][3];
3635
3636         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3637                 ramp[i][0] = curves[i].x;
3638                 ramp[i][1] = curves[i].y;
3639                 ramp[i][2] = curves[i].z;
3640         }
3641
3642         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3643         compiler.add(this, "node_rgb_curves");
3644 }
3645
3646 /* VectorCurvesNode */
3647
3648 VectorCurvesNode::VectorCurvesNode()
3649 : ShaderNode("vector_curves")
3650 {
3651         add_input("Fac", SHADER_SOCKET_FLOAT);
3652         add_input("Vector", SHADER_SOCKET_VECTOR);
3653         add_output("Vector", SHADER_SOCKET_VECTOR);
3654 }
3655
3656 void VectorCurvesNode::compile(SVMCompiler& compiler)
3657 {
3658         ShaderInput *fac_in = input("Fac");
3659         ShaderInput *vector_in = input("Vector");
3660         ShaderOutput *vector_out = output("Vector");
3661
3662         compiler.stack_assign(fac_in);
3663         compiler.stack_assign(vector_in);
3664         compiler.stack_assign(vector_out);
3665
3666         compiler.add_node(NODE_VECTOR_CURVES, fac_in->stack_offset, vector_in->stack_offset, vector_out->stack_offset);
3667         compiler.add_array(curves, RAMP_TABLE_SIZE);
3668 }
3669
3670 void VectorCurvesNode::compile(OSLCompiler& compiler)
3671 {
3672         float ramp[RAMP_TABLE_SIZE][3];
3673
3674         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3675                 ramp[i][0] = curves[i].x;
3676                 ramp[i][1] = curves[i].y;
3677                 ramp[i][2] = curves[i].z;
3678         }
3679
3680         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3681         compiler.add(this, "node_vector_curves");
3682 }
3683
3684 /* RGBRampNode */
3685
3686 RGBRampNode::RGBRampNode()
3687 : ShaderNode("rgb_ramp")
3688 {
3689         add_input("Fac", SHADER_SOCKET_FLOAT);
3690         add_output("Color", SHADER_SOCKET_COLOR);
3691         add_output("Alpha", SHADER_SOCKET_FLOAT);
3692
3693         interpolate = true;
3694 }
3695
3696 void RGBRampNode::compile(SVMCompiler& compiler)
3697 {
3698         ShaderInput *fac_in = input("Fac");
3699         ShaderOutput *color_out = output("Color");
3700         ShaderOutput *alpha_out = output("Alpha");
3701
3702         compiler.stack_assign(fac_in);
3703         if(!color_out->links.empty())
3704                 compiler.stack_assign(color_out);
3705         if(!alpha_out->links.empty())
3706                 compiler.stack_assign(alpha_out);
3707
3708         compiler.add_node(NODE_RGB_RAMP,
3709                 compiler.encode_uchar4(
3710                         fac_in->stack_offset,
3711                         color_out->stack_offset,
3712                         alpha_out->stack_offset),
3713                 interpolate);
3714         compiler.add_array(ramp, RAMP_TABLE_SIZE);
3715 }
3716
3717 void RGBRampNode::compile(OSLCompiler& compiler)
3718 {
3719         /* OSL shader only takes separate RGB and A array, split the RGBA base array */
3720         /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
3721         float ramp_color[RAMP_TABLE_SIZE][3];
3722         float ramp_alpha[RAMP_TABLE_SIZE];
3723
3724         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3725                 ramp_color[i][0] = ramp[i].x;
3726                 ramp_color[i][1] = ramp[i].y;
3727                 ramp_color[i][2] = ramp[i].z;
3728                 ramp_alpha[i] = ramp[i].w;
3729         }
3730
3731         compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
3732         compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
3733         compiler.parameter("ramp_interpolate", interpolate);
3734         
3735         compiler.add(this, "node_rgb_ramp");
3736 }
3737
3738 /* Set Normal Node */
3739
3740 SetNormalNode::SetNormalNode()
3741 : ShaderNode("set_normal")
3742 {
3743         add_input("Direction", SHADER_SOCKET_VECTOR);
3744         add_output("Normal", SHADER_SOCKET_NORMAL);
3745 }
3746
3747 void SetNormalNode::compile(SVMCompiler& compiler)
3748 {
3749         ShaderInput  *direction_in = input("Direction");
3750         ShaderOutput *normal_out = output("Normal");
3751
3752         compiler.stack_assign(direction_in);
3753         compiler.stack_assign(normal_out);
3754
3755         compiler.add_node(NODE_CLOSURE_SET_NORMAL, direction_in->stack_offset, normal_out->stack_offset);
3756 }
3757
3758 void SetNormalNode::compile(OSLCompiler& compiler)
3759 {
3760         compiler.add(this, "node_set_normal"); 
3761 }
3762
3763 /* OSLScriptNode */
3764