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