d5ca20e6af1d981dbfd9c237eae323019e7c541a
[blender-staging.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 /* Image Texture */
104
105 static ShaderEnum color_space_init()
106 {
107         ShaderEnum enm;
108
109         enm.insert("None", 0);
110         enm.insert("Color", 1);
111
112         return enm;
113 }
114
115 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
116
117 ImageTextureNode::ImageTextureNode()
118 : TextureNode("image_texture")
119 {
120         image_manager = NULL;
121         slot = -1;
122         is_float = false;
123         filename = "";
124         color_space = ustring("Color");
125
126         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_UV);
127         add_output("Color", SHADER_SOCKET_COLOR);
128         add_output("Alpha", SHADER_SOCKET_FLOAT);
129 }
130
131 ImageTextureNode::~ImageTextureNode()
132 {
133         if(image_manager)
134                 image_manager->remove_image(filename);
135 }
136
137 ShaderNode *ImageTextureNode::clone() const
138 {
139         ImageTextureNode *node = new ImageTextureNode(*this);
140         node->image_manager = NULL;
141         node->slot = -1;
142         node->is_float = false;
143         return node;
144 }
145
146 void ImageTextureNode::compile(SVMCompiler& compiler)
147 {
148         ShaderInput *vector_in = input("Vector");
149         ShaderOutput *color_out = output("Color");
150         ShaderOutput *alpha_out = output("Alpha");
151
152         image_manager = compiler.image_manager;
153         if(slot == -1)
154                 slot = image_manager->add_image(filename, is_float);
155
156         if(!color_out->links.empty())
157                 compiler.stack_assign(color_out);
158         if(!alpha_out->links.empty())
159                 compiler.stack_assign(alpha_out);
160
161         if(slot != -1) {
162                 compiler.stack_assign(vector_in);
163
164                 int srgb = (is_float || color_space != "Color")? 0: 1;
165                 int vector_offset = vector_in->stack_offset;
166
167                 if(!tex_mapping.skip()) {
168                         vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
169                         tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
170                 }
171
172                 compiler.add_node(NODE_TEX_IMAGE,
173                         slot,
174                         compiler.encode_uchar4(
175                                 vector_offset,
176                                 color_out->stack_offset,
177                                 alpha_out->stack_offset,
178                                 srgb));
179         
180                 if(vector_offset != vector_in->stack_offset)
181                         compiler.stack_clear_offset(vector_in->type, vector_offset);
182         }
183         else {
184                 /* image not found */
185                 if(!color_out->links.empty()) {
186                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
187                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
188                                                                     TEX_IMAGE_MISSING_G,
189                                                                     TEX_IMAGE_MISSING_B));
190                 }
191                 if(!alpha_out->links.empty())
192                         compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
193         }
194 }
195
196 void ImageTextureNode::compile(OSLCompiler& compiler)
197 {
198         compiler.parameter("filename", filename.c_str());
199         if(is_float || color_space != "Color")
200                 compiler.parameter("color_space", "Linear");
201         else
202                 compiler.parameter("color_space", "sRGB");
203         compiler.add(this, "node_image_texture");
204 }
205
206 /* Environment Texture */
207
208 static ShaderEnum projection_init()
209 {
210         ShaderEnum enm;
211
212         enm.insert("Equirectangular", 0);
213         enm.insert("Mirror Ball", 1);
214
215         return enm;
216 }
217
218 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
219 ShaderEnum EnvironmentTextureNode::projection_enum = projection_init();
220
221 EnvironmentTextureNode::EnvironmentTextureNode()
222 : TextureNode("environment_texture")
223 {
224         image_manager = NULL;
225         slot = -1;
226         is_float = false;
227         filename = "";
228         color_space = ustring("Color");
229         projection = ustring("Equirectangular");
230
231         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
232         add_output("Color", SHADER_SOCKET_COLOR);
233         add_output("Alpha", SHADER_SOCKET_FLOAT);
234 }
235
236 EnvironmentTextureNode::~EnvironmentTextureNode()
237 {
238         if(image_manager)
239                 image_manager->remove_image(filename);
240 }
241
242 ShaderNode *EnvironmentTextureNode::clone() const
243 {
244         EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
245         node->image_manager = NULL;
246         node->slot = -1;
247         node->is_float = false;
248         return node;
249 }
250
251 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
252 {
253         ShaderInput *vector_in = input("Vector");
254         ShaderOutput *color_out = output("Color");
255         ShaderOutput *alpha_out = output("Alpha");
256
257         image_manager = compiler.image_manager;
258         if(slot == -1)
259                 slot = image_manager->add_image(filename, is_float);
260
261         if(!color_out->links.empty())
262                 compiler.stack_assign(color_out);
263         if(!alpha_out->links.empty())
264                 compiler.stack_assign(alpha_out);
265         
266         if(slot != -1) {
267                 compiler.stack_assign(vector_in);
268
269                 int srgb = (is_float || color_space != "Color")? 0: 1;
270                 int vector_offset = vector_in->stack_offset;
271
272                 if(!tex_mapping.skip()) {
273                         vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
274                         tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
275                 }
276
277                 compiler.add_node(NODE_TEX_ENVIRONMENT,
278                         slot,
279                         compiler.encode_uchar4(
280                                 vector_offset,
281                                 color_out->stack_offset,
282                                 alpha_out->stack_offset,
283                                 srgb),
284                         projection_enum[projection]);
285         
286                 if(vector_offset != vector_in->stack_offset)
287                         compiler.stack_clear_offset(vector_in->type, vector_offset);
288         }
289         else {
290                 /* image not found */
291                 if(!color_out->links.empty()) {
292                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
293                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
294                                                                     TEX_IMAGE_MISSING_G,
295                                                                     TEX_IMAGE_MISSING_B));
296                 }
297                 if(!alpha_out->links.empty())
298                         compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
299         }
300 }
301
302 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
303 {
304         compiler.parameter("filename", filename.c_str());
305         if(is_float || color_space != "Color")
306                 compiler.parameter("color_space", "Linear");
307         else
308                 compiler.parameter("color_space", "sRGB");
309         compiler.add(this, "node_environment_texture");
310 }
311
312 /* Sky Texture */
313
314 static float2 sky_spherical_coordinates(float3 dir)
315 {
316         return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
317 }
318
319 static float sky_perez_function(float lam[6], float theta, float gamma)
320 {
321         return (1.f + lam[0]*expf(lam[1]/cosf(theta))) * (1.f + lam[2]*expf(lam[3]*gamma)  + lam[4]*cosf(gamma)*cosf(gamma));
322 }
323
324 static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity)
325 {
326         float2 spherical = sky_spherical_coordinates(dir);
327         float theta = spherical.x;
328         float phi = spherical.y;
329
330         ksunsky->theta = theta;
331         ksunsky->phi = phi;
332
333         float theta2 = theta*theta;
334         float theta3 = theta*theta*theta;
335         float T = turbidity;
336         float T2 = T * T;
337
338         float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
339         ksunsky->zenith_Y = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
340         ksunsky->zenith_Y *= 0.06f;
341
342         ksunsky->zenith_x =
343         (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
344         (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
345         (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
346
347         ksunsky->zenith_y =
348         (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
349         (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta  + 0.00516f) * T +
350         (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta  + 0.26688f);
351
352         ksunsky->perez_Y[0] = (0.1787f * T  - 1.4630f);
353         ksunsky->perez_Y[1] = (-0.3554f * T  + 0.4275f);
354         ksunsky->perez_Y[2] = (-0.0227f * T  + 5.3251f);
355         ksunsky->perez_Y[3] = (0.1206f * T  - 2.5771f);
356         ksunsky->perez_Y[4] = (-0.0670f * T  + 0.3703f);
357
358         ksunsky->perez_x[0] = (-0.0193f * T  - 0.2592f);
359         ksunsky->perez_x[1] = (-0.0665f * T  + 0.0008f);
360         ksunsky->perez_x[2] = (-0.0004f * T  + 0.2125f);
361         ksunsky->perez_x[3] = (-0.0641f * T  - 0.8989f);
362         ksunsky->perez_x[4] = (-0.0033f * T  + 0.0452f);
363
364         ksunsky->perez_y[0] = (-0.0167f * T  - 0.2608f);
365         ksunsky->perez_y[1] = (-0.0950f * T  + 0.0092f);
366         ksunsky->perez_y[2] = (-0.0079f * T  + 0.2102f);
367         ksunsky->perez_y[3] = (-0.0441f * T  - 1.6537f);
368         ksunsky->perez_y[4] = (-0.0109f * T  + 0.0529f);
369
370         ksunsky->zenith_Y /= sky_perez_function(ksunsky->perez_Y, 0, theta);
371         ksunsky->zenith_x /= sky_perez_function(ksunsky->perez_x, 0, theta);
372         ksunsky->zenith_y /= sky_perez_function(ksunsky->perez_y, 0, theta);
373 }
374
375 SkyTextureNode::SkyTextureNode()
376 : TextureNode("sky_texture")
377 {
378         sun_direction = make_float3(0.0f, 0.0f, 1.0f);
379         turbidity = 2.2f;
380
381         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
382         add_output("Color", SHADER_SOCKET_COLOR);
383 }
384
385 void SkyTextureNode::compile(SVMCompiler& compiler)
386 {
387         ShaderInput *vector_in = input("Vector");
388         ShaderOutput *color_out = output("Color");
389
390         if(compiler.sunsky) {
391                 sky_texture_precompute(compiler.sunsky, sun_direction, turbidity);
392                 compiler.sunsky = NULL;
393         }
394
395         if(vector_in->link)
396                 compiler.stack_assign(vector_in);
397
398         int vector_offset = vector_in->stack_offset;
399
400         if(!tex_mapping.skip()) {
401                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
402                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
403         }
404
405         compiler.stack_assign(color_out);
406         compiler.add_node(NODE_TEX_SKY, vector_offset, color_out->stack_offset);
407
408         if(vector_offset != vector_in->stack_offset)
409                 compiler.stack_clear_offset(vector_in->type, vector_offset);
410 }
411
412 void SkyTextureNode::compile(OSLCompiler& compiler)
413 {
414         compiler.parameter_vector("sun_direction", sun_direction);
415         compiler.parameter("turbidity", turbidity);
416         compiler.add(this, "node_sky_texture");
417 }
418
419 /* Gradient Texture */
420
421 static ShaderEnum gradient_type_init()
422 {
423         ShaderEnum enm;
424
425         enm.insert("Linear", NODE_BLEND_LINEAR);
426         enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
427         enm.insert("Easing", NODE_BLEND_EASING);
428         enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
429         enm.insert("Radial", NODE_BLEND_RADIAL);
430         enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
431         enm.insert("Spherical", NODE_BLEND_SPHERICAL);
432
433         return enm;
434 }
435
436 ShaderEnum GradientTextureNode::type_enum = gradient_type_init();
437
438 GradientTextureNode::GradientTextureNode()
439 : TextureNode("gradient_texture")
440 {
441         type = ustring("Linear");
442
443         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
444         add_output("Color", SHADER_SOCKET_COLOR);
445         add_output("Fac", SHADER_SOCKET_FLOAT);
446 }
447
448 void GradientTextureNode::compile(SVMCompiler& compiler)
449 {
450         ShaderInput *vector_in = input("Vector");
451         ShaderOutput *color_out = output("Color");
452         ShaderOutput *fac_out = output("Fac");
453
454         if(vector_in->link) compiler.stack_assign(vector_in);
455
456         int vector_offset = vector_in->stack_offset;
457
458         if(!tex_mapping.skip()) {
459                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
460                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
461         }
462
463         if(!fac_out->links.empty())
464                 compiler.stack_assign(fac_out);
465         if(!color_out->links.empty())
466                 compiler.stack_assign(color_out);
467
468         compiler.add_node(NODE_TEX_GRADIENT,
469                 compiler.encode_uchar4(type_enum[type], vector_offset, fac_out->stack_offset, color_out->stack_offset));
470
471         if(vector_offset != vector_in->stack_offset)
472                 compiler.stack_clear_offset(vector_in->type, vector_offset);
473 }
474
475 void GradientTextureNode::compile(OSLCompiler& compiler)
476 {
477         compiler.parameter("Type", type);
478         compiler.add(this, "node_gradient_texture");
479 }
480
481 /* Noise Texture */
482
483 NoiseTextureNode::NoiseTextureNode()
484 : TextureNode("noise_texture")
485 {
486         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
487         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
488         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
489         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
490
491         add_output("Color", SHADER_SOCKET_COLOR);
492         add_output("Fac", SHADER_SOCKET_FLOAT);
493 }
494
495 void NoiseTextureNode::compile(SVMCompiler& compiler)
496 {
497         ShaderInput *distortion_in = input("Distortion");
498         ShaderInput *detail_in = input("Detail");
499         ShaderInput *scale_in = input("Scale");
500         ShaderInput *vector_in = input("Vector");
501         ShaderOutput *color_out = output("Color");
502         ShaderOutput *fac_out = output("Fac");
503
504         if(vector_in->link) compiler.stack_assign(vector_in);
505         if(scale_in->link) compiler.stack_assign(scale_in);
506         if(detail_in->link) compiler.stack_assign(detail_in);
507         if(distortion_in->link) compiler.stack_assign(distortion_in);
508
509         int vector_offset = vector_in->stack_offset;
510
511         if(!tex_mapping.skip()) {
512                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
513                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
514         }
515
516         if(!fac_out->links.empty())
517                 compiler.stack_assign(fac_out);
518         if(!color_out->links.empty())
519                 compiler.stack_assign(color_out);
520
521         compiler.add_node(NODE_TEX_NOISE,
522                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
523                 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset));
524         compiler.add_node(
525                 __float_as_int(scale_in->value.x),
526                 __float_as_int(detail_in->value.x),
527                 __float_as_int(distortion_in->value.x));
528
529         if(vector_offset != vector_in->stack_offset)
530                 compiler.stack_clear_offset(vector_in->type, vector_offset);
531 }
532
533 void NoiseTextureNode::compile(OSLCompiler& compiler)
534 {
535         compiler.add(this, "node_noise_texture");
536 }
537
538 /* Voronoi Texture */
539
540 static ShaderEnum voronoi_coloring_init()
541 {
542         ShaderEnum enm;
543
544         enm.insert("Intensity", NODE_VORONOI_INTENSITY);
545         enm.insert("Cells", NODE_VORONOI_CELLS);
546
547         return enm;
548 }
549
550 ShaderEnum VoronoiTextureNode::coloring_enum  = voronoi_coloring_init();
551
552 VoronoiTextureNode::VoronoiTextureNode()
553 : TextureNode("voronoi_texture")
554 {
555         coloring = ustring("Intensity");
556
557         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
558         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
559
560         add_output("Color", SHADER_SOCKET_COLOR);
561         add_output("Fac", SHADER_SOCKET_FLOAT);
562 }
563
564 void VoronoiTextureNode::compile(SVMCompiler& compiler)
565 {
566         ShaderInput *scale_in = input("Scale");
567         ShaderInput *vector_in = input("Vector");
568         ShaderOutput *color_out = output("Color");
569         ShaderOutput *fac_out = output("Fac");
570
571         if(vector_in->link) compiler.stack_assign(vector_in);
572         if(scale_in->link) compiler.stack_assign(scale_in);
573
574         int vector_offset = vector_in->stack_offset;
575
576         if(!tex_mapping.skip()) {
577                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
578                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
579         }
580
581         compiler.stack_assign(color_out);
582         compiler.stack_assign(fac_out);
583
584         compiler.add_node(NODE_TEX_VORONOI,
585                 coloring_enum[coloring],
586                 compiler.encode_uchar4(scale_in->stack_offset, vector_offset, fac_out->stack_offset, color_out->stack_offset),
587                 __float_as_int(scale_in->value.x));
588
589         if(vector_offset != vector_in->stack_offset)
590                 compiler.stack_clear_offset(vector_in->type, vector_offset);
591 }
592
593 void VoronoiTextureNode::compile(OSLCompiler& compiler)
594 {
595         compiler.parameter("Coloring", coloring);
596         compiler.add(this, "node_voronoi_texture");
597 }
598
599 /* Musgrave Texture */
600
601 static ShaderEnum musgrave_type_init()
602 {
603         ShaderEnum enm;
604
605         enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
606         enm.insert("fBM", NODE_MUSGRAVE_FBM);
607         enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
608         enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
609         enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
610
611         return enm;
612 }
613
614 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
615
616 MusgraveTextureNode::MusgraveTextureNode()
617 : TextureNode("musgrave_texture")
618 {
619         type = ustring("fBM");
620
621         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
622         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
623         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
624         add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
625         add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
626         add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
627         add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
628
629         add_output("Fac", SHADER_SOCKET_FLOAT);
630         add_output("Color", SHADER_SOCKET_COLOR);
631 }
632
633 void MusgraveTextureNode::compile(SVMCompiler& compiler)
634 {
635         ShaderInput *vector_in = input("Vector");
636         ShaderInput *scale_in = input("Scale");
637         ShaderInput *dimension_in = input("Dimension");
638         ShaderInput *lacunarity_in = input("Lacunarity");
639         ShaderInput *detail_in = input("Detail");
640         ShaderInput *offset_in = input("Offset");
641         ShaderInput *gain_in = input("Gain");
642         ShaderOutput *fac_out = output("Fac");
643         ShaderOutput *color_out = output("Color");
644
645         if(vector_in->link) compiler.stack_assign(vector_in);
646         if(dimension_in->link) compiler.stack_assign(dimension_in);
647         if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
648         if(detail_in->link) compiler.stack_assign(detail_in);
649         if(offset_in->link) compiler.stack_assign(offset_in);
650         if(gain_in->link) compiler.stack_assign(gain_in);
651         if(scale_in->link) compiler.stack_assign(scale_in);
652
653         int vector_offset = vector_in->stack_offset;
654
655         if(!tex_mapping.skip()) {
656                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
657                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
658         }
659
660         if(!fac_out->links.empty())
661                 compiler.stack_assign(fac_out);
662         if(!color_out->links.empty())
663                 compiler.stack_assign(color_out);
664
665         compiler.add_node(NODE_TEX_MUSGRAVE,
666                 compiler.encode_uchar4(type_enum[type], vector_offset, color_out->stack_offset, fac_out->stack_offset),
667                 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, detail_in->stack_offset, offset_in->stack_offset),
668                 compiler.encode_uchar4(gain_in->stack_offset, scale_in->stack_offset));
669         compiler.add_node(__float_as_int(dimension_in->value.x),
670                 __float_as_int(lacunarity_in->value.x),
671                 __float_as_int(detail_in->value.x),
672                 __float_as_int(offset_in->value.x));
673         compiler.add_node(__float_as_int(gain_in->value.x),
674                 __float_as_int(scale_in->value.x));
675
676         if(vector_offset != vector_in->stack_offset)
677                 compiler.stack_clear_offset(vector_in->type, vector_offset);
678 }
679
680 void MusgraveTextureNode::compile(OSLCompiler& compiler)
681 {
682         compiler.parameter("Type", type);
683
684         compiler.add(this, "node_musgrave_texture");
685 }
686
687 /* Wave Texture */
688
689 static ShaderEnum wave_type_init()
690 {
691         ShaderEnum enm;
692
693         enm.insert("Bands", NODE_WAVE_BANDS);
694         enm.insert("Rings", NODE_WAVE_RINGS);
695
696         return enm;
697 }
698
699 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
700
701 WaveTextureNode::WaveTextureNode()
702 : TextureNode("wave_texture")
703 {
704         type = ustring("Bands");
705
706         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
707         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
708         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
709         add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
710         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
711
712         add_output("Color", SHADER_SOCKET_COLOR);
713         add_output("Fac", SHADER_SOCKET_FLOAT);
714 }
715
716 void WaveTextureNode::compile(SVMCompiler& compiler)
717 {
718         ShaderInput *scale_in = input("Scale");
719         ShaderInput *distortion_in = input("Distortion");
720         ShaderInput *dscale_in = input("Detail Scale");
721         ShaderInput *detail_in = input("Detail");
722         ShaderInput *vector_in = input("Vector");
723         ShaderOutput *fac_out = output("Fac");
724         ShaderOutput *color_out = output("Color");
725
726         if(scale_in->link) compiler.stack_assign(scale_in);
727         if(detail_in->link) compiler.stack_assign(detail_in);
728         if(distortion_in->link) compiler.stack_assign(distortion_in);
729         if(dscale_in->link) compiler.stack_assign(dscale_in);
730         if(vector_in->link) compiler.stack_assign(vector_in);
731
732         int vector_offset = vector_in->stack_offset;
733
734         if(!tex_mapping.skip()) {
735                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
736                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
737         }
738
739         if(!fac_out->links.empty())
740                 compiler.stack_assign(fac_out);
741         if(!color_out->links.empty())
742                 compiler.stack_assign(color_out);
743
744         compiler.add_node(NODE_TEX_WAVE,
745                 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
746                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset));
747
748         compiler.add_node(
749                 __float_as_int(scale_in->value.x),
750                 __float_as_int(detail_in->value.x),
751                 __float_as_int(distortion_in->value.x),
752                 __float_as_int(dscale_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 WaveTextureNode::compile(OSLCompiler& compiler)
759 {
760         compiler.parameter("Type", type);
761
762         compiler.add(this, "node_wave_texture");
763 }
764
765 /* Magic Texture */
766
767 MagicTextureNode::MagicTextureNode()
768 : TextureNode("magic_texture")
769 {
770         depth = 2;
771
772         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
773         add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
774         add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
775
776         add_output("Color", SHADER_SOCKET_COLOR);
777         add_output("Fac", SHADER_SOCKET_FLOAT);
778 }
779
780 void MagicTextureNode::compile(SVMCompiler& compiler)
781 {
782         ShaderInput *vector_in = input("Vector");
783         ShaderInput *scale_in = input("Scale");
784         ShaderInput *distortion_in = input("Distortion");
785         ShaderOutput *color_out = output("Color");
786         ShaderOutput *fac_out = output("Fac");
787
788         if(vector_in->link) compiler.stack_assign(vector_in);
789         if(distortion_in->link) compiler.stack_assign(distortion_in);
790         if(scale_in->link) compiler.stack_assign(scale_in);
791
792         int vector_offset = vector_in->stack_offset;
793
794         if(!tex_mapping.skip()) {
795                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
796                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
797         }
798
799         if(!fac_out->links.empty())
800                 compiler.stack_assign(fac_out);
801         if(!color_out->links.empty())
802                 compiler.stack_assign(color_out);
803
804         compiler.add_node(NODE_TEX_MAGIC,
805                 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
806                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, distortion_in->stack_offset));
807         compiler.add_node(
808                 __float_as_int(scale_in->value.x),
809                 __float_as_int(distortion_in->value.x));
810
811         if(vector_offset != vector_in->stack_offset)
812                 compiler.stack_clear_offset(vector_in->type, vector_offset);
813 }
814
815 void MagicTextureNode::compile(OSLCompiler& compiler)
816 {
817         compiler.parameter("Depth", depth);
818         compiler.add(this, "node_magic_texture");
819 }
820
821 /* Checker Texture */
822
823 CheckerTextureNode::CheckerTextureNode()
824 : TextureNode("checker_texture")
825 {
826         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
827         add_input("Color1", SHADER_SOCKET_COLOR);
828         add_input("Color2", SHADER_SOCKET_COLOR);
829         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
830
831         add_output("Color", SHADER_SOCKET_COLOR);
832         add_output("Fac", SHADER_SOCKET_FLOAT);
833 }
834
835 void CheckerTextureNode::compile(SVMCompiler& compiler)
836 {
837         ShaderInput *vector_in = input("Vector");
838         ShaderInput *color1_in = input("Color1");
839         ShaderInput *color2_in = input("Color2");
840         ShaderInput *scale_in = input("Scale");
841         
842         ShaderOutput *color_out = output("Color");
843         ShaderOutput *fac_out = output("Fac");
844
845         compiler.stack_assign(vector_in);
846         compiler.stack_assign(color1_in);
847         compiler.stack_assign(color2_in);
848         if(scale_in->link) compiler.stack_assign(scale_in);
849
850         int vector_offset = vector_in->stack_offset;
851
852         if(!tex_mapping.skip()) {
853                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
854                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
855         }
856
857         if(!color_out->links.empty())
858                 compiler.stack_assign(color_out);
859         if(!fac_out->links.empty())
860                 compiler.stack_assign(fac_out);
861
862         compiler.add_node(NODE_TEX_CHECKER,
863                 compiler.encode_uchar4(vector_offset, color1_in->stack_offset, color2_in->stack_offset, scale_in->stack_offset),
864                 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset),
865                 __float_as_int(scale_in->value.x));
866
867         if(vector_offset != vector_in->stack_offset)
868                 compiler.stack_clear_offset(vector_in->type, vector_offset);
869 }
870
871 void CheckerTextureNode::compile(OSLCompiler& compiler)
872 {
873         compiler.add(this, "node_checker_texture");
874 }
875
876 /* Normal */
877
878 NormalNode::NormalNode()
879 : ShaderNode("normal")
880 {
881         direction = make_float3(0.0f, 0.0f, 1.0f);
882
883         add_input("Normal", SHADER_SOCKET_NORMAL);
884         add_output("Normal", SHADER_SOCKET_NORMAL);
885         add_output("Dot",  SHADER_SOCKET_FLOAT);
886 }
887
888 void NormalNode::compile(SVMCompiler& compiler)
889 {
890         ShaderInput *normal_in = input("Normal");
891         ShaderOutput *normal_out = output("Normal");
892         ShaderOutput *dot_out = output("Dot");
893
894         compiler.stack_assign(normal_in);
895         compiler.stack_assign(normal_out);
896         compiler.stack_assign(dot_out);
897
898         compiler.add_node(NODE_NORMAL, normal_in->stack_offset, normal_out->stack_offset, dot_out->stack_offset);
899         compiler.add_node(
900                 __float_as_int(direction.x),
901                 __float_as_int(direction.y),
902                 __float_as_int(direction.z));
903 }
904
905 void NormalNode::compile(OSLCompiler& compiler)
906 {
907         compiler.parameter_vector("Direction", direction);
908         compiler.add(this, "node_normal");
909 }
910
911 /* Mapping */
912
913 MappingNode::MappingNode()
914 : ShaderNode("mapping")
915 {
916         add_input("Vector", SHADER_SOCKET_POINT);
917         add_output("Vector", SHADER_SOCKET_POINT);
918 }
919
920 void MappingNode::compile(SVMCompiler& compiler)
921 {
922         ShaderInput *vector_in = input("Vector");
923         ShaderOutput *vector_out = output("Vector");
924
925         compiler.stack_assign(vector_in);
926         compiler.stack_assign(vector_out);
927
928         tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
929 }
930
931 void MappingNode::compile(OSLCompiler& compiler)
932 {
933         Transform tfm = transform_transpose(tex_mapping.compute_transform());
934         compiler.parameter("Matrix", tfm);
935
936         compiler.add(this, "node_mapping");
937 }
938
939 /* Convert */
940
941 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_)
942 : ShaderNode("convert")
943 {
944         from = from_;
945         to = to_;
946
947         assert(from != to);
948
949         if(from == SHADER_SOCKET_FLOAT)
950                 add_input("Val", SHADER_SOCKET_FLOAT);
951         else if(from == SHADER_SOCKET_COLOR)
952                 add_input("Color", SHADER_SOCKET_COLOR);
953         else if(from == SHADER_SOCKET_VECTOR)
954                 add_input("Vector", SHADER_SOCKET_VECTOR);
955         else if(from == SHADER_SOCKET_POINT)
956                 add_input("Point", SHADER_SOCKET_POINT);
957         else if(from == SHADER_SOCKET_NORMAL)
958                 add_input("Normal", SHADER_SOCKET_NORMAL);
959         else
960                 assert(0);
961
962         if(to == SHADER_SOCKET_FLOAT)
963                 add_output("Val", SHADER_SOCKET_FLOAT);
964         else if(to == SHADER_SOCKET_COLOR)
965                 add_output("Color", SHADER_SOCKET_COLOR);
966         else if(to == SHADER_SOCKET_VECTOR)
967                 add_output("Vector", SHADER_SOCKET_VECTOR);
968         else if(to == SHADER_SOCKET_POINT)
969                 add_output("Point", SHADER_SOCKET_POINT);
970         else if(to == SHADER_SOCKET_NORMAL)
971                 add_output("Normal", SHADER_SOCKET_NORMAL);
972         else
973                 assert(0);
974 }
975
976 void ConvertNode::compile(SVMCompiler& compiler)
977 {
978         ShaderInput *in = inputs[0];
979         ShaderOutput *out = outputs[0];
980
981         if(to == SHADER_SOCKET_FLOAT) {
982                 compiler.stack_assign(in);
983                 compiler.stack_assign(out);
984
985                 if(from == SHADER_SOCKET_COLOR)
986                         /* color to float */
987                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
988                 else
989                         /* vector/point/normal to float */
990                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
991         }
992         else if(from == SHADER_SOCKET_FLOAT) {
993                 compiler.stack_assign(in);
994                 compiler.stack_assign(out);
995
996                 /* float to float3 */
997                 compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
998         }
999         else {
1000                 /* float3 to float3 */
1001                 if(in->link) {
1002                         /* no op in SVM */
1003                         compiler.stack_link(in, out);
1004                 }
1005                 else {
1006                         /* set 0,0,0 value */
1007                         compiler.stack_assign(in);
1008                         compiler.stack_assign(out);
1009
1010                         compiler.add_node(NODE_VALUE_V, in->stack_offset);
1011                         compiler.add_node(NODE_VALUE_V, in->value);
1012                 }
1013         }
1014 }
1015
1016 void ConvertNode::compile(OSLCompiler& compiler)
1017 {
1018         if(from == SHADER_SOCKET_FLOAT)
1019                 compiler.add(this, "node_convert_from_float");
1020         else if(from == SHADER_SOCKET_COLOR)
1021                 compiler.add(this, "node_convert_from_color");
1022         else if(from == SHADER_SOCKET_VECTOR)
1023                 compiler.add(this, "node_convert_from_vector");
1024         else if(from == SHADER_SOCKET_POINT)
1025                 compiler.add(this, "node_convert_from_point");
1026         else if(from == SHADER_SOCKET_NORMAL)
1027                 compiler.add(this, "node_convert_from_normal");
1028         else
1029                 assert(0);
1030 }
1031
1032 /* Proxy */
1033
1034 ProxyNode::ProxyNode(ShaderSocketType from_, ShaderSocketType to_)
1035 : ShaderNode("proxy")
1036 {
1037         from = from_;
1038         to = to_;
1039         special_type = SHADER_SPECIAL_TYPE_PROXY;
1040
1041         add_input("Input", from);
1042         add_output("Output", to);
1043 }
1044
1045 void ProxyNode::compile(SVMCompiler& compiler)
1046 {
1047 }
1048
1049 void ProxyNode::compile(OSLCompiler& compiler)
1050 {
1051 }
1052
1053 /* BSDF Closure */
1054
1055 BsdfNode::BsdfNode()
1056 : ShaderNode("bsdf")
1057 {
1058         closure = ccl::CLOSURE_BSDF_DIFFUSE_ID;
1059
1060         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1061         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1062
1063         add_output("BSDF", SHADER_SOCKET_CLOSURE);
1064 }
1065
1066 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1067 {
1068         ShaderInput *color_in = input("Color");
1069
1070         if(color_in->link) {
1071                 compiler.stack_assign(color_in);
1072                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1073         }
1074         else
1075                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1076         
1077         if(param1)
1078                 compiler.stack_assign(param1);
1079         if(param2)
1080                 compiler.stack_assign(param2);
1081
1082         compiler.add_node(NODE_CLOSURE_BSDF,
1083                 compiler.encode_uchar4(closure,
1084                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1085                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1086                         compiler.closure_mix_weight_offset()),
1087                 __float_as_int((param1)? param1->value.x: 0.0f),
1088                 __float_as_int((param2)? param2->value.x: 0.0f));
1089 }
1090
1091 void BsdfNode::compile(SVMCompiler& compiler)
1092 {
1093         compile(compiler, NULL, NULL);
1094 }
1095
1096 void BsdfNode::compile(OSLCompiler& compiler)
1097 {
1098         assert(0);
1099 }
1100
1101 /* Ward BSDF Closure */
1102
1103 WardBsdfNode::WardBsdfNode()
1104 {
1105         closure = CLOSURE_BSDF_WARD_ID;
1106
1107         add_input("Roughness U", SHADER_SOCKET_FLOAT, 0.2f);
1108         add_input("Roughness V", SHADER_SOCKET_FLOAT, 0.2f);
1109 }
1110
1111 void WardBsdfNode::compile(SVMCompiler& compiler)
1112 {
1113         BsdfNode::compile(compiler, input("Roughness U"), input("Roughness V"));
1114 }
1115
1116 void WardBsdfNode::compile(OSLCompiler& compiler)
1117 {
1118         compiler.add(this, "node_ward_bsdf");
1119 }
1120
1121 /* Glossy BSDF Closure */
1122
1123 static ShaderEnum glossy_distribution_init()
1124 {
1125         ShaderEnum enm;
1126
1127         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1128         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1129         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1130
1131         return enm;
1132 }
1133
1134 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1135
1136 GlossyBsdfNode::GlossyBsdfNode()
1137 {
1138         distribution = ustring("Beckmann");
1139
1140         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1141 }
1142
1143 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1144 {
1145         closure = (ClosureType)distribution_enum[distribution];
1146
1147         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1148                 BsdfNode::compile(compiler, NULL, NULL);
1149         else
1150                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1151 }
1152
1153 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1154 {
1155         compiler.parameter("distribution", distribution);
1156         compiler.add(this, "node_glossy_bsdf");
1157 }
1158
1159 /* Glass BSDF Closure */
1160
1161 static ShaderEnum glass_distribution_init()
1162 {
1163         ShaderEnum enm;
1164
1165         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1166         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1167         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1168
1169         return enm;
1170 }
1171
1172 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1173
1174 GlassBsdfNode::GlassBsdfNode()
1175 {
1176         distribution = ustring("Sharp");
1177
1178         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1179         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1180 }
1181
1182 void GlassBsdfNode::compile(SVMCompiler& compiler)
1183 {
1184         closure = (ClosureType)distribution_enum[distribution];
1185
1186         if(closure == CLOSURE_BSDF_REFRACTION_ID)
1187                 BsdfNode::compile(compiler, NULL, input("IOR"));
1188         else
1189                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1190 }
1191
1192 void GlassBsdfNode::compile(OSLCompiler& compiler)
1193 {
1194         compiler.parameter("distribution", distribution);
1195         compiler.add(this, "node_glass_bsdf");
1196 }
1197
1198 /* Velvet BSDF Closure */
1199
1200 VelvetBsdfNode::VelvetBsdfNode()
1201 {
1202         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1203
1204         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1205 }
1206
1207 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1208 {
1209         BsdfNode::compile(compiler, input("Sigma"), NULL);
1210 }
1211
1212 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1213 {
1214         compiler.add(this, "node_velvet_bsdf");
1215 }
1216
1217 /* Diffuse BSDF Closure */
1218
1219 DiffuseBsdfNode::DiffuseBsdfNode()
1220 {
1221         closure = CLOSURE_BSDF_DIFFUSE_ID;
1222         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1223 }
1224
1225 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1226 {
1227         BsdfNode::compile(compiler, input("Roughness"), NULL);
1228 }
1229
1230 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1231 {
1232         compiler.add(this, "node_diffuse_bsdf");
1233 }
1234
1235 /* Translucent BSDF Closure */
1236
1237 TranslucentBsdfNode::TranslucentBsdfNode()
1238 {
1239         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1240 }
1241
1242 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1243 {
1244         BsdfNode::compile(compiler, NULL, NULL);
1245 }
1246
1247 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1248 {
1249         compiler.add(this, "node_translucent_bsdf");
1250 }
1251
1252 /* Transparent BSDF Closure */
1253
1254 TransparentBsdfNode::TransparentBsdfNode()
1255 {
1256         name = "transparent";
1257         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1258 }
1259
1260 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1261 {
1262         BsdfNode::compile(compiler, NULL, NULL);
1263 }
1264
1265 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1266 {
1267         compiler.add(this, "node_transparent_bsdf");
1268 }
1269
1270 /* Emissive Closure */
1271
1272 EmissionNode::EmissionNode()
1273 : ShaderNode("emission")
1274 {
1275         total_power = false;
1276
1277         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1278         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1279         add_output("Emission", SHADER_SOCKET_CLOSURE);
1280 }
1281
1282 void EmissionNode::compile(SVMCompiler& compiler)
1283 {
1284         ShaderInput *color_in = input("Color");
1285         ShaderInput *strength_in = input("Strength");
1286
1287         if(color_in->link || strength_in->link) {
1288                 compiler.stack_assign(color_in);
1289                 compiler.stack_assign(strength_in);
1290                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1291         }
1292         else if(total_power)
1293                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1294         else
1295                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1296
1297         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1298 }
1299
1300 void EmissionNode::compile(OSLCompiler& compiler)
1301 {
1302         compiler.parameter("TotalPower", (total_power)? 1: 0);
1303         compiler.add(this, "node_emission");
1304 }
1305
1306 /* Background Closure */
1307
1308 BackgroundNode::BackgroundNode()
1309 : ShaderNode("background")
1310 {
1311         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1312         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1313         add_output("Background", SHADER_SOCKET_CLOSURE);
1314 }
1315
1316 void BackgroundNode::compile(SVMCompiler& compiler)
1317 {
1318         ShaderInput *color_in = input("Color");
1319         ShaderInput *strength_in = input("Strength");
1320
1321         if(color_in->link || strength_in->link) {
1322                 compiler.stack_assign(color_in);
1323                 compiler.stack_assign(strength_in);
1324                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1325         }
1326         else
1327                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1328
1329         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1330 }
1331
1332 void BackgroundNode::compile(OSLCompiler& compiler)
1333 {
1334         compiler.add(this, "node_background");
1335 }
1336
1337 /* Holdout Closure */
1338
1339 HoldoutNode::HoldoutNode()
1340 : ShaderNode("holdout")
1341 {
1342         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1343 }
1344
1345 void HoldoutNode::compile(SVMCompiler& compiler)
1346 {
1347         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1348 }
1349
1350 void HoldoutNode::compile(OSLCompiler& compiler)
1351 {
1352         compiler.add(this, "node_holdout");
1353 }
1354
1355 /* Volume Closure */
1356
1357 VolumeNode::VolumeNode()
1358 : ShaderNode("volume")
1359 {
1360         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1361
1362         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1363         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1364
1365         add_output("Volume", SHADER_SOCKET_CLOSURE);
1366 }
1367
1368 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1369 {
1370         ShaderInput *color_in = input("Color");
1371
1372         if(color_in->link) {
1373                 compiler.stack_assign(color_in);
1374                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1375         }
1376         else
1377                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1378         
1379         if(param1)
1380                 compiler.stack_assign(param1);
1381         if(param2)
1382                 compiler.stack_assign(param2);
1383
1384         compiler.add_node(NODE_CLOSURE_VOLUME,
1385                 compiler.encode_uchar4(closure,
1386                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1387                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1388                         compiler.closure_mix_weight_offset()),
1389                 __float_as_int((param1)? param1->value.x: 0.0f),
1390                 __float_as_int((param2)? param2->value.x: 0.0f));
1391 }
1392
1393 void VolumeNode::compile(SVMCompiler& compiler)
1394 {
1395         compile(compiler, NULL, NULL);
1396 }
1397
1398 void VolumeNode::compile(OSLCompiler& compiler)
1399 {
1400         assert(0);
1401 }
1402
1403 /* Transparent Volume Closure */
1404
1405 TransparentVolumeNode::TransparentVolumeNode()
1406 {
1407         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1408 }
1409
1410 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1411 {
1412         VolumeNode::compile(compiler, input("Density"), NULL);
1413 }
1414
1415 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1416 {
1417         compiler.add(this, "node_isotropic_volume");
1418 }
1419
1420 /* Isotropic Volume Closure */
1421
1422 IsotropicVolumeNode::IsotropicVolumeNode()
1423 {
1424         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1425 }
1426
1427 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1428 {
1429         VolumeNode::compile(compiler, input("Density"), NULL);
1430 }
1431
1432 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1433 {
1434         compiler.add(this, "node_isotropic_volume");
1435 }
1436
1437 /* Geometry */
1438
1439 GeometryNode::GeometryNode()
1440 : ShaderNode("geometry")
1441 {
1442         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1443         add_output("Position", SHADER_SOCKET_POINT);
1444         add_output("Normal", SHADER_SOCKET_NORMAL);
1445         add_output("Tangent", SHADER_SOCKET_NORMAL);
1446         add_output("True Normal", SHADER_SOCKET_NORMAL);
1447         add_output("Incoming", SHADER_SOCKET_VECTOR);
1448         add_output("Parametric", SHADER_SOCKET_POINT);
1449         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1450 }
1451
1452 void GeometryNode::compile(SVMCompiler& compiler)
1453 {
1454         ShaderOutput *out;
1455         NodeType geom_node = NODE_GEOMETRY;
1456
1457         if(bump == SHADER_BUMP_DX)
1458                 geom_node = NODE_GEOMETRY_BUMP_DX;
1459         else if(bump == SHADER_BUMP_DY)
1460                 geom_node = NODE_GEOMETRY_BUMP_DY;
1461         
1462         out = output("Position");
1463         if(!out->links.empty()) {
1464                 compiler.stack_assign(out);
1465                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1466         }
1467
1468         out = output("Normal");
1469         if(!out->links.empty()) {
1470                 compiler.stack_assign(out);
1471                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1472         }
1473
1474         out = output("Tangent");
1475         if(!out->links.empty()) {
1476                 compiler.stack_assign(out);
1477                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1478         }
1479
1480         out = output("True Normal");
1481         if(!out->links.empty()) {
1482                 compiler.stack_assign(out);
1483                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1484         }
1485
1486         out = output("Incoming");
1487         if(!out->links.empty()) {
1488                 compiler.stack_assign(out);
1489                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1490         }
1491
1492         out = output("Parametric");
1493         if(!out->links.empty()) {
1494                 compiler.stack_assign(out);
1495                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1496         }
1497
1498         out = output("Backfacing");
1499         if(!out->links.empty()) {
1500                 compiler.stack_assign(out);
1501                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1502         }
1503 }
1504
1505 void GeometryNode::compile(OSLCompiler& compiler)
1506 {
1507         if(bump == SHADER_BUMP_DX)
1508                 compiler.parameter("bump_offset", "dx");
1509         else if(bump == SHADER_BUMP_DY)
1510                 compiler.parameter("bump_offset", "dy");
1511         else
1512                 compiler.parameter("bump_offset", "center");
1513
1514         compiler.add(this, "node_geometry");
1515 }
1516
1517 /* TextureCoordinate */
1518
1519 TextureCoordinateNode::TextureCoordinateNode()
1520 : ShaderNode("texture_coordinate")
1521 {
1522         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1523         add_output("Generated", SHADER_SOCKET_POINT);
1524         add_output("Normal", SHADER_SOCKET_NORMAL);
1525         add_output("UV", SHADER_SOCKET_POINT);
1526         add_output("Object", SHADER_SOCKET_POINT);
1527         add_output("Camera", SHADER_SOCKET_POINT);
1528         add_output("Window", SHADER_SOCKET_POINT);
1529         add_output("Reflection", SHADER_SOCKET_NORMAL);
1530 }
1531
1532 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1533 {
1534         if(!output("Generated")->links.empty())
1535                 attributes->add(ATTR_STD_GENERATED);
1536         if(!output("UV")->links.empty())
1537                 attributes->add(ATTR_STD_UV);
1538
1539         ShaderNode::attributes(attributes);
1540 }
1541
1542 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1543 {
1544         ShaderOutput *out;
1545         NodeType texco_node = NODE_TEX_COORD;
1546         NodeType attr_node = NODE_ATTR;
1547         NodeType geom_node = NODE_GEOMETRY;
1548
1549         if(bump == SHADER_BUMP_DX) {
1550                 texco_node = NODE_TEX_COORD_BUMP_DX;
1551                 attr_node = NODE_ATTR_BUMP_DX;
1552                 geom_node = NODE_GEOMETRY_BUMP_DX;
1553         }
1554         else if(bump == SHADER_BUMP_DY) {
1555                 texco_node = NODE_TEX_COORD_BUMP_DY;
1556                 attr_node = NODE_ATTR_BUMP_DY;
1557                 geom_node = NODE_GEOMETRY_BUMP_DY;
1558         }
1559         
1560         out = output("Generated");
1561         if(!out->links.empty()) {
1562                 if(compiler.background) {
1563                         compiler.stack_assign(out);
1564                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1565                 }
1566                 else {
1567                         int attr = compiler.attribute(ATTR_STD_GENERATED);
1568                         compiler.stack_assign(out);
1569                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1570                 }
1571         }
1572
1573         out = output("Normal");
1574         if(!out->links.empty()) {
1575                 compiler.stack_assign(out);
1576                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
1577         }
1578
1579         out = output("UV");
1580         if(!out->links.empty()) {
1581                 int attr = compiler.attribute(ATTR_STD_UV);
1582                 compiler.stack_assign(out);
1583                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1584         }
1585
1586         out = output("Object");
1587         if(!out->links.empty()) {
1588                 compiler.stack_assign(out);
1589                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
1590         }
1591
1592         out = output("Camera");
1593         if(!out->links.empty()) {
1594                 compiler.stack_assign(out);
1595                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
1596         }
1597
1598         out = output("Window");
1599         if(!out->links.empty()) {
1600                 compiler.stack_assign(out);
1601                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
1602         }
1603
1604         out = output("Reflection");
1605         if(!out->links.empty()) {
1606                 if(compiler.background) {
1607                         compiler.stack_assign(out);
1608                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1609                 }
1610                 else {
1611                         compiler.stack_assign(out);
1612                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
1613                 }
1614         }
1615 }
1616
1617 void TextureCoordinateNode::compile(OSLCompiler& compiler)
1618 {
1619         if(bump == SHADER_BUMP_DX)
1620                 compiler.parameter("bump_offset", "dx");
1621         else if(bump == SHADER_BUMP_DY)
1622                 compiler.parameter("bump_offset", "dy");
1623         else
1624                 compiler.parameter("bump_offset", "center");
1625         
1626         if(compiler.background)
1627                 compiler.parameter("is_background", true);
1628
1629         compiler.add(this, "node_texture_coordinate");
1630 }
1631
1632 /* Light Path */
1633
1634 LightPathNode::LightPathNode()
1635 : ShaderNode("light_path")
1636 {
1637         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
1638         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
1639         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
1640         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
1641         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
1642         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
1643         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
1644         add_output("Ray Length", SHADER_SOCKET_FLOAT);
1645 }
1646
1647 void LightPathNode::compile(SVMCompiler& compiler)
1648 {
1649         ShaderOutput *out;
1650
1651         out = output("Is Camera Ray");
1652         if(!out->links.empty()) {
1653                 compiler.stack_assign(out);
1654                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
1655         }
1656
1657         out = output("Is Shadow Ray");
1658         if(!out->links.empty()) {
1659                 compiler.stack_assign(out);
1660                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
1661         }
1662
1663         out = output("Is Diffuse Ray");
1664         if(!out->links.empty()) {
1665                 compiler.stack_assign(out);
1666                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
1667         }
1668
1669         out = output("Is Glossy Ray");
1670         if(!out->links.empty()) {
1671                 compiler.stack_assign(out);
1672                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
1673         }
1674
1675         out = output("Is Singular Ray");
1676         if(!out->links.empty()) {
1677                 compiler.stack_assign(out);
1678                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
1679         }
1680
1681         out = output("Is Reflection Ray");
1682         if(!out->links.empty()) {
1683                 compiler.stack_assign(out);
1684                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
1685         }
1686
1687
1688         out = output("Is Transmission Ray");
1689         if(!out->links.empty()) {
1690                 compiler.stack_assign(out);
1691                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
1692         }
1693         
1694         out = output("Ray Length");
1695         if(!out->links.empty()) {
1696                 compiler.stack_assign(out);
1697                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
1698         }
1699
1700 }
1701
1702 void LightPathNode::compile(OSLCompiler& compiler)
1703 {
1704         compiler.add(this, "node_light_path");
1705 }
1706
1707 /* Light Falloff */
1708
1709 LightFalloffNode::LightFalloffNode()
1710 : ShaderNode("light_path")
1711 {
1712         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
1713         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1714         add_output("Quadratic", SHADER_SOCKET_FLOAT);
1715         add_output("Linear", SHADER_SOCKET_FLOAT);
1716         add_output("Constant", SHADER_SOCKET_FLOAT);
1717 }
1718
1719 void LightFalloffNode::compile(SVMCompiler& compiler)
1720 {
1721         ShaderInput *strength_in = input("Strength");
1722         ShaderInput *smooth_in = input("Smooth");
1723
1724         compiler.stack_assign(strength_in);
1725         compiler.stack_assign(smooth_in);
1726
1727         ShaderOutput *out = output("Quadratic");
1728         if(!out->links.empty()) {
1729                 compiler.stack_assign(out);
1730                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
1731                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1732         }
1733
1734         out = output("Linear");
1735         if(!out->links.empty()) {
1736                 compiler.stack_assign(out);
1737                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
1738                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1739         }
1740
1741         out = output("Constant");
1742         if(!out->links.empty()) {
1743                 compiler.stack_assign(out);
1744                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
1745                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1746         }
1747 }
1748
1749 void LightFalloffNode::compile(OSLCompiler& compiler)
1750 {
1751         compiler.add(this, "node_light_falloff");
1752 }
1753
1754 /* Object Info */
1755
1756 ObjectInfoNode::ObjectInfoNode()
1757 : ShaderNode("object_info")
1758 {
1759         add_output("Location", SHADER_SOCKET_VECTOR);
1760         add_output("Object Index", SHADER_SOCKET_FLOAT);
1761         add_output("Material Index", SHADER_SOCKET_FLOAT);
1762         add_output("Random", SHADER_SOCKET_FLOAT);
1763 }
1764
1765 void ObjectInfoNode::compile(SVMCompiler& compiler)
1766 {
1767         ShaderOutput *out = output("Location");
1768         if(!out->links.empty()) {
1769                 compiler.stack_assign(out);
1770                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
1771         }
1772
1773         out = output("Object Index");
1774         if(!out->links.empty()) {
1775                 compiler.stack_assign(out);
1776                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
1777         }
1778
1779         out = output("Material Index");
1780         if(!out->links.empty()) {
1781                 compiler.stack_assign(out);
1782                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
1783         }
1784
1785         out = output("Random");
1786         if(!out->links.empty()) {
1787                 compiler.stack_assign(out);
1788                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
1789         }
1790 }
1791
1792 void ObjectInfoNode::compile(OSLCompiler& compiler)
1793 {
1794         compiler.add(this, "node_object_info");
1795 }
1796
1797 /* Particle Info */
1798
1799 ParticleInfoNode::ParticleInfoNode()
1800 : ShaderNode("particle_info")
1801 {
1802         add_output("Index", SHADER_SOCKET_FLOAT);
1803         add_output("Age", SHADER_SOCKET_FLOAT);
1804         add_output("Lifetime", SHADER_SOCKET_FLOAT);
1805         add_output("Location", SHADER_SOCKET_POINT);
1806         #if 0   /* not yet supported */
1807         add_output("Rotation", SHADER_SOCKET_QUATERNION);
1808         #endif
1809         add_output("Size", SHADER_SOCKET_FLOAT);
1810         add_output("Velocity", SHADER_SOCKET_VECTOR);
1811         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
1812 }
1813
1814 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
1815 {
1816         if(!output("Index")->links.empty())
1817                 attributes->add(ATTR_STD_PARTICLE);
1818         if(!output("Age")->links.empty())
1819                 attributes->add(ATTR_STD_PARTICLE);
1820         if(!output("Lifetime")->links.empty())
1821                 attributes->add(ATTR_STD_PARTICLE);
1822         if(!output("Location")->links.empty())
1823                 attributes->add(ATTR_STD_PARTICLE);
1824         #if 0   /* not yet supported */
1825         if(!output("Rotation")->links.empty())
1826                 attributes->add(ATTR_STD_PARTICLE);
1827         #endif
1828         if(!output("Size")->links.empty())
1829                 attributes->add(ATTR_STD_PARTICLE);
1830         if(!output("Velocity")->links.empty())
1831                 attributes->add(ATTR_STD_PARTICLE);
1832         if(!output("Angular Velocity")->links.empty())
1833                 attributes->add(ATTR_STD_PARTICLE);
1834
1835         ShaderNode::attributes(attributes);
1836 }
1837
1838 void ParticleInfoNode::compile(SVMCompiler& compiler)
1839 {
1840         ShaderOutput *out;
1841         
1842         out = output("Index");
1843         if(!out->links.empty()) {
1844                 compiler.stack_assign(out);
1845                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
1846         }
1847         
1848         out = output("Age");
1849         if(!out->links.empty()) {
1850                 compiler.stack_assign(out);
1851                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
1852         }
1853         
1854         out = output("Lifetime");
1855         if(!out->links.empty()) {
1856                 compiler.stack_assign(out);
1857                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
1858         }
1859         
1860         out = output("Location");
1861         if(!out->links.empty()) {
1862                 compiler.stack_assign(out);
1863                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
1864         }
1865         
1866         #if 0   /* XXX Quaternion data is not yet supported by Cycles */
1867         out = output("Rotation");
1868         if(!out->links.empty()) {
1869                 compiler.stack_assign(out);
1870                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
1871         }
1872         #endif
1873         
1874         out = output("Size");
1875         if(!out->links.empty()) {
1876                 compiler.stack_assign(out);
1877                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
1878         }
1879         
1880         out = output("Velocity");
1881         if(!out->links.empty()) {
1882                 compiler.stack_assign(out);
1883                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
1884         }
1885         
1886         out = output("Angular Velocity");
1887         if(!out->links.empty()) {
1888                 compiler.stack_assign(out);
1889                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
1890         }
1891 }
1892
1893 void ParticleInfoNode::compile(OSLCompiler& compiler)
1894 {
1895         compiler.add(this, "node_particle_info");
1896 }
1897
1898 /* Value */
1899
1900 ValueNode::ValueNode()
1901 : ShaderNode("value")
1902 {
1903         value = 0.0f;
1904
1905         add_output("Value", SHADER_SOCKET_FLOAT);
1906 }
1907
1908 void ValueNode::compile(SVMCompiler& compiler)
1909 {
1910         ShaderOutput *val_out = output("Value");
1911
1912         compiler.stack_assign(val_out);
1913         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
1914 }
1915
1916 void ValueNode::compile(OSLCompiler& compiler)
1917 {
1918         compiler.parameter("value_value", value);
1919         compiler.add(this, "node_value");
1920 }
1921
1922 /* Color */
1923
1924 ColorNode::ColorNode()
1925 : ShaderNode("color")
1926 {
1927         value = make_float3(0.0f, 0.0f, 0.0f);
1928
1929         add_output("Color", SHADER_SOCKET_COLOR);
1930 }
1931
1932 void ColorNode::compile(SVMCompiler& compiler)
1933 {
1934         ShaderOutput *color_out = output("Color");
1935
1936         if(color_out && !color_out->links.empty()) {
1937                 compiler.stack_assign(color_out);
1938                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
1939                 compiler.add_node(NODE_VALUE_V, value);
1940         }
1941 }
1942
1943 void ColorNode::compile(OSLCompiler& compiler)
1944 {
1945         compiler.parameter_color("color_value", value);
1946
1947         compiler.add(this, "node_value");
1948 }
1949
1950 /* Add Closure */
1951
1952 AddClosureNode::AddClosureNode()
1953 : ShaderNode("add_closure")
1954 {
1955         add_input("Closure1", SHADER_SOCKET_CLOSURE);
1956         add_input("Closure2", SHADER_SOCKET_CLOSURE);
1957         add_output("Closure",  SHADER_SOCKET_CLOSURE);
1958 }
1959
1960 void AddClosureNode::compile(SVMCompiler& compiler)
1961 {
1962         /* handled in the SVM compiler */
1963 }
1964
1965 void AddClosureNode::compile(OSLCompiler& compiler)
1966 {
1967         compiler.add(this, "node_add_closure");
1968 }
1969
1970 /* Mix Closure */
1971
1972 MixClosureNode::MixClosureNode()
1973 : ShaderNode("mix_closure")
1974 {
1975         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
1976         
1977         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1978         add_input("Closure1", SHADER_SOCKET_CLOSURE);
1979         add_input("Closure2", SHADER_SOCKET_CLOSURE);
1980         add_output("Closure",  SHADER_SOCKET_CLOSURE);
1981 }
1982
1983 void MixClosureNode::compile(SVMCompiler& compiler)
1984 {
1985         /* handled in the SVM compiler */
1986 }
1987
1988 void MixClosureNode::compile(OSLCompiler& compiler)
1989 {
1990         compiler.add(this, "node_mix_closure");
1991 }
1992
1993 /* Invert */
1994
1995 InvertNode::InvertNode()
1996 : ShaderNode("invert")
1997 {
1998         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
1999         add_input("Color", SHADER_SOCKET_COLOR);
2000         add_output("Color",  SHADER_SOCKET_COLOR);
2001 }
2002
2003 void InvertNode::compile(SVMCompiler& compiler)
2004 {
2005         ShaderInput *fac_in = input("Fac");
2006         ShaderInput *color_in = input("Color");
2007         ShaderOutput *color_out = output("Color");
2008
2009         compiler.stack_assign(fac_in);
2010         compiler.stack_assign(color_in);
2011         compiler.stack_assign(color_out);
2012
2013         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2014 }
2015
2016 void InvertNode::compile(OSLCompiler& compiler)
2017 {
2018         compiler.add(this, "node_invert");
2019 }
2020
2021 /* Mix */
2022
2023 MixNode::MixNode()
2024 : ShaderNode("mix")
2025 {
2026         type = ustring("Mix");
2027
2028         use_clamp = false;
2029
2030         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2031         add_input("Color1", SHADER_SOCKET_COLOR);
2032         add_input("Color2", SHADER_SOCKET_COLOR);
2033         add_output("Color",  SHADER_SOCKET_COLOR);
2034 }
2035
2036 static ShaderEnum mix_type_init()
2037 {
2038         ShaderEnum enm;
2039
2040         enm.insert("Mix", NODE_MIX_BLEND);
2041         enm.insert("Add", NODE_MIX_ADD);
2042         enm.insert("Multiply", NODE_MIX_MUL);
2043         enm.insert("Screen", NODE_MIX_SCREEN);
2044         enm.insert("Overlay", NODE_MIX_OVERLAY);
2045         enm.insert("Subtract", NODE_MIX_SUB);
2046         enm.insert("Divide", NODE_MIX_DIV);
2047         enm.insert("Difference", NODE_MIX_DIFF);
2048         enm.insert("Darken", NODE_MIX_DARK);
2049         enm.insert("Lighten", NODE_MIX_LIGHT);
2050         enm.insert("Dodge", NODE_MIX_DODGE);
2051         enm.insert("Burn", NODE_MIX_BURN);
2052         enm.insert("Hue", NODE_MIX_HUE);
2053         enm.insert("Saturation", NODE_MIX_SAT);
2054         enm.insert("Value", NODE_MIX_VAL );
2055         enm.insert("Color", NODE_MIX_COLOR);
2056         enm.insert("Soft Light", NODE_MIX_SOFT);
2057         enm.insert("Linear Light", NODE_MIX_LINEAR);
2058
2059         return enm;
2060 }
2061
2062 ShaderEnum MixNode::type_enum = mix_type_init();
2063
2064 void MixNode::compile(SVMCompiler& compiler)
2065 {
2066         ShaderInput *fac_in = input("Fac");
2067         ShaderInput *color1_in = input("Color1");
2068         ShaderInput *color2_in = input("Color2");
2069         ShaderOutput *color_out = output("Color");
2070
2071         compiler.stack_assign(fac_in);
2072         compiler.stack_assign(color1_in);
2073         compiler.stack_assign(color2_in);
2074         compiler.stack_assign(color_out);
2075
2076         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2077         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2078
2079         if(use_clamp) {
2080                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2081                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2082         }
2083 }
2084
2085 void MixNode::compile(OSLCompiler& compiler)
2086 {
2087         compiler.parameter("type", type);
2088         compiler.parameter("Clamp", use_clamp);
2089         compiler.add(this, "node_mix");
2090 }
2091
2092 /* Combine RGB */
2093 CombineRGBNode::CombineRGBNode()
2094 : ShaderNode("combine_rgb")
2095 {
2096         add_input("R", SHADER_SOCKET_FLOAT);
2097         add_input("G", SHADER_SOCKET_FLOAT);
2098         add_input("B", SHADER_SOCKET_FLOAT);
2099         add_output("Image", SHADER_SOCKET_COLOR);
2100 }
2101
2102 void CombineRGBNode::compile(SVMCompiler& compiler)
2103 {
2104         ShaderInput *red_in = input("R");
2105         ShaderInput *green_in = input("G");
2106         ShaderInput *blue_in = input("B");
2107         ShaderOutput *color_out = output("Image");
2108
2109         compiler.stack_assign(color_out);
2110
2111         compiler.stack_assign(red_in);
2112         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2113
2114         compiler.stack_assign(green_in);
2115         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2116
2117         compiler.stack_assign(blue_in);
2118         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2119 }
2120
2121 void CombineRGBNode::compile(OSLCompiler& compiler)
2122 {
2123         compiler.add(this, "node_combine_rgb");
2124 }
2125
2126 /* Gamma */
2127 GammaNode::GammaNode()
2128 : ShaderNode("gamma")
2129 {
2130         add_input("Color", SHADER_SOCKET_COLOR);
2131         add_input("Gamma", SHADER_SOCKET_FLOAT);
2132         add_output("Color", SHADER_SOCKET_COLOR);
2133 }
2134
2135 void GammaNode::compile(SVMCompiler& compiler)
2136 {
2137         ShaderInput *color_in = input("Color");
2138         ShaderInput *gamma_in = input("Gamma");
2139         ShaderOutput *color_out = output("Color");
2140
2141         compiler.stack_assign(color_in);
2142         compiler.stack_assign(gamma_in);
2143         compiler.stack_assign(color_out);
2144
2145         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2146 }
2147
2148 void GammaNode::compile(OSLCompiler& compiler)
2149 {
2150         compiler.add(this, "node_gamma");
2151 }
2152
2153 /* Bright Contrast */
2154 BrightContrastNode::BrightContrastNode()
2155 : ShaderNode("brightness")
2156 {
2157         add_input("Color", SHADER_SOCKET_COLOR);
2158         add_input("Bright", SHADER_SOCKET_FLOAT);
2159         add_input("Contrast", SHADER_SOCKET_FLOAT);
2160         add_output("Color", SHADER_SOCKET_COLOR);
2161 }
2162
2163 void BrightContrastNode::compile(SVMCompiler& compiler)
2164 {
2165         ShaderInput *color_in = input("Color");
2166         ShaderInput *bright_in = input("Bright");
2167         ShaderInput *contrast_in = input("Contrast");
2168         ShaderOutput *color_out = output("Color");
2169
2170         compiler.stack_assign(color_in);
2171         compiler.stack_assign(bright_in);
2172         compiler.stack_assign(contrast_in);
2173         compiler.stack_assign(color_out);
2174
2175         compiler.add_node(NODE_BRIGHTCONTRAST,
2176                 color_in->stack_offset, color_out->stack_offset,
2177                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2178 }
2179
2180 void BrightContrastNode::compile(OSLCompiler& compiler)
2181 {
2182         compiler.add(this, "node_brightness");
2183 }
2184
2185 /* Separate RGB */
2186 SeparateRGBNode::SeparateRGBNode()
2187 : ShaderNode("separate_rgb")
2188 {
2189         add_input("Image", SHADER_SOCKET_COLOR);
2190         add_output("R", SHADER_SOCKET_FLOAT);
2191         add_output("G", SHADER_SOCKET_FLOAT);
2192         add_output("B", SHADER_SOCKET_FLOAT);
2193 }
2194
2195 void SeparateRGBNode::compile(SVMCompiler& compiler)
2196 {
2197         ShaderInput *color_in = input("Image");
2198         ShaderOutput *red_out = output("R");
2199         ShaderOutput *green_out = output("G");
2200         ShaderOutput *blue_out = output("B");
2201
2202         compiler.stack_assign(color_in);
2203
2204         compiler.stack_assign(red_out);
2205         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2206
2207         compiler.stack_assign(green_out);
2208         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2209
2210         compiler.stack_assign(blue_out);
2211         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2212 }
2213
2214 void SeparateRGBNode::compile(OSLCompiler& compiler)
2215 {
2216         compiler.add(this, "node_separate_rgb");
2217 }
2218
2219 /* Separate RGB */
2220 HSVNode::HSVNode()
2221 : ShaderNode("hsv")
2222 {
2223         add_input("Hue", SHADER_SOCKET_FLOAT);
2224         add_input("Saturation", SHADER_SOCKET_FLOAT);
2225         add_input("Value", SHADER_SOCKET_FLOAT);
2226         add_input("Fac", SHADER_SOCKET_FLOAT);
2227         add_input("Color", SHADER_SOCKET_COLOR);
2228         add_output("Color", SHADER_SOCKET_COLOR);
2229 }
2230
2231 void HSVNode::compile(SVMCompiler& compiler)
2232 {
2233         ShaderInput *hue_in = input("Hue");
2234         ShaderInput *saturation_in = input("Saturation");
2235         ShaderInput *value_in = input("Value");
2236         ShaderInput *fac_in = input("Fac");
2237         ShaderInput *color_in = input("Color");
2238         ShaderOutput *color_out = output("Color");
2239
2240         compiler.stack_assign(hue_in);
2241         compiler.stack_assign(saturation_in);
2242         compiler.stack_assign(value_in);
2243         compiler.stack_assign(fac_in);
2244         compiler.stack_assign(color_in);
2245         compiler.stack_assign(color_out);
2246
2247         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2248         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2249 }
2250
2251 void HSVNode::compile(OSLCompiler& compiler)
2252 {
2253         compiler.add(this, "node_hsv");
2254 }
2255
2256 /* Attribute */
2257
2258 AttributeNode::AttributeNode()
2259 : ShaderNode("attribute")
2260 {
2261         attribute = "";
2262
2263         add_output("Color",  SHADER_SOCKET_COLOR);
2264         add_output("Vector",  SHADER_SOCKET_VECTOR);
2265         add_output("Fac",  SHADER_SOCKET_FLOAT);
2266 }
2267
2268 void AttributeNode::attributes(AttributeRequestSet *attributes)
2269 {
2270         ShaderOutput *color_out = output("Color");
2271         ShaderOutput *vector_out = output("Vector");
2272         ShaderOutput *fac_out = output("Fac");
2273
2274         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2275                 attributes->add(attribute);
2276         
2277         ShaderNode::attributes(attributes);
2278 }
2279
2280 void AttributeNode::compile(SVMCompiler& compiler)
2281 {
2282         ShaderOutput *color_out = output("Color");
2283         ShaderOutput *vector_out = output("Vector");
2284         ShaderOutput *fac_out = output("Fac");
2285         NodeType attr_node = NODE_ATTR;
2286
2287         if(bump == SHADER_BUMP_DX)
2288                 attr_node = NODE_ATTR_BUMP_DX;
2289         else if(bump == SHADER_BUMP_DY)
2290                 attr_node = NODE_ATTR_BUMP_DY;
2291
2292         if(!color_out->links.empty() || !vector_out->links.empty()) {
2293                 int attr = compiler.attribute(attribute);
2294
2295                 if(!color_out->links.empty()) {
2296                         compiler.stack_assign(color_out);
2297                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2298                 }
2299                 if(!vector_out->links.empty()) {
2300                         compiler.stack_assign(vector_out);
2301                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2302                 }
2303         }
2304
2305         if(!fac_out->links.empty()) {
2306                 int attr = compiler.attribute(attribute);
2307
2308                 compiler.stack_assign(fac_out);
2309                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2310         }
2311 }
2312
2313 void AttributeNode::compile(OSLCompiler& compiler)
2314 {
2315         if(bump == SHADER_BUMP_DX)
2316                 compiler.parameter("bump_offset", "dx");
2317         else if(bump == SHADER_BUMP_DY)
2318                 compiler.parameter("bump_offset", "dy");
2319         else
2320                 compiler.parameter("bump_offset", "center");
2321
2322         compiler.parameter("name", attribute.c_str());
2323         compiler.add(this, "node_attribute");
2324 }
2325
2326 /* Camera */
2327
2328 CameraNode::CameraNode()
2329 : ShaderNode("camera")
2330 {
2331         add_output("View Vector",  SHADER_SOCKET_VECTOR);
2332         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
2333         add_output("View Distance",  SHADER_SOCKET_FLOAT);
2334 }
2335
2336 void CameraNode::compile(SVMCompiler& compiler)
2337 {
2338         ShaderOutput *vector_out = output("View Vector");
2339         ShaderOutput *z_depth_out = output("View Z Depth");
2340         ShaderOutput *distance_out = output("View Distance");
2341
2342         compiler.stack_assign(vector_out);
2343         compiler.stack_assign(z_depth_out);
2344         compiler.stack_assign(distance_out);
2345         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2346 }
2347
2348 void CameraNode::compile(OSLCompiler& compiler)
2349 {
2350         compiler.add(this, "node_camera");
2351 }
2352
2353 /* Fresnel */
2354
2355 FresnelNode::FresnelNode()
2356 : ShaderNode("Fresnel")
2357 {
2358         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2359         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2360         add_output("Fac", SHADER_SOCKET_FLOAT);
2361 }
2362
2363 void FresnelNode::compile(SVMCompiler& compiler)
2364 {
2365         ShaderInput *ior_in = input("IOR");
2366         ShaderOutput *fac_out = output("Fac");
2367
2368         compiler.stack_assign(ior_in);
2369         compiler.stack_assign(fac_out);
2370         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2371 }
2372
2373 void FresnelNode::compile(OSLCompiler& compiler)
2374 {
2375         compiler.add(this, "node_fresnel");
2376 }
2377
2378 /* Blend Weight */
2379
2380 LayerWeightNode::LayerWeightNode()
2381 : ShaderNode("LayerWeight")
2382 {
2383         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2384         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2385
2386         add_output("Fresnel", SHADER_SOCKET_FLOAT);
2387         add_output("Facing", SHADER_SOCKET_FLOAT);
2388 }
2389
2390 void LayerWeightNode::compile(SVMCompiler& compiler)
2391 {
2392         ShaderInput *blend_in = input("Blend");
2393
2394         if(blend_in->link)
2395                 compiler.stack_assign(blend_in);
2396
2397         ShaderOutput *fresnel_out = output("Fresnel");
2398         if(!fresnel_out->links.empty()) {
2399                 compiler.stack_assign(fresnel_out);
2400                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2401                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
2402         }
2403
2404         ShaderOutput *facing_out = output("Facing");
2405         if(!facing_out->links.empty()) {
2406                 compiler.stack_assign(facing_out);
2407                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2408                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
2409         }
2410 }
2411
2412 void LayerWeightNode::compile(OSLCompiler& compiler)
2413 {
2414         compiler.add(this, "node_blend_weight");
2415 }
2416
2417 /* Output */
2418
2419 OutputNode::OutputNode()
2420 : ShaderNode("output")
2421 {
2422         add_input("Surface", SHADER_SOCKET_CLOSURE);
2423         add_input("Volume", SHADER_SOCKET_CLOSURE);
2424         add_input("Displacement", SHADER_SOCKET_FLOAT);
2425 }
2426
2427 void OutputNode::compile(SVMCompiler& compiler)
2428 {
2429         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
2430                 ShaderInput *displacement_in = input("Displacement");
2431
2432                 if(displacement_in->link) {
2433                         compiler.stack_assign(displacement_in);
2434                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
2435                 }
2436         }
2437 }
2438
2439 void OutputNode::compile(OSLCompiler& compiler)
2440 {
2441         if(compiler.output_type() == SHADER_TYPE_SURFACE)
2442                 compiler.add(this, "node_output_surface");
2443         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
2444                 compiler.add(this, "node_output_volume");
2445         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
2446                 compiler.add(this, "node_output_displacement");
2447 }
2448
2449 /* Math */
2450
2451 MathNode::MathNode()
2452 : ShaderNode("math")
2453 {
2454         type = ustring("Add");
2455
2456         use_clamp = false;
2457
2458         add_input("Value1", SHADER_SOCKET_FLOAT);
2459         add_input("Value2", SHADER_SOCKET_FLOAT);
2460         add_output("Value",  SHADER_SOCKET_FLOAT);
2461 }
2462
2463 static ShaderEnum math_type_init()
2464 {
2465         ShaderEnum enm;
2466
2467         enm.insert("Add", NODE_MATH_ADD);
2468         enm.insert("Subtract", NODE_MATH_SUBTRACT);
2469         enm.insert("Multiply", NODE_MATH_MULTIPLY);
2470         enm.insert("Divide", NODE_MATH_DIVIDE);
2471         enm.insert("Sine", NODE_MATH_SINE);
2472         enm.insert("Cosine", NODE_MATH_COSINE);
2473         enm.insert("Tangent", NODE_MATH_TANGENT);
2474         enm.insert("Arcsine", NODE_MATH_ARCSINE);
2475         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
2476         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
2477         enm.insert("Power", NODE_MATH_POWER);
2478         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
2479         enm.insert("Minimum", NODE_MATH_MINIMUM);
2480         enm.insert("Maximum", NODE_MATH_MAXIMUM);
2481         enm.insert("Round", NODE_MATH_ROUND);
2482         enm.insert("Less Than", NODE_MATH_LESS_THAN);
2483         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
2484
2485         return enm;
2486 }
2487
2488 ShaderEnum MathNode::type_enum = math_type_init();
2489
2490 void MathNode::compile(SVMCompiler& compiler)
2491 {
2492         ShaderInput *value1_in = input("Value1");
2493         ShaderInput *value2_in = input("Value2");
2494         ShaderOutput *value_out = output("Value");
2495
2496         compiler.stack_assign(value1_in);
2497         compiler.stack_assign(value2_in);
2498         compiler.stack_assign(value_out);
2499
2500         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
2501         compiler.add_node(NODE_MATH, value_out->stack_offset);
2502
2503         if(use_clamp) {
2504                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
2505                 compiler.add_node(NODE_MATH, value_out->stack_offset);
2506         }
2507 }
2508
2509 void MathNode::compile(OSLCompiler& compiler)
2510 {
2511         compiler.parameter("type", type);
2512         compiler.parameter("Clamp", use_clamp);
2513         compiler.add(this, "node_math");
2514 }
2515
2516 /* VectorMath */
2517
2518 VectorMathNode::VectorMathNode()
2519 : ShaderNode("vector_math")
2520 {
2521         type = ustring("Add");
2522
2523         add_input("Vector1", SHADER_SOCKET_VECTOR);
2524         add_input("Vector2", SHADER_SOCKET_VECTOR);
2525         add_output("Value",  SHADER_SOCKET_FLOAT);
2526         add_output("Vector",  SHADER_SOCKET_VECTOR);
2527 }
2528
2529 static ShaderEnum vector_math_type_init()
2530 {
2531         ShaderEnum enm;
2532
2533         enm.insert("Add", NODE_VECTOR_MATH_ADD);
2534         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
2535         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
2536         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
2537         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
2538         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
2539
2540         return enm;
2541 }
2542
2543 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
2544
2545 void VectorMathNode::compile(SVMCompiler& compiler)
2546 {
2547         ShaderInput *vector1_in = input("Vector1");
2548         ShaderInput *vector2_in = input("Vector2");
2549         ShaderOutput *value_out = output("Value");
2550         ShaderOutput *vector_out = output("Vector");
2551
2552         compiler.stack_assign(vector1_in);
2553         compiler.stack_assign(vector2_in);
2554         compiler.stack_assign(value_out);
2555         compiler.stack_assign(vector_out);
2556
2557         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
2558         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
2559 }
2560
2561 void VectorMathNode::compile(OSLCompiler& compiler)
2562 {
2563         compiler.parameter("type", type);
2564         compiler.add(this, "node_vector_math");
2565 }
2566
2567 /* BumpNode */
2568
2569 BumpNode::BumpNode()
2570 : ShaderNode("bump")
2571 {
2572         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
2573         add_input("SampleX", SHADER_SOCKET_FLOAT);
2574         add_input("SampleY", SHADER_SOCKET_FLOAT);
2575
2576         add_output("Normal", SHADER_SOCKET_NORMAL);
2577 }
2578
2579 void BumpNode::compile(SVMCompiler& compiler)
2580 {
2581         ShaderInput *center_in = input("SampleCenter");
2582         ShaderInput *dx_in = input("SampleX");
2583         ShaderInput *dy_in = input("SampleY");
2584
2585         compiler.stack_assign(center_in);
2586         compiler.stack_assign(dx_in);
2587         compiler.stack_assign(dy_in);
2588
2589         compiler.add_node(NODE_SET_BUMP, center_in->stack_offset, dx_in->stack_offset, dy_in->stack_offset);
2590 }
2591
2592 void BumpNode::compile(OSLCompiler& compiler)
2593 {
2594         compiler.add(this, "node_bump");
2595 }
2596
2597 /* RGBCurvesNode */
2598
2599 RGBCurvesNode::RGBCurvesNode()
2600 : ShaderNode("rgb_curves")
2601 {
2602         add_input("Fac", SHADER_SOCKET_FLOAT);
2603         add_input("Color", SHADER_SOCKET_COLOR);
2604         add_output("Color", SHADER_SOCKET_COLOR);
2605 }
2606
2607 void RGBCurvesNode::compile(SVMCompiler& compiler)
2608 {
2609         ShaderInput *fac_in = input("Fac");
2610         ShaderInput *color_in = input("Color");
2611         ShaderOutput *color_out = output("Color");
2612
2613         compiler.stack_assign(fac_in);
2614         compiler.stack_assign(color_in);
2615         compiler.stack_assign(color_out);
2616
2617         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2618         compiler.add_array(curves, RAMP_TABLE_SIZE);
2619 }
2620
2621 void RGBCurvesNode::compile(OSLCompiler& compiler)
2622 {
2623         compiler.add(this, "node_rgb_curves");
2624 }
2625
2626 /* RGBRampNode */
2627
2628 RGBRampNode::RGBRampNode()
2629 : ShaderNode("rgb_ramp")
2630 {
2631         add_input("Fac", SHADER_SOCKET_FLOAT);
2632         add_output("Color", SHADER_SOCKET_COLOR);
2633         add_output("Alpha", SHADER_SOCKET_FLOAT);
2634 }
2635
2636 void RGBRampNode::compile(SVMCompiler& compiler)
2637 {
2638         ShaderInput *fac_in = input("Fac");
2639         ShaderOutput *color_out = output("Color");
2640         ShaderOutput *alpha_out = output("Alpha");
2641
2642         compiler.stack_assign(fac_in);
2643         if(!color_out->links.empty())
2644                 compiler.stack_assign(color_out);
2645         if(!alpha_out->links.empty())
2646                 compiler.stack_assign(alpha_out);
2647
2648         compiler.add_node(NODE_RGB_RAMP, fac_in->stack_offset, color_out->stack_offset, alpha_out->stack_offset);
2649         compiler.add_array(ramp, RAMP_TABLE_SIZE);
2650 }
2651
2652 void RGBRampNode::compile(OSLCompiler& compiler)
2653 {
2654         compiler.add(this, "node_rgb_ramp");
2655 }
2656
2657 CCL_NAMESPACE_END
2658