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