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