Cycles / Vector Transform node:
[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         add_output("Ray Depth", SHADER_SOCKET_FLOAT);
2068 }
2069
2070 void LightPathNode::compile(SVMCompiler& compiler)
2071 {
2072         ShaderOutput *out;
2073
2074         out = output("Is Camera Ray");
2075         if(!out->links.empty()) {
2076                 compiler.stack_assign(out);
2077                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2078         }
2079
2080         out = output("Is Shadow Ray");
2081         if(!out->links.empty()) {
2082                 compiler.stack_assign(out);
2083                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2084         }
2085
2086         out = output("Is Diffuse Ray");
2087         if(!out->links.empty()) {
2088                 compiler.stack_assign(out);
2089                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2090         }
2091
2092         out = output("Is Glossy Ray");
2093         if(!out->links.empty()) {
2094                 compiler.stack_assign(out);
2095                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2096         }
2097
2098         out = output("Is Singular Ray");
2099         if(!out->links.empty()) {
2100                 compiler.stack_assign(out);
2101                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2102         }
2103
2104         out = output("Is Reflection Ray");
2105         if(!out->links.empty()) {
2106                 compiler.stack_assign(out);
2107                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2108         }
2109
2110
2111         out = output("Is Transmission Ray");
2112         if(!out->links.empty()) {
2113                 compiler.stack_assign(out);
2114                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2115         }
2116         
2117         out = output("Ray Length");
2118         if(!out->links.empty()) {
2119                 compiler.stack_assign(out);
2120                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2121         }
2122         
2123         out = output("Ray Depth");
2124         if(!out->links.empty()) {
2125                 compiler.stack_assign(out);
2126                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, out->stack_offset);
2127         }
2128
2129 }
2130
2131 void LightPathNode::compile(OSLCompiler& compiler)
2132 {
2133         compiler.add(this, "node_light_path");
2134 }
2135
2136 /* Light Falloff */
2137
2138 LightFalloffNode::LightFalloffNode()
2139 : ShaderNode("light_path")
2140 {
2141         add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2142         add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2143         add_output("Quadratic", SHADER_SOCKET_FLOAT);
2144         add_output("Linear", SHADER_SOCKET_FLOAT);
2145         add_output("Constant", SHADER_SOCKET_FLOAT);
2146 }
2147
2148 void LightFalloffNode::compile(SVMCompiler& compiler)
2149 {
2150         ShaderInput *strength_in = input("Strength");
2151         ShaderInput *smooth_in = input("Smooth");
2152
2153         compiler.stack_assign(strength_in);
2154         compiler.stack_assign(smooth_in);
2155
2156         ShaderOutput *out = output("Quadratic");
2157         if(!out->links.empty()) {
2158                 compiler.stack_assign(out);
2159                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2160                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2161         }
2162
2163         out = output("Linear");
2164         if(!out->links.empty()) {
2165                 compiler.stack_assign(out);
2166                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2167                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2168         }
2169
2170         out = output("Constant");
2171         if(!out->links.empty()) {
2172                 compiler.stack_assign(out);
2173                 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2174                         compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2175         }
2176 }
2177
2178 void LightFalloffNode::compile(OSLCompiler& compiler)
2179 {
2180         compiler.add(this, "node_light_falloff");
2181 }
2182
2183 /* Object Info */
2184
2185 ObjectInfoNode::ObjectInfoNode()
2186 : ShaderNode("object_info")
2187 {
2188         add_output("Location", SHADER_SOCKET_VECTOR);
2189         add_output("Object Index", SHADER_SOCKET_FLOAT);
2190         add_output("Material Index", SHADER_SOCKET_FLOAT);
2191         add_output("Random", SHADER_SOCKET_FLOAT);
2192 }
2193
2194 void ObjectInfoNode::compile(SVMCompiler& compiler)
2195 {
2196         ShaderOutput *out = output("Location");
2197         if(!out->links.empty()) {
2198                 compiler.stack_assign(out);
2199                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2200         }
2201
2202         out = output("Object Index");
2203         if(!out->links.empty()) {
2204                 compiler.stack_assign(out);
2205                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2206         }
2207
2208         out = output("Material Index");
2209         if(!out->links.empty()) {
2210                 compiler.stack_assign(out);
2211                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2212         }
2213
2214         out = output("Random");
2215         if(!out->links.empty()) {
2216                 compiler.stack_assign(out);
2217                 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2218         }
2219 }
2220
2221 void ObjectInfoNode::compile(OSLCompiler& compiler)
2222 {
2223         compiler.add(this, "node_object_info");
2224 }
2225
2226 /* Particle Info */
2227
2228 ParticleInfoNode::ParticleInfoNode()
2229 : ShaderNode("particle_info")
2230 {
2231         add_output("Index", SHADER_SOCKET_FLOAT);
2232         add_output("Age", SHADER_SOCKET_FLOAT);
2233         add_output("Lifetime", SHADER_SOCKET_FLOAT);
2234         add_output("Location", SHADER_SOCKET_POINT);
2235         #if 0   /* not yet supported */
2236         add_output("Rotation", SHADER_SOCKET_QUATERNION);
2237         #endif
2238         add_output("Size", SHADER_SOCKET_FLOAT);
2239         add_output("Velocity", SHADER_SOCKET_VECTOR);
2240         add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2241 }
2242
2243 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
2244 {
2245         if(!output("Index")->links.empty())
2246                 attributes->add(ATTR_STD_PARTICLE);
2247         if(!output("Age")->links.empty())
2248                 attributes->add(ATTR_STD_PARTICLE);
2249         if(!output("Lifetime")->links.empty())
2250                 attributes->add(ATTR_STD_PARTICLE);
2251         if(!output("Location")->links.empty())
2252                 attributes->add(ATTR_STD_PARTICLE);
2253         #if 0   /* not yet supported */
2254         if(!output("Rotation")->links.empty())
2255                 attributes->add(ATTR_STD_PARTICLE);
2256         #endif
2257         if(!output("Size")->links.empty())
2258                 attributes->add(ATTR_STD_PARTICLE);
2259         if(!output("Velocity")->links.empty())
2260                 attributes->add(ATTR_STD_PARTICLE);
2261         if(!output("Angular Velocity")->links.empty())
2262                 attributes->add(ATTR_STD_PARTICLE);
2263
2264         ShaderNode::attributes(attributes);
2265 }
2266
2267 void ParticleInfoNode::compile(SVMCompiler& compiler)
2268 {
2269         ShaderOutput *out;
2270         
2271         out = output("Index");
2272         if(!out->links.empty()) {
2273                 compiler.stack_assign(out);
2274                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2275         }
2276         
2277         out = output("Age");
2278         if(!out->links.empty()) {
2279                 compiler.stack_assign(out);
2280                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2281         }
2282         
2283         out = output("Lifetime");
2284         if(!out->links.empty()) {
2285                 compiler.stack_assign(out);
2286                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2287         }
2288         
2289         out = output("Location");
2290         if(!out->links.empty()) {
2291                 compiler.stack_assign(out);
2292                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2293         }
2294         
2295         /* quaternion data is not yet supported by Cycles */
2296 #if 0
2297         out = output("Rotation");
2298         if(!out->links.empty()) {
2299                 compiler.stack_assign(out);
2300                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2301         }
2302 #endif
2303         
2304         out = output("Size");
2305         if(!out->links.empty()) {
2306                 compiler.stack_assign(out);
2307                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2308         }
2309         
2310         out = output("Velocity");
2311         if(!out->links.empty()) {
2312                 compiler.stack_assign(out);
2313                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2314         }
2315         
2316         out = output("Angular Velocity");
2317         if(!out->links.empty()) {
2318                 compiler.stack_assign(out);
2319                 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2320         }
2321 }
2322
2323 void ParticleInfoNode::compile(OSLCompiler& compiler)
2324 {
2325         compiler.add(this, "node_particle_info");
2326 }
2327
2328 /* Hair Info */
2329
2330 HairInfoNode::HairInfoNode()
2331 : ShaderNode("hair_info")
2332 {
2333         add_output("Is Strand", SHADER_SOCKET_FLOAT);
2334         add_output("Intercept", SHADER_SOCKET_FLOAT);
2335         add_output("Thickness", SHADER_SOCKET_FLOAT);
2336         add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2337         /*output for minimum hair width transparency - deactivated*/
2338         /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2339 }
2340
2341 void HairInfoNode::attributes(AttributeRequestSet *attributes)
2342 {
2343         ShaderOutput *intercept_out = output("Intercept");
2344
2345         if(!intercept_out->links.empty())
2346                 attributes->add(ATTR_STD_CURVE_INTERCEPT);
2347         
2348         ShaderNode::attributes(attributes);
2349 }
2350
2351 void HairInfoNode::compile(SVMCompiler& compiler)
2352 {
2353         ShaderOutput *out;
2354         
2355         out = output("Is Strand");
2356         if(!out->links.empty()) {
2357                 compiler.stack_assign(out);
2358                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2359         }
2360
2361         out = output("Intercept");
2362         if(!out->links.empty()) {
2363                 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2364                 compiler.stack_assign(out);
2365                 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2366         }
2367
2368         out = output("Thickness");
2369         if(!out->links.empty()) {
2370                 compiler.stack_assign(out);
2371                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2372         }
2373
2374         out = output("Tangent Normal");
2375         if(!out->links.empty()) {
2376                 compiler.stack_assign(out);
2377                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2378         }
2379
2380         /*out = output("Fade");
2381         if(!out->links.empty()) {
2382                 compiler.stack_assign(out);
2383                 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2384         }*/
2385
2386 }
2387
2388 void HairInfoNode::compile(OSLCompiler& compiler)
2389 {
2390         compiler.add(this, "node_hair_info");
2391 }
2392
2393 /* Value */
2394
2395 ValueNode::ValueNode()
2396 : ShaderNode("value")
2397 {
2398         value = 0.0f;
2399
2400         add_output("Value", SHADER_SOCKET_FLOAT);
2401 }
2402
2403 void ValueNode::compile(SVMCompiler& compiler)
2404 {
2405         ShaderOutput *val_out = output("Value");
2406
2407         compiler.stack_assign(val_out);
2408         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2409 }
2410
2411 void ValueNode::compile(OSLCompiler& compiler)
2412 {
2413         compiler.parameter("value_value", value);
2414         compiler.add(this, "node_value");
2415 }
2416
2417 /* Color */
2418
2419 ColorNode::ColorNode()
2420 : ShaderNode("color")
2421 {
2422         value = make_float3(0.0f, 0.0f, 0.0f);
2423
2424         add_output("Color", SHADER_SOCKET_COLOR);
2425 }
2426
2427 void ColorNode::compile(SVMCompiler& compiler)
2428 {
2429         ShaderOutput *color_out = output("Color");
2430
2431         if(color_out && !color_out->links.empty()) {
2432                 compiler.stack_assign(color_out);
2433                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2434                 compiler.add_node(NODE_VALUE_V, value);
2435         }
2436 }
2437
2438 void ColorNode::compile(OSLCompiler& compiler)
2439 {
2440         compiler.parameter_color("color_value", value);
2441
2442         compiler.add(this, "node_value");
2443 }
2444
2445 /* Add Closure */
2446
2447 AddClosureNode::AddClosureNode()
2448 : ShaderNode("add_closure")
2449 {
2450         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2451         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2452         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2453 }
2454
2455 void AddClosureNode::compile(SVMCompiler& compiler)
2456 {
2457         /* handled in the SVM compiler */
2458 }
2459
2460 void AddClosureNode::compile(OSLCompiler& compiler)
2461 {
2462         compiler.add(this, "node_add_closure");
2463 }
2464
2465 /* Mix Closure */
2466
2467 MixClosureNode::MixClosureNode()
2468 : ShaderNode("mix_closure")
2469 {
2470         special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2471         
2472         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2473         add_input("Closure1", SHADER_SOCKET_CLOSURE);
2474         add_input("Closure2", SHADER_SOCKET_CLOSURE);
2475         add_output("Closure",  SHADER_SOCKET_CLOSURE);
2476 }
2477
2478 void MixClosureNode::compile(SVMCompiler& compiler)
2479 {
2480         /* handled in the SVM compiler */
2481 }
2482
2483 void MixClosureNode::compile(OSLCompiler& compiler)
2484 {
2485         compiler.add(this, "node_mix_closure");
2486 }
2487
2488 /* Mix Closure */
2489
2490 MixClosureWeightNode::MixClosureWeightNode()
2491 : ShaderNode("mix_closure_weight")
2492 {
2493         add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2494         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2495         add_output("Weight1", SHADER_SOCKET_FLOAT);
2496         add_output("Weight2", SHADER_SOCKET_FLOAT);
2497 }
2498
2499 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2500 {
2501         ShaderInput *weight_in = input("Weight");
2502         ShaderInput *fac_in = input("Fac");
2503         ShaderOutput *weight1_out = output("Weight1");
2504         ShaderOutput *weight2_out = output("Weight2");
2505
2506         compiler.stack_assign(weight_in);
2507         compiler.stack_assign(fac_in);
2508         compiler.stack_assign(weight1_out);
2509         compiler.stack_assign(weight2_out);
2510
2511         compiler.add_node(NODE_MIX_CLOSURE,
2512                 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2513                         weight1_out->stack_offset, weight2_out->stack_offset));
2514 }
2515
2516 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2517 {
2518         assert(0);
2519 }
2520
2521 /* Invert */
2522
2523 InvertNode::InvertNode()
2524 : ShaderNode("invert")
2525 {
2526         add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2527         add_input("Color", SHADER_SOCKET_COLOR);
2528         add_output("Color",  SHADER_SOCKET_COLOR);
2529 }
2530
2531 void InvertNode::compile(SVMCompiler& compiler)
2532 {
2533         ShaderInput *fac_in = input("Fac");
2534         ShaderInput *color_in = input("Color");
2535         ShaderOutput *color_out = output("Color");
2536
2537         compiler.stack_assign(fac_in);
2538         compiler.stack_assign(color_in);
2539         compiler.stack_assign(color_out);
2540
2541         compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2542 }
2543
2544 void InvertNode::compile(OSLCompiler& compiler)
2545 {
2546         compiler.add(this, "node_invert");
2547 }
2548
2549 /* Mix */
2550
2551 MixNode::MixNode()
2552 : ShaderNode("mix")
2553 {
2554         type = ustring("Mix");
2555
2556         use_clamp = false;
2557
2558         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2559         add_input("Color1", SHADER_SOCKET_COLOR);
2560         add_input("Color2", SHADER_SOCKET_COLOR);
2561         add_output("Color",  SHADER_SOCKET_COLOR);
2562 }
2563
2564 static ShaderEnum mix_type_init()
2565 {
2566         ShaderEnum enm;
2567
2568         enm.insert("Mix", NODE_MIX_BLEND);
2569         enm.insert("Add", NODE_MIX_ADD);
2570         enm.insert("Multiply", NODE_MIX_MUL);
2571         enm.insert("Screen", NODE_MIX_SCREEN);
2572         enm.insert("Overlay", NODE_MIX_OVERLAY);
2573         enm.insert("Subtract", NODE_MIX_SUB);
2574         enm.insert("Divide", NODE_MIX_DIV);
2575         enm.insert("Difference", NODE_MIX_DIFF);
2576         enm.insert("Darken", NODE_MIX_DARK);
2577         enm.insert("Lighten", NODE_MIX_LIGHT);
2578         enm.insert("Dodge", NODE_MIX_DODGE);
2579         enm.insert("Burn", NODE_MIX_BURN);
2580         enm.insert("Hue", NODE_MIX_HUE);
2581         enm.insert("Saturation", NODE_MIX_SAT);
2582         enm.insert("Value", NODE_MIX_VAL);
2583         enm.insert("Color", NODE_MIX_COLOR);
2584         enm.insert("Soft Light", NODE_MIX_SOFT);
2585         enm.insert("Linear Light", NODE_MIX_LINEAR);
2586
2587         return enm;
2588 }
2589
2590 ShaderEnum MixNode::type_enum = mix_type_init();
2591
2592 void MixNode::compile(SVMCompiler& compiler)
2593 {
2594         ShaderInput *fac_in = input("Fac");
2595         ShaderInput *color1_in = input("Color1");
2596         ShaderInput *color2_in = input("Color2");
2597         ShaderOutput *color_out = output("Color");
2598
2599         compiler.stack_assign(fac_in);
2600         compiler.stack_assign(color1_in);
2601         compiler.stack_assign(color2_in);
2602         compiler.stack_assign(color_out);
2603
2604         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2605         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2606
2607         if(use_clamp) {
2608                 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2609                 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2610         }
2611 }
2612
2613 void MixNode::compile(OSLCompiler& compiler)
2614 {
2615         compiler.parameter("type", type);
2616         compiler.parameter("Clamp", use_clamp);
2617         compiler.add(this, "node_mix");
2618 }
2619
2620 /* Combine RGB */
2621 CombineRGBNode::CombineRGBNode()
2622 : ShaderNode("combine_rgb")
2623 {
2624         add_input("R", SHADER_SOCKET_FLOAT);
2625         add_input("G", SHADER_SOCKET_FLOAT);
2626         add_input("B", SHADER_SOCKET_FLOAT);
2627         add_output("Image", SHADER_SOCKET_COLOR);
2628 }
2629
2630 void CombineRGBNode::compile(SVMCompiler& compiler)
2631 {
2632         ShaderInput *red_in = input("R");
2633         ShaderInput *green_in = input("G");
2634         ShaderInput *blue_in = input("B");
2635         ShaderOutput *color_out = output("Image");
2636
2637         compiler.stack_assign(color_out);
2638
2639         compiler.stack_assign(red_in);
2640         compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2641
2642         compiler.stack_assign(green_in);
2643         compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2644
2645         compiler.stack_assign(blue_in);
2646         compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2647 }
2648
2649 void CombineRGBNode::compile(OSLCompiler& compiler)
2650 {
2651         compiler.add(this, "node_combine_rgb");
2652 }
2653
2654 /* Gamma */
2655 GammaNode::GammaNode()
2656 : ShaderNode("gamma")
2657 {
2658         add_input("Color", SHADER_SOCKET_COLOR);
2659         add_input("Gamma", SHADER_SOCKET_FLOAT);
2660         add_output("Color", SHADER_SOCKET_COLOR);
2661 }
2662
2663 void GammaNode::compile(SVMCompiler& compiler)
2664 {
2665         ShaderInput *color_in = input("Color");
2666         ShaderInput *gamma_in = input("Gamma");
2667         ShaderOutput *color_out = output("Color");
2668
2669         compiler.stack_assign(color_in);
2670         compiler.stack_assign(gamma_in);
2671         compiler.stack_assign(color_out);
2672
2673         compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2674 }
2675
2676 void GammaNode::compile(OSLCompiler& compiler)
2677 {
2678         compiler.add(this, "node_gamma");
2679 }
2680
2681 /* Bright Contrast */
2682 BrightContrastNode::BrightContrastNode()
2683 : ShaderNode("brightness")
2684 {
2685         add_input("Color", SHADER_SOCKET_COLOR);
2686         add_input("Bright", SHADER_SOCKET_FLOAT);
2687         add_input("Contrast", SHADER_SOCKET_FLOAT);
2688         add_output("Color", SHADER_SOCKET_COLOR);
2689 }
2690
2691 void BrightContrastNode::compile(SVMCompiler& compiler)
2692 {
2693         ShaderInput *color_in = input("Color");
2694         ShaderInput *bright_in = input("Bright");
2695         ShaderInput *contrast_in = input("Contrast");
2696         ShaderOutput *color_out = output("Color");
2697
2698         compiler.stack_assign(color_in);
2699         compiler.stack_assign(bright_in);
2700         compiler.stack_assign(contrast_in);
2701         compiler.stack_assign(color_out);
2702
2703         compiler.add_node(NODE_BRIGHTCONTRAST,
2704                 color_in->stack_offset, color_out->stack_offset,
2705                 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2706 }
2707
2708 void BrightContrastNode::compile(OSLCompiler& compiler)
2709 {
2710         compiler.add(this, "node_brightness");
2711 }
2712
2713 /* Separate RGB */
2714 SeparateRGBNode::SeparateRGBNode()
2715 : ShaderNode("separate_rgb")
2716 {
2717         add_input("Image", SHADER_SOCKET_COLOR);
2718         add_output("R", SHADER_SOCKET_FLOAT);
2719         add_output("G", SHADER_SOCKET_FLOAT);
2720         add_output("B", SHADER_SOCKET_FLOAT);
2721 }
2722
2723 void SeparateRGBNode::compile(SVMCompiler& compiler)
2724 {
2725         ShaderInput *color_in = input("Image");
2726         ShaderOutput *red_out = output("R");
2727         ShaderOutput *green_out = output("G");
2728         ShaderOutput *blue_out = output("B");
2729
2730         compiler.stack_assign(color_in);
2731
2732         compiler.stack_assign(red_out);
2733         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2734
2735         compiler.stack_assign(green_out);
2736         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2737
2738         compiler.stack_assign(blue_out);
2739         compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2740 }
2741
2742 void SeparateRGBNode::compile(OSLCompiler& compiler)
2743 {
2744         compiler.add(this, "node_separate_rgb");
2745 }
2746
2747 /* Separate RGB */
2748 HSVNode::HSVNode()
2749 : ShaderNode("hsv")
2750 {
2751         add_input("Hue", SHADER_SOCKET_FLOAT);
2752         add_input("Saturation", SHADER_SOCKET_FLOAT);
2753         add_input("Value", SHADER_SOCKET_FLOAT);
2754         add_input("Fac", SHADER_SOCKET_FLOAT);
2755         add_input("Color", SHADER_SOCKET_COLOR);
2756         add_output("Color", SHADER_SOCKET_COLOR);
2757 }
2758
2759 void HSVNode::compile(SVMCompiler& compiler)
2760 {
2761         ShaderInput *hue_in = input("Hue");
2762         ShaderInput *saturation_in = input("Saturation");
2763         ShaderInput *value_in = input("Value");
2764         ShaderInput *fac_in = input("Fac");
2765         ShaderInput *color_in = input("Color");
2766         ShaderOutput *color_out = output("Color");
2767
2768         compiler.stack_assign(hue_in);
2769         compiler.stack_assign(saturation_in);
2770         compiler.stack_assign(value_in);
2771         compiler.stack_assign(fac_in);
2772         compiler.stack_assign(color_in);
2773         compiler.stack_assign(color_out);
2774
2775         compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2776         compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2777 }
2778
2779 void HSVNode::compile(OSLCompiler& compiler)
2780 {
2781         compiler.add(this, "node_hsv");
2782 }
2783
2784 /* Attribute */
2785
2786 AttributeNode::AttributeNode()
2787 : ShaderNode("attribute")
2788 {
2789         attribute = "";
2790
2791         add_output("Color",  SHADER_SOCKET_COLOR);
2792         add_output("Vector",  SHADER_SOCKET_VECTOR);
2793         add_output("Fac",  SHADER_SOCKET_FLOAT);
2794 }
2795
2796 void AttributeNode::attributes(AttributeRequestSet *attributes)
2797 {
2798         ShaderOutput *color_out = output("Color");
2799         ShaderOutput *vector_out = output("Vector");
2800         ShaderOutput *fac_out = output("Fac");
2801
2802         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2803                 attributes->add(attribute);
2804         
2805         ShaderNode::attributes(attributes);
2806 }
2807
2808 void AttributeNode::compile(SVMCompiler& compiler)
2809 {
2810         ShaderOutput *color_out = output("Color");
2811         ShaderOutput *vector_out = output("Vector");
2812         ShaderOutput *fac_out = output("Fac");
2813         NodeType attr_node = NODE_ATTR;
2814
2815         if(bump == SHADER_BUMP_DX)
2816                 attr_node = NODE_ATTR_BUMP_DX;
2817         else if(bump == SHADER_BUMP_DY)
2818                 attr_node = NODE_ATTR_BUMP_DY;
2819
2820         if(!color_out->links.empty() || !vector_out->links.empty()) {
2821                 int attr = compiler.attribute(attribute);
2822
2823                 if(!color_out->links.empty()) {
2824                         compiler.stack_assign(color_out);
2825                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2826                 }
2827                 if(!vector_out->links.empty()) {
2828                         compiler.stack_assign(vector_out);
2829                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2830                 }
2831         }
2832
2833         if(!fac_out->links.empty()) {
2834                 int attr = compiler.attribute(attribute);
2835
2836                 compiler.stack_assign(fac_out);
2837                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2838         }
2839 }
2840
2841 void AttributeNode::compile(OSLCompiler& compiler)
2842 {
2843         if(bump == SHADER_BUMP_DX)
2844                 compiler.parameter("bump_offset", "dx");
2845         else if(bump == SHADER_BUMP_DY)
2846                 compiler.parameter("bump_offset", "dy");
2847         else
2848                 compiler.parameter("bump_offset", "center");
2849
2850         compiler.parameter("name", attribute.c_str());
2851         compiler.add(this, "node_attribute");
2852 }
2853
2854 /* Camera */
2855
2856 CameraNode::CameraNode()
2857 : ShaderNode("camera")
2858 {
2859         add_output("View Vector",  SHADER_SOCKET_VECTOR);
2860         add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
2861         add_output("View Distance",  SHADER_SOCKET_FLOAT);
2862 }
2863
2864 void CameraNode::compile(SVMCompiler& compiler)
2865 {
2866         ShaderOutput *vector_out = output("View Vector");
2867         ShaderOutput *z_depth_out = output("View Z Depth");
2868         ShaderOutput *distance_out = output("View Distance");
2869
2870         compiler.stack_assign(vector_out);
2871         compiler.stack_assign(z_depth_out);
2872         compiler.stack_assign(distance_out);
2873         compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2874 }
2875
2876 void CameraNode::compile(OSLCompiler& compiler)
2877 {
2878         compiler.add(this, "node_camera");
2879 }
2880
2881 /* Fresnel */
2882
2883 FresnelNode::FresnelNode()
2884 : ShaderNode("Fresnel")
2885 {
2886         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2887         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2888         add_output("Fac", SHADER_SOCKET_FLOAT);
2889 }
2890
2891 void FresnelNode::compile(SVMCompiler& compiler)
2892 {
2893         ShaderInput *ior_in = input("IOR");
2894         ShaderOutput *fac_out = output("Fac");
2895
2896         compiler.stack_assign(ior_in);
2897         compiler.stack_assign(fac_out);
2898         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2899 }
2900
2901 void FresnelNode::compile(OSLCompiler& compiler)
2902 {
2903         compiler.add(this, "node_fresnel");
2904 }
2905
2906 /* Blend Weight */
2907
2908 LayerWeightNode::LayerWeightNode()
2909 : ShaderNode("LayerWeight")
2910 {
2911         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2912         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2913
2914         add_output("Fresnel", SHADER_SOCKET_FLOAT);
2915         add_output("Facing", SHADER_SOCKET_FLOAT);
2916 }
2917
2918 void LayerWeightNode::compile(SVMCompiler& compiler)
2919 {
2920         ShaderInput *blend_in = input("Blend");
2921
2922         if(blend_in->link)
2923                 compiler.stack_assign(blend_in);
2924
2925         ShaderOutput *fresnel_out = output("Fresnel");
2926         if(!fresnel_out->links.empty()) {
2927                 compiler.stack_assign(fresnel_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_FRESNEL, fresnel_out->stack_offset));
2930         }
2931
2932         ShaderOutput *facing_out = output("Facing");
2933         if(!facing_out->links.empty()) {
2934                 compiler.stack_assign(facing_out);
2935                 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2936                         compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
2937         }
2938 }
2939
2940 void LayerWeightNode::compile(OSLCompiler& compiler)
2941 {
2942         compiler.add(this, "node_layer_weight");
2943 }
2944
2945 /* Wireframe */
2946
2947 WireframeNode::WireframeNode()
2948 : ShaderNode("Wireframe")
2949 {
2950         add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
2951         add_output("Fac", SHADER_SOCKET_FLOAT);
2952         
2953         use_pixel_size = false;
2954 }
2955
2956 void WireframeNode::compile(SVMCompiler& compiler)
2957 {
2958         ShaderInput *size_in = input("Size");
2959         ShaderOutput *fac_out = output("Fac");
2960
2961         compiler.stack_assign(size_in);
2962         compiler.stack_assign(fac_out);
2963         compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
2964 }
2965
2966 void WireframeNode::compile(OSLCompiler& compiler)
2967 {
2968         compiler.parameter("use_pixel_size", use_pixel_size);
2969         compiler.add(this, "node_wireframe");
2970 }
2971
2972 /* Wavelength */
2973
2974 WavelengthNode::WavelengthNode()
2975 : ShaderNode("Wavelength")
2976 {
2977         add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
2978         add_output("Color", SHADER_SOCKET_COLOR);
2979 }
2980
2981 void WavelengthNode::compile(SVMCompiler& compiler)
2982 {
2983         ShaderInput *wavelength_in = input("Wavelength");
2984         ShaderOutput *color_out = output("Color");
2985
2986         compiler.stack_assign(wavelength_in);
2987         compiler.stack_assign(color_out);
2988         compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset);
2989 }
2990
2991 void WavelengthNode::compile(OSLCompiler& compiler)
2992 {
2993         compiler.add(this, "node_wavelength");
2994 }
2995
2996 /* Blackbody */
2997
2998 BlackbodyNode::BlackbodyNode()
2999 : ShaderNode("Blackbody")
3000 {
3001         add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f);
3002         add_output("Color", SHADER_SOCKET_COLOR);
3003 }
3004
3005 void BlackbodyNode::compile(SVMCompiler& compiler)
3006 {
3007         ShaderInput *temperature_in = input("Temperature");
3008         ShaderOutput *color_out = output("Color");
3009
3010         compiler.stack_assign(temperature_in);
3011         compiler.stack_assign(color_out);
3012         compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset);
3013 }
3014
3015 void BlackbodyNode::compile(OSLCompiler& compiler)
3016 {
3017         compiler.add(this, "node_blackbody");
3018 }
3019
3020 /* Output */
3021
3022 OutputNode::OutputNode()
3023 : ShaderNode("output")
3024 {
3025         add_input("Surface", SHADER_SOCKET_CLOSURE);
3026         add_input("Volume", SHADER_SOCKET_CLOSURE);
3027         add_input("Displacement", SHADER_SOCKET_FLOAT);
3028         add_input("Normal", SHADER_SOCKET_NORMAL);
3029 }
3030
3031 void OutputNode::compile(SVMCompiler& compiler)
3032 {
3033         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
3034                 ShaderInput *displacement_in = input("Displacement");
3035
3036                 if(displacement_in->link) {
3037                         compiler.stack_assign(displacement_in);
3038                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
3039                 }
3040         }
3041 }
3042
3043 void OutputNode::compile(OSLCompiler& compiler)
3044 {
3045         if(compiler.output_type() == SHADER_TYPE_SURFACE)
3046                 compiler.add(this, "node_output_surface");
3047         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3048                 compiler.add(this, "node_output_volume");
3049         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3050                 compiler.add(this, "node_output_displacement");
3051 }
3052
3053 /* Math */
3054
3055 MathNode::MathNode()
3056 : ShaderNode("math")
3057 {
3058         type = ustring("Add");
3059
3060         use_clamp = false;
3061
3062         add_input("Value1", SHADER_SOCKET_FLOAT);
3063         add_input("Value2", SHADER_SOCKET_FLOAT);
3064         add_output("Value",  SHADER_SOCKET_FLOAT);
3065 }
3066
3067 static ShaderEnum math_type_init()
3068 {
3069         ShaderEnum enm;
3070
3071         enm.insert("Add", NODE_MATH_ADD);
3072         enm.insert("Subtract", NODE_MATH_SUBTRACT);
3073         enm.insert("Multiply", NODE_MATH_MULTIPLY);
3074         enm.insert("Divide", NODE_MATH_DIVIDE);
3075         enm.insert("Sine", NODE_MATH_SINE);
3076         enm.insert("Cosine", NODE_MATH_COSINE);
3077         enm.insert("Tangent", NODE_MATH_TANGENT);
3078         enm.insert("Arcsine", NODE_MATH_ARCSINE);
3079         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3080         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3081         enm.insert("Power", NODE_MATH_POWER);
3082         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3083         enm.insert("Minimum", NODE_MATH_MINIMUM);
3084         enm.insert("Maximum", NODE_MATH_MAXIMUM);
3085         enm.insert("Round", NODE_MATH_ROUND);
3086         enm.insert("Less Than", NODE_MATH_LESS_THAN);
3087         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3088         enm.insert("Modulo", NODE_MATH_MODULO);
3089
3090         return enm;
3091 }
3092
3093 ShaderEnum MathNode::type_enum = math_type_init();
3094
3095 void MathNode::compile(SVMCompiler& compiler)
3096 {
3097         ShaderInput *value1_in = input("Value1");
3098         ShaderInput *value2_in = input("Value2");
3099         ShaderOutput *value_out = output("Value");
3100
3101         compiler.stack_assign(value1_in);
3102         compiler.stack_assign(value2_in);
3103         compiler.stack_assign(value_out);
3104
3105         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3106         compiler.add_node(NODE_MATH, value_out->stack_offset);
3107
3108         if(use_clamp) {
3109                 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3110                 compiler.add_node(NODE_MATH, value_out->stack_offset);
3111         }
3112 }
3113
3114 void MathNode::compile(OSLCompiler& compiler)
3115 {
3116         compiler.parameter("type", type);
3117         compiler.parameter("Clamp", use_clamp);
3118         compiler.add(this, "node_math");
3119 }
3120
3121 /* VectorMath */
3122
3123 VectorMathNode::VectorMathNode()
3124 : ShaderNode("vector_math")
3125 {
3126         type = ustring("Add");
3127
3128         add_input("Vector1", SHADER_SOCKET_VECTOR);
3129         add_input("Vector2", SHADER_SOCKET_VECTOR);
3130         add_output("Value",  SHADER_SOCKET_FLOAT);
3131         add_output("Vector",  SHADER_SOCKET_VECTOR);
3132 }
3133
3134 static ShaderEnum vector_math_type_init()
3135 {
3136         ShaderEnum enm;
3137
3138         enm.insert("Add", NODE_VECTOR_MATH_ADD);
3139         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3140         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3141         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3142         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3143         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3144
3145         return enm;
3146 }
3147
3148 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3149
3150 void VectorMathNode::compile(SVMCompiler& compiler)
3151 {
3152         ShaderInput *vector1_in = input("Vector1");
3153         ShaderInput *vector2_in = input("Vector2");
3154         ShaderOutput *value_out = output("Value");
3155         ShaderOutput *vector_out = output("Vector");
3156
3157         compiler.stack_assign(vector1_in);
3158         compiler.stack_assign(vector2_in);
3159         compiler.stack_assign(value_out);
3160         compiler.stack_assign(vector_out);
3161
3162         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
3163         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
3164 }
3165
3166 void VectorMathNode::compile(OSLCompiler& compiler)
3167 {
3168         compiler.parameter("type", type);
3169         compiler.add(this, "node_vector_math");
3170 }
3171
3172 /* VectorTransform */
3173
3174 VectorTransformNode::VectorTransformNode()
3175 : ShaderNode("vector_transform")
3176 {
3177         type = ustring("Vector");
3178         convert_from = ustring("world");
3179         convert_to = ustring("object");
3180
3181         add_input("Vector", SHADER_SOCKET_VECTOR);
3182         add_output("Vector",  SHADER_SOCKET_VECTOR);
3183 }
3184
3185 static ShaderEnum vector_transform_type_init()
3186 {
3187         ShaderEnum enm;
3188
3189         enm.insert("Vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
3190         enm.insert("Point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
3191         enm.insert("Normal", NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
3192
3193         return enm;
3194 }
3195
3196 static ShaderEnum vector_transform_convert_space_init()
3197 {
3198         ShaderEnum enm;
3199
3200         enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
3201         enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
3202         enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA);
3203
3204         return enm;
3205 }
3206
3207 ShaderEnum VectorTransformNode::type_enum = vector_transform_type_init();
3208 ShaderEnum VectorTransformNode::convert_space_enum = vector_transform_convert_space_init();
3209
3210 void VectorTransformNode::compile(SVMCompiler& compiler)
3211 {
3212         ShaderInput *vector_in = input("Vector");
3213         ShaderOutput *vector_out = output("Vector");
3214
3215         compiler.stack_assign(vector_in);
3216         compiler.stack_assign(vector_out);
3217
3218         compiler.add_node(NODE_VECTOR_TRANSFORM,
3219                 compiler.encode_uchar4(type_enum[type], convert_space_enum[convert_from], convert_space_enum[convert_to]),
3220                 compiler.encode_uchar4(vector_in->stack_offset, vector_out->stack_offset));
3221 }
3222
3223 void VectorTransformNode::compile(OSLCompiler& compiler)
3224 {
3225         compiler.parameter("type", type);
3226         compiler.parameter("convert_from", convert_from);
3227         compiler.parameter("convert_to", convert_to);
3228         compiler.add(this, "node_vector_transform");
3229 }
3230
3231 /* BumpNode */
3232
3233 BumpNode::BumpNode()
3234 : ShaderNode("bump")
3235 {
3236         invert = false;
3237
3238         /* this input is used by the user, but after graph transform it is no longer
3239          * used and moved to sampler center/x/y instead */
3240         add_input("Height", SHADER_SOCKET_FLOAT);
3241
3242         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
3243         add_input("SampleX", SHADER_SOCKET_FLOAT);
3244         add_input("SampleY", SHADER_SOCKET_FLOAT);
3245         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
3246         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3247         add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
3248
3249         add_output("Normal", SHADER_SOCKET_NORMAL);
3250 }
3251
3252 void BumpNode::compile(SVMCompiler& compiler)
3253 {
3254         ShaderInput *center_in = input("SampleCenter");
3255         ShaderInput *dx_in = input("SampleX");
3256         ShaderInput *dy_in = input("SampleY");
3257         ShaderInput *normal_in = input("Normal");
3258         ShaderInput *strength_in = input("Strength");
3259         ShaderInput *distance_in = input("Distance");
3260         ShaderOutput *normal_out = output("Normal");
3261
3262         compiler.stack_assign(center_in);
3263         compiler.stack_assign(dx_in);
3264         compiler.stack_assign(dy_in);
3265         compiler.stack_assign(strength_in);
3266         compiler.stack_assign(distance_in);
3267         compiler.stack_assign(normal_out);
3268
3269         if(normal_in->link)
3270                 compiler.stack_assign(normal_in);
3271         
3272         /* pack all parameters in the node */
3273         compiler.add_node(NODE_SET_BUMP,
3274                 compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
3275                 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
3276                         dy_in->stack_offset, strength_in->stack_offset),
3277                 normal_out->stack_offset);
3278 }
3279
3280 void BumpNode::compile(OSLCompiler& compiler)
3281 {
3282         compiler.parameter("invert", invert);
3283         compiler.add(this, "node_bump");
3284 }
3285
3286 /* RGBCurvesNode */
3287
3288 RGBCurvesNode::RGBCurvesNode()
3289 : ShaderNode("rgb_curves")
3290 {
3291         add_input("Fac", SHADER_SOCKET_FLOAT);
3292         add_input("Color", SHADER_SOCKET_COLOR);
3293         add_output("Color", SHADER_SOCKET_COLOR);
3294 }
3295
3296 void RGBCurvesNode::compile(SVMCompiler& compiler)
3297 {
3298         ShaderInput *fac_in = input("Fac");
3299         ShaderInput *color_in = input("Color");
3300         ShaderOutput *color_out = output("Color");
3301
3302         compiler.stack_assign(fac_in);
3303         compiler.stack_assign(color_in);
3304         compiler.stack_assign(color_out);
3305
3306         compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3307         compiler.add_array(curves, RAMP_TABLE_SIZE);
3308 }
3309
3310 void RGBCurvesNode::compile(OSLCompiler& compiler)
3311 {
3312         float ramp[RAMP_TABLE_SIZE][3];
3313
3314         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3315                 ramp[i][0] = curves[i].x;
3316                 ramp[i][1] = curves[i].y;
3317                 ramp[i][2] = curves[i].z;
3318         }
3319
3320         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3321         compiler.add(this, "node_rgb_curves");
3322 }
3323
3324 /* VectorCurvesNode */
3325
3326 VectorCurvesNode::VectorCurvesNode()
3327 : ShaderNode("rgb_curves")
3328 {
3329         add_input("Fac", SHADER_SOCKET_FLOAT);
3330         add_input("Vector", SHADER_SOCKET_VECTOR);
3331         add_output("Vector", SHADER_SOCKET_VECTOR);
3332 }
3333
3334 void VectorCurvesNode::compile(SVMCompiler& compiler)
3335 {
3336         ShaderInput *fac_in = input("Fac");
3337         ShaderInput *vector_in = input("Vector");
3338         ShaderOutput *vector_out = output("Vector");
3339
3340         compiler.stack_assign(fac_in);
3341         compiler.stack_assign(vector_in);
3342         compiler.stack_assign(vector_out);
3343
3344         compiler.add_node(NODE_VECTOR_CURVES, fac_in->stack_offset, vector_in->stack_offset, vector_out->stack_offset);
3345         compiler.add_array(curves, RAMP_TABLE_SIZE);
3346 }
3347
3348 void VectorCurvesNode::compile(OSLCompiler& compiler)
3349 {
3350         float ramp[RAMP_TABLE_SIZE][3];
3351
3352         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3353                 ramp[i][0] = curves[i].x;
3354                 ramp[i][1] = curves[i].y;
3355                 ramp[i][2] = curves[i].z;
3356         }
3357
3358         compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3359         compiler.add(this, "node_vector_curves");
3360 }
3361
3362 /* RGBRampNode */
3363
3364 RGBRampNode::RGBRampNode()
3365 : ShaderNode("rgb_ramp")
3366 {
3367         add_input("Fac", SHADER_SOCKET_FLOAT);
3368         add_output("Color", SHADER_SOCKET_COLOR);
3369         add_output("Alpha", SHADER_SOCKET_FLOAT);
3370
3371         interpolate = true;
3372 }
3373
3374 void RGBRampNode::compile(SVMCompiler& compiler)
3375 {
3376         ShaderInput *fac_in = input("Fac");
3377         ShaderOutput *color_out = output("Color");
3378         ShaderOutput *alpha_out = output("Alpha");
3379
3380         compiler.stack_assign(fac_in);
3381         if(!color_out->links.empty())
3382                 compiler.stack_assign(color_out);
3383         if(!alpha_out->links.empty())
3384                 compiler.stack_assign(alpha_out);
3385
3386         compiler.add_node(NODE_RGB_RAMP,
3387                 compiler.encode_uchar4(
3388                         fac_in->stack_offset,
3389                         color_out->stack_offset,
3390                         alpha_out->stack_offset),
3391                 interpolate);
3392         compiler.add_array(ramp, RAMP_TABLE_SIZE);
3393 }
3394
3395 void RGBRampNode::compile(OSLCompiler& compiler)
3396 {
3397         /* OSL shader only takes separate RGB and A array, split the RGBA base array */
3398         /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
3399         float ramp_color[RAMP_TABLE_SIZE][3];
3400         float ramp_alpha[RAMP_TABLE_SIZE];
3401
3402         for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3403                 ramp_color[i][0] = ramp[i].x;
3404                 ramp_color[i][1] = ramp[i].y;
3405                 ramp_color[i][2] = ramp[i].z;
3406                 ramp_alpha[i] = ramp[i].w;
3407         }
3408
3409         compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
3410         compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
3411         compiler.parameter("ramp_interpolate", interpolate);
3412         
3413         compiler.add(this, "node_rgb_ramp");
3414 }
3415
3416 /* Set Normal Node */
3417
3418 SetNormalNode::SetNormalNode()
3419 : ShaderNode("set_normal")
3420 {
3421         add_input("Direction", SHADER_SOCKET_VECTOR);
3422         add_output("Normal", SHADER_SOCKET_NORMAL);
3423 }
3424
3425 void SetNormalNode::compile(SVMCompiler& compiler)
3426 {
3427         ShaderInput  *direction_in = input("Direction");
3428         ShaderOutput *normal_out = output("Normal");
3429
3430         compiler.stack_assign(direction_in);
3431         compiler.stack_assign(normal_out);
3432
3433         compiler.add_node(NODE_CLOSURE_SET_NORMAL, direction_in->stack_offset, normal_out->stack_offset);
3434 }
3435
3436 void SetNormalNode::compile(OSLCompiler& compiler)
3437 {
3438         compiler.add(this, "node_set_normal"); 
3439 }
3440
3441 /* OSLScriptNode */
3442
3443 OSLScriptNode::OSLScriptNode()
3444 : ShaderNode("osl_script")
3445 {
3446 }
3447
3448 void OSLScriptNode::compile(SVMCompiler& compiler)
3449 {
3450         /* doesn't work for SVM, obviously ... */
3451 }
3452
3453 void OSLScriptNode::compile(OSLCompiler& compiler)
3454 {
3455         if(!filepath.empty())
3456                 compiler.add(this, filepath.c_str(), true);
3457         else
3458                 compiler.add(this, bytecode_hash.c_str(), false);
3459 }
3460
3461 /* Normal Map */
3462
3463 static ShaderEnum normal_map_space_init()
3464 {
3465         ShaderEnum enm;
3466
3467         enm.insert("Tangent", NODE_NORMAL_MAP_TANGENT);
3468         enm.insert("Object", NODE_NORMAL_MAP_OBJECT);
3469         enm.insert("World", NODE_NORMAL_MAP_WORLD);
3470         enm.insert("Blender Object", NODE_NORMAL_MAP_BLENDER_OBJECT);
3471         enm.insert("Blender World", NODE_NORMAL_MAP_BLENDER_WORLD);
3472
3473         return enm;
3474 }
3475
3476 ShaderEnum NormalMapNode::space_enum = normal_map_space_init();
3477
3478 NormalMapNode::NormalMapNode()
3479 : ShaderNode("normal_map")
3480 {
3481         space = ustring("Tangent");
3482         attribute = ustring("");
3483
3484         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3485         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3486         add_input("Color", SHADER_SOCKET_COLOR);
3487
3488         add_output("Normal", SHADER_SOCKET_NORMAL);
3489 }
3490
3491 void NormalMapNode::attributes(AttributeRequestSet *attributes)
3492 {
3493         if(space == ustring("Tangent")) {
3494                 if(attribute == ustring("")) {
3495                         attributes->add(ATTR_STD_UV_TANGENT);
3496                         attributes->add(ATTR_STD_UV_TANGENT_SIGN);
3497                 }
3498                 else {
3499                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3500                         attributes->add(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3501                 }
3502
3503                 attributes->add(ATTR_STD_VERTEX_NORMAL);
3504         }
3505         
3506         ShaderNode::attributes(attributes);
3507 }
3508
3509 void NormalMapNode::compile(SVMCompiler& compiler)
3510 {
3511         ShaderInput  *color_in = input("Color");
3512         ShaderInput  *strength_in = input("Strength");
3513         ShaderOutput *normal_out = output("Normal");
3514         int attr = 0, attr_sign = 0;
3515
3516         if(space == ustring("Tangent")) {
3517                 if(attribute == ustring("")) {
3518                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3519                         attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
3520                 }
3521                 else {
3522                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3523                         attr_sign = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3524                 }
3525         }
3526
3527         compiler.stack_assign(color_in);
3528         compiler.stack_assign(strength_in);
3529         compiler.stack_assign(normal_out);
3530
3531         compiler.add_node(NODE_NORMAL_MAP,
3532                 compiler.encode_uchar4(
3533                         color_in->stack_offset,
3534                         strength_in->stack_offset,
3535                         normal_out->stack_offset,
3536                         space_enum[space]),
3537                 attr, attr_sign);
3538 }
3539
3540 void NormalMapNode::compile(OSLCompiler& compiler)
3541 {
3542         if(space == ustring("Tangent")) {
3543                 if(attribute == ustring("")) {
3544                         compiler.parameter("attr_name", ustring("geom:tangent"));
3545                         compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
3546                 }
3547                 else {
3548                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3549                         compiler.parameter("attr_sign_name", ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
3550                 }
3551         }
3552
3553         compiler.parameter("space", space);
3554
3555         compiler.add(this, "node_normal_map"); 
3556 }
3557
3558 /* Tangent */
3559
3560 static ShaderEnum tangent_direction_type_init()
3561 {
3562         ShaderEnum enm;
3563
3564         enm.insert("Radial", NODE_TANGENT_RADIAL);
3565         enm.insert("UV Map", NODE_TANGENT_UVMAP);
3566
3567         return enm;
3568 }
3569
3570 static ShaderEnum tangent_axis_init()
3571 {
3572         ShaderEnum enm;
3573
3574         enm.insert("X", NODE_TANGENT_AXIS_X);
3575         enm.insert("Y", NODE_TANGENT_AXIS_Y);
3576         enm.insert("Z", NODE_TANGENT_AXIS_Z);
3577
3578         return enm;
3579 }
3580
3581 ShaderEnum TangentNode::direction_type_enum = tangent_direction_type_init();
3582 ShaderEnum TangentNode::axis_enum = tangent_axis_init();
3583
3584 TangentNode::TangentNode()
3585 : ShaderNode("normal_map")
3586 {
3587         direction_type = ustring("Radial");
3588         axis = ustring("X");
3589         attribute = ustring("");
3590
3591         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3592         add_output("Tangent", SHADER_SOCKET_NORMAL);
3593 }
3594
3595 void TangentNode::attributes(AttributeRequestSet *attributes)
3596 {
3597         if(direction_type == ustring("UV Map")) {
3598                 if(attribute == ustring(""))
3599                         attributes->add(ATTR_STD_UV_TANGENT);
3600                 else
3601                         attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3602         }
3603         else
3604                 attributes->add(ATTR_STD_GENERATED);
3605         
3606         ShaderNode::attributes(attributes);
3607 }
3608
3609 void TangentNode::compile(SVMCompiler& compiler)
3610 {
3611         ShaderOutput *tangent_out = output("Tangent");
3612         int attr;
3613
3614         if(direction_type == ustring("UV Map")) {
3615                 if(attribute == ustring(""))
3616                         attr = compiler.attribute(ATTR_STD_UV_TANGENT);
3617                 else
3618                         attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
3619         }
3620         else
3621                 attr = compiler.attribute(ATTR_STD_GENERATED);
3622
3623         compiler.stack_assign(tangent_out);
3624
3625         compiler.add_node(NODE_TANGENT,
3626                 compiler.encode_uchar4(
3627                         tangent_out->stack_offset,
3628                         direction_type_enum[direction_type],
3629                         axis_enum[axis]), attr);
3630 }
3631
3632 void TangentNode::compile(OSLCompiler& compiler)
3633 {
3634         if(direction_type == ustring("UV Map")) {
3635                 if(attribute == ustring(""))
3636                         compiler.parameter("attr_name", ustring("geom:tangent"));
3637                 else
3638                         compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
3639         }
3640
3641         compiler.parameter("direction_type", direction_type);
3642         compiler.parameter("axis", axis);
3643         compiler.add(this, "node_tangent"); 
3644 }
3645
3646 CCL_NAMESPACE_END
3647