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