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 projection_blend = 0.0f;
199 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_UV);
200 add_output("Color", SHADER_SOCKET_COLOR);
201 add_output("Alpha", SHADER_SOCKET_FLOAT);
204 ImageTextureNode::~ImageTextureNode()
207 image_manager->remove_image(filename, builtin_data);
210 ShaderNode *ImageTextureNode::clone() const
212 ImageTextureNode *node = new ImageTextureNode(*this);
213 node->image_manager = NULL;
216 node->is_linear = false;
220 void ImageTextureNode::attributes(AttributeRequestSet *attributes)
223 /* todo: avoid loading other texture coordinates when using ptex,
224 * and hide texture coordinate socket in the UI */
225 if (string_endswith(filename, ".ptx")) {
227 attributes->add(ATTR_STD_PTEX_FACE_ID);
228 attributes->add(ATTR_STD_PTEX_UV);
232 ShaderNode::attributes(attributes);
235 void ImageTextureNode::compile(SVMCompiler& compiler)
237 ShaderInput *vector_in = input("Vector");
238 ShaderOutput *color_out = output("Color");
239 ShaderOutput *alpha_out = output("Alpha");
241 image_manager = compiler.image_manager;
244 slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear);
245 is_float = (int)is_float_bool;
248 if(!color_out->links.empty())
249 compiler.stack_assign(color_out);
250 if(!alpha_out->links.empty())
251 compiler.stack_assign(alpha_out);
254 compiler.stack_assign(vector_in);
256 int srgb = (is_linear || color_space != "Color")? 0: 1;
257 int vector_offset = vector_in->stack_offset;
259 if(!tex_mapping.skip()) {
260 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
261 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
264 if(projection == "Flat") {
265 compiler.add_node(NODE_TEX_IMAGE,
267 compiler.encode_uchar4(
269 color_out->stack_offset,
270 alpha_out->stack_offset,
274 compiler.add_node(NODE_TEX_IMAGE_BOX,
276 compiler.encode_uchar4(
278 color_out->stack_offset,
279 alpha_out->stack_offset,
281 __float_as_int(projection_blend));
284 if(vector_offset != vector_in->stack_offset)
285 compiler.stack_clear_offset(vector_in->type, vector_offset);
288 /* image not found */
289 if(!color_out->links.empty()) {
290 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
291 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
293 TEX_IMAGE_MISSING_B));
295 if(!alpha_out->links.empty())
296 compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
300 void ImageTextureNode::compile(OSLCompiler& compiler)
302 ShaderOutput *alpha_out = output("Alpha");
304 tex_mapping.compile(compiler);
307 is_float = (int)image_manager->is_float_image(filename, NULL, is_linear);
309 compiler.parameter("filename", filename.c_str());
310 if(is_linear || color_space != "Color")
311 compiler.parameter("color_space", "Linear");
313 compiler.parameter("color_space", "sRGB");
314 compiler.parameter("projection", projection);
315 compiler.parameter("projection_blend", projection_blend);
316 compiler.parameter("is_float", is_float);
317 compiler.parameter("use_alpha", !alpha_out->links.empty());
318 compiler.add(this, "node_image_texture");
321 /* Environment Texture */
323 static ShaderEnum env_projection_init()
327 enm.insert("Equirectangular", 0);
328 enm.insert("Mirror Ball", 1);
333 ShaderEnum EnvironmentTextureNode::color_space_enum = color_space_init();
334 ShaderEnum EnvironmentTextureNode::projection_enum = env_projection_init();
336 EnvironmentTextureNode::EnvironmentTextureNode()
337 : TextureNode("environment_texture")
339 image_manager = NULL;
345 color_space = ustring("Color");
346 projection = ustring("Equirectangular");
349 add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
350 add_output("Color", SHADER_SOCKET_COLOR);
351 add_output("Alpha", SHADER_SOCKET_FLOAT);
354 EnvironmentTextureNode::~EnvironmentTextureNode()
357 image_manager->remove_image(filename, builtin_data);
360 ShaderNode *EnvironmentTextureNode::clone() const
362 EnvironmentTextureNode *node = new EnvironmentTextureNode(*this);
363 node->image_manager = NULL;
366 node->is_linear = false;
370 void EnvironmentTextureNode::attributes(AttributeRequestSet *attributes)
373 if (string_endswith(filename, ".ptx")) {
375 attributes->add(ATTR_STD_PTEX_FACE_ID);
376 attributes->add(ATTR_STD_PTEX_UV);
380 ShaderNode::attributes(attributes);
383 void EnvironmentTextureNode::compile(SVMCompiler& compiler)
385 ShaderInput *vector_in = input("Vector");
386 ShaderOutput *color_out = output("Color");
387 ShaderOutput *alpha_out = output("Alpha");
389 image_manager = compiler.image_manager;
392 slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear);
393 is_float = (int)is_float_bool;
396 if(!color_out->links.empty())
397 compiler.stack_assign(color_out);
398 if(!alpha_out->links.empty())
399 compiler.stack_assign(alpha_out);
402 compiler.stack_assign(vector_in);
404 int srgb = (is_linear || color_space != "Color")? 0: 1;
405 int vector_offset = vector_in->stack_offset;
407 if(!tex_mapping.skip()) {
408 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
409 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
412 compiler.add_node(NODE_TEX_ENVIRONMENT,
414 compiler.encode_uchar4(
416 color_out->stack_offset,
417 alpha_out->stack_offset,
419 projection_enum[projection]);
421 if(vector_offset != vector_in->stack_offset)
422 compiler.stack_clear_offset(vector_in->type, vector_offset);
425 /* image not found */
426 if(!color_out->links.empty()) {
427 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
428 compiler.add_node(NODE_VALUE_V, make_float3(TEX_IMAGE_MISSING_R,
430 TEX_IMAGE_MISSING_B));
432 if(!alpha_out->links.empty())
433 compiler.add_node(NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), alpha_out->stack_offset);
437 void EnvironmentTextureNode::compile(OSLCompiler& compiler)
439 ShaderOutput *alpha_out = output("Alpha");
441 tex_mapping.compile(compiler);
444 is_float = (int)image_manager->is_float_image(filename, NULL, is_linear);
446 compiler.parameter("filename", filename.c_str());
447 compiler.parameter("projection", projection);
448 if(is_linear || color_space != "Color")
449 compiler.parameter("color_space", "Linear");
451 compiler.parameter("color_space", "sRGB");
452 compiler.parameter("is_float", is_float);
453 compiler.parameter("use_alpha", !alpha_out->links.empty());
454 compiler.add(this, "node_environment_texture");
459 static float2 sky_spherical_coordinates(float3 dir)
461 return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
464 typedef struct SunSky {
465 /* sun direction in spherical and cartesian */
469 float radiance_x, radiance_y, radiance_z;
470 float config_x[9], config_y[9], config_z[9];
474 static float sky_perez_function(float lam[6], float theta, float gamma)
476 return (1.0f + lam[0]*expf(lam[1]/cosf(theta))) * (1.0f + lam[2]*expf(lam[3]*gamma) + lam[4]*cosf(gamma)*cosf(gamma));
479 static void sky_texture_precompute_old(SunSky *sunsky, float3 dir, float turbidity)
482 * We re-use the SunSky struct of the new model, to avoid extra variables
483 * zenith_Y/x/y is now radiance_x/y/z
484 * perez_Y/x/y is now config_x/y/z
487 float2 spherical = sky_spherical_coordinates(dir);
488 float theta = spherical.x;
489 float phi = spherical.y;
491 sunsky->theta = theta;
494 float theta2 = theta*theta;
495 float theta3 = theta2*theta;
499 float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
500 sunsky->radiance_x = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
501 sunsky->radiance_x *= 0.06f;
504 (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
505 (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
506 (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
509 (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
510 (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) * T +
511 (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
513 sunsky->config_x[0] = (0.1787f * T - 1.4630f);
514 sunsky->config_x[1] = (-0.3554f * T + 0.4275f);
515 sunsky->config_x[2] = (-0.0227f * T + 5.3251f);
516 sunsky->config_x[3] = (0.1206f * T - 2.5771f);
517 sunsky->config_x[4] = (-0.0670f * T + 0.3703f);
519 sunsky->config_y[0] = (-0.0193f * T - 0.2592f);
520 sunsky->config_y[1] = (-0.0665f * T + 0.0008f);
521 sunsky->config_y[2] = (-0.0004f * T + 0.2125f);
522 sunsky->config_y[3] = (-0.0641f * T - 0.8989f);
523 sunsky->config_y[4] = (-0.0033f * T + 0.0452f);
525 sunsky->config_z[0] = (-0.0167f * T - 0.2608f);
526 sunsky->config_z[1] = (-0.0950f * T + 0.0092f);
527 sunsky->config_z[2] = (-0.0079f * T + 0.2102f);
528 sunsky->config_z[3] = (-0.0441f * T - 1.6537f);
529 sunsky->config_z[4] = (-0.0109f * T + 0.0529f);
531 /* unused for old sky model */
532 for(int i = 5; i < 9; i++) {
533 sunsky->config_x[i] = 0.0f;
534 sunsky->config_y[i] = 0.0f;
535 sunsky->config_z[i] = 0.0f;
538 sunsky->radiance_x /= sky_perez_function(sunsky->config_x, 0, theta);
539 sunsky->radiance_y /= sky_perez_function(sunsky->config_y, 0, theta);
540 sunsky->radiance_z /= sky_perez_function(sunsky->config_z, 0, theta);
544 static void sky_texture_precompute_new(SunSky *sunsky, float3 dir, float turbidity, float ground_albedo)
546 /* Calculate Sun Direction and save coordinates */
547 float2 spherical = sky_spherical_coordinates(dir);
548 float theta = spherical.x;
549 float phi = spherical.y;
551 /* Clamp Turbidity */
552 turbidity = clamp(turbidity, 0.0f, 10.0f);
554 /* Clamp to Horizon */
555 theta = clamp(theta, 0.0f, M_PI_2_F);
557 sunsky->theta = theta;
560 double solarElevation = M_PI_2_F - theta;
562 /* Initialize Sky Model */
563 ArHosekSkyModelState *sky_state;
564 sky_state = arhosek_xyz_skymodelstate_alloc_init(turbidity, ground_albedo, solarElevation);
566 /* Copy values from sky_state to SunSky */
567 for (int i = 0; i < 9; ++i) {
568 sunsky->config_x[i] = sky_state->configs[0][i];
569 sunsky->config_y[i] = sky_state->configs[1][i];
570 sunsky->config_z[i] = sky_state->configs[2][i];
572 sunsky->radiance_x = sky_state->radiances[0];
573 sunsky->radiance_y = sky_state->radiances[1];
574 sunsky->radiance_z = sky_state->radiances[2];
577 arhosekskymodelstate_free(sky_state);
580 static ShaderEnum sky_type_init()
584 enm.insert("Preetham", NODE_SKY_OLD);
585 enm.insert("Hosek / Wilkie", NODE_SKY_NEW);
590 ShaderEnum SkyTextureNode::type_enum = sky_type_init();
592 SkyTextureNode::SkyTextureNode()
593 : TextureNode("sky_texture")
595 type = ustring("Hosek / Wilkie");
597 sun_direction = make_float3(0.0f, 0.0f, 1.0f);
599 ground_albedo = 0.3f;
601 add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
602 add_output("Color", SHADER_SOCKET_COLOR);
605 void SkyTextureNode::compile(SVMCompiler& compiler)
607 ShaderInput *vector_in = input("Vector");
608 ShaderOutput *color_out = output("Color");
611 if(type_enum[type] == NODE_SKY_OLD)
612 sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
613 else if(type_enum[type] == NODE_SKY_NEW)
614 sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
617 compiler.stack_assign(vector_in);
619 int vector_offset = vector_in->stack_offset;
620 int sky_model = type_enum[type];
622 if(!tex_mapping.skip()) {
623 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
624 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
627 compiler.stack_assign(color_out);
628 compiler.add_node(NODE_TEX_SKY, vector_offset, color_out->stack_offset, sky_model);
629 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));
630 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]));
631 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]));
632 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]));
633 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]));
634 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]));
635 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]));
636 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]));
638 if(vector_offset != vector_in->stack_offset)
639 compiler.stack_clear_offset(vector_in->type, vector_offset);
642 void SkyTextureNode::compile(OSLCompiler& compiler)
644 tex_mapping.compile(compiler);
648 if(type_enum[type] == NODE_SKY_OLD)
649 sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
650 else if(type_enum[type] == NODE_SKY_NEW)
651 sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
653 compiler.parameter("sky_model", type);
654 compiler.parameter("theta", sunsky.theta);
655 compiler.parameter("phi", sunsky.phi);
656 compiler.parameter_color("radiance", make_float3(sunsky.radiance_x, sunsky.radiance_y, sunsky.radiance_z));
657 compiler.parameter_array("config_x", sunsky.config_x, 9);
658 compiler.parameter_array("config_y", sunsky.config_y, 9);
659 compiler.parameter_array("config_z", sunsky.config_z, 9);
660 compiler.add(this, "node_sky_texture");
663 /* Gradient Texture */
665 static ShaderEnum gradient_type_init()
669 enm.insert("Linear", NODE_BLEND_LINEAR);
670 enm.insert("Quadratic", NODE_BLEND_QUADRATIC);
671 enm.insert("Easing", NODE_BLEND_EASING);
672 enm.insert("Diagonal", NODE_BLEND_DIAGONAL);
673 enm.insert("Radial", NODE_BLEND_RADIAL);
674 enm.insert("Quadratic Sphere", NODE_BLEND_QUADRATIC_SPHERE);
675 enm.insert("Spherical", NODE_BLEND_SPHERICAL);
680 ShaderEnum GradientTextureNode::type_enum = gradient_type_init();
682 GradientTextureNode::GradientTextureNode()
683 : TextureNode("gradient_texture")
685 type = ustring("Linear");
687 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
688 add_output("Color", SHADER_SOCKET_COLOR);
689 add_output("Fac", SHADER_SOCKET_FLOAT);
692 void GradientTextureNode::compile(SVMCompiler& compiler)
694 ShaderInput *vector_in = input("Vector");
695 ShaderOutput *color_out = output("Color");
696 ShaderOutput *fac_out = output("Fac");
698 if(vector_in->link) compiler.stack_assign(vector_in);
700 int vector_offset = vector_in->stack_offset;
702 if(!tex_mapping.skip()) {
703 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
704 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
707 if(!fac_out->links.empty())
708 compiler.stack_assign(fac_out);
709 if(!color_out->links.empty())
710 compiler.stack_assign(color_out);
712 compiler.add_node(NODE_TEX_GRADIENT,
713 compiler.encode_uchar4(type_enum[type], vector_offset, fac_out->stack_offset, color_out->stack_offset));
715 if(vector_offset != vector_in->stack_offset)
716 compiler.stack_clear_offset(vector_in->type, vector_offset);
719 void GradientTextureNode::compile(OSLCompiler& compiler)
721 tex_mapping.compile(compiler);
723 compiler.parameter("Type", type);
724 compiler.add(this, "node_gradient_texture");
729 NoiseTextureNode::NoiseTextureNode()
730 : TextureNode("noise_texture")
732 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
733 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
734 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
735 add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
737 add_output("Color", SHADER_SOCKET_COLOR);
738 add_output("Fac", SHADER_SOCKET_FLOAT);
741 void NoiseTextureNode::compile(SVMCompiler& compiler)
743 ShaderInput *distortion_in = input("Distortion");
744 ShaderInput *detail_in = input("Detail");
745 ShaderInput *scale_in = input("Scale");
746 ShaderInput *vector_in = input("Vector");
747 ShaderOutput *color_out = output("Color");
748 ShaderOutput *fac_out = output("Fac");
750 if(vector_in->link) compiler.stack_assign(vector_in);
751 if(scale_in->link) compiler.stack_assign(scale_in);
752 if(detail_in->link) compiler.stack_assign(detail_in);
753 if(distortion_in->link) compiler.stack_assign(distortion_in);
755 int vector_offset = vector_in->stack_offset;
757 if(!tex_mapping.skip()) {
758 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
759 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
762 if(!fac_out->links.empty())
763 compiler.stack_assign(fac_out);
764 if(!color_out->links.empty())
765 compiler.stack_assign(color_out);
767 compiler.add_node(NODE_TEX_NOISE,
768 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset),
769 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset));
771 __float_as_int(scale_in->value.x),
772 __float_as_int(detail_in->value.x),
773 __float_as_int(distortion_in->value.x));
775 if(vector_offset != vector_in->stack_offset)
776 compiler.stack_clear_offset(vector_in->type, vector_offset);
779 void NoiseTextureNode::compile(OSLCompiler& compiler)
781 tex_mapping.compile(compiler);
783 compiler.add(this, "node_noise_texture");
786 /* Voronoi Texture */
788 static ShaderEnum voronoi_coloring_init()
792 enm.insert("Intensity", NODE_VORONOI_INTENSITY);
793 enm.insert("Cells", NODE_VORONOI_CELLS);
798 ShaderEnum VoronoiTextureNode::coloring_enum = voronoi_coloring_init();
800 VoronoiTextureNode::VoronoiTextureNode()
801 : TextureNode("voronoi_texture")
803 coloring = ustring("Intensity");
805 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
806 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
808 add_output("Color", SHADER_SOCKET_COLOR);
809 add_output("Fac", SHADER_SOCKET_FLOAT);
812 void VoronoiTextureNode::compile(SVMCompiler& compiler)
814 ShaderInput *scale_in = input("Scale");
815 ShaderInput *vector_in = input("Vector");
816 ShaderOutput *color_out = output("Color");
817 ShaderOutput *fac_out = output("Fac");
819 if(vector_in->link) compiler.stack_assign(vector_in);
820 if(scale_in->link) compiler.stack_assign(scale_in);
822 int vector_offset = vector_in->stack_offset;
824 if(!tex_mapping.skip()) {
825 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
826 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
829 compiler.stack_assign(color_out);
830 compiler.stack_assign(fac_out);
832 compiler.add_node(NODE_TEX_VORONOI,
833 coloring_enum[coloring],
834 compiler.encode_uchar4(scale_in->stack_offset, vector_offset, fac_out->stack_offset, color_out->stack_offset),
835 __float_as_int(scale_in->value.x));
837 if(vector_offset != vector_in->stack_offset)
838 compiler.stack_clear_offset(vector_in->type, vector_offset);
841 void VoronoiTextureNode::compile(OSLCompiler& compiler)
843 tex_mapping.compile(compiler);
845 compiler.parameter("Coloring", coloring);
846 compiler.add(this, "node_voronoi_texture");
849 /* Musgrave Texture */
851 static ShaderEnum musgrave_type_init()
855 enm.insert("Multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
856 enm.insert("fBM", NODE_MUSGRAVE_FBM);
857 enm.insert("Hybrid Multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
858 enm.insert("Ridged Multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
859 enm.insert("Hetero Terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
864 ShaderEnum MusgraveTextureNode::type_enum = musgrave_type_init();
866 MusgraveTextureNode::MusgraveTextureNode()
867 : TextureNode("musgrave_texture")
869 type = ustring("fBM");
871 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
872 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
873 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
874 add_input("Dimension", SHADER_SOCKET_FLOAT, 2.0f);
875 add_input("Lacunarity", SHADER_SOCKET_FLOAT, 1.0f);
876 add_input("Offset", SHADER_SOCKET_FLOAT, 0.0f);
877 add_input("Gain", SHADER_SOCKET_FLOAT, 1.0f);
879 add_output("Fac", SHADER_SOCKET_FLOAT);
880 add_output("Color", SHADER_SOCKET_COLOR);
883 void MusgraveTextureNode::compile(SVMCompiler& compiler)
885 ShaderInput *vector_in = input("Vector");
886 ShaderInput *scale_in = input("Scale");
887 ShaderInput *dimension_in = input("Dimension");
888 ShaderInput *lacunarity_in = input("Lacunarity");
889 ShaderInput *detail_in = input("Detail");
890 ShaderInput *offset_in = input("Offset");
891 ShaderInput *gain_in = input("Gain");
892 ShaderOutput *fac_out = output("Fac");
893 ShaderOutput *color_out = output("Color");
895 if(vector_in->link) compiler.stack_assign(vector_in);
896 if(dimension_in->link) compiler.stack_assign(dimension_in);
897 if(lacunarity_in->link) compiler.stack_assign(lacunarity_in);
898 if(detail_in->link) compiler.stack_assign(detail_in);
899 if(offset_in->link) compiler.stack_assign(offset_in);
900 if(gain_in->link) compiler.stack_assign(gain_in);
901 if(scale_in->link) compiler.stack_assign(scale_in);
903 int vector_offset = vector_in->stack_offset;
905 if(!tex_mapping.skip()) {
906 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
907 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
910 if(!fac_out->links.empty())
911 compiler.stack_assign(fac_out);
912 if(!color_out->links.empty())
913 compiler.stack_assign(color_out);
915 compiler.add_node(NODE_TEX_MUSGRAVE,
916 compiler.encode_uchar4(type_enum[type], vector_offset, color_out->stack_offset, fac_out->stack_offset),
917 compiler.encode_uchar4(dimension_in->stack_offset, lacunarity_in->stack_offset, detail_in->stack_offset, offset_in->stack_offset),
918 compiler.encode_uchar4(gain_in->stack_offset, scale_in->stack_offset));
919 compiler.add_node(__float_as_int(dimension_in->value.x),
920 __float_as_int(lacunarity_in->value.x),
921 __float_as_int(detail_in->value.x),
922 __float_as_int(offset_in->value.x));
923 compiler.add_node(__float_as_int(gain_in->value.x),
924 __float_as_int(scale_in->value.x));
926 if(vector_offset != vector_in->stack_offset)
927 compiler.stack_clear_offset(vector_in->type, vector_offset);
930 void MusgraveTextureNode::compile(OSLCompiler& compiler)
932 tex_mapping.compile(compiler);
934 compiler.parameter("Type", type);
936 compiler.add(this, "node_musgrave_texture");
941 static ShaderEnum wave_type_init()
945 enm.insert("Bands", NODE_WAVE_BANDS);
946 enm.insert("Rings", NODE_WAVE_RINGS);
951 ShaderEnum WaveTextureNode::type_enum = wave_type_init();
953 WaveTextureNode::WaveTextureNode()
954 : TextureNode("wave_texture")
956 type = ustring("Bands");
958 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
959 add_input("Distortion", SHADER_SOCKET_FLOAT, 0.0f);
960 add_input("Detail", SHADER_SOCKET_FLOAT, 2.0f);
961 add_input("Detail Scale", SHADER_SOCKET_FLOAT, 1.0f);
962 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
964 add_output("Color", SHADER_SOCKET_COLOR);
965 add_output("Fac", SHADER_SOCKET_FLOAT);
968 void WaveTextureNode::compile(SVMCompiler& compiler)
970 ShaderInput *scale_in = input("Scale");
971 ShaderInput *distortion_in = input("Distortion");
972 ShaderInput *dscale_in = input("Detail Scale");
973 ShaderInput *detail_in = input("Detail");
974 ShaderInput *vector_in = input("Vector");
975 ShaderOutput *fac_out = output("Fac");
976 ShaderOutput *color_out = output("Color");
978 if(scale_in->link) compiler.stack_assign(scale_in);
979 if(detail_in->link) compiler.stack_assign(detail_in);
980 if(distortion_in->link) compiler.stack_assign(distortion_in);
981 if(dscale_in->link) compiler.stack_assign(dscale_in);
982 if(vector_in->link) compiler.stack_assign(vector_in);
984 int vector_offset = vector_in->stack_offset;
986 if(!tex_mapping.skip()) {
987 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
988 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
991 if(!fac_out->links.empty())
992 compiler.stack_assign(fac_out);
993 if(!color_out->links.empty())
994 compiler.stack_assign(color_out);
996 compiler.add_node(NODE_TEX_WAVE,
997 compiler.encode_uchar4(type_enum[type], color_out->stack_offset, fac_out->stack_offset, dscale_in->stack_offset),
998 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, detail_in->stack_offset, distortion_in->stack_offset));
1001 __float_as_int(scale_in->value.x),
1002 __float_as_int(detail_in->value.x),
1003 __float_as_int(distortion_in->value.x),
1004 __float_as_int(dscale_in->value.x));
1006 if(vector_offset != vector_in->stack_offset)
1007 compiler.stack_clear_offset(vector_in->type, vector_offset);
1010 void WaveTextureNode::compile(OSLCompiler& compiler)
1012 tex_mapping.compile(compiler);
1014 compiler.parameter("Type", type);
1016 compiler.add(this, "node_wave_texture");
1021 MagicTextureNode::MagicTextureNode()
1022 : TextureNode("magic_texture")
1026 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1027 add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
1028 add_input("Distortion", SHADER_SOCKET_FLOAT, 1.0f);
1030 add_output("Color", SHADER_SOCKET_COLOR);
1031 add_output("Fac", SHADER_SOCKET_FLOAT);
1034 void MagicTextureNode::compile(SVMCompiler& compiler)
1036 ShaderInput *vector_in = input("Vector");
1037 ShaderInput *scale_in = input("Scale");
1038 ShaderInput *distortion_in = input("Distortion");
1039 ShaderOutput *color_out = output("Color");
1040 ShaderOutput *fac_out = output("Fac");
1042 if(vector_in->link) compiler.stack_assign(vector_in);
1043 if(distortion_in->link) compiler.stack_assign(distortion_in);
1044 if(scale_in->link) compiler.stack_assign(scale_in);
1046 int vector_offset = vector_in->stack_offset;
1048 if(!tex_mapping.skip()) {
1049 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1050 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1053 if(!fac_out->links.empty())
1054 compiler.stack_assign(fac_out);
1055 if(!color_out->links.empty())
1056 compiler.stack_assign(color_out);
1058 compiler.add_node(NODE_TEX_MAGIC,
1059 compiler.encode_uchar4(depth, color_out->stack_offset, fac_out->stack_offset),
1060 compiler.encode_uchar4(vector_offset, scale_in->stack_offset, distortion_in->stack_offset));
1062 __float_as_int(scale_in->value.x),
1063 __float_as_int(distortion_in->value.x));
1065 if(vector_offset != vector_in->stack_offset)
1066 compiler.stack_clear_offset(vector_in->type, vector_offset);
1069 void MagicTextureNode::compile(OSLCompiler& compiler)
1071 tex_mapping.compile(compiler);
1073 compiler.parameter("Depth", depth);
1074 compiler.add(this, "node_magic_texture");
1077 /* Checker Texture */
1079 CheckerTextureNode::CheckerTextureNode()
1080 : TextureNode("checker_texture")
1082 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1083 add_input("Color1", SHADER_SOCKET_COLOR);
1084 add_input("Color2", SHADER_SOCKET_COLOR);
1085 add_input("Scale", SHADER_SOCKET_FLOAT, 1.0f);
1087 add_output("Color", SHADER_SOCKET_COLOR);
1088 add_output("Fac", SHADER_SOCKET_FLOAT);
1091 void CheckerTextureNode::compile(SVMCompiler& compiler)
1093 ShaderInput *vector_in = input("Vector");
1094 ShaderInput *color1_in = input("Color1");
1095 ShaderInput *color2_in = input("Color2");
1096 ShaderInput *scale_in = input("Scale");
1098 ShaderOutput *color_out = output("Color");
1099 ShaderOutput *fac_out = output("Fac");
1101 compiler.stack_assign(vector_in);
1102 compiler.stack_assign(color1_in);
1103 compiler.stack_assign(color2_in);
1104 if(scale_in->link) compiler.stack_assign(scale_in);
1106 int vector_offset = vector_in->stack_offset;
1108 if(!tex_mapping.skip()) {
1109 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1110 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1113 if(!color_out->links.empty())
1114 compiler.stack_assign(color_out);
1115 if(!fac_out->links.empty())
1116 compiler.stack_assign(fac_out);
1118 compiler.add_node(NODE_TEX_CHECKER,
1119 compiler.encode_uchar4(vector_offset, color1_in->stack_offset, color2_in->stack_offset, scale_in->stack_offset),
1120 compiler.encode_uchar4(color_out->stack_offset, fac_out->stack_offset),
1121 __float_as_int(scale_in->value.x));
1123 if(vector_offset != vector_in->stack_offset)
1124 compiler.stack_clear_offset(vector_in->type, vector_offset);
1127 void CheckerTextureNode::compile(OSLCompiler& compiler)
1129 tex_mapping.compile(compiler);
1131 compiler.add(this, "node_checker_texture");
1136 BrickTextureNode::BrickTextureNode()
1137 : TextureNode("brick_texture")
1140 offset_frequency = 2;
1142 squash_frequency = 2;
1144 add_input("Vector", SHADER_SOCKET_POINT, ShaderInput::TEXTURE_GENERATED);
1145 add_input("Color1", SHADER_SOCKET_COLOR);
1146 add_input("Color2", SHADER_SOCKET_COLOR);
1147 add_input("Mortar", SHADER_SOCKET_COLOR);
1148 add_input("Scale", SHADER_SOCKET_FLOAT, 5.0f);
1149 add_input("Mortar Size", SHADER_SOCKET_FLOAT, 0.02f);
1150 add_input("Bias", SHADER_SOCKET_FLOAT, 0.0f);
1151 add_input("Brick Width", SHADER_SOCKET_FLOAT, 0.5f);
1152 add_input("Row Height", SHADER_SOCKET_FLOAT, 0.25f);
1154 add_output("Color", SHADER_SOCKET_COLOR);
1155 add_output("Fac", SHADER_SOCKET_FLOAT);
1158 void BrickTextureNode::compile(SVMCompiler& compiler)
1160 ShaderInput *vector_in = input("Vector");
1161 ShaderInput *color1_in = input("Color1");
1162 ShaderInput *color2_in = input("Color2");
1163 ShaderInput *mortar_in = input("Mortar");
1164 ShaderInput *scale_in = input("Scale");
1165 ShaderInput *mortar_size_in = input("Mortar Size");
1166 ShaderInput *bias_in = input("Bias");
1167 ShaderInput *brick_width_in = input("Brick Width");
1168 ShaderInput *row_height_in = input("Row Height");
1170 ShaderOutput *color_out = output("Color");
1171 ShaderOutput *fac_out = output("Fac");
1173 compiler.stack_assign(vector_in);
1174 compiler.stack_assign(color1_in);
1175 compiler.stack_assign(color2_in);
1176 compiler.stack_assign(mortar_in);
1177 if(scale_in->link) compiler.stack_assign(scale_in);
1178 if(mortar_size_in->link) compiler.stack_assign(mortar_size_in);
1179 if(bias_in->link) compiler.stack_assign(bias_in);
1180 if(brick_width_in->link) compiler.stack_assign(brick_width_in);
1181 if(row_height_in->link) compiler.stack_assign(row_height_in);
1183 int vector_offset = vector_in->stack_offset;
1185 if(!tex_mapping.skip()) {
1186 vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
1187 tex_mapping.compile(compiler, vector_in->stack_offset, vector_offset);
1190 if(!color_out->links.empty())
1191 compiler.stack_assign(color_out);
1192 if(!fac_out->links.empty())
1193 compiler.stack_assign(fac_out);
1195 compiler.add_node(NODE_TEX_BRICK,
1196 compiler.encode_uchar4(vector_offset,
1197 color1_in->stack_offset, color2_in->stack_offset, mortar_in->stack_offset),
1198 compiler.encode_uchar4(scale_in->stack_offset,
1199 mortar_size_in->stack_offset, bias_in->stack_offset, brick_width_in->stack_offset),
1200 compiler.encode_uchar4(row_height_in->stack_offset,
1201 color_out->stack_offset, fac_out->stack_offset));
1203 compiler.add_node(compiler.encode_uchar4(offset_frequency, squash_frequency),
1204 __float_as_int(scale_in->value.x),
1205 __float_as_int(mortar_size_in->value.x),
1206 __float_as_int(bias_in->value.x));
1208 compiler.add_node(__float_as_int(brick_width_in->value.x),
1209 __float_as_int(row_height_in->value.x),
1210 __float_as_int(offset),
1211 __float_as_int(squash));
1213 if(vector_offset != vector_in->stack_offset)
1214 compiler.stack_clear_offset(vector_in->type, vector_offset);
1217 void BrickTextureNode::compile(OSLCompiler& compiler)
1219 tex_mapping.compile(compiler);
1221 compiler.parameter("Offset", offset);
1222 compiler.parameter("OffsetFrequency", offset_frequency);
1223 compiler.parameter("Squash", squash);
1224 compiler.parameter("SquashFrequency", squash_frequency);
1225 compiler.add(this, "node_brick_texture");
1230 NormalNode::NormalNode()
1231 : ShaderNode("normal")
1233 direction = make_float3(0.0f, 0.0f, 1.0f);
1235 add_input("Normal", SHADER_SOCKET_NORMAL);
1236 add_output("Normal", SHADER_SOCKET_NORMAL);
1237 add_output("Dot", SHADER_SOCKET_FLOAT);
1240 void NormalNode::compile(SVMCompiler& compiler)
1242 ShaderInput *normal_in = input("Normal");
1243 ShaderOutput *normal_out = output("Normal");
1244 ShaderOutput *dot_out = output("Dot");
1246 compiler.stack_assign(normal_in);
1247 compiler.stack_assign(normal_out);
1248 compiler.stack_assign(dot_out);
1250 compiler.add_node(NODE_NORMAL, normal_in->stack_offset, normal_out->stack_offset, dot_out->stack_offset);
1252 __float_as_int(direction.x),
1253 __float_as_int(direction.y),
1254 __float_as_int(direction.z));
1257 void NormalNode::compile(OSLCompiler& compiler)
1259 compiler.parameter_normal("Direction", direction);
1260 compiler.add(this, "node_normal");
1265 MappingNode::MappingNode()
1266 : ShaderNode("mapping")
1268 add_input("Vector", SHADER_SOCKET_POINT);
1269 add_output("Vector", SHADER_SOCKET_POINT);
1272 void MappingNode::compile(SVMCompiler& compiler)
1274 ShaderInput *vector_in = input("Vector");
1275 ShaderOutput *vector_out = output("Vector");
1277 compiler.stack_assign(vector_in);
1278 compiler.stack_assign(vector_out);
1280 tex_mapping.compile(compiler, vector_in->stack_offset, vector_out->stack_offset);
1283 void MappingNode::compile(OSLCompiler& compiler)
1285 Transform tfm = transform_transpose(tex_mapping.compute_transform());
1286 compiler.parameter("Matrix", tfm);
1287 compiler.parameter_point("mapping_min", tex_mapping.min);
1288 compiler.parameter_point("mapping_max", tex_mapping.max);
1289 compiler.parameter("use_minmax", tex_mapping.use_minmax);
1291 compiler.add(this, "node_mapping");
1296 ConvertNode::ConvertNode(ShaderSocketType from_, ShaderSocketType to_, bool autoconvert)
1297 : ShaderNode("convert")
1303 special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
1307 if(from == SHADER_SOCKET_FLOAT)
1308 add_input("Val", SHADER_SOCKET_FLOAT);
1309 else if(from == SHADER_SOCKET_INT)
1310 add_input("ValInt", SHADER_SOCKET_INT);
1311 else if(from == SHADER_SOCKET_COLOR)
1312 add_input("Color", SHADER_SOCKET_COLOR);
1313 else if(from == SHADER_SOCKET_VECTOR)
1314 add_input("Vector", SHADER_SOCKET_VECTOR);
1315 else if(from == SHADER_SOCKET_POINT)
1316 add_input("Point", SHADER_SOCKET_POINT);
1317 else if(from == SHADER_SOCKET_NORMAL)
1318 add_input("Normal", SHADER_SOCKET_NORMAL);
1319 else if(from == SHADER_SOCKET_STRING)
1320 add_input("String", SHADER_SOCKET_STRING);
1324 if(to == SHADER_SOCKET_FLOAT)
1325 add_output("Val", SHADER_SOCKET_FLOAT);
1326 else if(to == SHADER_SOCKET_INT)
1327 add_output("ValInt", SHADER_SOCKET_INT);
1328 else if(to == SHADER_SOCKET_COLOR)
1329 add_output("Color", SHADER_SOCKET_COLOR);
1330 else if(to == SHADER_SOCKET_VECTOR)
1331 add_output("Vector", SHADER_SOCKET_VECTOR);
1332 else if(to == SHADER_SOCKET_POINT)
1333 add_output("Point", SHADER_SOCKET_POINT);
1334 else if(to == SHADER_SOCKET_NORMAL)
1335 add_output("Normal", SHADER_SOCKET_NORMAL);
1336 else if(to == SHADER_SOCKET_STRING)
1337 add_output("String", SHADER_SOCKET_STRING);
1342 void ConvertNode::compile(SVMCompiler& compiler)
1344 ShaderInput *in = inputs[0];
1345 ShaderOutput *out = outputs[0];
1347 if(from == SHADER_SOCKET_FLOAT) {
1348 compiler.stack_assign(in);
1349 compiler.stack_assign(out);
1351 if(to == SHADER_SOCKET_INT)
1353 compiler.add_node(NODE_CONVERT, NODE_CONVERT_FI, in->stack_offset, out->stack_offset);
1355 /* float to float3 */
1356 compiler.add_node(NODE_CONVERT, NODE_CONVERT_FV, in->stack_offset, out->stack_offset);
1358 else if(from == SHADER_SOCKET_INT) {
1359 compiler.stack_assign(in);
1360 compiler.stack_assign(out);
1362 if(to == SHADER_SOCKET_FLOAT)
1364 compiler.add_node(NODE_CONVERT, NODE_CONVERT_IF, in->stack_offset, out->stack_offset);
1366 /* int to vector/point/normal */
1367 compiler.add_node(NODE_CONVERT, NODE_CONVERT_IV, in->stack_offset, out->stack_offset);
1369 else if(to == SHADER_SOCKET_FLOAT) {
1370 compiler.stack_assign(in);
1371 compiler.stack_assign(out);
1373 if(from == SHADER_SOCKET_COLOR)
1374 /* color to float */
1375 compiler.add_node(NODE_CONVERT, NODE_CONVERT_CF, in->stack_offset, out->stack_offset);
1377 /* vector/point/normal to float */
1378 compiler.add_node(NODE_CONVERT, NODE_CONVERT_VF, in->stack_offset, out->stack_offset);
1380 else if(to == SHADER_SOCKET_INT) {
1381 compiler.stack_assign(in);
1382 compiler.stack_assign(out);
1384 if(from == SHADER_SOCKET_COLOR)
1386 compiler.add_node(NODE_CONVERT, NODE_CONVERT_CI, in->stack_offset, out->stack_offset);
1388 /* vector/point/normal to int */
1389 compiler.add_node(NODE_CONVERT, NODE_CONVERT_VI, in->stack_offset, out->stack_offset);
1392 /* float3 to float3 */
1395 compiler.stack_link(in, out);
1398 /* set 0,0,0 value */
1399 compiler.stack_assign(in);
1400 compiler.stack_assign(out);
1402 compiler.add_node(NODE_VALUE_V, in->stack_offset);
1403 compiler.add_node(NODE_VALUE_V, in->value);
1408 void ConvertNode::compile(OSLCompiler& compiler)
1410 if(from == SHADER_SOCKET_FLOAT)
1411 compiler.add(this, "node_convert_from_float");
1412 else if(from == SHADER_SOCKET_INT)
1413 compiler.add(this, "node_convert_from_int");
1414 else if(from == SHADER_SOCKET_COLOR)
1415 compiler.add(this, "node_convert_from_color");
1416 else if(from == SHADER_SOCKET_VECTOR)
1417 compiler.add(this, "node_convert_from_vector");
1418 else if(from == SHADER_SOCKET_POINT)
1419 compiler.add(this, "node_convert_from_point");
1420 else if(from == SHADER_SOCKET_NORMAL)
1421 compiler.add(this, "node_convert_from_normal");
1428 ProxyNode::ProxyNode(ShaderSocketType type_)
1429 : ShaderNode("proxy")
1432 special_type = SHADER_SPECIAL_TYPE_PROXY;
1434 add_input("Input", type);
1435 add_output("Output", type);
1438 void ProxyNode::compile(SVMCompiler& compiler)
1442 void ProxyNode::compile(OSLCompiler& compiler)
1448 BsdfNode::BsdfNode(bool scattering_)
1449 : ShaderNode("bsdf"), scattering(scattering_)
1451 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1452 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
1453 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1456 closure = CLOSURE_BSSRDF_CUBIC_ID;
1457 add_output("BSSRDF", SHADER_SOCKET_CLOSURE);
1460 closure = CLOSURE_BSDF_DIFFUSE_ID;
1461 add_output("BSDF", SHADER_SOCKET_CLOSURE);
1465 void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3, ShaderInput *param4)
1467 ShaderInput *color_in = input("Color");
1468 ShaderInput *normal_in = input("Normal");
1469 ShaderInput *tangent_in = input("Tangent");
1471 if(color_in->link) {
1472 compiler.stack_assign(color_in);
1473 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1476 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1479 compiler.stack_assign(param1);
1481 compiler.stack_assign(param2);
1483 compiler.stack_assign(param3);
1485 compiler.stack_assign(param4);
1488 compiler.stack_assign(normal_in);
1490 if(tangent_in && tangent_in->link)
1491 compiler.stack_assign(tangent_in);
1493 compiler.add_node(NODE_CLOSURE_BSDF,
1494 compiler.encode_uchar4(closure,
1495 (param1)? param1->stack_offset: SVM_STACK_INVALID,
1496 (param2)? param2->stack_offset: SVM_STACK_INVALID,
1497 compiler.closure_mix_weight_offset()),
1498 __float_as_int((param1)? param1->value.x: 0.0f),
1499 __float_as_int((param2)? param2->value.x: 0.0f));
1502 compiler.add_node(normal_in->stack_offset, tangent_in->stack_offset,
1503 (param3)? param3->stack_offset: SVM_STACK_INVALID,
1504 (param4)? param4->stack_offset: SVM_STACK_INVALID);
1507 compiler.add_node(normal_in->stack_offset, SVM_STACK_INVALID,
1508 (param3)? param3->stack_offset: SVM_STACK_INVALID,
1509 (param4)? param4->stack_offset: SVM_STACK_INVALID);
1513 void BsdfNode::compile(SVMCompiler& compiler)
1515 compile(compiler, NULL, NULL);
1518 void BsdfNode::compile(OSLCompiler& compiler)
1523 /* Ward BSDF Closure */
1525 WardBsdfNode::WardBsdfNode()
1527 closure = CLOSURE_BSDF_WARD_ID;
1529 add_input("Tangent", SHADER_SOCKET_VECTOR, ShaderInput::TANGENT);
1531 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1532 add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.5f);
1533 add_input("Rotation", SHADER_SOCKET_FLOAT, 0.0f);
1536 void WardBsdfNode::attributes(AttributeRequestSet *attributes)
1538 ShaderInput *tangent_in = input("Tangent");
1540 if(!tangent_in->link)
1541 attributes->add(ATTR_STD_GENERATED);
1543 ShaderNode::attributes(attributes);
1546 void WardBsdfNode::compile(SVMCompiler& compiler)
1548 BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
1551 void WardBsdfNode::compile(OSLCompiler& compiler)
1553 compiler.add(this, "node_ward_bsdf");
1556 /* Glossy BSDF Closure */
1558 static ShaderEnum glossy_distribution_init()
1562 enm.insert("Sharp", CLOSURE_BSDF_REFLECTION_ID);
1563 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
1564 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
1569 ShaderEnum GlossyBsdfNode::distribution_enum = glossy_distribution_init();
1571 GlossyBsdfNode::GlossyBsdfNode()
1573 distribution = ustring("Beckmann");
1575 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
1578 void GlossyBsdfNode::compile(SVMCompiler& compiler)
1580 closure = (ClosureType)distribution_enum[distribution];
1582 if(closure == CLOSURE_BSDF_REFLECTION_ID)
1583 BsdfNode::compile(compiler, NULL, NULL);
1585 BsdfNode::compile(compiler, input("Roughness"), NULL);
1588 void GlossyBsdfNode::compile(OSLCompiler& compiler)
1590 compiler.parameter("distribution", distribution);
1591 compiler.add(this, "node_glossy_bsdf");
1594 /* Glass BSDF Closure */
1596 static ShaderEnum glass_distribution_init()
1600 enm.insert("Sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
1601 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
1602 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
1607 ShaderEnum GlassBsdfNode::distribution_enum = glass_distribution_init();
1609 GlassBsdfNode::GlassBsdfNode()
1611 distribution = ustring("Sharp");
1613 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1614 add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1617 void GlassBsdfNode::compile(SVMCompiler& compiler)
1619 closure = (ClosureType)distribution_enum[distribution];
1621 if(closure == CLOSURE_BSDF_SHARP_GLASS_ID)
1622 BsdfNode::compile(compiler, NULL, input("IOR"));
1624 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1627 void GlassBsdfNode::compile(OSLCompiler& compiler)
1629 compiler.parameter("distribution", distribution);
1630 compiler.add(this, "node_glass_bsdf");
1633 /* Refraction BSDF Closure */
1635 static ShaderEnum refraction_distribution_init()
1639 enm.insert("Sharp", CLOSURE_BSDF_REFRACTION_ID);
1640 enm.insert("Beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
1641 enm.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
1646 ShaderEnum RefractionBsdfNode::distribution_enum = refraction_distribution_init();
1648 RefractionBsdfNode::RefractionBsdfNode()
1650 distribution = ustring("Sharp");
1652 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1653 add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
1656 void RefractionBsdfNode::compile(SVMCompiler& compiler)
1658 closure = (ClosureType)distribution_enum[distribution];
1660 if(closure == CLOSURE_BSDF_REFRACTION_ID)
1661 BsdfNode::compile(compiler, NULL, input("IOR"));
1663 BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
1666 void RefractionBsdfNode::compile(OSLCompiler& compiler)
1668 compiler.parameter("distribution", distribution);
1669 compiler.add(this, "node_refraction_bsdf");
1672 /* Toon BSDF Closure */
1674 static ShaderEnum toon_component_init()
1678 enm.insert("Diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
1679 enm.insert("Glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
1684 ShaderEnum ToonBsdfNode::component_enum = toon_component_init();
1686 ToonBsdfNode::ToonBsdfNode()
1688 component = ustring("Diffuse");
1690 add_input("Size", SHADER_SOCKET_FLOAT, 0.5f);
1691 add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
1694 void ToonBsdfNode::compile(SVMCompiler& compiler)
1696 closure = (ClosureType)component_enum[component];
1698 BsdfNode::compile(compiler, input("Size"), input("Smooth"));
1701 void ToonBsdfNode::compile(OSLCompiler& compiler)
1703 compiler.parameter("component", component);
1704 compiler.add(this, "node_toon_bsdf");
1707 /* Velvet BSDF Closure */
1709 VelvetBsdfNode::VelvetBsdfNode()
1711 closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
1713 add_input("Sigma", SHADER_SOCKET_FLOAT, 1.0f);
1716 void VelvetBsdfNode::compile(SVMCompiler& compiler)
1718 BsdfNode::compile(compiler, input("Sigma"), NULL);
1721 void VelvetBsdfNode::compile(OSLCompiler& compiler)
1723 compiler.add(this, "node_velvet_bsdf");
1726 /* Diffuse BSDF Closure */
1728 DiffuseBsdfNode::DiffuseBsdfNode()
1730 closure = CLOSURE_BSDF_DIFFUSE_ID;
1731 add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
1734 void DiffuseBsdfNode::compile(SVMCompiler& compiler)
1736 BsdfNode::compile(compiler, input("Roughness"), NULL);
1739 void DiffuseBsdfNode::compile(OSLCompiler& compiler)
1741 compiler.add(this, "node_diffuse_bsdf");
1744 /* Translucent BSDF Closure */
1746 TranslucentBsdfNode::TranslucentBsdfNode()
1748 closure = CLOSURE_BSDF_TRANSLUCENT_ID;
1751 void TranslucentBsdfNode::compile(SVMCompiler& compiler)
1753 BsdfNode::compile(compiler, NULL, NULL);
1756 void TranslucentBsdfNode::compile(OSLCompiler& compiler)
1758 compiler.add(this, "node_translucent_bsdf");
1761 /* Transparent BSDF Closure */
1763 TransparentBsdfNode::TransparentBsdfNode()
1765 name = "transparent";
1766 closure = CLOSURE_BSDF_TRANSPARENT_ID;
1769 void TransparentBsdfNode::compile(SVMCompiler& compiler)
1771 BsdfNode::compile(compiler, NULL, NULL);
1774 void TransparentBsdfNode::compile(OSLCompiler& compiler)
1776 compiler.add(this, "node_transparent_bsdf");
1779 /* Subsurface Scattering Closure */
1781 static ShaderEnum subsurface_falloff_init()
1785 enm.insert("Cubic", CLOSURE_BSSRDF_CUBIC_ID);
1786 enm.insert("Gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
1791 ShaderEnum SubsurfaceScatteringNode::falloff_enum = subsurface_falloff_init();
1793 SubsurfaceScatteringNode::SubsurfaceScatteringNode()
1796 name = "subsurface_scattering";
1797 closure = CLOSURE_BSSRDF_CUBIC_ID;
1799 add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
1800 add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
1801 add_input("Sharpness", SHADER_SOCKET_FLOAT, 0.0f);
1802 add_input("Texture Blur", SHADER_SOCKET_FLOAT, 1.0f);
1805 void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
1807 BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
1810 void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
1812 compiler.parameter("Falloff", falloff_enum[closure]);
1813 compiler.add(this, "node_subsurface_scattering");
1816 bool SubsurfaceScatteringNode::has_bssrdf_bump()
1818 /* detect if anything is plugged into the normal input besides the default */
1819 ShaderInput *normal_in = input("Normal");
1820 return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
1823 /* Emissive Closure */
1825 EmissionNode::EmissionNode()
1826 : ShaderNode("emission")
1828 total_power = false;
1830 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1831 add_input("Strength", SHADER_SOCKET_FLOAT, 10.0f);
1832 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1834 add_output("Emission", SHADER_SOCKET_CLOSURE);
1837 void EmissionNode::compile(SVMCompiler& compiler)
1839 ShaderInput *color_in = input("Color");
1840 ShaderInput *strength_in = input("Strength");
1842 if(color_in->link || strength_in->link) {
1843 compiler.stack_assign(color_in);
1844 compiler.stack_assign(strength_in);
1845 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset, total_power? 1: 0);
1847 else if(total_power)
1848 compiler.add_node(NODE_EMISSION_SET_WEIGHT_TOTAL, color_in->value * strength_in->value.x);
1850 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value * strength_in->value.x);
1852 compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
1855 void EmissionNode::compile(OSLCompiler& compiler)
1857 compiler.parameter("TotalPower", (total_power)? 1: 0);
1858 compiler.add(this, "node_emission");
1861 /* Background Closure */
1863 BackgroundNode::BackgroundNode()
1864 : ShaderNode("background")
1866 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1867 add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
1868 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1870 add_output("Background", SHADER_SOCKET_CLOSURE);
1873 void BackgroundNode::compile(SVMCompiler& compiler)
1875 ShaderInput *color_in = input("Color");
1876 ShaderInput *strength_in = input("Strength");
1878 if(color_in->link || strength_in->link) {
1879 compiler.stack_assign(color_in);
1880 compiler.stack_assign(strength_in);
1881 compiler.add_node(NODE_EMISSION_WEIGHT, color_in->stack_offset, strength_in->stack_offset);
1884 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value*strength_in->value.x);
1886 compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
1889 void BackgroundNode::compile(OSLCompiler& compiler)
1891 compiler.add(this, "node_background");
1894 /* Holdout Closure */
1896 HoldoutNode::HoldoutNode()
1897 : ShaderNode("holdout")
1899 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1900 add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1902 add_output("Holdout", SHADER_SOCKET_CLOSURE);
1905 void HoldoutNode::compile(SVMCompiler& compiler)
1907 float3 value = make_float3(1.0f, 1.0f, 1.0f);
1909 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
1910 compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
1913 void HoldoutNode::compile(OSLCompiler& compiler)
1915 compiler.add(this, "node_holdout");
1918 /* Ambient Occlusion */
1920 AmbientOcclusionNode::AmbientOcclusionNode()
1921 : ShaderNode("ambient_occlusion")
1923 add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
1924 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1925 add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1927 add_output("AO", SHADER_SOCKET_CLOSURE);
1930 void AmbientOcclusionNode::compile(SVMCompiler& compiler)
1932 ShaderInput *color_in = input("Color");
1934 if(color_in->link) {
1935 compiler.stack_assign(color_in);
1936 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1939 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1941 compiler.add_node(NODE_CLOSURE_AMBIENT_OCCLUSION, compiler.closure_mix_weight_offset());
1944 void AmbientOcclusionNode::compile(OSLCompiler& compiler)
1946 compiler.add(this, "node_ambient_occlusion");
1949 /* Volume Closure */
1951 VolumeNode::VolumeNode()
1952 : ShaderNode("volume")
1954 closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
1956 add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
1957 add_input("Density", SHADER_SOCKET_FLOAT, 1.0f);
1958 add_input("VolumeMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
1960 add_output("Volume", SHADER_SOCKET_CLOSURE);
1963 void VolumeNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2)
1965 ShaderInput *color_in = input("Color");
1967 if(color_in->link) {
1968 compiler.stack_assign(color_in);
1969 compiler.add_node(NODE_CLOSURE_WEIGHT, color_in->stack_offset);
1972 compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color_in->value);
1975 compiler.stack_assign(param1);
1977 compiler.stack_assign(param2);
1979 compiler.add_node(NODE_CLOSURE_VOLUME,
1980 compiler.encode_uchar4(closure,
1981 (param1)? param1->stack_offset: SVM_STACK_INVALID,
1982 (param2)? param2->stack_offset: SVM_STACK_INVALID,
1983 compiler.closure_mix_weight_offset()),
1984 __float_as_int((param1)? param1->value.x: 0.0f),
1985 __float_as_int((param2)? param2->value.x: 0.0f));
1988 void VolumeNode::compile(SVMCompiler& compiler)
1990 compile(compiler, NULL, NULL);
1993 void VolumeNode::compile(OSLCompiler& compiler)
1998 /* Absorption Volume Closure */
2000 AbsorptionVolumeNode::AbsorptionVolumeNode()
2002 closure = CLOSURE_VOLUME_ABSORPTION_ID;
2005 void AbsorptionVolumeNode::compile(SVMCompiler& compiler)
2007 VolumeNode::compile(compiler, input("Density"), NULL);
2010 void AbsorptionVolumeNode::compile(OSLCompiler& compiler)
2012 compiler.add(this, "node_absorption_volume");
2015 /* Scatter Volume Closure */
2017 ScatterVolumeNode::ScatterVolumeNode()
2019 closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
2021 add_input("Anisotropy", SHADER_SOCKET_FLOAT, 0.0f);
2024 void ScatterVolumeNode::compile(SVMCompiler& compiler)
2026 VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
2029 void ScatterVolumeNode::compile(OSLCompiler& compiler)
2031 compiler.add(this, "node_scatter_volume");
2034 /* Hair BSDF Closure */
2036 static ShaderEnum hair_component_init()
2040 enm.insert("Reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
2041 enm.insert("Transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
2047 ShaderEnum HairBsdfNode::component_enum = hair_component_init();
2049 HairBsdfNode::HairBsdfNode()
2051 component = ustring("Reflection");
2053 add_input("Offset", SHADER_SOCKET_FLOAT);
2054 add_input("RoughnessU", SHADER_SOCKET_FLOAT);
2055 add_input("RoughnessV", SHADER_SOCKET_FLOAT);
2059 void HairBsdfNode::compile(SVMCompiler& compiler)
2061 closure = (ClosureType)component_enum[component];
2063 BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
2066 void HairBsdfNode::compile(OSLCompiler& compiler)
2068 compiler.parameter("component", component);
2070 compiler.add(this, "node_hair_bsdf");
2076 GeometryNode::GeometryNode()
2077 : ShaderNode("geometry")
2079 special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
2081 add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2082 add_output("Position", SHADER_SOCKET_POINT);
2083 add_output("Normal", SHADER_SOCKET_NORMAL);
2084 add_output("Tangent", SHADER_SOCKET_NORMAL);
2085 add_output("True Normal", SHADER_SOCKET_NORMAL);
2086 add_output("Incoming", SHADER_SOCKET_VECTOR);
2087 add_output("Parametric", SHADER_SOCKET_POINT);
2088 add_output("Backfacing", SHADER_SOCKET_FLOAT);
2091 void GeometryNode::attributes(AttributeRequestSet *attributes)
2093 if(!output("Tangent")->links.empty())
2094 attributes->add(ATTR_STD_GENERATED);
2096 ShaderNode::attributes(attributes);
2099 void GeometryNode::compile(SVMCompiler& compiler)
2102 NodeType geom_node = NODE_GEOMETRY;
2104 if(bump == SHADER_BUMP_DX)
2105 geom_node = NODE_GEOMETRY_BUMP_DX;
2106 else if(bump == SHADER_BUMP_DY)
2107 geom_node = NODE_GEOMETRY_BUMP_DY;
2109 out = output("Position");
2110 if(!out->links.empty()) {
2111 compiler.stack_assign(out);
2112 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2115 out = output("Normal");
2116 if(!out->links.empty()) {
2117 compiler.stack_assign(out);
2118 compiler.add_node(geom_node, NODE_GEOM_N, out->stack_offset);
2121 out = output("Tangent");
2122 if(!out->links.empty()) {
2123 compiler.stack_assign(out);
2124 compiler.add_node(geom_node, NODE_GEOM_T, out->stack_offset);
2127 out = output("True Normal");
2128 if(!out->links.empty()) {
2129 compiler.stack_assign(out);
2130 compiler.add_node(geom_node, NODE_GEOM_Ng, out->stack_offset);
2133 out = output("Incoming");
2134 if(!out->links.empty()) {
2135 compiler.stack_assign(out);
2136 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2139 out = output("Parametric");
2140 if(!out->links.empty()) {
2141 compiler.stack_assign(out);
2142 compiler.add_node(geom_node, NODE_GEOM_uv, out->stack_offset);
2145 out = output("Backfacing");
2146 if(!out->links.empty()) {
2147 compiler.stack_assign(out);
2148 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, out->stack_offset);
2152 void GeometryNode::compile(OSLCompiler& compiler)
2154 if(bump == SHADER_BUMP_DX)
2155 compiler.parameter("bump_offset", "dx");
2156 else if(bump == SHADER_BUMP_DY)
2157 compiler.parameter("bump_offset", "dy");
2159 compiler.parameter("bump_offset", "center");
2161 compiler.add(this, "node_geometry");
2164 /* TextureCoordinate */
2166 TextureCoordinateNode::TextureCoordinateNode()
2167 : ShaderNode("texture_coordinate")
2169 add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
2170 add_output("Generated", SHADER_SOCKET_POINT);
2171 add_output("Normal", SHADER_SOCKET_NORMAL);
2172 add_output("UV", SHADER_SOCKET_POINT);
2173 add_output("Object", SHADER_SOCKET_POINT);
2174 add_output("Camera", SHADER_SOCKET_POINT);
2175 add_output("Window", SHADER_SOCKET_POINT);
2176 add_output("Reflection", SHADER_SOCKET_NORMAL);
2181 void TextureCoordinateNode::attributes(AttributeRequestSet *attributes)
2184 if(!output("Generated")->links.empty())
2185 attributes->add(ATTR_STD_GENERATED);
2186 if(!output("UV")->links.empty())
2187 attributes->add(ATTR_STD_UV);
2190 ShaderNode::attributes(attributes);
2193 void TextureCoordinateNode::compile(SVMCompiler& compiler)
2196 NodeType texco_node = NODE_TEX_COORD;
2197 NodeType attr_node = NODE_ATTR;
2198 NodeType geom_node = NODE_GEOMETRY;
2200 if(bump == SHADER_BUMP_DX) {
2201 texco_node = NODE_TEX_COORD_BUMP_DX;
2202 attr_node = NODE_ATTR_BUMP_DX;
2203 geom_node = NODE_GEOMETRY_BUMP_DX;
2205 else if(bump == SHADER_BUMP_DY) {
2206 texco_node = NODE_TEX_COORD_BUMP_DY;
2207 attr_node = NODE_ATTR_BUMP_DY;
2208 geom_node = NODE_GEOMETRY_BUMP_DY;
2211 out = output("Generated");
2212 if(!out->links.empty()) {
2213 if(compiler.background) {
2214 compiler.stack_assign(out);
2215 compiler.add_node(geom_node, NODE_GEOM_P, out->stack_offset);
2219 compiler.stack_assign(out);
2220 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, out->stack_offset);
2223 int attr = compiler.attribute(ATTR_STD_GENERATED);
2224 compiler.stack_assign(out);
2225 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2230 out = output("Normal");
2231 if(!out->links.empty()) {
2232 compiler.stack_assign(out);
2233 compiler.add_node(texco_node, NODE_TEXCO_NORMAL, out->stack_offset);
2237 if(!out->links.empty()) {
2239 compiler.stack_assign(out);
2240 compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, out->stack_offset);
2243 int attr = compiler.attribute(ATTR_STD_UV);
2244 compiler.stack_assign(out);
2245 compiler.add_node(attr_node, attr, out->stack_offset, NODE_ATTR_FLOAT3);
2249 out = output("Object");
2250 if(!out->links.empty()) {
2251 compiler.stack_assign(out);
2252 compiler.add_node(texco_node, NODE_TEXCO_OBJECT, out->stack_offset);
2255 out = output("Camera");
2256 if(!out->links.empty()) {
2257 compiler.stack_assign(out);
2258 compiler.add_node(texco_node, NODE_TEXCO_CAMERA, out->stack_offset);
2261 out = output("Window");
2262 if(!out->links.empty()) {
2263 compiler.stack_assign(out);
2264 compiler.add_node(texco_node, NODE_TEXCO_WINDOW, out->stack_offset);
2267 out = output("Reflection");
2268 if(!out->links.empty()) {
2269 if(compiler.background) {
2270 compiler.stack_assign(out);
2271 compiler.add_node(geom_node, NODE_GEOM_I, out->stack_offset);
2274 compiler.stack_assign(out);
2275 compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, out->stack_offset);
2280 void TextureCoordinateNode::compile(OSLCompiler& compiler)
2282 if(bump == SHADER_BUMP_DX)
2283 compiler.parameter("bump_offset", "dx");
2284 else if(bump == SHADER_BUMP_DY)
2285 compiler.parameter("bump_offset", "dy");
2287 compiler.parameter("bump_offset", "center");
2289 if(compiler.background)
2290 compiler.parameter("is_background", true);
2292 compiler.parameter("from_dupli", from_dupli);
2294 compiler.add(this, "node_texture_coordinate");
2299 LightPathNode::LightPathNode()
2300 : ShaderNode("light_path")
2302 add_output("Is Camera Ray", SHADER_SOCKET_FLOAT);
2303 add_output("Is Shadow Ray", SHADER_SOCKET_FLOAT);
2304 add_output("Is Diffuse Ray", SHADER_SOCKET_FLOAT);
2305 add_output("Is Glossy Ray", SHADER_SOCKET_FLOAT);
2306 add_output("Is Singular Ray", SHADER_SOCKET_FLOAT);
2307 add_output("Is Reflection Ray", SHADER_SOCKET_FLOAT);
2308 add_output("Is Transmission Ray", SHADER_SOCKET_FLOAT);
2309 add_output("Is Volume Scatter Ray", SHADER_SOCKET_FLOAT);
2310 add_output("Ray Length", SHADER_SOCKET_FLOAT);
2311 add_output("Ray Depth", SHADER_SOCKET_FLOAT);
2314 void LightPathNode::compile(SVMCompiler& compiler)
2318 out = output("Is Camera Ray");
2319 if(!out->links.empty()) {
2320 compiler.stack_assign(out);
2321 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, out->stack_offset);
2324 out = output("Is Shadow Ray");
2325 if(!out->links.empty()) {
2326 compiler.stack_assign(out);
2327 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, out->stack_offset);
2330 out = output("Is Diffuse Ray");
2331 if(!out->links.empty()) {
2332 compiler.stack_assign(out);
2333 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, out->stack_offset);
2336 out = output("Is Glossy Ray");
2337 if(!out->links.empty()) {
2338 compiler.stack_assign(out);
2339 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, out->stack_offset);
2342 out = output("Is Singular Ray");
2343 if(!out->links.empty()) {
2344 compiler.stack_assign(out);
2345 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, out->stack_offset);
2348 out = output("Is Reflection Ray");
2349 if(!out->links.empty()) {
2350 compiler.stack_assign(out);
2351 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, out->stack_offset);
2355 out = output("Is Transmission Ray");
2356 if(!out->links.empty()) {
2357 compiler.stack_assign(out);
2358 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, out->stack_offset);
2361 out = output("Is Volume Scatter Ray");
2362 if(!out->links.empty()) {
2363 compiler.stack_assign(out);
2364 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, out->stack_offset);
2367 out = output("Ray Length");
2368 if(!out->links.empty()) {
2369 compiler.stack_assign(out);
2370 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, out->stack_offset);
2373 out = output("Ray Depth");
2374 if(!out->links.empty()) {
2375 compiler.stack_assign(out);
2376 compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, out->stack_offset);
2381 void LightPathNode::compile(OSLCompiler& compiler)
2383 compiler.add(this, "node_light_path");
2388 LightFalloffNode::LightFalloffNode()
2389 : ShaderNode("light_fallof")
2391 add_input("Strength", SHADER_SOCKET_FLOAT, 100.0f);
2392 add_input("Smooth", SHADER_SOCKET_FLOAT, 0.0f);
2393 add_output("Quadratic", SHADER_SOCKET_FLOAT);
2394 add_output("Linear", SHADER_SOCKET_FLOAT);
2395 add_output("Constant", SHADER_SOCKET_FLOAT);
2398 void LightFalloffNode::compile(SVMCompiler& compiler)
2400 ShaderInput *strength_in = input("Strength");
2401 ShaderInput *smooth_in = input("Smooth");
2403 compiler.stack_assign(strength_in);
2404 compiler.stack_assign(smooth_in);
2406 ShaderOutput *out = output("Quadratic");
2407 if(!out->links.empty()) {
2408 compiler.stack_assign(out);
2409 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_QUADRATIC,
2410 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2413 out = output("Linear");
2414 if(!out->links.empty()) {
2415 compiler.stack_assign(out);
2416 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_LINEAR,
2417 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2420 out = output("Constant");
2421 if(!out->links.empty()) {
2422 compiler.stack_assign(out);
2423 compiler.add_node(NODE_LIGHT_FALLOFF, NODE_LIGHT_FALLOFF_CONSTANT,
2424 compiler.encode_uchar4(strength_in->stack_offset, smooth_in->stack_offset, out->stack_offset));
2428 void LightFalloffNode::compile(OSLCompiler& compiler)
2430 compiler.add(this, "node_light_falloff");
2435 ObjectInfoNode::ObjectInfoNode()
2436 : ShaderNode("object_info")
2438 add_output("Location", SHADER_SOCKET_VECTOR);
2439 add_output("Object Index", SHADER_SOCKET_FLOAT);
2440 add_output("Material Index", SHADER_SOCKET_FLOAT);
2441 add_output("Random", SHADER_SOCKET_FLOAT);
2444 void ObjectInfoNode::compile(SVMCompiler& compiler)
2446 ShaderOutput *out = output("Location");
2447 if(!out->links.empty()) {
2448 compiler.stack_assign(out);
2449 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, out->stack_offset);
2452 out = output("Object Index");
2453 if(!out->links.empty()) {
2454 compiler.stack_assign(out);
2455 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, out->stack_offset);
2458 out = output("Material Index");
2459 if(!out->links.empty()) {
2460 compiler.stack_assign(out);
2461 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, out->stack_offset);
2464 out = output("Random");
2465 if(!out->links.empty()) {
2466 compiler.stack_assign(out);
2467 compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, out->stack_offset);
2471 void ObjectInfoNode::compile(OSLCompiler& compiler)
2473 compiler.add(this, "node_object_info");
2478 ParticleInfoNode::ParticleInfoNode()
2479 : ShaderNode("particle_info")
2481 add_output("Index", SHADER_SOCKET_FLOAT);
2482 add_output("Age", SHADER_SOCKET_FLOAT);
2483 add_output("Lifetime", SHADER_SOCKET_FLOAT);
2484 add_output("Location", SHADER_SOCKET_POINT);
2485 #if 0 /* not yet supported */
2486 add_output("Rotation", SHADER_SOCKET_QUATERNION);
2488 add_output("Size", SHADER_SOCKET_FLOAT);
2489 add_output("Velocity", SHADER_SOCKET_VECTOR);
2490 add_output("Angular Velocity", SHADER_SOCKET_VECTOR);
2493 void ParticleInfoNode::attributes(AttributeRequestSet *attributes)
2495 if(!output("Index")->links.empty())
2496 attributes->add(ATTR_STD_PARTICLE);
2497 if(!output("Age")->links.empty())
2498 attributes->add(ATTR_STD_PARTICLE);
2499 if(!output("Lifetime")->links.empty())
2500 attributes->add(ATTR_STD_PARTICLE);
2501 if(!output("Location")->links.empty())
2502 attributes->add(ATTR_STD_PARTICLE);
2503 #if 0 /* not yet supported */
2504 if(!output("Rotation")->links.empty())
2505 attributes->add(ATTR_STD_PARTICLE);
2507 if(!output("Size")->links.empty())
2508 attributes->add(ATTR_STD_PARTICLE);
2509 if(!output("Velocity")->links.empty())
2510 attributes->add(ATTR_STD_PARTICLE);
2511 if(!output("Angular Velocity")->links.empty())
2512 attributes->add(ATTR_STD_PARTICLE);
2514 ShaderNode::attributes(attributes);
2517 void ParticleInfoNode::compile(SVMCompiler& compiler)
2521 out = output("Index");
2522 if(!out->links.empty()) {
2523 compiler.stack_assign(out);
2524 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, out->stack_offset);
2527 out = output("Age");
2528 if(!out->links.empty()) {
2529 compiler.stack_assign(out);
2530 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, out->stack_offset);
2533 out = output("Lifetime");
2534 if(!out->links.empty()) {
2535 compiler.stack_assign(out);
2536 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, out->stack_offset);
2539 out = output("Location");
2540 if(!out->links.empty()) {
2541 compiler.stack_assign(out);
2542 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, out->stack_offset);
2545 /* quaternion data is not yet supported by Cycles */
2547 out = output("Rotation");
2548 if(!out->links.empty()) {
2549 compiler.stack_assign(out);
2550 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, out->stack_offset);
2554 out = output("Size");
2555 if(!out->links.empty()) {
2556 compiler.stack_assign(out);
2557 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, out->stack_offset);
2560 out = output("Velocity");
2561 if(!out->links.empty()) {
2562 compiler.stack_assign(out);
2563 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, out->stack_offset);
2566 out = output("Angular Velocity");
2567 if(!out->links.empty()) {
2568 compiler.stack_assign(out);
2569 compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, out->stack_offset);
2573 void ParticleInfoNode::compile(OSLCompiler& compiler)
2575 compiler.add(this, "node_particle_info");
2580 HairInfoNode::HairInfoNode()
2581 : ShaderNode("hair_info")
2583 add_output("Is Strand", SHADER_SOCKET_FLOAT);
2584 add_output("Intercept", SHADER_SOCKET_FLOAT);
2585 add_output("Thickness", SHADER_SOCKET_FLOAT);
2586 add_output("Tangent Normal", SHADER_SOCKET_NORMAL);
2587 /*output for minimum hair width transparency - deactivated*/
2588 /*add_output("Fade", SHADER_SOCKET_FLOAT);*/
2591 void HairInfoNode::attributes(AttributeRequestSet *attributes)
2593 ShaderOutput *intercept_out = output("Intercept");
2595 if(!intercept_out->links.empty())
2596 attributes->add(ATTR_STD_CURVE_INTERCEPT);
2598 ShaderNode::attributes(attributes);
2601 void HairInfoNode::compile(SVMCompiler& compiler)
2605 out = output("Is Strand");
2606 if(!out->links.empty()) {
2607 compiler.stack_assign(out);
2608 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, out->stack_offset);
2611 out = output("Intercept");
2612 if(!out->links.empty()) {
2613 int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
2614 compiler.stack_assign(out);
2615 compiler.add_node(NODE_ATTR, attr, out->stack_offset, NODE_ATTR_FLOAT);
2618 out = output("Thickness");
2619 if(!out->links.empty()) {
2620 compiler.stack_assign(out);
2621 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, out->stack_offset);
2624 out = output("Tangent Normal");
2625 if(!out->links.empty()) {
2626 compiler.stack_assign(out);
2627 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, out->stack_offset);
2630 /*out = output("Fade");
2631 if(!out->links.empty()) {
2632 compiler.stack_assign(out);
2633 compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, out->stack_offset);
2638 void HairInfoNode::compile(OSLCompiler& compiler)
2640 compiler.add(this, "node_hair_info");
2645 ValueNode::ValueNode()
2646 : ShaderNode("value")
2650 add_output("Value", SHADER_SOCKET_FLOAT);
2653 void ValueNode::compile(SVMCompiler& compiler)
2655 ShaderOutput *val_out = output("Value");
2657 compiler.stack_assign(val_out);
2658 compiler.add_node(NODE_VALUE_F, __float_as_int(value), val_out->stack_offset);
2661 void ValueNode::compile(OSLCompiler& compiler)
2663 compiler.parameter("value_value", value);
2664 compiler.add(this, "node_value");
2669 ColorNode::ColorNode()
2670 : ShaderNode("color")
2672 value = make_float3(0.0f, 0.0f, 0.0f);
2674 add_output("Color", SHADER_SOCKET_COLOR);
2677 void ColorNode::compile(SVMCompiler& compiler)
2679 ShaderOutput *color_out = output("Color");
2681 if(color_out && !color_out->links.empty()) {
2682 compiler.stack_assign(color_out);
2683 compiler.add_node(NODE_VALUE_V, color_out->stack_offset);
2684 compiler.add_node(NODE_VALUE_V, value);
2688 void ColorNode::compile(OSLCompiler& compiler)
2690 compiler.parameter_color("color_value", value);
2692 compiler.add(this, "node_value");
2697 AddClosureNode::AddClosureNode()
2698 : ShaderNode("add_closure")
2700 add_input("Closure1", SHADER_SOCKET_CLOSURE);
2701 add_input("Closure2", SHADER_SOCKET_CLOSURE);
2702 add_output("Closure", SHADER_SOCKET_CLOSURE);
2705 void AddClosureNode::compile(SVMCompiler& compiler)
2707 /* handled in the SVM compiler */
2710 void AddClosureNode::compile(OSLCompiler& compiler)
2712 compiler.add(this, "node_add_closure");
2717 MixClosureNode::MixClosureNode()
2718 : ShaderNode("mix_closure")
2720 special_type = SHADER_SPECIAL_TYPE_MIX_CLOSURE;
2722 add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2723 add_input("Closure1", SHADER_SOCKET_CLOSURE);
2724 add_input("Closure2", SHADER_SOCKET_CLOSURE);
2725 add_output("Closure", SHADER_SOCKET_CLOSURE);
2728 void MixClosureNode::compile(SVMCompiler& compiler)
2730 /* handled in the SVM compiler */
2733 void MixClosureNode::compile(OSLCompiler& compiler)
2735 compiler.add(this, "node_mix_closure");
2740 MixClosureWeightNode::MixClosureWeightNode()
2741 : ShaderNode("mix_closure_weight")
2743 add_input("Weight", SHADER_SOCKET_FLOAT, 1.0f);
2744 add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2745 add_output("Weight1", SHADER_SOCKET_FLOAT);
2746 add_output("Weight2", SHADER_SOCKET_FLOAT);
2749 void MixClosureWeightNode::compile(SVMCompiler& compiler)
2751 ShaderInput *weight_in = input("Weight");
2752 ShaderInput *fac_in = input("Fac");
2753 ShaderOutput *weight1_out = output("Weight1");
2754 ShaderOutput *weight2_out = output("Weight2");
2756 compiler.stack_assign(weight_in);
2757 compiler.stack_assign(fac_in);
2758 compiler.stack_assign(weight1_out);
2759 compiler.stack_assign(weight2_out);
2761 compiler.add_node(NODE_MIX_CLOSURE,
2762 compiler.encode_uchar4(fac_in->stack_offset, weight_in->stack_offset,
2763 weight1_out->stack_offset, weight2_out->stack_offset));
2766 void MixClosureWeightNode::compile(OSLCompiler& compiler)
2773 InvertNode::InvertNode()
2774 : ShaderNode("invert")
2776 add_input("Fac", SHADER_SOCKET_FLOAT, 1.0f);
2777 add_input("Color", SHADER_SOCKET_COLOR);
2778 add_output("Color", SHADER_SOCKET_COLOR);
2781 void InvertNode::compile(SVMCompiler& compiler)
2783 ShaderInput *fac_in = input("Fac");
2784 ShaderInput *color_in = input("Color");
2785 ShaderOutput *color_out = output("Color");
2787 compiler.stack_assign(fac_in);
2788 compiler.stack_assign(color_in);
2789 compiler.stack_assign(color_out);
2791 compiler.add_node(NODE_INVERT, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2794 void InvertNode::compile(OSLCompiler& compiler)
2796 compiler.add(this, "node_invert");
2804 type = ustring("Mix");
2808 add_input("Fac", SHADER_SOCKET_FLOAT, 0.5f);
2809 add_input("Color1", SHADER_SOCKET_COLOR);
2810 add_input("Color2", SHADER_SOCKET_COLOR);
2811 add_output("Color", SHADER_SOCKET_COLOR);
2814 static ShaderEnum mix_type_init()
2818 enm.insert("Mix", NODE_MIX_BLEND);
2819 enm.insert("Add", NODE_MIX_ADD);
2820 enm.insert("Multiply", NODE_MIX_MUL);
2821 enm.insert("Screen", NODE_MIX_SCREEN);
2822 enm.insert("Overlay", NODE_MIX_OVERLAY);
2823 enm.insert("Subtract", NODE_MIX_SUB);
2824 enm.insert("Divide", NODE_MIX_DIV);
2825 enm.insert("Difference", NODE_MIX_DIFF);
2826 enm.insert("Darken", NODE_MIX_DARK);
2827 enm.insert("Lighten", NODE_MIX_LIGHT);
2828 enm.insert("Dodge", NODE_MIX_DODGE);
2829 enm.insert("Burn", NODE_MIX_BURN);
2830 enm.insert("Hue", NODE_MIX_HUE);
2831 enm.insert("Saturation", NODE_MIX_SAT);
2832 enm.insert("Value", NODE_MIX_VAL);
2833 enm.insert("Color", NODE_MIX_COLOR);
2834 enm.insert("Soft Light", NODE_MIX_SOFT);
2835 enm.insert("Linear Light", NODE_MIX_LINEAR);
2840 ShaderEnum MixNode::type_enum = mix_type_init();
2842 void MixNode::compile(SVMCompiler& compiler)
2844 ShaderInput *fac_in = input("Fac");
2845 ShaderInput *color1_in = input("Color1");
2846 ShaderInput *color2_in = input("Color2");
2847 ShaderOutput *color_out = output("Color");
2849 compiler.stack_assign(fac_in);
2850 compiler.stack_assign(color1_in);
2851 compiler.stack_assign(color2_in);
2852 compiler.stack_assign(color_out);
2854 compiler.add_node(NODE_MIX, fac_in->stack_offset, color1_in->stack_offset, color2_in->stack_offset);
2855 compiler.add_node(NODE_MIX, type_enum[type], color_out->stack_offset);
2858 compiler.add_node(NODE_MIX, 0, color_out->stack_offset);
2859 compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, color_out->stack_offset);
2863 void MixNode::compile(OSLCompiler& compiler)
2865 compiler.parameter("type", type);
2866 compiler.parameter("Clamp", use_clamp);
2867 compiler.add(this, "node_mix");
2871 CombineRGBNode::CombineRGBNode()
2872 : ShaderNode("combine_rgb")
2874 add_input("R", SHADER_SOCKET_FLOAT);
2875 add_input("G", SHADER_SOCKET_FLOAT);
2876 add_input("B", SHADER_SOCKET_FLOAT);
2877 add_output("Image", SHADER_SOCKET_COLOR);
2880 void CombineRGBNode::compile(SVMCompiler& compiler)
2882 ShaderInput *red_in = input("R");
2883 ShaderInput *green_in = input("G");
2884 ShaderInput *blue_in = input("B");
2885 ShaderOutput *color_out = output("Image");
2887 compiler.stack_assign(color_out);
2889 compiler.stack_assign(red_in);
2890 compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
2892 compiler.stack_assign(green_in);
2893 compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
2895 compiler.stack_assign(blue_in);
2896 compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
2899 void CombineRGBNode::compile(OSLCompiler& compiler)
2901 compiler.add(this, "node_combine_rgb");
2905 CombineHSVNode::CombineHSVNode()
2906 : ShaderNode("combine_hsv")
2908 add_input("H", SHADER_SOCKET_FLOAT);
2909 add_input("S", SHADER_SOCKET_FLOAT);
2910 add_input("V", SHADER_SOCKET_FLOAT);
2911 add_output("Color", SHADER_SOCKET_COLOR);
2914 void CombineHSVNode::compile(SVMCompiler& compiler)
2916 ShaderInput *hue_in = input("H");
2917 ShaderInput *saturation_in = input("S");
2918 ShaderInput *value_in = input("V");
2919 ShaderOutput *color_out = output("Color");
2921 compiler.stack_assign(color_out);
2922 compiler.stack_assign(hue_in);
2923 compiler.stack_assign(saturation_in);
2924 compiler.stack_assign(value_in);
2926 compiler.add_node(NODE_COMBINE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
2927 compiler.add_node(NODE_COMBINE_HSV, color_out->stack_offset);
2930 void CombineHSVNode::compile(OSLCompiler& compiler)
2932 compiler.add(this, "node_combine_hsv");
2936 GammaNode::GammaNode()
2937 : ShaderNode("gamma")
2939 add_input("Color", SHADER_SOCKET_COLOR);
2940 add_input("Gamma", SHADER_SOCKET_FLOAT);
2941 add_output("Color", SHADER_SOCKET_COLOR);
2944 void GammaNode::compile(SVMCompiler& compiler)
2946 ShaderInput *color_in = input("Color");
2947 ShaderInput *gamma_in = input("Gamma");
2948 ShaderOutput *color_out = output("Color");
2950 compiler.stack_assign(color_in);
2951 compiler.stack_assign(gamma_in);
2952 compiler.stack_assign(color_out);
2954 compiler.add_node(NODE_GAMMA, gamma_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
2957 void GammaNode::compile(OSLCompiler& compiler)
2959 compiler.add(this, "node_gamma");
2962 /* Bright Contrast */
2963 BrightContrastNode::BrightContrastNode()
2964 : ShaderNode("brightness")
2966 add_input("Color", SHADER_SOCKET_COLOR);
2967 add_input("Bright", SHADER_SOCKET_FLOAT);
2968 add_input("Contrast", SHADER_SOCKET_FLOAT);
2969 add_output("Color", SHADER_SOCKET_COLOR);
2972 void BrightContrastNode::compile(SVMCompiler& compiler)
2974 ShaderInput *color_in = input("Color");
2975 ShaderInput *bright_in = input("Bright");
2976 ShaderInput *contrast_in = input("Contrast");
2977 ShaderOutput *color_out = output("Color");
2979 compiler.stack_assign(color_in);
2980 compiler.stack_assign(bright_in);
2981 compiler.stack_assign(contrast_in);
2982 compiler.stack_assign(color_out);
2984 compiler.add_node(NODE_BRIGHTCONTRAST,
2985 color_in->stack_offset, color_out->stack_offset,
2986 compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
2989 void BrightContrastNode::compile(OSLCompiler& compiler)
2991 compiler.add(this, "node_brightness");
2995 SeparateRGBNode::SeparateRGBNode()
2996 : ShaderNode("separate_rgb")
2998 add_input("Image", SHADER_SOCKET_COLOR);
2999 add_output("R", SHADER_SOCKET_FLOAT);
3000 add_output("G", SHADER_SOCKET_FLOAT);
3001 add_output("B", SHADER_SOCKET_FLOAT);
3004 void SeparateRGBNode::compile(SVMCompiler& compiler)
3006 ShaderInput *color_in = input("Image");
3007 ShaderOutput *red_out = output("R");
3008 ShaderOutput *green_out = output("G");
3009 ShaderOutput *blue_out = output("B");
3011 compiler.stack_assign(color_in);
3013 compiler.stack_assign(red_out);
3014 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
3016 compiler.stack_assign(green_out);
3017 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
3019 compiler.stack_assign(blue_out);
3020 compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
3023 void SeparateRGBNode::compile(OSLCompiler& compiler)
3025 compiler.add(this, "node_separate_rgb");
3029 SeparateHSVNode::SeparateHSVNode()
3030 : ShaderNode("separate_hsv")
3032 add_input("Color", SHADER_SOCKET_COLOR);
3033 add_output("H", SHADER_SOCKET_FLOAT);
3034 add_output("S", SHADER_SOCKET_FLOAT);
3035 add_output("V", SHADER_SOCKET_FLOAT);
3038 void SeparateHSVNode::compile(SVMCompiler& compiler)
3040 ShaderInput *color_in = input("Color");
3041 ShaderOutput *hue_out = output("H");
3042 ShaderOutput *saturation_out = output("S");
3043 ShaderOutput *value_out = output("V");
3045 compiler.stack_assign(color_in);
3046 compiler.stack_assign(hue_out);
3047 compiler.stack_assign(saturation_out);
3048 compiler.stack_assign(value_out);
3050 compiler.add_node(NODE_SEPARATE_HSV, color_in->stack_offset, hue_out->stack_offset, saturation_out->stack_offset);
3051 compiler.add_node(NODE_SEPARATE_HSV, value_out->stack_offset);
3055 void SeparateHSVNode::compile(OSLCompiler& compiler)
3057 compiler.add(this, "node_separate_hsv");
3060 /* Hue Saturation Value */
3064 add_input("Hue", SHADER_SOCKET_FLOAT);
3065 add_input("Saturation", SHADER_SOCKET_FLOAT);
3066 add_input("Value", SHADER_SOCKET_FLOAT);
3067 add_input("Fac", SHADER_SOCKET_FLOAT);
3068 add_input("Color", SHADER_SOCKET_COLOR);
3069 add_output("Color", SHADER_SOCKET_COLOR);
3072 void HSVNode::compile(SVMCompiler& compiler)
3074 ShaderInput *hue_in = input("Hue");
3075 ShaderInput *saturation_in = input("Saturation");
3076 ShaderInput *value_in = input("Value");
3077 ShaderInput *fac_in = input("Fac");
3078 ShaderInput *color_in = input("Color");
3079 ShaderOutput *color_out = output("Color");
3081 compiler.stack_assign(hue_in);
3082 compiler.stack_assign(saturation_in);
3083 compiler.stack_assign(value_in);
3084 compiler.stack_assign(fac_in);
3085 compiler.stack_assign(color_in);
3086 compiler.stack_assign(color_out);
3088 compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
3089 compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
3092 void HSVNode::compile(OSLCompiler& compiler)
3094 compiler.add(this, "node_hsv");
3099 AttributeNode::AttributeNode()
3100 : ShaderNode("attribute")
3104 add_output("Color", SHADER_SOCKET_COLOR);
3105 add_output("Vector", SHADER_SOCKET_VECTOR);
3106 add_output("Fac", SHADER_SOCKET_FLOAT);
3109 void AttributeNode::attributes(AttributeRequestSet *attributes)
3111 ShaderOutput *color_out = output("Color");
3112 ShaderOutput *vector_out = output("Vector");
3113 ShaderOutput *fac_out = output("Fac");
3115 if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty())
3116 attributes->add(attribute);
3118 ShaderNode::attributes(attributes);
3121 void AttributeNode::compile(SVMCompiler& compiler)
3123 ShaderOutput *color_out = output("Color");
3124 ShaderOutput *vector_out = output("Vector");
3125 ShaderOutput *fac_out = output("Fac");
3126 NodeType attr_node = NODE_ATTR;
3128 if(bump == SHADER_BUMP_DX)
3129 attr_node = NODE_ATTR_BUMP_DX;
3130 else if(bump == SHADER_BUMP_DY)
3131 attr_node = NODE_ATTR_BUMP_DY;
3133 if(!color_out->links.empty() || !vector_out->links.empty()) {
3134 int attr = compiler.attribute(attribute);
3136 if(!color_out->links.empty()) {
3137 compiler.stack_assign(color_out);
3138 compiler.add_node(attr_node, attr, color_out->stack_offset, NODE_ATTR_FLOAT3);
3140 if(!vector_out->links.empty()) {
3141 compiler.stack_assign(vector_out);
3142 compiler.add_node(attr_node, attr, vector_out->stack_offset, NODE_ATTR_FLOAT3);
3146 if(!fac_out->links.empty()) {
3147 int attr = compiler.attribute(attribute);
3149 compiler.stack_assign(fac_out);
3150 compiler.add_node(attr_node, attr, fac_out->stack_offset, NODE_ATTR_FLOAT);
3154 void AttributeNode::compile(OSLCompiler& compiler)
3156 if(bump == SHADER_BUMP_DX)
3157 compiler.parameter("bump_offset", "dx");
3158 else if(bump == SHADER_BUMP_DY)
3159 compiler.parameter("bump_offset", "dy");
3161 compiler.parameter("bump_offset", "center");
3163 compiler.parameter("name", attribute.c_str());
3164 compiler.add(this, "node_attribute");
3169 CameraNode::CameraNode()
3170 : ShaderNode("camera")
3172 add_output("View Vector", SHADER_SOCKET_VECTOR);
3173 add_output("View Z Depth", SHADER_SOCKET_FLOAT);
3174 add_output("View Distance", SHADER_SOCKET_FLOAT);
3177 void CameraNode::compile(SVMCompiler& compiler)
3179 ShaderOutput *vector_out = output("View Vector");
3180 ShaderOutput *z_depth_out = output("View Z Depth");
3181 ShaderOutput *distance_out = output("View Distance");
3183 compiler.stack_assign(vector_out);
3184 compiler.stack_assign(z_depth_out);
3185 compiler.stack_assign(distance_out);
3186 compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
3189 void CameraNode::compile(OSLCompiler& compiler)
3191 compiler.add(this, "node_camera");
3196 FresnelNode::FresnelNode()
3197 : ShaderNode("fresnel")
3199 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3200 add_input("IOR", SHADER_SOCKET_FLOAT, 1.45f);
3201 add_output("Fac", SHADER_SOCKET_FLOAT);
3204 void FresnelNode::compile(SVMCompiler& compiler)
3206 ShaderInput *normal_in = input("Normal");
3207 ShaderInput *ior_in = input("IOR");
3208 ShaderOutput *fac_out = output("Fac");
3210 compiler.stack_assign(ior_in);
3211 compiler.stack_assign(fac_out);
3214 compiler.stack_assign(normal_in);
3216 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));
3219 void FresnelNode::compile(OSLCompiler& compiler)
3221 compiler.add(this, "node_fresnel");
3226 LayerWeightNode::LayerWeightNode()
3227 : ShaderNode("layer_weight")
3229 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
3230 add_input("Blend", SHADER_SOCKET_FLOAT, 0.5f);
3232 add_output("Fresnel", SHADER_SOCKET_FLOAT);
3233 add_output("Facing", SHADER_SOCKET_FLOAT);
3236 void LayerWeightNode::compile(SVMCompiler& compiler)
3238 ShaderInput *normal_in = input("Normal");
3239 ShaderInput *blend_in = input("Blend");
3242 compiler.stack_assign(normal_in);
3245 compiler.stack_assign(blend_in);
3247 ShaderOutput *fresnel_out = output("Fresnel");
3248 if(!fresnel_out->links.empty()) {
3249 compiler.stack_assign(fresnel_out);
3250 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3251 compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL, normal_in->stack_offset, fresnel_out->stack_offset));
3254 ShaderOutput *facing_out = output("Facing");
3255 if(!facing_out->links.empty()) {
3256 compiler.stack_assign(facing_out);
3257 compiler.add_node(NODE_LAYER_WEIGHT, blend_in->stack_offset, __float_as_int(blend_in->value.x),
3258 compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING, normal_in->stack_offset, facing_out->stack_offset));
3262 void LayerWeightNode::compile(OSLCompiler& compiler)
3264 compiler.add(this, "node_layer_weight");
3269 WireframeNode::WireframeNode()
3270 : ShaderNode("wireframe")
3272 add_input("Size", SHADER_SOCKET_FLOAT, 0.01f);
3273 add_output("Fac", SHADER_SOCKET_FLOAT);
3275 use_pixel_size = false;
3278 void WireframeNode::compile(SVMCompiler& compiler)
3280 ShaderInput *size_in = input("Size");
3281 ShaderOutput *fac_out = output("Fac");
3283 compiler.stack_assign(size_in);
3284 compiler.stack_assign(fac_out);
3285 compiler.add_node(NODE_WIREFRAME, size_in->stack_offset, fac_out->stack_offset, use_pixel_size);
3288 void WireframeNode::compile(OSLCompiler& compiler)
3290 compiler.parameter("use_pixel_size", use_pixel_size);
3291 compiler.add(this, "node_wireframe");
3296 WavelengthNode::WavelengthNode()
3297 : ShaderNode("wavelength")
3299 add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
3300 add_output("Color", SHADER_SOCKET_COLOR);
3303 void WavelengthNode::compile(SVMCompiler& compiler)
3305 ShaderInput *wavelength_in = input("Wavelength");
3306 ShaderOutput *color_out = output("Color");
3308 compiler.stack_assign(wavelength_in);
3309 compiler.stack_assign(color_out);
3310 compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset);
3313 void WavelengthNode::compile(OSLCompiler& compiler)
3315 compiler.add(this, "node_wavelength");
3320 BlackbodyNode::BlackbodyNode()
3321 : ShaderNode("blackbody")
3323 add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f);
3324 add_output("Color", SHADER_SOCKET_COLOR);
3327 void BlackbodyNode::compile(SVMCompiler& compiler)
3329 ShaderInput *temperature_in = input("Temperature");
3330 ShaderOutput *color_out = output("Color");
3332 compiler.stack_assign(temperature_in);
3333 compiler.stack_assign(color_out);
3334 compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset);
3337 void BlackbodyNode::compile(OSLCompiler& compiler)
3339 compiler.add(this, "node_blackbody");
3344 OutputNode::OutputNode()
3345 : ShaderNode("output")
3347 add_input("Surface", SHADER_SOCKET_CLOSURE);
3348 add_input("Volume", SHADER_SOCKET_CLOSURE);
3349 add_input("Displacement", SHADER_SOCKET_FLOAT);
3350 add_input("Normal", SHADER_SOCKET_NORMAL);
3353 void OutputNode::compile(SVMCompiler& compiler)
3355 if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
3356 ShaderInput *displacement_in = input("Displacement");
3358 if(displacement_in->link) {
3359 compiler.stack_assign(displacement_in);
3360 compiler.add_node(NODE_SET_DISPLACEMENT, displacement_in->stack_offset);
3365 void OutputNode::compile(OSLCompiler& compiler)
3367 if(compiler.output_type() == SHADER_TYPE_SURFACE)
3368 compiler.add(this, "node_output_surface");
3369 else if(compiler.output_type() == SHADER_TYPE_VOLUME)
3370 compiler.add(this, "node_output_volume");
3371 else if(compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
3372 compiler.add(this, "node_output_displacement");
3377 MathNode::MathNode()
3378 : ShaderNode("math")
3380 type = ustring("Add");
3384 add_input("Value1", SHADER_SOCKET_FLOAT);
3385 add_input("Value2", SHADER_SOCKET_FLOAT);
3386 add_output("Value", SHADER_SOCKET_FLOAT);
3389 static ShaderEnum math_type_init()
3393 enm.insert("Add", NODE_MATH_ADD);
3394 enm.insert("Subtract", NODE_MATH_SUBTRACT);
3395 enm.insert("Multiply", NODE_MATH_MULTIPLY);
3396 enm.insert("Divide", NODE_MATH_DIVIDE);
3397 enm.insert("Sine", NODE_MATH_SINE);
3398 enm.insert("Cosine", NODE_MATH_COSINE);
3399 enm.insert("Tangent", NODE_MATH_TANGENT);
3400 enm.insert("Arcsine", NODE_MATH_ARCSINE);
3401 enm.insert("Arccosine", NODE_MATH_ARCCOSINE);
3402 enm.insert("Arctangent", NODE_MATH_ARCTANGENT);
3403 enm.insert("Power", NODE_MATH_POWER);
3404 enm.insert("Logarithm", NODE_MATH_LOGARITHM);
3405 enm.insert("Minimum", NODE_MATH_MINIMUM);
3406 enm.insert("Maximum", NODE_MATH_MAXIMUM);
3407 enm.insert("Round", NODE_MATH_ROUND);
3408 enm.insert("Less Than", NODE_MATH_LESS_THAN);
3409 enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
3410 enm.insert("Modulo", NODE_MATH_MODULO);
3415 ShaderEnum MathNode::type_enum = math_type_init();
3417 void MathNode::compile(SVMCompiler& compiler)
3419 ShaderInput *value1_in = input("Value1");
3420 ShaderInput *value2_in = input("Value2");
3421 ShaderOutput *value_out = output("Value");
3423 compiler.stack_assign(value1_in);
3424 compiler.stack_assign(value2_in);
3425 compiler.stack_assign(value_out);
3427 compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
3428 compiler.add_node(NODE_MATH, value_out->stack_offset);
3431 compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
3432 compiler.add_node(NODE_MATH, value_out->stack_offset);
3436 void MathNode::compile(OSLCompiler& compiler)
3438 compiler.parameter("type", type);
3439 compiler.parameter("Clamp", use_clamp);
3440 compiler.add(this, "node_math");
3445 VectorMathNode::VectorMathNode()
3446 : ShaderNode("vector_math")
3448 type = ustring("Add");
3450 add_input("Vector1", SHADER_SOCKET_VECTOR);
3451 add_input("Vector2", SHADER_SOCKET_VECTOR);
3452 add_output("Value", SHADER_SOCKET_FLOAT);
3453 add_output("Vector", SHADER_SOCKET_VECTOR);
3456 static ShaderEnum vector_math_type_init()
3460 enm.insert("Add", NODE_VECTOR_MATH_ADD);
3461 enm.insert("Subtract", NODE_VECTOR_MATH_SUBTRACT);
3462 enm.insert("Average", NODE_VECTOR_MATH_AVERAGE);
3463 enm.insert("Dot Product", NODE_VECTOR_MATH_DOT_PRODUCT);
3464 enm.insert("Cross Product", NODE_VECTOR_MATH_CROSS_PRODUCT);
3465 enm.insert("Normalize", NODE_VECTOR_MATH_NORMALIZE);
3470 ShaderEnum VectorMathNode::type_enum = vector_math_type_init();
3472 void VectorMathNode::compile(SVMCompiler& compiler)
3474 ShaderInput *vector1_in = input("Vector1");
3475 ShaderInput *vector2_in = input("Vector2");
3476 ShaderOutput *value_out = output("Value");
3477 ShaderOutput *vector_out = output("Vector");
3479 compiler.stack_assign(vector1_in);
3480 compiler.stack_assign(vector2_in);
3481 compiler.stack_assign(value_out);
3482 compiler.stack_assign(vector_out);
3484 compiler.add_node(NODE_VECTOR_MATH, type_enum[type], vector1_in->stack_offset, vector2_in->stack_offset);
3485 compiler.add_node(NODE_VECTOR_MATH, value_out->stack_offset, vector_out->stack_offset);
3488 void VectorMathNode::compile(OSLCompiler& compiler)
3490 compiler.parameter("type", type);
3491 compiler.add(this, "node_vector_math");
3494 /* VectorTransform */
3496 VectorTransformNode::VectorTransformNode()
3497 : ShaderNode("vector_transform")
3499 type = ustring("Vector");
3500 convert_from = ustring("world");
3501 convert_to = ustring("object");
3503 add_input("Vector", SHADER_SOCKET_VECTOR);
3504 add_output("Vector", SHADER_SOCKET_VECTOR);
3507 static ShaderEnum vector_transform_type_init()
3511 enm.insert("Vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
3512 enm.insert("Point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
3513 enm.insert("Normal", NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
3518 static ShaderEnum vector_transform_convert_space_init()
3522 enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
3523 enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
3524 enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA);
3529 ShaderEnum VectorTransformNode::type_enum = vector_transform_type_init();
3530 ShaderEnum VectorTransformNode::convert_space_enum = vector_transform_convert_space_init();
3532 void VectorTransformNode::compile(SVMCompiler& compiler)
3534 ShaderInput *vector_in = input("Vector");
3535 ShaderOutput *vector_out = output("Vector");
3537 compiler.stack_assign(vector_in);
3538 compiler.stack_assign(vector_out);
3540 compiler.add_node(NODE_VECTOR_TRANSFORM,
3541 compiler.encode_uchar4(type_enum[type], convert_space_enum[convert_from], convert_space_enum[convert_to]),
3542 compiler.encode_uchar4(vector_in->stack_offset, vector_out->stack_offset));
3545 void VectorTransformNode::compile(OSLCompiler& compiler)
3547 compiler.parameter("type", type);
3548 compiler.parameter("convert_from", convert_from);
3549 compiler.parameter("convert_to", convert_to);
3550 compiler.add(this, "node_vector_transform");
3555 BumpNode::BumpNode()
3556 : ShaderNode("bump")
3560 /* this input is used by the user, but after graph transform it is no longer
3561 * used and moved to sampler center/x/y instead */
3562 add_input("Height", SHADER_SOCKET_FLOAT);
3564 add_input("SampleCenter", SHADER_SOCKET_FLOAT);
3565 add_input("SampleX", SHADER_SOCKET_FLOAT);
3566 add_input("SampleY", SHADER_SOCKET_FLOAT);
3567 add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
3568 add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
3569 add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
3571 add_output("Normal", SHADER_SOCKET_NORMAL);
3574 void BumpNode::compile(SVMCompiler& compiler)
3576 ShaderInput *center_in = input("SampleCenter");
3577 ShaderInput *dx_in = input("SampleX");
3578 ShaderInput *dy_in = input("SampleY");
3579 ShaderInput *normal_in = input("Normal");
3580 ShaderInput *strength_in = input("Strength");
3581 ShaderInput *distance_in = input("Distance");
3582 ShaderOutput *normal_out = output("Normal");
3584 compiler.stack_assign(center_in);
3585 compiler.stack_assign(dx_in);
3586 compiler.stack_assign(dy_in);
3587 compiler.stack_assign(strength_in);
3588 compiler.stack_assign(distance_in);
3589 compiler.stack_assign(normal_out);
3592 compiler.stack_assign(normal_in);
3594 /* pack all parameters in the node */
3595 compiler.add_node(NODE_SET_BUMP,
3596 compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
3597 compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
3598 dy_in->stack_offset, strength_in->stack_offset),
3599 normal_out->stack_offset);
3602 void BumpNode::compile(OSLCompiler& compiler)
3604 compiler.parameter("invert", invert);
3605 compiler.add(this, "node_bump");
3610 RGBCurvesNode::RGBCurvesNode()
3611 : ShaderNode("rgb_curves")
3613 add_input("Fac", SHADER_SOCKET_FLOAT);
3614 add_input("Color", SHADER_SOCKET_COLOR);
3615 add_output("Color", SHADER_SOCKET_COLOR);
3618 void RGBCurvesNode::compile(SVMCompiler& compiler)
3620 ShaderInput *fac_in = input("Fac");
3621 ShaderInput *color_in = input("Color");
3622 ShaderOutput *color_out = output("Color");
3624 compiler.stack_assign(fac_in);
3625 compiler.stack_assign(color_in);
3626 compiler.stack_assign(color_out);
3628 compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
3629 compiler.add_array(curves, RAMP_TABLE_SIZE);
3632 void RGBCurvesNode::compile(OSLCompiler& compiler)
3634 float ramp[RAMP_TABLE_SIZE][3];
3636 for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3637 ramp[i][0] = curves[i].x;
3638 ramp[i][1] = curves[i].y;
3639 ramp[i][2] = curves[i].z;
3642 compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3643 compiler.add(this, "node_rgb_curves");
3646 /* VectorCurvesNode */
3648 VectorCurvesNode::VectorCurvesNode()
3649 : ShaderNode("vector_curves")
3651 add_input("Fac", SHADER_SOCKET_FLOAT);
3652 add_input("Vector", SHADER_SOCKET_VECTOR);
3653 add_output("Vector", SHADER_SOCKET_VECTOR);
3656 void VectorCurvesNode::compile(SVMCompiler& compiler)
3658 ShaderInput *fac_in = input("Fac");
3659 ShaderInput *vector_in = input("Vector");
3660 ShaderOutput *vector_out = output("Vector");
3662 compiler.stack_assign(fac_in);
3663 compiler.stack_assign(vector_in);
3664 compiler.stack_assign(vector_out);
3666 compiler.add_node(NODE_VECTOR_CURVES, fac_in->stack_offset, vector_in->stack_offset, vector_out->stack_offset);
3667 compiler.add_array(curves, RAMP_TABLE_SIZE);
3670 void VectorCurvesNode::compile(OSLCompiler& compiler)
3672 float ramp[RAMP_TABLE_SIZE][3];
3674 for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3675 ramp[i][0] = curves[i].x;
3676 ramp[i][1] = curves[i].y;
3677 ramp[i][2] = curves[i].z;
3680 compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
3681 compiler.add(this, "node_vector_curves");
3686 RGBRampNode::RGBRampNode()
3687 : ShaderNode("rgb_ramp")
3689 add_input("Fac", SHADER_SOCKET_FLOAT);
3690 add_output("Color", SHADER_SOCKET_COLOR);
3691 add_output("Alpha", SHADER_SOCKET_FLOAT);
3696 void RGBRampNode::compile(SVMCompiler& compiler)
3698 ShaderInput *fac_in = input("Fac");
3699 ShaderOutput *color_out = output("Color");
3700 ShaderOutput *alpha_out = output("Alpha");
3702 compiler.stack_assign(fac_in);
3703 if(!color_out->links.empty())
3704 compiler.stack_assign(color_out);
3705 if(!alpha_out->links.empty())
3706 compiler.stack_assign(alpha_out);
3708 compiler.add_node(NODE_RGB_RAMP,
3709 compiler.encode_uchar4(
3710 fac_in->stack_offset,
3711 color_out->stack_offset,
3712 alpha_out->stack_offset),
3714 compiler.add_array(ramp, RAMP_TABLE_SIZE);
3717 void RGBRampNode::compile(OSLCompiler& compiler)
3719 /* OSL shader only takes separate RGB and A array, split the RGBA base array */
3720 /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
3721 float ramp_color[RAMP_TABLE_SIZE][3];
3722 float ramp_alpha[RAMP_TABLE_SIZE];
3724 for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
3725 ramp_color[i][0] = ramp[i].x;
3726 ramp_color[i][1] = ramp[i].y;
3727 ramp_color[i][2] = ramp[i].z;
3728 ramp_alpha[i] = ramp[i].w;
3731 compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
3732 compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
3733 compiler.parameter("ramp_interpolate", interpolate);
3735 compiler.add(this, "node_rgb_ramp");
3738 /* Set Normal Node */
3740 SetNormalNode::SetNormalNode()
3741 : ShaderNode("set_normal")
3743 add_input("Direction", SHADER_SOCKET_VECTOR);
3744 add_output("Normal", SHADER_SOCKET_NORMAL);
3747 void SetNormalNode::compile(SVMCompiler& compiler)
3749 ShaderInput *direction_in = input("Direction");
3750 ShaderOutput *normal_out = output("Normal");
3752 compiler.stack_assign(direction_in);
3753 compiler.stack_assign(normal_out);
3755 compiler.add_node(NODE_CLOSURE_SET_NORMAL, direction_in->stack_offset, normal_out->stack_offset);
3758 void SetNormalNode::compile(OSLCompiler& compiler)
3760 compiler.add(this, "node_set_normal");