2 * Copyright 2011, Blender Foundation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "util_transform.h"
30 TextureMapping::TextureMapping()
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);
43 Transform TextureMapping::compute_transform()
45 Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
48 mmat[0][x_mapping-1] = 1.0f;
50 mmat[1][y_mapping-1] = 1.0f;
52 mmat[2][z_mapping-1] = 1.0f;
54 Transform smat = transform_scale(scale);
55 Transform rmat = transform_euler(rotation);
56 Transform tmat = transform_translate(translation);
58 return tmat*rmat*smat*mmat;
61 bool TextureMapping::skip()
63 if(translation != make_float3(0.0f, 0.0f, 0.0f))
65 if(rotation != make_float3(0.0f, 0.0f, 0.0f))
67 if(scale != make_float3(1.0f, 1.0f, 1.0f))
70 if(x_mapping != X || y_mapping != Y || z_mapping != Z)
76 void TextureMapping::compile(SVMCompiler& compiler, int offset_in, int offset_out)
78 if(offset_in == SVM_STACK_INVALID || offset_out == SVM_STACK_INVALID)
81 compiler.add_node(NODE_MAPPING, offset_in, offset_out);
83 Transform tfm = compute_transform();
84 compiler.add_node(tfm.x);
85 compiler.add_node(tfm.y);
86 compiler.add_node(tfm.z);
87 compiler.add_node(tfm.w);
92 static ShaderEnum color_space_init()
96 enm.insert("Linear", 0);
97 enm.insert("sRGB", 1);
102 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
104 ImageTextureNode::ImageTextureNode()
105 : TextureNode("image_texture")
107 image_manager = NULL;
110 color_space = ustring("sRGB");
112 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_UV);
113 add_output("Color", SHADER_SOCKET_COLOR);
114 add_output("Alpha", SHADER_SOCKET_FLOAT);
117 ImageTextureNode::~ImageTextureNode()
120 image_manager->remove_image(filename);
123 ShaderNode *ImageTextureNode::clone() const
125 ImageTextureNode *node = new ImageTextureNode(*this);
126 node->image_manager = NULL;
131 void ImageTextureNode::compile(SVMCompiler& compiler)
133 ShaderInput *vector_in = input("Vector");
134 ShaderOutput *color_out = output("Color");
135 ShaderOutput *alpha_out = output("Alpha");
137 image_manager = compiler.image_manager;
139 slot = image_manager->add_image(filename);
141 if(!color_out->links.empty())
142 compiler.stack_assign(color_out);
143 if(!alpha_out->links.empty())
144 compiler.stack_assign(alpha_out);
147 compiler.stack_assign(vector_in);
149 if(!tex_mapping.skip())
150 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
152 compiler.add_node(NODE_TEX_IMAGE,
154 compiler.encode_uchar4(
155 vector_in->stack_offset,
156 color_out->stack_offset,
157 alpha_out->stack_offset,
158 color_space_enum[color_space]));
161 /* image not found */
162 if(!color_out->links.empty()) {
163 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
164 compiler.add_node(NODE_VALUE_V, make_float3(0, 0, 0));
166 if(!alpha_out->links.empty())
167 compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), alpha_out->stack_offset);
171 void ImageTextureNode::compile(OSLCompiler& compiler)
173 compiler.parameter("filename", filename.c_str());
174 compiler.parameter("color_space", color_space.c_str());
175 compiler.add(this, "node_image_texture");
178 /* Environment Texture */
180 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
182 EnvironmentTextureNode::EnvironmentTextureNode()
183 : TextureNode("environment_texture")
185 image_manager = NULL;
188 color_space = ustring("sRGB");
190 add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
191 add_output("Color", SHADER_SOCKET_COLOR);
192 add_output("Alpha", SHADER_SOCKET_FLOAT);
195 EnvironmentTextureNode::~EnvironmentTextureNode()
198 image_manager->remove_image(filename);
201 ShaderNode *EnvironmentTextureNode::clone() const
203 EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
204 node->image_manager = NULL;
209 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
211 ShaderInput *vector_in = input("Vector");
212 ShaderOutput *color_out = output("Color");
213 ShaderOutput *alpha_out = output("Alpha");
215 image_manager = compiler.image_manager;
217 slot = image_manager->add_image(filename);
219 if(!color_out->links.empty())
220 compiler.stack_assign(color_out);
221 if(!alpha_out->links.empty())
222 compiler.stack_assign(alpha_out);
225 compiler.stack_assign(vector_in);
227 if(!tex_mapping.skip())
228 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
230 compiler.add_node(NODE_TEX_ENVIRONMENT,
232 compiler.encode_uchar4(
233 vector_in->stack_offset,
234 color_out->stack_offset,
235 alpha_out->stack_offset,
236 color_space_enum[color_space]));
239 /* image not found */
240 if(!color_out->links.empty()) {
241 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
242 compiler.add_node(NODE_VALUE_V, make_float3(0, 0, 0));
244 if(!alpha_out->links.empty())
245 compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), alpha_out->stack_offset);
249 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
251 compiler.parameter("filename", filename.c_str());
252 compiler.parameter("color_space", color_space.c_str());
253 compiler.add(this, "node_environment_texture");
258 static float2 sky_spherical_coordinates(float3 dir)
260 return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
263 static float sky_perez_function(float lam[6], float theta, float gamma)
265 return (1.f + lam[0]*expf(lam[1]/cosf(theta))) * (1.f + lam[2]*expf(lam[3]*gamma) + lam[4]*cosf(gamma)*cosf(gamma));
268 static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity)
270 float2 spherical = sky_spherical_coordinates(dir);
271 float theta = spherical.x;
272 float phi = spherical.y;
274 ksunsky->theta = theta;
278 float theta2 = theta*theta;
279 float theta3 = theta*theta*theta;
283 float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
284 ksunsky->zenith_Y = (4.0453f * T - 4.9710f) * tan(chi) - 0.2155f * T + 2.4192f;
285 ksunsky->zenith_Y *= 0.06f;
288 (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
289 (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
290 (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
293 (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
294 (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) * T +
295 (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
297 ksunsky->perez_Y[0] = (0.1787f * T - 1.4630f);
298 ksunsky->perez_Y[1] = (-0.3554f * T + 0.4275f);
299 ksunsky->perez_Y[2] = (-0.0227f * T + 5.3251f);
300 ksunsky->perez_Y[3] = (0.1206f * T - 2.5771f);
301 ksunsky->perez_Y[4] = (-0.0670f * T + 0.3703f);
303 ksunsky->perez_x[0] = (-0.0193f * T - 0.2592f);
304 ksunsky->perez_x[1] = (-0.0665f * T + 0.0008f);
305 ksunsky->perez_x[2] = (-0.0004f * T + 0.2125f);
306 ksunsky->perez_x[3] = (-0.0641f * T - 0.8989f);
307 ksunsky->perez_x[4] = (-0.0033f * T + 0.0452f);
309 ksunsky->perez_y[0] = (-0.0167f * T - 0.2608f);
310 ksunsky->perez_y[1] = (-0.0950f * T + 0.0092f);
311 ksunsky->perez_y[2] = (-0.0079f * T + 0.2102f);
312 ksunsky->perez_y[3] = (-0.0441f * T - 1.6537f);
313 ksunsky->perez_y[4] = (-0.0109f * T + 0.0529f);
315 ksunsky->zenith_Y /= sky_perez_function(ksunsky->perez_Y, 0, theta);
316 ksunsky->zenith_x /= sky_perez_function(ksunsky->perez_x, 0, theta);
317 ksunsky->zenith_y /= sky_perez_function(ksunsky->perez_y, 0, theta);
320 SkyTextureNode::SkyTextureNode()
321 : TextureNode("sky_texture")
323 sun_direction = make_float3(0.0f, 0.0f, 1.0f);
326 add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
327 add_output("Color", SHADER_SOCKET_COLOR);
330 void SkyTextureNode::compile(SVMCompiler& compiler)
332 ShaderInput *vector_in = input("Vector");
333 ShaderOutput *color_out = output("Color");
335 if(compiler.sunsky) {
336 sky_texture_precompute(compiler.sunsky, sun_direction, turbidity);
337 compiler.sunsky = NULL;
341 compiler.stack_assign(vector_in);
342 if(!tex_mapping.skip())
343 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
345 compiler.stack_assign(color_out);
346 compiler.add_node(NODE_TEX_SKY, vector_in->stack_offset, color_out->stack_offset);
349 void SkyTextureNode::compile(OSLCompiler& compiler)
351 compiler.parameter_vector("sun_direction", sun_direction);
352 compiler.parameter("turbidity", turbidity);
353 compiler.add(this, "node_sky_texture");
356 /* Gradient Texture */
358 static ShaderEnum gradient_type_init()
362 enm.insert("Linear", NODE_BLEND_LINEAR);
363 enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
364 enm.insert("Easing", NODE_BLEND_EASING);
365 enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
366 enm.insert("Radial", NODE_BLEND_RADIAL);
367 enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
368 enm.insert("Spherical", NODE_BLEND_SPHERICAL);
373 ShaderEnum GradientTextureNode::type_enum = gradient_type_init();
375 GradientTextureNode::GradientTextureNode()
376 : TextureNode("gradient_texture")
378 type = ustring("Linear");
380 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
381 add_output("Color", SHADER_SOCKET_COLOR);
382 add_output("Fac", SHADER_SOCKET_FLOAT);
385 void GradientTextureNode::compile(SVMCompiler& compiler)
387 ShaderInput *vector_in = input("Vector");
388 ShaderOutput *color_out = output("Color");
389 ShaderOutput *fac_out = output("Fac");
391 if(vector_in->link) compiler.stack_assign(vector_in);
393 if(!tex_mapping.skip())
394 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
396 if(!fac_out->links.empty())
397 compiler.stack_assign(fac_out);
398 if(!color_out->links.empty())
399 compiler.stack_assign(color_out);
401 compiler.add_node(NODE_TEX_GRADIENT,
402 compiler.encode_uchar4(type_enum[type], vector_in->stack_offset, fac_out->stack_offset, color_out->stack_offset));
405 void GradientTextureNode::compile(OSLCompiler& compiler)
407 compiler.parameter("Type", type);
408 compiler.add(this, "node_gradient_texture");
413 NoiseTextureNode::NoiseTextureNode()
414 : TextureNode("noise_texture")
416 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
417 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
418 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
419 add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
421 add_output("Color", SHADER_SOCKET_COLOR);
422 add_output("Fac", SHADER_SOCKET_FLOAT);
425 void NoiseTextureNode::compile(SVMCompiler& compiler)
427 ShaderInput *distortion_in = input("Distortion");
428 ShaderInput *detail_in = input("Detail");
429 ShaderInput *scale_in = input("Scale");
430 ShaderInput *vector_in = input("Vector");
431 ShaderOutput *color_out = output("Color");
432 ShaderOutput *fac_out = output("Fac");
434 if(vector_in->link) compiler.stack_assign(vector_in);
435 if(scale_in->link) compiler.stack_assign(scale_in);
436 if(detail_in->link) compiler.stack_assign(detail_in);
437 if(distortion_in->link) compiler.stack_assign(distortion_in);
439 if(!tex_mapping.skip())
440 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
442 if(!fac_out->links.empty())
443 compiler.stack_assign(fac_out);
444 if(!color_out->links.empty())
445 compiler.stack_assign(color_out);
447 compiler.add_node(NODE_TEX_NOISE,
448 compiler.encode_uchar4(vector_in->stack_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
449 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset));
451 __float_as_int(scale_in->value.x),
452 __float_as_int(detail_in->value.x),
453 __float_as_int(distortion_in->value.x));
456 void NoiseTextureNode::compile(OSLCompiler& compiler)
458 compiler.add(this, "node_noise_texture");
461 /* Voronoi Texture */
463 static ShaderEnum voronoi_coloring_init()
467 enm.insert("Intensity", NODE_VORONOI_INTENSITY);
468 enm.insert("Cells", NODE_VORONOI_CELLS);
473 ShaderEnum VoronoiTextureNode::coloring_enum = voronoi_coloring_init();
475 VoronoiTextureNode::VoronoiTextureNode()
476 : TextureNode("voronoi_texture")
478 coloring = ustring("Intensity");
480 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
481 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
483 add_output("Color", SHADER_SOCKET_COLOR);
484 add_output("Fac", SHADER_SOCKET_FLOAT);
487 void VoronoiTextureNode::compile(SVMCompiler& compiler)
489 ShaderInput *scale_in = input("Scale");
490 ShaderInput *vector_in = input("Vector");
491 ShaderOutput *color_out = output("Color");
492 ShaderOutput *fac_out = output("Fac");
494 if(vector_in->link) compiler.stack_assign(vector_in);
495 if(scale_in->link) compiler.stack_assign(scale_in);
497 if(!tex_mapping.skip())
498 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
500 compiler.stack_assign(color_out);
501 compiler.stack_assign(fac_out);
503 compiler.add_node(NODE_TEX_VORONOI,
504 coloring_enum[coloring],
505 compiler.encode_uchar4(scale_in->stack_offset, vector_in->stack_offset, fac_out->stack_offset, color_out->stack_offset),
506 __float_as_int(scale_in->value.x));
509 void VoronoiTextureNode::compile(OSLCompiler& compiler)
511 compiler.parameter("Coloring", coloring);
512 compiler.add(this, "node_voronoi_texture");
515 /* Musgrave Texture */
517 static ShaderEnum musgrave_type_init()
521 enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
522 enm.insert("fBM", NODE_MUSGRAVE_FBM);
523 enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
524 enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
525 enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
530 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
532 MusgraveTextureNode::MusgraveTextureNode()
533 : TextureNode("musgrave_texture")
535 type = ustring("fBM");
537 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
538 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
539 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
540 add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
541 add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
542 add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
543 add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
545 add_output("Fac", SHADER_SOCKET_FLOAT);
546 add_output("Color", SHADER_SOCKET_COLOR);
549 void MusgraveTextureNode::compile(SVMCompiler& compiler)
551 ShaderInput *vector_in = input("Vector");
552 ShaderInput *scale_in = input("Scale");
553 ShaderInput *dimension_in = input("Dimension");
554 ShaderInput *lacunarity_in = input("Lacunarity");
555 ShaderInput *detail_in = input("Detail");
556 ShaderInput *offset_in = input("Offset");
557 ShaderInput *gain_in = input("Gain");
558 ShaderOutput *fac_out = output("Fac");
559 ShaderOutput *color_out = output("Color");
561 if(vector_in->link) compiler.stack_assign(vector_in);
562 if(dimension_in->link) compiler.stack_assign(dimension_in);
563 if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
564 if(detail_in->link) compiler.stack_assign(detail_in);
565 if(offset_in->link) compiler.stack_assign(offset_in);
566 if(gain_in->link) compiler.stack_assign(gain_in);
567 if(scale_in->link) compiler.stack_assign(scale_in);
569 if(!tex_mapping.skip())
570 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
572 if(!fac_out->links.empty())
573 compiler.stack_assign(fac_out);
574 if(!color_out->links.empty())
575 compiler.stack_assign(color_out);
577 compiler.add_node(NODE_TEX_MUSGRAVE,
578 compiler.encode_uchar4(type_enum[type], vector_in->stack_offset, color_out->stack_offset, fac_out->stack_offset),
579 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, detail_in->stack_offset, offset_in->stack_offset),
580 compiler.encode_uchar4(gain_in->stack_offset, scale_in->stack_offset));
581 compiler.add_node(__float_as_int(dimension_in->value.x),
582 __float_as_int(lacunarity_in->value.x),
583 __float_as_int(detail_in->value.x),
584 __float_as_int(offset_in->value.x));
585 compiler.add_node(__float_as_int(gain_in->value.x),
586 __float_as_int(scale_in->value.x));
589 void MusgraveTextureNode::compile(OSLCompiler& compiler)
591 compiler.parameter("Type", type);
593 compiler.add(this, "node_musgrave_texture");
598 static ShaderEnum wave_type_init()
602 enm.insert("Bands", NODE_WAVE_BANDS);
603 enm.insert("Rings", NODE_WAVE_RINGS);
608 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
610 WaveTextureNode::WaveTextureNode()
611 : TextureNode("marble_texture")
613 type = ustring("Bands");
615 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
616 add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
617 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
618 add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
619 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
621 add_output("Color", SHADER_SOCKET_COLOR);
622 add_output("Fac", SHADER_SOCKET_FLOAT);
625 void WaveTextureNode::compile(SVMCompiler& compiler)
627 ShaderInput *scale_in = input("Scale");
628 ShaderInput *distortion_in = input("Distortion");
629 ShaderInput *dscale_in = input("Detail Scale");
630 ShaderInput *detail_in = input("Detail");
631 ShaderInput *vector_in = input("Vector");
632 ShaderOutput *fac_out = output("Fac");
633 ShaderOutput *color_out = output("Color");
635 if(scale_in->link) compiler.stack_assign(scale_in);
636 if(detail_in->link) compiler.stack_assign(detail_in);
637 if(distortion_in->link) compiler.stack_assign(distortion_in);
638 if(dscale_in->link) compiler.stack_assign(dscale_in);
639 if(vector_in->link) compiler.stack_assign(vector_in);
641 if(!tex_mapping.skip())
642 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
644 if(!fac_out->links.empty())
645 compiler.stack_assign(fac_out);
646 if(!color_out->links.empty())
647 compiler.stack_assign(color_out);
649 compiler.add_node(NODE_TEX_WAVE,
650 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
651 compiler.encode_uchar4(vector_in->stack_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset));
654 __float_as_int(scale_in->value.x),
655 __float_as_int(detail_in->value.x),
656 __float_as_int(distortion_in->value.x),
657 __float_as_int(dscale_in->value.x));
660 void WaveTextureNode::compile(OSLCompiler& compiler)
662 compiler.parameter("Type", type);
664 compiler.add(this, "node_marble_texture");
669 MagicTextureNode::MagicTextureNode()
670 : TextureNode("magic_texture")
674 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
675 add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
676 add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
678 add_output("Color", SHADER_SOCKET_COLOR);
679 add_output("Fac", SHADER_SOCKET_FLOAT);
682 void MagicTextureNode::compile(SVMCompiler& compiler)
684 ShaderInput *vector_in = input("Vector");
685 ShaderInput *scale_in = input("Scale");
686 ShaderInput *distortion_in = input("Distortion");
687 ShaderOutput *color_out = output("Color");
688 ShaderOutput *fac_out = output("Fac");
690 if(vector_in->link) compiler.stack_assign(vector_in);
691 if(distortion_in->link) compiler.stack_assign(distortion_in);
692 if(scale_in->link) compiler.stack_assign(scale_in);
694 if(!tex_mapping.skip())
695 tex_mapping.compile(compiler, vector_in->stack_offset, vector_in->stack_offset);
697 if(!fac_out->links.empty())
698 compiler.stack_assign(fac_out);
699 if(!color_out->links.empty())
700 compiler.stack_assign(color_out);
702 compiler.add_node(NODE_TEX_MAGIC,
703 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
704 compiler.encode_uchar4(vector_in->stack_offset, scale_in->stack_offset, distortion_in->stack_offset));
706 __float_as_int(scale_in->value.x),
707 __float_as_int(distortion_in->value.x));
710 void MagicTextureNode::compile(OSLCompiler& compiler)
712 compiler.parameter("Depth", depth);
713 compiler.add(this, "node_magic_texture");
718 MappingNode::MappingNode()
719 : ShaderNode("mapping")
721 add_input("Vector", SHADER_SOCKET_POINT);
722 add_output("Vector", SHADER_SOCKET_POINT);
725 void MappingNode::compile(SVMCompiler& compiler)
727 ShaderInput *vector_in = input("Vector");
728 ShaderOutput *vector_out = output("Vector");
730 compiler.stack_assign(vector_in);
731 compiler.stack_assign(vector_out);
733 tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
736 void MappingNode::compile(OSLCompiler& compiler)
738 Transform tfm = transform_transpose(tex_mapping.compute_transform());
739 compiler.parameter("Matrix", tfm);
741 compiler.add(this, "node_mapping");
746 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_)
747 : ShaderNode("convert")
754 if(from == SHADER_SOCKET_FLOAT)
755 add_input("Val", SHADER_SOCKET_FLOAT);
756 else if(from == SHADER_SOCKET_COLOR)
757 add_input("Color", SHADER_SOCKET_COLOR);
758 else if(from == SHADER_SOCKET_VECTOR)
759 add_input("Vector", SHADER_SOCKET_VECTOR);
760 else if(from == SHADER_SOCKET_POINT)
761 add_input("Point", SHADER_SOCKET_POINT);
762 else if(from == SHADER_SOCKET_NORMAL)
763 add_input("Normal", SHADER_SOCKET_NORMAL);
767 if(to == SHADER_SOCKET_FLOAT)
768 add_output("Val", SHADER_SOCKET_FLOAT);
769 else if(to == SHADER_SOCKET_COLOR)
770 add_output("Color", SHADER_SOCKET_COLOR);
771 else if(to == SHADER_SOCKET_VECTOR)
772 add_output("Vector", SHADER_SOCKET_VECTOR);
773 else if(to == SHADER_SOCKET_POINT)
774 add_output("Point", SHADER_SOCKET_POINT);
775 else if(to == SHADER_SOCKET_NORMAL)
776 add_output("Normal", SHADER_SOCKET_NORMAL);
781 void ConvertNode::compile(SVMCompiler& compiler)
783 ShaderInput *in = inputs[0];
784 ShaderOutput *out = outputs[0];
786 if(to == SHADER_SOCKET_FLOAT) {
787 compiler.stack_assign(in);
788 compiler.stack_assign(out);
790 if(from == SHADER_SOCKET_COLOR)
792 compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
794 /* vector/point/normal to float */
795 compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
797 else if(from == SHADER_SOCKET_FLOAT) {
798 compiler.stack_assign(in);
799 compiler.stack_assign(out);
801 /* float to float3 */
802 compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
805 /* float3 to float3 */
808 compiler.stack_link(in, out);
811 /* set 0,0,0 value */
812 compiler.stack_assign(in);
813 compiler.stack_assign(out);
815 compiler.add_node(NODE_VALUE_V, in->stack_offset);
816 compiler.add_node(NODE_VALUE_V, in->value);
821 void ConvertNode::compile(OSLCompiler& compiler)
823 if(from == SHADER_SOCKET_FLOAT)
824 compiler.add(this, "node_convert_from_float");
825 else if(from == SHADER_SOCKET_COLOR)
826 compiler.add(this, "node_convert_from_color");
827 else if(from == SHADER_SOCKET_VECTOR)
828 compiler.add(this, "node_convert_from_vector");
829 else if(from == SHADER_SOCKET_POINT)
830 compiler.add(this, "node_convert_from_point");
831 else if(from == SHADER_SOCKET_NORMAL)
832 compiler.add(this, "node_convert_from_normal");
842 closure = ccl::CLOSURE_BSDF_DIFFUSE_ID;
844 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
845 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
847 add_output("BSDF", SHADER_SOCKET_CLOSURE);
850 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
852 ShaderInput *color_in = input("Color");
855 compiler.stack_assign(color_in);
856 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
859 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
862 compiler.stack_assign(param1);
864 compiler.stack_assign(param2);
866 compiler.add_node(NODE_CLOSURE_BSDF,
867 compiler.encode_uchar4(closure,
868 (param1)? param1->stack_offset: SVM_STACK_INVALID,
869 (param2)? param2->stack_offset: SVM_STACK_INVALID,
870 compiler.closure_mix_weight_offset()),
871 __float_as_int((param1)? param1->value.x: 0.0f),
872 __float_as_int((param2)? param2->value.x: 0.0f));
875 void BsdfNode::compile(SVMCompiler& compiler)
877 compile(compiler, NULL, NULL);
880 void BsdfNode::compile(OSLCompiler& compiler)
885 /* Ward BSDF Closure */
887 WardBsdfNode::WardBsdfNode()
889 closure = CLOSURE_BSDF_WARD_ID;
891 add_input("Roughness U", SHADER_SOCKET_FLOAT, 0.2f);
892 add_input("Roughness V", SHADER_SOCKET_FLOAT, 0.2f);
895 void WardBsdfNode::compile(SVMCompiler& compiler)
897 BsdfNode::compile(compiler, input("Roughness U"), input("Roughness V"));
900 void WardBsdfNode::compile(OSLCompiler& compiler)
902 compiler.add(this, "node_ward_bsdf");
905 /* Glossy BSDF Closure */
907 static ShaderEnum glossy_distribution_init()
911 enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
912 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
913 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
918 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
920 GlossyBsdfNode::GlossyBsdfNode()
922 distribution = ustring("Beckmann");
924 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
927 void GlossyBsdfNode::compile(SVMCompiler& compiler)
929 closure = (ClosureType)distribution_enum[distribution];
931 if(closure == CLOSURE_BSDF_REFLECTION_ID)
932 BsdfNode::compile(compiler, NULL, NULL);
934 BsdfNode::compile(compiler, input("Roughness"), NULL);
937 void GlossyBsdfNode::compile(OSLCompiler& compiler)
939 compiler.parameter("distribution", distribution);
940 compiler.add(this, "node_glossy_bsdf");
943 /* Glass BSDF Closure */
945 static ShaderEnum glass_distribution_init()
949 enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
950 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
951 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
956 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
958 GlassBsdfNode::GlassBsdfNode()
960 distribution = ustring("Sharp");
962 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
963 add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
966 void GlassBsdfNode::compile(SVMCompiler& compiler)
968 closure = (ClosureType)distribution_enum[distribution];
970 if(closure == CLOSURE_BSDF_REFRACTION_ID)
971 BsdfNode::compile(compiler, NULL, input("IOR"));
973 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
976 void GlassBsdfNode::compile(OSLCompiler& compiler)
978 compiler.parameter("distribution", distribution);
979 compiler.add(this, "node_glass_bsdf");
982 /* Velvet BSDF Closure */
984 VelvetBsdfNode::VelvetBsdfNode()
986 closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
988 add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
991 void VelvetBsdfNode::compile(SVMCompiler& compiler)
993 BsdfNode::compile(compiler, input("Sigma"), NULL);
996 void VelvetBsdfNode::compile(OSLCompiler& compiler)
998 compiler.add(this, "node_velvet_bsdf");
1001 /* Diffuse BSDF Closure */
1003 DiffuseBsdfNode::DiffuseBsdfNode()
1005 closure = CLOSURE_BSDF_DIFFUSE_ID;
1006 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1009 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1011 BsdfNode::compile(compiler, input("Roughness"), NULL);
1014 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1016 compiler.add(this, "node_diffuse_bsdf");
1019 /* Translucent BSDF Closure */
1021 TranslucentBsdfNode::TranslucentBsdfNode()
1023 closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1026 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1028 BsdfNode::compile(compiler, NULL, NULL);
1031 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1033 compiler.add(this, "node_translucent_bsdf");
1036 /* Transparent BSDF Closure */
1038 TransparentBsdfNode::TransparentBsdfNode()
1040 name = "transparent";
1041 closure = CLOSURE_BSDF_TRANSPARENT_ID;
1044 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1046 BsdfNode::compile(compiler, NULL, NULL);
1049 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1051 compiler.add(this, "node_transparent_bsdf");
1054 /* Emissive Closure */
1056 EmissionNode::EmissionNode()
1057 : ShaderNode("emission")
1059 total_power = false;
1061 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1062 add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1063 add_output("Emission", SHADER_SOCKET_CLOSURE);
1066 void EmissionNode::compile(SVMCompiler& compiler)
1068 ShaderInput *color_in = input("Color");
1069 ShaderInput *strength_in = input("Strength");
1071 if(color_in->link || strength_in->link) {
1072 compiler.stack_assign(color_in);
1073 compiler.stack_assign(strength_in);
1074 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1076 else if(total_power)
1077 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1079 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1081 compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1084 void EmissionNode::compile(OSLCompiler& compiler)
1086 compiler.parameter("TotalPower", (total_power)? 1: 0);
1087 compiler.add(this, "node_emission");
1090 /* Background Closure */
1092 BackgroundNode::BackgroundNode()
1093 : ShaderNode("background")
1095 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1096 add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1097 add_output("Background", SHADER_SOCKET_CLOSURE);
1100 void BackgroundNode::compile(SVMCompiler& compiler)
1102 ShaderInput *color_in = input("Color");
1103 ShaderInput *strength_in = input("Strength");
1105 if(color_in->link || strength_in->link) {
1106 compiler.stack_assign(color_in);
1107 compiler.stack_assign(strength_in);
1108 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1111 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1113 compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1116 void BackgroundNode::compile(OSLCompiler& compiler)
1118 compiler.add(this, "node_background");
1121 /* Holdout Closure */
1123 HoldoutNode::HoldoutNode()
1124 : ShaderNode("holdout")
1126 add_output("Holdout", SHADER_SOCKET_CLOSURE);
1129 void HoldoutNode::compile(SVMCompiler& compiler)
1131 compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1134 void HoldoutNode::compile(OSLCompiler& compiler)
1136 compiler.add(this, "node_holdout");
1139 /* Volume Closure */
1141 VolumeNode::VolumeNode()
1142 : ShaderNode("volume")
1144 closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1146 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1147 add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1149 add_output("Volume", SHADER_SOCKET_CLOSURE);
1152 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1154 ShaderInput *color_in = input("Color");
1156 if(color_in->link) {
1157 compiler.stack_assign(color_in);
1158 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1161 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1164 compiler.stack_assign(param1);
1166 compiler.stack_assign(param2);
1168 compiler.add_node(NODE_CLOSURE_VOLUME,
1169 compiler.encode_uchar4(closure,
1170 (param1)? param1->stack_offset: SVM_STACK_INVALID,
1171 (param2)? param2->stack_offset: SVM_STACK_INVALID,
1172 compiler.closure_mix_weight_offset()),
1173 __float_as_int((param1)? param1->value.x: 0.0f),
1174 __float_as_int((param2)? param2->value.x: 0.0f));
1177 void VolumeNode::compile(SVMCompiler& compiler)
1179 compile(compiler, NULL, NULL);
1182 void VolumeNode::compile(OSLCompiler& compiler)
1187 /* Transparent Volume Closure */
1189 TransparentVolumeNode::TransparentVolumeNode()
1191 closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1194 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1196 VolumeNode::compile(compiler, input("Density"), NULL);
1199 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1201 compiler.add(this, "node_isotropic_volume");
1204 /* Isotropic Volume Closure */
1206 IsotropicVolumeNode::IsotropicVolumeNode()
1208 closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1211 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1213 VolumeNode::compile(compiler, input("Density"), NULL);
1216 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1218 compiler.add(this, "node_isotropic_volume");
1223 GeometryNode::GeometryNode()
1224 : ShaderNode("geometry")
1226 add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1227 add_output("Position", SHADER_SOCKET_POINT);
1228 add_output("Normal", SHADER_SOCKET_NORMAL);
1229 add_output("Tangent", SHADER_SOCKET_NORMAL);
1230 add_output("True Normal", SHADER_SOCKET_NORMAL);
1231 add_output("Incoming", SHADER_SOCKET_VECTOR);
1232 add_output("Parametric", SHADER_SOCKET_POINT);
1233 add_output("Backfacing", SHADER_SOCKET_FLOAT);
1236 void GeometryNode::compile(SVMCompiler& compiler)
1239 NodeType geom_node = NODE_GEOMETRY;
1241 if(bump == SHADER_BUMP_DX)
1242 geom_node = NODE_GEOMETRY_BUMP_DX;
1243 else if(bump == SHADER_BUMP_DY)
1244 geom_node = NODE_GEOMETRY_BUMP_DY;
1246 out = output("Position");
1247 if(!out->links.empty()) {
1248 compiler.stack_assign(out);
1249 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1252 out = output("Normal");
1253 if(!out->links.empty()) {
1254 compiler.stack_assign(out);
1255 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1258 out = output("Tangent");
1259 if(!out->links.empty()) {
1260 compiler.stack_assign(out);
1261 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1264 out = output("True Normal");
1265 if(!out->links.empty()) {
1266 compiler.stack_assign(out);
1267 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1270 out = output("Incoming");
1271 if(!out->links.empty()) {
1272 compiler.stack_assign(out);
1273 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1276 out = output("Parametric");
1277 if(!out->links.empty()) {
1278 compiler.stack_assign(out);
1279 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1282 out = output("Backfacing");
1283 if(!out->links.empty()) {
1284 compiler.stack_assign(out);
1285 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1289 void GeometryNode::compile(OSLCompiler& compiler)
1291 if(bump == SHADER_BUMP_DX)
1292 compiler.parameter("bump_offset", "dx");
1293 else if(bump == SHADER_BUMP_DY)
1294 compiler.parameter("bump_offset", "dy");
1296 compiler.parameter("bump_offset", "center");
1298 compiler.add(this, "node_geometry");
1301 /* TextureCoordinate */
1303 TextureCoordinateNode::TextureCoordinateNode()
1304 : ShaderNode("texture_coordinate")
1306 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1307 add_output("Generated", SHADER_SOCKET_POINT);
1308 add_output("UV", SHADER_SOCKET_POINT);
1309 add_output("Object", SHADER_SOCKET_POINT);
1310 add_output("Camera", SHADER_SOCKET_POINT);
1311 add_output("Window", SHADER_SOCKET_POINT);
1312 add_output("Reflection", SHADER_SOCKET_NORMAL);
1315 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1317 if(!output("Generated")->links.empty())
1318 attributes->add(Attribute::STD_GENERATED);
1319 if(!output("UV")->links.empty())
1320 attributes->add(Attribute::STD_UV);
1322 ShaderNode::attributes(attributes);
1325 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1328 NodeType texco_node = NODE_TEX_COORD;
1329 NodeType attr_node = NODE_ATTR;
1330 NodeType geom_node = NODE_GEOMETRY;
1332 if(bump == SHADER_BUMP_DX) {
1333 texco_node = NODE_TEX_COORD_BUMP_DX;
1334 attr_node = NODE_ATTR_BUMP_DX;
1335 geom_node = NODE_GEOMETRY_BUMP_DX;
1337 else if(bump == SHADER_BUMP_DY) {
1338 texco_node = NODE_TEX_COORD_BUMP_DY;
1339 attr_node = NODE_ATTR_BUMP_DY;
1340 geom_node = NODE_GEOMETRY_BUMP_DY;
1343 out = output("Generated");
1344 if(!out->links.empty()) {
1345 if(compiler.background) {
1346 compiler.stack_assign(out);
1347 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1350 int attr = compiler.attribute(Attribute::STD_GENERATED);
1351 compiler.stack_assign(out);
1352 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1357 if(!out->links.empty()) {
1358 int attr = compiler.attribute(Attribute::STD_UV);
1359 compiler.stack_assign(out);
1360 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1363 out = output("Object");
1364 if(!out->links.empty()) {
1365 compiler.stack_assign(out);
1366 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
1369 out = output("Camera");
1370 if(!out->links.empty()) {
1371 compiler.stack_assign(out);
1372 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
1375 out = output("Window");
1376 if(!out->links.empty()) {
1377 compiler.stack_assign(out);
1378 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
1381 out = output("Reflection");
1382 if(!out->links.empty()) {
1383 if(compiler.background) {
1384 compiler.stack_assign(out);
1385 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1388 compiler.stack_assign(out);
1389 compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
1394 void TextureCoordinateNode::compile(OSLCompiler& compiler)
1396 if(bump == SHADER_BUMP_DX)
1397 compiler.parameter("bump_offset", "dx");
1398 else if(bump == SHADER_BUMP_DY)
1399 compiler.parameter("bump_offset", "dy");
1401 compiler.parameter("bump_offset", "center");
1403 if(compiler.background)
1404 compiler.parameter("is_background", true);
1406 compiler.add(this, "node_texture_coordinate");
1411 LightPathNode::LightPathNode()
1412 : ShaderNode("light_path")
1414 add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
1415 add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
1416 add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
1417 add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
1418 add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
1419 add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
1420 add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
1423 void LightPathNode::compile(SVMCompiler& compiler)
1427 out = output("Is Camera Ray");
1428 if(!out->links.empty()) {
1429 compiler.stack_assign(out);
1430 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
1433 out = output("Is Shadow Ray");
1434 if(!out->links.empty()) {
1435 compiler.stack_assign(out);
1436 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
1439 out = output("Is Diffuse Ray");
1440 if(!out->links.empty()) {
1441 compiler.stack_assign(out);
1442 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
1445 out = output("Is Glossy Ray");
1446 if(!out->links.empty()) {
1447 compiler.stack_assign(out);
1448 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
1451 out = output("Is Singular Ray");
1452 if(!out->links.empty()) {
1453 compiler.stack_assign(out);
1454 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
1457 out = output("Is Reflection Ray");
1458 if(!out->links.empty()) {
1459 compiler.stack_assign(out);
1460 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
1464 out = output("Is Transmission Ray");
1465 if(!out->links.empty()) {
1466 compiler.stack_assign(out);
1467 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
1471 void LightPathNode::compile(OSLCompiler& compiler)
1473 compiler.add(this, "node_light_path");
1478 ValueNode::ValueNode()
1479 : ShaderNode("value")
1483 add_output("Value", SHADER_SOCKET_FLOAT);
1486 void ValueNode::compile(SVMCompiler& compiler)
1488 ShaderOutput *val_out = output("Value");
1490 compiler.stack_assign(val_out);
1491 compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
1494 void ValueNode::compile(OSLCompiler& compiler)
1496 compiler.parameter("value_value", value);
1497 compiler.add(this, "node_value");
1502 ColorNode::ColorNode()
1503 : ShaderNode("color")
1505 value = make_float3(0.0f, 0.0f, 0.0f);
1507 add_output("Color", SHADER_SOCKET_COLOR);
1510 void ColorNode::compile(SVMCompiler& compiler)
1512 ShaderOutput *color_out = output("Color");
1514 if(color_out && !color_out->links.empty()) {
1515 compiler.stack_assign(color_out);
1516 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
1517 compiler.add_node(NODE_VALUE_V, value);
1521 void ColorNode::compile(OSLCompiler& compiler)
1523 compiler.parameter_color("color_value", value);
1525 compiler.add(this, "node_value");
1530 AddClosureNode::AddClosureNode()
1531 : ShaderNode("add_closure")
1533 add_input("Closure1", SHADER_SOCKET_CLOSURE);
1534 add_input("Closure2", SHADER_SOCKET_CLOSURE);
1535 add_output("Closure", SHADER_SOCKET_CLOSURE);
1538 void AddClosureNode::compile(SVMCompiler& compiler)
1540 /* handled in the SVM compiler */
1543 void AddClosureNode::compile(OSLCompiler& compiler)
1545 compiler.add(this, "node_add_closure");
1550 MixClosureNode::MixClosureNode()
1551 : ShaderNode("mix_closure")
1553 add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1554 add_input("Closure1", SHADER_SOCKET_CLOSURE);
1555 add_input("Closure2", SHADER_SOCKET_CLOSURE);
1556 add_output("Closure", SHADER_SOCKET_CLOSURE);
1559 void MixClosureNode::compile(SVMCompiler& compiler)
1561 /* handled in the SVM compiler */
1564 void MixClosureNode::compile(OSLCompiler& compiler)
1566 compiler.add(this, "node_mix_closure");
1571 InvertNode::InvertNode()
1572 : ShaderNode("invert")
1574 add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
1575 add_input("Color", SHADER_SOCKET_COLOR);
1576 add_output("Color", SHADER_SOCKET_COLOR);
1579 void InvertNode::compile(SVMCompiler& compiler)
1581 ShaderInput *fac_in = input("Fac");
1582 ShaderInput *color_in = input("Color");
1583 ShaderOutput *color_out = output("Color");
1585 compiler.stack_assign(fac_in);
1586 compiler.stack_assign(color_in);
1587 compiler.stack_assign(color_out);
1589 compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
1592 void InvertNode::compile(OSLCompiler& compiler)
1594 compiler.add(this, "node_invert");
1602 type = ustring("Mix");
1604 add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1605 add_input("Color1", SHADER_SOCKET_COLOR);
1606 add_input("Color2", SHADER_SOCKET_COLOR);
1607 add_output("Color", SHADER_SOCKET_COLOR);
1610 static ShaderEnum mix_type_init()
1614 enm.insert("Mix", NODE_MIX_BLEND);
1615 enm.insert("Add", NODE_MIX_ADD);
1616 enm.insert("Multiply", NODE_MIX_MUL);
1617 enm.insert("Screen", NODE_MIX_SCREEN);
1618 enm.insert("Overlay", NODE_MIX_OVERLAY);
1619 enm.insert("Subtract", NODE_MIX_SUB);
1620 enm.insert("Divide", NODE_MIX_DIV);
1621 enm.insert("Difference", NODE_MIX_DIFF);
1622 enm.insert("Darken", NODE_MIX_DARK);
1623 enm.insert("Lighten", NODE_MIX_LIGHT);
1624 enm.insert("Dodge", NODE_MIX_DODGE);
1625 enm.insert("Burn", NODE_MIX_BURN);
1626 enm.insert("Hue", NODE_MIX_HUE);
1627 enm.insert("Saturation", NODE_MIX_SAT);
1628 enm.insert("Value", NODE_MIX_VAL );
1629 enm.insert("Color", NODE_MIX_COLOR);
1630 enm.insert("Soft Light", NODE_MIX_SOFT);
1631 enm.insert("Linear Light", NODE_MIX_LINEAR);
1636 ShaderEnum MixNode::type_enum = mix_type_init();
1638 void MixNode::compile(SVMCompiler& compiler)
1640 ShaderInput *fac_in = input("Fac");
1641 ShaderInput *color1_in = input("Color1");
1642 ShaderInput *color2_in = input("Color2");
1643 ShaderOutput *color_out = output("Color");
1645 compiler.stack_assign(fac_in);
1646 compiler.stack_assign(color1_in);
1647 compiler.stack_assign(color2_in);
1648 compiler.stack_assign(color_out);
1650 compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
1651 compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
1654 void MixNode::compile(OSLCompiler& compiler)
1656 compiler.parameter("type", type);
1657 compiler.add(this, "node_mix");
1661 CombineRGBNode::CombineRGBNode()
1662 : ShaderNode("combine_rgb")
1664 add_input("R", SHADER_SOCKET_FLOAT);
1665 add_input("G", SHADER_SOCKET_FLOAT);
1666 add_input("B", SHADER_SOCKET_FLOAT);
1667 add_output("Image", SHADER_SOCKET_COLOR);
1670 void CombineRGBNode::compile(SVMCompiler& compiler)
1672 ShaderInput *red_in = input("R");
1673 ShaderInput *green_in = input("G");
1674 ShaderInput *blue_in = input("B");
1675 ShaderOutput *color_out = output("Image");
1677 compiler.stack_assign(color_out);
1679 compiler.stack_assign(red_in);
1680 compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
1682 compiler.stack_assign(green_in);
1683 compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
1685 compiler.stack_assign(blue_in);
1686 compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
1689 void CombineRGBNode::compile(OSLCompiler& compiler)
1691 compiler.add(this, "node_combine_rgb");
1695 SeparateRGBNode::SeparateRGBNode()
1696 : ShaderNode("separate_rgb")
1698 add_input("Image", SHADER_SOCKET_COLOR);
1699 add_output("R", SHADER_SOCKET_FLOAT);
1700 add_output("G", SHADER_SOCKET_FLOAT);
1701 add_output("B", SHADER_SOCKET_FLOAT);
1704 void SeparateRGBNode::compile(SVMCompiler& compiler)
1706 ShaderInput *color_in = input("Image");
1707 ShaderOutput *red_out = output("R");
1708 ShaderOutput *green_out = output("G");
1709 ShaderOutput *blue_out = output("B");
1711 compiler.stack_assign(color_in);
1713 compiler.stack_assign(red_out);
1714 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
1716 compiler.stack_assign(green_out);
1717 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
1719 compiler.stack_assign(blue_out);
1720 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
1723 void SeparateRGBNode::compile(OSLCompiler& compiler)
1725 compiler.add(this, "node_separate_rgb");
1732 add_input("Hue", SHADER_SOCKET_FLOAT);
1733 add_input("Saturation", SHADER_SOCKET_FLOAT);
1734 add_input("Value", SHADER_SOCKET_FLOAT);
1735 add_input("Fac", SHADER_SOCKET_FLOAT);
1736 add_input("Color", SHADER_SOCKET_COLOR);
1737 add_output("Color", SHADER_SOCKET_COLOR);
1740 void HSVNode::compile(SVMCompiler& compiler)
1742 ShaderInput *hue_in = input("Hue");
1743 ShaderInput *saturation_in = input("Saturation");
1744 ShaderInput *value_in = input("Value");
1745 ShaderInput *fac_in = input("Fac");
1746 ShaderInput *color_in = input("Color");
1747 ShaderOutput *color_out = output("Color");
1749 compiler.stack_assign(hue_in);
1750 compiler.stack_assign(saturation_in);
1751 compiler.stack_assign(value_in);
1752 compiler.stack_assign(fac_in);
1753 compiler.stack_assign(color_in);
1754 compiler.stack_assign(color_out);
1756 compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
1757 compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
1760 void HSVNode::compile(OSLCompiler& compiler)
1762 compiler.add(this, "node_hsv");
1767 AttributeNode::AttributeNode()
1768 : ShaderNode("attribute")
1772 add_output("Color", SHADER_SOCKET_COLOR);
1773 add_output("Vector", SHADER_SOCKET_VECTOR);
1774 add_output("Fac", SHADER_SOCKET_FLOAT);
1777 void AttributeNode::attributes(AttributeRequestSet *attributes)
1779 ShaderOutput *color_out = output("Color");
1780 ShaderOutput *vector_out = output("Vector");
1781 ShaderOutput *fac_out = output("Fac");
1783 if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
1784 attributes->add(attribute);
1786 ShaderNode::attributes(attributes);
1789 void AttributeNode::compile(SVMCompiler& compiler)
1791 ShaderOutput *color_out = output("Color");
1792 ShaderOutput *vector_out = output("Vector");
1793 ShaderOutput *fac_out = output("Fac");
1794 NodeType attr_node = NODE_ATTR;
1796 if(bump == SHADER_BUMP_DX)
1797 attr_node = NODE_ATTR_BUMP_DX;
1798 else if(bump == SHADER_BUMP_DY)
1799 attr_node = NODE_ATTR_BUMP_DY;
1801 if(!color_out->links.empty() || !vector_out->links.empty()) {
1802 int attr = compiler.attribute(attribute);
1804 if(!color_out->links.empty()) {
1805 compiler.stack_assign(color_out);
1806 compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
1808 if(!vector_out->links.empty()) {
1809 compiler.stack_assign(vector_out);
1810 compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
1814 if(!fac_out->links.empty()) {
1815 int attr = compiler.attribute(attribute);
1817 compiler.stack_assign(fac_out);
1818 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
1822 void AttributeNode::compile(OSLCompiler& compiler)
1824 if(bump == SHADER_BUMP_DX)
1825 compiler.parameter("bump_offset", "dx");
1826 else if(bump == SHADER_BUMP_DY)
1827 compiler.parameter("bump_offset", "dy");
1829 compiler.parameter("bump_offset", "center");
1831 compiler.parameter("name", attribute.c_str());
1832 compiler.add(this, "node_attribute");
1837 CameraNode::CameraNode()
1838 : ShaderNode("camera")
1840 add_output("View Vector", SHADER_SOCKET_VECTOR);
1841 add_output("View Z Depth", SHADER_SOCKET_FLOAT);
1842 add_output("View Distance", SHADER_SOCKET_FLOAT);
1845 void CameraNode::compile(SVMCompiler& compiler)
1847 ShaderOutput *vector_out = output("View Vector");
1848 ShaderOutput *z_depth_out = output("View Z Depth");
1849 ShaderOutput *distance_out = output("View Distance");
1851 compiler.stack_assign(vector_out);
1852 compiler.stack_assign(z_depth_out);
1853 compiler.stack_assign(distance_out);
1854 compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
1857 void CameraNode::compile(OSLCompiler& compiler)
1859 compiler.add(this, "node_camera");
1864 FresnelNode::FresnelNode()
1865 : ShaderNode("Fresnel")
1867 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1868 add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
1869 add_output("Fac", SHADER_SOCKET_FLOAT);
1872 void FresnelNode::compile(SVMCompiler& compiler)
1874 ShaderInput *ior_in = input("IOR");
1875 ShaderOutput *fac_out = output("Fac");
1877 compiler.stack_assign(ior_in);
1878 compiler.stack_assign(fac_out);
1879 compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
1882 void FresnelNode::compile(OSLCompiler& compiler)
1884 compiler.add(this, "node_fresnel");
1889 LayerWeightNode::LayerWeightNode()
1890 : ShaderNode("LayerWeight")
1892 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1893 add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
1895 add_output("Fresnel", SHADER_SOCKET_FLOAT);
1896 add_output("Facing", SHADER_SOCKET_FLOAT);
1899 void LayerWeightNode::compile(SVMCompiler& compiler)
1901 ShaderInput *blend_in = input("Blend");
1904 compiler.stack_assign(blend_in);
1906 ShaderOutput *fresnel_out = output("Fresnel");
1907 if(!fresnel_out->links.empty()) {
1908 compiler.stack_assign(fresnel_out);
1909 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
1910 compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
1913 ShaderOutput *facing_out = output("Facing");
1914 if(!facing_out->links.empty()) {
1915 compiler.stack_assign(facing_out);
1916 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
1917 compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
1921 void LayerWeightNode::compile(OSLCompiler& compiler)
1923 compiler.add(this, "node_layer_height");
1928 OutputNode::OutputNode()
1929 : ShaderNode("output")
1931 add_input("Surface", SHADER_SOCKET_CLOSURE);
1932 add_input("Volume", SHADER_SOCKET_CLOSURE);
1933 add_input("Displacement", SHADER_SOCKET_FLOAT);
1936 void OutputNode::compile(SVMCompiler& compiler)
1938 if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
1939 ShaderInput *displacement_in = input("Displacement");
1941 if(displacement_in->link) {
1942 compiler.stack_assign(displacement_in);
1943 compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
1948 void OutputNode::compile(OSLCompiler& compiler)
1950 if(compiler.output_type() == SHADER_TYPE_SURFACE)
1951 compiler.add(this, "node_output_surface");
1952 else if(compiler.output_type() == SHADER_TYPE_VOLUME)
1953 compiler.add(this, "node_output_volume");
1954 else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
1955 compiler.add(this, "node_output_displacement");
1960 MathNode::MathNode()
1961 : ShaderNode("math")
1963 type = ustring("Add");
1965 add_input("Value1", SHADER_SOCKET_FLOAT);
1966 add_input("Value2", SHADER_SOCKET_FLOAT);
1967 add_output("Value", SHADER_SOCKET_FLOAT);
1970 static ShaderEnum math_type_init()
1974 enm.insert("Add", NODE_MATH_ADD);
1975 enm.insert("Subtract", NODE_MATH_SUBTRACT);
1976 enm.insert("Multiply", NODE_MATH_MULTIPLY);
1977 enm.insert("Divide", NODE_MATH_DIVIDE);
1978 enm.insert("Sine", NODE_MATH_SINE);
1979 enm.insert("Cosine", NODE_MATH_COSINE);
1980 enm.insert("Tangent", NODE_MATH_TANGENT);
1981 enm.insert("Arcsine", NODE_MATH_ARCSINE);
1982 enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
1983 enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
1984 enm.insert("Power", NODE_MATH_POWER);
1985 enm.insert("Logarithm", NODE_MATH_LOGARITHM);
1986 enm.insert("Minimum", NODE_MATH_MINIMUM);
1987 enm.insert("Maximum", NODE_MATH_MAXIMUM);
1988 enm.insert("Round", NODE_MATH_ROUND);
1989 enm.insert("Less Than", NODE_MATH_LESS_THAN);
1990 enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
1995 ShaderEnum MathNode::type_enum = math_type_init();
1997 void MathNode::compile(SVMCompiler& compiler)
1999 ShaderInput *value1_in = input("Value1");
2000 ShaderInput *value2_in = input("Value2");
2001 ShaderOutput *value_out = output("Value");
2003 compiler.stack_assign(value1_in);
2004 compiler.stack_assign(value2_in);
2005 compiler.stack_assign(value_out);
2007 compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
2008 compiler.add_node(NODE_MATH, value_out->stack_offset);
2011 void MathNode::compile(OSLCompiler& compiler)
2013 compiler.parameter("type", type);
2014 compiler.add(this, "node_math");
2019 VectorMathNode::VectorMathNode()
2020 : ShaderNode("vector_math")
2022 type = ustring("Add");
2024 add_input("Vector1", SHADER_SOCKET_VECTOR);
2025 add_input("Vector2", SHADER_SOCKET_VECTOR);
2026 add_output("Value", SHADER_SOCKET_FLOAT);
2027 add_output("Vector", SHADER_SOCKET_VECTOR);
2030 static ShaderEnum vector_math_type_init()
2034 enm.insert("Add", NODE_VECTOR_MATH_ADD);
2035 enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
2036 enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
2037 enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
2038 enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
2039 enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
2044 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
2046 void VectorMathNode::compile(SVMCompiler& compiler)
2048 ShaderInput *vector1_in = input("Vector1");
2049 ShaderInput *vector2_in = input("Vector2");
2050 ShaderOutput *value_out = output("Value");
2051 ShaderOutput *vector_out = output("Vector");
2053 compiler.stack_assign(vector1_in);
2054 compiler.stack_assign(vector2_in);
2055 compiler.stack_assign(value_out);
2056 compiler.stack_assign(vector_out);
2058 compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
2059 compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
2062 void VectorMathNode::compile(OSLCompiler& compiler)
2064 compiler.parameter("type", type);
2065 compiler.add(this, "node_vector_math");
2070 BumpNode::BumpNode()
2071 : ShaderNode("bump")
2073 add_input("SampleCenter", SHADER_SOCKET_FLOAT);
2074 add_input("SampleX", SHADER_SOCKET_FLOAT);
2075 add_input("SampleY", SHADER_SOCKET_FLOAT);
2077 add_output("Normal", SHADER_SOCKET_NORMAL);
2080 void BumpNode::compile(SVMCompiler& compiler)
2082 ShaderInput *center_in = input("SampleCenter");
2083 ShaderInput *dx_in = input("SampleX");
2084 ShaderInput *dy_in = input("SampleY");
2086 compiler.stack_assign(center_in);
2087 compiler.stack_assign(dx_in);
2088 compiler.stack_assign(dy_in);
2090 compiler.add_node(NODE_SET_BUMP, center_in->stack_offset, dx_in->stack_offset, dy_in->stack_offset);
2093 void BumpNode::compile(OSLCompiler& compiler)
2095 compiler.add(this, "node_bump");