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