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