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