Cycles / OSL:
[blender.git] / intern / cycles / render / nodes.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "image.h"
20 #include "nodes.h"
21 #include "svm.h"
22 #include "osl.h"
23
24 #include "util_transform.h"
25
26 CCL_NAMESPACE_BEGIN
27
28 /* Texture Mapping */
29
30 TextureMapping::TextureMapping()
31 {
32         translation = make_float3(0.0f, 0.0f, 0.0f);
33         rotation = make_float3(0.0f, 0.0f, 0.0f);
34         scale = make_float3(1.0f, 1.0f, 1.0f);
35
36         min = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
37         max = make_float3(FLT_MAX, FLT_MAX, FLT_MAX);
38
39         use_minmax = false;
40
41         x_mapping = X;
42         y_mapping = Y;
43         z_mapping = Z;
44
45         projection = FLAT;
46 }
47
48 Transform TextureMapping::compute_transform()
49 {
50         Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
51
52         if(x_mapping != NONE)
53                 mmat[0][x_mapping-1] = 1.0f;
54         if(y_mapping != NONE)
55                 mmat[1][y_mapping-1] = 1.0f;
56         if(z_mapping != NONE)
57                 mmat[2][z_mapping-1] = 1.0f;
58
59         Transform smat = transform_scale(scale);
60         Transform rmat = transform_euler(rotation);
61         Transform tmat = transform_translate(translation);
62
63         return tmat*rmat*smat*mmat;
64 }
65
66 bool TextureMapping::skip()
67 {
68         if(translation != make_float3(0.0f, 0.0f, 0.0f))
69                 return false;
70         if(rotation != make_float3(0.0f, 0.0f, 0.0f))
71                 return false;
72         if(scale != make_float3(1.0f, 1.0f, 1.0f))
73                 return false;
74         
75         if(x_mapping != X || y_mapping != Y || z_mapping != Z)
76                 return false;
77         if(use_minmax)
78                 return false;
79         
80         return true;
81 }
82
83 void TextureMapping::compile(SVMCompiler& compiler, int offset_in, int offset_out)
84 {
85         if(offset_in == SVM_STACK_INVALID || offset_out == SVM_STACK_INVALID)
86                 return;
87
88         compiler.add_node(NODE_MAPPING, offset_in, offset_out);
89
90         Transform tfm = compute_transform();
91         compiler.add_node(tfm.x);
92         compiler.add_node(tfm.y);
93         compiler.add_node(tfm.z);
94         compiler.add_node(tfm.w);
95
96         if(use_minmax) {
97                 compiler.add_node(NODE_MIN_MAX, offset_out, offset_out);
98                 compiler.add_node(float3_to_float4(min));
99                 compiler.add_node(float3_to_float4(max));
100         }
101 }
102
103 /* 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
1040         add_input("Input", from);
1041         add_output("Output", to);
1042 }
1043
1044 void ProxyNode::compile(SVMCompiler& compiler)
1045 {
1046 }
1047
1048 void ProxyNode::compile(OSLCompiler& compiler)
1049 {
1050 }
1051
1052 /* BSDF Closure */
1053
1054 BsdfNode::BsdfNode()
1055 : ShaderNode("bsdf")
1056 {
1057         closure = ccl::CLOSURE_BSDF_DIFFUSE_ID;
1058
1059         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1060         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1061
1062         add_output("BSDF", SHADER_SOCKET_CLOSURE);
1063 }
1064
1065 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1066 {
1067         ShaderInput *color_in = input("Color");
1068
1069         if(color_in->link) {
1070                 compiler.stack_assign(color_in);
1071                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1072         }
1073         else
1074                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1075         
1076         if(param1)
1077                 compiler.stack_assign(param1);
1078         if(param2)
1079                 compiler.stack_assign(param2);
1080
1081         compiler.add_node(NODE_CLOSURE_BSDF,
1082                 compiler.encode_uchar4(closure,
1083                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1084                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1085                         compiler.closure_mix_weight_offset()),
1086                 __float_as_int((param1)? param1->value.x: 0.0f),
1087                 __float_as_int((param2)? param2->value.x: 0.0f));
1088 }
1089
1090 void BsdfNode::compile(SVMCompiler& compiler)
1091 {
1092         compile(compiler, NULL, NULL);
1093 }
1094
1095 void BsdfNode::compile(OSLCompiler& compiler)
1096 {
1097         assert(0);
1098 }
1099
1100 /* Ward BSDF Closure */
1101
1102 WardBsdfNode::WardBsdfNode()
1103 {
1104         closure = CLOSURE_BSDF_WARD_ID;
1105
1106         add_input("Roughness U", SHADER_SOCKET_FLOAT, 0.2f);
1107         add_input("Roughness V", SHADER_SOCKET_FLOAT, 0.2f);
1108 }
1109
1110 void WardBsdfNode::compile(SVMCompiler& compiler)
1111 {
1112         BsdfNode::compile(compiler, input("Roughness U"), input("Roughness V"));
1113 }
1114
1115 void WardBsdfNode::compile(OSLCompiler& compiler)
1116 {
1117         compiler.add(this, "node_ward_bsdf");
1118 }
1119
1120 /* Glossy BSDF Closure */
1121
1122 static ShaderEnum glossy_distribution_init()
1123 {
1124         ShaderEnum enm;
1125
1126         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1127         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1128         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1129
1130         return enm;
1131 }
1132
1133 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1134
1135 GlossyBsdfNode::GlossyBsdfNode()
1136 {
1137         distribution = ustring("Beckmann");
1138
1139         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1140 }
1141
1142 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1143 {
1144         closure = (ClosureType)distribution_enum[distribution];
1145
1146         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1147                 BsdfNode::compile(compiler, NULL, NULL);
1148         else
1149                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1150 }
1151
1152 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1153 {
1154         compiler.parameter("distribution", distribution);
1155         compiler.add(this, "node_glossy_bsdf");
1156 }
1157
1158 /* Glass BSDF Closure */
1159
1160 static ShaderEnum glass_distribution_init()
1161 {
1162         ShaderEnum enm;
1163
1164         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1165         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1166         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1167
1168         return enm;
1169 }
1170
1171 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1172
1173 GlassBsdfNode::GlassBsdfNode()
1174 {
1175         distribution = ustring("Sharp");
1176
1177         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1178         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1179 }
1180
1181 void GlassBsdfNode::compile(SVMCompiler& compiler)
1182 {
1183         closure = (ClosureType)distribution_enum[distribution];
1184
1185         if(closure == CLOSURE_BSDF_REFRACTION_ID)
1186                 BsdfNode::compile(compiler, NULL, input("IOR"));
1187         else
1188                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1189 }
1190
1191 void GlassBsdfNode::compile(OSLCompiler& compiler)
1192 {
1193         compiler.parameter("distribution", distribution);
1194         compiler.add(this, "node_glass_bsdf");
1195 }
1196
1197 /* Velvet BSDF Closure */
1198
1199 VelvetBsdfNode::VelvetBsdfNode()
1200 {
1201         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1202
1203         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1204 }
1205
1206 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1207 {
1208         BsdfNode::compile(compiler, input("Sigma"), NULL);
1209 }
1210
1211 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1212 {
1213         compiler.add(this, "node_velvet_bsdf");
1214 }
1215
1216 /* Diffuse BSDF Closure */
1217
1218 DiffuseBsdfNode::DiffuseBsdfNode()
1219 {
1220         closure = CLOSURE_BSDF_DIFFUSE_ID;
1221         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1222 }
1223
1224 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1225 {
1226         BsdfNode::compile(compiler, input("Roughness"), NULL);
1227 }
1228
1229 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1230 {
1231         compiler.add(this, "node_diffuse_bsdf");
1232 }
1233
1234 /* Translucent BSDF Closure */
1235
1236 TranslucentBsdfNode::TranslucentBsdfNode()
1237 {
1238         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1239 }
1240
1241 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1242 {
1243         BsdfNode::compile(compiler, NULL, NULL);
1244 }
1245
1246 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1247 {
1248         compiler.add(this, "node_translucent_bsdf");
1249 }
1250
1251 /* Transparent BSDF Closure */
1252
1253 TransparentBsdfNode::TransparentBsdfNode()
1254 {
1255         name = "transparent";
1256         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1257 }
1258
1259 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1260 {
1261         BsdfNode::compile(compiler, NULL, NULL);
1262 }
1263
1264 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1265 {
1266         compiler.add(this, "node_transparent_bsdf");
1267 }
1268
1269 /* Emissive Closure */
1270
1271 EmissionNode::EmissionNode()
1272 : ShaderNode("emission")
1273 {
1274         total_power = false;
1275
1276         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1277         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1278         add_output("Emission", SHADER_SOCKET_CLOSURE);
1279 }
1280
1281 void EmissionNode::compile(SVMCompiler& compiler)
1282 {
1283         ShaderInput *color_in = input("Color");
1284         ShaderInput *strength_in = input("Strength");
1285
1286         if(color_in->link || strength_in->link) {
1287                 compiler.stack_assign(color_in);
1288                 compiler.stack_assign(strength_in);
1289                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1290         }
1291         else if(total_power)
1292                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1293         else
1294                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1295
1296         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1297 }
1298
1299 void EmissionNode::compile(OSLCompiler& compiler)
1300 {
1301         compiler.parameter("TotalPower", (total_power)? 1: 0);
1302         compiler.add(this, "node_emission");
1303 }
1304
1305 /* Background Closure */
1306
1307 BackgroundNode::BackgroundNode()
1308 : ShaderNode("background")
1309 {
1310         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1311         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1312         add_output("Background", SHADER_SOCKET_CLOSURE);
1313 }
1314
1315 void BackgroundNode::compile(SVMCompiler& compiler)
1316 {
1317         ShaderInput *color_in = input("Color");
1318         ShaderInput *strength_in = input("Strength");
1319
1320         if(color_in->link || strength_in->link) {
1321                 compiler.stack_assign(color_in);
1322                 compiler.stack_assign(strength_in);
1323                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1324         }
1325         else
1326                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1327
1328         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1329 }
1330
1331 void BackgroundNode::compile(OSLCompiler& compiler)
1332 {
1333         compiler.add(this, "node_background");
1334 }
1335
1336 /* Holdout Closure */
1337
1338 HoldoutNode::HoldoutNode()
1339 : ShaderNode("holdout")
1340 {
1341         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1342 }
1343
1344 void HoldoutNode::compile(SVMCompiler& compiler)
1345 {
1346         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1347 }
1348
1349 void HoldoutNode::compile(OSLCompiler& compiler)
1350 {
1351         compiler.add(this, "node_holdout");
1352 }
1353
1354 /* Volume Closure */
1355
1356 VolumeNode::VolumeNode()
1357 : ShaderNode("volume")
1358 {
1359         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1360
1361         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1362         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1363
1364         add_output("Volume", SHADER_SOCKET_CLOSURE);
1365 }
1366
1367 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1368 {
1369         ShaderInput *color_in = input("Color");
1370
1371         if(color_in->link) {
1372                 compiler.stack_assign(color_in);
1373                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1374         }
1375         else
1376                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1377         
1378         if(param1)
1379                 compiler.stack_assign(param1);
1380         if(param2)
1381                 compiler.stack_assign(param2);
1382
1383         compiler.add_node(NODE_CLOSURE_VOLUME,
1384                 compiler.encode_uchar4(closure,
1385                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1386                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1387                         compiler.closure_mix_weight_offset()),
1388                 __float_as_int((param1)? param1->value.x: 0.0f),
1389                 __float_as_int((param2)? param2->value.x: 0.0f));
1390 }
1391
1392 void VolumeNode::compile(SVMCompiler& compiler)
1393 {
1394         compile(compiler, NULL, NULL);
1395 }
1396
1397 void VolumeNode::compile(OSLCompiler& compiler)
1398 {
1399         assert(0);
1400 }
1401
1402 /* Transparent Volume Closure */
1403
1404 TransparentVolumeNode::TransparentVolumeNode()
1405 {
1406         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1407 }
1408
1409 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1410 {
1411         VolumeNode::compile(compiler, input("Density"), NULL);
1412 }
1413
1414 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1415 {
1416         compiler.add(this, "node_isotropic_volume");
1417 }
1418
1419 /* Isotropic Volume Closure */
1420
1421 IsotropicVolumeNode::IsotropicVolumeNode()
1422 {
1423         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1424 }
1425
1426 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1427 {
1428         VolumeNode::compile(compiler, input("Density"), NULL);
1429 }
1430
1431 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1432 {
1433         compiler.add(this, "node_isotropic_volume");
1434 }
1435
1436 /* Geometry */
1437
1438 GeometryNode::GeometryNode()
1439 : ShaderNode("geometry")
1440 {
1441         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1442         add_output("Position", SHADER_SOCKET_POINT);
1443         add_output("Normal", SHADER_SOCKET_NORMAL);
1444         add_output("Tangent", SHADER_SOCKET_NORMAL);
1445         add_output("True Normal", SHADER_SOCKET_NORMAL);
1446         add_output("Incoming", SHADER_SOCKET_VECTOR);
1447         add_output("Parametric", SHADER_SOCKET_POINT);
1448         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1449 }
1450
1451 void GeometryNode::compile(SVMCompiler& compiler)
1452 {
1453         ShaderOutput *out;
1454         NodeType geom_node = NODE_GEOMETRY;
1455
1456         if(bump == SHADER_BUMP_DX)
1457                 geom_node = NODE_GEOMETRY_BUMP_DX;
1458         else if(bump == SHADER_BUMP_DY)
1459                 geom_node = NODE_GEOMETRY_BUMP_DY;
1460         
1461         out = output("Position");
1462         if(!out->links.empty()) {
1463                 compiler.stack_assign(out);
1464                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1465         }
1466
1467         out = output("Normal");
1468         if(!out->links.empty()) {
1469                 compiler.stack_assign(out);
1470                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1471         }
1472
1473         out = output("Tangent");
1474         if(!out->links.empty()) {
1475                 compiler.stack_assign(out);
1476                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1477         }
1478
1479         out = output("True Normal");
1480         if(!out->links.empty()) {
1481                 compiler.stack_assign(out);
1482                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1483         }
1484
1485         out = output("Incoming");
1486         if(!out->links.empty()) {
1487                 compiler.stack_assign(out);
1488                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1489         }
1490
1491         out = output("Parametric");
1492         if(!out->links.empty()) {
1493                 compiler.stack_assign(out);
1494                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1495         }
1496
1497         out = output("Backfacing");
1498         if(!out->links.empty()) {
1499                 compiler.stack_assign(out);
1500                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1501         }
1502 }
1503
1504 void GeometryNode::compile(OSLCompiler& compiler)
1505 {
1506         if(bump == SHADER_BUMP_DX)
1507                 compiler.parameter("bump_offset", "dx");
1508         else if(bump == SHADER_BUMP_DY)
1509                 compiler.parameter("bump_offset", "dy");
1510         else
1511                 compiler.parameter("bump_offset", "center");
1512
1513         compiler.add(this, "node_geometry");
1514 }
1515
1516 /* TextureCoordinate */
1517
1518 TextureCoordinateNode::TextureCoordinateNode()
1519 : ShaderNode("texture_coordinate")
1520 {
1521         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1522         add_output("Generated", SHADER_SOCKET_POINT);
1523         add_output("Normal", SHADER_SOCKET_NORMAL);
1524         add_output("UV", SHADER_SOCKET_POINT);
1525         add_output("Object", SHADER_SOCKET_POINT);
1526         add_output("Camera", SHADER_SOCKET_POINT);
1527         add_output("Window", SHADER_SOCKET_POINT);
1528         add_output("Reflection", SHADER_SOCKET_NORMAL);
1529 }
1530
1531 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1532 {
1533         if(!output("Generated")->links.empty())
1534                 attributes->add(ATTR_STD_GENERATED);
1535         if(!output("UV")->links.empty())
1536                 attributes->add(ATTR_STD_UV);
1537
1538         ShaderNode::attributes(attributes);
1539 }
1540
1541 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1542 {
1543         ShaderOutput *out;
1544         NodeType texco_node = NODE_TEX_COORD;
1545         NodeType attr_node = NODE_ATTR;
1546         NodeType geom_node = NODE_GEOMETRY;
1547
1548         if(bump == SHADER_BUMP_DX) {
1549                 texco_node = NODE_TEX_COORD_BUMP_DX;
1550                 attr_node = NODE_ATTR_BUMP_DX;
1551                 geom_node = NODE_GEOMETRY_BUMP_DX;
1552         }
1553         else if(bump == SHADER_BUMP_DY) {
1554                 texco_node = NODE_TEX_COORD_BUMP_DY;
1555                 attr_node = NODE_ATTR_BUMP_DY;
1556                 geom_node = NODE_GEOMETRY_BUMP_DY;
1557         }
1558         
1559         out = output("Generated");
1560         if(!out->links.empty()) {
1561                 if(compiler.background) {
1562                         compiler.stack_assign(out);
1563                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1564                 }
1565                 else {
1566                         int attr = compiler.attribute(ATTR_STD_GENERATED);
1567                         compiler.stack_assign(out);
1568                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1569                 }
1570         }
1571
1572         out = output("Normal");
1573         if(!out->links.empty()) {
1574                 compiler.stack_assign(out);
1575                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
1576         }
1577
1578         out = output("UV");
1579         if(!out->links.empty()) {
1580                 int attr = compiler.attribute(ATTR_STD_UV);
1581                 compiler.stack_assign(out);
1582                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1583         }
1584
1585         out = output("Object");
1586         if(!out->links.empty()) {
1587                 compiler.stack_assign(out);
1588                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
1589         }
1590
1591         out = output("Camera");
1592         if(!out->links.empty()) {
1593                 compiler.stack_assign(out);
1594                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
1595         }
1596
1597         out = output("Window");
1598         if(!out->links.empty()) {
1599                 compiler.stack_assign(out);
1600                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
1601         }
1602
1603         out = output("Reflection");
1604         if(!out->links.empty()) {
1605                 if(compiler.background) {
1606                         compiler.stack_assign(out);
1607                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1608                 }
1609                 else {
1610                         compiler.stack_assign(out);
1611                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
1612                 }
1613         }
1614 }
1615
1616 void TextureCoordinateNode::compile(OSLCompiler& compiler)
1617 {
1618         if(bump == SHADER_BUMP_DX)
1619                 compiler.parameter("bump_offset", "dx");
1620         else if(bump == SHADER_BUMP_DY)
1621                 compiler.parameter("bump_offset", "dy");
1622         else
1623                 compiler.parameter("bump_offset", "center");
1624         
1625         if(compiler.background)
1626                 compiler.parameter("is_background", true);
1627
1628         compiler.add(this, "node_texture_coordinate");
1629 }
1630
1631 /* Light Path */
1632
1633 LightPathNode::LightPathNode()
1634 : ShaderNode("light_path")
1635 {
1636         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
1637         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
1638         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
1639         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
1640         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
1641         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
1642         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
1643         add_output("Ray Length", SHADER_SOCKET_FLOAT);
1644 }
1645
1646 void LightPathNode::compile(SVMCompiler& compiler)
1647 {
1648         ShaderOutput *out;
1649
1650         out = output("Is Camera Ray");
1651         if(!out->links.empty()) {
1652                 compiler.stack_assign(out);
1653                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
1654         }
1655
1656         out = output("Is Shadow Ray");
1657         if(!out->links.empty()) {
1658                 compiler.stack_assign(out);
1659                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
1660         }
1661
1662         out = output("Is Diffuse Ray");
1663         if(!out->links.empty()) {
1664                 compiler.stack_assign(out);
1665                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
1666         }
1667
1668         out = output("Is Glossy Ray");
1669         if(!out->links.empty()) {
1670                 compiler.stack_assign(out);
1671                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
1672         }
1673
1674         out = output("Is Singular Ray");
1675         if(!out->links.empty()) {
1676                 compiler.stack_assign(out);
1677                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
1678         }
1679
1680         out = output("Is Reflection Ray");
1681         if(!out->links.empty()) {
1682                 compiler.stack_assign(out);
1683                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
1684         }
1685
1686
1687         out = output("Is Transmission Ray");
1688         if(!out->links.empty()) {
1689                 compiler.stack_assign(out);
1690                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
1691         }
1692         
1693         out = output("Ray Length");
1694         if(!out->links.empty()) {
1695                 compiler.stack_assign(out);
1696                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
1697         }
1698
1699 }
1700
1701 void LightPathNode::compile(OSLCompiler& compiler)
1702 {
1703         compiler.add(this, "node_light_path");
1704 }
1705
1706 /* Light Falloff */
1707
1708 LightFalloffNode::LightFalloffNode()
1709 : ShaderNode("light_path")
1710 {
1711         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
1712         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1713         add_output("Quadratic", SHADER_SOCKET_FLOAT);
1714         add_output("Linear", SHADER_SOCKET_FLOAT);
1715         add_output("Constant", SHADER_SOCKET_FLOAT);
1716 }
1717
1718 void LightFalloffNode::compile(SVMCompiler& compiler)
1719 {
1720         ShaderInput *strength_in = input("Strength");
1721         ShaderInput *smooth_in = input("Smooth");
1722
1723         compiler.stack_assign(strength_in);
1724         compiler.stack_assign(smooth_in);
1725
1726         ShaderOutput *out = output("Quadratic");
1727         if(!out->links.empty()) {
1728                 compiler.stack_assign(out);
1729                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
1730                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1731         }
1732
1733         out = output("Linear");
1734         if(!out->links.empty()) {
1735                 compiler.stack_assign(out);
1736                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
1737                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1738         }
1739
1740         out = output("Constant");
1741         if(!out->links.empty()) {
1742                 compiler.stack_assign(out);
1743                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
1744                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1745         }
1746 }
1747
1748 void LightFalloffNode::compile(OSLCompiler& compiler)
1749 {
1750         compiler.add(this, "node_light_falloff");
1751 }
1752
1753 /* Object Info */
1754
1755 ObjectInfoNode::ObjectInfoNode()
1756 : ShaderNode("object_info")
1757 {
1758         add_output("Location", SHADER_SOCKET_VECTOR);
1759         add_output("Object Index", SHADER_SOCKET_FLOAT);
1760         add_output("Material Index", SHADER_SOCKET_FLOAT);
1761         add_output("Random", SHADER_SOCKET_FLOAT);
1762 }
1763
1764 void ObjectInfoNode::compile(SVMCompiler& compiler)
1765 {
1766         ShaderOutput *out = output("Location");
1767         if(!out->links.empty()) {
1768                 compiler.stack_assign(out);
1769                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
1770         }
1771
1772         out = output("Object Index");
1773         if(!out->links.empty()) {
1774                 compiler.stack_assign(out);
1775                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
1776         }
1777
1778         out = output("Material Index");
1779         if(!out->links.empty()) {
1780                 compiler.stack_assign(out);
1781                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
1782         }
1783
1784         out = output("Random");
1785         if(!out->links.empty()) {
1786                 compiler.stack_assign(out);
1787                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
1788         }
1789 }
1790
1791 void ObjectInfoNode::compile(OSLCompiler& compiler)
1792 {
1793         compiler.add(this, "node_object_info");
1794 }
1795
1796 /* Particle Info */
1797
1798 ParticleInfoNode::ParticleInfoNode()
1799 : ShaderNode("particle_info")
1800 {
1801         add_output("Index", SHADER_SOCKET_FLOAT);
1802         add_output("Age", SHADER_SOCKET_FLOAT);
1803         add_output("Lifetime", SHADER_SOCKET_FLOAT);
1804         add_output("Location", SHADER_SOCKET_POINT);
1805         #if 0   /* not yet supported */
1806         add_output("Rotation", SHADER_SOCKET_QUATERNION);
1807         #endif
1808         add_output("Size", SHADER_SOCKET_FLOAT);
1809         add_output("Velocity", SHADER_SOCKET_VECTOR);
1810         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
1811 }
1812
1813 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
1814 {
1815         if(!output("Index")->links.empty())
1816                 attributes->add(ATTR_STD_PARTICLE);
1817         if(!output("Age")->links.empty())
1818                 attributes->add(ATTR_STD_PARTICLE);
1819         if(!output("Lifetime")->links.empty())
1820                 attributes->add(ATTR_STD_PARTICLE);
1821         if(!output("Location")->links.empty())
1822                 attributes->add(ATTR_STD_PARTICLE);
1823         #if 0   /* not yet supported */
1824         if(!output("Rotation")->links.empty())
1825                 attributes->add(ATTR_STD_PARTICLE);
1826         #endif
1827         if(!output("Size")->links.empty())
1828                 attributes->add(ATTR_STD_PARTICLE);
1829         if(!output("Velocity")->links.empty())
1830                 attributes->add(ATTR_STD_PARTICLE);
1831         if(!output("Angular Velocity")->links.empty())
1832                 attributes->add(ATTR_STD_PARTICLE);
1833
1834         ShaderNode::attributes(attributes);
1835 }
1836
1837 void ParticleInfoNode::compile(SVMCompiler& compiler)
1838 {
1839         ShaderOutput *out;
1840         
1841         out = output("Index");
1842         if(!out->links.empty()) {
1843                 compiler.stack_assign(out);
1844                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
1845         }
1846         
1847         out = output("Age");
1848         if(!out->links.empty()) {
1849                 compiler.stack_assign(out);
1850                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
1851         }
1852         
1853         out = output("Lifetime");
1854         if(!out->links.empty()) {
1855                 compiler.stack_assign(out);
1856                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
1857         }
1858         
1859         out = output("Location");
1860         if(!out->links.empty()) {
1861                 compiler.stack_assign(out);
1862                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
1863         }
1864         
1865         #if 0   /* XXX Quaternion data is not yet supported by Cycles */
1866         out = output("Rotation");
1867         if(!out->links.empty()) {
1868                 compiler.stack_assign(out);
1869                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
1870         }
1871         #endif
1872         
1873         out = output("Size");
1874         if(!out->links.empty()) {
1875                 compiler.stack_assign(out);
1876                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
1877         }
1878         
1879         out = output("Velocity");
1880         if(!out->links.empty()) {
1881                 compiler.stack_assign(out);
1882                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
1883         }
1884         
1885         out = output("Angular Velocity");
1886         if(!out->links.empty()) {
1887                 compiler.stack_assign(out);
1888                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
1889         }
1890 }
1891
1892 void ParticleInfoNode::compile(OSLCompiler& compiler)
1893 {
1894         compiler.add(this, "node_particle_info");
1895 }
1896
1897 /* Value */
1898
1899 ValueNode::ValueNode()
1900 : ShaderNode("value")
1901 {
1902         value = 0.0f;
1903
1904         add_output("Value", SHADER_SOCKET_FLOAT);
1905 }
1906
1907 void ValueNode::compile(SVMCompiler& compiler)
1908 {
1909         ShaderOutput *val_out = output("Value");
1910
1911         compiler.stack_assign(val_out);
1912         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
1913 }
1914
1915 void ValueNode::compile(OSLCompiler& compiler)
1916 {
1917         compiler.parameter("value_value", value);
1918         compiler.add(this, "node_value");
1919 }
1920
1921 /* Color */
1922
1923 ColorNode::ColorNode()
1924 : ShaderNode("color")
1925 {
1926         value = make_float3(0.0f, 0.0f, 0.0f);
1927
1928         add_output("Color", SHADER_SOCKET_COLOR);
1929 }
1930
1931 void ColorNode::compile(SVMCompiler& compiler)
1932 {
1933         ShaderOutput *color_out = output("Color");
1934
1935         if(color_out && !color_out->links.empty()) {
1936                 compiler.stack_assign(color_out);
1937                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
1938                 compiler.add_node(NODE_VALUE_V, value);
1939         }
1940 }
1941
1942 void ColorNode::compile(OSLCompiler& compiler)
1943 {
1944         compiler.parameter_color("color_value", value);
1945
1946         compiler.add(this, "node_value");
1947 }
1948
1949 /* Add Closure */
1950
1951 AddClosureNode::AddClosureNode()
1952 : ShaderNode("add_closure")
1953 {
1954         add_input("Closure1", SHADER_SOCKET_CLOSURE);
1955         add_input("Closure2", SHADER_SOCKET_CLOSURE);
1956         add_output("Closure",  SHADER_SOCKET_CLOSURE);
1957 }
1958
1959 void AddClosureNode::compile(SVMCompiler& compiler)
1960 {
1961         /* handled in the SVM compiler */
1962 }
1963
1964 void AddClosureNode::compile(OSLCompiler& compiler)
1965 {
1966         compiler.add(this, "node_add_closure");
1967 }
1968
1969 /* Mix Closure */
1970
1971 MixClosureNode::MixClosureNode()
1972 : ShaderNode("mix_closure")
1973 {
1974         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1975         add_input("Closure1", SHADER_SOCKET_CLOSURE);
1976         add_input("Closure2", SHADER_SOCKET_CLOSURE);
1977         add_output("Closure",  SHADER_SOCKET_CLOSURE);
1978 }
1979
1980 void MixClosureNode::compile(SVMCompiler& compiler)
1981 {
1982         /* handled in the SVM compiler */
1983 }
1984
1985 void MixClosureNode::compile(OSLCompiler& compiler)
1986 {
1987         compiler.add(this, "node_mix_closure");
1988 }
1989
1990 /* Invert */
1991
1992 InvertNode::InvertNode()
1993 : ShaderNode("invert")
1994 {
1995         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
1996         add_input("Color", SHADER_SOCKET_COLOR);
1997         add_output("Color",  SHADER_SOCKET_COLOR);
1998 }
1999
2000 void InvertNode::compile(SVMCompiler& compiler)
2001 {
2002         ShaderInput *fac_in = input("Fac");
2003         ShaderInput *color_in = input("Color");
2004         ShaderOutput *color_out = output("Color");
2005
2006         compiler.stack_assign(fac_in);
2007         compiler.stack_assign(color_in);
2008         compiler.stack_assign(color_out);
2009
2010         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2011 }
2012
2013 void InvertNode::compile(OSLCompiler& compiler)
2014 {
2015         compiler.add(this, "node_invert");
2016 }
2017
2018 /* Mix */
2019
2020 MixNode::MixNode()
2021 : ShaderNode("mix")
2022 {
2023         type = ustring("Mix");
2024
2025         use_clamp = false;
2026
2027         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2028         add_input("Color1", SHADER_SOCKET_COLOR);
2029         add_input("Color2", SHADER_SOCKET_COLOR);
2030         add_output("Color",  SHADER_SOCKET_COLOR);
2031 }
2032
2033 static ShaderEnum mix_type_init()
2034 {
2035         ShaderEnum enm;
2036
2037         enm.insert("Mix", NODE_MIX_BLEND);
2038         enm.insert("Add", NODE_MIX_ADD);
2039         enm.insert("Multiply", NODE_MIX_MUL);
2040         enm.insert("Screen", NODE_MIX_SCREEN);
2041         enm.insert("Overlay", NODE_MIX_OVERLAY);
2042         enm.insert("Subtract", NODE_MIX_SUB);
2043         enm.insert("Divide", NODE_MIX_DIV);
2044         enm.insert("Difference", NODE_MIX_DIFF);
2045         enm.insert("Darken", NODE_MIX_DARK);
2046         enm.insert("Lighten", NODE_MIX_LIGHT);
2047         enm.insert("Dodge", NODE_MIX_DODGE);
2048         enm.insert("Burn", NODE_MIX_BURN);
2049         enm.insert("Hue", NODE_MIX_HUE);
2050         enm.insert("Saturation", NODE_MIX_SAT);
2051         enm.insert("Value", NODE_MIX_VAL );
2052         enm.insert("Color", NODE_MIX_COLOR);
2053         enm.insert("Soft Light", NODE_MIX_SOFT);
2054         enm.insert("Linear Light", NODE_MIX_LINEAR);
2055
2056         return enm;
2057 }
2058
2059 ShaderEnum MixNode::type_enum = mix_type_init();
2060
2061 void MixNode::compile(SVMCompiler& compiler)
2062 {
2063         ShaderInput *fac_in = input("Fac");
2064         ShaderInput *color1_in = input("Color1");
2065         ShaderInput *color2_in = input("Color2");
2066         ShaderOutput *color_out = output("Color");
2067
2068         compiler.stack_assign(fac_in);
2069         compiler.stack_assign(color1_in);
2070         compiler.stack_assign(color2_in);
2071         compiler.stack_assign(color_out);
2072
2073         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2074         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2075
2076         if(use_clamp) {
2077                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2078                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2079         }
2080 }
2081
2082 void MixNode::compile(OSLCompiler& compiler)
2083 {
2084         compiler.parameter("type", type);
2085         compiler.parameter("Clamp", use_clamp);
2086         compiler.add(this, "node_mix");
2087 }
2088
2089 /* Combine RGB */
2090 CombineRGBNode::CombineRGBNode()
2091 : ShaderNode("combine_rgb")
2092 {
2093         add_input("R", SHADER_SOCKET_FLOAT);
2094         add_input("G", SHADER_SOCKET_FLOAT);
2095         add_input("B", SHADER_SOCKET_FLOAT);
2096         add_output("Image", SHADER_SOCKET_COLOR);
2097 }
2098
2099 void CombineRGBNode::compile(SVMCompiler& compiler)
2100 {
2101         ShaderInput *red_in = input("R");
2102         ShaderInput *green_in = input("G");
2103         ShaderInput *blue_in = input("B");
2104         ShaderOutput *color_out = output("Image");
2105
2106         compiler.stack_assign(color_out);
2107
2108         compiler.stack_assign(red_in);
2109         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2110
2111         compiler.stack_assign(green_in);
2112         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2113
2114         compiler.stack_assign(blue_in);
2115         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2116 }
2117
2118 void CombineRGBNode::compile(OSLCompiler& compiler)
2119 {
2120         compiler.add(this, "node_combine_rgb");
2121 }
2122
2123 /* Gamma */
2124 GammaNode::GammaNode()
2125 : ShaderNode("gamma")
2126 {
2127         add_input("Color", SHADER_SOCKET_COLOR);
2128         add_input("Gamma", SHADER_SOCKET_FLOAT);
2129         add_output("Color", SHADER_SOCKET_COLOR);
2130 }
2131
2132 void GammaNode::compile(SVMCompiler& compiler)
2133 {
2134         ShaderInput *color_in = input("Color");
2135         ShaderInput *gamma_in = input("Gamma");
2136         ShaderOutput *color_out = output("Color");
2137
2138         compiler.stack_assign(color_in);
2139         compiler.stack_assign(gamma_in);
2140         compiler.stack_assign(color_out);
2141
2142         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2143 }
2144
2145 void GammaNode::compile(OSLCompiler& compiler)
2146 {
2147         compiler.add(this, "node_gamma");
2148 }
2149
2150 /* Bright Contrast */
2151 BrightContrastNode::BrightContrastNode()
2152 : ShaderNode("brightness")
2153 {
2154         add_input("Color", SHADER_SOCKET_COLOR);
2155         add_input("Bright", SHADER_SOCKET_FLOAT);
2156         add_input("Contrast", SHADER_SOCKET_FLOAT);
2157         add_output("Color", SHADER_SOCKET_COLOR);
2158 }
2159
2160 void BrightContrastNode::compile(SVMCompiler& compiler)
2161 {
2162         ShaderInput *color_in = input("Color");
2163         ShaderInput *bright_in = input("Bright");
2164         ShaderInput *contrast_in = input("Contrast");
2165         ShaderOutput *color_out = output("Color");
2166
2167         compiler.stack_assign(color_in);
2168         compiler.stack_assign(bright_in);
2169         compiler.stack_assign(contrast_in);
2170         compiler.stack_assign(color_out);
2171
2172         compiler.add_node(NODE_BRIGHTCONTRAST,
2173                 color_in->stack_offset, color_out->stack_offset,
2174                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2175 }
2176
2177 void BrightContrastNode::compile(OSLCompiler& compiler)
2178 {
2179         compiler.add(this, "node_brightness");
2180 }
2181
2182 /* Separate RGB */
2183 SeparateRGBNode::SeparateRGBNode()
2184 : ShaderNode("separate_rgb")
2185 {
2186         add_input("Image", SHADER_SOCKET_COLOR);
2187         add_output("R", SHADER_SOCKET_FLOAT);
2188         add_output("G", SHADER_SOCKET_FLOAT);
2189         add_output("B", SHADER_SOCKET_FLOAT);
2190 }
2191
2192 void SeparateRGBNode::compile(SVMCompiler& compiler)
2193 {
2194         ShaderInput *color_in = input("Image");
2195         ShaderOutput *red_out = output("R");
2196         ShaderOutput *green_out = output("G");
2197         ShaderOutput *blue_out = output("B");
2198
2199         compiler.stack_assign(color_in);
2200
2201         compiler.stack_assign(red_out);
2202         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2203
2204         compiler.stack_assign(green_out);
2205         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2206
2207         compiler.stack_assign(blue_out);
2208         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2209 }
2210
2211 void SeparateRGBNode::compile(OSLCompiler& compiler)
2212 {
2213         compiler.add(this, "node_separate_rgb");
2214 }
2215
2216 /* Separate RGB */
2217 HSVNode::HSVNode()
2218 : ShaderNode("hsv")
2219 {
2220         add_input("Hue", SHADER_SOCKET_FLOAT);
2221         add_input("Saturation", SHADER_SOCKET_FLOAT);
2222         add_input("Value", SHADER_SOCKET_FLOAT);
2223         add_input("Fac", SHADER_SOCKET_FLOAT);
2224         add_input("Color", SHADER_SOCKET_COLOR);
2225         add_output("Color", SHADER_SOCKET_COLOR);
2226 }
2227
2228 void HSVNode::compile(SVMCompiler& compiler)
2229 {
2230         ShaderInput *hue_in = input("Hue");
2231         ShaderInput *saturation_in = input("Saturation");
2232         ShaderInput *value_in = input("Value");
2233         ShaderInput *fac_in = input("Fac");
2234         ShaderInput *color_in = input("Color");
2235         ShaderOutput *color_out = output("Color");
2236
2237         compiler.stack_assign(hue_in);
2238         compiler.stack_assign(saturation_in);
2239         compiler.stack_assign(value_in);
2240         compiler.stack_assign(fac_in);
2241         compiler.stack_assign(color_in);
2242         compiler.stack_assign(color_out);
2243
2244         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2245         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2246 }
2247
2248 void HSVNode::compile(OSLCompiler& compiler)
2249 {
2250         compiler.add(this, "node_hsv");
2251 }
2252
2253 /* Attribute */
2254
2255 AttributeNode::AttributeNode()
2256 : ShaderNode("attribute")
2257 {
2258         attribute = "";
2259
2260         add_output("Color",  SHADER_SOCKET_COLOR);
2261         add_output("Vector",  SHADER_SOCKET_VECTOR);
2262         add_output("Fac",  SHADER_SOCKET_FLOAT);
2263 }
2264
2265 void AttributeNode::attributes(AttributeRequestSet *attributes)
2266 {
2267         ShaderOutput *color_out = output("Color");
2268         ShaderOutput *vector_out = output("Vector");
2269         ShaderOutput *fac_out = output("Fac");
2270
2271         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2272                 attributes->add(attribute);
2273         
2274         ShaderNode::attributes(attributes);
2275 }
2276
2277 void AttributeNode::compile(SVMCompiler& compiler)
2278 {
2279         ShaderOutput *color_out = output("Color");
2280         ShaderOutput *vector_out = output("Vector");
2281         ShaderOutput *fac_out = output("Fac");
2282         NodeType attr_node = NODE_ATTR;
2283
2284         if(bump == SHADER_BUMP_DX)
2285                 attr_node = NODE_ATTR_BUMP_DX;
2286         else if(bump == SHADER_BUMP_DY)
2287                 attr_node = NODE_ATTR_BUMP_DY;
2288
2289         if(!color_out->links.empty() || !vector_out->links.empty()) {
2290                 int attr = compiler.attribute(attribute);
2291
2292                 if(!color_out->links.empty()) {
2293                         compiler.stack_assign(color_out);
2294                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2295                 }
2296                 if(!vector_out->links.empty()) {
2297                         compiler.stack_assign(vector_out);
2298                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2299                 }
2300         }
2301
2302         if(!fac_out->links.empty()) {
2303                 int attr = compiler.attribute(attribute);
2304
2305                 compiler.stack_assign(fac_out);
2306                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2307         }
2308 }
2309
2310 void AttributeNode::compile(OSLCompiler& compiler)
2311 {
2312         if(bump == SHADER_BUMP_DX)
2313                 compiler.parameter("bump_offset", "dx");
2314         else if(bump == SHADER_BUMP_DY)
2315                 compiler.parameter("bump_offset", "dy");
2316         else
2317                 compiler.parameter("bump_offset", "center");
2318
2319         compiler.parameter("name", attribute.c_str());
2320         compiler.add(this, "node_attribute");
2321 }
2322
2323 /* Camera */
2324
2325 CameraNode::CameraNode()
2326 : ShaderNode("camera")
2327 {
2328         add_output("View Vector",  SHADER_SOCKET_VECTOR);
2329         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
2330         add_output("View Distance",  SHADER_SOCKET_FLOAT);
2331 }
2332
2333 void CameraNode::compile(SVMCompiler& compiler)
2334 {
2335         ShaderOutput *vector_out = output("View Vector");
2336         ShaderOutput *z_depth_out = output("View Z Depth");
2337         ShaderOutput *distance_out = output("View Distance");
2338
2339         compiler.stack_assign(vector_out);
2340         compiler.stack_assign(z_depth_out);
2341         compiler.stack_assign(distance_out);
2342         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2343 }
2344
2345 void CameraNode::compile(OSLCompiler& compiler)
2346 {
2347         compiler.add(this, "node_camera");
2348 }
2349
2350 /* Fresnel */
2351
2352 FresnelNode::FresnelNode()
2353 : ShaderNode("Fresnel")
2354 {
2355         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2356         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2357         add_output("Fac", SHADER_SOCKET_FLOAT);
2358 }
2359
2360 void FresnelNode::compile(SVMCompiler& compiler)
2361 {
2362         ShaderInput *ior_in = input("IOR");
2363         ShaderOutput *fac_out = output("Fac");
2364
2365         compiler.stack_assign(ior_in);
2366         compiler.stack_assign(fac_out);
2367         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2368 }
2369
2370 void FresnelNode::compile(OSLCompiler& compiler)
2371 {
2372         compiler.add(this, "node_fresnel");
2373 }
2374
2375 /* Blend Weight */
2376
2377 LayerWeightNode::LayerWeightNode()
2378 : ShaderNode("LayerWeight")
2379 {
2380         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2381         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2382
2383         add_output("Fresnel", SHADER_SOCKET_FLOAT);
2384         add_output("Facing", SHADER_SOCKET_FLOAT);
2385 }
2386
2387 void LayerWeightNode::compile(SVMCompiler& compiler)
2388 {
2389         ShaderInput *blend_in = input("Blend");
2390
2391         if(blend_in->link)
2392                 compiler.stack_assign(blend_in);
2393
2394         ShaderOutput *fresnel_out = output("Fresnel");
2395         if(!fresnel_out->links.empty()) {
2396                 compiler.stack_assign(fresnel_out);
2397                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2398                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
2399         }
2400
2401         ShaderOutput *facing_out = output("Facing");
2402         if(!facing_out->links.empty()) {
2403                 compiler.stack_assign(facing_out);
2404                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2405                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
2406         }
2407 }
2408
2409 void LayerWeightNode::compile(OSLCompiler& compiler)
2410 {
2411         compiler.add(this, "node_blend_weight");
2412 }
2413
2414 /* Output */
2415
2416 OutputNode::OutputNode()
2417 : ShaderNode("output")
2418 {
2419         add_input("Surface", SHADER_SOCKET_CLOSURE);
2420         add_input("Volume", SHADER_SOCKET_CLOSURE);
2421         add_input("Displacement", SHADER_SOCKET_FLOAT);
2422 }
2423
2424 void OutputNode::compile(SVMCompiler& compiler)
2425 {
2426         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
2427                 ShaderInput *displacement_in = input("Displacement");
2428
2429                 if(displacement_in->link) {
2430                         compiler.stack_assign(displacement_in);
2431                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
2432                 }
2433         }
2434 }
2435
2436 void OutputNode::compile(OSLCompiler& compiler)
2437 {
2438         if(compiler.output_type() == SHADER_TYPE_SURFACE)
2439                 compiler.add(this, "node_output_surface");
2440         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
2441                 compiler.add(this, "node_output_volume");
2442         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
2443                 compiler.add(this, "node_output_displacement");
2444 }
2445
2446 /* Math */
2447
2448 MathNode::MathNode()
2449 : ShaderNode("math")
2450 {
2451         type = ustring("Add");
2452
2453         use_clamp = false;
2454
2455         add_input("Value1", SHADER_SOCKET_FLOAT);
2456         add_input("Value2", SHADER_SOCKET_FLOAT);
2457         add_output("Value",  SHADER_SOCKET_FLOAT);
2458 }
2459
2460 static ShaderEnum math_type_init()
2461 {
2462         ShaderEnum enm;
2463
2464         enm.insert("Add", NODE_MATH_ADD);
2465         enm.insert("Subtract", NODE_MATH_SUBTRACT);
2466         enm.insert("Multiply", NODE_MATH_MULTIPLY);
2467         enm.insert("Divide", NODE_MATH_DIVIDE);
2468         enm.insert("Sine", NODE_MATH_SINE);
2469         enm.insert("Cosine", NODE_MATH_COSINE);
2470         enm.insert("Tangent", NODE_MATH_TANGENT);
2471         enm.insert("Arcsine", NODE_MATH_ARCSINE);
2472         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
2473         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
2474         enm.insert("Power", NODE_MATH_POWER);
2475         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
2476         enm.insert("Minimum", NODE_MATH_MINIMUM);
2477         enm.insert("Maximum", NODE_MATH_MAXIMUM);
2478         enm.insert("Round", NODE_MATH_ROUND);
2479         enm.insert("Less Than", NODE_MATH_LESS_THAN);
2480         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
2481
2482         return enm;
2483 }
2484
2485 ShaderEnum MathNode::type_enum = math_type_init();
2486
2487 void MathNode::compile(SVMCompiler& compiler)
2488 {
2489         ShaderInput *value1_in = input("Value1");
2490         ShaderInput *value2_in = input("Value2");
2491         ShaderOutput *value_out = output("Value");
2492
2493         compiler.stack_assign(value1_in);
2494         compiler.stack_assign(value2_in);
2495         compiler.stack_assign(value_out);
2496
2497         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
2498         compiler.add_node(NODE_MATH, value_out->stack_offset);
2499
2500         if(use_clamp) {
2501                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
2502                 compiler.add_node(NODE_MATH, value_out->stack_offset);
2503         }
2504 }
2505
2506 void MathNode::compile(OSLCompiler& compiler)
2507 {
2508         compiler.parameter("type", type);
2509         compiler.parameter("Clamp", use_clamp);
2510         compiler.add(this, "node_math");
2511 }
2512
2513 /* VectorMath */
2514
2515 VectorMathNode::VectorMathNode()
2516 : ShaderNode("vector_math")
2517 {
2518         type = ustring("Add");
2519
2520         add_input("Vector1", SHADER_SOCKET_VECTOR);
2521         add_input("Vector2", SHADER_SOCKET_VECTOR);
2522         add_output("Value",  SHADER_SOCKET_FLOAT);
2523         add_output("Vector",  SHADER_SOCKET_VECTOR);
2524 }
2525
2526 static ShaderEnum vector_math_type_init()
2527 {
2528         ShaderEnum enm;
2529
2530         enm.insert("Add", NODE_VECTOR_MATH_ADD);
2531         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
2532         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
2533         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
2534         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
2535         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
2536
2537         return enm;
2538 }
2539
2540 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
2541
2542 void VectorMathNode::compile(SVMCompiler& compiler)
2543 {
2544         ShaderInput *vector1_in = input("Vector1");
2545         ShaderInput *vector2_in = input("Vector2");
2546         ShaderOutput *value_out = output("Value");
2547         ShaderOutput *vector_out = output("Vector");
2548
2549         compiler.stack_assign(vector1_in);
2550         compiler.stack_assign(vector2_in);
2551         compiler.stack_assign(value_out);
2552         compiler.stack_assign(vector_out);
2553
2554         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
2555         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
2556 }
2557
2558 void VectorMathNode::compile(OSLCompiler& compiler)
2559 {
2560         compiler.parameter("type", type);
2561         compiler.add(this, "node_vector_math");
2562 }
2563
2564 /* BumpNode */
2565
2566 BumpNode::BumpNode()
2567 : ShaderNode("bump")
2568 {
2569         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
2570         add_input("SampleX", SHADER_SOCKET_FLOAT);
2571         add_input("SampleY", SHADER_SOCKET_FLOAT);
2572
2573         add_output("Normal", SHADER_SOCKET_NORMAL);
2574 }
2575
2576 void BumpNode::compile(SVMCompiler& compiler)
2577 {
2578         ShaderInput *center_in = input("SampleCenter");
2579         ShaderInput *dx_in = input("SampleX");
2580         ShaderInput *dy_in = input("SampleY");
2581
2582         compiler.stack_assign(center_in);
2583         compiler.stack_assign(dx_in);
2584         compiler.stack_assign(dy_in);
2585
2586         compiler.add_node(NODE_SET_BUMP, center_in->stack_offset, dx_in->stack_offset, dy_in->stack_offset);
2587 }
2588
2589 void BumpNode::compile(OSLCompiler& compiler)
2590 {
2591         compiler.add(this, "node_bump");
2592 }
2593
2594 /* RGBCurvesNode */
2595
2596 RGBCurvesNode::RGBCurvesNode()
2597 : ShaderNode("rgb_curves")
2598 {
2599         add_input("Fac", SHADER_SOCKET_FLOAT);
2600         add_input("Color", SHADER_SOCKET_COLOR);
2601         add_output("Color", SHADER_SOCKET_COLOR);
2602 }
2603
2604 void RGBCurvesNode::compile(SVMCompiler& compiler)
2605 {
2606         ShaderInput *fac_in = input("Fac");
2607         ShaderInput *color_in = input("Color");
2608         ShaderOutput *color_out = output("Color");
2609
2610         compiler.stack_assign(fac_in);
2611         compiler.stack_assign(color_in);
2612         compiler.stack_assign(color_out);
2613
2614         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2615         compiler.add_array(curves, RAMP_TABLE_SIZE);
2616 }
2617
2618 void RGBCurvesNode::compile(OSLCompiler& compiler)
2619 {
2620         compiler.add(this, "node_rgb_curves");
2621 }
2622
2623 /* RGBRampNode */
2624
2625 RGBRampNode::RGBRampNode()
2626 : ShaderNode("rgb_ramp")
2627 {
2628         add_input("Fac", SHADER_SOCKET_FLOAT);
2629         add_output("Color", SHADER_SOCKET_COLOR);
2630         add_output("Alpha", SHADER_SOCKET_FLOAT);
2631 }
2632
2633 void RGBRampNode::compile(SVMCompiler& compiler)
2634 {
2635         ShaderInput *fac_in = input("Fac");
2636         ShaderOutput *color_out = output("Color");
2637         ShaderOutput *alpha_out = output("Alpha");
2638
2639         compiler.stack_assign(fac_in);
2640         if(!color_out->links.empty())
2641                 compiler.stack_assign(color_out);
2642         if(!alpha_out->links.empty())
2643                 compiler.stack_assign(alpha_out);
2644
2645         compiler.add_node(NODE_RGB_RAMP, fac_in->stack_offset, color_out->stack_offset, alpha_out->stack_offset);
2646         compiler.add_array(ramp, RAMP_TABLE_SIZE);
2647 }
2648
2649 void RGBRampNode::compile(OSLCompiler& compiler)
2650 {
2651         compiler.add(this, "node_rgb_ramp");
2652 }
2653
2654 CCL_NAMESPACE_END
2655