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