3fbcadee42390504af40b450803faf00aa5dcdba
[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 void TextureMapping::compile(OSLCompiler &compiler)
104 {
105         if(!skip()) {
106                 Transform tfm = transform_transpose(compute_transform());
107
108                 compiler.parameter("mapping", tfm);
109                 compiler.parameter("use_mapping", 1);
110         }
111 }
112
113 /* Image Texture */
114
115 static ShaderEnum color_space_init()
116 {
117         ShaderEnum enm;
118
119         enm.insert("None", 0);
120         enm.insert("Color", 1);
121
122         return enm;
123 }
124
125 static ShaderEnum image_projection_init()
126 {
127         ShaderEnum enm;
128
129         enm.insert("Flat", 0);
130         enm.insert("Box", 1);
131
132         return enm;
133 }
134
135 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
136 ShaderEnum ImageTextureNode::projection_enum = image_projection_init();
137
138 ImageTextureNode::ImageTextureNode()
139 : TextureNode("image_texture")
140 {
141         image_manager = NULL;
142         slot = -1;
143         is_float = -1;
144         is_linear = false;
145         filename = "";
146         builtin_data = NULL;
147         color_space = ustring("Color");
148         projection = ustring("Flat");
149         projection_blend = 0.0f;
150         animated = false;
151
152         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_UV);
153         add_output("Color", SHADER_SOCKET_COLOR);
154         add_output("Alpha", SHADER_SOCKET_FLOAT);
155 }
156
157 ImageTextureNode::~ImageTextureNode()
158 {
159         if(image_manager)
160                 image_manager->remove_image(filename, builtin_data);
161 }
162
163 ShaderNode *ImageTextureNode::clone() const
164 {
165         ImageTextureNode *node = new ImageTextureNode(*this);
166         node->image_manager = NULL;
167         node->slot = -1;
168         node->is_float = -1;
169         node->is_linear = false;
170         return node;
171 }
172
173 void ImageTextureNode::compile(SVMCompiler& compiler)
174 {
175         ShaderInput *vector_in = input("Vector");
176         ShaderOutput *color_out = output("Color");
177         ShaderOutput *alpha_out = output("Alpha");
178
179         image_manager = compiler.image_manager;
180         if(is_float == -1) {
181                 bool is_float_bool;
182                 slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear);
183                 is_float = (int)is_float_bool;
184         }
185
186         if(!color_out->links.empty())
187                 compiler.stack_assign(color_out);
188         if(!alpha_out->links.empty())
189                 compiler.stack_assign(alpha_out);
190
191         if(slot != -1) {
192                 compiler.stack_assign(vector_in);
193
194                 int srgb = (is_linear || color_space != "Color")? 0: 1;
195                 int vector_offset = vector_in->stack_offset;
196
197                 if(!tex_mapping.skip()) {
198                         vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
199                         tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
200                 }
201
202                 if(projection == "Flat") {
203                         compiler.add_node(NODE_TEX_IMAGE,
204                                 slot,
205                                 compiler.encode_uchar4(
206                                         vector_offset,
207                                         color_out->stack_offset,
208                                         alpha_out->stack_offset,
209                                         srgb));
210                 }
211                 else {
212                         compiler.add_node(NODE_TEX_IMAGE_BOX,
213                                 slot,
214                                 compiler.encode_uchar4(
215                                         vector_offset,
216                                         color_out->stack_offset,
217                                         alpha_out->stack_offset,
218                                         srgb),
219                                 __float_as_int(projection_blend));
220                 }
221         
222                 if(vector_offset != vector_in->stack_offset)
223                         compiler.stack_clear_offset(vector_in->type, vector_offset);
224         }
225         else {
226                 /* image not found */
227                 if(!color_out->links.empty()) {
228                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
229                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
230                                                                     TEX_IMAGE_MISSING_G,
231                                                                     TEX_IMAGE_MISSING_B));
232                 }
233                 if(!alpha_out->links.empty())
234                         compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
235         }
236 }
237
238 void ImageTextureNode::compile(OSLCompiler& compiler)
239 {
240         ShaderOutput *alpha_out = output("Alpha");
241
242         tex_mapping.compile(compiler);
243
244         if(is_float == -1)
245                 is_float = (int)image_manager->is_float_image(filename, NULL, is_linear);
246
247         compiler.parameter("filename", filename.c_str());
248         if(is_linear || color_space != "Color")
249                 compiler.parameter("color_space", "Linear");
250         else
251                 compiler.parameter("color_space", "sRGB");
252         compiler.parameter("projection", projection);
253         compiler.parameter("projection_blend", projection_blend);
254         compiler.parameter("is_float", is_float);
255         compiler.parameter("use_alpha", !alpha_out->links.empty());
256         compiler.add(this, "node_image_texture");
257 }
258
259 /* Environment Texture */
260
261 static ShaderEnum env_projection_init()
262 {
263         ShaderEnum enm;
264
265         enm.insert("Equirectangular", 0);
266         enm.insert("Mirror Ball", 1);
267
268         return enm;
269 }
270
271 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
272 ShaderEnum EnvironmentTextureNode::projection_enum = env_projection_init();
273
274 EnvironmentTextureNode::EnvironmentTextureNode()
275 : TextureNode("environment_texture")
276 {
277         image_manager = NULL;
278         slot = -1;
279         is_float = -1;
280         is_linear = false;
281         filename = "";
282         builtin_data = NULL;
283         color_space = ustring("Color");
284         projection = ustring("Equirectangular");
285         animated = false;
286
287         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
288         add_output("Color", SHADER_SOCKET_COLOR);
289         add_output("Alpha", SHADER_SOCKET_FLOAT);
290 }
291
292 EnvironmentTextureNode::~EnvironmentTextureNode()
293 {
294         if(image_manager)
295                 image_manager->remove_image(filename, builtin_data);
296 }
297
298 ShaderNode *EnvironmentTextureNode::clone() const
299 {
300         EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
301         node->image_manager = NULL;
302         node->slot = -1;
303         node->is_float = -1;
304         node->is_linear = false;
305         return node;
306 }
307
308 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
309 {
310         ShaderInput *vector_in = input("Vector");
311         ShaderOutput *color_out = output("Color");
312         ShaderOutput *alpha_out = output("Alpha");
313
314         image_manager = compiler.image_manager;
315         if(slot == -1) {
316                 bool is_float_bool;
317                 slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear);
318                 is_float = (int)is_float_bool;
319         }
320
321         if(!color_out->links.empty())
322                 compiler.stack_assign(color_out);
323         if(!alpha_out->links.empty())
324                 compiler.stack_assign(alpha_out);
325         
326         if(slot != -1) {
327                 compiler.stack_assign(vector_in);
328
329                 int srgb = (is_linear || color_space != "Color")? 0: 1;
330                 int vector_offset = vector_in->stack_offset;
331
332                 if(!tex_mapping.skip()) {
333                         vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
334                         tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
335                 }
336
337                 compiler.add_node(NODE_TEX_ENVIRONMENT,
338                         slot,
339                         compiler.encode_uchar4(
340                                 vector_offset,
341                                 color_out->stack_offset,
342                                 alpha_out->stack_offset,
343                                 srgb),
344                         projection_enum[projection]);
345         
346                 if(vector_offset != vector_in->stack_offset)
347                         compiler.stack_clear_offset(vector_in->type, vector_offset);
348         }
349         else {
350                 /* image not found */
351                 if(!color_out->links.empty()) {
352                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
353                         compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
354                                                                     TEX_IMAGE_MISSING_G,
355                                                                     TEX_IMAGE_MISSING_B));
356                 }
357                 if(!alpha_out->links.empty())
358                         compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
359         }
360 }
361
362 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
363 {
364         ShaderOutput *alpha_out = output("Alpha");
365
366         tex_mapping.compile(compiler);
367
368         if(is_float == -1)
369                 is_float = (int)image_manager->is_float_image(filename, NULL, is_linear);
370
371         compiler.parameter("filename", filename.c_str());
372         compiler.parameter("projection", projection);
373         if(is_linear || color_space != "Color")
374                 compiler.parameter("color_space", "Linear");
375         else
376                 compiler.parameter("color_space", "sRGB");
377         compiler.parameter("is_float", is_float);
378         compiler.parameter("use_alpha", !alpha_out->links.empty());
379         compiler.add(this, "node_environment_texture");
380 }
381
382 /* Sky Texture */
383
384 static float2 sky_spherical_coordinates(float3 dir)
385 {
386         return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
387 }
388
389 static float sky_perez_function(float lam[6], float theta, float gamma)
390 {
391         return (1.0f + lam[0]*expf(lam[1]/cosf(theta))) * (1.0f + lam[2]*expf(lam[3]*gamma)  + lam[4]*cosf(gamma)*cosf(gamma));
392 }
393
394 static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity)
395 {
396         float2 spherical = sky_spherical_coordinates(dir);
397         float theta = spherical.x;
398         float phi = spherical.y;
399
400         ksunsky->theta = theta;
401         ksunsky->phi = phi;
402
403         float theta2 = theta*theta;
404         float theta3 = theta2*theta;
405         float T = turbidity;
406         float T2 = T * T;
407
408         float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
409         ksunsky->zenith_Y = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
410         ksunsky->zenith_Y *= 0.06f;
411
412         ksunsky->zenith_x =
413         (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
414         (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
415         (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
416
417         ksunsky->zenith_y =
418         (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
419         (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta  + 0.00516f) * T +
420         (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta  + 0.26688f);
421
422         ksunsky->perez_Y[0] = (0.1787f * T  - 1.4630f);
423         ksunsky->perez_Y[1] = (-0.3554f * T  + 0.4275f);
424         ksunsky->perez_Y[2] = (-0.0227f * T  + 5.3251f);
425         ksunsky->perez_Y[3] = (0.1206f * T  - 2.5771f);
426         ksunsky->perez_Y[4] = (-0.0670f * T  + 0.3703f);
427
428         ksunsky->perez_x[0] = (-0.0193f * T  - 0.2592f);
429         ksunsky->perez_x[1] = (-0.0665f * T  + 0.0008f);
430         ksunsky->perez_x[2] = (-0.0004f * T  + 0.2125f);
431         ksunsky->perez_x[3] = (-0.0641f * T  - 0.8989f);
432         ksunsky->perez_x[4] = (-0.0033f * T  + 0.0452f);
433
434         ksunsky->perez_y[0] = (-0.0167f * T  - 0.2608f);
435         ksunsky->perez_y[1] = (-0.0950f * T  + 0.0092f);
436         ksunsky->perez_y[2] = (-0.0079f * T  + 0.2102f);
437         ksunsky->perez_y[3] = (-0.0441f * T  - 1.6537f);
438         ksunsky->perez_y[4] = (-0.0109f * T  + 0.0529f);
439
440         ksunsky->zenith_Y /= sky_perez_function(ksunsky->perez_Y, 0, theta);
441         ksunsky->zenith_x /= sky_perez_function(ksunsky->perez_x, 0, theta);
442         ksunsky->zenith_y /= sky_perez_function(ksunsky->perez_y, 0, theta);
443 }
444
445 SkyTextureNode::SkyTextureNode()
446 : TextureNode("sky_texture")
447 {
448         sun_direction = make_float3(0.0f, 0.0f, 1.0f);
449         turbidity = 2.2f;
450
451         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
452         add_output("Color", SHADER_SOCKET_COLOR);
453 }
454
455 void SkyTextureNode::compile(SVMCompiler& compiler)
456 {
457         ShaderInput *vector_in = input("Vector");
458         ShaderOutput *color_out = output("Color");
459
460         if(compiler.sunsky) {
461                 sky_texture_precompute(compiler.sunsky, sun_direction, turbidity);
462                 compiler.sunsky = NULL;
463         }
464
465         if(vector_in->link)
466                 compiler.stack_assign(vector_in);
467
468         int vector_offset = vector_in->stack_offset;
469
470         if(!tex_mapping.skip()) {
471                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
472                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
473         }
474
475         compiler.stack_assign(color_out);
476         compiler.add_node(NODE_TEX_SKY, vector_offset, color_out->stack_offset);
477
478         if(vector_offset != vector_in->stack_offset)
479                 compiler.stack_clear_offset(vector_in->type, vector_offset);
480 }
481
482 void SkyTextureNode::compile(OSLCompiler& compiler)
483 {
484         tex_mapping.compile(compiler);
485
486         compiler.parameter_vector("sun_direction", sun_direction);
487         compiler.parameter("turbidity", turbidity);
488         compiler.add(this, "node_sky_texture");
489 }
490
491 /* Gradient Texture */
492
493 static ShaderEnum gradient_type_init()
494 {
495         ShaderEnum enm;
496
497         enm.insert("Linear", NODE_BLEND_LINEAR);
498         enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
499         enm.insert("Easing", NODE_BLEND_EASING);
500         enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
501         enm.insert("Radial", NODE_BLEND_RADIAL);
502         enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
503         enm.insert("Spherical", NODE_BLEND_SPHERICAL);
504
505         return enm;
506 }
507
508 ShaderEnum GradientTextureNode::type_enum = gradient_type_init();
509
510 GradientTextureNode::GradientTextureNode()
511 : TextureNode("gradient_texture")
512 {
513         type = ustring("Linear");
514
515         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
516         add_output("Color", SHADER_SOCKET_COLOR);
517         add_output("Fac", SHADER_SOCKET_FLOAT);
518 }
519
520 void GradientTextureNode::compile(SVMCompiler& compiler)
521 {
522         ShaderInput *vector_in = input("Vector");
523         ShaderOutput *color_out = output("Color");
524         ShaderOutput *fac_out = output("Fac");
525
526         if(vector_in->link) compiler.stack_assign(vector_in);
527
528         int vector_offset = vector_in->stack_offset;
529
530         if(!tex_mapping.skip()) {
531                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
532                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
533         }
534
535         if(!fac_out->links.empty())
536                 compiler.stack_assign(fac_out);
537         if(!color_out->links.empty())
538                 compiler.stack_assign(color_out);
539
540         compiler.add_node(NODE_TEX_GRADIENT,
541                 compiler.encode_uchar4(type_enum[type], vector_offset, fac_out->stack_offset, color_out->stack_offset));
542
543         if(vector_offset != vector_in->stack_offset)
544                 compiler.stack_clear_offset(vector_in->type, vector_offset);
545 }
546
547 void GradientTextureNode::compile(OSLCompiler& compiler)
548 {
549         tex_mapping.compile(compiler);
550
551         compiler.parameter("Type", type);
552         compiler.add(this, "node_gradient_texture");
553 }
554
555 /* Noise Texture */
556
557 NoiseTextureNode::NoiseTextureNode()
558 : TextureNode("noise_texture")
559 {
560         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
561         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
562         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
563         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
564
565         add_output("Color", SHADER_SOCKET_COLOR);
566         add_output("Fac", SHADER_SOCKET_FLOAT);
567 }
568
569 void NoiseTextureNode::compile(SVMCompiler& compiler)
570 {
571         ShaderInput *distortion_in = input("Distortion");
572         ShaderInput *detail_in = input("Detail");
573         ShaderInput *scale_in = input("Scale");
574         ShaderInput *vector_in = input("Vector");
575         ShaderOutput *color_out = output("Color");
576         ShaderOutput *fac_out = output("Fac");
577
578         if(vector_in->link) compiler.stack_assign(vector_in);
579         if(scale_in->link) compiler.stack_assign(scale_in);
580         if(detail_in->link) compiler.stack_assign(detail_in);
581         if(distortion_in->link) compiler.stack_assign(distortion_in);
582
583         int vector_offset = vector_in->stack_offset;
584
585         if(!tex_mapping.skip()) {
586                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
587                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
588         }
589
590         if(!fac_out->links.empty())
591                 compiler.stack_assign(fac_out);
592         if(!color_out->links.empty())
593                 compiler.stack_assign(color_out);
594
595         compiler.add_node(NODE_TEX_NOISE,
596                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
597                 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset));
598         compiler.add_node(
599                 __float_as_int(scale_in->value.x),
600                 __float_as_int(detail_in->value.x),
601                 __float_as_int(distortion_in->value.x));
602
603         if(vector_offset != vector_in->stack_offset)
604                 compiler.stack_clear_offset(vector_in->type, vector_offset);
605 }
606
607 void NoiseTextureNode::compile(OSLCompiler& compiler)
608 {
609         tex_mapping.compile(compiler);
610
611         compiler.add(this, "node_noise_texture");
612 }
613
614 /* Voronoi Texture */
615
616 static ShaderEnum voronoi_coloring_init()
617 {
618         ShaderEnum enm;
619
620         enm.insert("Intensity", NODE_VORONOI_INTENSITY);
621         enm.insert("Cells", NODE_VORONOI_CELLS);
622
623         return enm;
624 }
625
626 ShaderEnum VoronoiTextureNode::coloring_enum  = voronoi_coloring_init();
627
628 VoronoiTextureNode::VoronoiTextureNode()
629 : TextureNode("voronoi_texture")
630 {
631         coloring = ustring("Intensity");
632
633         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
634         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
635
636         add_output("Color", SHADER_SOCKET_COLOR);
637         add_output("Fac", SHADER_SOCKET_FLOAT);
638 }
639
640 void VoronoiTextureNode::compile(SVMCompiler& compiler)
641 {
642         ShaderInput *scale_in = input("Scale");
643         ShaderInput *vector_in = input("Vector");
644         ShaderOutput *color_out = output("Color");
645         ShaderOutput *fac_out = output("Fac");
646
647         if(vector_in->link) compiler.stack_assign(vector_in);
648         if(scale_in->link) compiler.stack_assign(scale_in);
649
650         int vector_offset = vector_in->stack_offset;
651
652         if(!tex_mapping.skip()) {
653                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
654                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
655         }
656
657         compiler.stack_assign(color_out);
658         compiler.stack_assign(fac_out);
659
660         compiler.add_node(NODE_TEX_VORONOI,
661                 coloring_enum[coloring],
662                 compiler.encode_uchar4(scale_in->stack_offset, vector_offset, fac_out->stack_offset, color_out->stack_offset),
663                 __float_as_int(scale_in->value.x));
664
665         if(vector_offset != vector_in->stack_offset)
666                 compiler.stack_clear_offset(vector_in->type, vector_offset);
667 }
668
669 void VoronoiTextureNode::compile(OSLCompiler& compiler)
670 {
671         tex_mapping.compile(compiler);
672
673         compiler.parameter("Coloring", coloring);
674         compiler.add(this, "node_voronoi_texture");
675 }
676
677 /* Musgrave Texture */
678
679 static ShaderEnum musgrave_type_init()
680 {
681         ShaderEnum enm;
682
683         enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
684         enm.insert("fBM", NODE_MUSGRAVE_FBM);
685         enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
686         enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
687         enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
688
689         return enm;
690 }
691
692 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
693
694 MusgraveTextureNode::MusgraveTextureNode()
695 : TextureNode("musgrave_texture")
696 {
697         type = ustring("fBM");
698
699         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
700         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
701         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
702         add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
703         add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
704         add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
705         add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
706
707         add_output("Fac", SHADER_SOCKET_FLOAT);
708         add_output("Color", SHADER_SOCKET_COLOR);
709 }
710
711 void MusgraveTextureNode::compile(SVMCompiler& compiler)
712 {
713         ShaderInput *vector_in = input("Vector");
714         ShaderInput *scale_in = input("Scale");
715         ShaderInput *dimension_in = input("Dimension");
716         ShaderInput *lacunarity_in = input("Lacunarity");
717         ShaderInput *detail_in = input("Detail");
718         ShaderInput *offset_in = input("Offset");
719         ShaderInput *gain_in = input("Gain");
720         ShaderOutput *fac_out = output("Fac");
721         ShaderOutput *color_out = output("Color");
722
723         if(vector_in->link) compiler.stack_assign(vector_in);
724         if(dimension_in->link) compiler.stack_assign(dimension_in);
725         if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
726         if(detail_in->link) compiler.stack_assign(detail_in);
727         if(offset_in->link) compiler.stack_assign(offset_in);
728         if(gain_in->link) compiler.stack_assign(gain_in);
729         if(scale_in->link) compiler.stack_assign(scale_in);
730
731         int vector_offset = vector_in->stack_offset;
732
733         if(!tex_mapping.skip()) {
734                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
735                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
736         }
737
738         if(!fac_out->links.empty())
739                 compiler.stack_assign(fac_out);
740         if(!color_out->links.empty())
741                 compiler.stack_assign(color_out);
742
743         compiler.add_node(NODE_TEX_MUSGRAVE,
744                 compiler.encode_uchar4(type_enum[type], vector_offset, color_out->stack_offset, fac_out->stack_offset),
745                 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, detail_in->stack_offset, offset_in->stack_offset),
746                 compiler.encode_uchar4(gain_in->stack_offset, scale_in->stack_offset));
747         compiler.add_node(__float_as_int(dimension_in->value.x),
748                 __float_as_int(lacunarity_in->value.x),
749                 __float_as_int(detail_in->value.x),
750                 __float_as_int(offset_in->value.x));
751         compiler.add_node(__float_as_int(gain_in->value.x),
752                 __float_as_int(scale_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 MusgraveTextureNode::compile(OSLCompiler& compiler)
759 {
760         tex_mapping.compile(compiler);
761
762         compiler.parameter("Type", type);
763
764         compiler.add(this, "node_musgrave_texture");
765 }
766
767 /* Wave Texture */
768
769 static ShaderEnum wave_type_init()
770 {
771         ShaderEnum enm;
772
773         enm.insert("Bands", NODE_WAVE_BANDS);
774         enm.insert("Rings", NODE_WAVE_RINGS);
775
776         return enm;
777 }
778
779 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
780
781 WaveTextureNode::WaveTextureNode()
782 : TextureNode("wave_texture")
783 {
784         type = ustring("Bands");
785
786         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
787         add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
788         add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
789         add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
790         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
791
792         add_output("Color", SHADER_SOCKET_COLOR);
793         add_output("Fac", SHADER_SOCKET_FLOAT);
794 }
795
796 void WaveTextureNode::compile(SVMCompiler& compiler)
797 {
798         ShaderInput *scale_in = input("Scale");
799         ShaderInput *distortion_in = input("Distortion");
800         ShaderInput *dscale_in = input("Detail Scale");
801         ShaderInput *detail_in = input("Detail");
802         ShaderInput *vector_in = input("Vector");
803         ShaderOutput *fac_out = output("Fac");
804         ShaderOutput *color_out = output("Color");
805
806         if(scale_in->link) compiler.stack_assign(scale_in);
807         if(detail_in->link) compiler.stack_assign(detail_in);
808         if(distortion_in->link) compiler.stack_assign(distortion_in);
809         if(dscale_in->link) compiler.stack_assign(dscale_in);
810         if(vector_in->link) compiler.stack_assign(vector_in);
811
812         int vector_offset = vector_in->stack_offset;
813
814         if(!tex_mapping.skip()) {
815                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
816                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
817         }
818
819         if(!fac_out->links.empty())
820                 compiler.stack_assign(fac_out);
821         if(!color_out->links.empty())
822                 compiler.stack_assign(color_out);
823
824         compiler.add_node(NODE_TEX_WAVE,
825                 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
826                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset));
827
828         compiler.add_node(
829                 __float_as_int(scale_in->value.x),
830                 __float_as_int(detail_in->value.x),
831                 __float_as_int(distortion_in->value.x),
832                 __float_as_int(dscale_in->value.x));
833
834         if(vector_offset != vector_in->stack_offset)
835                 compiler.stack_clear_offset(vector_in->type, vector_offset);
836 }
837
838 void WaveTextureNode::compile(OSLCompiler& compiler)
839 {
840         tex_mapping.compile(compiler);
841
842         compiler.parameter("Type", type);
843
844         compiler.add(this, "node_wave_texture");
845 }
846
847 /* Magic Texture */
848
849 MagicTextureNode::MagicTextureNode()
850 : TextureNode("magic_texture")
851 {
852         depth = 2;
853
854         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
855         add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
856         add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
857
858         add_output("Color", SHADER_SOCKET_COLOR);
859         add_output("Fac", SHADER_SOCKET_FLOAT);
860 }
861
862 void MagicTextureNode::compile(SVMCompiler& compiler)
863 {
864         ShaderInput *vector_in = input("Vector");
865         ShaderInput *scale_in = input("Scale");
866         ShaderInput *distortion_in = input("Distortion");
867         ShaderOutput *color_out = output("Color");
868         ShaderOutput *fac_out = output("Fac");
869
870         if(vector_in->link) compiler.stack_assign(vector_in);
871         if(distortion_in->link) compiler.stack_assign(distortion_in);
872         if(scale_in->link) compiler.stack_assign(scale_in);
873
874         int vector_offset = vector_in->stack_offset;
875
876         if(!tex_mapping.skip()) {
877                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
878                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
879         }
880
881         if(!fac_out->links.empty())
882                 compiler.stack_assign(fac_out);
883         if(!color_out->links.empty())
884                 compiler.stack_assign(color_out);
885
886         compiler.add_node(NODE_TEX_MAGIC,
887                 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
888                 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, distortion_in->stack_offset));
889         compiler.add_node(
890                 __float_as_int(scale_in->value.x),
891                 __float_as_int(distortion_in->value.x));
892
893         if(vector_offset != vector_in->stack_offset)
894                 compiler.stack_clear_offset(vector_in->type, vector_offset);
895 }
896
897 void MagicTextureNode::compile(OSLCompiler& compiler)
898 {
899         tex_mapping.compile(compiler);
900
901         compiler.parameter("Depth", depth);
902         compiler.add(this, "node_magic_texture");
903 }
904
905 /* Checker Texture */
906
907 CheckerTextureNode::CheckerTextureNode()
908 : TextureNode("checker_texture")
909 {
910         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
911         add_input("Color1", SHADER_SOCKET_COLOR);
912         add_input("Color2", SHADER_SOCKET_COLOR);
913         add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
914
915         add_output("Color", SHADER_SOCKET_COLOR);
916         add_output("Fac", SHADER_SOCKET_FLOAT);
917 }
918
919 void CheckerTextureNode::compile(SVMCompiler& compiler)
920 {
921         ShaderInput *vector_in = input("Vector");
922         ShaderInput *color1_in = input("Color1");
923         ShaderInput *color2_in = input("Color2");
924         ShaderInput *scale_in = input("Scale");
925         
926         ShaderOutput *color_out = output("Color");
927         ShaderOutput *fac_out = output("Fac");
928
929         compiler.stack_assign(vector_in);
930         compiler.stack_assign(color1_in);
931         compiler.stack_assign(color2_in);
932         if(scale_in->link) compiler.stack_assign(scale_in);
933
934         int vector_offset = vector_in->stack_offset;
935
936         if(!tex_mapping.skip()) {
937                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
938                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
939         }
940
941         if(!color_out->links.empty())
942                 compiler.stack_assign(color_out);
943         if(!fac_out->links.empty())
944                 compiler.stack_assign(fac_out);
945
946         compiler.add_node(NODE_TEX_CHECKER,
947                 compiler.encode_uchar4(vector_offset, color1_in->stack_offset, color2_in->stack_offset, scale_in->stack_offset),
948                 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset),
949                 __float_as_int(scale_in->value.x));
950
951         if(vector_offset != vector_in->stack_offset)
952                 compiler.stack_clear_offset(vector_in->type, vector_offset);
953 }
954
955 void CheckerTextureNode::compile(OSLCompiler& compiler)
956 {
957         tex_mapping.compile(compiler);
958
959         compiler.add(this, "node_checker_texture");
960 }
961
962 /* Brick Texture */
963
964 BrickTextureNode::BrickTextureNode()
965 : TextureNode("brick_texture")
966 {
967         offset = 0.5f;
968         offset_frequency = 2;
969         squash = 1.0f;
970         squash_frequency = 2;
971         
972         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
973         add_input("Color1", SHADER_SOCKET_COLOR);
974         add_input("Color2", SHADER_SOCKET_COLOR);
975         add_input("Mortar", SHADER_SOCKET_COLOR);
976         add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
977         add_input("Mortar Size", SHADER_SOCKET_FLOAT, 0.02f);
978         add_input("Bias", SHADER_SOCKET_FLOAT, 0.0f);
979         add_input("Brick Width", SHADER_SOCKET_FLOAT, 0.5f);
980         add_input("Row Height", SHADER_SOCKET_FLOAT, 0.25f);
981
982         add_output("Color", SHADER_SOCKET_COLOR);
983         add_output("Fac", SHADER_SOCKET_FLOAT);
984 }
985
986 void BrickTextureNode::compile(SVMCompiler& compiler)
987 {
988         ShaderInput *vector_in = input("Vector");
989         ShaderInput *color1_in = input("Color1");
990         ShaderInput *color2_in = input("Color2");
991         ShaderInput *mortar_in = input("Mortar");
992         ShaderInput *scale_in = input("Scale");
993         ShaderInput *mortar_size_in = input("Mortar Size");
994         ShaderInput *bias_in = input("Bias");
995         ShaderInput *brick_width_in = input("Brick Width");
996         ShaderInput *row_height_in = input("Row Height");
997         
998         ShaderOutput *color_out = output("Color");
999         ShaderOutput *fac_out = output("Fac");
1000
1001         compiler.stack_assign(vector_in);
1002         compiler.stack_assign(color1_in);
1003         compiler.stack_assign(color2_in);
1004         compiler.stack_assign(mortar_in);
1005         if(scale_in->link) compiler.stack_assign(scale_in);
1006         if(mortar_size_in->link) compiler.stack_assign(mortar_size_in);
1007         if(bias_in->link) compiler.stack_assign(bias_in);
1008         if(brick_width_in->link) compiler.stack_assign(brick_width_in);
1009         if(row_height_in->link) compiler.stack_assign(row_height_in);
1010
1011         int vector_offset = vector_in->stack_offset;
1012
1013         if(!tex_mapping.skip()) {
1014                 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1015                 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1016         }
1017
1018         if(!color_out->links.empty())
1019                 compiler.stack_assign(color_out);
1020         if(!fac_out->links.empty())
1021                 compiler.stack_assign(fac_out);
1022
1023         compiler.add_node(NODE_TEX_BRICK,
1024                 compiler.encode_uchar4(vector_offset,
1025                         color1_in->stack_offset, color2_in->stack_offset, mortar_in->stack_offset),
1026                 compiler.encode_uchar4(scale_in->stack_offset,
1027                         mortar_size_in->stack_offset, bias_in->stack_offset, brick_width_in->stack_offset),
1028                 compiler.encode_uchar4(row_height_in->stack_offset,
1029                         color_out->stack_offset, fac_out->stack_offset));
1030                         
1031         compiler.add_node(compiler.encode_uchar4(offset_frequency, squash_frequency),
1032                 __float_as_int(scale_in->value.x),
1033                 __float_as_int(mortar_size_in->value.x),
1034                 __float_as_int(bias_in->value.x));
1035
1036         compiler.add_node(__float_as_int(brick_width_in->value.x),
1037                 __float_as_int(row_height_in->value.x),
1038                 __float_as_int(offset),
1039                 __float_as_int(squash));
1040
1041         if(vector_offset != vector_in->stack_offset)
1042                 compiler.stack_clear_offset(vector_in->type, vector_offset);
1043 }
1044
1045 void BrickTextureNode::compile(OSLCompiler& compiler)
1046 {
1047         tex_mapping.compile(compiler);
1048
1049         compiler.parameter("Offset", offset);
1050         compiler.parameter("OffsetFrequency", offset_frequency);
1051         compiler.parameter("Squash", squash);
1052         compiler.parameter("SquashFrequency", squash_frequency);
1053         compiler.add(this, "node_brick_texture");
1054 }
1055
1056 /* Normal */
1057
1058 NormalNode::NormalNode()
1059 : ShaderNode("normal")
1060 {
1061         direction = make_float3(0.0f, 0.0f, 1.0f);
1062
1063         add_input("Normal", SHADER_SOCKET_NORMAL);
1064         add_output("Normal", SHADER_SOCKET_NORMAL);
1065         add_output("Dot",  SHADER_SOCKET_FLOAT);
1066 }
1067
1068 void NormalNode::compile(SVMCompiler& compiler)
1069 {
1070         ShaderInput *normal_in = input("Normal");
1071         ShaderOutput *normal_out = output("Normal");
1072         ShaderOutput *dot_out = output("Dot");
1073
1074         compiler.stack_assign(normal_in);
1075         compiler.stack_assign(normal_out);
1076         compiler.stack_assign(dot_out);
1077
1078         compiler.add_node(NODE_NORMAL, normal_in->stack_offset, normal_out->stack_offset, dot_out->stack_offset);
1079         compiler.add_node(
1080                 __float_as_int(direction.x),
1081                 __float_as_int(direction.y),
1082                 __float_as_int(direction.z));
1083 }
1084
1085 void NormalNode::compile(OSLCompiler& compiler)
1086 {
1087         compiler.parameter_normal("Direction", direction);
1088         compiler.add(this, "node_normal");
1089 }
1090
1091 /* Mapping */
1092
1093 MappingNode::MappingNode()
1094 : ShaderNode("mapping")
1095 {
1096         add_input("Vector", SHADER_SOCKET_POINT);
1097         add_output("Vector", SHADER_SOCKET_POINT);
1098 }
1099
1100 void MappingNode::compile(SVMCompiler& compiler)
1101 {
1102         ShaderInput *vector_in = input("Vector");
1103         ShaderOutput *vector_out = output("Vector");
1104
1105         compiler.stack_assign(vector_in);
1106         compiler.stack_assign(vector_out);
1107
1108         tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
1109 }
1110
1111 void MappingNode::compile(OSLCompiler& compiler)
1112 {
1113         Transform tfm = transform_transpose(tex_mapping.compute_transform());
1114         compiler.parameter("Matrix", tfm);
1115         compiler.parameter_point("mapping_min", tex_mapping.min);
1116         compiler.parameter_point("mapping_max", tex_mapping.max);
1117         compiler.parameter("use_minmax", tex_mapping.use_minmax);
1118
1119         compiler.add(this, "node_mapping");
1120 }
1121
1122 /* Convert */
1123
1124 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_, bool autoconvert)
1125 : ShaderNode("convert")
1126 {
1127         from = from_;
1128         to = to_;
1129
1130         if(autoconvert)
1131                 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1132
1133         assert(from != to);
1134
1135         if(from == SHADER_SOCKET_FLOAT)
1136                 add_input("Val", SHADER_SOCKET_FLOAT);
1137         else if(from == SHADER_SOCKET_INT)
1138                 add_input("ValInt", SHADER_SOCKET_INT);
1139         else if(from == SHADER_SOCKET_COLOR)
1140                 add_input("Color", SHADER_SOCKET_COLOR);
1141         else if(from == SHADER_SOCKET_VECTOR)
1142                 add_input("Vector", SHADER_SOCKET_VECTOR);
1143         else if(from == SHADER_SOCKET_POINT)
1144                 add_input("Point", SHADER_SOCKET_POINT);
1145         else if(from == SHADER_SOCKET_NORMAL)
1146                 add_input("Normal", SHADER_SOCKET_NORMAL);
1147         else if(from == SHADER_SOCKET_STRING)
1148                 add_input("String", SHADER_SOCKET_STRING);
1149         else
1150                 assert(0);
1151
1152         if(to == SHADER_SOCKET_FLOAT)
1153                 add_output("Val", SHADER_SOCKET_FLOAT);
1154         else if(to == SHADER_SOCKET_INT)
1155                 add_output("ValInt", SHADER_SOCKET_INT);
1156         else if(to == SHADER_SOCKET_COLOR)
1157                 add_output("Color", SHADER_SOCKET_COLOR);
1158         else if(to == SHADER_SOCKET_VECTOR)
1159                 add_output("Vector", SHADER_SOCKET_VECTOR);
1160         else if(to == SHADER_SOCKET_POINT)
1161                 add_output("Point", SHADER_SOCKET_POINT);
1162         else if(to == SHADER_SOCKET_NORMAL)
1163                 add_output("Normal", SHADER_SOCKET_NORMAL);
1164         else if(to == SHADER_SOCKET_STRING)
1165                 add_output("String", SHADER_SOCKET_STRING);
1166         else
1167                 assert(0);
1168 }
1169
1170 void ConvertNode::compile(SVMCompiler& compiler)
1171 {
1172         ShaderInput *in = inputs[0];
1173         ShaderOutput *out = outputs[0];
1174
1175         if(from == SHADER_SOCKET_FLOAT) {
1176                 compiler.stack_assign(in);
1177                 compiler.stack_assign(out);
1178
1179                 if(to == SHADER_SOCKET_INT)
1180                         /* float to int */
1181                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, in->stack_offset, out->stack_offset);
1182                 else
1183                         /* float to float3 */
1184                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
1185         }
1186         else if(from == SHADER_SOCKET_INT) {
1187                 compiler.stack_assign(in);
1188                 compiler.stack_assign(out);
1189
1190                 if(to == SHADER_SOCKET_FLOAT)
1191                         /* int to float */
1192                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, in->stack_offset, out->stack_offset);
1193                 else
1194                         /* int to vector/point/normal */
1195                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, in->stack_offset, out->stack_offset);
1196         }
1197         else if(to == SHADER_SOCKET_FLOAT) {
1198                 compiler.stack_assign(in);
1199                 compiler.stack_assign(out);
1200
1201                 if(from == SHADER_SOCKET_COLOR)
1202                         /* color to float */
1203                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
1204                 else
1205                         /* vector/point/normal to float */
1206                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
1207         }
1208         else if(to == SHADER_SOCKET_INT) {
1209                 compiler.stack_assign(in);
1210                 compiler.stack_assign(out);
1211
1212                 if(from == SHADER_SOCKET_COLOR)
1213                         /* color to int */
1214                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, in->stack_offset, out->stack_offset);
1215                 else
1216                         /* vector/point/normal to int */
1217                         compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, in->stack_offset, out->stack_offset);
1218         }
1219         else {
1220                 /* float3 to float3 */
1221                 if(in->link) {
1222                         /* no op in SVM */
1223                         compiler.stack_link(in, out);
1224                 }
1225                 else {
1226                         /* set 0,0,0 value */
1227                         compiler.stack_assign(in);
1228                         compiler.stack_assign(out);
1229
1230                         compiler.add_node(NODE_VALUE_V, in->stack_offset);
1231                         compiler.add_node(NODE_VALUE_V, in->value);
1232                 }
1233         }
1234 }
1235
1236 void ConvertNode::compile(OSLCompiler& compiler)
1237 {
1238         if(from == SHADER_SOCKET_FLOAT)
1239                 compiler.add(this, "node_convert_from_float");
1240         else if(from == SHADER_SOCKET_INT)
1241                 compiler.add(this, "node_convert_from_int");
1242         else if(from == SHADER_SOCKET_COLOR)
1243                 compiler.add(this, "node_convert_from_color");
1244         else if(from == SHADER_SOCKET_VECTOR)
1245                 compiler.add(this, "node_convert_from_vector");
1246         else if(from == SHADER_SOCKET_POINT)
1247                 compiler.add(this, "node_convert_from_point");
1248         else if(from == SHADER_SOCKET_NORMAL)
1249                 compiler.add(this, "node_convert_from_normal");
1250         else
1251                 assert(0);
1252 }
1253
1254 /* Proxy */
1255
1256 ProxyNode::ProxyNode(ShaderSocketType type_)
1257 : ShaderNode("proxy")
1258 {
1259         type = type_;
1260         special_type = SHADER_SPECIAL_TYPE_PROXY;
1261
1262         add_input("Input", type);
1263         add_output("Output", type);
1264 }
1265
1266 void ProxyNode::compile(SVMCompiler& compiler)
1267 {
1268 }
1269
1270 void ProxyNode::compile(OSLCompiler& compiler)
1271 {
1272 }
1273
1274 /* BSDF Closure */
1275
1276 BsdfNode::BsdfNode(bool scattering_)
1277 : ShaderNode("bsdf"), scattering(scattering_)
1278 {
1279         closure = ccl::CLOSURE_BSSRDF_ID;
1280
1281         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1282         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
1283         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1284
1285         if(scattering)
1286                 add_output("BSSRDF", SHADER_SOCKET_CLOSURE);
1287         else
1288                 add_output("BSDF", SHADER_SOCKET_CLOSURE);
1289 }
1290
1291 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3)
1292 {
1293         ShaderInput *color_in = input("Color");
1294         ShaderInput *normal_in = input("Normal");
1295         ShaderInput *tangent_in = input("Tangent");
1296
1297         if(color_in->link) {
1298                 compiler.stack_assign(color_in);
1299                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1300         }
1301         else
1302                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1303         
1304         if(param1)
1305                 compiler.stack_assign(param1);
1306         if(param2)
1307                 compiler.stack_assign(param2);
1308         if(param3)
1309                 compiler.stack_assign(param3);
1310
1311         if(normal_in->link)
1312                 compiler.stack_assign(normal_in);
1313
1314         if(tangent_in && tangent_in->link)
1315                 compiler.stack_assign(tangent_in);
1316
1317         compiler.add_node(NODE_CLOSURE_BSDF,
1318                 compiler.encode_uchar4(closure,
1319                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1320                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1321                         compiler.closure_mix_weight_offset()),
1322                 __float_as_int((param1)? param1->value.x: 0.0f),
1323                 __float_as_int((param2)? param2->value.x: 0.0f));
1324
1325         if(tangent_in) {
1326                 compiler.add_node(NODE_CLOSURE_BSDF, normal_in->stack_offset, tangent_in->stack_offset,
1327                         (param3)? param3->stack_offset: SVM_STACK_INVALID);
1328         }
1329         else {
1330                 compiler.add_node(NODE_CLOSURE_BSDF, normal_in->stack_offset, SVM_STACK_INVALID,
1331                         (param3)? param3->stack_offset: SVM_STACK_INVALID);
1332         }
1333 }
1334
1335 void BsdfNode::compile(SVMCompiler& compiler)
1336 {
1337         compile(compiler, NULL, NULL);
1338 }
1339
1340 void BsdfNode::compile(OSLCompiler& compiler)
1341 {
1342         assert(0);
1343 }
1344
1345 /* Ward BSDF Closure */
1346
1347 WardBsdfNode::WardBsdfNode()
1348 {
1349         closure = CLOSURE_BSDF_WARD_ID;
1350
1351         add_input("Tangent", SHADER_SOCKET_VECTOR, ShaderInput::TANGENT);
1352
1353         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1354         add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.5f);
1355         add_input("Rotation", SHADER_SOCKET_FLOAT, 0.0f);
1356 }
1357
1358 void WardBsdfNode::attributes(AttributeRequestSet *attributes)
1359 {
1360         ShaderInput *tangent_in = input("Tangent");
1361
1362         if(!tangent_in->link)
1363                 attributes->add(ATTR_STD_GENERATED);
1364
1365         ShaderNode::attributes(attributes);
1366 }
1367
1368 void WardBsdfNode::compile(SVMCompiler& compiler)
1369 {
1370         BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1371 }
1372
1373 void WardBsdfNode::compile(OSLCompiler& compiler)
1374 {
1375         compiler.add(this, "node_ward_bsdf");
1376 }
1377
1378 /* Glossy BSDF Closure */
1379
1380 static ShaderEnum glossy_distribution_init()
1381 {
1382         ShaderEnum enm;
1383
1384         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1385         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1386         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1387
1388         return enm;
1389 }
1390
1391 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1392
1393 GlossyBsdfNode::GlossyBsdfNode()
1394 {
1395         distribution = ustring("Beckmann");
1396
1397         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1398 }
1399
1400 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1401 {
1402         closure = (ClosureType)distribution_enum[distribution];
1403
1404         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1405                 BsdfNode::compile(compiler, NULL, NULL);
1406         else
1407                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1408 }
1409
1410 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1411 {
1412         compiler.parameter("distribution", distribution);
1413         compiler.add(this, "node_glossy_bsdf");
1414 }
1415
1416 /* Glass BSDF Closure */
1417
1418 static ShaderEnum glass_distribution_init()
1419 {
1420         ShaderEnum enm;
1421
1422         enm.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1423         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1424         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1425
1426         return enm;
1427 }
1428
1429 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1430
1431 GlassBsdfNode::GlassBsdfNode()
1432 {
1433         distribution = ustring("Sharp");
1434
1435         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1436         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1437 }
1438
1439 void GlassBsdfNode::compile(SVMCompiler& compiler)
1440 {
1441         closure = (ClosureType)distribution_enum[distribution];
1442
1443         if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
1444                 BsdfNode::compile(compiler, NULL, input("IOR"));
1445         else
1446                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1447 }
1448
1449 void GlassBsdfNode::compile(OSLCompiler& compiler)
1450 {
1451         compiler.parameter("distribution", distribution);
1452         compiler.add(this, "node_glass_bsdf");
1453 }
1454
1455 /* Refraction BSDF Closure */
1456
1457 static ShaderEnum refraction_distribution_init()
1458 {
1459         ShaderEnum enm;
1460
1461         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1462         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1463         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1464
1465         return enm;
1466 }
1467
1468 ShaderEnum RefractionBsdfNode::distribution_enum = refraction_distribution_init();
1469
1470 RefractionBsdfNode::RefractionBsdfNode()
1471 {
1472         distribution = ustring("Sharp");
1473
1474         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1475         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1476 }
1477
1478 void RefractionBsdfNode::compile(SVMCompiler& compiler)
1479 {
1480         closure = (ClosureType)distribution_enum[distribution];
1481
1482         if(closure == CLOSURE_BSDF_REFRACTION_ID)
1483                 BsdfNode::compile(compiler, NULL, input("IOR"));
1484         else
1485                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1486 }
1487
1488 void RefractionBsdfNode::compile(OSLCompiler& compiler)
1489 {
1490         compiler.parameter("distribution", distribution);
1491         compiler.add(this, "node_refraction_bsdf");
1492 }
1493
1494 /* Toon BSDF Closure */
1495
1496 static ShaderEnum toon_component_init()
1497 {
1498         ShaderEnum enm;
1499
1500         enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
1501         enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
1502
1503         return enm;
1504 }
1505
1506 ShaderEnum ToonBsdfNode::component_enum = toon_component_init();
1507
1508 ToonBsdfNode::ToonBsdfNode()
1509 {
1510         component = ustring("Diffuse");
1511
1512         add_input("Size", SHADER_SOCKET_FLOAT, 0.5f);
1513         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1514 }
1515
1516 void ToonBsdfNode::compile(SVMCompiler& compiler)
1517 {
1518         closure = (ClosureType)component_enum[component];
1519         
1520         BsdfNode::compile(compiler, input("Size"), input("Smooth"));
1521 }
1522
1523 void ToonBsdfNode::compile(OSLCompiler& compiler)
1524 {
1525         compiler.parameter("component", component);
1526         compiler.add(this, "node_toon_bsdf");
1527 }
1528
1529 /* Velvet BSDF Closure */
1530
1531 VelvetBsdfNode::VelvetBsdfNode()
1532 {
1533         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1534
1535         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1536 }
1537
1538 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1539 {
1540         BsdfNode::compile(compiler, input("Sigma"), NULL);
1541 }
1542
1543 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1544 {
1545         compiler.add(this, "node_velvet_bsdf");
1546 }
1547
1548 /* Diffuse BSDF Closure */
1549
1550 DiffuseBsdfNode::DiffuseBsdfNode()
1551 {
1552         closure = CLOSURE_BSDF_DIFFUSE_ID;
1553         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1554 }
1555
1556 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1557 {
1558         BsdfNode::compile(compiler, input("Roughness"), NULL);
1559 }
1560
1561 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1562 {
1563         compiler.add(this, "node_diffuse_bsdf");
1564 }
1565
1566 /* Translucent BSDF Closure */
1567
1568 TranslucentBsdfNode::TranslucentBsdfNode()
1569 {
1570         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1571 }
1572
1573 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1574 {
1575         BsdfNode::compile(compiler, NULL, NULL);
1576 }
1577
1578 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1579 {
1580         compiler.add(this, "node_translucent_bsdf");
1581 }
1582
1583 /* Transparent BSDF Closure */
1584
1585 TransparentBsdfNode::TransparentBsdfNode()
1586 {
1587         name = "transparent";
1588         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1589 }
1590
1591 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1592 {
1593         BsdfNode::compile(compiler, NULL, NULL);
1594 }
1595
1596 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1597 {
1598         compiler.add(this, "node_transparent_bsdf");
1599 }
1600
1601 /* Subsurface Scattering Closure */
1602
1603 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
1604 : BsdfNode(true)
1605 {
1606         name = "subsurface_scattering";
1607         closure = CLOSURE_BSSRDF_ID;
1608
1609         add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
1610         add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
1611         add_input("IOR", SHADER_SOCKET_FLOAT, 1.3f);
1612 }
1613
1614 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
1615 {
1616         BsdfNode::compile(compiler, input("Scale"), input("IOR"), input("Radius"));
1617 }
1618
1619 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
1620 {
1621         compiler.add(this, "node_subsurface_scattering");
1622 }
1623
1624 /* Emissive Closure */
1625
1626 EmissionNode::EmissionNode()
1627 : ShaderNode("emission")
1628 {
1629         total_power = false;
1630
1631         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1632         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1633         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1634
1635         add_output("Emission", SHADER_SOCKET_CLOSURE);
1636 }
1637
1638 void EmissionNode::compile(SVMCompiler& compiler)
1639 {
1640         ShaderInput *color_in = input("Color");
1641         ShaderInput *strength_in = input("Strength");
1642
1643         if(color_in->link || strength_in->link) {
1644                 compiler.stack_assign(color_in);
1645                 compiler.stack_assign(strength_in);
1646                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1647         }
1648         else if(total_power)
1649                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1650         else
1651                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1652
1653         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1654 }
1655
1656 void EmissionNode::compile(OSLCompiler& compiler)
1657 {
1658         compiler.parameter("TotalPower", (total_power)? 1: 0);
1659         compiler.add(this, "node_emission");
1660 }
1661
1662 /* Background Closure */
1663
1664 BackgroundNode::BackgroundNode()
1665 : ShaderNode("background")
1666 {
1667         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1668         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1669         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1670
1671         add_output("Background", SHADER_SOCKET_CLOSURE);
1672 }
1673
1674 void BackgroundNode::compile(SVMCompiler& compiler)
1675 {
1676         ShaderInput *color_in = input("Color");
1677         ShaderInput *strength_in = input("Strength");
1678
1679         if(color_in->link || strength_in->link) {
1680                 compiler.stack_assign(color_in);
1681                 compiler.stack_assign(strength_in);
1682                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1683         }
1684         else
1685                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1686
1687         compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1688 }
1689
1690 void BackgroundNode::compile(OSLCompiler& compiler)
1691 {
1692         compiler.add(this, "node_background");
1693 }
1694
1695 /* Holdout Closure */
1696
1697 HoldoutNode::HoldoutNode()
1698 : ShaderNode("holdout")
1699 {
1700         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1701         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1702
1703         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1704 }
1705
1706 void HoldoutNode::compile(SVMCompiler& compiler)
1707 {
1708         float3 value = make_float3(1.0f, 1.0f, 1.0f);
1709
1710         compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
1711         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1712 }
1713
1714 void HoldoutNode::compile(OSLCompiler& compiler)
1715 {
1716         compiler.add(this, "node_holdout");
1717 }
1718
1719 /* Ambient Occlusion */
1720
1721 AmbientOcclusionNode::AmbientOcclusionNode()
1722 : ShaderNode("ambient_occlusion")
1723 {
1724         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1725         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1726         add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1727
1728         add_output("AO", SHADER_SOCKET_CLOSURE);
1729 }
1730
1731 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
1732 {
1733         ShaderInput *color_in = input("Color");
1734
1735         if(color_in->link) {
1736                 compiler.stack_assign(color_in);
1737                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1738         }
1739         else
1740                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1741
1742         compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
1743 }
1744
1745 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
1746 {
1747         compiler.add(this, "node_ambient_occlusion");
1748 }
1749
1750 /* Volume Closure */
1751
1752 VolumeNode::VolumeNode()
1753 : ShaderNode("volume")
1754 {
1755         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1756
1757         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1758         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1759         add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1760
1761         add_output("Volume", SHADER_SOCKET_CLOSURE);
1762 }
1763
1764 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1765 {
1766         ShaderInput *color_in = input("Color");
1767
1768         if(color_in->link) {
1769                 compiler.stack_assign(color_in);
1770                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1771         }
1772         else
1773                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1774         
1775         if(param1)
1776                 compiler.stack_assign(param1);
1777         if(param2)
1778                 compiler.stack_assign(param2);
1779
1780         compiler.add_node(NODE_CLOSURE_VOLUME,
1781                 compiler.encode_uchar4(closure,
1782                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1783                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1784                         compiler.closure_mix_weight_offset()),
1785                 __float_as_int((param1)? param1->value.x: 0.0f),
1786                 __float_as_int((param2)? param2->value.x: 0.0f));
1787 }
1788
1789 void VolumeNode::compile(SVMCompiler& compiler)
1790 {
1791         compile(compiler, NULL, NULL);
1792 }
1793
1794 void VolumeNode::compile(OSLCompiler& compiler)
1795 {
1796         assert(0);
1797 }
1798
1799 /* Transparent Volume Closure */
1800
1801 TransparentVolumeNode::TransparentVolumeNode()
1802 {
1803         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1804 }
1805
1806 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1807 {
1808         VolumeNode::compile(compiler, input("Density"), NULL);
1809 }
1810
1811 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1812 {
1813         compiler.add(this, "node_isotropic_volume");
1814 }
1815
1816 /* Isotropic Volume Closure */
1817
1818 IsotropicVolumeNode::IsotropicVolumeNode()
1819 {
1820         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1821 }
1822
1823 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1824 {
1825         VolumeNode::compile(compiler, input("Density"), NULL);
1826 }
1827
1828 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1829 {
1830         compiler.add(this, "node_isotropic_volume");
1831 }
1832
1833 /* Geometry */
1834
1835 GeometryNode::GeometryNode()
1836 : ShaderNode("geometry")
1837 {
1838         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1839         add_output("Position", SHADER_SOCKET_POINT);
1840         add_output("Normal", SHADER_SOCKET_NORMAL);
1841         add_output("Tangent", SHADER_SOCKET_NORMAL);
1842         add_output("True Normal", SHADER_SOCKET_NORMAL);
1843         add_output("Incoming", SHADER_SOCKET_VECTOR);
1844         add_output("Parametric", SHADER_SOCKET_POINT);
1845         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1846 }
1847
1848 void GeometryNode::attributes(AttributeRequestSet *attributes)
1849 {
1850         if(!output("Tangent")->links.empty())
1851                 attributes->add(ATTR_STD_GENERATED);
1852
1853         ShaderNode::attributes(attributes);
1854 }
1855
1856 void GeometryNode::compile(SVMCompiler& compiler)
1857 {
1858         ShaderOutput *out;
1859         NodeType geom_node = NODE_GEOMETRY;
1860
1861         if(bump == SHADER_BUMP_DX)
1862                 geom_node = NODE_GEOMETRY_BUMP_DX;
1863         else if(bump == SHADER_BUMP_DY)
1864                 geom_node = NODE_GEOMETRY_BUMP_DY;
1865         
1866         out = output("Position");
1867         if(!out->links.empty()) {
1868                 compiler.stack_assign(out);
1869                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1870         }
1871
1872         out = output("Normal");
1873         if(!out->links.empty()) {
1874                 compiler.stack_assign(out);
1875                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1876         }
1877
1878         out = output("Tangent");
1879         if(!out->links.empty()) {
1880                 compiler.stack_assign(out);
1881                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1882         }
1883
1884         out = output("True Normal");
1885         if(!out->links.empty()) {
1886                 compiler.stack_assign(out);
1887                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1888         }
1889
1890         out = output("Incoming");
1891         if(!out->links.empty()) {
1892                 compiler.stack_assign(out);
1893                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1894         }
1895
1896         out = output("Parametric");
1897         if(!out->links.empty()) {
1898                 compiler.stack_assign(out);
1899                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1900         }
1901
1902         out = output("Backfacing");
1903         if(!out->links.empty()) {
1904                 compiler.stack_assign(out);
1905                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1906         }
1907 }
1908
1909 void GeometryNode::compile(OSLCompiler& compiler)
1910 {
1911         if(bump == SHADER_BUMP_DX)
1912                 compiler.parameter("bump_offset", "dx");
1913         else if(bump == SHADER_BUMP_DY)
1914                 compiler.parameter("bump_offset", "dy");
1915         else
1916                 compiler.parameter("bump_offset", "center");
1917
1918         compiler.add(this, "node_geometry");
1919 }
1920
1921 /* TextureCoordinate */
1922
1923 TextureCoordinateNode::TextureCoordinateNode()
1924 : ShaderNode("texture_coordinate")
1925 {
1926         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1927         add_output("Generated", SHADER_SOCKET_POINT);
1928         add_output("Normal", SHADER_SOCKET_NORMAL);
1929         add_output("UV", SHADER_SOCKET_POINT);
1930         add_output("Object", SHADER_SOCKET_POINT);
1931         add_output("Camera", SHADER_SOCKET_POINT);
1932         add_output("Window", SHADER_SOCKET_POINT);
1933         add_output("Reflection", SHADER_SOCKET_NORMAL);
1934
1935         from_dupli = false;
1936 }
1937
1938 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1939 {
1940         if(!from_dupli) {
1941                 if(!output("Generated")->links.empty())
1942                         attributes->add(ATTR_STD_GENERATED);
1943                 if(!output("UV")->links.empty())
1944                         attributes->add(ATTR_STD_UV);
1945         }
1946
1947         ShaderNode::attributes(attributes);
1948 }
1949
1950 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1951 {
1952         ShaderOutput *out;
1953         NodeType texco_node = NODE_TEX_COORD;
1954         NodeType attr_node = NODE_ATTR;
1955         NodeType geom_node = NODE_GEOMETRY;
1956
1957         if(bump == SHADER_BUMP_DX) {
1958                 texco_node = NODE_TEX_COORD_BUMP_DX;
1959                 attr_node = NODE_ATTR_BUMP_DX;
1960                 geom_node = NODE_GEOMETRY_BUMP_DX;
1961         }
1962         else if(bump == SHADER_BUMP_DY) {
1963                 texco_node = NODE_TEX_COORD_BUMP_DY;
1964                 attr_node = NODE_ATTR_BUMP_DY;
1965                 geom_node = NODE_GEOMETRY_BUMP_DY;
1966         }
1967         
1968         out = output("Generated");
1969         if(!out->links.empty()) {
1970                 if(compiler.background) {
1971                         compiler.stack_assign(out);
1972                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1973                 }
1974                 else {
1975                         if(from_dupli) {
1976                                 compiler.stack_assign(out);
1977                                 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
1978                         }
1979                         else {
1980                                 int attr = compiler.attribute(ATTR_STD_GENERATED);
1981                                 compiler.stack_assign(out);
1982                                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1983                         }
1984                 }
1985         }
1986
1987         out = output("Normal");
1988         if(!out->links.empty()) {
1989                 compiler.stack_assign(out);
1990                 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
1991         }
1992
1993         out = output("UV");
1994         if(!out->links.empty()) {
1995                 if(from_dupli) {
1996                         compiler.stack_assign(out);
1997                         compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
1998                 }
1999                 else {
2000                         int attr = compiler.attribute(ATTR_STD_UV);
2001                         compiler.stack_assign(out);
2002                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2003                 }
2004         }
2005
2006         out = output("Object");
2007         if(!out->links.empty()) {
2008                 compiler.stack_assign(out);
2009                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
2010         }
2011
2012         out = output("Camera");
2013         if(!out->links.empty()) {
2014                 compiler.stack_assign(out);
2015                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
2016         }
2017
2018         out = output("Window");
2019         if(!out->links.empty()) {
2020                 compiler.stack_assign(out);
2021                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
2022         }
2023
2024         out = output("Reflection");
2025         if(!out->links.empty()) {
2026                 if(compiler.background) {
2027                         compiler.stack_assign(out);
2028                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2029                 }
2030                 else {
2031                         compiler.stack_assign(out);
2032                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
2033                 }
2034         }
2035 }
2036
2037 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2038 {
2039         if(bump == SHADER_BUMP_DX)
2040                 compiler.parameter("bump_offset", "dx");
2041         else if(bump == SHADER_BUMP_DY)
2042                 compiler.parameter("bump_offset", "dy");
2043         else
2044                 compiler.parameter("bump_offset", "center");
2045         
2046         if(compiler.background)
2047                 compiler.parameter("is_background", true);
2048         
2049         compiler.parameter("from_dupli", from_dupli);
2050
2051         compiler.add(this, "node_texture_coordinate");
2052 }
2053
2054 /* Light Path */
2055
2056 LightPathNode::LightPathNode()
2057 : ShaderNode("light_path")
2058 {
2059         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
2060         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
2061         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
2062         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
2063         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
2064         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
2065         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
2066         add_output("Ray Length", SHADER_SOCKET_FLOAT);
2067 }
2068
2069 void LightPathNode::compile(SVMCompiler& compiler)
2070 {
2071         ShaderOutput *out;
2072
2073         out = output("Is Camera Ray");
2074         if(!out->links.empty()) {
2075                 compiler.stack_assign(out);
2076                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2077         }
2078
2079         out = output("Is Shadow Ray");
2080         if(!out->links.empty()) {
2081                 compiler.stack_assign(out);
2082                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2083         }
2084
2085         out = output("Is Diffuse Ray");
2086         if(!out->links.empty()) {
2087                 compiler.stack_assign(out);
2088                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2089         }
2090
2091         out = output("Is Glossy Ray");
2092         if(!out->links.empty()) {
2093                 compiler.stack_assign(out);
2094                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2095         }
2096
2097         out = output("Is Singular Ray");
2098         if(!out->links.empty()) {
2099                 compiler.stack_assign(out);
2100                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2101         }
2102
2103         out = output("Is Reflection Ray");
2104         if(!out->links.empty()) {
2105                 compiler.stack_assign(out);
2106                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2107         }
2108
2109
2110         out = output("Is Transmission Ray");
2111         if(!out->links.empty()) {
2112                 compiler.stack_assign(out);
2113                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2114         }
2115         
2116         out = output("Ray Length");
2117         if(!out->links.empty()) {
2118                 compiler.stack_assign(out);
2119                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2120         }
2121
2122 }
2123
2124 void LightPathNode::compile(OSLCompiler& compiler)
2125 {
2126         compiler.add(this, "node_light_path");
2127 }
2128
2129 /* Light Falloff */
2130
2131 LightFalloffNode::LightFalloffNode()
2132 : ShaderNode("light_path")
2133 {
2134         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2135         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2136         add_output("Quadratic", SHADER_SOCKET_FLOAT);
2137         add_output("Linear", SHADER_SOCKET_FLOAT);
2138         add_output("Constant", SHADER_SOCKET_FLOAT);
2139 }
2140
2141 void LightFalloffNode::compile(SVMCompiler& compiler)
2142 {
2143         ShaderInput *strength_in = input("Strength");
2144         ShaderInput *smooth_in = input("Smooth");
2145
2146         compiler.stack_assign(strength_in);
2147         compiler.stack_assign(smooth_in);
2148
2149         ShaderOutput *out = output("Quadratic");
2150         if(!out->links.empty()) {
2151                 compiler.stack_assign(out);
2152                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2153                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2154         }
2155
2156         out = output("Linear");
2157         if(!out->links.empty()) {
2158                 compiler.stack_assign(out);
2159                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2160                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2161         }
2162
2163         out = output("Constant");
2164         if(!out->links.empty()) {
2165                 compiler.stack_assign(out);
2166                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2167                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2168         }
2169 }
2170
2171 void LightFalloffNode::compile(OSLCompiler& compiler)
2172 {
2173         compiler.add(this, "node_light_falloff");
2174 }
2175
2176 /* Object Info */
2177
2178 ObjectInfoNode::ObjectInfoNode()
2179 : ShaderNode("object_info")
2180 {
2181         add_output("Location", SHADER_SOCKET_VECTOR);
2182         add_output("Object Index", SHADER_SOCKET_FLOAT);
2183         add_output("Material Index", SHADER_SOCKET_FLOAT);
2184         add_output("Random", SHADER_SOCKET_FLOAT);
2185 }
2186
2187 void ObjectInfoNode::compile(SVMCompiler& compiler)
2188 {
2189         ShaderOutput *out = output("Location");
2190         if(!out->links.empty()) {
2191                 compiler.stack_assign(out);
2192                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2193         }
2194
2195         out = output("Object Index");
2196         if(!out->links.empty()) {
2197                 compiler.stack_assign(out);
2198                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2199         }
2200
2201         out = output("Material Index");
2202         if(!out->links.empty()) {
2203                 compiler.stack_assign(out);
2204                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2205         }
2206
2207         out = output("Random");
2208         if(!out->links.empty()) {
2209                 compiler.stack_assign(out);
2210                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2211         }
2212 }
2213
2214 void ObjectInfoNode::compile(OSLCompiler& compiler)
2215 {
2216         compiler.add(this, "node_object_info");
2217 }
2218
2219 /* Particle Info */
2220
2221 ParticleInfoNode::ParticleInfoNode()
2222 : ShaderNode("particle_info")
2223 {
2224         add_output("Index", SHADER_SOCKET_FLOAT);
2225         add_output("Age", SHADER_SOCKET_FLOAT);
2226         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2227         add_output("Location", SHADER_SOCKET_POINT);
2228         #if 0   /* not yet supported */
2229         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2230         #endif
2231         add_output("Size", SHADER_SOCKET_FLOAT);
2232         add_output("Velocity", SHADER_SOCKET_VECTOR);
2233         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2234 }
2235
2236 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
2237 {
2238         if(!output("Index")->links.empty())
2239                 attributes->add(ATTR_STD_PARTICLE);
2240         if(!output("Age")->links.empty())
2241                 attributes->add(ATTR_STD_PARTICLE);
2242         if(!output("Lifetime")->links.empty())
2243                 attributes->add(ATTR_STD_PARTICLE);
2244         if(!output("Location")->links.empty())
2245                 attributes->add(ATTR_STD_PARTICLE);
2246         #if 0   /* not yet supported */
2247         if(!output("Rotation")->links.empty())
2248                 attributes->add(ATTR_STD_PARTICLE);
2249         #endif
2250         if(!output("Size")->links.empty())
2251                 attributes->add(ATTR_STD_PARTICLE);
2252         if(!output("Velocity")->links.empty())
2253                 attributes->add(ATTR_STD_PARTICLE);
2254         if(!output("Angular Velocity")->links.empty())
2255                 attributes->add(ATTR_STD_PARTICLE);
2256
2257         ShaderNode::attributes(attributes);
2258 }
2259
2260 void ParticleInfoNode::compile(SVMCompiler& compiler)
2261 {
2262         ShaderOutput *out;
2263         
2264         out = output("Index");
2265         if(!out->links.empty()) {
2266                 compiler.stack_assign(out);
2267                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2268         }
2269         
2270         out = output("Age");
2271         if(!out->links.empty()) {
2272                 compiler.stack_assign(out);
2273                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2274         }
2275         
2276         out = output("Lifetime");
2277         if(!out->links.empty()) {
2278                 compiler.stack_assign(out);
2279                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2280         }
2281         
2282         out = output("Location");
2283         if(!out->links.empty()) {
2284                 compiler.stack_assign(out);
2285                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2286         }
2287         
2288         /* quaternion data is not yet supported by Cycles */
2289 #if 0
2290         out = output("Rotation");
2291         if(!out->links.empty()) {
2292                 compiler.stack_assign(out);
2293                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2294         }
2295 #endif
2296         
2297         out = output("Size");
2298         if(!out->links.empty()) {
2299                 compiler.stack_assign(out);
2300                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2301         }
2302         
2303         out = output("Velocity");
2304         if(!out->links.empty()) {
2305                 compiler.stack_assign(out);
2306                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2307         }
2308         
2309         out = output("Angular Velocity");
2310         if(!out->links.empty()) {
2311                 compiler.stack_assign(out);
2312                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2313         }
2314 }
2315
2316 void ParticleInfoNode::compile(OSLCompiler& compiler)
2317 {
2318         compiler.add(this, "node_particle_info");
2319 }
2320
2321 /* Hair Info */
2322
2323 HairInfoNode::HairInfoNode()
2324 : ShaderNode("hair_info")
2325 {
2326         add_output("Is Strand", SHADER_SOCKET_FLOAT);
2327         add_output("Intercept", SHADER_SOCKET_FLOAT);
2328         add_output("Thickness", SHADER_SOCKET_FLOAT);
2329         add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2330         /*output for minimum hair width transparency - deactivated*/
2331         /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2332 }
2333
2334 void HairInfoNode::attributes(AttributeRequestSet *attributes)
2335 {
2336         ShaderOutput *intercept_out = output("Intercept");
2337
2338         if(!intercept_out->links.empty())
2339                 attributes->add(ATTR_STD_CURVE_INTERCEPT);
2340         
2341         ShaderNode::attributes(attributes);
2342 }
2343
2344 void HairInfoNode::compile(SVMCompiler& compiler)
2345 {
2346         ShaderOutput *out;
2347         
2348         out = output("Is Strand");
2349         if(!out->links.empty()) {
2350                 compiler.stack_assign(out);
2351                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2352         }
2353
2354         out = output("Intercept");
2355         if(!out->links.empty()) {
2356                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2357                 compiler.stack_assign(out);
2358                 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2359         }
2360
2361         out = output("Thickness");
2362         if(!out->links.empty()) {
2363                 compiler.stack_assign(out);
2364                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2365         }
2366
2367         out = output("Tangent Normal");
2368         if(!out->links.empty()) {
2369                 compiler.stack_assign(out);
2370                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2371         }
2372
2373         /*out = output("Fade");
2374         if(!out->links.empty()) {
2375                 compiler.stack_assign(out);
2376                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2377         }*/
2378
2379 }
2380
2381 void HairInfoNode::compile(OSLCompiler& compiler)
2382 {
2383         compiler.add(this, "node_hair_info");
2384 }
2385
2386 /* Value */
2387
2388 ValueNode::ValueNode()
2389 : ShaderNode("value")
2390 {
2391         value = 0.0f;
2392
2393         add_output("Value", SHADER_SOCKET_FLOAT);
2394 }
2395
2396 void ValueNode::compile(SVMCompiler& compiler)
2397 {
2398         ShaderOutput *val_out = output("Value");
2399
2400         compiler.stack_assign(val_out);
2401         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2402 }
2403
2404 void ValueNode::compile(OSLCompiler& compiler)
2405 {
2406         compiler.parameter("value_value", value);
2407         compiler.add(this, "node_value");
2408 }
2409
2410 /* Color */
2411
2412 ColorNode::ColorNode()
2413 : ShaderNode("color")
2414 {
2415         value = make_float3(0.0f, 0.0f, 0.0f);
2416
2417         add_output("Color", SHADER_SOCKET_COLOR);
2418 }
2419
2420 void ColorNode::compile(SVMCompiler& compiler)
2421 {
2422         ShaderOutput *color_out = output("Color");
2423
2424         if(color_out && !color_out->links.empty()) {
2425                 compiler.stack_assign(color_out);
2426                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2427                 compiler.add_node(NODE_VALUE_V, value);
2428         }
2429 }
2430
2431 void ColorNode::compile(OSLCompiler& compiler)
2432 {
2433         compiler.parameter_color("color_value", value);
2434
2435         compiler.add(this, "node_value");
2436 }
2437
2438 /* Add Closure */
2439
2440 AddClosureNode::AddClosureNode()
2441 : ShaderNode("add_closure")
2442 {
2443         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2444         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2445         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2446 }
2447
2448 void AddClosureNode::compile(SVMCompiler& compiler)
2449 {
2450         /* handled in the SVM compiler */
2451 }
2452
2453 void AddClosureNode::compile(OSLCompiler& compiler)
2454 {
2455         compiler.add(this, "node_add_closure");
2456 }
2457
2458 /* Mix Closure */
2459
2460 MixClosureNode::MixClosureNode()
2461 : ShaderNode("mix_closure")
2462 {
2463         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2464         
2465         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2466         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2467         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2468         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2469 }
2470
2471 void MixClosureNode::compile(SVMCompiler& compiler)
2472 {
2473         /* handled in the SVM compiler */
2474 }
2475
2476 void MixClosureNode::compile(OSLCompiler& compiler)
2477 {
2478         compiler.add(this, "node_mix_closure");
2479 }
2480
2481 /* Mix Closure */
2482
2483 MixClosureWeightNode::MixClosureWeightNode()
2484 : ShaderNode("mix_closure_weight")
2485 {
2486         add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2487         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2488         add_output("Weight1", SHADER_SOCKET_FLOAT);
2489         add_output("Weight2", SHADER_SOCKET_FLOAT);
2490 }
2491
2492 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2493 {
2494         ShaderInput *weight_in = input("Weight");
2495         ShaderInput *fac_in = input("Fac");
2496         ShaderOutput *weight1_out = output("Weight1");
2497         ShaderOutput *weight2_out = output("Weight2");
2498
2499         compiler.stack_assign(weight_in);
2500         compiler.stack_assign(fac_in);
2501         compiler.stack_assign(weight1_out);
2502         compiler.stack_assign(weight2_out);
2503
2504         compiler.add_node(NODE_MIX_CLOSURE,
2505                 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2506                         weight1_out->stack_offset, weight2_out->stack_offset));
2507 }
2508
2509 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2510 {
2511         assert(0);
2512 }
2513
2514 /* Invert */
2515
2516 InvertNode::InvertNode()
2517 : ShaderNode("invert")
2518 {
2519         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2520         add_input("Color", SHADER_SOCKET_COLOR);
2521         add_output("Color",  SHADER_SOCKET_COLOR);
2522 }
2523
2524 void InvertNode::compile(SVMCompiler& compiler)
2525 {
2526         ShaderInput *fac_in = input("Fac");
2527         ShaderInput *color_in = input("Color");
2528         ShaderOutput *color_out = output("Color");
2529
2530         compiler.stack_assign(fac_in);
2531         compiler.stack_assign(color_in);
2532         compiler.stack_assign(color_out);
2533
2534         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2535 }
2536
2537 void InvertNode::compile(OSLCompiler& compiler)
2538 {
2539         compiler.add(this, "node_invert");
2540 }
2541
2542 /* Mix */
2543
2544 MixNode::MixNode()
2545 : ShaderNode("mix")
2546 {
2547         type = ustring("Mix");
2548
2549         use_clamp = false;
2550
2551         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2552         add_input("Color1", SHADER_SOCKET_COLOR);
2553         add_input("Color2", SHADER_SOCKET_COLOR);
2554         add_output("Color",  SHADER_SOCKET_COLOR);
2555 }
2556
2557 static ShaderEnum mix_type_init()
2558 {
2559         ShaderEnum enm;
2560
2561         enm.insert("Mix", NODE_MIX_BLEND);
2562         enm.insert("Add", NODE_MIX_ADD);
2563         enm.insert("Multiply", NODE_MIX_MUL);
2564         enm.insert("Screen", NODE_MIX_SCREEN);
2565         enm.insert("Overlay", NODE_MIX_OVERLAY);
2566         enm.insert("Subtract", NODE_MIX_SUB);
2567         enm.insert("Divide", NODE_MIX_DIV);
2568         enm.insert("Difference", NODE_MIX_DIFF);
2569         enm.insert("Darken", NODE_MIX_DARK);
2570         enm.insert("Lighten", NODE_MIX_LIGHT);
2571         enm.insert("Dodge", NODE_MIX_DODGE);
2572         enm.insert("Burn", NODE_MIX_BURN);
2573         enm.insert("Hue", NODE_MIX_HUE);
2574         enm.insert("Saturation", NODE_MIX_SAT);
2575         enm.insert("Value", NODE_MIX_VAL);
2576         enm.insert("Color", NODE_MIX_COLOR);
2577         enm.insert("Soft Light", NODE_MIX_SOFT);
2578         enm.insert("Linear Light", NODE_MIX_LINEAR);
2579
2580         return enm;
2581 }
2582
2583 ShaderEnum MixNode::type_enum = mix_type_init();
2584
2585 void MixNode::compile(SVMCompiler& compiler)
2586 {
2587         ShaderInput *fac_in = input("Fac");
2588         ShaderInput *color1_in = input("Color1");
2589         ShaderInput *color2_in = input("Color2");
2590         ShaderOutput *color_out = output("Color");
2591
2592         compiler.stack_assign(fac_in);
2593         compiler.stack_assign(color1_in);
2594         compiler.stack_assign(color2_in);
2595         compiler.stack_assign(color_out);
2596
2597         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2598         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2599
2600         if(use_clamp) {
2601                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2602                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2603         }
2604 }
2605
2606 void MixNode::compile(OSLCompiler& compiler)
2607 {
2608         compiler.parameter("type", type);
2609         compiler.parameter("Clamp", use_clamp);
2610         compiler.add(this, "node_mix");
2611 }
2612
2613 /* Combine RGB */
2614 CombineRGBNode::CombineRGBNode()
2615 : ShaderNode("combine_rgb")
2616 {
2617         add_input("R", SHADER_SOCKET_FLOAT);
2618         add_input("G", SHADER_SOCKET_FLOAT);
2619         add_input("B", SHADER_SOCKET_FLOAT);
2620         add_output("Image", SHADER_SOCKET_COLOR);
2621 }
2622
2623 void CombineRGBNode::compile(SVMCompiler& compiler)
2624 {
2625         ShaderInput *red_in = input("R");
2626         ShaderInput *green_in = input("G");
2627         ShaderInput *blue_in = input("B");
2628         ShaderOutput *color_out = output("Image");
2629
2630         compiler.stack_assign(color_out);
2631
2632         compiler.stack_assign(red_in);
2633         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2634
2635         compiler.stack_assign(green_in);
2636         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2637
2638         compiler.stack_assign(blue_in);
2639         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2640 }
2641
2642 void CombineRGBNode::compile(OSLCompiler& compiler)
2643 {
2644         compiler.add(this, "node_combine_rgb");
2645 }
2646
2647 /* Gamma */
2648 GammaNode::GammaNode()
2649 : ShaderNode("gamma")
2650 {
2651         add_input("Color", SHADER_SOCKET_COLOR);
2652         add_input("Gamma", SHADER_SOCKET_FLOAT);
2653         add_output("Color", SHADER_SOCKET_COLOR);
2654 }
2655
2656 void GammaNode::compile(SVMCompiler& compiler)
2657 {
2658         ShaderInput *color_in = input("Color");
2659         ShaderInput *gamma_in = input("Gamma");
2660         ShaderOutput *color_out = output("Color");
2661
2662         compiler.stack_assign(color_in);
2663         compiler.stack_assign(gamma_in);
2664         compiler.stack_assign(color_out);
2665
2666         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2667 }
2668
2669 void GammaNode::compile(OSLCompiler& compiler)
2670 {
2671         compiler.add(this, "node_gamma");
2672 }
2673
2674 /* Bright Contrast */
2675 BrightContrastNode::BrightContrastNode()
2676 : ShaderNode("brightness")
2677 {
2678         add_input("Color", SHADER_SOCKET_COLOR);
2679         add_input("Bright", SHADER_SOCKET_FLOAT);
2680         add_input("Contrast", SHADER_SOCKET_FLOAT);
2681         add_output("Color", SHADER_SOCKET_COLOR);
2682 }
2683
2684 void BrightContrastNode::compile(SVMCompiler& compiler)
2685 {
2686         ShaderInput *color_in = input("Color");
2687         ShaderInput *bright_in = input("Bright");
2688         ShaderInput *contrast_in = input("Contrast");
2689         ShaderOutput *color_out = output("Color");
2690
2691         compiler.stack_assign(color_in);
2692         compiler.stack_assign(bright_in);
2693         compiler.stack_assign(contrast_in);
2694         compiler.stack_assign(color_out);
2695
2696         compiler.add_node(NODE_BRIGHTCONTRAST,
2697                 color_in->stack_offset, color_out->stack_offset,
2698                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2699 }
2700
2701 void BrightContrastNode::compile(OSLCompiler& compiler)
2702 {
2703         compiler.add(this, "node_brightness");
2704 }
2705
2706 /* Separate RGB */
2707 SeparateRGBNode::SeparateRGBNode()
2708 : ShaderNode("separate_rgb")
2709 {
2710         add_input("Image", SHADER_SOCKET_COLOR);
2711         add_output("R", SHADER_SOCKET_FLOAT);
2712         add_output("G", SHADER_SOCKET_FLOAT);
2713         add_output("B", SHADER_SOCKET_FLOAT);
2714 }
2715
2716 void SeparateRGBNode::compile(SVMCompiler& compiler)
2717 {
2718         ShaderInput *color_in = input("Image");
2719         ShaderOutput *red_out = output("R");
2720         ShaderOutput *green_out = output("G");
2721         ShaderOutput *blue_out = output("B");
2722
2723         compiler.stack_assign(color_in);
2724
2725         compiler.stack_assign(red_out);
2726         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2727
2728         compiler.stack_assign(green_out);
2729         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2730
2731         compiler.stack_assign(blue_out);
2732         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2733 }
2734
2735 void SeparateRGBNode::compile(OSLCompiler& compiler)
2736 {
2737         compiler.add(this, "node_separate_rgb");
2738 }
2739
2740 /* Separate RGB */
2741 HSVNode::HSVNode()
2742 : ShaderNode("hsv")
2743 {
2744         add_input("Hue", SHADER_SOCKET_FLOAT);
2745         add_input("Saturation", SHADER_SOCKET_FLOAT);
2746         add_input("Value", SHADER_SOCKET_FLOAT);
2747         add_input("Fac", SHADER_SOCKET_FLOAT);
2748         add_input("Color", SHADER_SOCKET_COLOR);
2749         add_output("Color", SHADER_SOCKET_COLOR);
2750 }
2751
2752 void HSVNode::compile(SVMCompiler& compiler)
2753 {
2754         ShaderInput *hue_in = input("Hue");
2755         ShaderInput *saturation_in = input("Saturation");
2756         ShaderInput *value_in = input("Value");
2757         ShaderInput *fac_in = input("Fac");
2758         ShaderInput *color_in = input("Color");
2759         ShaderOutput *color_out = output("Color");
2760
2761         compiler.stack_assign(hue_in);
2762         compiler.stack_assign(saturation_in);
2763         compiler.stack_assign(value_in);
2764         compiler.stack_assign(fac_in);
2765         compiler.stack_assign(color_in);
2766         compiler.stack_assign(color_out);
2767
2768         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2769         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2770 }
2771
2772 void HSVNode::compile(OSLCompiler& compiler)
2773 {
2774         compiler.add(this, "node_hsv");
2775 }
2776
2777 /* Attribute */
2778
2779 AttributeNode::AttributeNode()
2780 : ShaderNode("attribute")
2781 {
2782         attribute = "";
2783
2784         add_output("Color",  SHADER_SOCKET_COLOR);
2785         add_output("Vector",  SHADER_SOCKET_VECTOR);
2786         add_output("Fac",  SHADER_SOCKET_FLOAT);
2787 }
2788
2789 void AttributeNode::attributes(AttributeRequestSet *attributes)
2790 {
2791         ShaderOutput *color_out = output("Color");
2792         ShaderOutput *vector_out = output("Vector");
2793         ShaderOutput *fac_out = output("Fac");
2794
2795         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2796                 attributes->add(attribute);
2797         
2798         ShaderNode::attributes(attributes);
2799 }
2800
2801 void AttributeNode::compile(SVMCompiler& compiler)
2802 {
2803         ShaderOutput *color_out = output("Color");
2804         ShaderOutput *vector_out = output("Vector");
2805         ShaderOutput *fac_out = output("Fac");
2806         NodeType attr_node = NODE_ATTR;
2807
2808         if(bump == SHADER_BUMP_DX)
2809                 attr_node = NODE_ATTR_BUMP_DX;
2810         else if(bump == SHADER_BUMP_DY)
2811                 attr_node = NODE_ATTR_BUMP_DY;
2812
2813         if(!color_out->links.empty() || !vector_out->links.empty()) {
2814                 int attr = compiler.attribute(attribute);
2815
2816                 if(!color_out->links.empty()) {
2817                         compiler.stack_assign(color_out);
2818                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2819                 }
2820                 if(!vector_out->links.empty()) {
2821                         compiler.stack_assign(vector_out);
2822                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2823                 }
2824         }
2825
2826         if(!fac_out->links.empty()) {
2827                 int attr = compiler.attribute(attribute);
2828
2829                 compiler.stack_assign(fac_out);
2830                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2831         }
2832 }
2833
2834 void AttributeNode::compile(OSLCompiler& compiler)
2835 {
2836         if(bump == SHADER_BUMP_DX)
2837                 compiler.parameter("bump_offset", "dx");
2838         else if(bump == SHADER_BUMP_DY)
2839                 compiler.parameter("bump_offset", "dy");
2840         else
2841                 compiler.parameter("bump_offset", "center");
2842
2843         compiler.parameter("name", attribute.c_str());
2844         compiler.add(this, "node_attribute");
2845 }
2846
2847 /* Camera */
2848
2849 CameraNode::CameraNode()
2850 : ShaderNode("camera")
2851 {
2852         add_output("View Vector",  SHADER_SOCKET_VECTOR);
2853         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
2854         add_output("View Distance",  SHADER_SOCKET_FLOAT);
2855 }
2856
2857 void CameraNode::compile(SVMCompiler& compiler)
2858 {
2859         ShaderOutput *vector_out = output("View Vector");
2860         ShaderOutput *z_depth_out = output("View Z Depth");
2861         ShaderOutput *distance_out = output("View Distance");
2862
2863         compiler.stack_assign(vector_out);
2864         compiler.stack_assign(z_depth_out);
2865         compiler.stack_assign(distance_out);
2866         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2867 }
2868
2869 void CameraNode::compile(OSLCompiler& compiler)
2870 {
2871         compiler.add(this, "node_camera");
2872 }
2873
2874 /* Fresnel */
2875
2876 FresnelNode::FresnelNode()
2877 : ShaderNode("Fresnel")
2878 {
2879         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2880         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2881         add_output("Fac", SHADER_SOCKET_FLOAT);
2882 }
2883
2884 void FresnelNode::compile(SVMCompiler& compiler)
2885 {
2886         ShaderInput *ior_in = input("IOR");
2887         ShaderOutput *fac_out = output("Fac");
2888
2889         compiler.stack_assign(ior_in);
2890         compiler.stack_assign(fac_out);
2891         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2892 }
2893
2894 void FresnelNode::compile(OSLCompiler& compiler)
2895 {
2896         compiler.add(this, "node_fresnel");
2897 }
2898
2899 /* Blend Weight */
2900
2901 LayerWeightNode::LayerWeightNode()
2902 : ShaderNode("LayerWeight")
2903 {
2904         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2905         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2906
2907         add_output("Fresnel", SHADER_SOCKET_FLOAT);
2908         add_output("Facing", SHADER_SOCKET_FLOAT);
2909 }
2910
2911 void LayerWeightNode::compile(SVMCompiler& compiler)
2912 {
2913         ShaderInput *blend_in = input("Blend");
2914
2915         if(blend_in->link)
2916                 compiler.stack_assign(blend_in);
2917
2918         ShaderOutput *fresnel_out = output("Fresnel");
2919         if(!fresnel_out->links.empty()) {
2920                 compiler.stack_assign(fresnel_out);
2921                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2922                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
2923         }
2924
2925         ShaderOutput *facing_out = output("Facing");
2926         if(!facing_out->links.empty()) {
2927                 compiler.stack_assign(facing_out);
2928                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2929                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
2930         }
2931 }
2932
2933 void LayerWeightNode::compile(OSLCompiler& compiler)
2934 {
2935         compiler.add(this, "node_layer_weight");
2936 }
2937
2938 /* Wireframe */
2939
2940 WireframeNode::WireframeNode()
2941 : ShaderNode("Wireframe")
2942 {
2943         add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
2944         add_output("Fac", SHADER_SOCKET_FLOAT);
2945         
2946         use_pixel_size = false;
2947 }
2948
2949 void WireframeNode::compile(SVMCompiler& compiler)
2950 {
2951         ShaderInput *size_in = input("Size");
2952         ShaderOutput *fac_out = output("Fac");
2953
2954         compiler.stack_assign(size_in);
2955         compiler.stack_assign(fac_out);
2956         compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
2957 }
2958
2959 void WireframeNode::compile(OSLCompiler& compiler)
2960 {
2961         compiler.parameter("use_pixel_size", use_pixel_size);
2962         compiler.add(this, "node_wireframe");
2963 }
2964
2965 /* Wavelength */
2966
2967 WavelengthNode::WavelengthNode()
2968 : ShaderNode("Wavelength")
2969 {
2970         add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
2971         add_output("Color", SHADER_SOCKET_COLOR);
2972 }
2973
2974 void WavelengthNode::compile(SVMCompiler& compiler)
2975 {
2976         ShaderInput *wavelength_in = input("Wavelength");
2977         ShaderOutput *color_out = output("Color");
2978
2979         compiler.stack_assign(wavelength_in);
2980         compiler.stack_assign(color_out);
2981         compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset);
2982 }
2983
2984 void WavelengthNode::compile(OSLCompiler& compiler)
2985 {
2986         compiler.add(this, "node_wavelength");
2987 }
2988
2989 /* Blackbody */
2990
2991 BlackbodyNode::BlackbodyNode()
2992 : ShaderNode("Blackbody")
2993 {
2994         add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f);
2995         add_output("Color", SHADER_SOCKET_COLOR);
2996 }
2997
2998 void BlackbodyNode::compile(SVMCompiler& compiler)
2999 {
3000         ShaderInput *temperature_in = input("Temperature");
3001         ShaderOutput *color_out = output("Color");
3002
3003         compiler.stack_assign(temperature_in);
3004         compiler.stack_assign(color_out);
3005         compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset);
3006 }
3007
3008 void BlackbodyNode::compile(OSLCompiler& compiler)
3009 {
3010         compiler.add(this, "node_blackbody");
3011 }
3012
3013 /* Output */
3014
3015 OutputNode::OutputNode()
3016 : ShaderNode("output")
3017 {
3018         add_input("Surface", SHADER_SOCKET_CLOSURE);
3019         add_input("Volume", SHADER_SOCKET_CLOSURE);
3020         add_input("Displacement", SHADER_SOCKET_FLOAT);
3021         add_input("Normal", SHADER_SOCKET_NORMAL);
3022 }
3023
3024 void OutputNode::compile(SVMCompiler& compiler)
3025 {
3026         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
3027                 ShaderInput *displacement_in = input("Displacement");
3028
3029                 if(displacement_in->link) {
3030                         compiler.stack_assign(displacement_in);
3031                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
3032                 }
3033         }
3034 }
3035
3036 void OutputNode::compile(OSLCompiler& compiler)
3037 {
3038         if(compiler.output_type() == SHADER_TYPE_SURFACE)
3039                 compiler.add(this, "node_output_surface");
3040         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3041                 compiler.add(this, "node_output_volume");
3042         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3043                 compiler.add(this, "node_output_displacement");
3044 }
3045
3046 /* Math */
3047
3048 MathNode::MathNode()
3049 : ShaderNode("math")
3050 {
3051         type = ustring("Add");
3052
3053         use_clamp = false;
3054
3055         add_input("Value1", SHADER_SOCKET_FLOAT);
3056         add_input("Value2", SHADER_SOCKET_FLOAT);
3057         add_output("Value",  SHADER_SOCKET_FLOAT);
3058 }
3059
3060 static ShaderEnum math_type_init()
3061 {
3062         ShaderEnum enm;
3063
3064         enm.insert("Add", NODE_MATH_ADD);
3065         enm.insert("Subtract", NODE_MATH_SUBTRACT);
3066         enm.insert("Multiply", NODE_MATH_MULTIPLY);
3067         enm.insert("Divide", NODE_MATH_DIVIDE);
3068         enm.insert("Sine", NODE_MATH_SINE);
3069         enm.insert("Cosine", NODE_MATH_COSINE);
3070         enm.insert("Tangent", NODE_MATH_TANGENT);
3071         enm.insert("Arcsine", NODE_MATH_ARCSINE);
3072         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3073         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3074         enm.insert("Power", NODE_MATH_POWER);
3075         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3076         enm.insert("Minimum", NODE_MATH_MINIMUM);
3077         enm.insert("Maximum", NODE_MATH_MAXIMUM);
3078         enm.insert("Round", NODE_MATH_ROUND);
3079         enm.insert("Less Than", NODE_MATH_LESS_THAN);
3080         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3081         enm.insert("Modulo", NODE_MATH_MODULO);
3082
3083         return enm;
3084 }
3085
3086 ShaderEnum MathNode::type_enum = math_type_init();
3087
3088 void MathNode::compile(SVMCompiler& compiler)
3089 {
3090         ShaderInput *value1_in = input("Value1");
3091         ShaderInput *value2_in = input("Value2");
3092         ShaderOutput *value_out = output("Value");
3093
3094         compiler.stack_assign(value1_in);
3095         compiler.stack_assign(value2_in);
3096         compiler.stack_assign(value_out);
3097
3098         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3099         compiler.add_node(NODE_MATH, value_out->stack_offset);
3100
3101         if(use_clamp) {
3102                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3103                 compiler.add_node(NODE_MATH, value_out->stack_offset);
3104         }
3105 }
3106
3107 void MathNode::compile(OSLCompiler& compiler)
3108 {
3109         compiler.parameter("type", type);
3110         compiler.parameter("Clamp", use_clamp);
3111         compiler.add(this, "node_math");
3112 }
3113
3114 /* VectorMath */
3115
3116 VectorMathNode::VectorMathNode()
3117 : ShaderNode("vector_math")
3118 {
3119         type = ustring("Add");
3120
3121         add_input("Vector1", SHADER_SOCKET_VECTOR);
3122         add_input("Vector2", SHADER_SOCKET_VECTOR);
3123         add_output("Value",  SHADER_SOCKET_FLOAT);
3124         add_output("Vector",  SHADER_SOCKET_VECTOR);
3125 }
3126
3127 static ShaderEnum vector_math_type_init()
3128 {
3129         ShaderEnum enm;
3130
3131         enm.insert("Add", NODE_VECTOR_MATH_ADD);
3132         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3133         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3134         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3135         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3136         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3137
3138         return enm;
3139 }
3140
3141 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3142
3143 void VectorMathNode::compile(SVMCompiler& compiler)
3144 {
3145         ShaderInput *vector1_in = input("Vector1");
3146         ShaderInput *vector2_in = input("Vector2");
3147         ShaderOutput *value_out = output("Value");
3148         ShaderOutput *vector_out = output("Vector");
3149
3150         compiler.stack_assign(vector1_in);
3151         compiler.stack_assign(vector2_in);
3152         compiler.stack_assign(value_out);
3153         compiler.stack_assign(vector_out);
3154
3155         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
3156         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
3157 }
3158
3159 void VectorMathNode::compile(OSLCompiler& compiler)
3160 {
3161         compiler.parameter("type", type);
3162         compiler.add(this, "node_vector_math");
3163 }
3164
3165 /* VectorTransform */
3166
3167 VectorTransformNode::VectorTransformNode()
3168 : ShaderNode("vector_transform")
3169 {
3170         type = ustring("Vector");
3171         convert_from = ustring("World");
3172         convert_to = ustring("Object");
3173
3174         add_input("Vector", SHADER_SOCKET_VECTOR);
3175         add_output("Vector",  SHADER_SOCKET_VECTOR);
3176 }
3177
3178 static ShaderEnum vector_transform_type_init()
3179 {
3180         ShaderEnum enm;
3181
3182         enm.insert("Vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
3183         enm.insert("Point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
3184
3185         return enm;
3186 }
3187
3188 static ShaderEnum vector_transform_convert_from_init()
3189 {
3190         ShaderEnum enm;
3191
3192         enm.insert("World", NODE_VECTOR_TRANSFORM_CONVERT_FROM_WORLD);
3193         enm.insert("Object", NODE_VECTOR_TRANSFORM_CONVERT_FROM_OBJECT);
3194         enm.insert("Camera", NODE_VECTOR_TRANSFORM_CONVERT_FROM_CAMERA);
3195
3196         return enm;
3197 }
3198
3199 static ShaderEnum vector_transform_convert_to_init()
3200 {
3201         ShaderEnum enm;
3202
3203         enm.insert("World", NODE_VECTOR_TRANSFORM_CONVERT_TO_WORLD);
3204         enm.insert("Object", NODE_VECTOR_TRANSFORM_CONVERT_TO_OBJECT);
3205         enm.insert("Camera", NODE_VECTOR_TRANSFORM_CONVERT_TO_CAMERA);
3206
3207         return enm;
3208 }
3209
3210 ShaderEnum VectorTransformNode::type_enum = vector_transform_type_init();
3211 ShaderEnum VectorTransformNode::convert_from_enum = vector_transform_convert_from_init();
3212 ShaderEnum VectorTransformNode::convert_to_enum = vector_transform_convert_to_init();
3213
3214 void VectorTransformNode::compile(SVMCompiler& compiler)
3215 {
3216         ShaderInput *vector_in = input("Vector");
3217         ShaderOutput *vector_out = output("Vector");
3218
3219         compiler.stack_assign(vector_in);
3220         compiler.stack_assign(vector_out);
3221
3222         compiler.add_node(NODE_VECTOR_TRANSFORM,
3223                 compiler.encode_uchar4(type_enum[type], convert_from_enum[convert_from], convert_to_enum[convert_to]),
3224                 compiler.encode_uchar4(vector_in->stack_offset, vector_out->stack_offset));
3225 }
3226
3227 void VectorTransformNode::compile(OSLCompiler& compiler)
3228 {
3229         compiler.parameter("type", type);
3230         compiler.parameter("convert_from", convert_from);
3231         compiler.parameter("convert_to", convert_to);
3232         compiler.add(this, "node_vector_transform");
3233 }
3234
3235 /* BumpNode */
3236
3237 BumpNode::BumpNode()
3238 : ShaderNode("bump")
3239 {
3240         invert = false;
3241
3242         /* this input is used by the user, but after graph transform it is no longer
3243          * used and moved to sampler center/x/y instead */
3244         add_input("Height", SHADER_SOCKET_FLOAT);
3245
3246         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
3247         add_input("SampleX", SHADER_SOCKET_FLOAT);
3248         add_input("SampleY", SHADER_SOCKET_FLOAT);
3249         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
3250         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3251         add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
3252
3253         add_output("Normal", SHADER_SOCKET_NORMAL);
3254 }
3255
3256 void BumpNode::compile(SVMCompiler& compiler)
3257 {
3258         ShaderInput *center_in = input("SampleCenter");
3259         ShaderInput *dx_in = input("SampleX");
3260         ShaderInput *dy_in = input("SampleY");
3261         ShaderInput *normal_in = input("Normal");
3262         ShaderInput *strength_in = input("Strength");
3263         ShaderInput *distance_in = input("Distance");
3264         ShaderOutput *normal_out = output("Normal");
3265
3266         compiler.stack_assign(center_in);
3267         compiler.stack_assign(dx_in);
3268         compiler.stack_assign(dy_in);
3269         compiler.stack_assign(strength_in);
3270         compiler.stack_assign(distance_in);
3271         compiler.stack_assign(normal_out);
3272
3273         if(normal_in->link)
3274                 compiler.stack_assign(normal_in);
3275         
3276         /* pack all parameters in the node */
3277         compiler.add_node(NODE_SET_BUMP,
3278                 compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
3279                 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
3280                         dy_in->stack_offset, strength_in->stack_offset),
3281                 normal_out->stack_offset);
3282 }
3283
3284 void BumpNode::compile(OSLCompiler& compiler)
3285 {
3286         compiler.parameter("invert", invert);
3287         compiler.add(this, "node_bump");
3288 }
3289
3290 /* RGBCurvesNode */
3291
3292 RGBCurvesNode::RGBCurvesNode()
3293 : ShaderNode("rgb_curves")
3294 {
3295         add_input("Fac", SHADER_SOCKET_FLOAT);
3296         add_input("Color", SHADER_SOCKET_COLOR);
3297         add_output("Color", SHADER_SOCKET_COLOR);
3298 }
3299
3300 void RGBCurvesNode::compile(SVMCompiler& compiler)
3301 {
3302         ShaderInput *fac_in = input("Fac");
3303         ShaderInput *color_in = input("Color");
3304         ShaderOutput *color_out = output("Color");
3305
3306         compiler.stack_assign(fac_in);
3307         compiler.stack_assign(color_in);
3308         compiler.stack_assign(color_out);
3309
3310         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3311         compiler.add_array(curves, RAMP_TABLE_SIZE);
3312 }
3313
3314 void RGBCurvesNode::compile(OSLCompiler& compiler)
3315 {
3316         float ramp[RAMP_TABLE_SIZE][3];
3317
3318         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3319                 ramp[i][0] = curves[i].x;
3320                 ramp[i][1] = curves[i].y;
3321                 ramp[i][2] = curves[i].z;
3322         }
3323
3324         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3325         compiler.add(this, "node_rgb_curves");
3326 }
3327
3328 /* VectorCurvesNode */
3329
3330 VectorCurvesNode::VectorCurvesNode()
3331 : ShaderNode("rgb_curves")
3332 {
3333         add_input("Fac", SHADER_SOCKET_FLOAT);
3334         add_input("Vector", SHADER_SOCKET_VECTOR);
3335         add_output("Vector", SHADER_SOCKET_VECTOR);
3336 }
3337
3338 void VectorCurvesNode::compile(SVMCompiler& compiler)
3339 {
3340         ShaderInput *fac_in = input("Fac");
3341         ShaderInput *vector_in = input("Vector");
3342         ShaderOutput *vector_out = output("Vector");
3343
3344         compiler.stack_assign(fac_in);
3345         compiler.stack_assign(vector_in);
3346         compiler.stack_assign(vector_out);
3347
3348         compiler.add_node(NODE_VECTOR_CURVES, fac_in->stack_offset, vector_in->stack_offset, vector_out->stack_offset);
3349         compiler.add_array(curves, RAMP_TABLE_SIZE);
3350 }
3351
3352 void VectorCurvesNode::compile(OSLCompiler& compiler)
3353 {
3354         float ramp[RAMP_TABLE_SIZE][3];
3355
3356         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3357                 ramp[i][0] = curves[i].x;
3358                 ramp[i][1] = curves[i].y;
3359                 ramp[i][2] = curves[i].z;
3360         }
3361
3362         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3363         compiler.add(this, "node_vector_curves");
3364 }
3365
3366 /* RGBRampNode */
3367
3368 RGBRampNode::RGBRampNode()
3369 : ShaderNode("rgb_ramp")
3370 {
3371         add_input("Fac", SHADER_SOCKET_FLOAT);
3372         add_output("Color", SHADER_SOCKET_COLOR);
3373         add_output("Alpha", SHADER_SOCKET_FLOAT);
3374
3375         interpolate = true;
3376 }
3377
3378 void RGBRampNode::compile(SVMCompiler& compiler)
3379 {
3380         ShaderInput *fac_in = input("Fac");
3381         ShaderOutput *color_out = output("Color");
3382         ShaderOutput *alpha_out = output("Alpha");
3383
3384         compiler.stack_assign(fac_in);
3385         if(!color_out->links.empty())
3386                 compiler.stack_assign(color_out);
3387         if(!alpha_out->links.empty())
3388                 compiler.stack_assign(alpha_out);
3389
3390         compiler.add_node(NODE_RGB_RAMP,
3391                 compiler.encode_uchar4(
3392                         fac_in->stack_offset,
3393                         color_out->stack_offset,
3394                         alpha_out->stack_offset),
3395                 interpolate);
3396         compiler.add_array(ramp, RAMP_TABLE_SIZE);
3397 }
3398
3399 void RGBRampNode::compile(OSLCompiler& compiler)
3400 {
3401         /* OSL shader only takes separate RGB and A array, split the RGBA base array */
3402         /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
3403         float ramp_color[RAMP_TABLE_SIZE][3];
3404         float ramp_alpha[RAMP_TABLE_SIZE];
3405
3406         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3407                 ramp_color[i][0] = ramp[i].x;
3408                 ramp_color[i][1] = ramp[i].y;
3409                 ramp_color[i][2] = ramp[i].z;
3410                 ramp_alpha[i] = ramp[i].w;
3411         }
3412
3413         compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
3414         compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
3415         compiler.parameter("ramp_interpolate", interpolate);
3416         
3417         compiler.add(this, "node_rgb_ramp");
3418 }
3419
3420 /* Set Normal Node */
3421
3422 SetNormalNode::SetNormalNode()
3423 : ShaderNode("set_normal")
3424 {
3425         add_input("Direction", SHADER_SOCKET_VECTOR);
3426         add_output("Normal", SHADER_SOCKET_NORMAL);
3427 }
3428
3429 void SetNormalNode::compile(SVMCompiler& compiler)
3430 {
3431         ShaderInput  *direction_in = input("Direction");
3432         ShaderOutput *normal_out = output("Normal");
3433
3434         compiler.stack_assign(direction_in);
3435         compiler.stack_assign(normal_out);
3436
3437         compiler.add_node(NODE_CLOSURE_SET_NORMAL, direction_in->stack_offset, normal_out->stack_offset);
3438 }
3439
3440 void SetNormalNode::compile(OSLCompiler& compiler)
3441 {
3442         compiler.add(this, "node_set_normal"); 
3443 }
3444
3445 /* OSLScriptNode */
3446
3447 OSLScriptNode::OSLScriptNode()
3448 : ShaderNode("osl_script")
3449 {
3450 }
3451
3452 void OSLScriptNode::compile(SVMCompiler& compiler)
3453 {
3454         /* doesn't work for SVM, obviously ... */
3455 }
3456
3457 void OSLScriptNode::compile(OSLCompiler& compiler)
3458 {
3459         if(!filepath.empty())
3460                 compiler.add(this, filepath.c_str(), true);
3461         else
3462                 compiler.add(this, bytecode_hash.c_str(), false);
3463 }
3464
3465 /* Normal Map */
3466
3467 static ShaderEnum normal_map_space_init()
3468 {
3469         ShaderEnum enm;
3470
3471         enm.insert("Tangent", NODE_NORMAL_MAP_TANGENT);
3472         enm.insert("Object", NODE_NORMAL_MAP_OBJECT);
3473         enm.insert("World", NODE_NORMAL_MAP_WORLD);
3474         enm.insert("Blender Object", NODE_NORMAL_MAP_BLENDER_OBJECT);
3475         enm.insert("Blender World", NODE_NORMAL_MAP_BLENDER_WORLD);
3476
3477         return enm;
3478 }
3479
3480 ShaderEnum NormalMapNode::space_enum = normal_map_space_init();
3481
3482 NormalMapNode::NormalMapNode()
3483 : ShaderNode("normal_map")
3484 {
3485         space = ustring("Tangent");
3486         attribute = ustring("");
3487
3488         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3489         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3490         add_input("Color", SHADER_SOCKET_COLOR);
3491
3492         add_output("Normal", SHADER_SOCKET_NORMAL);
3493 }
3494
3495 void NormalMapNode::attributes(AttributeRequestSet *attributes)
3496 {
3497         if(space == ustring("Tangent")) {
3498                 if(attribute == ustring("")) {
3499                         attributes->add(ATTR_STD_UV_TANGENT);
3500                         attributes->add(ATTR_STD_UV_TANGENT_SIGN);
3501                 }
3502                 else {
3503                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3504                         attributes->add(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3505                 }
3506
3507                 attributes->add(ATTR_STD_VERTEX_NORMAL);
3508         }
3509         
3510         ShaderNode::attributes(attributes);
3511 }
3512
3513 void NormalMapNode::compile(SVMCompiler& compiler)
3514 {
3515         ShaderInput  *color_in = input("Color");
3516         ShaderInput  *strength_in = input("Strength");
3517         ShaderOutput *normal_out = output("Normal");
3518         int attr = 0, attr_sign = 0;
3519
3520         if(space == ustring("Tangent")) {
3521                 if(attribute == ustring("")) {
3522                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3523                         attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
3524                 }
3525                 else {
3526                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3527                         attr_sign = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3528                 }
3529         }
3530
3531         compiler.stack_assign(color_in);
3532         compiler.stack_assign(strength_in);
3533         compiler.stack_assign(normal_out);
3534
3535         compiler.add_node(NODE_NORMAL_MAP,
3536                 compiler.encode_uchar4(
3537                         color_in->stack_offset,
3538                         strength_in->stack_offset,
3539                         normal_out->stack_offset,
3540                         space_enum[space]),
3541                 attr, attr_sign);
3542 }
3543
3544 void NormalMapNode::compile(OSLCompiler& compiler)
3545 {
3546         if(space == ustring("Tangent")) {
3547                 if(attribute == ustring("")) {
3548                         compiler.parameter("attr_name", ustring("geom:tangent"));
3549                         compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
3550                 }
3551                 else {
3552                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3553                         compiler.parameter("attr_sign_name", ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3554                 }
3555         }
3556
3557         compiler.parameter("space", space);
3558
3559         compiler.add(this, "node_normal_map"); 
3560 }
3561
3562 /* Tangent */
3563
3564 static ShaderEnum tangent_direction_type_init()
3565 {
3566         ShaderEnum enm;
3567
3568         enm.insert("Radial", NODE_TANGENT_RADIAL);
3569         enm.insert("UV Map", NODE_TANGENT_UVMAP);
3570
3571         return enm;
3572 }
3573
3574 static ShaderEnum tangent_axis_init()
3575 {
3576         ShaderEnum enm;
3577
3578         enm.insert("X", NODE_TANGENT_AXIS_X);
3579         enm.insert("Y", NODE_TANGENT_AXIS_Y);
3580         enm.insert("Z", NODE_TANGENT_AXIS_Z);
3581
3582         return enm;
3583 }
3584
3585 ShaderEnum TangentNode::direction_type_enum = tangent_direction_type_init();
3586 ShaderEnum TangentNode::axis_enum = tangent_axis_init();
3587
3588 TangentNode::TangentNode()
3589 : ShaderNode("normal_map")
3590 {
3591         direction_type = ustring("Radial");
3592         axis = ustring("X");
3593         attribute = ustring("");
3594
3595         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3596         add_output("Tangent", SHADER_SOCKET_NORMAL);
3597 }
3598
3599 void TangentNode::attributes(AttributeRequestSet *attributes)
3600 {
3601         if(direction_type == ustring("UV Map")) {
3602                 if(attribute == ustring(""))
3603                         attributes->add(ATTR_STD_UV_TANGENT);
3604                 else
3605                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3606         }
3607         else
3608                 attributes->add(ATTR_STD_GENERATED);
3609         
3610         ShaderNode::attributes(attributes);
3611 }
3612
3613 void TangentNode::compile(SVMCompiler& compiler)
3614 {
3615         ShaderOutput *tangent_out = output("Tangent");
3616         int attr;
3617
3618         if(direction_type == ustring("UV Map")) {
3619                 if(attribute == ustring(""))
3620                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3621                 else
3622                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3623         }
3624         else
3625                 attr = compiler.attribute(ATTR_STD_GENERATED);
3626
3627         compiler.stack_assign(tangent_out);
3628
3629         compiler.add_node(NODE_TANGENT,
3630                 compiler.encode_uchar4(
3631                         tangent_out->stack_offset,
3632                         direction_type_enum[direction_type],
3633                         axis_enum[axis]), attr);
3634 }
3635
3636 void TangentNode::compile(OSLCompiler& compiler)
3637 {
3638         if(direction_type == ustring("UV Map")) {
3639                 if(attribute == ustring(""))
3640                         compiler.parameter("attr_name", ustring("geom:tangent"));
3641                 else
3642                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3643         }
3644
3645         compiler.parameter("direction_type", direction_type);
3646         compiler.parameter("axis", axis);
3647         compiler.add(this, "node_tangent"); 
3648 }
3649
3650 CCL_NAMESPACE_END
3651