Cycles: add some volume nodes, they don't actually do anything, this is just
[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 /* Image Texture */
29
30 static ShaderEnum color_space_init()
31 {
32         ShaderEnum enm;
33
34         enm.insert("Linear", 0);
35         enm.insert("sRGB", 1);
36
37         return enm;
38 }
39
40 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
41
42 ImageTextureNode::ImageTextureNode()
43 : ShaderNode("image_texture")
44 {
45         image_manager = NULL;
46         slot = -1;
47         filename = "";
48         color_space = ustring("sRGB");
49
50         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
51         add_output("Color", SHADER_SOCKET_COLOR);
52         add_output("Alpha", SHADER_SOCKET_FLOAT);
53 }
54
55 ImageTextureNode::~ImageTextureNode()
56 {
57         if(image_manager)
58                 image_manager->remove_image(filename);
59 }
60
61 ShaderNode *ImageTextureNode::clone() const
62 {
63         ImageTextureNode *node = new ImageTextureNode(*this);
64         node->image_manager = NULL;
65         node->slot = -1;
66         return node;
67 }
68
69 void ImageTextureNode::compile(SVMCompiler& compiler)
70 {
71         ShaderInput *vector_in = input("Vector");
72         ShaderOutput *color_out = output("Color");
73         ShaderOutput *alpha_out = output("Alpha");
74
75         image_manager = compiler.image_manager;
76         if(slot == -1)
77                 slot = image_manager->add_image(filename);
78
79         if(!color_out->links.empty())
80                 compiler.stack_assign(color_out);
81         if(!alpha_out->links.empty())
82                 compiler.stack_assign(alpha_out);
83
84         if(slot != -1) {
85                 compiler.stack_assign(vector_in);
86                 compiler.add_node(NODE_TEX_IMAGE,
87                         slot,
88                         compiler.encode_uchar4(
89                                 vector_in->stack_offset,
90                                 color_out->stack_offset,
91                                 alpha_out->stack_offset,
92                                 color_space_enum[color_space]));
93         }
94         else {
95                 /* image not found */
96                 if(!color_out->links.empty()) {
97                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
98                         compiler.add_node(NODE_VALUE_V, make_float3(0, 0, 0));
99                 }
100                 if(!alpha_out->links.empty())
101                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), alpha_out->stack_offset);
102         }
103 }
104
105 void ImageTextureNode::compile(OSLCompiler& compiler)
106 {
107         compiler.parameter("filename", filename.c_str());
108         compiler.parameter("color_space", color_space.c_str());
109         compiler.add(this, "node_image_texture");
110 }
111
112 /* Environment Texture */
113
114 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
115
116 EnvironmentTextureNode::EnvironmentTextureNode()
117 : ShaderNode("environment_texture")
118 {
119         image_manager = NULL;
120         slot = -1;
121         filename = "";
122         color_space = ustring("sRGB");
123
124         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
125         add_output("Color", SHADER_SOCKET_COLOR);
126         add_output("Alpha", SHADER_SOCKET_FLOAT);
127 }
128
129 EnvironmentTextureNode::~EnvironmentTextureNode()
130 {
131         if(image_manager)
132                 image_manager->remove_image(filename);
133 }
134
135 ShaderNode *EnvironmentTextureNode::clone() const
136 {
137         EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
138         node->image_manager = NULL;
139         node->slot = -1;
140         return node;
141 }
142
143 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
144 {
145         ShaderInput *vector_in = input("Vector");
146         ShaderOutput *color_out = output("Color");
147         ShaderOutput *alpha_out = output("Alpha");
148
149         image_manager = compiler.image_manager;
150         if(slot == -1)
151                 slot = image_manager->add_image(filename);
152
153         if(!color_out->links.empty())
154                 compiler.stack_assign(color_out);
155         if(!alpha_out->links.empty())
156                 compiler.stack_assign(alpha_out);
157
158         if(slot != -1) {
159                 compiler.stack_assign(vector_in);
160                 compiler.add_node(NODE_TEX_ENVIRONMENT,
161                         slot,
162                         compiler.encode_uchar4(
163                                 vector_in->stack_offset,
164                                 color_out->stack_offset,
165                                 alpha_out->stack_offset,
166                                 color_space_enum[color_space]));
167         }
168         else {
169                 /* image not found */
170                 if(!color_out->links.empty()) {
171                         compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
172                         compiler.add_node(NODE_VALUE_V, make_float3(0, 0, 0));
173                 }
174                 if(!alpha_out->links.empty())
175                         compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), alpha_out->stack_offset);
176         }
177 }
178
179 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
180 {
181         compiler.parameter("filename", filename.c_str());
182         compiler.parameter("color_space", color_space.c_str());
183         compiler.add(this, "node_environment_texture");
184 }
185
186 /* Sky Texture */
187
188 static float2 sky_spherical_coordinates(float3 dir)
189 {
190         return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
191 }
192
193 static float sky_perez_function(float lam[6], float theta, float gamma)
194 {
195         return (1.f + lam[0]*expf(lam[1]/cosf(theta))) * (1.f + lam[2]*expf(lam[3]*gamma)  + lam[4]*cosf(gamma)*cosf(gamma));
196 }
197
198 static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity)
199 {
200         float2 spherical = sky_spherical_coordinates(dir);
201         float theta = spherical.x;
202         float phi = spherical.y;
203
204         ksunsky->theta = theta;
205         ksunsky->phi = phi;
206         ksunsky->dir = dir;
207
208         float theta2 = theta*theta;
209         float theta3 = theta*theta*theta;
210         float T = turbidity;
211         float T2 = T * T;
212
213         float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
214         ksunsky->zenith_Y = (4.0453f * T - 4.9710f) * tan(chi) - 0.2155f * T + 2.4192f;
215         ksunsky->zenith_Y *= 0.06f;
216
217         ksunsky->zenith_x =
218         (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
219         (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
220         (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
221
222         ksunsky->zenith_y =
223         (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
224         (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta  + 0.00516f) * T +
225         (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta  + 0.26688f);
226
227         ksunsky->perez_Y[0] = (0.1787f * T  - 1.4630f);
228         ksunsky->perez_Y[1] = (-0.3554f * T  + 0.4275f);
229         ksunsky->perez_Y[2] = (-0.0227f * T  + 5.3251f);
230         ksunsky->perez_Y[3] = (0.1206f * T  - 2.5771f);
231         ksunsky->perez_Y[4] = (-0.0670f * T  + 0.3703f);
232
233         ksunsky->perez_x[0] = (-0.0193f * T  - 0.2592f);
234         ksunsky->perez_x[1] = (-0.0665f * T  + 0.0008f);
235         ksunsky->perez_x[2] = (-0.0004f * T  + 0.2125f);
236         ksunsky->perez_x[3] = (-0.0641f * T  - 0.8989f);
237         ksunsky->perez_x[4] = (-0.0033f * T  + 0.0452f);
238
239         ksunsky->perez_y[0] = (-0.0167f * T  - 0.2608f);
240         ksunsky->perez_y[1] = (-0.0950f * T  + 0.0092f);
241         ksunsky->perez_y[2] = (-0.0079f * T  + 0.2102f);
242         ksunsky->perez_y[3] = (-0.0441f * T  - 1.6537f);
243         ksunsky->perez_y[4] = (-0.0109f * T  + 0.0529f);
244
245         ksunsky->zenith_Y /= sky_perez_function(ksunsky->perez_Y, 0, theta);
246         ksunsky->zenith_x /= sky_perez_function(ksunsky->perez_x, 0, theta);
247         ksunsky->zenith_y /= sky_perez_function(ksunsky->perez_y, 0, theta);
248 }
249
250 SkyTextureNode::SkyTextureNode()
251 : ShaderNode("sky_texture")
252 {
253         sun_direction = make_float3(0.0f, 0.0f, 1.0f);
254         turbidity = 2.2f;
255
256         add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
257         add_output("Color", SHADER_SOCKET_COLOR);
258 }
259
260 void SkyTextureNode::compile(SVMCompiler& compiler)
261 {
262         ShaderInput *vector_in = input("Vector");
263         ShaderOutput *color_out = output("Color");
264
265         if(compiler.sunsky) {
266                 sky_texture_precompute(compiler.sunsky, sun_direction, turbidity);
267                 compiler.sunsky = NULL;
268         }
269
270         if(vector_in->link)
271                 compiler.stack_assign(vector_in);
272         compiler.stack_assign(color_out);
273         compiler.add_node(NODE_TEX_SKY, vector_in->stack_offset, color_out->stack_offset);
274 }
275
276 void SkyTextureNode::compile(OSLCompiler& compiler)
277 {
278         compiler.parameter_vector("sun_direction", sun_direction);
279         compiler.parameter("turbidity", turbidity);
280         compiler.add(this, "node_sky_texture");
281 }
282
283 /* Noise Texture */
284
285 NoiseTextureNode::NoiseTextureNode()
286 : ShaderNode("noise_texture")
287 {
288         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
289         add_output("Color", SHADER_SOCKET_COLOR);
290         add_output("Fac", SHADER_SOCKET_FLOAT);
291 }
292
293 void NoiseTextureNode::compile(SVMCompiler& compiler)
294 {
295         ShaderInput *vector_in = input("Vector");
296         ShaderOutput *color_out = output("Color");
297         ShaderOutput *fac_out = output("Fac");
298
299         if(!color_out->links.empty()) {
300                 compiler.stack_assign(vector_in);
301                 compiler.stack_assign(color_out);
302                 compiler.add_node(NODE_TEX_NOISE_V, vector_in->stack_offset, color_out->stack_offset);
303         }
304
305         if(!fac_out->links.empty()) {
306                 compiler.stack_assign(vector_in);
307                 compiler.stack_assign(fac_out);
308                 compiler.add_node(NODE_TEX_NOISE_F, vector_in->stack_offset, fac_out->stack_offset);
309         }
310 }
311
312 void NoiseTextureNode::compile(OSLCompiler& compiler)
313 {
314         compiler.add(this, "node_noise_texture");
315 }
316
317 /* Blend Texture */
318
319 static ShaderEnum blend_progression_init()
320 {
321         ShaderEnum enm;
322
323         enm.insert("Linear", NODE_BLEND_LINEAR);
324         enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
325         enm.insert("Easing", NODE_BLEND_EASING);
326         enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
327         enm.insert("Radial", NODE_BLEND_RADIAL);
328         enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
329         enm.insert("Spherical", NODE_BLEND_SPHERICAL);
330
331         return enm;
332 }
333
334 static ShaderEnum blend_axis_init()
335 {
336         ShaderEnum enm;
337
338         enm.insert("Horizontal", NODE_BLEND_HORIZONTAL);
339         enm.insert("Vertical", NODE_BLEND_VERTICAL);
340
341         return enm;
342 }
343
344 ShaderEnum BlendTextureNode::progression_enum = blend_progression_init();
345 ShaderEnum BlendTextureNode::axis_enum = blend_axis_init();
346
347 BlendTextureNode::BlendTextureNode()
348 : ShaderNode("blend_texture")
349 {
350         progression = ustring("Linear");
351         axis = ustring("Horizontal");
352
353         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
354         add_output("Fac", SHADER_SOCKET_FLOAT);
355 }
356
357 void BlendTextureNode::compile(SVMCompiler& compiler)
358 {
359         ShaderInput *vector_in = input("Vector");
360         ShaderOutput *fac_out = output("Fac");
361
362         if(vector_in->link) compiler.stack_assign(vector_in);
363
364         compiler.stack_assign(fac_out);
365         compiler.add_node(NODE_TEX_BLEND,
366                 compiler.encode_uchar4(progression_enum[progression], axis_enum[axis]),
367                 vector_in->stack_offset, fac_out->stack_offset);
368 }
369
370 void BlendTextureNode::compile(OSLCompiler& compiler)
371 {
372         compiler.parameter("Progression", progression);
373         compiler.parameter("Axis", axis);
374         compiler.add(this, "node_blend_texture");
375 }
376
377 /* Clouds Texture */
378
379 static ShaderEnum noise_basis_init()
380 {
381         ShaderEnum enm;
382
383         enm.insert("Perlin", NODE_NOISE_PERLIN);
384         enm.insert("Voronoi F1", NODE_NOISE_VORONOI_F1);
385         enm.insert("Voronoi F2", NODE_NOISE_VORONOI_F2);
386         enm.insert("Voronoi F3", NODE_NOISE_VORONOI_F3);
387         enm.insert("Voronoi F4", NODE_NOISE_VORONOI_F4);
388         enm.insert("Voronoi F2-F1", NODE_NOISE_VORONOI_F2_F1);
389         enm.insert("Voronoi Crackle", NODE_NOISE_VORONOI_CRACKLE);
390         enm.insert("Cell Noise", NODE_NOISE_CELL_NOISE);
391
392         return enm;
393 }
394
395 ShaderEnum CloudsTextureNode::basis_enum = noise_basis_init();
396
397 CloudsTextureNode::CloudsTextureNode()
398 : ShaderNode("clouds_texture")
399 {
400         basis = ustring("Perlin");
401         hard = false;
402         depth = 2;
403
404         add_input("Size", SHADER_SOCKET_FLOAT, 0.25f);
405         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
406
407         add_output("Color", SHADER_SOCKET_COLOR);
408         add_output("Fac", SHADER_SOCKET_FLOAT);
409 }
410
411 void CloudsTextureNode::compile(SVMCompiler& compiler)
412 {
413         ShaderInput *size_in = input("Size");
414         ShaderInput *vector_in = input("Vector");
415         ShaderOutput *color_out = output("Color");
416         ShaderOutput *fac_out = output("Fac");
417
418         if(vector_in->link) compiler.stack_assign(vector_in);
419         if(size_in->link) compiler.stack_assign(size_in);
420
421         compiler.stack_assign(color_out);
422         compiler.stack_assign(fac_out);
423
424         compiler.add_node(NODE_TEX_CLOUDS,
425                 compiler.encode_uchar4(basis_enum[basis], hard, depth),
426                 compiler.encode_uchar4(size_in->stack_offset, vector_in->stack_offset, fac_out->stack_offset, color_out->stack_offset),
427                 __float_as_int(size_in->value.x));
428 }
429
430 void CloudsTextureNode::compile(OSLCompiler& compiler)
431 {
432         compiler.parameter("Hard", (hard)? 1: 0);
433         compiler.parameter("Depth", depth);
434         compiler.parameter("Basis", basis);
435         compiler.add(this, "node_clouds_texture");
436 }
437
438 /* Voronoi Texture */
439
440 static ShaderEnum distance_metric_init()
441 {
442         ShaderEnum enm;
443
444         enm.insert("Distance Squared", NODE_VORONOI_DISTANCE_SQUARED);
445         enm.insert("Actual Distance", NODE_VORONOI_ACTUAL_DISTANCE);
446         enm.insert("Manhattan", NODE_VORONOI_MANHATTAN);
447         enm.insert("Chebychev", NODE_VORONOI_CHEBYCHEV);
448         enm.insert("Minkovsky 1/2", NODE_VORONOI_MINKOVSKY_H);
449         enm.insert("Minkovsky 4", NODE_VORONOI_MINKOVSKY_4);
450         enm.insert("Minkovsky", NODE_VORONOI_MINKOVSKY);
451
452         return enm;
453 }
454
455 static ShaderEnum voronoi_coloring_init()
456 {
457         ShaderEnum enm;
458
459         enm.insert("Intensity", NODE_VORONOI_INTENSITY);
460         enm.insert("Position", NODE_VORONOI_POSITION);
461         enm.insert("Position and Outline", NODE_VORONOI_POSITION_OUTLINE);
462         enm.insert("Position, Outline, and Intensity", NODE_VORONOI_POSITION_OUTLINE_INTENSITY);
463
464         return enm;
465 }
466
467 ShaderEnum VoronoiTextureNode::distance_metric_enum  = distance_metric_init();
468 ShaderEnum VoronoiTextureNode::coloring_enum  = voronoi_coloring_init();
469
470 VoronoiTextureNode::VoronoiTextureNode()
471 : ShaderNode("voronoi_texture")
472 {
473         distance_metric = ustring("Actual Distance");
474         coloring = ustring("Intensity");
475
476         add_input("Size", SHADER_SOCKET_FLOAT, 0.25f);
477         add_input("Weight1", SHADER_SOCKET_FLOAT, 1.0f);
478         add_input("Weight2", SHADER_SOCKET_FLOAT, 0.0f);
479         add_input("Weight3", SHADER_SOCKET_FLOAT, 0.0f);
480         add_input("Weight4", SHADER_SOCKET_FLOAT, 0.0f);
481         add_input("Exponent", SHADER_SOCKET_FLOAT, 2.5f);
482         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
483
484         add_output("Color", SHADER_SOCKET_COLOR);
485         add_output("Fac", SHADER_SOCKET_FLOAT);
486 }
487
488 void VoronoiTextureNode::compile(SVMCompiler& compiler)
489 {
490         ShaderInput *weight1_in = input("Weight1");
491         ShaderInput *weight2_in = input("Weight2");
492         ShaderInput *weight3_in = input("Weight3");
493         ShaderInput *weight4_in = input("Weight4");
494         ShaderInput *exponent_in = input("Exponent");
495         ShaderInput *size_in = input("Size");
496         ShaderInput *vector_in = input("Vector");
497         ShaderOutput *color_out = output("Color");
498         ShaderOutput *fac_out = output("Fac");
499
500         if(weight1_in->link) compiler.stack_assign(weight1_in);
501         if(weight2_in->link) compiler.stack_assign(weight2_in);
502         if(weight3_in->link) compiler.stack_assign(weight3_in);
503         if(weight4_in->link) compiler.stack_assign(weight4_in);
504         if(exponent_in->link) compiler.stack_assign(exponent_in);
505         if(vector_in->link) compiler.stack_assign(vector_in);
506         if(size_in->link) compiler.stack_assign(size_in);
507
508         compiler.stack_assign(color_out);
509         compiler.stack_assign(fac_out);
510
511         compiler.add_node(NODE_TEX_VORONOI,
512                 compiler.encode_uchar4(distance_metric_enum[distance_metric], coloring_enum[coloring], exponent_in->stack_offset),
513                 compiler.encode_uchar4(size_in->stack_offset, vector_in->stack_offset, fac_out->stack_offset, color_out->stack_offset),
514                 compiler.encode_uchar4(weight1_in->stack_offset, weight2_in->stack_offset, weight3_in->stack_offset, weight4_in->stack_offset));
515         compiler.add_node(__float_as_int(weight1_in->value.x),
516                 __float_as_int(weight2_in->value.x),
517                 __float_as_int(weight3_in->value.x),
518                 __float_as_int(weight4_in->value.x));
519         compiler.add_node(__float_as_int(exponent_in->value.x),
520                 __float_as_int(size_in->value.x));
521 }
522
523 void VoronoiTextureNode::compile(OSLCompiler& compiler)
524 {
525         compiler.parameter("DistanceMetric", distance_metric);
526         compiler.parameter("Coloring", coloring);
527         compiler.add(this, "node_voronoi_texture");
528 }
529
530 /* Musgrave Texture */
531
532 static ShaderEnum musgrave_type_init()
533 {
534         ShaderEnum enm;
535
536         enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
537         enm.insert("fBM", NODE_MUSGRAVE_FBM);
538         enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
539         enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
540         enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
541
542         return enm;
543 }
544
545 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
546 ShaderEnum MusgraveTextureNode::basis_enum = noise_basis_init();
547
548 MusgraveTextureNode::MusgraveTextureNode()
549 : ShaderNode("musgrave_texture")
550 {
551         type = ustring("fBM");
552         basis = ustring("Perlin");
553
554         add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
555         add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
556         add_input("Octaves", SHADER_SOCKET_FLOAT, 2.0f);
557         add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
558         add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
559         add_input("Size", SHADER_SOCKET_FLOAT, 0.25f);
560         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
561
562         add_output("Fac", SHADER_SOCKET_FLOAT);
563 }
564
565 void MusgraveTextureNode::compile(SVMCompiler& compiler)
566 {
567         ShaderInput *vector_in = input("Vector");
568         ShaderInput *dimension_in = input("Dimension");
569         ShaderInput *lacunarity_in = input("Lacunarity");
570         ShaderInput *octaves_in = input("Octaves");
571         ShaderInput *offset_in = input("Offset");
572         ShaderInput *gain_in = input("Gain");
573         ShaderInput *size_in = input("Size");
574         ShaderOutput *fac_out = output("Fac");
575
576         if(vector_in->link) compiler.stack_assign(vector_in);
577         if(dimension_in->link) compiler.stack_assign(dimension_in);
578         if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
579         if(octaves_in->link) compiler.stack_assign(octaves_in);
580         if(offset_in->link) compiler.stack_assign(offset_in);
581         if(gain_in->link) compiler.stack_assign(gain_in);
582         if(size_in->link) compiler.stack_assign(size_in);
583
584         compiler.stack_assign(fac_out);
585         compiler.add_node(NODE_TEX_MUSGRAVE,
586                 compiler.encode_uchar4(type_enum[type], basis_enum[basis], vector_in->stack_offset, fac_out->stack_offset),
587                 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, octaves_in->stack_offset, offset_in->stack_offset),
588                 compiler.encode_uchar4(gain_in->stack_offset, size_in->stack_offset));
589         compiler.add_node(__float_as_int(dimension_in->value.x),
590                 __float_as_int(lacunarity_in->value.x),
591                 __float_as_int(octaves_in->value.x),
592                 __float_as_int(offset_in->value.x));
593         compiler.add_node(__float_as_int(gain_in->value.x),
594                 __float_as_int(size_in->value.x));
595 }
596
597 void MusgraveTextureNode::compile(OSLCompiler& compiler)
598 {
599         compiler.parameter("Type", type);
600         compiler.parameter("Basis", basis);
601
602         compiler.add(this, "node_musgrave_texture");
603 }
604
605 /* Marble Texture */
606
607 static ShaderEnum marble_type_init()
608 {
609         ShaderEnum enm;
610
611         enm.insert("Soft", NODE_MARBLE_SOFT);
612         enm.insert("Sharp", NODE_MARBLE_SHARP);
613         enm.insert("Sharper", NODE_MARBLE_SHARPER);
614
615         return enm;
616 }
617
618 static ShaderEnum noise_wave_init()
619 {
620         ShaderEnum enm;
621
622         enm.insert("Sine", NODE_WAVE_SINE);
623         enm.insert("Saw", NODE_WAVE_SAW);
624         enm.insert("Tri", NODE_WAVE_TRI);
625
626         return enm;
627 }
628
629 ShaderEnum MarbleTextureNode::type_enum = marble_type_init();
630 ShaderEnum MarbleTextureNode::wave_enum = noise_wave_init();
631 ShaderEnum MarbleTextureNode::basis_enum = noise_basis_init();
632
633 MarbleTextureNode::MarbleTextureNode()
634 : ShaderNode("marble_texture")
635 {
636         type = ustring("Soft");
637         wave = ustring("Sine");
638         basis = ustring("Perlin");
639         hard = false;
640         depth = 2;
641
642         add_input("Size", SHADER_SOCKET_FLOAT, 0.25f);
643         add_input("Turbulence", SHADER_SOCKET_FLOAT, 5.0f);
644         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
645
646         add_output("Fac", SHADER_SOCKET_FLOAT);
647 }
648
649 void MarbleTextureNode::compile(SVMCompiler& compiler)
650 {
651         ShaderInput *size_in = input("Size");
652         ShaderInput *turbulence_in = input("Turbulence");
653         ShaderInput *vector_in = input("Vector");
654         ShaderOutput *fac_out = output("Fac");
655
656         if(size_in->link) compiler.stack_assign(size_in);
657         if(turbulence_in->link) compiler.stack_assign(turbulence_in);
658         if(vector_in->link) compiler.stack_assign(vector_in);
659
660         compiler.stack_assign(fac_out);
661         compiler.add_node(NODE_TEX_MARBLE,
662                 compiler.encode_uchar4(type_enum[type], wave_enum[wave], basis_enum[basis], hard),
663                 compiler.encode_uchar4(depth),
664                 compiler.encode_uchar4(size_in->stack_offset, turbulence_in->stack_offset, vector_in->stack_offset, fac_out->stack_offset));
665         compiler.add_node(__float_as_int(size_in->value.x), __float_as_int(turbulence_in->value.x));
666 }
667
668 void MarbleTextureNode::compile(OSLCompiler& compiler)
669 {
670         compiler.parameter("Type", type);
671         compiler.parameter("Wave", wave);
672         compiler.parameter("Basis", basis);
673         compiler.parameter("Hard", (hard)? 1: 0);
674         compiler.parameter("Depth", depth);
675
676         compiler.add(this, "node_marble_texture");
677 }
678
679 /* Magic Texture */
680
681 MagicTextureNode::MagicTextureNode()
682 : ShaderNode("magic_texture")
683 {
684         depth = 2;
685
686         add_input("Turbulence", SHADER_SOCKET_FLOAT, 5.0f);
687         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
688         add_output("Color", SHADER_SOCKET_COLOR);
689 }
690
691 void MagicTextureNode::compile(SVMCompiler& compiler)
692 {
693         ShaderInput *vector_in = input("Vector");
694         ShaderInput *turbulence_in = input("Turbulence");
695         ShaderOutput *color_out = output("Color");
696
697         if(vector_in->link) compiler.stack_assign(vector_in);
698         if(turbulence_in->link) compiler.stack_assign(turbulence_in);
699
700         compiler.stack_assign(color_out);
701         compiler.add_node(NODE_TEX_MAGIC,
702                 compiler.encode_uchar4(depth, turbulence_in->stack_offset, vector_in->stack_offset, color_out->stack_offset),
703                 __float_as_int(turbulence_in->value.x));
704 }
705
706 void MagicTextureNode::compile(OSLCompiler& compiler)
707 {
708         compiler.parameter("Depth", depth);
709         compiler.add(this, "node_magic_texture");
710 }
711
712 /* Stucci Texture */
713
714 static ShaderEnum stucci_type_init()
715 {
716         ShaderEnum enm;
717
718         enm.insert("Plastic", NODE_STUCCI_PLASTIC);
719         enm.insert("Wall In", NODE_STUCCI_WALL_IN);
720         enm.insert("Wall Out", NODE_STUCCI_WALL_OUT);
721
722         return enm;
723 }
724
725 ShaderEnum StucciTextureNode::type_enum = stucci_type_init();
726 ShaderEnum StucciTextureNode::basis_enum = noise_basis_init();
727
728 StucciTextureNode::StucciTextureNode()
729 : ShaderNode("stucci_texture")
730 {
731         type = ustring("Plastic");
732         basis = ustring("Perlin");
733         hard = false;
734
735         add_input("Size", SHADER_SOCKET_FLOAT, 1.0f);
736         add_input("Turbulence", SHADER_SOCKET_FLOAT, 1.0f);
737         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
738
739         add_output("Fac", SHADER_SOCKET_FLOAT);
740 }
741
742 void StucciTextureNode::compile(SVMCompiler& compiler)
743 {
744         ShaderInput *size_in = input("Size");
745         ShaderInput *turbulence_in = input("Turbulence");
746         ShaderInput *vector_in = input("Vector");
747         ShaderOutput *fac_out = output("Fac");
748
749         if(size_in->link) compiler.stack_assign(size_in);
750         if(turbulence_in->link) compiler.stack_assign(turbulence_in);
751         if(vector_in->link) compiler.stack_assign(vector_in);
752
753         compiler.stack_assign(fac_out);
754
755         compiler.add_node(NODE_TEX_STUCCI,
756                 compiler.encode_uchar4(type_enum[type], basis_enum[basis], hard),
757                 compiler.encode_uchar4(size_in->stack_offset, turbulence_in->stack_offset,
758                         vector_in->stack_offset, fac_out->stack_offset));
759         compiler.add_node(__float_as_int(size_in->value.x),
760                 __float_as_int(turbulence_in->value.x));
761 }
762
763 void StucciTextureNode::compile(OSLCompiler& compiler)
764 {
765         compiler.parameter("Type", type);
766         compiler.parameter("Basis", basis);
767         compiler.parameter("Hard", (hard)? 1: 0);
768         compiler.add(this, "node_stucci_texture");
769 }
770
771 /* Distorted Noise Texture */
772
773 ShaderEnum DistortedNoiseTextureNode::basis_enum = noise_basis_init();
774
775 DistortedNoiseTextureNode::DistortedNoiseTextureNode()
776 : ShaderNode("distorted_noise_texture")
777 {
778         basis = ustring("Perlin");
779         distortion_basis = ustring("Perlin");
780
781         add_input("Size", SHADER_SOCKET_FLOAT, 0.25f);
782         add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
783         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
784
785         add_output("Fac", SHADER_SOCKET_FLOAT);
786 }
787
788 void DistortedNoiseTextureNode::compile(SVMCompiler& compiler)
789 {
790         ShaderInput *size_in = input("Size");
791         ShaderInput *distortion_in = input("Distortion");
792         ShaderInput *vector_in = input("Vector");
793         ShaderOutput *fac_out = output("Fac");
794
795         if(size_in->link) compiler.stack_assign(size_in);
796         if(distortion_in->link) compiler.stack_assign(distortion_in);
797         if(vector_in->link) compiler.stack_assign(vector_in);
798
799         compiler.stack_assign(fac_out);
800
801         compiler.add_node(NODE_TEX_DISTORTED_NOISE,
802                 compiler.encode_uchar4(basis_enum[basis], basis_enum[distortion_basis]),
803                 compiler.encode_uchar4(size_in->stack_offset, distortion_in->stack_offset, vector_in->stack_offset, fac_out->stack_offset));
804         compiler.add_node(__float_as_int(size_in->value.x),
805                 __float_as_int(distortion_in->value.x));
806 }
807
808 void DistortedNoiseTextureNode::compile(OSLCompiler& compiler)
809 {
810         compiler.parameter("Basis", basis);
811         compiler.parameter("DistortionBasis", distortion_basis);
812         compiler.add(this, "node_distorted_noise_texture");
813 }
814
815 /* Wood Texture */
816
817 static ShaderEnum wood_type_init()
818 {
819         ShaderEnum enm;
820
821         enm.insert("Bands", NODE_WOOD_BANDS);
822         enm.insert("Rings", NODE_WOOD_RINGS);
823         enm.insert("Band Noise", NODE_WOOD_BAND_NOISE);
824         enm.insert("Ring Noise", NODE_WOOD_RING_NOISE);
825
826         return enm;
827 }
828
829 ShaderEnum WoodTextureNode::type_enum = wood_type_init();
830 ShaderEnum WoodTextureNode::wave_enum = noise_wave_init();
831 ShaderEnum WoodTextureNode::basis_enum = noise_basis_init();
832
833 WoodTextureNode::WoodTextureNode()
834 : ShaderNode("wood_texture")
835 {
836         type = ustring("Bands");
837         wave = ustring("Sine");
838         basis = ustring("Perlin");
839         hard = false;
840
841         add_input("Size", SHADER_SOCKET_FLOAT, 0.25f);
842         add_input("Turbulence", SHADER_SOCKET_FLOAT, 5.0f);
843         add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_COORDINATE);
844
845         add_output("Fac", SHADER_SOCKET_FLOAT);
846 }
847
848 void WoodTextureNode::compile(SVMCompiler& compiler)
849 {
850         ShaderInput *vector_in = input("Vector");
851         ShaderInput *size_in = input("Size");
852         ShaderInput *turbulence_in = input("Turbulence");
853         ShaderOutput *fac_out = output("Fac");
854
855         if(vector_in->link) compiler.stack_assign(vector_in);
856         if(size_in->link) compiler.stack_assign(size_in);
857         if(turbulence_in->link) compiler.stack_assign(turbulence_in);
858
859         compiler.stack_assign(fac_out);
860         compiler.add_node(NODE_TEX_WOOD,
861                 compiler.encode_uchar4(type_enum[type], wave_enum[wave], basis_enum[basis], hard),
862                 compiler.encode_uchar4(vector_in->stack_offset, size_in->stack_offset, turbulence_in->stack_offset, fac_out->stack_offset));
863         compiler.add_node(NODE_TEX_WOOD, make_float3(size_in->value.x, turbulence_in->value.x, 0.0f));
864 }
865
866 void WoodTextureNode::compile(OSLCompiler& compiler)
867 {
868         compiler.parameter("Type", type);
869         compiler.parameter("Wave", wave);
870         compiler.parameter("Basis", basis);
871         compiler.parameter("Hard", (hard)? 1: 0);
872         compiler.add(this, "node_wood_texture");
873 }
874
875 /* Mapping */
876
877 MappingNode::MappingNode()
878 : ShaderNode("mapping")
879 {
880         add_input("Vector", SHADER_SOCKET_POINT);
881         add_output("Vector", SHADER_SOCKET_POINT);
882
883         translation = make_float3(0.0f, 0.0f, 0.0f);
884         rotation = make_float3(0.0f, 0.0f, 0.0f);
885         scale = make_float3(1.0f, 1.0f, 1.0f);
886 }
887
888 Transform MappingNode::compute_transform()
889 {
890         Transform smat = transform_scale(scale);
891         Transform rmat = transform_euler(rotation);
892         Transform tmat = transform_translate(translation);
893
894         return tmat*rmat*smat;
895 }
896
897 void MappingNode::compile(SVMCompiler& compiler)
898 {
899         ShaderInput *vector_in = input("Vector");
900         ShaderOutput *vector_out = output("Vector");
901
902         compiler.stack_assign(vector_in);
903         compiler.stack_assign(vector_out);
904
905         compiler.add_node(NODE_MAPPING, vector_in->stack_offset, vector_out->stack_offset);
906
907         Transform tfm = compute_transform();
908         compiler.add_node(tfm.x);
909         compiler.add_node(tfm.y);
910         compiler.add_node(tfm.z);
911         compiler.add_node(tfm.w);
912 }
913
914 void MappingNode::compile(OSLCompiler& compiler)
915 {
916         Transform tfm = transform_transpose(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 /* BSDF Closure */
1016
1017 BsdfNode::BsdfNode()
1018 : ShaderNode("bsdf")
1019 {
1020         closure = ccl::CLOSURE_BSDF_DIFFUSE_ID;
1021
1022         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1023         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1024
1025         add_output("BSDF", SHADER_SOCKET_CLOSURE);
1026 }
1027
1028 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1029 {
1030         ShaderInput *color_in = input("Color");
1031
1032         if(color_in->link) {
1033                 compiler.stack_assign(color_in);
1034                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1035         }
1036         else
1037                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1038         
1039         if(param1)
1040                 compiler.stack_assign(param1);
1041         if(param2)
1042                 compiler.stack_assign(param2);
1043
1044         compiler.add_node(NODE_CLOSURE_BSDF,
1045                 compiler.encode_uchar4(closure,
1046                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1047                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1048                         compiler.closure_mix_weight_offset()),
1049                 __float_as_int((param1)? param1->value.x: 0.0f),
1050                 __float_as_int((param2)? param2->value.x: 0.0f));
1051 }
1052
1053 void BsdfNode::compile(SVMCompiler& compiler)
1054 {
1055         compile(compiler, NULL, NULL);
1056 }
1057
1058 void BsdfNode::compile(OSLCompiler& compiler)
1059 {
1060         assert(0);
1061 }
1062
1063 /* Ward BSDF Closure */
1064
1065 WardBsdfNode::WardBsdfNode()
1066 {
1067         closure = CLOSURE_BSDF_WARD_ID;
1068
1069         add_input("Roughness U", SHADER_SOCKET_FLOAT, 0.2f);
1070         add_input("Roughness V", SHADER_SOCKET_FLOAT, 0.2f);
1071 }
1072
1073 void WardBsdfNode::compile(SVMCompiler& compiler)
1074 {
1075         BsdfNode::compile(compiler, input("Roughness U"), input("Roughness V"));
1076 }
1077
1078 void WardBsdfNode::compile(OSLCompiler& compiler)
1079 {
1080         compiler.add(this, "node_ward_bsdf");
1081 }
1082
1083 /* Glossy BSDF Closure */
1084
1085 static ShaderEnum glossy_distribution_init()
1086 {
1087         ShaderEnum enm;
1088
1089         enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1090         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1091         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1092
1093         return enm;
1094 }
1095
1096 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1097
1098 GlossyBsdfNode::GlossyBsdfNode()
1099 {
1100         distribution = ustring("Beckmann");
1101
1102         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1103 }
1104
1105 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1106 {
1107         closure = (ClosureType)distribution_enum[distribution];
1108
1109         if(closure == CLOSURE_BSDF_REFLECTION_ID)
1110                 BsdfNode::compile(compiler, NULL, NULL);
1111         else
1112                 BsdfNode::compile(compiler, input("Roughness"), NULL);
1113 }
1114
1115 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1116 {
1117         compiler.parameter("distribution", distribution);
1118         compiler.add(this, "node_glossy_bsdf");
1119 }
1120
1121 /* Glass BSDF Closure */
1122
1123 static ShaderEnum glass_distribution_init()
1124 {
1125         ShaderEnum enm;
1126
1127         enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1128         enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1129         enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1130
1131         return enm;
1132 }
1133
1134 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1135
1136 GlassBsdfNode::GlassBsdfNode()
1137 {
1138         distribution = ustring("Sharp");
1139
1140         add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1141         add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1142 }
1143
1144 void GlassBsdfNode::compile(SVMCompiler& compiler)
1145 {
1146         closure = (ClosureType)distribution_enum[distribution];
1147
1148         if(closure == CLOSURE_BSDF_REFRACTION_ID)
1149                 BsdfNode::compile(compiler, NULL, input("IOR"));
1150         else
1151                 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1152 }
1153
1154 void GlassBsdfNode::compile(OSLCompiler& compiler)
1155 {
1156         compiler.parameter("distribution", distribution);
1157         compiler.add(this, "node_glass_bsdf");
1158 }
1159
1160 /* Velvet BSDF Closure */
1161
1162 VelvetBsdfNode::VelvetBsdfNode()
1163 {
1164         closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1165
1166         add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1167 }
1168
1169 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1170 {
1171         BsdfNode::compile(compiler, input("Sigma"), NULL);
1172 }
1173
1174 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1175 {
1176         compiler.add(this, "node_velvet_bsdf");
1177 }
1178
1179 /* Diffuse BSDF Closure */
1180
1181 DiffuseBsdfNode::DiffuseBsdfNode()
1182 {
1183         closure = CLOSURE_BSDF_DIFFUSE_ID;
1184 }
1185
1186 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1187 {
1188         BsdfNode::compile(compiler, NULL, NULL);
1189 }
1190
1191 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1192 {
1193         compiler.add(this, "node_diffuse_bsdf");
1194 }
1195
1196 /* Translucent BSDF Closure */
1197
1198 TranslucentBsdfNode::TranslucentBsdfNode()
1199 {
1200         closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1201 }
1202
1203 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1204 {
1205         BsdfNode::compile(compiler, NULL, NULL);
1206 }
1207
1208 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1209 {
1210         compiler.add(this, "node_translucent_bsdf");
1211 }
1212
1213 /* Transparent BSDF Closure */
1214
1215 TransparentBsdfNode::TransparentBsdfNode()
1216 {
1217         closure = CLOSURE_BSDF_TRANSPARENT_ID;
1218 }
1219
1220 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1221 {
1222         BsdfNode::compile(compiler, NULL, NULL);
1223 }
1224
1225 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1226 {
1227         compiler.add(this, "node_transparent_bsdf");
1228 }
1229
1230 /* Emissive Closure */
1231
1232 EmissionNode::EmissionNode()
1233 : ShaderNode("emission")
1234 {
1235         total_power = false;
1236
1237         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1238         add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1239         add_output("Emission", SHADER_SOCKET_CLOSURE);
1240 }
1241
1242 void EmissionNode::compile(SVMCompiler& compiler)
1243 {
1244         ShaderInput *color_in = input("Color");
1245         ShaderInput *strength_in = input("Strength");
1246
1247         if(color_in->link || strength_in->link) {
1248                 compiler.stack_assign(color_in);
1249                 compiler.stack_assign(strength_in);
1250                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1251         }
1252         else if(total_power)
1253                 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1254         else
1255                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1256
1257         compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1258 }
1259
1260 void EmissionNode::compile(OSLCompiler& compiler)
1261 {
1262         compiler.parameter("TotalPower", (total_power)? 1: 0);
1263         compiler.add(this, "node_emission");
1264 }
1265
1266 /* Background Closure */
1267
1268 BackgroundNode::BackgroundNode()
1269 : ShaderNode("background")
1270 {
1271         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1272         add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1273         add_output("Background", SHADER_SOCKET_CLOSURE);
1274 }
1275
1276 void BackgroundNode::compile(SVMCompiler& compiler)
1277 {
1278         ShaderInput *color_in = input("Color");
1279         ShaderInput *strength_in = input("Strength");
1280
1281         if(color_in->link || strength_in->link) {
1282                 compiler.stack_assign(color_in);
1283                 compiler.stack_assign(strength_in);
1284                 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1285         }
1286         else
1287                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1288
1289         compiler.add_node(NODE_CLOSURE_BACKGROUND, CLOSURE_BACKGROUND_ID);
1290 }
1291
1292 void BackgroundNode::compile(OSLCompiler& compiler)
1293 {
1294         compiler.add(this, "node_background");
1295 }
1296
1297 /* Holdout Closure */
1298
1299 HoldoutNode::HoldoutNode()
1300 : ShaderNode("holdout")
1301 {
1302         add_output("Holdout", SHADER_SOCKET_CLOSURE);
1303 }
1304
1305 void HoldoutNode::compile(SVMCompiler& compiler)
1306 {
1307         compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1308 }
1309
1310 void HoldoutNode::compile(OSLCompiler& compiler)
1311 {
1312         compiler.add(this, "node_holdout");
1313 }
1314
1315 /* Volume Closure */
1316
1317 VolumeNode::VolumeNode()
1318 : ShaderNode("volume")
1319 {
1320         closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1321
1322         add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1323         add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1324
1325         add_output("Volume", SHADER_SOCKET_CLOSURE);
1326 }
1327
1328 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1329 {
1330         ShaderInput *color_in = input("Color");
1331
1332         if(color_in->link) {
1333                 compiler.stack_assign(color_in);
1334                 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1335         }
1336         else
1337                 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1338         
1339         if(param1)
1340                 compiler.stack_assign(param1);
1341         if(param2)
1342                 compiler.stack_assign(param2);
1343
1344         compiler.add_node(NODE_CLOSURE_VOLUME,
1345                 compiler.encode_uchar4(closure,
1346                         (param1)? param1->stack_offset: SVM_STACK_INVALID,
1347                         (param2)? param2->stack_offset: SVM_STACK_INVALID,
1348                         compiler.closure_mix_weight_offset()),
1349                 __float_as_int((param1)? param1->value.x: 0.0f),
1350                 __float_as_int((param2)? param2->value.x: 0.0f));
1351 }
1352
1353 void VolumeNode::compile(SVMCompiler& compiler)
1354 {
1355         compile(compiler, NULL, NULL);
1356 }
1357
1358 void VolumeNode::compile(OSLCompiler& compiler)
1359 {
1360         assert(0);
1361 }
1362
1363 /* Transparent Volume Closure */
1364
1365 TransparentVolumeNode::TransparentVolumeNode()
1366 {
1367         closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1368 }
1369
1370 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1371 {
1372         VolumeNode::compile(compiler, input("Density"), NULL);
1373 }
1374
1375 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1376 {
1377         compiler.add(this, "node_isotropic_volume");
1378 }
1379
1380 /* Isotropic Volume Closure */
1381
1382 IsotropicVolumeNode::IsotropicVolumeNode()
1383 {
1384         closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1385 }
1386
1387 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1388 {
1389         VolumeNode::compile(compiler, input("Density"), NULL);
1390 }
1391
1392 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1393 {
1394         compiler.add(this, "node_isotropic_volume");
1395 }
1396
1397 /* Geometry */
1398
1399 GeometryNode::GeometryNode()
1400 : ShaderNode("geometry")
1401 {
1402         add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1403         add_output("Position", SHADER_SOCKET_POINT);
1404         add_output("Normal", SHADER_SOCKET_NORMAL);
1405         add_output("Tangent", SHADER_SOCKET_NORMAL);
1406         add_output("True Normal", SHADER_SOCKET_NORMAL);
1407         add_output("Incoming", SHADER_SOCKET_VECTOR);
1408         add_output("Parametric", SHADER_SOCKET_POINT);
1409         add_output("Backfacing", SHADER_SOCKET_FLOAT);
1410 }
1411
1412 void GeometryNode::compile(SVMCompiler& compiler)
1413 {
1414         ShaderOutput *out;
1415         NodeType geom_node = NODE_GEOMETRY;
1416
1417         if(bump == SHADER_BUMP_DX)
1418                 geom_node = NODE_GEOMETRY_BUMP_DX;
1419         else if(bump == SHADER_BUMP_DY)
1420                 geom_node = NODE_GEOMETRY_BUMP_DY;
1421         
1422         out = output("Position");
1423         if(!out->links.empty()) {
1424                 compiler.stack_assign(out);
1425                 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1426         }
1427
1428         out = output("Normal");
1429         if(!out->links.empty()) {
1430                 compiler.stack_assign(out);
1431                 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1432         }
1433
1434         out = output("Tangent");
1435         if(!out->links.empty()) {
1436                 compiler.stack_assign(out);
1437                 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1438         }
1439
1440         out = output("True Normal");
1441         if(!out->links.empty()) {
1442                 compiler.stack_assign(out);
1443                 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1444         }
1445
1446         out = output("Incoming");
1447         if(!out->links.empty()) {
1448                 compiler.stack_assign(out);
1449                 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1450         }
1451
1452         out = output("Parametric");
1453         if(!out->links.empty()) {
1454                 compiler.stack_assign(out);
1455                 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1456         }
1457
1458         out = output("Backfacing");
1459         if(!out->links.empty()) {
1460                 compiler.stack_assign(out);
1461                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1462         }
1463 }
1464
1465 void GeometryNode::compile(OSLCompiler& compiler)
1466 {
1467         if(bump == SHADER_BUMP_DX)
1468                 compiler.parameter("bump_offset", "dx");
1469         else if(bump == SHADER_BUMP_DY)
1470                 compiler.parameter("bump_offset", "dy");
1471         else
1472                 compiler.parameter("bump_offset", "center");
1473
1474         compiler.add(this, "node_geometry");
1475 }
1476
1477 /* TextureCoordinate */
1478
1479 TextureCoordinateNode::TextureCoordinateNode()
1480 : ShaderNode("texture_coordinate")
1481 {
1482         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1483         add_output("Generated", SHADER_SOCKET_POINT);
1484         add_output("UV", SHADER_SOCKET_POINT);
1485         add_output("Object", SHADER_SOCKET_POINT);
1486         add_output("Camera", SHADER_SOCKET_POINT);
1487         add_output("Window", SHADER_SOCKET_POINT);
1488         add_output("Reflection", SHADER_SOCKET_NORMAL);
1489 }
1490
1491 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1492 {
1493         if(!output("Generated")->links.empty())
1494                 attributes->add(Attribute::STD_GENERATED);
1495         if(!output("UV")->links.empty())
1496                 attributes->add(Attribute::STD_UV);
1497
1498         ShaderNode::attributes(attributes);
1499 }
1500
1501 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1502 {
1503         ShaderOutput *out;
1504         NodeType texco_node = NODE_TEX_COORD;
1505         NodeType attr_node = NODE_ATTR;
1506         NodeType geom_node = NODE_GEOMETRY;
1507
1508         if(bump == SHADER_BUMP_DX) {
1509                 texco_node = NODE_TEX_COORD_BUMP_DX;
1510                 attr_node = NODE_ATTR_BUMP_DX;
1511                 geom_node = NODE_GEOMETRY_BUMP_DX;
1512         }
1513         else if(bump == SHADER_BUMP_DY) {
1514                 texco_node = NODE_TEX_COORD_BUMP_DY;
1515                 attr_node = NODE_ATTR_BUMP_DY;
1516                 geom_node = NODE_GEOMETRY_BUMP_DY;
1517         }
1518         
1519         out = output("Generated");
1520         if(!out->links.empty()) {
1521                 if(compiler.background) {
1522                         compiler.stack_assign(out);
1523                         compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1524                 }
1525                 else {
1526                         int attr = compiler.attribute(Attribute::STD_GENERATED);
1527                         compiler.stack_assign(out);
1528                         compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1529                 }
1530         }
1531
1532         out = output("UV");
1533         if(!out->links.empty()) {
1534                 int attr = compiler.attribute(Attribute::STD_UV);
1535                 compiler.stack_assign(out);
1536                 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1537         }
1538
1539         out = output("Object");
1540         if(!out->links.empty()) {
1541                 compiler.stack_assign(out);
1542                 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
1543         }
1544
1545         out = output("Camera");
1546         if(!out->links.empty()) {
1547                 compiler.stack_assign(out);
1548                 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
1549         }
1550
1551         out = output("Window");
1552         if(!out->links.empty()) {
1553                 compiler.stack_assign(out);
1554                 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
1555         }
1556
1557         out = output("Reflection");
1558         if(!out->links.empty()) {
1559                 if(compiler.background) {
1560                         compiler.stack_assign(out);
1561                         compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1562                 }
1563                 else {
1564                         compiler.stack_assign(out);
1565                         compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
1566                 }
1567         }
1568 }
1569
1570 void TextureCoordinateNode::compile(OSLCompiler& compiler)
1571 {
1572         if(bump == SHADER_BUMP_DX)
1573                 compiler.parameter("bump_offset", "dx");
1574         else if(bump == SHADER_BUMP_DY)
1575                 compiler.parameter("bump_offset", "dy");
1576         else
1577                 compiler.parameter("bump_offset", "center");
1578         
1579         if(compiler.background)
1580                 compiler.parameter("is_background", true);
1581
1582         compiler.add(this, "node_texture_coordinate");
1583 }
1584
1585 /* Light Path */
1586
1587 LightPathNode::LightPathNode()
1588 : ShaderNode("light_path")
1589 {
1590         add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
1591         add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
1592         add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
1593         add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
1594         add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
1595         add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
1596         add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
1597 }
1598
1599 void LightPathNode::compile(SVMCompiler& compiler)
1600 {
1601         ShaderOutput *out;
1602
1603         out = output("Is Camera Ray");
1604         if(!out->links.empty()) {
1605                 compiler.stack_assign(out);
1606                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
1607         }
1608
1609         out = output("Is Shadow Ray");
1610         if(!out->links.empty()) {
1611                 compiler.stack_assign(out);
1612                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
1613         }
1614
1615         out = output("Is Diffuse Ray");
1616         if(!out->links.empty()) {
1617                 compiler.stack_assign(out);
1618                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
1619         }
1620
1621         out = output("Is Glossy Ray");
1622         if(!out->links.empty()) {
1623                 compiler.stack_assign(out);
1624                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
1625         }
1626
1627         out = output("Is Singular Ray");
1628         if(!out->links.empty()) {
1629                 compiler.stack_assign(out);
1630                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
1631         }
1632
1633         out = output("Is Reflection Ray");
1634         if(!out->links.empty()) {
1635                 compiler.stack_assign(out);
1636                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
1637         }
1638
1639
1640         out = output("Is Transmission Ray");
1641         if(!out->links.empty()) {
1642                 compiler.stack_assign(out);
1643                 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
1644         }
1645 }
1646
1647 void LightPathNode::compile(OSLCompiler& compiler)
1648 {
1649         compiler.add(this, "node_light_path");
1650 }
1651
1652 /* Value */
1653
1654 ValueNode::ValueNode()
1655 : ShaderNode("value")
1656 {
1657         value = 0.0f;
1658
1659         add_output("Value", SHADER_SOCKET_FLOAT);
1660 }
1661
1662 void ValueNode::compile(SVMCompiler& compiler)
1663 {
1664         ShaderOutput *val_out = output("Value");
1665
1666         compiler.stack_assign(val_out);
1667         compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
1668 }
1669
1670 void ValueNode::compile(OSLCompiler& compiler)
1671 {
1672         compiler.parameter("value_value", value);
1673         compiler.add(this, "node_value");
1674 }
1675
1676 /* Color */
1677
1678 ColorNode::ColorNode()
1679 : ShaderNode("color")
1680 {
1681         value = make_float3(0.0f, 0.0f, 0.0f);
1682
1683         add_output("Color", SHADER_SOCKET_COLOR);
1684 }
1685
1686 void ColorNode::compile(SVMCompiler& compiler)
1687 {
1688         ShaderOutput *color_out = output("Color");
1689
1690         if(color_out && !color_out->links.empty()) {
1691                 compiler.stack_assign(color_out);
1692                 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
1693                 compiler.add_node(NODE_VALUE_V, value);
1694         }
1695 }
1696
1697 void ColorNode::compile(OSLCompiler& compiler)
1698 {
1699         compiler.parameter_color("color_value", value);
1700
1701         compiler.add(this, "node_value");
1702 }
1703
1704 /* Add Closure */
1705
1706 AddClosureNode::AddClosureNode()
1707 : ShaderNode("add_closure")
1708 {
1709         add_input("Closure1", SHADER_SOCKET_CLOSURE);
1710         add_input("Closure2", SHADER_SOCKET_CLOSURE);
1711         add_output("Closure",  SHADER_SOCKET_CLOSURE);
1712 }
1713
1714 void AddClosureNode::compile(SVMCompiler& compiler)
1715 {
1716         /* handled in the SVM compiler */
1717 }
1718
1719 void AddClosureNode::compile(OSLCompiler& compiler)
1720 {
1721         compiler.add(this, "node_add_closure");
1722 }
1723
1724 /* Mix Closure */
1725
1726 MixClosureNode::MixClosureNode()
1727 : ShaderNode("mix_closure")
1728 {
1729         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1730         add_input("Closure1", SHADER_SOCKET_CLOSURE);
1731         add_input("Closure2", SHADER_SOCKET_CLOSURE);
1732         add_output("Closure",  SHADER_SOCKET_CLOSURE);
1733 }
1734
1735 void MixClosureNode::compile(SVMCompiler& compiler)
1736 {
1737         /* handled in the SVM compiler */
1738 }
1739
1740 void MixClosureNode::compile(OSLCompiler& compiler)
1741 {
1742         compiler.add(this, "node_mix_closure");
1743 }
1744
1745 /* Mix */
1746
1747 MixNode::MixNode()
1748 : ShaderNode("mix")
1749 {
1750         type = ustring("Mix");
1751
1752         add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1753         add_input("Color1", SHADER_SOCKET_COLOR);
1754         add_input("Color2", SHADER_SOCKET_COLOR);
1755         add_output("Color",  SHADER_SOCKET_COLOR);
1756 }
1757
1758 static ShaderEnum mix_type_init()
1759 {
1760         ShaderEnum enm;
1761
1762         enm.insert("Mix", NODE_MIX_BLEND);
1763         enm.insert("Add", NODE_MIX_ADD);
1764         enm.insert("Multiply", NODE_MIX_MUL);
1765         enm.insert("Screen", NODE_MIX_SCREEN);
1766         enm.insert("Overlay", NODE_MIX_OVERLAY);
1767         enm.insert("Subtract", NODE_MIX_SUB);
1768         enm.insert("Divide", NODE_MIX_DIV);
1769         enm.insert("Difference", NODE_MIX_DIFF);
1770         enm.insert("Darken", NODE_MIX_DARK);
1771         enm.insert("Lighten", NODE_MIX_LIGHT);
1772         enm.insert("Dodge", NODE_MIX_DODGE);
1773         enm.insert("Burn", NODE_MIX_BURN);
1774         enm.insert("Hue", NODE_MIX_HUE);
1775         enm.insert("Saturation", NODE_MIX_SAT);
1776         enm.insert("Value", NODE_MIX_VAL );
1777         enm.insert("Color", NODE_MIX_COLOR);
1778         enm.insert("Soft Light", NODE_MIX_SOFT);
1779         enm.insert("Linear Light", NODE_MIX_LINEAR);
1780
1781         return enm;
1782 }
1783
1784 ShaderEnum MixNode::type_enum = mix_type_init();
1785
1786 void MixNode::compile(SVMCompiler& compiler)
1787 {
1788         ShaderInput *fac_in = input("Fac");
1789         ShaderInput *color1_in = input("Color1");
1790         ShaderInput *color2_in = input("Color2");
1791         ShaderOutput *color_out = output("Color");
1792
1793         compiler.stack_assign(fac_in);
1794         compiler.stack_assign(color1_in);
1795         compiler.stack_assign(color2_in);
1796         compiler.stack_assign(color_out);
1797
1798         compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
1799         compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
1800 }
1801
1802 void MixNode::compile(OSLCompiler& compiler)
1803 {
1804         compiler.parameter("type", type);
1805         compiler.add(this, "node_mix");
1806 }
1807
1808 /* Attribute */
1809
1810 AttributeNode::AttributeNode()
1811 : ShaderNode("attribute")
1812 {
1813         attribute = "";
1814
1815         add_output("Color",  SHADER_SOCKET_COLOR);
1816         add_output("Vector",  SHADER_SOCKET_VECTOR);
1817         add_output("Fac",  SHADER_SOCKET_FLOAT);
1818 }
1819
1820 void AttributeNode::attributes(AttributeRequestSet *attributes)
1821 {
1822         ShaderOutput *color_out = output("Color");
1823         ShaderOutput *vector_out = output("Vector");
1824         ShaderOutput *fac_out = output("Fac");
1825
1826         if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
1827                 attributes->add(attribute);
1828         
1829         ShaderNode::attributes(attributes);
1830 }
1831
1832 void AttributeNode::compile(SVMCompiler& compiler)
1833 {
1834         ShaderOutput *color_out = output("Color");
1835         ShaderOutput *vector_out = output("Vector");
1836         ShaderOutput *fac_out = output("Fac");
1837         NodeType attr_node = NODE_ATTR;
1838
1839         if(bump == SHADER_BUMP_DX)
1840                 attr_node = NODE_ATTR_BUMP_DX;
1841         else if(bump == SHADER_BUMP_DY)
1842                 attr_node = NODE_ATTR_BUMP_DY;
1843
1844         if(!color_out->links.empty() || !vector_out->links.empty()) {
1845                 int attr = compiler.attribute(attribute);
1846
1847                 if(!color_out->links.empty()) {
1848                         compiler.stack_assign(color_out);
1849                         compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
1850                 }
1851                 if(!vector_out->links.empty()) {
1852                         compiler.stack_assign(vector_out);
1853                         compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
1854                 }
1855         }
1856
1857         if(!fac_out->links.empty()) {
1858                 int attr = compiler.attribute(attribute);
1859
1860                 compiler.stack_assign(fac_out);
1861                 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
1862         }
1863 }
1864
1865 void AttributeNode::compile(OSLCompiler& compiler)
1866 {
1867         if(bump == SHADER_BUMP_DX)
1868                 compiler.parameter("bump_offset", "dx");
1869         else if(bump == SHADER_BUMP_DY)
1870                 compiler.parameter("bump_offset", "dy");
1871         else
1872                 compiler.parameter("bump_offset", "center");
1873
1874         compiler.parameter("name", attribute.c_str());
1875         compiler.add(this, "node_attribute");
1876 }
1877
1878 /* Fresnel */
1879
1880 FresnelNode::FresnelNode()
1881 : ShaderNode("Fresnel")
1882 {
1883         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1884         add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
1885         add_output("Fac", SHADER_SOCKET_FLOAT);
1886 }
1887
1888 void FresnelNode::compile(SVMCompiler& compiler)
1889 {
1890         ShaderInput *ior_in = input("IOR");
1891         ShaderOutput *fac_out = output("Fac");
1892
1893         compiler.stack_assign(ior_in);
1894         compiler.stack_assign(fac_out);
1895         compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
1896 }
1897
1898 void FresnelNode::compile(OSLCompiler& compiler)
1899 {
1900         compiler.add(this, "node_fresnel");
1901 }
1902
1903 /* Blend Weight */
1904
1905 BlendWeightNode::BlendWeightNode()
1906 : ShaderNode("BlendWeight")
1907 {
1908         add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1909         add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
1910
1911         add_output("Fresnel", SHADER_SOCKET_FLOAT);
1912         add_output("Facing", SHADER_SOCKET_FLOAT);
1913 }
1914
1915 void BlendWeightNode::compile(SVMCompiler& compiler)
1916 {
1917         ShaderInput *blend_in = input("Blend");
1918
1919         if(blend_in->link)
1920                 compiler.stack_assign(blend_in);
1921
1922         ShaderOutput *fresnel_out = output("Fresnel");
1923         if(!fresnel_out->links.empty()) {
1924                 compiler.stack_assign(fresnel_out);
1925                 compiler.add_node(NODE_BLEND_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
1926                         compiler.encode_uchar4(NODE_BLEND_WEIGHT_FRESNEL, fresnel_out->stack_offset));
1927         }
1928
1929         ShaderOutput *facing_out = output("Facing");
1930         if(!facing_out->links.empty()) {
1931                 compiler.stack_assign(facing_out);
1932                 compiler.add_node(NODE_BLEND_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
1933                         compiler.encode_uchar4(NODE_BLEND_WEIGHT_FACING, facing_out->stack_offset));
1934         }
1935 }
1936
1937 void BlendWeightNode::compile(OSLCompiler& compiler)
1938 {
1939         compiler.add(this, "node_blend_weight");
1940 }
1941
1942 /* Output */
1943
1944 OutputNode::OutputNode()
1945 : ShaderNode("output")
1946 {
1947         add_input("Surface", SHADER_SOCKET_CLOSURE);
1948         add_input("Volume", SHADER_SOCKET_CLOSURE);
1949         add_input("Displacement", SHADER_SOCKET_FLOAT);
1950 }
1951
1952 void OutputNode::compile(SVMCompiler& compiler)
1953 {
1954         if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
1955                 ShaderInput *displacement_in = input("Displacement");
1956
1957                 if(displacement_in->link) {
1958                         compiler.stack_assign(displacement_in);
1959                         compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
1960                 }
1961         }
1962 }
1963
1964 void OutputNode::compile(OSLCompiler& compiler)
1965 {
1966         if(compiler.output_type() == SHADER_TYPE_SURFACE)
1967                 compiler.add(this, "node_output_surface");
1968         else if(compiler.output_type() == SHADER_TYPE_VOLUME)
1969                 compiler.add(this, "node_output_volume");
1970         else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
1971                 compiler.add(this, "node_output_displacement");
1972 }
1973
1974 /* Math */
1975
1976 MathNode::MathNode()
1977 : ShaderNode("math")
1978 {
1979         type = ustring("Add");
1980
1981         add_input("Value1", SHADER_SOCKET_FLOAT);
1982         add_input("Value2", SHADER_SOCKET_FLOAT);
1983         add_output("Value",  SHADER_SOCKET_FLOAT);
1984 }
1985
1986 static ShaderEnum math_type_init()
1987 {
1988         ShaderEnum enm;
1989
1990         enm.insert("Add", NODE_MATH_ADD);
1991         enm.insert("Subtract", NODE_MATH_SUBTRACT);
1992         enm.insert("Multiply", NODE_MATH_MULTIPLY);
1993         enm.insert("Divide", NODE_MATH_DIVIDE);
1994         enm.insert("Sine", NODE_MATH_SINE);
1995         enm.insert("Cosine", NODE_MATH_COSINE);
1996         enm.insert("Tangent", NODE_MATH_TANGENT);
1997         enm.insert("Arcsine", NODE_MATH_ARCSINE);
1998         enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
1999         enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
2000         enm.insert("Power", NODE_MATH_POWER);
2001         enm.insert("Logarithm", NODE_MATH_LOGARITHM);
2002         enm.insert("Minimum", NODE_MATH_MINIMUM);
2003         enm.insert("Maximum", NODE_MATH_MAXIMUM);
2004         enm.insert("Round", NODE_MATH_ROUND);
2005         enm.insert("Less Than", NODE_MATH_LESS_THAN);
2006         enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
2007
2008         return enm;
2009 }
2010
2011 ShaderEnum MathNode::type_enum = math_type_init();
2012
2013 void MathNode::compile(SVMCompiler& compiler)
2014 {
2015         ShaderInput *value1_in = input("Value1");
2016         ShaderInput *value2_in = input("Value2");
2017         ShaderOutput *value_out = output("Value");
2018
2019         compiler.stack_assign(value1_in);
2020         compiler.stack_assign(value2_in);
2021         compiler.stack_assign(value_out);
2022
2023         compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
2024         compiler.add_node(NODE_MATH, value_out->stack_offset);
2025 }
2026
2027 void MathNode::compile(OSLCompiler& compiler)
2028 {
2029         compiler.parameter("type", type);
2030         compiler.add(this, "node_math");
2031 }
2032
2033 /* VectorMath */
2034
2035 VectorMathNode::VectorMathNode()
2036 : ShaderNode("vector_math")
2037 {
2038         type = ustring("Add");
2039
2040         add_input("Vector1", SHADER_SOCKET_VECTOR);
2041         add_input("Vector2", SHADER_SOCKET_VECTOR);
2042         add_output("Value",  SHADER_SOCKET_FLOAT);
2043         add_output("Vector",  SHADER_SOCKET_VECTOR);
2044 }
2045
2046 static ShaderEnum vector_math_type_init()
2047 {
2048         ShaderEnum enm;
2049
2050         enm.insert("Add", NODE_VECTOR_MATH_ADD);
2051         enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
2052         enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
2053         enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
2054         enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
2055         enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
2056
2057         return enm;
2058 }
2059
2060 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
2061
2062 void VectorMathNode::compile(SVMCompiler& compiler)
2063 {
2064         ShaderInput *vector1_in = input("Vector1");
2065         ShaderInput *vector2_in = input("Vector2");
2066         ShaderOutput *value_out = output("Value");
2067         ShaderOutput *vector_out = output("Vector");
2068
2069         compiler.stack_assign(vector1_in);
2070         compiler.stack_assign(vector2_in);
2071         compiler.stack_assign(value_out);
2072         compiler.stack_assign(vector_out);
2073
2074         compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
2075         compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
2076 }
2077
2078 void VectorMathNode::compile(OSLCompiler& compiler)
2079 {
2080         compiler.parameter("type", type);
2081         compiler.add(this, "node_vector_math");
2082 }
2083
2084 /* BumpNode */
2085
2086 BumpNode::BumpNode()
2087 : ShaderNode("bump")
2088 {
2089         add_input("SampleCenter", SHADER_SOCKET_FLOAT);
2090         add_input("SampleX", SHADER_SOCKET_FLOAT);
2091         add_input("SampleY", SHADER_SOCKET_FLOAT);
2092
2093         add_output("Normal", SHADER_SOCKET_NORMAL);
2094 }
2095
2096 void BumpNode::compile(SVMCompiler& compiler)
2097 {
2098         ShaderInput *center_in = input("SampleCenter");
2099         ShaderInput *dx_in = input("SampleX");
2100         ShaderInput *dy_in = input("SampleY");
2101
2102         compiler.stack_assign(center_in);
2103         compiler.stack_assign(dx_in);
2104         compiler.stack_assign(dy_in);
2105
2106         compiler.add_node(NODE_SET_BUMP, center_in->stack_offset, dx_in->stack_offset, dy_in->stack_offset);
2107 }
2108
2109 void BumpNode::compile(OSLCompiler& compiler)
2110 {
2111         compiler.add(this, "node_bump");
2112 }
2113
2114 CCL_NAMESPACE_END
2115