2 * Copyright 2011-2013 Blender Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
21 #include "sky_model.h"
23 #include "util_foreach.h"
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);
50 Transform TextureMapping::compute_transform()
52 Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
55 mmat[0][x_mapping-1] = 1.0f;
57 mmat[1][y_mapping-1] = 1.0f;
59 mmat[2][z_mapping-1] = 1.0f;
61 float3 scale_clamped = scale;
63 if(type == TEXTURE || type == NORMAL) {
64 /* keep matrix invertible */
65 if(fabsf(scale.x) < 1e-5f)
66 scale_clamped.x = signf(scale.x)*1e-5f;
67 if(fabsf(scale.y) < 1e-5f)
68 scale_clamped.y = signf(scale.y)*1e-5f;
69 if(fabsf(scale.z) < 1e-5f)
70 scale_clamped.z = signf(scale.z)*1e-5f;
73 Transform smat = transform_scale(scale_clamped);
74 Transform rmat = transform_euler(rotation);
75 Transform tmat = transform_translate(translation);
81 /* inverse transform on texture coordinate gives
82 * forward transform on texture */
84 mat = transform_inverse(mat);
91 /* no translation for vectors */
95 /* no translation for normals, and inverse transpose */
97 mat = transform_inverse(mat);
98 mat = transform_transpose(mat);
102 /* projection last */
108 bool TextureMapping::skip()
110 if(translation != make_float3(0.0f, 0.0f, 0.0f))
112 if(rotation != make_float3(0.0f, 0.0f, 0.0f))
114 if(scale != make_float3(1.0f, 1.0f, 1.0f))
117 if(x_mapping != X || y_mapping != Y || z_mapping != Z)
125 void TextureMapping::compile(SVMCompiler& compiler, int offset_in, int offset_out)
127 if(offset_in == SVM_STACK_INVALID || offset_out == SVM_STACK_INVALID)
130 compiler.add_node(NODE_MAPPING, offset_in, offset_out);
132 Transform tfm = compute_transform();
133 compiler.add_node(tfm.x);
134 compiler.add_node(tfm.y);
135 compiler.add_node(tfm.z);
136 compiler.add_node(tfm.w);
139 compiler.add_node(NODE_MIN_MAX, offset_out, offset_out);
140 compiler.add_node(float3_to_float4(min));
141 compiler.add_node(float3_to_float4(max));
145 compiler.add_node(NODE_VECTOR_MATH, NODE_VECTOR_MATH_NORMALIZE, offset_out, offset_out);
146 compiler.add_node(NODE_VECTOR_MATH, SVM_STACK_INVALID, offset_out);
150 void TextureMapping::compile(OSLCompiler &compiler)
153 Transform tfm = transform_transpose(compute_transform());
155 compiler.parameter("mapping", tfm);
156 compiler.parameter("use_mapping", 1);
162 static ShaderEnum color_space_init()
166 enm.insert("None", 0);
167 enm.insert("Color", 1);
172 static ShaderEnum image_projection_init()
176 enm.insert("Flat", 0);
177 enm.insert("Box", 1);
182 ShaderEnum ImageTextureNode::color_space_enum = color_space_init();
183 ShaderEnum ImageTextureNode::projection_enum = image_projection_init();
185 ImageTextureNode::ImageTextureNode()
186 : TextureNode("image_texture")
188 image_manager = NULL;
194 color_space = ustring("Color");
195 projection = ustring("Flat");
196 interpolation = INTERPOLATION_LINEAR;
197 projection_blend = 0.0f;
200 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_UV);
201 add_output("Color", SHADER_SOCKET_COLOR);
202 add_output("Alpha", SHADER_SOCKET_FLOAT);
205 ImageTextureNode::~ImageTextureNode()
208 image_manager->remove_image(filename, builtin_data, interpolation);
211 ShaderNode *ImageTextureNode::clone() const
213 ImageTextureNode *node = new ImageTextureNode(*this);
214 node->image_manager = NULL;
217 node->is_linear = false;
221 void ImageTextureNode::attributes(Shader *shader, AttributeRequestSet *attributes)
224 /* todo: avoid loading other texture coordinates when using ptex,
225 * and hide texture coordinate socket in the UI */
226 if (shader->has_surface && string_endswith(filename, ".ptx")) {
228 attributes->add(ATTR_STD_PTEX_FACE_ID);
229 attributes->add(ATTR_STD_PTEX_UV);
233 ShaderNode::attributes(shader, attributes);
236 void ImageTextureNode::compile(SVMCompiler& compiler)
238 ShaderInput *vector_in = input("Vector");
239 ShaderOutput *color_out = output("Color");
240 ShaderOutput *alpha_out = output("Alpha");
242 image_manager = compiler.image_manager;
245 slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear, interpolation);
246 is_float = (int)is_float_bool;
249 if(!color_out->links.empty())
250 compiler.stack_assign(color_out);
251 if(!alpha_out->links.empty())
252 compiler.stack_assign(alpha_out);
255 compiler.stack_assign(vector_in);
257 int srgb = (is_linear || color_space != "Color")? 0: 1;
258 int vector_offset = vector_in->stack_offset;
260 if(!tex_mapping.skip()) {
261 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
262 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
265 if(projection == "Flat") {
266 compiler.add_node(NODE_TEX_IMAGE,
268 compiler.encode_uchar4(
270 color_out->stack_offset,
271 alpha_out->stack_offset,
275 compiler.add_node(NODE_TEX_IMAGE_BOX,
277 compiler.encode_uchar4(
279 color_out->stack_offset,
280 alpha_out->stack_offset,
282 __float_as_int(projection_blend));
285 if(vector_offset != vector_in->stack_offset)
286 compiler.stack_clear_offset(vector_in->type, vector_offset);
289 /* image not found */
290 if(!color_out->links.empty()) {
291 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
292 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
294 TEX_IMAGE_MISSING_B));
296 if(!alpha_out->links.empty())
297 compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
301 void ImageTextureNode::compile(OSLCompiler& compiler)
303 ShaderOutput *alpha_out = output("Alpha");
305 tex_mapping.compile(compiler);
308 is_float = (int)image_manager->is_float_image(filename, NULL, is_linear);
310 compiler.parameter("filename", filename.c_str());
311 if(is_linear || color_space != "Color")
312 compiler.parameter("color_space", "Linear");
314 compiler.parameter("color_space", "sRGB");
315 compiler.parameter("projection", projection);
316 compiler.parameter("projection_blend", projection_blend);
317 compiler.parameter("is_float", is_float);
318 compiler.parameter("use_alpha", !alpha_out->links.empty());
320 switch (interpolation){
321 case INTERPOLATION_CLOSEST:
322 compiler.parameter("interpolation", "closest");
324 case INTERPOLATION_CUBIC:
325 compiler.parameter("interpolation", "cubic");
327 case INTERPOLATION_SMART:
328 compiler.parameter("interpolation", "smart");
330 case INTERPOLATION_LINEAR:
332 compiler.parameter("interpolation", "linear");
335 compiler.add(this, "node_image_texture");
338 /* Environment Texture */
340 static ShaderEnum env_projection_init()
344 enm.insert("Equirectangular", 0);
345 enm.insert("Mirror Ball", 1);
350 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
351 ShaderEnum EnvironmentTextureNode::projection_enum = env_projection_init();
353 EnvironmentTextureNode::EnvironmentTextureNode()
354 : TextureNode("environment_texture")
356 image_manager = NULL;
362 color_space = ustring("Color");
363 projection = ustring("Equirectangular");
366 add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
367 add_output("Color", SHADER_SOCKET_COLOR);
368 add_output("Alpha", SHADER_SOCKET_FLOAT);
371 EnvironmentTextureNode::~EnvironmentTextureNode()
374 image_manager->remove_image(filename, builtin_data, INTERPOLATION_LINEAR);
377 ShaderNode *EnvironmentTextureNode::clone() const
379 EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
380 node->image_manager = NULL;
383 node->is_linear = false;
387 void EnvironmentTextureNode::attributes(Shader *shader, AttributeRequestSet *attributes)
390 if (shader->has_surface && string_endswith(filename, ".ptx")) {
392 attributes->add(ATTR_STD_PTEX_FACE_ID);
393 attributes->add(ATTR_STD_PTEX_UV);
397 ShaderNode::attributes(shader, attributes);
400 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
402 ShaderInput *vector_in = input("Vector");
403 ShaderOutput *color_out = output("Color");
404 ShaderOutput *alpha_out = output("Alpha");
406 image_manager = compiler.image_manager;
409 slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear, INTERPOLATION_LINEAR);
410 is_float = (int)is_float_bool;
413 if(!color_out->links.empty())
414 compiler.stack_assign(color_out);
415 if(!alpha_out->links.empty())
416 compiler.stack_assign(alpha_out);
419 compiler.stack_assign(vector_in);
421 int srgb = (is_linear || color_space != "Color")? 0: 1;
422 int vector_offset = vector_in->stack_offset;
424 if(!tex_mapping.skip()) {
425 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
426 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
429 compiler.add_node(NODE_TEX_ENVIRONMENT,
431 compiler.encode_uchar4(
433 color_out->stack_offset,
434 alpha_out->stack_offset,
436 projection_enum[projection]);
438 if(vector_offset != vector_in->stack_offset)
439 compiler.stack_clear_offset(vector_in->type, vector_offset);
442 /* image not found */
443 if(!color_out->links.empty()) {
444 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
445 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
447 TEX_IMAGE_MISSING_B));
449 if(!alpha_out->links.empty())
450 compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
454 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
456 ShaderOutput *alpha_out = output("Alpha");
458 tex_mapping.compile(compiler);
461 is_float = (int)image_manager->is_float_image(filename, NULL, is_linear);
463 compiler.parameter("filename", filename.c_str());
464 compiler.parameter("projection", projection);
465 if(is_linear || color_space != "Color")
466 compiler.parameter("color_space", "Linear");
468 compiler.parameter("color_space", "sRGB");
469 compiler.parameter("is_float", is_float);
470 compiler.parameter("use_alpha", !alpha_out->links.empty());
471 compiler.add(this, "node_environment_texture");
476 static float2 sky_spherical_coordinates(float3 dir)
478 return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
481 typedef struct SunSky {
482 /* sun direction in spherical and cartesian */
486 float radiance_x, radiance_y, radiance_z;
487 float config_x[9], config_y[9], config_z[9];
491 static float sky_perez_function(float lam[6], float theta, float gamma)
493 return (1.0f + lam[0]*expf(lam[1]/cosf(theta))) * (1.0f + lam[2]*expf(lam[3]*gamma) + lam[4]*cosf(gamma)*cosf(gamma));
496 static void sky_texture_precompute_old(SunSky *sunsky, float3 dir, float turbidity)
499 * We re-use the SunSky struct of the new model, to avoid extra variables
500 * zenith_Y/x/y is now radiance_x/y/z
501 * perez_Y/x/y is now config_x/y/z
504 float2 spherical = sky_spherical_coordinates(dir);
505 float theta = spherical.x;
506 float phi = spherical.y;
508 sunsky->theta = theta;
511 float theta2 = theta*theta;
512 float theta3 = theta2*theta;
516 float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
517 sunsky->radiance_x = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
518 sunsky->radiance_x *= 0.06f;
521 (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
522 (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
523 (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
526 (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
527 (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) * T +
528 (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
530 sunsky->config_x[0] = (0.1787f * T - 1.4630f);
531 sunsky->config_x[1] = (-0.3554f * T + 0.4275f);
532 sunsky->config_x[2] = (-0.0227f * T + 5.3251f);
533 sunsky->config_x[3] = (0.1206f * T - 2.5771f);
534 sunsky->config_x[4] = (-0.0670f * T + 0.3703f);
536 sunsky->config_y[0] = (-0.0193f * T - 0.2592f);
537 sunsky->config_y[1] = (-0.0665f * T + 0.0008f);
538 sunsky->config_y[2] = (-0.0004f * T + 0.2125f);
539 sunsky->config_y[3] = (-0.0641f * T - 0.8989f);
540 sunsky->config_y[4] = (-0.0033f * T + 0.0452f);
542 sunsky->config_z[0] = (-0.0167f * T - 0.2608f);
543 sunsky->config_z[1] = (-0.0950f * T + 0.0092f);
544 sunsky->config_z[2] = (-0.0079f * T + 0.2102f);
545 sunsky->config_z[3] = (-0.0441f * T - 1.6537f);
546 sunsky->config_z[4] = (-0.0109f * T + 0.0529f);
548 /* unused for old sky model */
549 for(int i = 5; i < 9; i++) {
550 sunsky->config_x[i] = 0.0f;
551 sunsky->config_y[i] = 0.0f;
552 sunsky->config_z[i] = 0.0f;
555 sunsky->radiance_x /= sky_perez_function(sunsky->config_x, 0, theta);
556 sunsky->radiance_y /= sky_perez_function(sunsky->config_y, 0, theta);
557 sunsky->radiance_z /= sky_perez_function(sunsky->config_z, 0, theta);
561 static void sky_texture_precompute_new(SunSky *sunsky, float3 dir, float turbidity, float ground_albedo)
563 /* Calculate Sun Direction and save coordinates */
564 float2 spherical = sky_spherical_coordinates(dir);
565 float theta = spherical.x;
566 float phi = spherical.y;
568 /* Clamp Turbidity */
569 turbidity = clamp(turbidity, 0.0f, 10.0f);
571 /* Clamp to Horizon */
572 theta = clamp(theta, 0.0f, M_PI_2_F);
574 sunsky->theta = theta;
577 double solarElevation = M_PI_2_F - theta;
579 /* Initialize Sky Model */
580 ArHosekSkyModelState *sky_state;
581 sky_state = arhosek_xyz_skymodelstate_alloc_init(turbidity, ground_albedo, solarElevation);
583 /* Copy values from sky_state to SunSky */
584 for (int i = 0; i < 9; ++i) {
585 sunsky->config_x[i] = sky_state->configs[0][i];
586 sunsky->config_y[i] = sky_state->configs[1][i];
587 sunsky->config_z[i] = sky_state->configs[2][i];
589 sunsky->radiance_x = sky_state->radiances[0];
590 sunsky->radiance_y = sky_state->radiances[1];
591 sunsky->radiance_z = sky_state->radiances[2];
594 arhosekskymodelstate_free(sky_state);
597 static ShaderEnum sky_type_init()
601 enm.insert("Preetham", NODE_SKY_OLD);
602 enm.insert("Hosek / Wilkie", NODE_SKY_NEW);
607 ShaderEnum SkyTextureNode::type_enum = sky_type_init();
609 SkyTextureNode::SkyTextureNode()
610 : TextureNode("sky_texture")
612 type = ustring("Hosek / Wilkie");
614 sun_direction = make_float3(0.0f, 0.0f, 1.0f);
616 ground_albedo = 0.3f;
618 add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
619 add_output("Color", SHADER_SOCKET_COLOR);
622 void SkyTextureNode::compile(SVMCompiler& compiler)
624 ShaderInput *vector_in = input("Vector");
625 ShaderOutput *color_out = output("Color");
628 if(type_enum[type] == NODE_SKY_OLD)
629 sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
630 else if(type_enum[type] == NODE_SKY_NEW)
631 sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
634 compiler.stack_assign(vector_in);
636 int vector_offset = vector_in->stack_offset;
637 int sky_model = type_enum[type];
639 if(!tex_mapping.skip()) {
640 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
641 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
644 compiler.stack_assign(color_out);
645 compiler.add_node(NODE_TEX_SKY, vector_offset, color_out->stack_offset, sky_model);
646 compiler.add_node(__float_as_uint(sunsky.phi), __float_as_uint(sunsky.theta), __float_as_uint(sunsky.radiance_x), __float_as_uint(sunsky.radiance_y));
647 compiler.add_node(__float_as_uint(sunsky.radiance_z), __float_as_uint(sunsky.config_x[0]), __float_as_uint(sunsky.config_x[1]), __float_as_uint(sunsky.config_x[2]));
648 compiler.add_node(__float_as_uint(sunsky.config_x[3]), __float_as_uint(sunsky.config_x[4]), __float_as_uint(sunsky.config_x[5]), __float_as_uint(sunsky.config_x[6]));
649 compiler.add_node(__float_as_uint(sunsky.config_x[7]), __float_as_uint(sunsky.config_x[8]), __float_as_uint(sunsky.config_y[0]), __float_as_uint(sunsky.config_y[1]));
650 compiler.add_node(__float_as_uint(sunsky.config_y[2]), __float_as_uint(sunsky.config_y[3]), __float_as_uint(sunsky.config_y[4]), __float_as_uint(sunsky.config_y[5]));
651 compiler.add_node(__float_as_uint(sunsky.config_y[6]), __float_as_uint(sunsky.config_y[7]), __float_as_uint(sunsky.config_y[8]), __float_as_uint(sunsky.config_z[0]));
652 compiler.add_node(__float_as_uint(sunsky.config_z[1]), __float_as_uint(sunsky.config_z[2]), __float_as_uint(sunsky.config_z[3]), __float_as_uint(sunsky.config_z[4]));
653 compiler.add_node(__float_as_uint(sunsky.config_z[5]), __float_as_uint(sunsky.config_z[6]), __float_as_uint(sunsky.config_z[7]), __float_as_uint(sunsky.config_z[8]));
655 if(vector_offset != vector_in->stack_offset)
656 compiler.stack_clear_offset(vector_in->type, vector_offset);
659 void SkyTextureNode::compile(OSLCompiler& compiler)
661 tex_mapping.compile(compiler);
665 if(type_enum[type] == NODE_SKY_OLD)
666 sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
667 else if(type_enum[type] == NODE_SKY_NEW)
668 sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
670 compiler.parameter("sky_model", type);
671 compiler.parameter("theta", sunsky.theta);
672 compiler.parameter("phi", sunsky.phi);
673 compiler.parameter_color("radiance", make_float3(sunsky.radiance_x, sunsky.radiance_y, sunsky.radiance_z));
674 compiler.parameter_array("config_x", sunsky.config_x, 9);
675 compiler.parameter_array("config_y", sunsky.config_y, 9);
676 compiler.parameter_array("config_z", sunsky.config_z, 9);
677 compiler.add(this, "node_sky_texture");
680 /* Gradient Texture */
682 static ShaderEnum gradient_type_init()
686 enm.insert("Linear", NODE_BLEND_LINEAR);
687 enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
688 enm.insert("Easing", NODE_BLEND_EASING);
689 enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
690 enm.insert("Radial", NODE_BLEND_RADIAL);
691 enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
692 enm.insert("Spherical", NODE_BLEND_SPHERICAL);
697 ShaderEnum GradientTextureNode::type_enum = gradient_type_init();
699 GradientTextureNode::GradientTextureNode()
700 : TextureNode("gradient_texture")
702 type = ustring("Linear");
704 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
705 add_output("Color", SHADER_SOCKET_COLOR);
706 add_output("Fac", SHADER_SOCKET_FLOAT);
709 void GradientTextureNode::compile(SVMCompiler& compiler)
711 ShaderInput *vector_in = input("Vector");
712 ShaderOutput *color_out = output("Color");
713 ShaderOutput *fac_out = output("Fac");
715 if(vector_in->link) compiler.stack_assign(vector_in);
717 int vector_offset = vector_in->stack_offset;
719 if(!tex_mapping.skip()) {
720 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
721 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
724 if(!fac_out->links.empty())
725 compiler.stack_assign(fac_out);
726 if(!color_out->links.empty())
727 compiler.stack_assign(color_out);
729 compiler.add_node(NODE_TEX_GRADIENT,
730 compiler.encode_uchar4(type_enum[type], vector_offset, fac_out->stack_offset, color_out->stack_offset));
732 if(vector_offset != vector_in->stack_offset)
733 compiler.stack_clear_offset(vector_in->type, vector_offset);
736 void GradientTextureNode::compile(OSLCompiler& compiler)
738 tex_mapping.compile(compiler);
740 compiler.parameter("Type", type);
741 compiler.add(this, "node_gradient_texture");
746 NoiseTextureNode::NoiseTextureNode()
747 : TextureNode("noise_texture")
749 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
750 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
751 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
752 add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
754 add_output("Color", SHADER_SOCKET_COLOR);
755 add_output("Fac", SHADER_SOCKET_FLOAT);
758 void NoiseTextureNode::compile(SVMCompiler& compiler)
760 ShaderInput *distortion_in = input("Distortion");
761 ShaderInput *detail_in = input("Detail");
762 ShaderInput *scale_in = input("Scale");
763 ShaderInput *vector_in = input("Vector");
764 ShaderOutput *color_out = output("Color");
765 ShaderOutput *fac_out = output("Fac");
767 if(vector_in->link) compiler.stack_assign(vector_in);
768 if(scale_in->link) compiler.stack_assign(scale_in);
769 if(detail_in->link) compiler.stack_assign(detail_in);
770 if(distortion_in->link) compiler.stack_assign(distortion_in);
772 int vector_offset = vector_in->stack_offset;
774 if(!tex_mapping.skip()) {
775 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
776 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
779 if(!fac_out->links.empty())
780 compiler.stack_assign(fac_out);
781 if(!color_out->links.empty())
782 compiler.stack_assign(color_out);
784 compiler.add_node(NODE_TEX_NOISE,
785 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
786 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset));
788 __float_as_int(scale_in->value.x),
789 __float_as_int(detail_in->value.x),
790 __float_as_int(distortion_in->value.x));
792 if(vector_offset != vector_in->stack_offset)
793 compiler.stack_clear_offset(vector_in->type, vector_offset);
796 void NoiseTextureNode::compile(OSLCompiler& compiler)
798 tex_mapping.compile(compiler);
800 compiler.add(this, "node_noise_texture");
803 /* Voronoi Texture */
805 static ShaderEnum voronoi_coloring_init()
809 enm.insert("Intensity", NODE_VORONOI_INTENSITY);
810 enm.insert("Cells", NODE_VORONOI_CELLS);
815 ShaderEnum VoronoiTextureNode::coloring_enum = voronoi_coloring_init();
817 VoronoiTextureNode::VoronoiTextureNode()
818 : TextureNode("voronoi_texture")
820 coloring = ustring("Intensity");
822 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
823 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
825 add_output("Color", SHADER_SOCKET_COLOR);
826 add_output("Fac", SHADER_SOCKET_FLOAT);
829 void VoronoiTextureNode::compile(SVMCompiler& compiler)
831 ShaderInput *scale_in = input("Scale");
832 ShaderInput *vector_in = input("Vector");
833 ShaderOutput *color_out = output("Color");
834 ShaderOutput *fac_out = output("Fac");
836 if(vector_in->link) compiler.stack_assign(vector_in);
837 if(scale_in->link) compiler.stack_assign(scale_in);
839 int vector_offset = vector_in->stack_offset;
841 if(!tex_mapping.skip()) {
842 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
843 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
846 compiler.stack_assign(color_out);
847 compiler.stack_assign(fac_out);
849 compiler.add_node(NODE_TEX_VORONOI,
850 coloring_enum[coloring],
851 compiler.encode_uchar4(scale_in->stack_offset, vector_offset, fac_out->stack_offset, color_out->stack_offset),
852 __float_as_int(scale_in->value.x));
854 if(vector_offset != vector_in->stack_offset)
855 compiler.stack_clear_offset(vector_in->type, vector_offset);
858 void VoronoiTextureNode::compile(OSLCompiler& compiler)
860 tex_mapping.compile(compiler);
862 compiler.parameter("Coloring", coloring);
863 compiler.add(this, "node_voronoi_texture");
866 /* Musgrave Texture */
868 static ShaderEnum musgrave_type_init()
872 enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
873 enm.insert("fBM", NODE_MUSGRAVE_FBM);
874 enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
875 enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
876 enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
881 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
883 MusgraveTextureNode::MusgraveTextureNode()
884 : TextureNode("musgrave_texture")
886 type = ustring("fBM");
888 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
889 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
890 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
891 add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
892 add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
893 add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
894 add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
896 add_output("Fac", SHADER_SOCKET_FLOAT);
897 add_output("Color", SHADER_SOCKET_COLOR);
900 void MusgraveTextureNode::compile(SVMCompiler& compiler)
902 ShaderInput *vector_in = input("Vector");
903 ShaderInput *scale_in = input("Scale");
904 ShaderInput *dimension_in = input("Dimension");
905 ShaderInput *lacunarity_in = input("Lacunarity");
906 ShaderInput *detail_in = input("Detail");
907 ShaderInput *offset_in = input("Offset");
908 ShaderInput *gain_in = input("Gain");
909 ShaderOutput *fac_out = output("Fac");
910 ShaderOutput *color_out = output("Color");
912 if(vector_in->link) compiler.stack_assign(vector_in);
913 if(dimension_in->link) compiler.stack_assign(dimension_in);
914 if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
915 if(detail_in->link) compiler.stack_assign(detail_in);
916 if(offset_in->link) compiler.stack_assign(offset_in);
917 if(gain_in->link) compiler.stack_assign(gain_in);
918 if(scale_in->link) compiler.stack_assign(scale_in);
920 int vector_offset = vector_in->stack_offset;
922 if(!tex_mapping.skip()) {
923 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
924 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
927 if(!fac_out->links.empty())
928 compiler.stack_assign(fac_out);
929 if(!color_out->links.empty())
930 compiler.stack_assign(color_out);
932 compiler.add_node(NODE_TEX_MUSGRAVE,
933 compiler.encode_uchar4(type_enum[type], vector_offset, color_out->stack_offset, fac_out->stack_offset),
934 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, detail_in->stack_offset, offset_in->stack_offset),
935 compiler.encode_uchar4(gain_in->stack_offset, scale_in->stack_offset));
936 compiler.add_node(__float_as_int(dimension_in->value.x),
937 __float_as_int(lacunarity_in->value.x),
938 __float_as_int(detail_in->value.x),
939 __float_as_int(offset_in->value.x));
940 compiler.add_node(__float_as_int(gain_in->value.x),
941 __float_as_int(scale_in->value.x));
943 if(vector_offset != vector_in->stack_offset)
944 compiler.stack_clear_offset(vector_in->type, vector_offset);
947 void MusgraveTextureNode::compile(OSLCompiler& compiler)
949 tex_mapping.compile(compiler);
951 compiler.parameter("Type", type);
953 compiler.add(this, "node_musgrave_texture");
958 static ShaderEnum wave_type_init()
962 enm.insert("Bands", NODE_WAVE_BANDS);
963 enm.insert("Rings", NODE_WAVE_RINGS);
968 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
970 WaveTextureNode::WaveTextureNode()
971 : TextureNode("wave_texture")
973 type = ustring("Bands");
975 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
976 add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
977 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
978 add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
979 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
981 add_output("Color", SHADER_SOCKET_COLOR);
982 add_output("Fac", SHADER_SOCKET_FLOAT);
985 void WaveTextureNode::compile(SVMCompiler& compiler)
987 ShaderInput *scale_in = input("Scale");
988 ShaderInput *distortion_in = input("Distortion");
989 ShaderInput *dscale_in = input("Detail Scale");
990 ShaderInput *detail_in = input("Detail");
991 ShaderInput *vector_in = input("Vector");
992 ShaderOutput *fac_out = output("Fac");
993 ShaderOutput *color_out = output("Color");
995 if(scale_in->link) compiler.stack_assign(scale_in);
996 if(detail_in->link) compiler.stack_assign(detail_in);
997 if(distortion_in->link) compiler.stack_assign(distortion_in);
998 if(dscale_in->link) compiler.stack_assign(dscale_in);
999 if(vector_in->link) compiler.stack_assign(vector_in);
1001 int vector_offset = vector_in->stack_offset;
1003 if(!tex_mapping.skip()) {
1004 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1005 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1008 if(!fac_out->links.empty())
1009 compiler.stack_assign(fac_out);
1010 if(!color_out->links.empty())
1011 compiler.stack_assign(color_out);
1013 compiler.add_node(NODE_TEX_WAVE,
1014 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
1015 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset));
1018 __float_as_int(scale_in->value.x),
1019 __float_as_int(detail_in->value.x),
1020 __float_as_int(distortion_in->value.x),
1021 __float_as_int(dscale_in->value.x));
1023 if(vector_offset != vector_in->stack_offset)
1024 compiler.stack_clear_offset(vector_in->type, vector_offset);
1027 void WaveTextureNode::compile(OSLCompiler& compiler)
1029 tex_mapping.compile(compiler);
1031 compiler.parameter("Type", type);
1033 compiler.add(this, "node_wave_texture");
1038 MagicTextureNode::MagicTextureNode()
1039 : TextureNode("magic_texture")
1043 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1044 add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
1045 add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
1047 add_output("Color", SHADER_SOCKET_COLOR);
1048 add_output("Fac", SHADER_SOCKET_FLOAT);
1051 void MagicTextureNode::compile(SVMCompiler& compiler)
1053 ShaderInput *vector_in = input("Vector");
1054 ShaderInput *scale_in = input("Scale");
1055 ShaderInput *distortion_in = input("Distortion");
1056 ShaderOutput *color_out = output("Color");
1057 ShaderOutput *fac_out = output("Fac");
1059 if(vector_in->link) compiler.stack_assign(vector_in);
1060 if(distortion_in->link) compiler.stack_assign(distortion_in);
1061 if(scale_in->link) compiler.stack_assign(scale_in);
1063 int vector_offset = vector_in->stack_offset;
1065 if(!tex_mapping.skip()) {
1066 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1067 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1070 if(!fac_out->links.empty())
1071 compiler.stack_assign(fac_out);
1072 if(!color_out->links.empty())
1073 compiler.stack_assign(color_out);
1075 compiler.add_node(NODE_TEX_MAGIC,
1076 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
1077 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, distortion_in->stack_offset));
1079 __float_as_int(scale_in->value.x),
1080 __float_as_int(distortion_in->value.x));
1082 if(vector_offset != vector_in->stack_offset)
1083 compiler.stack_clear_offset(vector_in->type, vector_offset);
1086 void MagicTextureNode::compile(OSLCompiler& compiler)
1088 tex_mapping.compile(compiler);
1090 compiler.parameter("Depth", depth);
1091 compiler.add(this, "node_magic_texture");
1094 /* Checker Texture */
1096 CheckerTextureNode::CheckerTextureNode()
1097 : TextureNode("checker_texture")
1099 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1100 add_input("Color1", SHADER_SOCKET_COLOR);
1101 add_input("Color2", SHADER_SOCKET_COLOR);
1102 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
1104 add_output("Color", SHADER_SOCKET_COLOR);
1105 add_output("Fac", SHADER_SOCKET_FLOAT);
1108 void CheckerTextureNode::compile(SVMCompiler& compiler)
1110 ShaderInput *vector_in = input("Vector");
1111 ShaderInput *color1_in = input("Color1");
1112 ShaderInput *color2_in = input("Color2");
1113 ShaderInput *scale_in = input("Scale");
1115 ShaderOutput *color_out = output("Color");
1116 ShaderOutput *fac_out = output("Fac");
1118 compiler.stack_assign(vector_in);
1119 compiler.stack_assign(color1_in);
1120 compiler.stack_assign(color2_in);
1121 if(scale_in->link) compiler.stack_assign(scale_in);
1123 int vector_offset = vector_in->stack_offset;
1125 if(!tex_mapping.skip()) {
1126 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1127 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1130 if(!color_out->links.empty())
1131 compiler.stack_assign(color_out);
1132 if(!fac_out->links.empty())
1133 compiler.stack_assign(fac_out);
1135 compiler.add_node(NODE_TEX_CHECKER,
1136 compiler.encode_uchar4(vector_offset, color1_in->stack_offset, color2_in->stack_offset, scale_in->stack_offset),
1137 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset),
1138 __float_as_int(scale_in->value.x));
1140 if(vector_offset != vector_in->stack_offset)
1141 compiler.stack_clear_offset(vector_in->type, vector_offset);
1144 void CheckerTextureNode::compile(OSLCompiler& compiler)
1146 tex_mapping.compile(compiler);
1148 compiler.add(this, "node_checker_texture");
1153 BrickTextureNode::BrickTextureNode()
1154 : TextureNode("brick_texture")
1157 offset_frequency = 2;
1159 squash_frequency = 2;
1161 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1162 add_input("Color1", SHADER_SOCKET_COLOR);
1163 add_input("Color2", SHADER_SOCKET_COLOR);
1164 add_input("Mortar", SHADER_SOCKET_COLOR);
1165 add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
1166 add_input("Mortar Size", SHADER_SOCKET_FLOAT, 0.02f);
1167 add_input("Bias", SHADER_SOCKET_FLOAT, 0.0f);
1168 add_input("Brick Width", SHADER_SOCKET_FLOAT, 0.5f);
1169 add_input("Row Height", SHADER_SOCKET_FLOAT, 0.25f);
1171 add_output("Color", SHADER_SOCKET_COLOR);
1172 add_output("Fac", SHADER_SOCKET_FLOAT);
1175 void BrickTextureNode::compile(SVMCompiler& compiler)
1177 ShaderInput *vector_in = input("Vector");
1178 ShaderInput *color1_in = input("Color1");
1179 ShaderInput *color2_in = input("Color2");
1180 ShaderInput *mortar_in = input("Mortar");
1181 ShaderInput *scale_in = input("Scale");
1182 ShaderInput *mortar_size_in = input("Mortar Size");
1183 ShaderInput *bias_in = input("Bias");
1184 ShaderInput *brick_width_in = input("Brick Width");
1185 ShaderInput *row_height_in = input("Row Height");
1187 ShaderOutput *color_out = output("Color");
1188 ShaderOutput *fac_out = output("Fac");
1190 compiler.stack_assign(vector_in);
1191 compiler.stack_assign(color1_in);
1192 compiler.stack_assign(color2_in);
1193 compiler.stack_assign(mortar_in);
1194 if(scale_in->link) compiler.stack_assign(scale_in);
1195 if(mortar_size_in->link) compiler.stack_assign(mortar_size_in);
1196 if(bias_in->link) compiler.stack_assign(bias_in);
1197 if(brick_width_in->link) compiler.stack_assign(brick_width_in);
1198 if(row_height_in->link) compiler.stack_assign(row_height_in);
1200 int vector_offset = vector_in->stack_offset;
1202 if(!tex_mapping.skip()) {
1203 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1204 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1207 if(!color_out->links.empty())
1208 compiler.stack_assign(color_out);
1209 if(!fac_out->links.empty())
1210 compiler.stack_assign(fac_out);
1212 compiler.add_node(NODE_TEX_BRICK,
1213 compiler.encode_uchar4(vector_offset,
1214 color1_in->stack_offset, color2_in->stack_offset, mortar_in->stack_offset),
1215 compiler.encode_uchar4(scale_in->stack_offset,
1216 mortar_size_in->stack_offset, bias_in->stack_offset, brick_width_in->stack_offset),
1217 compiler.encode_uchar4(row_height_in->stack_offset,
1218 color_out->stack_offset, fac_out->stack_offset));
1220 compiler.add_node(compiler.encode_uchar4(offset_frequency, squash_frequency),
1221 __float_as_int(scale_in->value.x),
1222 __float_as_int(mortar_size_in->value.x),
1223 __float_as_int(bias_in->value.x));
1225 compiler.add_node(__float_as_int(brick_width_in->value.x),
1226 __float_as_int(row_height_in->value.x),
1227 __float_as_int(offset),
1228 __float_as_int(squash));
1230 if(vector_offset != vector_in->stack_offset)
1231 compiler.stack_clear_offset(vector_in->type, vector_offset);
1234 void BrickTextureNode::compile(OSLCompiler& compiler)
1236 tex_mapping.compile(compiler);
1238 compiler.parameter("Offset", offset);
1239 compiler.parameter("OffsetFrequency", offset_frequency);
1240 compiler.parameter("Squash", squash);
1241 compiler.parameter("SquashFrequency", squash_frequency);
1242 compiler.add(this, "node_brick_texture");
1247 NormalNode::NormalNode()
1248 : ShaderNode("normal")
1250 direction = make_float3(0.0f, 0.0f, 1.0f);
1252 add_input("Normal", SHADER_SOCKET_NORMAL);
1253 add_output("Normal", SHADER_SOCKET_NORMAL);
1254 add_output("Dot", SHADER_SOCKET_FLOAT);
1257 void NormalNode::compile(SVMCompiler& compiler)
1259 ShaderInput *normal_in = input("Normal");
1260 ShaderOutput *normal_out = output("Normal");
1261 ShaderOutput *dot_out = output("Dot");
1263 compiler.stack_assign(normal_in);
1264 compiler.stack_assign(normal_out);
1265 compiler.stack_assign(dot_out);
1267 compiler.add_node(NODE_NORMAL, normal_in->stack_offset, normal_out->stack_offset, dot_out->stack_offset);
1269 __float_as_int(direction.x),
1270 __float_as_int(direction.y),
1271 __float_as_int(direction.z));
1274 void NormalNode::compile(OSLCompiler& compiler)
1276 compiler.parameter_normal("Direction", direction);
1277 compiler.add(this, "node_normal");
1282 MappingNode::MappingNode()
1283 : ShaderNode("mapping")
1285 add_input("Vector", SHADER_SOCKET_POINT);
1286 add_output("Vector", SHADER_SOCKET_POINT);
1289 void MappingNode::compile(SVMCompiler& compiler)
1291 ShaderInput *vector_in = input("Vector");
1292 ShaderOutput *vector_out = output("Vector");
1294 compiler.stack_assign(vector_in);
1295 compiler.stack_assign(vector_out);
1297 tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
1300 void MappingNode::compile(OSLCompiler& compiler)
1302 Transform tfm = transform_transpose(tex_mapping.compute_transform());
1303 compiler.parameter("Matrix", tfm);
1304 compiler.parameter_point("mapping_min", tex_mapping.min);
1305 compiler.parameter_point("mapping_max", tex_mapping.max);
1306 compiler.parameter("use_minmax", tex_mapping.use_minmax);
1308 compiler.add(this, "node_mapping");
1313 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_, bool autoconvert)
1314 : ShaderNode("convert")
1320 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1324 if(from == SHADER_SOCKET_FLOAT)
1325 add_input("Val", SHADER_SOCKET_FLOAT);
1326 else if(from == SHADER_SOCKET_INT)
1327 add_input("ValInt", SHADER_SOCKET_INT);
1328 else if(from == SHADER_SOCKET_COLOR)
1329 add_input("Color", SHADER_SOCKET_COLOR);
1330 else if(from == SHADER_SOCKET_VECTOR)
1331 add_input("Vector", SHADER_SOCKET_VECTOR);
1332 else if(from == SHADER_SOCKET_POINT)
1333 add_input("Point", SHADER_SOCKET_POINT);
1334 else if(from == SHADER_SOCKET_NORMAL)
1335 add_input("Normal", SHADER_SOCKET_NORMAL);
1336 else if(from == SHADER_SOCKET_STRING)
1337 add_input("String", SHADER_SOCKET_STRING);
1341 if(to == SHADER_SOCKET_FLOAT)
1342 add_output("Val", SHADER_SOCKET_FLOAT);
1343 else if(to == SHADER_SOCKET_INT)
1344 add_output("ValInt", SHADER_SOCKET_INT);
1345 else if(to == SHADER_SOCKET_COLOR)
1346 add_output("Color", SHADER_SOCKET_COLOR);
1347 else if(to == SHADER_SOCKET_VECTOR)
1348 add_output("Vector", SHADER_SOCKET_VECTOR);
1349 else if(to == SHADER_SOCKET_POINT)
1350 add_output("Point", SHADER_SOCKET_POINT);
1351 else if(to == SHADER_SOCKET_NORMAL)
1352 add_output("Normal", SHADER_SOCKET_NORMAL);
1353 else if(to == SHADER_SOCKET_STRING)
1354 add_output("String", SHADER_SOCKET_STRING);
1359 void ConvertNode::compile(SVMCompiler& compiler)
1361 ShaderInput *in = inputs[0];
1362 ShaderOutput *out = outputs[0];
1364 if(from == SHADER_SOCKET_FLOAT) {
1365 compiler.stack_assign(in);
1366 compiler.stack_assign(out);
1368 if(to == SHADER_SOCKET_INT)
1370 compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, in->stack_offset, out->stack_offset);
1372 /* float to float3 */
1373 compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
1375 else if(from == SHADER_SOCKET_INT) {
1376 compiler.stack_assign(in);
1377 compiler.stack_assign(out);
1379 if(to == SHADER_SOCKET_FLOAT)
1381 compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, in->stack_offset, out->stack_offset);
1383 /* int to vector/point/normal */
1384 compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, in->stack_offset, out->stack_offset);
1386 else if(to == SHADER_SOCKET_FLOAT) {
1387 compiler.stack_assign(in);
1388 compiler.stack_assign(out);
1390 if(from == SHADER_SOCKET_COLOR)
1391 /* color to float */
1392 compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
1394 /* vector/point/normal to float */
1395 compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
1397 else if(to == SHADER_SOCKET_INT) {
1398 compiler.stack_assign(in);
1399 compiler.stack_assign(out);
1401 if(from == SHADER_SOCKET_COLOR)
1403 compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, in->stack_offset, out->stack_offset);
1405 /* vector/point/normal to int */
1406 compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, in->stack_offset, out->stack_offset);
1409 /* float3 to float3 */
1412 compiler.stack_link(in, out);
1415 /* set 0,0,0 value */
1416 compiler.stack_assign(in);
1417 compiler.stack_assign(out);
1419 compiler.add_node(NODE_VALUE_V, in->stack_offset);
1420 compiler.add_node(NODE_VALUE_V, in->value);
1425 void ConvertNode::compile(OSLCompiler& compiler)
1427 if(from == SHADER_SOCKET_FLOAT)
1428 compiler.add(this, "node_convert_from_float");
1429 else if(from == SHADER_SOCKET_INT)
1430 compiler.add(this, "node_convert_from_int");
1431 else if(from == SHADER_SOCKET_COLOR)
1432 compiler.add(this, "node_convert_from_color");
1433 else if(from == SHADER_SOCKET_VECTOR)
1434 compiler.add(this, "node_convert_from_vector");
1435 else if(from == SHADER_SOCKET_POINT)
1436 compiler.add(this, "node_convert_from_point");
1437 else if(from == SHADER_SOCKET_NORMAL)
1438 compiler.add(this, "node_convert_from_normal");
1445 ProxyNode::ProxyNode(ShaderSocketType type_)
1446 : ShaderNode("proxy")
1449 special_type = SHADER_SPECIAL_TYPE_PROXY;
1451 add_input("Input", type);
1452 add_output("Output", type);
1455 void ProxyNode::compile(SVMCompiler& compiler)
1459 void ProxyNode::compile(OSLCompiler& compiler)
1465 BsdfNode::BsdfNode(bool scattering_)
1466 : ShaderNode("bsdf"), scattering(scattering_)
1468 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1469 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
1470 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1473 closure = CLOSURE_BSSRDF_CUBIC_ID;
1474 add_output("BSSRDF", SHADER_SOCKET_CLOSURE);
1477 closure = CLOSURE_BSDF_DIFFUSE_ID;
1478 add_output("BSDF", SHADER_SOCKET_CLOSURE);
1482 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3, ShaderInput *param4)
1484 ShaderInput *color_in = input("Color");
1485 ShaderInput *normal_in = input("Normal");
1486 ShaderInput *tangent_in = input("Tangent");
1488 if(color_in->link) {
1489 compiler.stack_assign(color_in);
1490 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1493 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1496 compiler.stack_assign(param1);
1498 compiler.stack_assign(param2);
1500 compiler.stack_assign(param3);
1502 compiler.stack_assign(param4);
1505 compiler.stack_assign(normal_in);
1507 if(tangent_in && tangent_in->link)
1508 compiler.stack_assign(tangent_in);
1510 compiler.add_node(NODE_CLOSURE_BSDF,
1511 compiler.encode_uchar4(closure,
1512 (param1)? param1->stack_offset: SVM_STACK_INVALID,
1513 (param2)? param2->stack_offset: SVM_STACK_INVALID,
1514 compiler.closure_mix_weight_offset()),
1515 __float_as_int((param1)? param1->value.x: 0.0f),
1516 __float_as_int((param2)? param2->value.x: 0.0f));
1519 compiler.add_node(normal_in->stack_offset, tangent_in->stack_offset,
1520 (param3)? param3->stack_offset: SVM_STACK_INVALID,
1521 (param4)? param4->stack_offset: SVM_STACK_INVALID);
1524 compiler.add_node(normal_in->stack_offset, SVM_STACK_INVALID,
1525 (param3)? param3->stack_offset: SVM_STACK_INVALID,
1526 (param4)? param4->stack_offset: SVM_STACK_INVALID);
1530 void BsdfNode::compile(SVMCompiler& compiler)
1532 compile(compiler, NULL, NULL);
1535 void BsdfNode::compile(OSLCompiler& compiler)
1540 /* Ward BSDF Closure */
1542 WardBsdfNode::WardBsdfNode()
1544 closure = CLOSURE_BSDF_WARD_ID;
1546 add_input("Tangent", SHADER_SOCKET_VECTOR, ShaderInput::TANGENT);
1548 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1549 add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.5f);
1550 add_input("Rotation", SHADER_SOCKET_FLOAT, 0.0f);
1553 void WardBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
1555 if(shader->has_surface) {
1556 ShaderInput *tangent_in = input("Tangent");
1558 if(!tangent_in->link)
1559 attributes->add(ATTR_STD_GENERATED);
1562 ShaderNode::attributes(shader, attributes);
1565 void WardBsdfNode::compile(SVMCompiler& compiler)
1567 BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1570 void WardBsdfNode::compile(OSLCompiler& compiler)
1572 compiler.add(this, "node_ward_bsdf");
1575 /* Glossy BSDF Closure */
1577 static ShaderEnum glossy_distribution_init()
1581 enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1582 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1583 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1588 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1590 GlossyBsdfNode::GlossyBsdfNode()
1592 distribution = ustring("Beckmann");
1594 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1597 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1599 closure = (ClosureType)distribution_enum[distribution];
1601 if(closure == CLOSURE_BSDF_REFLECTION_ID)
1602 BsdfNode::compile(compiler, NULL, NULL);
1604 BsdfNode::compile(compiler, input("Roughness"), NULL);
1607 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1609 compiler.parameter("distribution", distribution);
1610 compiler.add(this, "node_glossy_bsdf");
1613 /* Glass BSDF Closure */
1615 static ShaderEnum glass_distribution_init()
1619 enm.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1620 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1621 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1626 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1628 GlassBsdfNode::GlassBsdfNode()
1630 distribution = ustring("Sharp");
1632 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1633 add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1636 void GlassBsdfNode::compile(SVMCompiler& compiler)
1638 closure = (ClosureType)distribution_enum[distribution];
1640 if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
1641 BsdfNode::compile(compiler, NULL, input("IOR"));
1643 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1646 void GlassBsdfNode::compile(OSLCompiler& compiler)
1648 compiler.parameter("distribution", distribution);
1649 compiler.add(this, "node_glass_bsdf");
1652 /* Refraction BSDF Closure */
1654 static ShaderEnum refraction_distribution_init()
1658 enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1659 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1660 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1665 ShaderEnum RefractionBsdfNode::distribution_enum = refraction_distribution_init();
1667 RefractionBsdfNode::RefractionBsdfNode()
1669 distribution = ustring("Sharp");
1671 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1672 add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1675 void RefractionBsdfNode::compile(SVMCompiler& compiler)
1677 closure = (ClosureType)distribution_enum[distribution];
1679 if(closure == CLOSURE_BSDF_REFRACTION_ID)
1680 BsdfNode::compile(compiler, NULL, input("IOR"));
1682 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1685 void RefractionBsdfNode::compile(OSLCompiler& compiler)
1687 compiler.parameter("distribution", distribution);
1688 compiler.add(this, "node_refraction_bsdf");
1691 /* Toon BSDF Closure */
1693 static ShaderEnum toon_component_init()
1697 enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
1698 enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
1703 ShaderEnum ToonBsdfNode::component_enum = toon_component_init();
1705 ToonBsdfNode::ToonBsdfNode()
1707 component = ustring("Diffuse");
1709 add_input("Size", SHADER_SOCKET_FLOAT, 0.5f);
1710 add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1713 void ToonBsdfNode::compile(SVMCompiler& compiler)
1715 closure = (ClosureType)component_enum[component];
1717 BsdfNode::compile(compiler, input("Size"), input("Smooth"));
1720 void ToonBsdfNode::compile(OSLCompiler& compiler)
1722 compiler.parameter("component", component);
1723 compiler.add(this, "node_toon_bsdf");
1726 /* Velvet BSDF Closure */
1728 VelvetBsdfNode::VelvetBsdfNode()
1730 closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1732 add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1735 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1737 BsdfNode::compile(compiler, input("Sigma"), NULL);
1740 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1742 compiler.add(this, "node_velvet_bsdf");
1745 /* Diffuse BSDF Closure */
1747 DiffuseBsdfNode::DiffuseBsdfNode()
1749 closure = CLOSURE_BSDF_DIFFUSE_ID;
1750 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1753 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1755 BsdfNode::compile(compiler, input("Roughness"), NULL);
1758 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1760 compiler.add(this, "node_diffuse_bsdf");
1763 /* Translucent BSDF Closure */
1765 TranslucentBsdfNode::TranslucentBsdfNode()
1767 closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1770 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1772 BsdfNode::compile(compiler, NULL, NULL);
1775 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1777 compiler.add(this, "node_translucent_bsdf");
1780 /* Transparent BSDF Closure */
1782 TransparentBsdfNode::TransparentBsdfNode()
1784 name = "transparent";
1785 closure = CLOSURE_BSDF_TRANSPARENT_ID;
1788 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1790 BsdfNode::compile(compiler, NULL, NULL);
1793 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1795 compiler.add(this, "node_transparent_bsdf");
1798 /* Subsurface Scattering Closure */
1800 static ShaderEnum subsurface_falloff_init()
1804 enm.insert("Cubic", CLOSURE_BSSRDF_CUBIC_ID);
1805 enm.insert("Gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
1810 ShaderEnum SubsurfaceScatteringNode::falloff_enum = subsurface_falloff_init();
1812 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
1815 name = "subsurface_scattering";
1816 closure = CLOSURE_BSSRDF_CUBIC_ID;
1818 add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
1819 add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
1820 add_input("Sharpness", SHADER_SOCKET_FLOAT, 0.0f);
1821 add_input("Texture Blur", SHADER_SOCKET_FLOAT, 1.0f);
1824 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
1826 BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
1829 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
1831 compiler.parameter("Falloff", falloff_enum[closure]);
1832 compiler.add(this, "node_subsurface_scattering");
1835 bool SubsurfaceScatteringNode::has_bssrdf_bump()
1837 /* detect if anything is plugged into the normal input besides the default */
1838 ShaderInput *normal_in = input("Normal");
1839 return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
1842 /* Emissive Closure */
1844 EmissionNode::EmissionNode()
1845 : ShaderNode("emission")
1847 total_power = false;
1849 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1850 add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1851 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1853 add_output("Emission", SHADER_SOCKET_CLOSURE);
1856 void EmissionNode::compile(SVMCompiler& compiler)
1858 ShaderInput *color_in = input("Color");
1859 ShaderInput *strength_in = input("Strength");
1861 if(color_in->link || strength_in->link) {
1862 compiler.stack_assign(color_in);
1863 compiler.stack_assign(strength_in);
1864 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1866 else if(total_power)
1867 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1869 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1871 compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1874 void EmissionNode::compile(OSLCompiler& compiler)
1876 compiler.parameter("TotalPower", (total_power)? 1: 0);
1877 compiler.add(this, "node_emission");
1880 /* Background Closure */
1882 BackgroundNode::BackgroundNode()
1883 : ShaderNode("background")
1885 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1886 add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1887 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1889 add_output("Background", SHADER_SOCKET_CLOSURE);
1892 void BackgroundNode::compile(SVMCompiler& compiler)
1894 ShaderInput *color_in = input("Color");
1895 ShaderInput *strength_in = input("Strength");
1897 if(color_in->link || strength_in->link) {
1898 compiler.stack_assign(color_in);
1899 compiler.stack_assign(strength_in);
1900 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1903 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1905 compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1908 void BackgroundNode::compile(OSLCompiler& compiler)
1910 compiler.add(this, "node_background");
1913 /* Holdout Closure */
1915 HoldoutNode::HoldoutNode()
1916 : ShaderNode("holdout")
1918 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1919 add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1921 add_output("Holdout", SHADER_SOCKET_CLOSURE);
1924 void HoldoutNode::compile(SVMCompiler& compiler)
1926 float3 value = make_float3(1.0f, 1.0f, 1.0f);
1928 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
1929 compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1932 void HoldoutNode::compile(OSLCompiler& compiler)
1934 compiler.add(this, "node_holdout");
1937 /* Ambient Occlusion */
1939 AmbientOcclusionNode::AmbientOcclusionNode()
1940 : ShaderNode("ambient_occlusion")
1942 add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1943 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1944 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1946 add_output("AO", SHADER_SOCKET_CLOSURE);
1949 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
1951 ShaderInput *color_in = input("Color");
1953 if(color_in->link) {
1954 compiler.stack_assign(color_in);
1955 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1958 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1960 compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
1963 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
1965 compiler.add(this, "node_ambient_occlusion");
1968 /* Volume Closure */
1970 VolumeNode::VolumeNode()
1971 : ShaderNode("volume")
1973 closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
1975 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1976 add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1977 add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1979 add_output("Volume", SHADER_SOCKET_CLOSURE);
1982 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1984 ShaderInput *color_in = input("Color");
1986 if(color_in->link) {
1987 compiler.stack_assign(color_in);
1988 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1991 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1994 compiler.stack_assign(param1);
1996 compiler.stack_assign(param2);
1998 compiler.add_node(NODE_CLOSURE_VOLUME,
1999 compiler.encode_uchar4(closure,
2000 (param1)? param1->stack_offset: SVM_STACK_INVALID,
2001 (param2)? param2->stack_offset: SVM_STACK_INVALID,
2002 compiler.closure_mix_weight_offset()),
2003 __float_as_int((param1)? param1->value.x: 0.0f),
2004 __float_as_int((param2)? param2->value.x: 0.0f));
2007 void VolumeNode::compile(SVMCompiler& compiler)
2009 compile(compiler, NULL, NULL);
2012 void VolumeNode::compile(OSLCompiler& compiler)
2017 /* Absorption Volume Closure */
2019 AbsorptionVolumeNode::AbsorptionVolumeNode()
2021 closure = CLOSURE_VOLUME_ABSORPTION_ID;
2024 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2026 VolumeNode::compile(compiler, input("Density"), NULL);
2029 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2031 compiler.add(this, "node_absorption_volume");
2034 /* Scatter Volume Closure */
2036 ScatterVolumeNode::ScatterVolumeNode()
2038 closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2040 add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.0f);
2043 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2045 VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2048 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2050 compiler.add(this, "node_scatter_volume");
2053 /* Hair BSDF Closure */
2055 static ShaderEnum hair_component_init()
2059 enm.insert("Reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2060 enm.insert("Transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2065 ShaderEnum HairBsdfNode::component_enum = hair_component_init();
2067 HairBsdfNode::HairBsdfNode()
2069 component = ustring("Reflection");
2071 add_input("Offset", SHADER_SOCKET_FLOAT);
2072 add_input("RoughnessU", SHADER_SOCKET_FLOAT);
2073 add_input("RoughnessV", SHADER_SOCKET_FLOAT);
2076 void HairBsdfNode::compile(SVMCompiler& compiler)
2078 closure = (ClosureType)component_enum[component];
2080 BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
2083 void HairBsdfNode::compile(OSLCompiler& compiler)
2085 compiler.parameter("component", component);
2087 compiler.add(this, "node_hair_bsdf");
2092 GeometryNode::GeometryNode()
2093 : ShaderNode("geometry")
2095 special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2097 add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2098 add_output("Position", SHADER_SOCKET_POINT);
2099 add_output("Normal", SHADER_SOCKET_NORMAL);
2100 add_output("Tangent", SHADER_SOCKET_NORMAL);
2101 add_output("True Normal", SHADER_SOCKET_NORMAL);
2102 add_output("Incoming", SHADER_SOCKET_VECTOR);
2103 add_output("Parametric", SHADER_SOCKET_POINT);
2104 add_output("Backfacing", SHADER_SOCKET_FLOAT);
2107 void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2109 if(shader->has_surface) {
2110 if(!output("Tangent")->links.empty())
2111 attributes->add(ATTR_STD_GENERATED);
2114 ShaderNode::attributes(shader, attributes);
2117 void GeometryNode::compile(SVMCompiler& compiler)
2120 NodeType geom_node = NODE_GEOMETRY;
2122 if(bump == SHADER_BUMP_DX)
2123 geom_node = NODE_GEOMETRY_BUMP_DX;
2124 else if(bump == SHADER_BUMP_DY)
2125 geom_node = NODE_GEOMETRY_BUMP_DY;
2127 out = output("Position");
2128 if(!out->links.empty()) {
2129 compiler.stack_assign(out);
2130 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2133 out = output("Normal");
2134 if(!out->links.empty()) {
2135 compiler.stack_assign(out);
2136 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
2139 out = output("Tangent");
2140 if(!out->links.empty()) {
2141 compiler.stack_assign(out);
2142 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
2145 out = output("True Normal");
2146 if(!out->links.empty()) {
2147 compiler.stack_assign(out);
2148 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
2151 out = output("Incoming");
2152 if(!out->links.empty()) {
2153 compiler.stack_assign(out);
2154 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2157 out = output("Parametric");
2158 if(!out->links.empty()) {
2159 compiler.stack_assign(out);
2160 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
2163 out = output("Backfacing");
2164 if(!out->links.empty()) {
2165 compiler.stack_assign(out);
2166 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
2170 void GeometryNode::compile(OSLCompiler& compiler)
2172 if(bump == SHADER_BUMP_DX)
2173 compiler.parameter("bump_offset", "dx");
2174 else if(bump == SHADER_BUMP_DY)
2175 compiler.parameter("bump_offset", "dy");
2177 compiler.parameter("bump_offset", "center");
2179 compiler.add(this, "node_geometry");
2182 /* TextureCoordinate */
2184 TextureCoordinateNode::TextureCoordinateNode()
2185 : ShaderNode("texture_coordinate")
2187 add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2188 add_output("Generated", SHADER_SOCKET_POINT);
2189 add_output("Normal", SHADER_SOCKET_NORMAL);
2190 add_output("UV", SHADER_SOCKET_POINT);
2191 add_output("Object", SHADER_SOCKET_POINT);
2192 add_output("Camera", SHADER_SOCKET_POINT);
2193 add_output("Window", SHADER_SOCKET_POINT);
2194 add_output("Reflection", SHADER_SOCKET_NORMAL);
2199 void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2201 if(shader->has_surface) {
2203 if(!output("Generated")->links.empty())
2204 attributes->add(ATTR_STD_GENERATED);
2205 if(!output("UV")->links.empty())
2206 attributes->add(ATTR_STD_UV);
2210 if(shader->has_volume) {
2212 if(!output("Generated")->links.empty()) {
2213 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
2218 ShaderNode::attributes(shader, attributes);
2221 void TextureCoordinateNode::compile(SVMCompiler& compiler)
2224 NodeType texco_node = NODE_TEX_COORD;
2225 NodeType attr_node = NODE_ATTR;
2226 NodeType geom_node = NODE_GEOMETRY;
2228 if(bump == SHADER_BUMP_DX) {
2229 texco_node = NODE_TEX_COORD_BUMP_DX;
2230 attr_node = NODE_ATTR_BUMP_DX;
2231 geom_node = NODE_GEOMETRY_BUMP_DX;
2233 else if(bump == SHADER_BUMP_DY) {
2234 texco_node = NODE_TEX_COORD_BUMP_DY;
2235 attr_node = NODE_ATTR_BUMP_DY;
2236 geom_node = NODE_GEOMETRY_BUMP_DY;
2239 out = output("Generated");
2240 if(!out->links.empty()) {
2241 if(compiler.background) {
2242 compiler.stack_assign(out);
2243 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2247 compiler.stack_assign(out);
2248 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
2250 else if(compiler.output_type() == SHADER_TYPE_VOLUME) {
2251 compiler.stack_assign(out);
2252 compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, out->stack_offset);
2255 int attr = compiler.attribute(ATTR_STD_GENERATED);
2256 compiler.stack_assign(out);
2257 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2262 out = output("Normal");
2263 if(!out->links.empty()) {
2264 compiler.stack_assign(out);
2265 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
2269 if(!out->links.empty()) {
2271 compiler.stack_assign(out);
2272 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
2275 int attr = compiler.attribute(ATTR_STD_UV);
2276 compiler.stack_assign(out);
2277 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2281 out = output("Object");
2282 if(!out->links.empty()) {
2283 compiler.stack_assign(out);
2284 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
2287 out = output("Camera");
2288 if(!out->links.empty()) {
2289 compiler.stack_assign(out);
2290 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
2293 out = output("Window");
2294 if(!out->links.empty()) {
2295 compiler.stack_assign(out);
2296 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
2299 out = output("Reflection");
2300 if(!out->links.empty()) {
2301 if(compiler.background) {
2302 compiler.stack_assign(out);
2303 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2306 compiler.stack_assign(out);
2307 compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
2312 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2314 if(bump == SHADER_BUMP_DX)
2315 compiler.parameter("bump_offset", "dx");
2316 else if(bump == SHADER_BUMP_DY)
2317 compiler.parameter("bump_offset", "dy");
2319 compiler.parameter("bump_offset", "center");
2321 if(compiler.background)
2322 compiler.parameter("is_background", true);
2323 if(compiler.output_type() == SHADER_TYPE_VOLUME)
2324 compiler.parameter("is_volume", true);
2326 compiler.parameter("from_dupli", from_dupli);
2328 compiler.add(this, "node_texture_coordinate");
2331 UVMapNode::UVMapNode()
2332 : ShaderNode("uvmap")
2337 add_output("UV", SHADER_SOCKET_POINT);
2340 void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2342 if(shader->has_surface) {
2344 if(!output("UV")->links.empty()) {
2345 if (attribute != "")
2346 attributes->add(attribute);
2348 attributes->add(ATTR_STD_UV);
2353 ShaderNode::attributes(shader, attributes);
2356 void UVMapNode::compile(SVMCompiler& compiler)
2358 ShaderOutput *out = output("UV");
2359 NodeType texco_node = NODE_TEX_COORD;
2360 NodeType attr_node = NODE_ATTR;
2363 if(!out->links.empty()) {
2365 compiler.stack_assign(out);
2366 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
2369 if (attribute != "")
2370 attr = compiler.attribute(attribute);
2372 attr = compiler.attribute(ATTR_STD_UV);
2374 compiler.stack_assign(out);
2375 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2380 void UVMapNode::compile(OSLCompiler& compiler)
2382 compiler.parameter("from_dupli", from_dupli);
2383 compiler.parameter("name", attribute.c_str());
2384 compiler.add(this, "node_uv_map");
2389 LightPathNode::LightPathNode()
2390 : ShaderNode("light_path")
2392 add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
2393 add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
2394 add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
2395 add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
2396 add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
2397 add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
2398 add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
2399 add_output("Is Volume Scatter Ray", SHADER_SOCKET_FLOAT);
2400 add_output("Ray Length", SHADER_SOCKET_FLOAT);
2401 add_output("Ray Depth", SHADER_SOCKET_FLOAT);
2404 void LightPathNode::compile(SVMCompiler& compiler)
2408 out = output("Is Camera Ray");
2409 if(!out->links.empty()) {
2410 compiler.stack_assign(out);
2411 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2414 out = output("Is Shadow Ray");
2415 if(!out->links.empty()) {
2416 compiler.stack_assign(out);
2417 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2420 out = output("Is Diffuse Ray");
2421 if(!out->links.empty()) {
2422 compiler.stack_assign(out);
2423 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2426 out = output("Is Glossy Ray");
2427 if(!out->links.empty()) {
2428 compiler.stack_assign(out);
2429 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2432 out = output("Is Singular Ray");
2433 if(!out->links.empty()) {
2434 compiler.stack_assign(out);
2435 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2438 out = output("Is Reflection Ray");
2439 if(!out->links.empty()) {
2440 compiler.stack_assign(out);
2441 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2445 out = output("Is Transmission Ray");
2446 if(!out->links.empty()) {
2447 compiler.stack_assign(out);
2448 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2451 out = output("Is Volume Scatter Ray");
2452 if(!out->links.empty()) {
2453 compiler.stack_assign(out);
2454 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, out->stack_offset);
2457 out = output("Ray Length");
2458 if(!out->links.empty()) {
2459 compiler.stack_assign(out);
2460 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2463 out = output("Ray Depth");
2464 if(!out->links.empty()) {
2465 compiler.stack_assign(out);
2466 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, out->stack_offset);
2471 void LightPathNode::compile(OSLCompiler& compiler)
2473 compiler.add(this, "node_light_path");
2478 LightFalloffNode::LightFalloffNode()
2479 : ShaderNode("light_fallof")
2481 add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2482 add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2483 add_output("Quadratic", SHADER_SOCKET_FLOAT);
2484 add_output("Linear", SHADER_SOCKET_FLOAT);
2485 add_output("Constant", SHADER_SOCKET_FLOAT);
2488 void LightFalloffNode::compile(SVMCompiler& compiler)
2490 ShaderInput *strength_in = input("Strength");
2491 ShaderInput *smooth_in = input("Smooth");
2493 compiler.stack_assign(strength_in);
2494 compiler.stack_assign(smooth_in);
2496 ShaderOutput *out = output("Quadratic");
2497 if(!out->links.empty()) {
2498 compiler.stack_assign(out);
2499 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2500 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2503 out = output("Linear");
2504 if(!out->links.empty()) {
2505 compiler.stack_assign(out);
2506 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2507 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2510 out = output("Constant");
2511 if(!out->links.empty()) {
2512 compiler.stack_assign(out);
2513 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2514 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2518 void LightFalloffNode::compile(OSLCompiler& compiler)
2520 compiler.add(this, "node_light_falloff");
2525 ObjectInfoNode::ObjectInfoNode()
2526 : ShaderNode("object_info")
2528 add_output("Location", SHADER_SOCKET_VECTOR);
2529 add_output("Object Index", SHADER_SOCKET_FLOAT);
2530 add_output("Material Index", SHADER_SOCKET_FLOAT);
2531 add_output("Random", SHADER_SOCKET_FLOAT);
2534 void ObjectInfoNode::compile(SVMCompiler& compiler)
2536 ShaderOutput *out = output("Location");
2537 if(!out->links.empty()) {
2538 compiler.stack_assign(out);
2539 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2542 out = output("Object Index");
2543 if(!out->links.empty()) {
2544 compiler.stack_assign(out);
2545 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2548 out = output("Material Index");
2549 if(!out->links.empty()) {
2550 compiler.stack_assign(out);
2551 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2554 out = output("Random");
2555 if(!out->links.empty()) {
2556 compiler.stack_assign(out);
2557 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2561 void ObjectInfoNode::compile(OSLCompiler& compiler)
2563 compiler.add(this, "node_object_info");
2568 ParticleInfoNode::ParticleInfoNode()
2569 : ShaderNode("particle_info")
2571 add_output("Index", SHADER_SOCKET_FLOAT);
2572 add_output("Age", SHADER_SOCKET_FLOAT);
2573 add_output("Lifetime", SHADER_SOCKET_FLOAT);
2574 add_output("Location", SHADER_SOCKET_POINT);
2575 #if 0 /* not yet supported */
2576 add_output("Rotation", SHADER_SOCKET_QUATERNION);
2578 add_output("Size", SHADER_SOCKET_FLOAT);
2579 add_output("Velocity", SHADER_SOCKET_VECTOR);
2580 add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2583 void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2585 if(!output("Index")->links.empty())
2586 attributes->add(ATTR_STD_PARTICLE);
2587 if(!output("Age")->links.empty())
2588 attributes->add(ATTR_STD_PARTICLE);
2589 if(!output("Lifetime")->links.empty())
2590 attributes->add(ATTR_STD_PARTICLE);
2591 if(!output("Location")->links.empty())
2592 attributes->add(ATTR_STD_PARTICLE);
2593 #if 0 /* not yet supported */
2594 if(!output("Rotation")->links.empty())
2595 attributes->add(ATTR_STD_PARTICLE);
2597 if(!output("Size")->links.empty())
2598 attributes->add(ATTR_STD_PARTICLE);
2599 if(!output("Velocity")->links.empty())
2600 attributes->add(ATTR_STD_PARTICLE);
2601 if(!output("Angular Velocity")->links.empty())
2602 attributes->add(ATTR_STD_PARTICLE);
2604 ShaderNode::attributes(shader, attributes);
2607 void ParticleInfoNode::compile(SVMCompiler& compiler)
2611 out = output("Index");
2612 if(!out->links.empty()) {
2613 compiler.stack_assign(out);
2614 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2617 out = output("Age");
2618 if(!out->links.empty()) {
2619 compiler.stack_assign(out);
2620 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2623 out = output("Lifetime");
2624 if(!out->links.empty()) {
2625 compiler.stack_assign(out);
2626 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2629 out = output("Location");
2630 if(!out->links.empty()) {
2631 compiler.stack_assign(out);
2632 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2635 /* quaternion data is not yet supported by Cycles */
2637 out = output("Rotation");
2638 if(!out->links.empty()) {
2639 compiler.stack_assign(out);
2640 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2644 out = output("Size");
2645 if(!out->links.empty()) {
2646 compiler.stack_assign(out);
2647 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2650 out = output("Velocity");
2651 if(!out->links.empty()) {
2652 compiler.stack_assign(out);
2653 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2656 out = output("Angular Velocity");
2657 if(!out->links.empty()) {
2658 compiler.stack_assign(out);
2659 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2663 void ParticleInfoNode::compile(OSLCompiler& compiler)
2665 compiler.add(this, "node_particle_info");
2670 HairInfoNode::HairInfoNode()
2671 : ShaderNode("hair_info")
2673 add_output("Is Strand", SHADER_SOCKET_FLOAT);
2674 add_output("Intercept", SHADER_SOCKET_FLOAT);
2675 add_output("Thickness", SHADER_SOCKET_FLOAT);
2676 add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2677 /*output for minimum hair width transparency - deactivated*/
2678 /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2681 void HairInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
2683 if(shader->has_surface) {
2684 ShaderOutput *intercept_out = output("Intercept");
2686 if(!intercept_out->links.empty())
2687 attributes->add(ATTR_STD_CURVE_INTERCEPT);
2690 ShaderNode::attributes(shader, attributes);
2693 void HairInfoNode::compile(SVMCompiler& compiler)
2697 out = output("Is Strand");
2698 if(!out->links.empty()) {
2699 compiler.stack_assign(out);
2700 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2703 out = output("Intercept");
2704 if(!out->links.empty()) {
2705 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2706 compiler.stack_assign(out);
2707 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2710 out = output("Thickness");
2711 if(!out->links.empty()) {
2712 compiler.stack_assign(out);
2713 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2716 out = output("Tangent Normal");
2717 if(!out->links.empty()) {
2718 compiler.stack_assign(out);
2719 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2722 /*out = output("Fade");
2723 if(!out->links.empty()) {
2724 compiler.stack_assign(out);
2725 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2730 void HairInfoNode::compile(OSLCompiler& compiler)
2732 compiler.add(this, "node_hair_info");
2737 ValueNode::ValueNode()
2738 : ShaderNode("value")
2742 add_output("Value", SHADER_SOCKET_FLOAT);
2745 void ValueNode::compile(SVMCompiler& compiler)
2747 ShaderOutput *val_out = output("Value");
2749 compiler.stack_assign(val_out);
2750 compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2753 void ValueNode::compile(OSLCompiler& compiler)
2755 compiler.parameter("value_value", value);
2756 compiler.add(this, "node_value");
2761 ColorNode::ColorNode()
2762 : ShaderNode("color")
2764 value = make_float3(0.0f, 0.0f, 0.0f);
2766 add_output("Color", SHADER_SOCKET_COLOR);
2769 void ColorNode::compile(SVMCompiler& compiler)
2771 ShaderOutput *color_out = output("Color");
2773 if(color_out && !color_out->links.empty()) {
2774 compiler.stack_assign(color_out);
2775 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2776 compiler.add_node(NODE_VALUE_V, value);
2780 void ColorNode::compile(OSLCompiler& compiler)
2782 compiler.parameter_color("color_value", value);
2784 compiler.add(this, "node_value");
2789 AddClosureNode::AddClosureNode()
2790 : ShaderNode("add_closure")
2792 add_input("Closure1", SHADER_SOCKET_CLOSURE);
2793 add_input("Closure2", SHADER_SOCKET_CLOSURE);
2794 add_output("Closure", SHADER_SOCKET_CLOSURE);
2797 void AddClosureNode::compile(SVMCompiler& compiler)
2799 /* handled in the SVM compiler */
2802 void AddClosureNode::compile(OSLCompiler& compiler)
2804 compiler.add(this, "node_add_closure");
2809 MixClosureNode::MixClosureNode()
2810 : ShaderNode("mix_closure")
2812 special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2814 add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2815 add_input("Closure1", SHADER_SOCKET_CLOSURE);
2816 add_input("Closure2", SHADER_SOCKET_CLOSURE);
2817 add_output("Closure", SHADER_SOCKET_CLOSURE);
2820 void MixClosureNode::compile(SVMCompiler& compiler)
2822 /* handled in the SVM compiler */
2825 void MixClosureNode::compile(OSLCompiler& compiler)
2827 compiler.add(this, "node_mix_closure");
2832 MixClosureWeightNode::MixClosureWeightNode()
2833 : ShaderNode("mix_closure_weight")
2835 add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2836 add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2837 add_output("Weight1", SHADER_SOCKET_FLOAT);
2838 add_output("Weight2", SHADER_SOCKET_FLOAT);
2841 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2843 ShaderInput *weight_in = input("Weight");
2844 ShaderInput *fac_in = input("Fac");
2845 ShaderOutput *weight1_out = output("Weight1");
2846 ShaderOutput *weight2_out = output("Weight2");
2848 compiler.stack_assign(weight_in);
2849 compiler.stack_assign(fac_in);
2850 compiler.stack_assign(weight1_out);
2851 compiler.stack_assign(weight2_out);
2853 compiler.add_node(NODE_MIX_CLOSURE,
2854 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2855 weight1_out->stack_offset, weight2_out->stack_offset));
2858 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2865 InvertNode::InvertNode()
2866 : ShaderNode("invert")
2868 add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2869 add_input("Color", SHADER_SOCKET_COLOR);
2870 add_output("Color", SHADER_SOCKET_COLOR);
2873 void InvertNode::compile(SVMCompiler& compiler)
2875 ShaderInput *fac_in = input("Fac");
2876 ShaderInput *color_in = input("Color");
2877 ShaderOutput *color_out = output("Color");
2879 compiler.stack_assign(fac_in);
2880 compiler.stack_assign(color_in);
2881 compiler.stack_assign(color_out);
2883 compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2886 void InvertNode::compile(OSLCompiler& compiler)
2888 compiler.add(this, "node_invert");
2896 type = ustring("Mix");
2900 add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2901 add_input("Color1", SHADER_SOCKET_COLOR);
2902 add_input("Color2", SHADER_SOCKET_COLOR);
2903 add_output("Color", SHADER_SOCKET_COLOR);
2906 static ShaderEnum mix_type_init()
2910 enm.insert("Mix", NODE_MIX_BLEND);
2911 enm.insert("Add", NODE_MIX_ADD);
2912 enm.insert("Multiply", NODE_MIX_MUL);
2913 enm.insert("Screen", NODE_MIX_SCREEN);
2914 enm.insert("Overlay", NODE_MIX_OVERLAY);
2915 enm.insert("Subtract", NODE_MIX_SUB);
2916 enm.insert("Divide", NODE_MIX_DIV);
2917 enm.insert("Difference", NODE_MIX_DIFF);
2918 enm.insert("Darken", NODE_MIX_DARK);
2919 enm.insert("Lighten", NODE_MIX_LIGHT);
2920 enm.insert("Dodge", NODE_MIX_DODGE);
2921 enm.insert("Burn", NODE_MIX_BURN);
2922 enm.insert("Hue", NODE_MIX_HUE);
2923 enm.insert("Saturation", NODE_MIX_SAT);
2924 enm.insert("Value", NODE_MIX_VAL);
2925 enm.insert("Color", NODE_MIX_COLOR);
2926 enm.insert("Soft Light", NODE_MIX_SOFT);
2927 enm.insert("Linear Light", NODE_MIX_LINEAR);
2932 ShaderEnum MixNode::type_enum = mix_type_init();
2934 void MixNode::compile(SVMCompiler& compiler)
2936 ShaderInput *fac_in = input("Fac");
2937 ShaderInput *color1_in = input("Color1");
2938 ShaderInput *color2_in = input("Color2");
2939 ShaderOutput *color_out = output("Color");
2941 compiler.stack_assign(fac_in);
2942 compiler.stack_assign(color1_in);
2943 compiler.stack_assign(color2_in);
2944 compiler.stack_assign(color_out);
2946 compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2947 compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2950 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2951 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2955 void MixNode::compile(OSLCompiler& compiler)
2957 compiler.parameter("type", type);
2958 compiler.parameter("Clamp", use_clamp);
2959 compiler.add(this, "node_mix");
2963 CombineRGBNode::CombineRGBNode()
2964 : ShaderNode("combine_rgb")
2966 add_input("R", SHADER_SOCKET_FLOAT);
2967 add_input("G", SHADER_SOCKET_FLOAT);
2968 add_input("B", SHADER_SOCKET_FLOAT);
2969 add_output("Image", SHADER_SOCKET_COLOR);
2972 void CombineRGBNode::compile(SVMCompiler& compiler)
2974 ShaderInput *red_in = input("R");
2975 ShaderInput *green_in = input("G");
2976 ShaderInput *blue_in = input("B");
2977 ShaderOutput *color_out = output("Image");
2979 compiler.stack_assign(color_out);
2981 compiler.stack_assign(red_in);
2982 compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2984 compiler.stack_assign(green_in);
2985 compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2987 compiler.stack_assign(blue_in);
2988 compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2991 void CombineRGBNode::compile(OSLCompiler& compiler)
2993 compiler.add(this, "node_combine_rgb");
2997 CombineHSVNode::CombineHSVNode()
2998 : ShaderNode("combine_hsv")
3000 add_input("H", SHADER_SOCKET_FLOAT);
3001 add_input("S", SHADER_SOCKET_FLOAT);
3002 add_input("V", SHADER_SOCKET_FLOAT);
3003 add_output("Color", SHADER_SOCKET_COLOR);
3006 void CombineHSVNode::compile(SVMCompiler& compiler)
3008 ShaderInput *hue_in = input("H");
3009 ShaderInput *saturation_in = input("S");
3010 ShaderInput *value_in = input("V");
3011 ShaderOutput *color_out = output("Color");
3013 compiler.stack_assign(color_out);
3014 compiler.stack_assign(hue_in);
3015 compiler.stack_assign(saturation_in);
3016 compiler.stack_assign(value_in);
3018 compiler.add_node(NODE_COMBINE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3019 compiler.add_node(NODE_COMBINE_HSV, color_out->stack_offset);
3022 void CombineHSVNode::compile(OSLCompiler& compiler)
3024 compiler.add(this, "node_combine_hsv");
3028 GammaNode::GammaNode()
3029 : ShaderNode("gamma")
3031 add_input("Color", SHADER_SOCKET_COLOR);
3032 add_input("Gamma", SHADER_SOCKET_FLOAT);
3033 add_output("Color", SHADER_SOCKET_COLOR);
3036 void GammaNode::compile(SVMCompiler& compiler)
3038 ShaderInput *color_in = input("Color");
3039 ShaderInput *gamma_in = input("Gamma");
3040 ShaderOutput *color_out = output("Color");
3042 compiler.stack_assign(color_in);
3043 compiler.stack_assign(gamma_in);
3044 compiler.stack_assign(color_out);
3046 compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3049 void GammaNode::compile(OSLCompiler& compiler)
3051 compiler.add(this, "node_gamma");
3054 /* Bright Contrast */
3055 BrightContrastNode::BrightContrastNode()
3056 : ShaderNode("brightness")
3058 add_input("Color", SHADER_SOCKET_COLOR);
3059 add_input("Bright", SHADER_SOCKET_FLOAT);
3060 add_input("Contrast", SHADER_SOCKET_FLOAT);
3061 add_output("Color", SHADER_SOCKET_COLOR);
3064 void BrightContrastNode::compile(SVMCompiler& compiler)
3066 ShaderInput *color_in = input("Color");
3067 ShaderInput *bright_in = input("Bright");
3068 ShaderInput *contrast_in = input("Contrast");
3069 ShaderOutput *color_out = output("Color");
3071 compiler.stack_assign(color_in);
3072 compiler.stack_assign(bright_in);
3073 compiler.stack_assign(contrast_in);
3074 compiler.stack_assign(color_out);
3076 compiler.add_node(NODE_BRIGHTCONTRAST,
3077 color_in->stack_offset, color_out->stack_offset,
3078 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
3081 void BrightContrastNode::compile(OSLCompiler& compiler)
3083 compiler.add(this, "node_brightness");
3087 SeparateRGBNode::SeparateRGBNode()
3088 : ShaderNode("separate_rgb")
3090 add_input("Image", SHADER_SOCKET_COLOR);
3091 add_output("R", SHADER_SOCKET_FLOAT);
3092 add_output("G", SHADER_SOCKET_FLOAT);
3093 add_output("B", SHADER_SOCKET_FLOAT);
3096 void SeparateRGBNode::compile(SVMCompiler& compiler)
3098 ShaderInput *color_in = input("Image");
3099 ShaderOutput *red_out = output("R");
3100 ShaderOutput *green_out = output("G");
3101 ShaderOutput *blue_out = output("B");
3103 compiler.stack_assign(color_in);
3105 compiler.stack_assign(red_out);
3106 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
3108 compiler.stack_assign(green_out);
3109 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
3111 compiler.stack_assign(blue_out);
3112 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
3115 void SeparateRGBNode::compile(OSLCompiler& compiler)
3117 compiler.add(this, "node_separate_rgb");
3121 SeparateHSVNode::SeparateHSVNode()
3122 : ShaderNode("separate_hsv")
3124 add_input("Color", SHADER_SOCKET_COLOR);
3125 add_output("H", SHADER_SOCKET_FLOAT);
3126 add_output("S", SHADER_SOCKET_FLOAT);
3127 add_output("V", SHADER_SOCKET_FLOAT);
3130 void SeparateHSVNode::compile(SVMCompiler& compiler)
3132 ShaderInput *color_in = input("Color");
3133 ShaderOutput *hue_out = output("H");
3134 ShaderOutput *saturation_out = output("S");
3135 ShaderOutput *value_out = output("V");
3137 compiler.stack_assign(color_in);
3138 compiler.stack_assign(hue_out);
3139 compiler.stack_assign(saturation_out);
3140 compiler.stack_assign(value_out);
3142 compiler.add_node(NODE_SEPARATE_HSV, color_in->stack_offset, hue_out->stack_offset, saturation_out->stack_offset);
3143 compiler.add_node(NODE_SEPARATE_HSV, value_out->stack_offset);
3147 void SeparateHSVNode::compile(OSLCompiler& compiler)
3149 compiler.add(this, "node_separate_hsv");
3152 /* Hue Saturation Value */
3156 add_input("Hue", SHADER_SOCKET_FLOAT);
3157 add_input("Saturation", SHADER_SOCKET_FLOAT);
3158 add_input("Value", SHADER_SOCKET_FLOAT);
3159 add_input("Fac", SHADER_SOCKET_FLOAT);
3160 add_input("Color", SHADER_SOCKET_COLOR);
3161 add_output("Color", SHADER_SOCKET_COLOR);
3164 void HSVNode::compile(SVMCompiler& compiler)
3166 ShaderInput *hue_in = input("Hue");
3167 ShaderInput *saturation_in = input("Saturation");
3168 ShaderInput *value_in = input("Value");
3169 ShaderInput *fac_in = input("Fac");
3170 ShaderInput *color_in = input("Color");
3171 ShaderOutput *color_out = output("Color");
3173 compiler.stack_assign(hue_in);
3174 compiler.stack_assign(saturation_in);
3175 compiler.stack_assign(value_in);
3176 compiler.stack_assign(fac_in);
3177 compiler.stack_assign(color_in);
3178 compiler.stack_assign(color_out);
3180 compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
3181 compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3184 void HSVNode::compile(OSLCompiler& compiler)
3186 compiler.add(this, "node_hsv");
3191 AttributeNode::AttributeNode()
3192 : ShaderNode("attribute")
3196 add_output("Color", SHADER_SOCKET_COLOR);
3197 add_output("Vector", SHADER_SOCKET_VECTOR);
3198 add_output("Fac", SHADER_SOCKET_FLOAT);
3201 void AttributeNode::attributes(Shader *shader, AttributeRequestSet *attributes)
3203 ShaderOutput *color_out = output("Color");
3204 ShaderOutput *vector_out = output("Vector");
3205 ShaderOutput *fac_out = output("Fac");
3207 if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty()) {
3208 AttributeStandard std = Attribute::name_standard(attribute.c_str());
3210 if(std != ATTR_STD_NONE)
3211 attributes->add(std);
3213 attributes->add(attribute);
3216 if(shader->has_volume)
3217 attributes->add(ATTR_STD_GENERATED_TRANSFORM);
3219 ShaderNode::attributes(shader, attributes);
3222 void AttributeNode::compile(SVMCompiler& compiler)
3224 ShaderOutput *color_out = output("Color");
3225 ShaderOutput *vector_out = output("Vector");
3226 ShaderOutput *fac_out = output("Fac");
3227 NodeType attr_node = NODE_ATTR;
3228 AttributeStandard std = Attribute::name_standard(attribute.c_str());
3231 if(std != ATTR_STD_NONE)
3232 attr = compiler.attribute(std);
3234 attr = compiler.attribute(attribute);
3236 if(bump == SHADER_BUMP_DX)
3237 attr_node = NODE_ATTR_BUMP_DX;
3238 else if(bump == SHADER_BUMP_DY)
3239 attr_node = NODE_ATTR_BUMP_DY;
3241 if(!color_out->links.empty() || !vector_out->links.empty()) {
3242 if(!color_out->links.empty()) {
3243 compiler.stack_assign(color_out);
3244 compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
3246 if(!vector_out->links.empty()) {
3247 compiler.stack_assign(vector_out);
3248 compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
3252 if(!fac_out->links.empty()) {
3253 compiler.stack_assign(fac_out);
3254 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
3258 void AttributeNode::compile(OSLCompiler& compiler)
3260 if(bump == SHADER_BUMP_DX)
3261 compiler.parameter("bump_offset", "dx");
3262 else if(bump == SHADER_BUMP_DY)
3263 compiler.parameter("bump_offset", "dy");
3265 compiler.parameter("bump_offset", "center");
3267 if(Attribute::name_standard(attribute.c_str()) != ATTR_STD_NONE)
3268 compiler.parameter("name", (string("geom:") + attribute.c_str()).c_str());
3270 compiler.parameter("name", attribute.c_str());
3272 compiler.add(this, "node_attribute");
3277 CameraNode::CameraNode()
3278 : ShaderNode("camera")
3280 add_output("View Vector", SHADER_SOCKET_VECTOR);
3281 add_output("View Z Depth", SHADER_SOCKET_FLOAT);
3282 add_output("View Distance", SHADER_SOCKET_FLOAT);
3285 void CameraNode::compile(SVMCompiler& compiler)
3287 ShaderOutput *vector_out = output("View Vector");
3288 ShaderOutput *z_depth_out = output("View Z Depth");
3289 ShaderOutput *distance_out = output("View Distance");
3291 compiler.stack_assign(vector_out);
3292 compiler.stack_assign(z_depth_out);
3293 compiler.stack_assign(distance_out);
3294 compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
3297 void CameraNode::compile(OSLCompiler& compiler)
3299 compiler.add(this, "node_camera");
3304 FresnelNode::FresnelNode()
3305 : ShaderNode("fresnel")
3307 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3308 add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
3309 add_output("Fac", SHADER_SOCKET_FLOAT);
3312 void FresnelNode::compile(SVMCompiler& compiler)
3314 ShaderInput *normal_in = input("Normal");
3315 ShaderInput *ior_in = input("IOR");
3316 ShaderOutput *fac_out = output("Fac");
3318 compiler.stack_assign(ior_in);
3319 compiler.stack_assign(fac_out);
3322 compiler.stack_assign(normal_in);
3324 compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), compiler.encode_uchar4(normal_in->stack_offset, fac_out->stack_offset));
3327 void FresnelNode::compile(OSLCompiler& compiler)
3329 compiler.add(this, "node_fresnel");
3334 LayerWeightNode::LayerWeightNode()
3335 : ShaderNode("layer_weight")
3337 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3338 add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
3340 add_output("Fresnel", SHADER_SOCKET_FLOAT);
3341 add_output("Facing", SHADER_SOCKET_FLOAT);
3344 void LayerWeightNode::compile(SVMCompiler& compiler)
3346 ShaderInput *normal_in = input("Normal");
3347 ShaderInput *blend_in = input("Blend");
3350 compiler.stack_assign(normal_in);
3353 compiler.stack_assign(blend_in);
3355 ShaderOutput *fresnel_out = output("Fresnel");
3356 if(!fresnel_out->links.empty()) {
3357 compiler.stack_assign(fresnel_out);
3358 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3359 compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, normal_in->stack_offset, fresnel_out->stack_offset));
3362 ShaderOutput *facing_out = output("Facing");
3363 if(!facing_out->links.empty()) {
3364 compiler.stack_assign(facing_out);
3365 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3366 compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, normal_in->stack_offset, facing_out->stack_offset));
3370 void LayerWeightNode::compile(OSLCompiler& compiler)
3372 compiler.add(this, "node_layer_weight");
3377 WireframeNode::WireframeNode()
3378 : ShaderNode("wireframe")
3380 add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
3381 add_output("Fac", SHADER_SOCKET_FLOAT);
3383 use_pixel_size = false;
3386 void WireframeNode::compile(SVMCompiler& compiler)
3388 ShaderInput *size_in = input("Size");
3389 ShaderOutput *fac_out = output("Fac");
3391 compiler.stack_assign(size_in);
3392 compiler.stack_assign(fac_out);
3393 compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
3396 void WireframeNode::compile(OSLCompiler& compiler)
3398 compiler.parameter("use_pixel_size", use_pixel_size);
3399 compiler.add(this, "node_wireframe");
3404 WavelengthNode::WavelengthNode()
3405 : ShaderNode("wavelength")
3407 add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
3408 add_output("Color", SHADER_SOCKET_COLOR);
3411 void WavelengthNode::compile(SVMCompiler& compiler)
3413 ShaderInput *wavelength_in = input("Wavelength");
3414 ShaderOutput *color_out = output("Color");
3416 compiler.stack_assign(wavelength_in);
3417 compiler.stack_assign(color_out);
3418 compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset);
3421 void WavelengthNode::compile(OSLCompiler& compiler)
3423 compiler.add(this, "node_wavelength");
3428 BlackbodyNode::BlackbodyNode()
3429 : ShaderNode("blackbody")
3431 add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f);
3432 add_output("Color", SHADER_SOCKET_COLOR);
3435 void BlackbodyNode::compile(SVMCompiler& compiler)
3437 ShaderInput *temperature_in = input("Temperature");
3438 ShaderOutput *color_out = output("Color");
3440 compiler.stack_assign(temperature_in);
3441 compiler.stack_assign(color_out);
3442 compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset);
3445 void BlackbodyNode::compile(OSLCompiler& compiler)
3447 compiler.add(this, "node_blackbody");
3452 OutputNode::OutputNode()
3453 : ShaderNode("output")
3455 add_input("Surface", SHADER_SOCKET_CLOSURE);
3456 add_input("Volume", SHADER_SOCKET_CLOSURE);
3457 add_input("Displacement", SHADER_SOCKET_FLOAT);
3458 add_input("Normal", SHADER_SOCKET_NORMAL);
3461 void OutputNode::compile(SVMCompiler& compiler)
3463 if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
3464 ShaderInput *displacement_in = input("Displacement");
3466 if(displacement_in->link) {
3467 compiler.stack_assign(displacement_in);
3468 compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
3473 void OutputNode::compile(OSLCompiler& compiler)
3475 if(compiler.output_type() == SHADER_TYPE_SURFACE)
3476 compiler.add(this, "node_output_surface");
3477 else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3478 compiler.add(this, "node_output_volume");
3479 else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3480 compiler.add(this, "node_output_displacement");
3485 MathNode::MathNode()
3486 : ShaderNode("math")
3488 type = ustring("Add");
3492 add_input("Value1", SHADER_SOCKET_FLOAT);
3493 add_input("Value2", SHADER_SOCKET_FLOAT);
3494 add_output("Value", SHADER_SOCKET_FLOAT);
3497 static ShaderEnum math_type_init()
3501 enm.insert("Add", NODE_MATH_ADD);
3502 enm.insert("Subtract", NODE_MATH_SUBTRACT);
3503 enm.insert("Multiply", NODE_MATH_MULTIPLY);
3504 enm.insert("Divide", NODE_MATH_DIVIDE);
3505 enm.insert("Sine", NODE_MATH_SINE);
3506 enm.insert("Cosine", NODE_MATH_COSINE);
3507 enm.insert("Tangent", NODE_MATH_TANGENT);
3508 enm.insert("Arcsine", NODE_MATH_ARCSINE);
3509 enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3510 enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3511 enm.insert("Power", NODE_MATH_POWER);
3512 enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3513 enm.insert("Minimum", NODE_MATH_MINIMUM);
3514 enm.insert("Maximum", NODE_MATH_MAXIMUM);
3515 enm.insert("Round", NODE_MATH_ROUND);
3516 enm.insert("Less Than", NODE_MATH_LESS_THAN);
3517 enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3518 enm.insert("Modulo", NODE_MATH_MODULO);
3523 ShaderEnum MathNode::type_enum = math_type_init();
3525 void MathNode::compile(SVMCompiler& compiler)
3527 ShaderInput *value1_in = input("Value1");
3528 ShaderInput *value2_in = input("Value2");
3529 ShaderOutput *value_out = output("Value");
3531 compiler.stack_assign(value1_in);
3532 compiler.stack_assign(value2_in);
3533 compiler.stack_assign(value_out);
3535 compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3536 compiler.add_node(NODE_MATH, value_out->stack_offset);
3539 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3540 compiler.add_node(NODE_MATH, value_out->stack_offset);
3544 void MathNode::compile(OSLCompiler& compiler)
3546 compiler.parameter("type", type);
3547 compiler.parameter("Clamp", use_clamp);
3548 compiler.add(this, "node_math");
3553 VectorMathNode::VectorMathNode()
3554 : ShaderNode("vector_math")
3556 type = ustring("Add");
3558 add_input("Vector1", SHADER_SOCKET_VECTOR);
3559 add_input("Vector2", SHADER_SOCKET_VECTOR);
3560 add_output("Value", SHADER_SOCKET_FLOAT);
3561 add_output("Vector", SHADER_SOCKET_VECTOR);
3564 static ShaderEnum vector_math_type_init()
3568 enm.insert("Add", NODE_VECTOR_MATH_ADD);
3569 enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3570 enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3571 enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3572 enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3573 enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3578 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3580 void VectorMathNode::compile(SVMCompiler& compiler)
3582 ShaderInput *vector1_in = input("Vector1");
3583 ShaderInput *vector2_in = input("Vector2");
3584 ShaderOutput *value_out = output("Value");
3585 ShaderOutput *vector_out = output("Vector");
3587 compiler.stack_assign(vector1_in);
3588 compiler.stack_assign(vector2_in);
3589 compiler.stack_assign(value_out);
3590 compiler.stack_assign(vector_out);
3592 compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
3593 compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
3596 void VectorMathNode::compile(OSLCompiler& compiler)
3598 compiler.parameter("type", type);
3599 compiler.add(this, "node_vector_math");
3602 /* VectorTransform */
3604 VectorTransformNode::VectorTransformNode()
3605 : ShaderNode("vector_transform")
3607 type = ustring("Vector");
3608 convert_from = ustring("world");
3609 convert_to = ustring("object");
3611 add_input("Vector", SHADER_SOCKET_VECTOR);
3612 add_output("Vector", SHADER_SOCKET_VECTOR);
3615 static ShaderEnum vector_transform_type_init()
3619 enm.insert("Vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
3620 enm.insert("Point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
3621 enm.insert("Normal", NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
3626 static ShaderEnum vector_transform_convert_space_init()
3630 enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
3631 enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
3632 enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA);
3637 ShaderEnum VectorTransformNode::type_enum = vector_transform_type_init();
3638 ShaderEnum VectorTransformNode::convert_space_enum = vector_transform_convert_space_init();
3640 void VectorTransformNode::compile(SVMCompiler& compiler)
3642 ShaderInput *vector_in = input("Vector");
3643 ShaderOutput *vector_out = output("Vector");
3645 compiler.stack_assign(vector_in);
3646 compiler.stack_assign(vector_out);
3648 compiler.add_node(NODE_VECTOR_TRANSFORM,
3649 compiler.encode_uchar4(type_enum[type], convert_space_enum[convert_from], convert_space_enum[convert_to]),
3650 compiler.encode_uchar4(vector_in->stack_offset, vector_out->stack_offset));
3653 void VectorTransformNode::compile(OSLCompiler& compiler)
3655 compiler.parameter("type", type);
3656 compiler.parameter("convert_from", convert_from);
3657 compiler.parameter("convert_to", convert_to);
3658 compiler.add(this, "node_vector_transform");
3663 BumpNode::BumpNode()
3664 : ShaderNode("bump")
3668 /* this input is used by the user, but after graph transform it is no longer
3669 * used and moved to sampler center/x/y instead */
3670 add_input("Height", SHADER_SOCKET_FLOAT);
3672 add_input("SampleCenter", SHADER_SOCKET_FLOAT);
3673 add_input("SampleX", SHADER_SOCKET_FLOAT);
3674 add_input("SampleY", SHADER_SOCKET_FLOAT);
3675 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
3676 add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3677 add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
3679 add_output("Normal", SHADER_SOCKET_NORMAL);
3682 void BumpNode::compile(SVMCompiler& compiler)
3684 ShaderInput *center_in = input("SampleCenter");
3685 ShaderInput *dx_in = input("SampleX");
3686 ShaderInput *dy_in = input("SampleY");
3687 ShaderInput *normal_in = input("Normal");
3688 ShaderInput *strength_in = input("Strength");
3689 ShaderInput *distance_in = input("Distance");
3690 ShaderOutput *normal_out = output("Normal");
3692 compiler.stack_assign(center_in);
3693 compiler.stack_assign(dx_in);
3694 compiler.stack_assign(dy_in);
3695 compiler.stack_assign(strength_in);
3696 compiler.stack_assign(distance_in);
3697 compiler.stack_assign(normal_out);
3700 compiler.stack_assign(normal_in);
3702 /* pack all parameters in the node */
3703 compiler.add_node(NODE_SET_BUMP,
3704 compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
3705 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
3706 dy_in->stack_offset, strength_in->stack_offset),
3707 normal_out->stack_offset);
3710 void BumpNode::compile(OSLCompiler& compiler)
3712 compiler.parameter("invert", invert);
3713 compiler.add(this, "node_bump");
3718 RGBCurvesNode::RGBCurvesNode()
3719 : ShaderNode("rgb_curves")
3721 add_input("Fac", SHADER_SOCKET_FLOAT);
3722 add_input("Color", SHADER_SOCKET_COLOR);
3723 add_output("Color", SHADER_SOCKET_COLOR);
3726 void RGBCurvesNode::compile(SVMCompiler& compiler)
3728 ShaderInput *fac_in = input("Fac");
3729 ShaderInput *color_in = input("Color");
3730 ShaderOutput *color_out = output("Color");
3732 compiler.stack_assign(fac_in);
3733 compiler.stack_assign(color_in);
3734 compiler.stack_assign(color_out);
3736 compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3737 compiler.add_array(curves, RAMP_TABLE_SIZE);
3740 void RGBCurvesNode::compile(OSLCompiler& compiler)
3742 float ramp[RAMP_TABLE_SIZE][3];