36 if (!done.contains(
in->link->parent)) {
96 if (socket->name() ==
name) {
107 if (socket->name() ==
name) {
118 if (socket->name() ==
name) {
129 if (socket->name() ==
name) {
140 if (socket && socket->
link) {
195 if (input_a->
link ==
nullptr && input_b->
link ==
nullptr) {
201 else if (input_a->
link !=
nullptr && input_b->
link !=
nullptr) {
203 if (input_a->
link != input_b->
link) {
238 node->set_owner(
this);
254 LOG_WARNING <<
"Graph connect: input already connected.";
261 LOG_WARNING <<
"Shader graph connect: can only connect closure to closure ("
262 << from->
parent->
name.c_str() <<
"." << from->
name().c_str() <<
" to "
275 emission->set_strength(1.0f);
279 convert_in = convert->input(
"Strength");
282 convert_in = convert->input(
"Color");
287 convert_in = convert->inputs[0];
291 connect(convert->outputs[0], to);
296 from->
links.push_back(to);
393 if (surface_in->
link) {
396 if (volume_in->
link) {
409 if (
node !=
nullptr && !dependencies.contains(
node)) {
414 dependencies.insert(
node);
431 nnodemap[
node] = nnode;
462 bool any_node_removed =
false;
484 bool all_links_removed =
true;
492 all_links_removed =
false;
496 if (all_links_removed) {
497 removed[tonode->
id] =
true;
508 removed[proxy->
id] =
true;
509 any_node_removed =
true;
514 if (any_node_removed) {
517 for (
size_t i = 0; i <
nodes.size(); i++) {
519 if (!removed[
node->id]) {
524 nodes = std::move(newnodes);
537 queue<ShaderNode *> traverse_queue;
539 const bool has_displacement = (
output()->
input(
"Displacement")->
link !=
nullptr);
543 if (!check_node_inputs_has_links(
node)) {
544 traverse_queue.push(
node);
545 scheduled.insert(
node);
549 while (!traverse_queue.empty()) {
551 traverse_queue.pop();
554 if (
output->links.empty()) {
561 if (scheduled.contains(input->
parent)) {
568 if (check_node_inputs_traversed(input->
parent, done)) {
569 traverse_queue.push(input->
parent);
570 scheduled.insert(input->
parent);
575 node->constant_fold(folder);
583 if (has_displacement && !
output()->input(
"Displacement")->link) {
595 node->simplify_settings(scene);
613 map<ustring, ShaderNodeSet> candidates;
614 queue<ShaderNode *> traverse_queue;
615 int num_deduplicated = 0;
619 if (!check_node_inputs_has_links(
node)) {
620 traverse_queue.push(
node);
621 scheduled.insert(
node);
625 while (!traverse_queue.empty()) {
627 traverse_queue.pop();
630 bool has_output_links =
false;
633 has_output_links =
true;
634 if (scheduled.contains(input->
parent)) {
641 if (check_node_inputs_traversed(input->
parent, done)) {
642 traverse_queue.push(input->
parent);
643 scheduled.insert(input->
parent);
648 if (!has_output_links) {
654 if (
node != other_node &&
node->equals(*other_node)) {
655 merge_with = other_node;
660 if (merge_with !=
nullptr) {
661 for (
int i = 0; i <
node->outputs.size(); ++i) {
667 candidates[
node->type->name].insert(
node);
671 if (num_deduplicated > 0) {
672 LOG_TRACE <<
"Deduplicated " << num_deduplicated <<
" nodes.";
682 if (volume_in->
link ==
nullptr) {
686 bool has_valid_volume =
false;
688 using ShaderNodeAndNonLinear = std::pair<ShaderNode *, bool>;
689 set<ShaderNodeAndNonLinear, ShaderNodeIDAndBoolComparator> scheduled;
690 queue<ShaderNodeAndNonLinear> traverse_queue;
693 traverse_queue.emplace(volume_in->
link->
parent,
false);
694 scheduled.insert({volume_in->
link->
parent,
false});
697 while (!traverse_queue.empty()) {
698 auto [
node, nonlinear] = traverse_queue.front();
699 traverse_queue.pop();
703 if (nonlinear &&
node->type == AttributeNode::get_node_type()) {
706 nonlinear = nonlinear || !
node->is_linear_operation();
709 if (
node->has_volume_support()) {
710 has_valid_volume =
true;
714 if (input->
link ==
nullptr) {
718 if (scheduled.contains({input_node, nonlinear})) {
721 traverse_queue.emplace(input_node, nonlinear);
722 scheduled.insert({input_node, nonlinear});
728 if (
node->type == AttributeNode::get_node_type() &&
731 LOG_TRACE <<
"Volume attribute node " <<
node->name <<
" uses stochastic sampling";
736 if (!has_valid_volume) {
738 LOG_TRACE <<
"Disconnect meaningless volume output.";
745 visited[
node->id] =
true;
746 on_stack[
node->id] =
true;
752 if (on_stack[depnode->
id]) {
755 LOG_WARNING <<
"Shader graph: detected cycle in graph, connection removed.";
757 else if (!visited[depnode->
id]) {
764 on_stack[
node->id] =
false;
773 if (!displacement_in->
link) {
786 md5.
append((uint8_t *)&link_id,
sizeof(link_id));
828 if (!visited[
node->id]) {
843 for (
size_t i = 0; i <
nodes.size(); i++) {
845 if (visited[
node->id]) {
850 nodes = std::move(newnodes);
858 for (
size_t i = 0; i <
nodes.
size(); i++) {
873 for (
size_t i = 0; i <
nodes.
size(); i++) {
903 if (!normal_transform) {
976 node->bump_filter_width = bump->get_filter_width();
978 for (
const NodePair &pair : nodes_dx) {
980 pair.second->bump_filter_width = bump->get_filter_width();
982 for (
const NodePair &pair : nodes_dy) {
984 pair.second->bump_filter_width = bump->get_filter_width();
1022 if (!displacement_in->
link) {
1032 bump->set_use_object_space(use_object_space);
1033 bump->set_distance(1.0f);
1046 for (
const NodePair &pair : nodes_center) {
1048 pair.second->bump_filter_width = bump->get_filter_width();
1050 for (
const NodePair &pair : nodes_dx) {
1052 pair.second->bump_filter_width = bump->get_filter_width();
1054 for (
const NodePair &pair : nodes_dy) {
1056 pair.second->bump_filter_width = bump->get_filter_width();
1129 connect(weight_out, weight_in);
1132 weight1_out = mix_node->
output(
"Weight1");
1133 weight2_out = mix_node->
output(
"Weight2");
1137 weight1_out = weight_out;
1138 weight2_out = weight_out;
1149 ShaderInput *weight_in =
node->input((volume) ?
"VolumeMixWeight" :
"SurfaceMixWeight");
1158 if (weight_in->
link || weight_value != 0.0f) {
1161 if (weight_in->
link) {
1165 math_node->set_value1(weight_value);
1172 math_node->set_value2(1.0f);
1175 weight_out = math_node->
output(
"Value");
1176 if (weight_in->
link) {
1183 connect(weight_out, weight_in);
1193 int num_closures = 0;
1226 return num_closures;
1231 FILE *fd = fopen(filename,
"w");
1233 if (fd ==
nullptr) {
1234 LOG_ERROR <<
"Error opening file for dumping the graph: " << filename;
1238 fprintf(fd,
"digraph shader_graph {\n");
1239 fprintf(fd,
"ranksep=1.5\n");
1240 fprintf(fd,
"rankdir=LR\n");
1241 fprintf(fd,
"splines=false\n");
1244 fprintf(fd,
"// NODE: %p\n",
node);
1245 fprintf(fd,
"\"%p\" [shape=record,label=\"{",
node);
1246 if (!
node->inputs.empty()) {
1249 if (socket !=
node->inputs[0]) {
1252 fprintf(fd,
"<IN_%p>%s", socket, socket->
name().c_str());
1256 fprintf(fd,
"%s",
node->name.c_str());
1258 fprintf(fd,
" (bump:center)");
1261 fprintf(fd,
" (bump:dx)");
1264 fprintf(fd,
" (bump:dy)");
1266 if (!
node->outputs.empty()) {
1269 if (socket !=
node->outputs[0]) {
1272 fprintf(fd,
"<OUT_%p>%s", socket, socket->
name().c_str());
1276 fprintf(fd,
"}\"]");
1283 "// CONNECTION: OUT_%p->IN_%p (%s:%s)\n",
1287 input->
name().c_str());
1289 "\"%p\":\"OUT_%p\":e -> \"%p\":\"IN_%p\":w [label=\"\"]\n",
bool from_auto_conversion
void append(const uint8_t *data, const int nbytes)
unique_ptr< ShaderManager > shader_manager
void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input)
unique_ptr_vector< ShaderNode > nodes
void simplify(Scene *scene)
T * create_node(Args &&...args)
void add_node(unique_ptr< ShaderNode > &&node)
void disconnect(ShaderOutput *from)
void break_cycles(ShaderNode *node, vector< bool > &visited, vector< bool > &on_stack)
void copy_nodes(ShaderNodeSet &nodes, ShaderNodeMap &nnodemap)
void finalize(Scene *scene, bool do_bump=false, bool bump_in_object_space=false)
void compute_displacement_hash()
void default_inputs(bool do_osl)
void connect(ShaderOutput *from, ShaderInput *to)
void relink(ShaderInput *from, ShaderInput *to)
void optimize_volume_output()
void constant_fold(Scene *scene)
void transform_multi_closure(ShaderNode *node, ShaderOutput *weight_out, bool volume)
void remove_proxy_nodes()
void simplify_settings(Scene *scene)
void bump_from_displacement(bool use_object_space)
pair< ShaderNode *const, ShaderNode * > NodePair
void dump_graph(const char *filename)
const SocketType & socket_type
SocketType::Type type() const
ShaderInput * input(const char *name)
void remove_input(ShaderInput *input)
ShaderNodeSpecialType special_type
virtual void global_attributes(Shader *shader, AttributeRequestSet *attributes)
virtual bool equals(const ShaderNode &other)
void disconnect_unused_input(const char *name)
ShaderNode(const NodeType *type)
void create_inputs_outputs(const NodeType *type)
unique_ptr_vector< ShaderInput > inputs
ShaderOutput * output(const char *name)
virtual void attributes(Shader *shader, AttributeRequestSet *attributes)
unique_ptr_vector< ShaderOutput > outputs
void disconnect() override
vector< ShaderInput * > links
bool has_surface_link() const
void push_back(unique_ptr< T > &&value)
void erase(const T *value)
#define CLOSURE_IS_VOLUME(type)
#define MAX_VOLUME_STACK_SIZE
#define CLOSURE_IS_PRINCIPLED(type)
#define CLOSURE_IS_BSSRDF(type)
#define CLOSURE_IS_BSDF_MULTISCATTER(type)
#define CCL_NAMESPACE_END
#define assert(assertion)
@ NODE_VECTOR_TRANSFORM_TYPE_NORMAL
@ CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID
@ CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID
@ CLOSURE_BSDF_PHYSICAL_CONDUCTOR
@ CLOSURE_BSDF_HAIR_HUANG_ID
@ CLOSURE_BSDF_F82_CONDUCTOR
@ CLOSURE_BSDF_HAIR_CHIANG_ID
@ NODE_VECTOR_MATH_DOT_PRODUCT
@ NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT
@ NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD
@ ATTR_STD_GENERATED_TRANSFORM
ccl_device_inline float3 one_float3()
bool check_node_inputs_traversed(const ShaderNode *node, const ShaderNodeSet &done)
bool check_node_inputs_has_links(const ShaderNode *node)
@ SHADER_SPECIAL_TYPE_PROXY
@ SHADER_SPECIAL_TYPE_OUTPUT_AOV
@ SHADER_SPECIAL_TYPE_COMBINE_CLOSURE
@ SHADER_SPECIAL_TYPE_BUMP
@ SHADER_SPECIAL_TYPE_AUTOCONVERT
@ SHADER_SPECIAL_TYPE_OSL
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
map< ShaderNode *, ShaderNode *, ShaderNodeIDComparator > ShaderNodeMap
vector< SocketType, std::allocator< SocketType > > inputs
vector< SocketType, std::allocator< SocketType > > outputs
void set_value(const SocketType &socket, const Node &other, const SocketType &other_socket)
void copy_value(const SocketType &socket, const Node &other, const SocketType &other_socket)
bool equals_value(const Node &other, const SocketType &socket) const
Node(const NodeType *type, ustring name=ustring())