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);
36 min = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
37 max = make_float3(FLT_MAX, FLT_MAX, FLT_MAX);
48 Transform TextureMapping::compute_transform()
50 Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
53 mmat[0][x_mapping-1] = 1.0f;
55 mmat[1][y_mapping-1] = 1.0f;
57 mmat[2][z_mapping-1] = 1.0f;
59 Transform smat = transform_scale(scale);
60 Transform rmat = transform_euler(rotation);
61 Transform tmat = transform_translate(translation);
63 return tmat*rmat*smat*mmat;
66 bool TextureMapping::skip()
68 if(translation != make_float3(0.0f, 0.0f, 0.0f))
70 if(rotation != make_float3(0.0f, 0.0f, 0.0f))
72 if(scale != make_float3(1.0f, 1.0f, 1.0f))
75 if(x_mapping != X || y_mapping != Y || z_mapping != Z)
83 void TextureMapping::compile(SVMCompiler& compiler, int offset_in, int offset_out)
85 if(offset_in == SVM_STACK_INVALID || offset_out == SVM_STACK_INVALID)
88 compiler.add_node(NODE_MAPPING, offset_in, offset_out);
90 Transform tfm = compute_transform();
91 compiler.add_node(tfm.x);
92 compiler.add_node(tfm.y);
93 compiler.add_node(tfm.z);
94 compiler.add_node(tfm.w);
97 compiler.add_node(NODE_MIN_MAX, offset_out, offset_out);
98 compiler.add_node(float3_to_float4(min));
99 compiler.add_node(float3_to_float4(max));
105 static ShaderEnum color_space_init()
109 enm.insert("None", 0);
110 enm.insert("Color", 1);
115 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
117 ImageTextureNode::ImageTextureNode()
118 : TextureNode("image_texture")
120 image_manager = NULL;
124 color_space = ustring("Color");
126 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_UV);
127 add_output("Color", SHADER_SOCKET_COLOR);
128 add_output("Alpha", SHADER_SOCKET_FLOAT);
131 ImageTextureNode::~ImageTextureNode()
134 image_manager->remove_image(filename);
137 ShaderNode *ImageTextureNode::clone() const
139 ImageTextureNode *node = new ImageTextureNode(*this);
140 node->image_manager = NULL;
142 node->is_float = false;
146 void ImageTextureNode::compile(SVMCompiler& compiler)
148 ShaderInput *vector_in = input("Vector");
149 ShaderOutput *color_out = output("Color");
150 ShaderOutput *alpha_out = output("Alpha");
152 image_manager = compiler.image_manager;
154 slot = image_manager->add_image(filename, is_float);
156 if(!color_out->links.empty())
157 compiler.stack_assign(color_out);
158 if(!alpha_out->links.empty())
159 compiler.stack_assign(alpha_out);
162 compiler.stack_assign(vector_in);
164 int srgb = (is_float || color_space != "Color")? 0: 1;
165 int vector_offset = vector_in->stack_offset;
167 if(!tex_mapping.skip()) {
168 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
169 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
172 compiler.add_node(NODE_TEX_IMAGE,
174 compiler.encode_uchar4(
176 color_out->stack_offset,
177 alpha_out->stack_offset,
180 if(vector_offset != vector_in->stack_offset)
181 compiler.stack_clear_offset(vector_in->type, vector_offset);
184 /* image not found */
185 if(!color_out->links.empty()) {
186 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
187 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
189 TEX_IMAGE_MISSING_B));
191 if(!alpha_out->links.empty())
192 compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
196 void ImageTextureNode::compile(OSLCompiler& compiler)
198 compiler.parameter("filename", filename.c_str());
199 if(is_float || color_space != "Color")
200 compiler.parameter("color_space", "Linear");
202 compiler.parameter("color_space", "sRGB");
203 compiler.add(this, "node_image_texture");
206 /* Environment Texture */
208 static ShaderEnum projection_init()
212 enm.insert("Equirectangular", 0);
213 enm.insert("Mirror Ball", 1);
218 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
219 ShaderEnum EnvironmentTextureNode::projection_enum = projection_init();
221 EnvironmentTextureNode::EnvironmentTextureNode()
222 : TextureNode("environment_texture")
224 image_manager = NULL;
228 color_space = ustring("Color");
229 projection = ustring("Equirectangular");
231 add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
232 add_output("Color", SHADER_SOCKET_COLOR);
233 add_output("Alpha", SHADER_SOCKET_FLOAT);
236 EnvironmentTextureNode::~EnvironmentTextureNode()
239 image_manager->remove_image(filename);
242 ShaderNode *EnvironmentTextureNode::clone() const
244 EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
245 node->image_manager = NULL;
247 node->is_float = false;
251 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
253 ShaderInput *vector_in = input("Vector");
254 ShaderOutput *color_out = output("Color");
255 ShaderOutput *alpha_out = output("Alpha");
257 image_manager = compiler.image_manager;
259 slot = image_manager->add_image(filename, is_float);
261 if(!color_out->links.empty())
262 compiler.stack_assign(color_out);
263 if(!alpha_out->links.empty())
264 compiler.stack_assign(alpha_out);
267 compiler.stack_assign(vector_in);
269 int srgb = (is_float || color_space != "Color")? 0: 1;
270 int vector_offset = vector_in->stack_offset;
272 if(!tex_mapping.skip()) {
273 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
274 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
277 compiler.add_node(NODE_TEX_ENVIRONMENT,
279 compiler.encode_uchar4(
281 color_out->stack_offset,
282 alpha_out->stack_offset,
284 projection_enum[projection]);
286 if(vector_offset != vector_in->stack_offset)
287 compiler.stack_clear_offset(vector_in->type, vector_offset);
290 /* image not found */
291 if(!color_out->links.empty()) {
292 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
293 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
295 TEX_IMAGE_MISSING_B));
297 if(!alpha_out->links.empty())
298 compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
302 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
304 compiler.parameter("filename", filename.c_str());
305 if(is_float || color_space != "Color")
306 compiler.parameter("color_space", "Linear");
308 compiler.parameter("color_space", "sRGB");
309 compiler.add(this, "node_environment_texture");
314 static float2 sky_spherical_coordinates(float3 dir)
316 return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
319 static float sky_perez_function(float lam[6], float theta, float gamma)
321 return (1.f + lam[0]*expf(lam[1]/cosf(theta))) * (1.f + lam[2]*expf(lam[3]*gamma) + lam[4]*cosf(gamma)*cosf(gamma));
324 static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity)
326 float2 spherical = sky_spherical_coordinates(dir);
327 float theta = spherical.x;
328 float phi = spherical.y;
330 ksunsky->theta = theta;
333 float theta2 = theta*theta;
334 float theta3 = theta*theta*theta;
338 float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
339 ksunsky->zenith_Y = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
340 ksunsky->zenith_Y *= 0.06f;
343 (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
344 (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
345 (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
348 (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
349 (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) * T +
350 (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
352 ksunsky->perez_Y[0] = (0.1787f * T - 1.4630f);
353 ksunsky->perez_Y[1] = (-0.3554f * T + 0.4275f);
354 ksunsky->perez_Y[2] = (-0.0227f * T + 5.3251f);
355 ksunsky->perez_Y[3] = (0.1206f * T - 2.5771f);
356 ksunsky->perez_Y[4] = (-0.0670f * T + 0.3703f);
358 ksunsky->perez_x[0] = (-0.0193f * T - 0.2592f);
359 ksunsky->perez_x[1] = (-0.0665f * T + 0.0008f);
360 ksunsky->perez_x[2] = (-0.0004f * T + 0.2125f);
361 ksunsky->perez_x[3] = (-0.0641f * T - 0.8989f);
362 ksunsky->perez_x[4] = (-0.0033f * T + 0.0452f);
364 ksunsky->perez_y[0] = (-0.0167f * T - 0.2608f);
365 ksunsky->perez_y[1] = (-0.0950f * T + 0.0092f);
366 ksunsky->perez_y[2] = (-0.0079f * T + 0.2102f);
367 ksunsky->perez_y[3] = (-0.0441f * T - 1.6537f);
368 ksunsky->perez_y[4] = (-0.0109f * T + 0.0529f);
370 ksunsky->zenith_Y /= sky_perez_function(ksunsky->perez_Y, 0, theta);
371 ksunsky->zenith_x /= sky_perez_function(ksunsky->perez_x, 0, theta);
372 ksunsky->zenith_y /= sky_perez_function(ksunsky->perez_y, 0, theta);
375 SkyTextureNode::SkyTextureNode()
376 : TextureNode("sky_texture")
378 sun_direction = make_float3(0.0f, 0.0f, 1.0f);
381 add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
382 add_output("Color", SHADER_SOCKET_COLOR);
385 void SkyTextureNode::compile(SVMCompiler& compiler)
387 ShaderInput *vector_in = input("Vector");
388 ShaderOutput *color_out = output("Color");
390 if(compiler.sunsky) {
391 sky_texture_precompute(compiler.sunsky, sun_direction, turbidity);
392 compiler.sunsky = NULL;
396 compiler.stack_assign(vector_in);
398 int vector_offset = vector_in->stack_offset;
400 if(!tex_mapping.skip()) {
401 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
402 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
405 compiler.stack_assign(color_out);
406 compiler.add_node(NODE_TEX_SKY, vector_offset, color_out->stack_offset);
408 if(vector_offset != vector_in->stack_offset)
409 compiler.stack_clear_offset(vector_in->type, vector_offset);
412 void SkyTextureNode::compile(OSLCompiler& compiler)
414 compiler.parameter_vector("sun_direction", sun_direction);
415 compiler.parameter("turbidity", turbidity);
416 compiler.add(this, "node_sky_texture");
419 /* Gradient Texture */
421 static ShaderEnum gradient_type_init()
425 enm.insert("Linear", NODE_BLEND_LINEAR);
426 enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
427 enm.insert("Easing", NODE_BLEND_EASING);
428 enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
429 enm.insert("Radial", NODE_BLEND_RADIAL);
430 enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
431 enm.insert("Spherical", NODE_BLEND_SPHERICAL);
436 ShaderEnum GradientTextureNode::type_enum = gradient_type_init();
438 GradientTextureNode::GradientTextureNode()
439 : TextureNode("gradient_texture")
441 type = ustring("Linear");
443 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
444 add_output("Color", SHADER_SOCKET_COLOR);
445 add_output("Fac", SHADER_SOCKET_FLOAT);
448 void GradientTextureNode::compile(SVMCompiler& compiler)
450 ShaderInput *vector_in = input("Vector");
451 ShaderOutput *color_out = output("Color");
452 ShaderOutput *fac_out = output("Fac");
454 if(vector_in->link) compiler.stack_assign(vector_in);
456 int vector_offset = vector_in->stack_offset;
458 if(!tex_mapping.skip()) {
459 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
460 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
463 if(!fac_out->links.empty())
464 compiler.stack_assign(fac_out);
465 if(!color_out->links.empty())
466 compiler.stack_assign(color_out);
468 compiler.add_node(NODE_TEX_GRADIENT,
469 compiler.encode_uchar4(type_enum[type], vector_offset, fac_out->stack_offset, color_out->stack_offset));
471 if(vector_offset != vector_in->stack_offset)
472 compiler.stack_clear_offset(vector_in->type, vector_offset);
475 void GradientTextureNode::compile(OSLCompiler& compiler)
477 compiler.parameter("Type", type);
478 compiler.add(this, "node_gradient_texture");
483 NoiseTextureNode::NoiseTextureNode()
484 : TextureNode("noise_texture")
486 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
487 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
488 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
489 add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
491 add_output("Color", SHADER_SOCKET_COLOR);
492 add_output("Fac", SHADER_SOCKET_FLOAT);
495 void NoiseTextureNode::compile(SVMCompiler& compiler)
497 ShaderInput *distortion_in = input("Distortion");
498 ShaderInput *detail_in = input("Detail");
499 ShaderInput *scale_in = input("Scale");
500 ShaderInput *vector_in = input("Vector");
501 ShaderOutput *color_out = output("Color");
502 ShaderOutput *fac_out = output("Fac");
504 if(vector_in->link) compiler.stack_assign(vector_in);
505 if(scale_in->link) compiler.stack_assign(scale_in);
506 if(detail_in->link) compiler.stack_assign(detail_in);
507 if(distortion_in->link) compiler.stack_assign(distortion_in);
509 int vector_offset = vector_in->stack_offset;
511 if(!tex_mapping.skip()) {
512 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
513 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
516 if(!fac_out->links.empty())
517 compiler.stack_assign(fac_out);
518 if(!color_out->links.empty())
519 compiler.stack_assign(color_out);
521 compiler.add_node(NODE_TEX_NOISE,
522 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
523 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset));
525 __float_as_int(scale_in->value.x),
526 __float_as_int(detail_in->value.x),
527 __float_as_int(distortion_in->value.x));
529 if(vector_offset != vector_in->stack_offset)
530 compiler.stack_clear_offset(vector_in->type, vector_offset);
533 void NoiseTextureNode::compile(OSLCompiler& compiler)
535 compiler.add(this, "node_noise_texture");
538 /* Voronoi Texture */
540 static ShaderEnum voronoi_coloring_init()
544 enm.insert("Intensity", NODE_VORONOI_INTENSITY);
545 enm.insert("Cells", NODE_VORONOI_CELLS);
550 ShaderEnum VoronoiTextureNode::coloring_enum = voronoi_coloring_init();
552 VoronoiTextureNode::VoronoiTextureNode()
553 : TextureNode("voronoi_texture")
555 coloring = ustring("Intensity");
557 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
558 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
560 add_output("Color", SHADER_SOCKET_COLOR);
561 add_output("Fac", SHADER_SOCKET_FLOAT);
564 void VoronoiTextureNode::compile(SVMCompiler& compiler)
566 ShaderInput *scale_in = input("Scale");
567 ShaderInput *vector_in = input("Vector");
568 ShaderOutput *color_out = output("Color");
569 ShaderOutput *fac_out = output("Fac");
571 if(vector_in->link) compiler.stack_assign(vector_in);
572 if(scale_in->link) compiler.stack_assign(scale_in);
574 int vector_offset = vector_in->stack_offset;
576 if(!tex_mapping.skip()) {
577 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
578 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
581 compiler.stack_assign(color_out);
582 compiler.stack_assign(fac_out);
584 compiler.add_node(NODE_TEX_VORONOI,
585 coloring_enum[coloring],
586 compiler.encode_uchar4(scale_in->stack_offset, vector_offset, fac_out->stack_offset, color_out->stack_offset),
587 __float_as_int(scale_in->value.x));
589 if(vector_offset != vector_in->stack_offset)
590 compiler.stack_clear_offset(vector_in->type, vector_offset);
593 void VoronoiTextureNode::compile(OSLCompiler& compiler)
595 compiler.parameter("Coloring", coloring);
596 compiler.add(this, "node_voronoi_texture");
599 /* Musgrave Texture */
601 static ShaderEnum musgrave_type_init()
605 enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
606 enm.insert("fBM", NODE_MUSGRAVE_FBM);
607 enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
608 enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
609 enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
614 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
616 MusgraveTextureNode::MusgraveTextureNode()
617 : TextureNode("musgrave_texture")
619 type = ustring("fBM");
621 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
622 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
623 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
624 add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
625 add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
626 add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
627 add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
629 add_output("Fac", SHADER_SOCKET_FLOAT);
630 add_output("Color", SHADER_SOCKET_COLOR);
633 void MusgraveTextureNode::compile(SVMCompiler& compiler)
635 ShaderInput *vector_in = input("Vector");
636 ShaderInput *scale_in = input("Scale");
637 ShaderInput *dimension_in = input("Dimension");
638 ShaderInput *lacunarity_in = input("Lacunarity");
639 ShaderInput *detail_in = input("Detail");
640 ShaderInput *offset_in = input("Offset");
641 ShaderInput *gain_in = input("Gain");
642 ShaderOutput *fac_out = output("Fac");
643 ShaderOutput *color_out = output("Color");
645 if(vector_in->link) compiler.stack_assign(vector_in);
646 if(dimension_in->link) compiler.stack_assign(dimension_in);
647 if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
648 if(detail_in->link) compiler.stack_assign(detail_in);
649 if(offset_in->link) compiler.stack_assign(offset_in);
650 if(gain_in->link) compiler.stack_assign(gain_in);
651 if(scale_in->link) compiler.stack_assign(scale_in);
653 int vector_offset = vector_in->stack_offset;
655 if(!tex_mapping.skip()) {
656 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
657 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
660 if(!fac_out->links.empty())
661 compiler.stack_assign(fac_out);
662 if(!color_out->links.empty())
663 compiler.stack_assign(color_out);
665 compiler.add_node(NODE_TEX_MUSGRAVE,
666 compiler.encode_uchar4(type_enum[type], vector_offset, color_out->stack_offset, fac_out->stack_offset),
667 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, detail_in->stack_offset, offset_in->stack_offset),
668 compiler.encode_uchar4(gain_in->stack_offset, scale_in->stack_offset));
669 compiler.add_node(__float_as_int(dimension_in->value.x),
670 __float_as_int(lacunarity_in->value.x),
671 __float_as_int(detail_in->value.x),
672 __float_as_int(offset_in->value.x));
673 compiler.add_node(__float_as_int(gain_in->value.x),
674 __float_as_int(scale_in->value.x));
676 if(vector_offset != vector_in->stack_offset)
677 compiler.stack_clear_offset(vector_in->type, vector_offset);
680 void MusgraveTextureNode::compile(OSLCompiler& compiler)
682 compiler.parameter("Type", type);
684 compiler.add(this, "node_musgrave_texture");
689 static ShaderEnum wave_type_init()
693 enm.insert("Bands", NODE_WAVE_BANDS);
694 enm.insert("Rings", NODE_WAVE_RINGS);
699 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
701 WaveTextureNode::WaveTextureNode()
702 : TextureNode("wave_texture")
704 type = ustring("Bands");
706 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
707 add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
708 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
709 add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
710 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
712 add_output("Color", SHADER_SOCKET_COLOR);
713 add_output("Fac", SHADER_SOCKET_FLOAT);
716 void WaveTextureNode::compile(SVMCompiler& compiler)
718 ShaderInput *scale_in = input("Scale");
719 ShaderInput *distortion_in = input("Distortion");
720 ShaderInput *dscale_in = input("Detail Scale");
721 ShaderInput *detail_in = input("Detail");
722 ShaderInput *vector_in = input("Vector");
723 ShaderOutput *fac_out = output("Fac");
724 ShaderOutput *color_out = output("Color");
726 if(scale_in->link) compiler.stack_assign(scale_in);
727 if(detail_in->link) compiler.stack_assign(detail_in);
728 if(distortion_in->link) compiler.stack_assign(distortion_in);
729 if(dscale_in->link) compiler.stack_assign(dscale_in);
730 if(vector_in->link) compiler.stack_assign(vector_in);
732 int vector_offset = vector_in->stack_offset;
734 if(!tex_mapping.skip()) {
735 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
736 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
739 if(!fac_out->links.empty())
740 compiler.stack_assign(fac_out);
741 if(!color_out->links.empty())
742 compiler.stack_assign(color_out);
744 compiler.add_node(NODE_TEX_WAVE,
745 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
746 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset));
749 __float_as_int(scale_in->value.x),
750 __float_as_int(detail_in->value.x),
751 __float_as_int(distortion_in->value.x),
752 __float_as_int(dscale_in->value.x));
754 if(vector_offset != vector_in->stack_offset)
755 compiler.stack_clear_offset(vector_in->type, vector_offset);
758 void WaveTextureNode::compile(OSLCompiler& compiler)
760 compiler.parameter("Type", type);
762 compiler.add(this, "node_wave_texture");
767 MagicTextureNode::MagicTextureNode()
768 : TextureNode("magic_texture")
772 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
773 add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
774 add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
776 add_output("Color", SHADER_SOCKET_COLOR);
777 add_output("Fac", SHADER_SOCKET_FLOAT);
780 void MagicTextureNode::compile(SVMCompiler& compiler)
782 ShaderInput *vector_in = input("Vector");
783 ShaderInput *scale_in = input("Scale");
784 ShaderInput *distortion_in = input("Distortion");
785 ShaderOutput *color_out = output("Color");
786 ShaderOutput *fac_out = output("Fac");
788 if(vector_in->link) compiler.stack_assign(vector_in);
789 if(distortion_in->link) compiler.stack_assign(distortion_in);
790 if(scale_in->link) compiler.stack_assign(scale_in);
792 int vector_offset = vector_in->stack_offset;
794 if(!tex_mapping.skip()) {
795 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
796 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
799 if(!fac_out->links.empty())
800 compiler.stack_assign(fac_out);
801 if(!color_out->links.empty())
802 compiler.stack_assign(color_out);
804 compiler.add_node(NODE_TEX_MAGIC,
805 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
806 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, distortion_in->stack_offset));
808 __float_as_int(scale_in->value.x),
809 __float_as_int(distortion_in->value.x));
811 if(vector_offset != vector_in->stack_offset)
812 compiler.stack_clear_offset(vector_in->type, vector_offset);
815 void MagicTextureNode::compile(OSLCompiler& compiler)
817 compiler.parameter("Depth", depth);
818 compiler.add(this, "node_magic_texture");
821 /* Checker Texture */
823 CheckerTextureNode::CheckerTextureNode()
824 : TextureNode("checker_texture")
826 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
827 add_input("Color1", SHADER_SOCKET_COLOR);
828 add_input("Color2", SHADER_SOCKET_COLOR);
829 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
831 add_output("Color", SHADER_SOCKET_COLOR);
832 add_output("Fac", SHADER_SOCKET_FLOAT);
835 void CheckerTextureNode::compile(SVMCompiler& compiler)
837 ShaderInput *vector_in = input("Vector");
838 ShaderInput *color1_in = input("Color1");
839 ShaderInput *color2_in = input("Color2");
840 ShaderInput *scale_in = input("Scale");
842 ShaderOutput *color_out = output("Color");
843 ShaderOutput *fac_out = output("Fac");
845 compiler.stack_assign(vector_in);
846 compiler.stack_assign(color1_in);
847 compiler.stack_assign(color2_in);
848 if(scale_in->link) compiler.stack_assign(scale_in);
850 int vector_offset = vector_in->stack_offset;
852 if(!tex_mapping.skip()) {
853 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
854 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
857 if(!color_out->links.empty())
858 compiler.stack_assign(color_out);
859 if(!fac_out->links.empty())
860 compiler.stack_assign(fac_out);
862 compiler.add_node(NODE_TEX_CHECKER,
863 compiler.encode_uchar4(vector_offset, color1_in->stack_offset, color2_in->stack_offset, scale_in->stack_offset),
864 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset),
865 __float_as_int(scale_in->value.x));
867 if(vector_offset != vector_in->stack_offset)
868 compiler.stack_clear_offset(vector_in->type, vector_offset);
871 void CheckerTextureNode::compile(OSLCompiler& compiler)
873 compiler.add(this, "node_checker_texture");
878 NormalNode::NormalNode()
879 : ShaderNode("normal")
881 direction = make_float3(0.0f, 0.0f, 1.0f);
883 add_input("Normal", SHADER_SOCKET_NORMAL);
884 add_output("Normal", SHADER_SOCKET_NORMAL);
885 add_output("Dot", SHADER_SOCKET_FLOAT);
888 void NormalNode::compile(SVMCompiler& compiler)
890 ShaderInput *normal_in = input("Normal");
891 ShaderOutput *normal_out = output("Normal");
892 ShaderOutput *dot_out = output("Dot");
894 compiler.stack_assign(normal_in);
895 compiler.stack_assign(normal_out);
896 compiler.stack_assign(dot_out);
898 compiler.add_node(NODE_NORMAL, normal_in->stack_offset, normal_out->stack_offset, dot_out->stack_offset);
900 __float_as_int(direction.x),
901 __float_as_int(direction.y),
902 __float_as_int(direction.z));
905 void NormalNode::compile(OSLCompiler& compiler)
907 compiler.parameter_vector("Direction", direction);
908 compiler.add(this, "node_normal");
913 MappingNode::MappingNode()
914 : ShaderNode("mapping")
916 add_input("Vector", SHADER_SOCKET_POINT);
917 add_output("Vector", SHADER_SOCKET_POINT);
920 void MappingNode::compile(SVMCompiler& compiler)
922 ShaderInput *vector_in = input("Vector");
923 ShaderOutput *vector_out = output("Vector");
925 compiler.stack_assign(vector_in);
926 compiler.stack_assign(vector_out);
928 tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
931 void MappingNode::compile(OSLCompiler& compiler)
933 Transform tfm = transform_transpose(tex_mapping.compute_transform());
934 compiler.parameter("Matrix", tfm);
936 compiler.add(this, "node_mapping");
941 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_)
942 : ShaderNode("convert")
949 if(from == SHADER_SOCKET_FLOAT)
950 add_input("Val", SHADER_SOCKET_FLOAT);
951 else if(from == SHADER_SOCKET_COLOR)
952 add_input("Color", SHADER_SOCKET_COLOR);
953 else if(from == SHADER_SOCKET_VECTOR)
954 add_input("Vector", SHADER_SOCKET_VECTOR);
955 else if(from == SHADER_SOCKET_POINT)
956 add_input("Point", SHADER_SOCKET_POINT);
957 else if(from == SHADER_SOCKET_NORMAL)
958 add_input("Normal", SHADER_SOCKET_NORMAL);
962 if(to == SHADER_SOCKET_FLOAT)
963 add_output("Val", SHADER_SOCKET_FLOAT);
964 else if(to == SHADER_SOCKET_COLOR)
965 add_output("Color", SHADER_SOCKET_COLOR);
966 else if(to == SHADER_SOCKET_VECTOR)
967 add_output("Vector", SHADER_SOCKET_VECTOR);
968 else if(to == SHADER_SOCKET_POINT)
969 add_output("Point", SHADER_SOCKET_POINT);
970 else if(to == SHADER_SOCKET_NORMAL)
971 add_output("Normal", SHADER_SOCKET_NORMAL);
976 void ConvertNode::compile(SVMCompiler& compiler)
978 ShaderInput *in = inputs[0];
979 ShaderOutput *out = outputs[0];
981 if(to == SHADER_SOCKET_FLOAT) {
982 compiler.stack_assign(in);
983 compiler.stack_assign(out);
985 if(from == SHADER_SOCKET_COLOR)
987 compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
989 /* vector/point/normal to float */
990 compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
992 else if(from == SHADER_SOCKET_FLOAT) {
993 compiler.stack_assign(in);
994 compiler.stack_assign(out);
996 /* float to float3 */
997 compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
1000 /* float3 to float3 */
1003 compiler.stack_link(in, out);
1006 /* set 0,0,0 value */
1007 compiler.stack_assign(in);
1008 compiler.stack_assign(out);
1010 compiler.add_node(NODE_VALUE_V, in->stack_offset);
1011 compiler.add_node(NODE_VALUE_V, in->value);
1016 void ConvertNode::compile(OSLCompiler& compiler)
1018 if(from == SHADER_SOCKET_FLOAT)
1019 compiler.add(this, "node_convert_from_float");
1020 else if(from == SHADER_SOCKET_COLOR)
1021 compiler.add(this, "node_convert_from_color");
1022 else if(from == SHADER_SOCKET_VECTOR)
1023 compiler.add(this, "node_convert_from_vector");
1024 else if(from == SHADER_SOCKET_POINT)
1025 compiler.add(this, "node_convert_from_point");
1026 else if(from == SHADER_SOCKET_NORMAL)
1027 compiler.add(this, "node_convert_from_normal");
1034 ProxyNode::ProxyNode(ShaderSocketType from_, ShaderSocketType to_)
1035 : ShaderNode("proxy")
1040 add_input("Input", from);
1041 add_output("Output", to);
1044 void ProxyNode::compile(SVMCompiler& compiler)
1048 void ProxyNode::compile(OSLCompiler& compiler)
1054 BsdfNode::BsdfNode()
1055 : ShaderNode("bsdf")
1057 closure = ccl::CLOSURE_BSDF_DIFFUSE_ID;
1059 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1060 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1062 add_output("BSDF", SHADER_SOCKET_CLOSURE);
1065 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1067 ShaderInput *color_in = input("Color");
1069 if(color_in->link) {
1070 compiler.stack_assign(color_in);
1071 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1074 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1077 compiler.stack_assign(param1);
1079 compiler.stack_assign(param2);
1081 compiler.add_node(NODE_CLOSURE_BSDF,
1082 compiler.encode_uchar4(closure,
1083 (param1)? param1->stack_offset: SVM_STACK_INVALID,
1084 (param2)? param2->stack_offset: SVM_STACK_INVALID,
1085 compiler.closure_mix_weight_offset()),
1086 __float_as_int((param1)? param1->value.x: 0.0f),
1087 __float_as_int((param2)? param2->value.x: 0.0f));
1090 void BsdfNode::compile(SVMCompiler& compiler)
1092 compile(compiler, NULL, NULL);
1095 void BsdfNode::compile(OSLCompiler& compiler)
1100 /* Ward BSDF Closure */
1102 WardBsdfNode::WardBsdfNode()
1104 closure = CLOSURE_BSDF_WARD_ID;
1106 add_input("Roughness U", SHADER_SOCKET_FLOAT, 0.2f);
1107 add_input("Roughness V", SHADER_SOCKET_FLOAT, 0.2f);
1110 void WardBsdfNode::compile(SVMCompiler& compiler)
1112 BsdfNode::compile(compiler, input("Roughness U"), input("Roughness V"));
1115 void WardBsdfNode::compile(OSLCompiler& compiler)
1117 compiler.add(this, "node_ward_bsdf");
1120 /* Glossy BSDF Closure */
1122 static ShaderEnum glossy_distribution_init()
1126 enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1127 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1128 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1133 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1135 GlossyBsdfNode::GlossyBsdfNode()
1137 distribution = ustring("Beckmann");
1139 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1142 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1144 closure = (ClosureType)distribution_enum[distribution];
1146 if(closure == CLOSURE_BSDF_REFLECTION_ID)
1147 BsdfNode::compile(compiler, NULL, NULL);
1149 BsdfNode::compile(compiler, input("Roughness"), NULL);
1152 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1154 compiler.parameter("distribution", distribution);
1155 compiler.add(this, "node_glossy_bsdf");
1158 /* Glass BSDF Closure */
1160 static ShaderEnum glass_distribution_init()
1164 enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1165 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1166 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1171 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1173 GlassBsdfNode::GlassBsdfNode()
1175 distribution = ustring("Sharp");
1177 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1178 add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1181 void GlassBsdfNode::compile(SVMCompiler& compiler)
1183 closure = (ClosureType)distribution_enum[distribution];
1185 if(closure == CLOSURE_BSDF_REFRACTION_ID)
1186 BsdfNode::compile(compiler, NULL, input("IOR"));
1188 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1191 void GlassBsdfNode::compile(OSLCompiler& compiler)
1193 compiler.parameter("distribution", distribution);
1194 compiler.add(this, "node_glass_bsdf");
1197 /* Velvet BSDF Closure */
1199 VelvetBsdfNode::VelvetBsdfNode()
1201 closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1203 add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1206 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1208 BsdfNode::compile(compiler, input("Sigma"), NULL);
1211 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1213 compiler.add(this, "node_velvet_bsdf");
1216 /* Diffuse BSDF Closure */
1218 DiffuseBsdfNode::DiffuseBsdfNode()
1220 closure = CLOSURE_BSDF_DIFFUSE_ID;
1221 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1224 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1226 BsdfNode::compile(compiler, input("Roughness"), NULL);
1229 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1231 compiler.add(this, "node_diffuse_bsdf");
1234 /* Translucent BSDF Closure */
1236 TranslucentBsdfNode::TranslucentBsdfNode()
1238 closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1241 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1243 BsdfNode::compile(compiler, NULL, NULL);
1246 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1248 compiler.add(this, "node_translucent_bsdf");
1251 /* Transparent BSDF Closure */
1253 TransparentBsdfNode::TransparentBsdfNode()
1255 name = "transparent";
1256 closure = CLOSURE_BSDF_TRANSPARENT_ID;
1259 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1261 BsdfNode::compile(compiler, NULL, NULL);
1264 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1266 compiler.add(this, "node_transparent_bsdf");
1269 /* Emissive Closure */
1271 EmissionNode::EmissionNode()
1272 : ShaderNode("emission")
1274 total_power = false;
1276 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1277 add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1278 add_output("Emission", SHADER_SOCKET_CLOSURE);
1281 void EmissionNode::compile(SVMCompiler& compiler)
1283 ShaderInput *color_in = input("Color");
1284 ShaderInput *strength_in = input("Strength");
1286 if(color_in->link || strength_in->link) {
1287 compiler.stack_assign(color_in);
1288 compiler.stack_assign(strength_in);
1289 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1291 else if(total_power)
1292 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1294 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1296 compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1299 void EmissionNode::compile(OSLCompiler& compiler)
1301 compiler.parameter("TotalPower", (total_power)? 1: 0);
1302 compiler.add(this, "node_emission");
1305 /* Background Closure */
1307 BackgroundNode::BackgroundNode()
1308 : ShaderNode("background")
1310 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1311 add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1312 add_output("Background", SHADER_SOCKET_CLOSURE);
1315 void BackgroundNode::compile(SVMCompiler& compiler)
1317 ShaderInput *color_in = input("Color");
1318 ShaderInput *strength_in = input("Strength");
1320 if(color_in->link || strength_in->link) {
1321 compiler.stack_assign(color_in);
1322 compiler.stack_assign(strength_in);
1323 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1326 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1328 compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1331 void BackgroundNode::compile(OSLCompiler& compiler)
1333 compiler.add(this, "node_background");
1336 /* Holdout Closure */
1338 HoldoutNode::HoldoutNode()
1339 : ShaderNode("holdout")
1341 add_output("Holdout", SHADER_SOCKET_CLOSURE);
1344 void HoldoutNode::compile(SVMCompiler& compiler)
1346 compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1349 void HoldoutNode::compile(OSLCompiler& compiler)
1351 compiler.add(this, "node_holdout");
1354 /* Volume Closure */
1356 VolumeNode::VolumeNode()
1357 : ShaderNode("volume")
1359 closure = ccl::CLOSURE_VOLUME_ISOTROPIC_ID;
1361 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1362 add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1364 add_output("Volume", SHADER_SOCKET_CLOSURE);
1367 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1369 ShaderInput *color_in = input("Color");
1371 if(color_in->link) {
1372 compiler.stack_assign(color_in);
1373 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1376 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1379 compiler.stack_assign(param1);
1381 compiler.stack_assign(param2);
1383 compiler.add_node(NODE_CLOSURE_VOLUME,
1384 compiler.encode_uchar4(closure,
1385 (param1)? param1->stack_offset: SVM_STACK_INVALID,
1386 (param2)? param2->stack_offset: SVM_STACK_INVALID,
1387 compiler.closure_mix_weight_offset()),
1388 __float_as_int((param1)? param1->value.x: 0.0f),
1389 __float_as_int((param2)? param2->value.x: 0.0f));
1392 void VolumeNode::compile(SVMCompiler& compiler)
1394 compile(compiler, NULL, NULL);
1397 void VolumeNode::compile(OSLCompiler& compiler)
1402 /* Transparent Volume Closure */
1404 TransparentVolumeNode::TransparentVolumeNode()
1406 closure = CLOSURE_VOLUME_TRANSPARENT_ID;
1409 void TransparentVolumeNode::compile(SVMCompiler& compiler)
1411 VolumeNode::compile(compiler, input("Density"), NULL);
1414 void TransparentVolumeNode::compile(OSLCompiler& compiler)
1416 compiler.add(this, "node_isotropic_volume");
1419 /* Isotropic Volume Closure */
1421 IsotropicVolumeNode::IsotropicVolumeNode()
1423 closure = CLOSURE_VOLUME_ISOTROPIC_ID;
1426 void IsotropicVolumeNode::compile(SVMCompiler& compiler)
1428 VolumeNode::compile(compiler, input("Density"), NULL);
1431 void IsotropicVolumeNode::compile(OSLCompiler& compiler)
1433 compiler.add(this, "node_isotropic_volume");
1438 GeometryNode::GeometryNode()
1439 : ShaderNode("geometry")
1441 add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1442 add_output("Position", SHADER_SOCKET_POINT);
1443 add_output("Normal", SHADER_SOCKET_NORMAL);
1444 add_output("Tangent", SHADER_SOCKET_NORMAL);
1445 add_output("True Normal", SHADER_SOCKET_NORMAL);
1446 add_output("Incoming", SHADER_SOCKET_VECTOR);
1447 add_output("Parametric", SHADER_SOCKET_POINT);
1448 add_output("Backfacing", SHADER_SOCKET_FLOAT);
1451 void GeometryNode::compile(SVMCompiler& compiler)
1454 NodeType geom_node = NODE_GEOMETRY;
1456 if(bump == SHADER_BUMP_DX)
1457 geom_node = NODE_GEOMETRY_BUMP_DX;
1458 else if(bump == SHADER_BUMP_DY)
1459 geom_node = NODE_GEOMETRY_BUMP_DY;
1461 out = output("Position");
1462 if(!out->links.empty()) {
1463 compiler.stack_assign(out);
1464 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1467 out = output("Normal");
1468 if(!out->links.empty()) {
1469 compiler.stack_assign(out);
1470 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
1473 out = output("Tangent");
1474 if(!out->links.empty()) {
1475 compiler.stack_assign(out);
1476 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
1479 out = output("True Normal");
1480 if(!out->links.empty()) {
1481 compiler.stack_assign(out);
1482 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
1485 out = output("Incoming");
1486 if(!out->links.empty()) {
1487 compiler.stack_assign(out);
1488 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1491 out = output("Parametric");
1492 if(!out->links.empty()) {
1493 compiler.stack_assign(out);
1494 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
1497 out = output("Backfacing");
1498 if(!out->links.empty()) {
1499 compiler.stack_assign(out);
1500 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
1504 void GeometryNode::compile(OSLCompiler& compiler)
1506 if(bump == SHADER_BUMP_DX)
1507 compiler.parameter("bump_offset", "dx");
1508 else if(bump == SHADER_BUMP_DY)
1509 compiler.parameter("bump_offset", "dy");
1511 compiler.parameter("bump_offset", "center");
1513 compiler.add(this, "node_geometry");
1516 /* TextureCoordinate */
1518 TextureCoordinateNode::TextureCoordinateNode()
1519 : ShaderNode("texture_coordinate")
1521 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
1522 add_output("Generated", SHADER_SOCKET_POINT);
1523 add_output("Normal", SHADER_SOCKET_NORMAL);
1524 add_output("UV", SHADER_SOCKET_POINT);
1525 add_output("Object", SHADER_SOCKET_POINT);
1526 add_output("Camera", SHADER_SOCKET_POINT);
1527 add_output("Window", SHADER_SOCKET_POINT);
1528 add_output("Reflection", SHADER_SOCKET_NORMAL);
1531 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
1533 if(!output("Generated")->links.empty())
1534 attributes->add(ATTR_STD_GENERATED);
1535 if(!output("UV")->links.empty())
1536 attributes->add(ATTR_STD_UV);
1538 ShaderNode::attributes(attributes);
1541 void TextureCoordinateNode::compile(SVMCompiler& compiler)
1544 NodeType texco_node = NODE_TEX_COORD;
1545 NodeType attr_node = NODE_ATTR;
1546 NodeType geom_node = NODE_GEOMETRY;
1548 if(bump == SHADER_BUMP_DX) {
1549 texco_node = NODE_TEX_COORD_BUMP_DX;
1550 attr_node = NODE_ATTR_BUMP_DX;
1551 geom_node = NODE_GEOMETRY_BUMP_DX;
1553 else if(bump == SHADER_BUMP_DY) {
1554 texco_node = NODE_TEX_COORD_BUMP_DY;
1555 attr_node = NODE_ATTR_BUMP_DY;
1556 geom_node = NODE_GEOMETRY_BUMP_DY;
1559 out = output("Generated");
1560 if(!out->links.empty()) {
1561 if(compiler.background) {
1562 compiler.stack_assign(out);
1563 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
1566 int attr = compiler.attribute(ATTR_STD_GENERATED);
1567 compiler.stack_assign(out);
1568 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1572 out = output("Normal");
1573 if(!out->links.empty()) {
1574 compiler.stack_assign(out);
1575 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
1579 if(!out->links.empty()) {
1580 int attr = compiler.attribute(ATTR_STD_UV);
1581 compiler.stack_assign(out);
1582 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
1585 out = output("Object");
1586 if(!out->links.empty()) {
1587 compiler.stack_assign(out);
1588 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
1591 out = output("Camera");
1592 if(!out->links.empty()) {
1593 compiler.stack_assign(out);
1594 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
1597 out = output("Window");
1598 if(!out->links.empty()) {
1599 compiler.stack_assign(out);
1600 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
1603 out = output("Reflection");
1604 if(!out->links.empty()) {
1605 if(compiler.background) {
1606 compiler.stack_assign(out);
1607 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
1610 compiler.stack_assign(out);
1611 compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
1616 void TextureCoordinateNode::compile(OSLCompiler& compiler)
1618 if(bump == SHADER_BUMP_DX)
1619 compiler.parameter("bump_offset", "dx");
1620 else if(bump == SHADER_BUMP_DY)
1621 compiler.parameter("bump_offset", "dy");
1623 compiler.parameter("bump_offset", "center");
1625 if(compiler.background)
1626 compiler.parameter("is_background", true);
1628 compiler.add(this, "node_texture_coordinate");
1633 LightPathNode::LightPathNode()
1634 : ShaderNode("light_path")
1636 add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
1637 add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
1638 add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
1639 add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
1640 add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
1641 add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
1642 add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
1643 add_output("Ray Length", SHADER_SOCKET_FLOAT);
1646 void LightPathNode::compile(SVMCompiler& compiler)
1650 out = output("Is Camera Ray");
1651 if(!out->links.empty()) {
1652 compiler.stack_assign(out);
1653 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
1656 out = output("Is Shadow Ray");
1657 if(!out->links.empty()) {
1658 compiler.stack_assign(out);
1659 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
1662 out = output("Is Diffuse Ray");
1663 if(!out->links.empty()) {
1664 compiler.stack_assign(out);
1665 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
1668 out = output("Is Glossy Ray");
1669 if(!out->links.empty()) {
1670 compiler.stack_assign(out);
1671 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
1674 out = output("Is Singular Ray");
1675 if(!out->links.empty()) {
1676 compiler.stack_assign(out);
1677 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
1680 out = output("Is Reflection Ray");
1681 if(!out->links.empty()) {
1682 compiler.stack_assign(out);
1683 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
1687 out = output("Is Transmission Ray");
1688 if(!out->links.empty()) {
1689 compiler.stack_assign(out);
1690 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
1693 out = output("Ray Length");
1694 if(!out->links.empty()) {
1695 compiler.stack_assign(out);
1696 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
1701 void LightPathNode::compile(OSLCompiler& compiler)
1703 compiler.add(this, "node_light_path");
1708 LightFalloffNode::LightFalloffNode()
1709 : ShaderNode("light_path")
1711 add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
1712 add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1713 add_output("Quadratic", SHADER_SOCKET_FLOAT);
1714 add_output("Linear", SHADER_SOCKET_FLOAT);
1715 add_output("Constant", SHADER_SOCKET_FLOAT);
1718 void LightFalloffNode::compile(SVMCompiler& compiler)
1720 ShaderInput *strength_in = input("Strength");
1721 ShaderInput *smooth_in = input("Smooth");
1723 compiler.stack_assign(strength_in);
1724 compiler.stack_assign(smooth_in);
1726 ShaderOutput *out = output("Quadratic");
1727 if(!out->links.empty()) {
1728 compiler.stack_assign(out);
1729 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
1730 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1733 out = output("Linear");
1734 if(!out->links.empty()) {
1735 compiler.stack_assign(out);
1736 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
1737 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1740 out = output("Constant");
1741 if(!out->links.empty()) {
1742 compiler.stack_assign(out);
1743 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
1744 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
1748 void LightFalloffNode::compile(OSLCompiler& compiler)
1750 compiler.add(this, "node_light_falloff");
1755 ObjectInfoNode::ObjectInfoNode()
1756 : ShaderNode("object_info")
1758 add_output("Location", SHADER_SOCKET_VECTOR);
1759 add_output("Object Index", SHADER_SOCKET_FLOAT);
1760 add_output("Material Index", SHADER_SOCKET_FLOAT);
1761 add_output("Random", SHADER_SOCKET_FLOAT);
1764 void ObjectInfoNode::compile(SVMCompiler& compiler)
1766 ShaderOutput *out = output("Location");
1767 if(!out->links.empty()) {
1768 compiler.stack_assign(out);
1769 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
1772 out = output("Object Index");
1773 if(!out->links.empty()) {
1774 compiler.stack_assign(out);
1775 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
1778 out = output("Material Index");
1779 if(!out->links.empty()) {
1780 compiler.stack_assign(out);
1781 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
1784 out = output("Random");
1785 if(!out->links.empty()) {
1786 compiler.stack_assign(out);
1787 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
1791 void ObjectInfoNode::compile(OSLCompiler& compiler)
1793 compiler.add(this, "node_object_info");
1798 ParticleInfoNode::ParticleInfoNode()
1799 : ShaderNode("particle_info")
1801 add_output("Age", SHADER_SOCKET_FLOAT);
1802 add_output("Lifetime", SHADER_SOCKET_FLOAT);
1805 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
1807 if(!output("Age")->links.empty())
1808 attributes->add(ATTR_STD_PARTICLE);
1809 if(!output("Lifetime")->links.empty())
1810 attributes->add(ATTR_STD_PARTICLE);
1812 ShaderNode::attributes(attributes);
1815 void ParticleInfoNode::compile(SVMCompiler& compiler)
1819 out = output("Age");
1820 if(!out->links.empty()) {
1821 compiler.stack_assign(out);
1822 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
1825 out = output("Lifetime");
1826 if(!out->links.empty()) {
1827 compiler.stack_assign(out);
1828 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
1832 void ParticleInfoNode::compile(OSLCompiler& compiler)
1834 compiler.add(this, "node_particle_info");
1839 ValueNode::ValueNode()
1840 : ShaderNode("value")
1844 add_output("Value", SHADER_SOCKET_FLOAT);
1847 void ValueNode::compile(SVMCompiler& compiler)
1849 ShaderOutput *val_out = output("Value");
1851 compiler.stack_assign(val_out);
1852 compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
1855 void ValueNode::compile(OSLCompiler& compiler)
1857 compiler.parameter("value_value", value);
1858 compiler.add(this, "node_value");
1863 ColorNode::ColorNode()
1864 : ShaderNode("color")
1866 value = make_float3(0.0f, 0.0f, 0.0f);
1868 add_output("Color", SHADER_SOCKET_COLOR);
1871 void ColorNode::compile(SVMCompiler& compiler)
1873 ShaderOutput *color_out = output("Color");
1875 if(color_out && !color_out->links.empty()) {
1876 compiler.stack_assign(color_out);
1877 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
1878 compiler.add_node(NODE_VALUE_V, value);
1882 void ColorNode::compile(OSLCompiler& compiler)
1884 compiler.parameter_color("color_value", value);
1886 compiler.add(this, "node_value");
1891 AddClosureNode::AddClosureNode()
1892 : ShaderNode("add_closure")
1894 add_input("Closure1", SHADER_SOCKET_CLOSURE);
1895 add_input("Closure2", SHADER_SOCKET_CLOSURE);
1896 add_output("Closure", SHADER_SOCKET_CLOSURE);
1899 void AddClosureNode::compile(SVMCompiler& compiler)
1901 /* handled in the SVM compiler */
1904 void AddClosureNode::compile(OSLCompiler& compiler)
1906 compiler.add(this, "node_add_closure");
1911 MixClosureNode::MixClosureNode()
1912 : ShaderNode("mix_closure")
1914 add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1915 add_input("Closure1", SHADER_SOCKET_CLOSURE);
1916 add_input("Closure2", SHADER_SOCKET_CLOSURE);
1917 add_output("Closure", SHADER_SOCKET_CLOSURE);
1920 void MixClosureNode::compile(SVMCompiler& compiler)
1922 /* handled in the SVM compiler */
1925 void MixClosureNode::compile(OSLCompiler& compiler)
1927 compiler.add(this, "node_mix_closure");
1932 InvertNode::InvertNode()
1933 : ShaderNode("invert")
1935 add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
1936 add_input("Color", SHADER_SOCKET_COLOR);
1937 add_output("Color", SHADER_SOCKET_COLOR);
1940 void InvertNode::compile(SVMCompiler& compiler)
1942 ShaderInput *fac_in = input("Fac");
1943 ShaderInput *color_in = input("Color");
1944 ShaderOutput *color_out = output("Color");
1946 compiler.stack_assign(fac_in);
1947 compiler.stack_assign(color_in);
1948 compiler.stack_assign(color_out);
1950 compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
1953 void InvertNode::compile(OSLCompiler& compiler)
1955 compiler.add(this, "node_invert");
1963 type = ustring("Mix");
1965 add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
1966 add_input("Color1", SHADER_SOCKET_COLOR);
1967 add_input("Color2", SHADER_SOCKET_COLOR);
1968 add_output("Color", SHADER_SOCKET_COLOR);
1971 static ShaderEnum mix_type_init()
1975 enm.insert("Mix", NODE_MIX_BLEND);
1976 enm.insert("Add", NODE_MIX_ADD);
1977 enm.insert("Multiply", NODE_MIX_MUL);
1978 enm.insert("Screen", NODE_MIX_SCREEN);
1979 enm.insert("Overlay", NODE_MIX_OVERLAY);
1980 enm.insert("Subtract", NODE_MIX_SUB);
1981 enm.insert("Divide", NODE_MIX_DIV);
1982 enm.insert("Difference", NODE_MIX_DIFF);
1983 enm.insert("Darken", NODE_MIX_DARK);
1984 enm.insert("Lighten", NODE_MIX_LIGHT);
1985 enm.insert("Dodge", NODE_MIX_DODGE);
1986 enm.insert("Burn", NODE_MIX_BURN);
1987 enm.insert("Hue", NODE_MIX_HUE);
1988 enm.insert("Saturation", NODE_MIX_SAT);
1989 enm.insert("Value", NODE_MIX_VAL );
1990 enm.insert("Color", NODE_MIX_COLOR);
1991 enm.insert("Soft Light", NODE_MIX_SOFT);
1992 enm.insert("Linear Light", NODE_MIX_LINEAR);
1997 ShaderEnum MixNode::type_enum = mix_type_init();
1999 void MixNode::compile(SVMCompiler& compiler)
2001 ShaderInput *fac_in = input("Fac");
2002 ShaderInput *color1_in = input("Color1");
2003 ShaderInput *color2_in = input("Color2");
2004 ShaderOutput *color_out = output("Color");
2006 compiler.stack_assign(fac_in);
2007 compiler.stack_assign(color1_in);
2008 compiler.stack_assign(color2_in);
2009 compiler.stack_assign(color_out);
2011 compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2012 compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2015 void MixNode::compile(OSLCompiler& compiler)
2017 compiler.parameter("type", type);
2018 compiler.add(this, "node_mix");
2022 CombineRGBNode::CombineRGBNode()
2023 : ShaderNode("combine_rgb")
2025 add_input("R", SHADER_SOCKET_FLOAT);
2026 add_input("G", SHADER_SOCKET_FLOAT);
2027 add_input("B", SHADER_SOCKET_FLOAT);
2028 add_output("Image", SHADER_SOCKET_COLOR);
2031 void CombineRGBNode::compile(SVMCompiler& compiler)
2033 ShaderInput *red_in = input("R");
2034 ShaderInput *green_in = input("G");
2035 ShaderInput *blue_in = input("B");
2036 ShaderOutput *color_out = output("Image");
2038 compiler.stack_assign(color_out);
2040 compiler.stack_assign(red_in);
2041 compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2043 compiler.stack_assign(green_in);
2044 compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2046 compiler.stack_assign(blue_in);
2047 compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2050 void CombineRGBNode::compile(OSLCompiler& compiler)
2052 compiler.add(this, "node_combine_rgb");
2056 GammaNode::GammaNode()
2057 : ShaderNode("gamma")
2059 add_input("Color", SHADER_SOCKET_COLOR);
2060 add_input("Gamma", SHADER_SOCKET_FLOAT);
2061 add_output("Color", SHADER_SOCKET_COLOR);
2064 void GammaNode::compile(SVMCompiler& compiler)
2066 ShaderInput *color_in = input("Color");
2067 ShaderInput *gamma_in = input("Gamma");
2068 ShaderOutput *color_out = output("Color");
2070 compiler.stack_assign(color_in);
2071 compiler.stack_assign(gamma_in);
2072 compiler.stack_assign(color_out);
2074 compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2077 void GammaNode::compile(OSLCompiler& compiler)
2079 compiler.add(this, "node_gamma");
2082 /* Bright Contrast */
2083 BrightContrastNode::BrightContrastNode()
2084 : ShaderNode("brightness")
2086 add_input("Color", SHADER_SOCKET_COLOR);
2087 add_input("Bright", SHADER_SOCKET_FLOAT);
2088 add_input("Contrast", SHADER_SOCKET_FLOAT);
2089 add_output("Color", SHADER_SOCKET_COLOR);
2092 void BrightContrastNode::compile(SVMCompiler& compiler)
2094 ShaderInput *color_in = input("Color");
2095 ShaderInput *bright_in = input("Bright");
2096 ShaderInput *contrast_in = input("Contrast");
2097 ShaderOutput *color_out = output("Color");
2099 compiler.stack_assign(color_in);
2100 compiler.stack_assign(bright_in);
2101 compiler.stack_assign(contrast_in);
2102 compiler.stack_assign(color_out);
2104 compiler.add_node(NODE_BRIGHTCONTRAST,
2105 color_in->stack_offset, color_out->stack_offset,
2106 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2109 void BrightContrastNode::compile(OSLCompiler& compiler)
2111 compiler.add(this, "node_brightness");
2115 SeparateRGBNode::SeparateRGBNode()
2116 : ShaderNode("separate_rgb")
2118 add_input("Image", SHADER_SOCKET_COLOR);
2119 add_output("R", SHADER_SOCKET_FLOAT);
2120 add_output("G", SHADER_SOCKET_FLOAT);
2121 add_output("B", SHADER_SOCKET_FLOAT);
2124 void SeparateRGBNode::compile(SVMCompiler& compiler)
2126 ShaderInput *color_in = input("Image");
2127 ShaderOutput *red_out = output("R");
2128 ShaderOutput *green_out = output("G");
2129 ShaderOutput *blue_out = output("B");
2131 compiler.stack_assign(color_in);
2133 compiler.stack_assign(red_out);
2134 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
2136 compiler.stack_assign(green_out);
2137 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
2139 compiler.stack_assign(blue_out);
2140 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
2143 void SeparateRGBNode::compile(OSLCompiler& compiler)
2145 compiler.add(this, "node_separate_rgb");
2152 add_input("Hue", SHADER_SOCKET_FLOAT);
2153 add_input("Saturation", SHADER_SOCKET_FLOAT);
2154 add_input("Value", SHADER_SOCKET_FLOAT);
2155 add_input("Fac", SHADER_SOCKET_FLOAT);
2156 add_input("Color", SHADER_SOCKET_COLOR);
2157 add_output("Color", SHADER_SOCKET_COLOR);
2160 void HSVNode::compile(SVMCompiler& compiler)
2162 ShaderInput *hue_in = input("Hue");
2163 ShaderInput *saturation_in = input("Saturation");
2164 ShaderInput *value_in = input("Value");
2165 ShaderInput *fac_in = input("Fac");
2166 ShaderInput *color_in = input("Color");
2167 ShaderOutput *color_out = output("Color");
2169 compiler.stack_assign(hue_in);
2170 compiler.stack_assign(saturation_in);
2171 compiler.stack_assign(value_in);
2172 compiler.stack_assign(fac_in);
2173 compiler.stack_assign(color_in);
2174 compiler.stack_assign(color_out);
2176 compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
2177 compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2180 void HSVNode::compile(OSLCompiler& compiler)
2182 compiler.add(this, "node_hsv");
2187 AttributeNode::AttributeNode()
2188 : ShaderNode("attribute")
2192 add_output("Color", SHADER_SOCKET_COLOR);
2193 add_output("Vector", SHADER_SOCKET_VECTOR);
2194 add_output("Fac", SHADER_SOCKET_FLOAT);
2197 void AttributeNode::attributes(AttributeRequestSet *attributes)
2199 ShaderOutput *color_out = output("Color");
2200 ShaderOutput *vector_out = output("Vector");
2201 ShaderOutput *fac_out = output("Fac");
2203 if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
2204 attributes->add(attribute);
2206 ShaderNode::attributes(attributes);
2209 void AttributeNode::compile(SVMCompiler& compiler)
2211 ShaderOutput *color_out = output("Color");
2212 ShaderOutput *vector_out = output("Vector");
2213 ShaderOutput *fac_out = output("Fac");
2214 NodeType attr_node = NODE_ATTR;
2216 if(bump == SHADER_BUMP_DX)
2217 attr_node = NODE_ATTR_BUMP_DX;
2218 else if(bump == SHADER_BUMP_DY)
2219 attr_node = NODE_ATTR_BUMP_DY;
2221 if(!color_out->links.empty() || !vector_out->links.empty()) {
2222 int attr = compiler.attribute(attribute);
2224 if(!color_out->links.empty()) {
2225 compiler.stack_assign(color_out);
2226 compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
2228 if(!vector_out->links.empty()) {
2229 compiler.stack_assign(vector_out);
2230 compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
2234 if(!fac_out->links.empty()) {
2235 int attr = compiler.attribute(attribute);
2237 compiler.stack_assign(fac_out);
2238 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
2242 void AttributeNode::compile(OSLCompiler& compiler)
2244 if(bump == SHADER_BUMP_DX)
2245 compiler.parameter("bump_offset", "dx");
2246 else if(bump == SHADER_BUMP_DY)
2247 compiler.parameter("bump_offset", "dy");
2249 compiler.parameter("bump_offset", "center");
2251 compiler.parameter("name", attribute.c_str());
2252 compiler.add(this, "node_attribute");
2257 CameraNode::CameraNode()
2258 : ShaderNode("camera")
2260 add_output("View Vector", SHADER_SOCKET_VECTOR);
2261 add_output("View Z Depth", SHADER_SOCKET_FLOAT);
2262 add_output("View Distance", SHADER_SOCKET_FLOAT);
2265 void CameraNode::compile(SVMCompiler& compiler)
2267 ShaderOutput *vector_out = output("View Vector");
2268 ShaderOutput *z_depth_out = output("View Z Depth");
2269 ShaderOutput *distance_out = output("View Distance");
2271 compiler.stack_assign(vector_out);
2272 compiler.stack_assign(z_depth_out);
2273 compiler.stack_assign(distance_out);
2274 compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
2277 void CameraNode::compile(OSLCompiler& compiler)
2279 compiler.add(this, "node_camera");
2284 FresnelNode::FresnelNode()
2285 : ShaderNode("Fresnel")
2287 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2288 add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
2289 add_output("Fac", SHADER_SOCKET_FLOAT);
2292 void FresnelNode::compile(SVMCompiler& compiler)
2294 ShaderInput *ior_in = input("IOR");
2295 ShaderOutput *fac_out = output("Fac");
2297 compiler.stack_assign(ior_in);
2298 compiler.stack_assign(fac_out);
2299 compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
2302 void FresnelNode::compile(OSLCompiler& compiler)
2304 compiler.add(this, "node_fresnel");
2309 LayerWeightNode::LayerWeightNode()
2310 : ShaderNode("LayerWeight")
2312 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, true);
2313 add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
2315 add_output("Fresnel", SHADER_SOCKET_FLOAT);
2316 add_output("Facing", SHADER_SOCKET_FLOAT);
2319 void LayerWeightNode::compile(SVMCompiler& compiler)
2321 ShaderInput *blend_in = input("Blend");
2324 compiler.stack_assign(blend_in);
2326 ShaderOutput *fresnel_out = output("Fresnel");
2327 if(!fresnel_out->links.empty()) {
2328 compiler.stack_assign(fresnel_out);
2329 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2330 compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, fresnel_out->stack_offset));
2333 ShaderOutput *facing_out = output("Facing");
2334 if(!facing_out->links.empty()) {
2335 compiler.stack_assign(facing_out);
2336 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
2337 compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, facing_out->stack_offset));
2341 void LayerWeightNode::compile(OSLCompiler& compiler)
2343 compiler.add(this, "node_layer_height");
2348 OutputNode::OutputNode()
2349 : ShaderNode("output")
2351 add_input("Surface", SHADER_SOCKET_CLOSURE);
2352 add_input("Volume", SHADER_SOCKET_CLOSURE);
2353 add_input("Displacement", SHADER_SOCKET_FLOAT);
2356 void OutputNode::compile(SVMCompiler& compiler)
2358 if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
2359 ShaderInput *displacement_in = input("Displacement");
2361 if(displacement_in->link) {
2362 compiler.stack_assign(displacement_in);
2363 compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
2368 void OutputNode::compile(OSLCompiler& compiler)
2370 if(compiler.output_type() == SHADER_TYPE_SURFACE)
2371 compiler.add(this, "node_output_surface");
2372 else if(compiler.output_type() == SHADER_TYPE_VOLUME)
2373 compiler.add(this, "node_output_volume");
2374 else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
2375 compiler.add(this, "node_output_displacement");
2380 MathNode::MathNode()
2381 : ShaderNode("math")
2383 type = ustring("Add");
2385 add_input("Value1", SHADER_SOCKET_FLOAT);
2386 add_input("Value2", SHADER_SOCKET_FLOAT);
2387 add_output("Value", SHADER_SOCKET_FLOAT);
2390 static ShaderEnum math_type_init()
2394 enm.insert("Add", NODE_MATH_ADD);
2395 enm.insert("Subtract", NODE_MATH_SUBTRACT);
2396 enm.insert("Multiply", NODE_MATH_MULTIPLY);
2397 enm.insert("Divide", NODE_MATH_DIVIDE);
2398 enm.insert("Sine", NODE_MATH_SINE);
2399 enm.insert("Cosine", NODE_MATH_COSINE);
2400 enm.insert("Tangent", NODE_MATH_TANGENT);
2401 enm.insert("Arcsine", NODE_MATH_ARCSINE);
2402 enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
2403 enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
2404 enm.insert("Power", NODE_MATH_POWER);
2405 enm.insert("Logarithm", NODE_MATH_LOGARITHM);
2406 enm.insert("Minimum", NODE_MATH_MINIMUM);
2407 enm.insert("Maximum", NODE_MATH_MAXIMUM);
2408 enm.insert("Round", NODE_MATH_ROUND);
2409 enm.insert("Less Than", NODE_MATH_LESS_THAN);
2410 enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
2415 ShaderEnum MathNode::type_enum = math_type_init();
2417 void MathNode::compile(SVMCompiler& compiler)
2419 ShaderInput *value1_in = input("Value1");
2420 ShaderInput *value2_in = input("Value2");
2421 ShaderOutput *value_out = output("Value");
2423 compiler.stack_assign(value1_in);
2424 compiler.stack_assign(value2_in);
2425 compiler.stack_assign(value_out);
2427 compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
2428 compiler.add_node(NODE_MATH, value_out->stack_offset);
2431 void MathNode::compile(OSLCompiler& compiler)
2433 compiler.parameter("type", type);
2434 compiler.add(this, "node_math");
2439 VectorMathNode::VectorMathNode()
2440 : ShaderNode("vector_math")
2442 type = ustring("Add");
2444 add_input("Vector1", SHADER_SOCKET_VECTOR);
2445 add_input("Vector2", SHADER_SOCKET_VECTOR);
2446 add_output("Value", SHADER_SOCKET_FLOAT);
2447 add_output("Vector", SHADER_SOCKET_VECTOR);
2450 static ShaderEnum vector_math_type_init()
2454 enm.insert("Add", NODE_VECTOR_MATH_ADD);
2455 enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
2456 enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
2457 enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
2458 enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
2459 enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
2464 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
2466 void VectorMathNode::compile(SVMCompiler& compiler)
2468 ShaderInput *vector1_in = input("Vector1");
2469 ShaderInput *vector2_in = input("Vector2");
2470 ShaderOutput *value_out = output("Value");
2471 ShaderOutput *vector_out = output("Vector");
2473 compiler.stack_assign(vector1_in);
2474 compiler.stack_assign(vector2_in);
2475 compiler.stack_assign(value_out);
2476 compiler.stack_assign(vector_out);
2478 compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
2479 compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
2482 void VectorMathNode::compile(OSLCompiler& compiler)
2484 compiler.parameter("type", type);
2485 compiler.add(this, "node_vector_math");
2490 BumpNode::BumpNode()
2491 : ShaderNode("bump")
2493 add_input("SampleCenter", SHADER_SOCKET_FLOAT);
2494 add_input("SampleX", SHADER_SOCKET_FLOAT);
2495 add_input("SampleY", SHADER_SOCKET_FLOAT);
2497 add_output("Normal", SHADER_SOCKET_NORMAL);
2500 void BumpNode::compile(SVMCompiler& compiler)
2502 ShaderInput *center_in = input("SampleCenter");
2503 ShaderInput *dx_in = input("SampleX");
2504 ShaderInput *dy_in = input("SampleY");
2506 compiler.stack_assign(center_in);
2507 compiler.stack_assign(dx_in);
2508 compiler.stack_assign(dy_in);
2510 compiler.add_node(NODE_SET_BUMP, center_in->stack_offset, dx_in->stack_offset, dy_in->stack_offset);
2513 void BumpNode::compile(OSLCompiler& compiler)
2515 compiler.add(this, "node_bump");
2520 RGBCurvesNode::RGBCurvesNode()
2521 : ShaderNode("rgb_curves")
2523 add_input("Fac", SHADER_SOCKET_FLOAT);
2524 add_input("Color", SHADER_SOCKET_COLOR);
2525 add_output("Color", SHADER_SOCKET_COLOR);
2528 void RGBCurvesNode::compile(SVMCompiler& compiler)
2530 ShaderInput *fac_in = input("Fac");
2531 ShaderInput *color_in = input("Color");
2532 ShaderOutput *color_out = output("Color");
2534 compiler.stack_assign(fac_in);
2535 compiler.stack_assign(color_in);
2536 compiler.stack_assign(color_out);
2538 compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2539 compiler.add_array(curves, RAMP_TABLE_SIZE);
2542 void RGBCurvesNode::compile(OSLCompiler& compiler)
2544 compiler.add(this, "node_rgb_curves");
2549 RGBRampNode::RGBRampNode()
2550 : ShaderNode("rgb_ramp")
2552 add_input("Fac", SHADER_SOCKET_FLOAT);
2553 add_output("Color", SHADER_SOCKET_COLOR);
2554 add_output("Alpha", SHADER_SOCKET_FLOAT);
2557 void RGBRampNode::compile(SVMCompiler& compiler)
2559 ShaderInput *fac_in = input("Fac");
2560 ShaderOutput *color_out = output("Color");
2561 ShaderOutput *alpha_out = output("Alpha");
2563 compiler.stack_assign(fac_in);
2564 if(!color_out->links.empty())
2565 compiler.stack_assign(color_out);
2566 if(!alpha_out->links.empty())
2567 compiler.stack_assign(alpha_out);
2569 compiler.add_node(NODE_RGB_RAMP, fac_in->stack_offset, color_out->stack_offset, alpha_out->stack_offset);
2570 compiler.add_array(ramp, RAMP_TABLE_SIZE);
2573 void RGBRampNode::compile(OSLCompiler& compiler)
2575 compiler.add(this, "node_rgb_ramp");