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