Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/render/graph.cpp
| Show All 27 Lines | |||||
| #include "util/util_queue.h" | #include "util/util_queue.h" | ||||
| CCL_NAMESPACE_BEGIN | CCL_NAMESPACE_BEGIN | ||||
| namespace { | namespace { | ||||
| bool check_node_inputs_has_links(const ShaderNode *node) | bool check_node_inputs_has_links(const ShaderNode *node) | ||||
| { | { | ||||
| foreach (const ShaderInput *in, node->inputs) { | foreach (const ShaderInput *in, node->get_inputs()) { | ||||
| if (in->link) { | if (in->get_link()) { | ||||
| return true; | return true; | ||||
| } | } | ||||
| } | } | ||||
| return false; | return false; | ||||
| } | } | ||||
| bool check_node_inputs_traversed(const ShaderNode *node, const ShaderNodeSet &done) | bool check_node_inputs_traversed(const ShaderNode *node, const ShaderNodeSet &done) | ||||
| { | { | ||||
| foreach (const ShaderInput *in, node->inputs) { | foreach (const ShaderInput *in, node->get_inputs()) { | ||||
| if (in->link) { | if (in->get_link()) { | ||||
| if (done.find(in->link->parent) == done.end()) { | if (done.find(in->get_link()->get_parent()) == done.end()) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| } /* namespace */ | } /* namespace */ | ||||
| /* Sockets */ | /* Sockets */ | ||||
| void ShaderInput::disconnect() | void ShaderInput::disconnect() | ||||
| { | { | ||||
| if (link) { | if (link) { | ||||
| link->links.erase(remove(link->links.begin(), link->links.end(), this), link->links.end()); | link->get_links().erase(remove(link->get_links().begin(), link->get_links().end(), this), | ||||
| link->get_links().end()); | |||||
| } | } | ||||
| link = NULL; | link = NULL; | ||||
| } | } | ||||
| void ShaderOutput::disconnect() | void ShaderOutput::disconnect() | ||||
| { | { | ||||
| foreach (ShaderInput *sock, links) { | foreach (ShaderInput *sock, links) { | ||||
| sock->link = NULL; | sock->get_link() = NULL; | ||||
| } | } | ||||
| links.clear(); | links.clear(); | ||||
| } | } | ||||
| /* Node */ | /* Node */ | ||||
| ShaderNode::ShaderNode(const NodeType *type) : Node(type) | ShaderNode::ShaderNode(const NodeType *type) : Node(type) | ||||
| { | { | ||||
| name = type->name; | name = type->get_name(); | ||||
| id = -1; | id = -1; | ||||
| bump = SHADER_BUMP_NONE; | bump = SHADER_BUMP_NONE; | ||||
| special_type = SHADER_SPECIAL_TYPE_NONE; | special_type = SHADER_SPECIAL_TYPE_NONE; | ||||
| create_inputs_outputs(type); | create_inputs_outputs(type); | ||||
| } | } | ||||
| ShaderNode::~ShaderNode() | ShaderNode::~ShaderNode() | ||||
| { | { | ||||
| foreach (ShaderInput *socket, inputs) | foreach (ShaderInput *socket, inputs) | ||||
| delete socket; | delete socket; | ||||
| foreach (ShaderOutput *socket, outputs) | foreach (ShaderOutput *socket, outputs) | ||||
| delete socket; | delete socket; | ||||
| } | } | ||||
| void ShaderNode::create_inputs_outputs(const NodeType *type) | void ShaderNode::create_inputs_outputs(const NodeType *type) | ||||
| { | { | ||||
| foreach (const SocketType &socket, type->inputs) { | foreach (const SocketType &socket, type->get_inputs()) { | ||||
| if (socket.flags & SocketType::LINKABLE) { | if (socket.get_flags() & SocketType::LINKABLE) { | ||||
| inputs.push_back(new ShaderInput(socket, this)); | inputs.push_back(new ShaderInput(socket, this)); | ||||
| } | } | ||||
| } | } | ||||
| foreach (const SocketType &socket, type->outputs) { | foreach (const SocketType &socket, type->get_outputs()) { | ||||
| outputs.push_back(new ShaderOutput(socket, this)); | outputs.push_back(new ShaderOutput(socket, this)); | ||||
| } | } | ||||
| } | } | ||||
| ShaderInput *ShaderNode::input(const char *name) | ShaderInput *ShaderNode::input(const char *name) | ||||
| { | { | ||||
| foreach (ShaderInput *socket, inputs) { | foreach (ShaderInput *socket, inputs) { | ||||
| if (socket->name() == name) | if (socket->name() == name) | ||||
| Show All 28 Lines | foreach (ShaderOutput *socket, outputs) | ||||
| if (socket->name() == name) | if (socket->name() == name) | ||||
| return socket; | return socket; | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| void ShaderNode::remove_input(ShaderInput *input) | void ShaderNode::remove_input(ShaderInput *input) | ||||
| { | { | ||||
| assert(input->link == NULL); | assert(input->get_link() == NULL); | ||||
| delete input; | delete input; | ||||
| inputs.erase(remove(inputs.begin(), inputs.end(), input), inputs.end()); | inputs.erase(remove(inputs.begin(), inputs.end(), input), inputs.end()); | ||||
| } | } | ||||
| void ShaderNode::attributes(Shader *shader, AttributeRequestSet *attributes) | void ShaderNode::attributes(Shader *shader, AttributeRequestSet *attributes) | ||||
| { | { | ||||
| foreach (ShaderInput *input, inputs) { | foreach (ShaderInput *input, inputs) { | ||||
| if (!input->link) { | if (!input->get_link()) { | ||||
| if (input->flags() & SocketType::LINK_TEXTURE_GENERATED) { | if (input->flags() & SocketType::LINK_TEXTURE_GENERATED) { | ||||
| if (shader->has_surface) | if (shader->get_has_surface()) | ||||
| attributes->add(ATTR_STD_GENERATED); | attributes->add(ATTR_STD_GENERATED); | ||||
| if (shader->has_volume) | if (shader->get_has_volume()) | ||||
| attributes->add(ATTR_STD_GENERATED_TRANSFORM); | attributes->add(ATTR_STD_GENERATED_TRANSFORM); | ||||
| } | } | ||||
| else if (input->flags() & SocketType::LINK_TEXTURE_UV) { | else if (input->flags() & SocketType::LINK_TEXTURE_UV) { | ||||
| if (shader->has_surface) | if (shader->get_has_surface()) | ||||
| attributes->add(ATTR_STD_UV); | attributes->add(ATTR_STD_UV); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| bool ShaderNode::equals(const ShaderNode &other) | bool ShaderNode::equals(const ShaderNode &other) | ||||
| { | { | ||||
| if (type != other.type || bump != other.bump) { | if (type != other.type || bump != other.bump) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| assert(inputs.size() == other.inputs.size()); | assert(inputs.size() == other.inputs.size()); | ||||
| /* Compare unlinkable sockets */ | /* Compare unlinkable sockets */ | ||||
| foreach (const SocketType &socket, type->inputs) { | foreach (const SocketType &socket, type->get_inputs()) { | ||||
| if (!(socket.flags & SocketType::LINKABLE)) { | if (!(socket.get_flags() & SocketType::LINKABLE)) { | ||||
| if (!Node::equals_value(other, socket)) { | if (!Node::equals_value(other, socket)) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /* Compare linkable input sockets */ | /* Compare linkable input sockets */ | ||||
| for (int i = 0; i < inputs.size(); ++i) { | for (int i = 0; i < inputs.size(); ++i) { | ||||
| ShaderInput *input_a = inputs[i], *input_b = other.inputs[i]; | ShaderInput *input_a = inputs[i], *input_b = other.inputs[i]; | ||||
| if (input_a->link == NULL && input_b->link == NULL) { | if (input_a->get_link() == NULL && input_b->get_link() == NULL) { | ||||
| /* Unconnected inputs are expected to have the same value. */ | /* Unconnected inputs are expected to have the same value. */ | ||||
| if (!Node::equals_value(other, input_a->socket_type)) { | if (!Node::equals_value(other, input_a->get_socket_type())) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| else if (input_a->link != NULL && input_b->link != NULL) { | else if (input_a->get_link() != NULL && input_b->get_link() != NULL) { | ||||
| /* Expect links are to come from the same exact socket. */ | /* Expect links are to come from the same exact socket. */ | ||||
| if (input_a->link != input_b->link) { | if (input_a->get_link() != input_b->get_link()) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| /* One socket has a link and another has not, inputs can't be | /* One socket has a link and another has not, inputs can't be | ||||
| * considered equal. | * considered equal. | ||||
| */ | */ | ||||
| return false; | return false; | ||||
| Show All 18 Lines | ShaderGraph::~ShaderGraph() | ||||
| clear_nodes(); | clear_nodes(); | ||||
| } | } | ||||
| ShaderNode *ShaderGraph::add(ShaderNode *node) | ShaderNode *ShaderGraph::add(ShaderNode *node) | ||||
| { | { | ||||
| assert(!finalized); | assert(!finalized); | ||||
| simplified = false; | simplified = false; | ||||
| node->id = num_node_ids++; | node->set_id(num_node_ids++); | ||||
| nodes.push_back(node); | nodes.push_back(node); | ||||
| return node; | return node; | ||||
| } | } | ||||
| OutputNode *ShaderGraph::output() | OutputNode *ShaderGraph::output() | ||||
| { | { | ||||
| return (OutputNode *)nodes.front(); | return (OutputNode *)nodes.front(); | ||||
| } | } | ||||
| void ShaderGraph::connect(ShaderOutput *from, ShaderInput *to) | void ShaderGraph::connect(ShaderOutput *from, ShaderInput *to) | ||||
| { | { | ||||
| assert(!finalized); | assert(!finalized); | ||||
| assert(from && to); | assert(from && to); | ||||
| if (to->link) { | if (to->get_link()) { | ||||
| fprintf(stderr, "Cycles shader graph connect: input already connected.\n"); | fprintf(stderr, "Cycles shader graph connect: input already connected.\n"); | ||||
| return; | return; | ||||
| } | } | ||||
| if (from->type() != to->type()) { | if (from->type() != to->type()) { | ||||
| /* can't do automatic conversion from closure */ | /* can't do automatic conversion from closure */ | ||||
| if (from->type() == SocketType::CLOSURE) { | if (from->type() == SocketType::CLOSURE) { | ||||
| fprintf(stderr, | fprintf(stderr, | ||||
| "Cycles shader graph connect: can only connect closure to closure " | "Cycles shader graph connect: can only connect closure to closure " | ||||
| "(%s.%s to %s.%s).\n", | "(%s.%s to %s.%s).\n", | ||||
| from->parent->name.c_str(), | from->get_parent()->get_name().c_str(), | ||||
| from->name().c_str(), | from->name().c_str(), | ||||
| to->parent->name.c_str(), | to->get_parent()->get_name().c_str(), | ||||
| to->name().c_str()); | to->name().c_str()); | ||||
| return; | return; | ||||
| } | } | ||||
| /* add automatic conversion node in case of type mismatch */ | /* add automatic conversion node in case of type mismatch */ | ||||
| ShaderNode *convert; | ShaderNode *convert; | ||||
| ShaderInput *convert_in; | ShaderInput *convert_in; | ||||
| if (to->type() == SocketType::CLOSURE) { | if (to->type() == SocketType::CLOSURE) { | ||||
| EmissionNode *emission = create_node<EmissionNode>(); | EmissionNode *emission = create_node<EmissionNode>(); | ||||
| emission->set_color(make_float3(1.0f, 1.0f, 1.0f)); | emission->set_color(make_float3(1.0f, 1.0f, 1.0f)); | ||||
| emission->set_strength(1.0f); | emission->set_strength(1.0f); | ||||
| convert = add(emission); | convert = add(emission); | ||||
| /* Connect float inputs to Strength to save an additional Falue->Color conversion. */ | /* Connect float inputs to Strength to save an additional Falue->Color conversion. */ | ||||
| if (from->type() == SocketType::FLOAT) { | if (from->type() == SocketType::FLOAT) { | ||||
| convert_in = convert->input("Strength"); | convert_in = convert->input("Strength"); | ||||
| } | } | ||||
| else { | else { | ||||
| convert_in = convert->input("Color"); | convert_in = convert->input("Color"); | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| convert = add(create_node<ConvertNode>(from->type(), to->type(), true)); | convert = add(create_node<ConvertNode>(from->type(), to->type(), true)); | ||||
| convert_in = convert->inputs[0]; | convert_in = convert->get_inputs()[0]; | ||||
| } | } | ||||
| connect(from, convert_in); | connect(from, convert_in); | ||||
| connect(convert->outputs[0], to); | connect(convert->get_outputs()[0], to); | ||||
| } | } | ||||
| else { | else { | ||||
| /* types match, just connect */ | /* types match, just connect */ | ||||
| to->link = from; | to->get_link() = from; | ||||
| from->links.push_back(to); | from->get_links().push_back(to); | ||||
| } | } | ||||
| } | } | ||||
| void ShaderGraph::disconnect(ShaderOutput *from) | void ShaderGraph::disconnect(ShaderOutput *from) | ||||
| { | { | ||||
| assert(!finalized); | assert(!finalized); | ||||
| simplified = false; | simplified = false; | ||||
| from->disconnect(); | from->disconnect(); | ||||
| } | } | ||||
| void ShaderGraph::disconnect(ShaderInput *to) | void ShaderGraph::disconnect(ShaderInput *to) | ||||
| { | { | ||||
| assert(!finalized); | assert(!finalized); | ||||
| assert(to->link); | assert(to->get_link()); | ||||
| simplified = false; | simplified = false; | ||||
| to->disconnect(); | to->disconnect(); | ||||
| } | } | ||||
| void ShaderGraph::relink(ShaderInput *from, ShaderInput *to) | void ShaderGraph::relink(ShaderInput *from, ShaderInput *to) | ||||
| { | { | ||||
| ShaderOutput *out = from->link; | ShaderOutput *out = from->get_link(); | ||||
| if (out) { | if (out) { | ||||
| disconnect(from); | disconnect(from); | ||||
| connect(out, to); | connect(out, to); | ||||
| } | } | ||||
| to->parent->copy_value(to->socket_type, *(from->parent), from->socket_type); | to->get_parent()->copy_value( | ||||
| to->get_socket_type(), *(from->get_parent()), from->get_socket_type()); | |||||
| } | } | ||||
| void ShaderGraph::relink(ShaderOutput *from, ShaderOutput *to) | void ShaderGraph::relink(ShaderOutput *from, ShaderOutput *to) | ||||
| { | { | ||||
| /* Copy because disconnect modifies this list. */ | /* Copy because disconnect modifies this list. */ | ||||
| vector<ShaderInput *> outputs = from->links; | vector<ShaderInput *> outputs = from->get_links(); | ||||
| foreach (ShaderInput *sock, outputs) { | foreach (ShaderInput *sock, outputs) { | ||||
| disconnect(sock); | disconnect(sock); | ||||
| if (to) | if (to) | ||||
| connect(to, sock); | connect(to, sock); | ||||
| } | } | ||||
| } | } | ||||
| void ShaderGraph::relink(ShaderNode *node, ShaderOutput *from, ShaderOutput *to) | void ShaderGraph::relink(ShaderNode *node, ShaderOutput *from, ShaderOutput *to) | ||||
| { | { | ||||
| simplified = false; | simplified = false; | ||||
| /* Copy because disconnect modifies this list */ | /* Copy because disconnect modifies this list */ | ||||
| vector<ShaderInput *> outputs = from->links; | vector<ShaderInput *> outputs = from->get_links(); | ||||
| /* Bypass node by moving all links from "from" to "to" */ | /* Bypass node by moving all links from "from" to "to" */ | ||||
| foreach (ShaderInput *sock, node->inputs) { | foreach (ShaderInput *sock, node->get_inputs()) { | ||||
| if (sock->link) | if (sock->get_link()) | ||||
| disconnect(sock); | disconnect(sock); | ||||
| } | } | ||||
| foreach (ShaderInput *sock, outputs) { | foreach (ShaderInput *sock, outputs) { | ||||
| disconnect(sock); | disconnect(sock); | ||||
| if (to) | if (to) | ||||
| connect(to, sock); | connect(to, sock); | ||||
| } | } | ||||
| } | } | ||||
| void ShaderGraph::simplify(Scene *scene) | void ShaderGraph::simplify(Scene *scene) | ||||
| { | { | ||||
| if (!simplified) { | if (!simplified) { | ||||
| expand(); | expand(); | ||||
| default_inputs(scene->shader_manager->use_osl()); | default_inputs(scene->get_shader_manager()->use_osl()); | ||||
| clean(scene); | clean(scene); | ||||
| refine_bump_nodes(); | refine_bump_nodes(); | ||||
| simplified = true; | simplified = true; | ||||
| } | } | ||||
| } | } | ||||
| void ShaderGraph::finalize(Scene *scene, bool do_bump, bool do_simplify, bool bump_in_object_space) | void ShaderGraph::finalize(Scene *scene, bool do_bump, bool do_simplify, bool bump_in_object_space) | ||||
| Show All 9 Lines | if (!finalized) { | ||||
| if (do_bump) | if (do_bump) | ||||
| bump_from_displacement(bump_in_object_space); | bump_from_displacement(bump_in_object_space); | ||||
| ShaderInput *surface_in = output()->input("Surface"); | ShaderInput *surface_in = output()->input("Surface"); | ||||
| ShaderInput *volume_in = output()->input("Volume"); | ShaderInput *volume_in = output()->input("Volume"); | ||||
| /* todo: make this work when surface and volume closures are tangled up */ | /* todo: make this work when surface and volume closures are tangled up */ | ||||
| if (surface_in->link) | if (surface_in->get_link()) | ||||
| transform_multi_closure(surface_in->link->parent, NULL, false); | transform_multi_closure(surface_in->get_link()->get_parent(), NULL, false); | ||||
| if (volume_in->link) | if (volume_in->get_link()) | ||||
| transform_multi_closure(volume_in->link->parent, NULL, true); | transform_multi_closure(volume_in->get_link()->get_parent(), NULL, true); | ||||
| finalized = true; | finalized = true; | ||||
| } | } | ||||
| else if (do_simplify) { | else if (do_simplify) { | ||||
| simplify_settings(scene); | simplify_settings(scene); | ||||
| } | } | ||||
| } | } | ||||
| void ShaderGraph::find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input) | void ShaderGraph::find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input) | ||||
| { | { | ||||
| /* find all nodes that this input depends on directly and indirectly */ | /* find all nodes that this input depends on directly and indirectly */ | ||||
| ShaderNode *node = (input->link) ? input->link->parent : NULL; | ShaderNode *node = (input->get_link()) ? input->get_link()->get_parent() : NULL; | ||||
| if (node != NULL && dependencies.find(node) == dependencies.end()) { | if (node != NULL && dependencies.find(node) == dependencies.end()) { | ||||
| foreach (ShaderInput *in, node->inputs) | foreach (ShaderInput *in, node->get_inputs()) | ||||
| find_dependencies(dependencies, in); | find_dependencies(dependencies, in); | ||||
| dependencies.insert(node); | dependencies.insert(node); | ||||
| } | } | ||||
| } | } | ||||
| void ShaderGraph::clear_nodes() | void ShaderGraph::clear_nodes() | ||||
| { | { | ||||
| Show All 11 Lines | void ShaderGraph::copy_nodes(ShaderNodeSet &nodes, ShaderNodeMap &nnodemap) | ||||
| /* copy nodes */ | /* copy nodes */ | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| ShaderNode *nnode = node->clone(this); | ShaderNode *nnode = node->clone(this); | ||||
| nnodemap[node] = nnode; | nnodemap[node] = nnode; | ||||
| /* create new inputs and outputs to recreate links and ensure | /* create new inputs and outputs to recreate links and ensure | ||||
| * that we still point to valid SocketType if the NodeType | * that we still point to valid SocketType if the NodeType | ||||
| * changed in cloning, as it does for OSL nodes */ | * changed in cloning, as it does for OSL nodes */ | ||||
| nnode->inputs.clear(); | nnode->get_inputs().clear(); | ||||
| nnode->outputs.clear(); | nnode->get_outputs().clear(); | ||||
| nnode->create_inputs_outputs(nnode->type); | nnode->create_inputs_outputs(nnode->get_type()); | ||||
| } | } | ||||
| /* recreate links */ | /* recreate links */ | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| foreach (ShaderInput *input, node->inputs) { | foreach (ShaderInput *input, node->get_inputs()) { | ||||
| if (input->link) { | if (input->get_link()) { | ||||
| /* find new input and output */ | /* find new input and output */ | ||||
| ShaderNode *nfrom = nnodemap[input->link->parent]; | ShaderNode *nfrom = nnodemap[input->get_link()->get_parent()]; | ||||
| ShaderNode *nto = nnodemap[input->parent]; | ShaderNode *nto = nnodemap[input->get_parent()]; | ||||
| ShaderOutput *noutput = nfrom->output(input->link->name()); | ShaderOutput *noutput = nfrom->output(input->get_link()->name()); | ||||
| ShaderInput *ninput = nto->input(input->name()); | ShaderInput *ninput = nto->input(input->name()); | ||||
| /* connect */ | /* connect */ | ||||
| connect(noutput, ninput); | connect(noutput, ninput); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /* Graph simplification */ | /* Graph simplification */ | ||||
| /* ******************** */ | /* ******************** */ | ||||
| /* Remove proxy nodes. | /* Remove proxy nodes. | ||||
| * | * | ||||
| * These only exists temporarily when exporting groups, and we must remove them | * These only exists temporarily when exporting groups, and we must remove them | ||||
| * early so that node->attributes() and default links do not see them. | * early so that node->attributes() and default links do not see them. | ||||
| */ | */ | ||||
| void ShaderGraph::remove_proxy_nodes() | void ShaderGraph::remove_proxy_nodes() | ||||
| { | { | ||||
| vector<bool> removed(num_node_ids, false); | vector<bool> removed(num_node_ids, false); | ||||
| bool any_node_removed = false; | bool any_node_removed = false; | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| if (node->special_type == SHADER_SPECIAL_TYPE_PROXY) { | if (node->get_special_type() == SHADER_SPECIAL_TYPE_PROXY) { | ||||
| ConvertNode *proxy = static_cast<ConvertNode *>(node); | ConvertNode *proxy = static_cast<ConvertNode *>(node); | ||||
| ShaderInput *input = proxy->inputs[0]; | ShaderInput *input = proxy->get_inputs()[0]; | ||||
| ShaderOutput *output = proxy->outputs[0]; | ShaderOutput *output = proxy->get_outputs()[0]; | ||||
| /* bypass the proxy node */ | /* bypass the proxy node */ | ||||
| if (input->link) { | if (input->get_link()) { | ||||
| relink(proxy, output, input->link); | relink(proxy, output, input->get_link()); | ||||
| } | } | ||||
| else { | else { | ||||
| /* Copy because disconnect modifies this list */ | /* Copy because disconnect modifies this list */ | ||||
| vector<ShaderInput *> links(output->links); | vector<ShaderInput *> links(output->get_links()); | ||||
| foreach (ShaderInput *to, links) { | foreach (ShaderInput *to, links) { | ||||
| /* Remove any auto-convert nodes too if they lead to | /* Remove any auto-convert nodes too if they lead to | ||||
| * sockets with an automatically set default value. */ | * sockets with an automatically set default value. */ | ||||
| ShaderNode *tonode = to->parent; | ShaderNode *tonode = to->get_parent(); | ||||
| if (tonode->special_type == SHADER_SPECIAL_TYPE_AUTOCONVERT) { | if (tonode->get_special_type() == SHADER_SPECIAL_TYPE_AUTOCONVERT) { | ||||
| bool all_links_removed = true; | bool all_links_removed = true; | ||||
| vector<ShaderInput *> links = tonode->outputs[0]->links; | vector<ShaderInput *> links = tonode->get_outputs()[0]->get_links(); | ||||
| foreach (ShaderInput *autoin, links) { | foreach (ShaderInput *autoin, links) { | ||||
| if (autoin->flags() & SocketType::DEFAULT_LINK_MASK) | if (autoin->flags() & SocketType::DEFAULT_LINK_MASK) | ||||
| disconnect(autoin); | disconnect(autoin); | ||||
| else | else | ||||
| all_links_removed = false; | all_links_removed = false; | ||||
| } | } | ||||
| if (all_links_removed) | if (all_links_removed) | ||||
| removed[tonode->id] = true; | removed[tonode->get_id()] = true; | ||||
| } | } | ||||
| disconnect(to); | disconnect(to); | ||||
| /* transfer the default input value to the target socket */ | /* transfer the default input value to the target socket */ | ||||
| tonode->copy_value(to->socket_type, *proxy, input->socket_type); | tonode->copy_value(to->get_socket_type(), *proxy, input->get_socket_type()); | ||||
| } | } | ||||
| } | } | ||||
| removed[proxy->id] = true; | removed[proxy->get_id()] = true; | ||||
| any_node_removed = true; | any_node_removed = true; | ||||
| } | } | ||||
| } | } | ||||
| /* remove nodes */ | /* remove nodes */ | ||||
| if (any_node_removed) { | if (any_node_removed) { | ||||
| list<ShaderNode *> newnodes; | list<ShaderNode *> newnodes; | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| if (!removed[node->id]) | if (!removed[node->get_id()]) | ||||
| newnodes.push_back(node); | newnodes.push_back(node); | ||||
| else | else | ||||
| delete_node(node); | delete_node(node); | ||||
| } | } | ||||
| nodes = newnodes; | nodes = newnodes; | ||||
| } | } | ||||
| } | } | ||||
| /* Constant folding. | /* Constant folding. | ||||
| * | * | ||||
| * Try to constant fold some nodes, and pipe result directly to | * Try to constant fold some nodes, and pipe result directly to | ||||
| * the input socket of connected nodes. | * the input socket of connected nodes. | ||||
| */ | */ | ||||
| void ShaderGraph::constant_fold(Scene *scene) | void ShaderGraph::constant_fold(Scene *scene) | ||||
| { | { | ||||
| ShaderNodeSet done, scheduled; | ShaderNodeSet done, scheduled; | ||||
| queue<ShaderNode *> traverse_queue; | queue<ShaderNode *> traverse_queue; | ||||
| bool has_displacement = (output()->input("Displacement")->link != NULL); | bool has_displacement = (output()->input("Displacement")->get_link() != NULL); | ||||
| /* Schedule nodes which doesn't have any dependencies. */ | /* Schedule nodes which doesn't have any dependencies. */ | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| if (!check_node_inputs_has_links(node)) { | if (!check_node_inputs_has_links(node)) { | ||||
| traverse_queue.push(node); | traverse_queue.push(node); | ||||
| scheduled.insert(node); | scheduled.insert(node); | ||||
| } | } | ||||
| } | } | ||||
| while (!traverse_queue.empty()) { | while (!traverse_queue.empty()) { | ||||
| ShaderNode *node = traverse_queue.front(); | ShaderNode *node = traverse_queue.front(); | ||||
| traverse_queue.pop(); | traverse_queue.pop(); | ||||
| done.insert(node); | done.insert(node); | ||||
| foreach (ShaderOutput *output, node->outputs) { | foreach (ShaderOutput *output, node->get_outputs()) { | ||||
| if (output->links.size() == 0) { | if (output->get_links().size() == 0) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| /* Schedule node which was depending on the value, | /* Schedule node which was depending on the value, | ||||
| * when possible. Do it before disconnect. | * when possible. Do it before disconnect. | ||||
| */ | */ | ||||
| foreach (ShaderInput *input, output->links) { | foreach (ShaderInput *input, output->get_links()) { | ||||
| if (scheduled.find(input->parent) != scheduled.end()) { | if (scheduled.find(input->get_parent()) != scheduled.end()) { | ||||
| /* Node might not be optimized yet but scheduled already | /* Node might not be optimized yet but scheduled already | ||||
| * by other dependencies. No need to re-schedule it. | * by other dependencies. No need to re-schedule it. | ||||
| */ | */ | ||||
| continue; | continue; | ||||
| } | } | ||||
| /* Schedule node if its inputs are fully done. */ | /* Schedule node if its inputs are fully done. */ | ||||
| if (check_node_inputs_traversed(input->parent, done)) { | if (check_node_inputs_traversed(input->get_parent(), done)) { | ||||
| traverse_queue.push(input->parent); | traverse_queue.push(input->get_parent()); | ||||
| scheduled.insert(input->parent); | scheduled.insert(input->get_parent()); | ||||
| } | } | ||||
| } | } | ||||
| /* Optimize current node. */ | /* Optimize current node. */ | ||||
| ConstantFolder folder(this, node, output, scene); | ConstantFolder folder(this, node, output, scene); | ||||
| node->constant_fold(folder); | node->constant_fold(folder); | ||||
| } | } | ||||
| } | } | ||||
| /* Folding might have removed all nodes connected to the displacement output | /* Folding might have removed all nodes connected to the displacement output | ||||
| * even tho there is displacement to be applied, so add in a value node if | * even tho there is displacement to be applied, so add in a value node if | ||||
| * that happens to ensure there is still a valid graph for displacement. | * that happens to ensure there is still a valid graph for displacement. | ||||
| */ | */ | ||||
| if (has_displacement && !output()->input("Displacement")->link) { | if (has_displacement && !output()->input("Displacement")->get_link()) { | ||||
| ColorNode *value = (ColorNode *)add(create_node<ColorNode>()); | ColorNode *value = (ColorNode *)add(create_node<ColorNode>()); | ||||
| value->set_value(output()->get_displacement()); | value->set_value(output()->get_displacement()); | ||||
| connect(value->output("Color"), output()->input("Displacement")); | connect(value->output("Color"), output()->input("Displacement")); | ||||
| } | } | ||||
| } | } | ||||
| /* Simplification. */ | /* Simplification. */ | ||||
| Show All 30 Lines | void ShaderGraph::deduplicate_nodes() | ||||
| } | } | ||||
| while (!traverse_queue.empty()) { | while (!traverse_queue.empty()) { | ||||
| ShaderNode *node = traverse_queue.front(); | ShaderNode *node = traverse_queue.front(); | ||||
| traverse_queue.pop(); | traverse_queue.pop(); | ||||
| done.insert(node); | done.insert(node); | ||||
| /* Schedule the nodes which were depending on the current node. */ | /* Schedule the nodes which were depending on the current node. */ | ||||
| bool has_output_links = false; | bool has_output_links = false; | ||||
| foreach (ShaderOutput *output, node->outputs) { | foreach (ShaderOutput *output, node->get_outputs()) { | ||||
| foreach (ShaderInput *input, output->links) { | foreach (ShaderInput *input, output->get_links()) { | ||||
| has_output_links = true; | has_output_links = true; | ||||
| if (scheduled.find(input->parent) != scheduled.end()) { | if (scheduled.find(input->get_parent()) != scheduled.end()) { | ||||
| /* Node might not be optimized yet but scheduled already | /* Node might not be optimized yet but scheduled already | ||||
| * by other dependencies. No need to re-schedule it. | * by other dependencies. No need to re-schedule it. | ||||
| */ | */ | ||||
| continue; | continue; | ||||
| } | } | ||||
| /* Schedule node if its inputs are fully done. */ | /* Schedule node if its inputs are fully done. */ | ||||
| if (check_node_inputs_traversed(input->parent, done)) { | if (check_node_inputs_traversed(input->get_parent(), done)) { | ||||
| traverse_queue.push(input->parent); | traverse_queue.push(input->get_parent()); | ||||
| scheduled.insert(input->parent); | scheduled.insert(input->get_parent()); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /* Only need to care about nodes that are actually used */ | /* Only need to care about nodes that are actually used */ | ||||
| if (!has_output_links) { | if (!has_output_links) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| /* Try to merge this node with another one. */ | /* Try to merge this node with another one. */ | ||||
| ShaderNode *merge_with = NULL; | ShaderNode *merge_with = NULL; | ||||
| foreach (ShaderNode *other_node, candidates[node->type->name]) { | foreach (ShaderNode *other_node, candidates[node->get_type()->get_name()]) { | ||||
| if (node != other_node && node->equals(*other_node)) { | if (node != other_node && node->equals(*other_node)) { | ||||
| merge_with = other_node; | merge_with = other_node; | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| /* If found an equivalent, merge; otherwise keep node for later merges */ | /* If found an equivalent, merge; otherwise keep node for later merges */ | ||||
| if (merge_with != NULL) { | if (merge_with != NULL) { | ||||
| for (int i = 0; i < node->outputs.size(); ++i) { | for (int i = 0; i < node->get_outputs().size(); ++i) { | ||||
| relink(node, node->outputs[i], merge_with->outputs[i]); | relink(node, node->get_outputs()[i], merge_with->get_outputs()[i]); | ||||
| } | } | ||||
| num_deduplicated++; | num_deduplicated++; | ||||
| } | } | ||||
| else { | else { | ||||
| candidates[node->type->name].insert(node); | candidates[node->get_type()->get_name()].insert(node); | ||||
| } | } | ||||
| } | } | ||||
| if (num_deduplicated > 0) { | if (num_deduplicated > 0) { | ||||
| VLOG(1) << "Deduplicated " << num_deduplicated << " nodes."; | VLOG(1) << "Deduplicated " << num_deduplicated << " nodes."; | ||||
| } | } | ||||
| } | } | ||||
| /* Check whether volume output has meaningful nodes, otherwise | /* Check whether volume output has meaningful nodes, otherwise | ||||
| * disconnect the output. | * disconnect the output. | ||||
| */ | */ | ||||
| void ShaderGraph::verify_volume_output() | void ShaderGraph::verify_volume_output() | ||||
| { | { | ||||
| /* Check whether we can optimize the whole volume graph out. */ | /* Check whether we can optimize the whole volume graph out. */ | ||||
| ShaderInput *volume_in = output()->input("Volume"); | ShaderInput *volume_in = output()->input("Volume"); | ||||
| if (volume_in->link == NULL) { | if (volume_in->get_link() == NULL) { | ||||
| return; | return; | ||||
| } | } | ||||
| bool has_valid_volume = false; | bool has_valid_volume = false; | ||||
| ShaderNodeSet scheduled; | ShaderNodeSet scheduled; | ||||
| queue<ShaderNode *> traverse_queue; | queue<ShaderNode *> traverse_queue; | ||||
| /* Schedule volume output. */ | /* Schedule volume output. */ | ||||
| traverse_queue.push(volume_in->link->parent); | traverse_queue.push(volume_in->get_link()->get_parent()); | ||||
| scheduled.insert(volume_in->link->parent); | scheduled.insert(volume_in->get_link()->get_parent()); | ||||
| /* Traverse down the tree. */ | /* Traverse down the tree. */ | ||||
| while (!traverse_queue.empty()) { | while (!traverse_queue.empty()) { | ||||
| ShaderNode *node = traverse_queue.front(); | ShaderNode *node = traverse_queue.front(); | ||||
| traverse_queue.pop(); | traverse_queue.pop(); | ||||
| /* Node is fully valid for volume, can't optimize anything out. */ | /* Node is fully valid for volume, can't optimize anything out. */ | ||||
| if (node->has_volume_support()) { | if (node->has_volume_support()) { | ||||
| has_valid_volume = true; | has_valid_volume = true; | ||||
| break; | break; | ||||
| } | } | ||||
| foreach (ShaderInput *input, node->inputs) { | foreach (ShaderInput *input, node->get_inputs()) { | ||||
| if (input->link == NULL) { | if (input->get_link() == NULL) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| if (scheduled.find(input->link->parent) != scheduled.end()) { | if (scheduled.find(input->get_link()->get_parent()) != scheduled.end()) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| traverse_queue.push(input->link->parent); | traverse_queue.push(input->get_link()->get_parent()); | ||||
| scheduled.insert(input->link->parent); | scheduled.insert(input->get_link()->get_parent()); | ||||
| } | } | ||||
| } | } | ||||
| if (!has_valid_volume) { | if (!has_valid_volume) { | ||||
| VLOG(1) << "Disconnect meaningless volume output."; | VLOG(1) << "Disconnect meaningless volume output."; | ||||
| disconnect(volume_in->link); | disconnect(volume_in->get_link()); | ||||
| } | } | ||||
| } | } | ||||
| void ShaderGraph::break_cycles(ShaderNode *node, vector<bool> &visited, vector<bool> &on_stack) | void ShaderGraph::break_cycles(ShaderNode *node, vector<bool> &visited, vector<bool> &on_stack) | ||||
| { | { | ||||
| visited[node->id] = true; | visited[node->get_id()] = true; | ||||
| on_stack[node->id] = true; | on_stack[node->get_id()] = true; | ||||
| foreach (ShaderInput *input, node->inputs) { | foreach (ShaderInput *input, node->get_inputs()) { | ||||
| if (input->link) { | if (input->get_link()) { | ||||
| ShaderNode *depnode = input->link->parent; | ShaderNode *depnode = input->get_link()->get_parent(); | ||||
| if (on_stack[depnode->id]) { | if (on_stack[depnode->get_id()]) { | ||||
| /* break cycle */ | /* break cycle */ | ||||
| disconnect(input); | disconnect(input); | ||||
| fprintf(stderr, "Cycles shader graph: detected cycle in graph, connection removed.\n"); | fprintf(stderr, "Cycles shader graph: detected cycle in graph, connection removed.\n"); | ||||
| } | } | ||||
| else if (!visited[depnode->id]) { | else if (!visited[depnode->get_id()]) { | ||||
| /* visit dependencies */ | /* visit dependencies */ | ||||
| break_cycles(depnode, visited, on_stack); | break_cycles(depnode, visited, on_stack); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| on_stack[node->id] = false; | on_stack[node->get_id()] = false; | ||||
| } | } | ||||
| void ShaderGraph::compute_displacement_hash() | void ShaderGraph::compute_displacement_hash() | ||||
| { | { | ||||
| /* Compute hash of all nodes linked to displacement, to detect if we need | /* Compute hash of all nodes linked to displacement, to detect if we need | ||||
| * to recompute displacement when shader nodes change. */ | * to recompute displacement when shader nodes change. */ | ||||
| ShaderInput *displacement_in = output()->input("Displacement"); | ShaderInput *displacement_in = output()->input("Displacement"); | ||||
| if (!displacement_in->link) { | if (!displacement_in->get_link()) { | ||||
| displacement_hash = ""; | displacement_hash = ""; | ||||
| return; | return; | ||||
| } | } | ||||
| ShaderNodeSet nodes_displace; | ShaderNodeSet nodes_displace; | ||||
| find_dependencies(nodes_displace, displacement_in); | find_dependencies(nodes_displace, displacement_in); | ||||
| MD5Hash md5; | MD5Hash md5; | ||||
| foreach (ShaderNode *node, nodes_displace) { | foreach (ShaderNode *node, nodes_displace) { | ||||
| node->hash(md5); | node->hash(md5); | ||||
| foreach (ShaderInput *input, node->inputs) { | foreach (ShaderInput *input, node->get_inputs()) { | ||||
| int link_id = (input->link) ? input->link->parent->id : 0; | int link_id = (input->get_link()) ? input->get_link()->get_parent()->get_id() : 0; | ||||
| md5.append((uint8_t *)&link_id, sizeof(link_id)); | md5.append((uint8_t *)&link_id, sizeof(link_id)); | ||||
| md5.append((input->link) ? input->link->name().c_str() : ""); | md5.append((input->get_link()) ? input->get_link()->name().c_str() : ""); | ||||
| } | } | ||||
| if (node->special_type == SHADER_SPECIAL_TYPE_OSL) { | if (node->get_special_type() == SHADER_SPECIAL_TYPE_OSL) { | ||||
| /* Hash takes into account socket values, to detect changes | /* Hash takes into account socket values, to detect changes | ||||
| * in the code of the node we need an exception. */ | * in the code of the node we need an exception. */ | ||||
| OSLNode *oslnode = static_cast<OSLNode *>(node); | OSLNode *oslnode = static_cast<OSLNode *>(node); | ||||
| md5.append(oslnode->bytecode_hash); | md5.append(oslnode->get_bytecode_hash()); | ||||
| } | } | ||||
| } | } | ||||
| displacement_hash = md5.get_hex(); | displacement_hash = md5.get_hex(); | ||||
| } | } | ||||
| void ShaderGraph::clean(Scene *scene) | void ShaderGraph::clean(Scene *scene) | ||||
| { | { | ||||
| Show All 10 Lines | void ShaderGraph::clean(Scene *scene) | ||||
| * undefined, they are invalid input, the important thing is to not crash */ | * undefined, they are invalid input, the important thing is to not crash */ | ||||
| vector<bool> visited(num_node_ids, false); | vector<bool> visited(num_node_ids, false); | ||||
| vector<bool> on_stack(num_node_ids, false); | vector<bool> on_stack(num_node_ids, false); | ||||
| /* break cycles */ | /* break cycles */ | ||||
| break_cycles(output(), visited, on_stack); | break_cycles(output(), visited, on_stack); | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| if (node->special_type == SHADER_SPECIAL_TYPE_OUTPUT_AOV) { | if (node->get_special_type() == SHADER_SPECIAL_TYPE_OUTPUT_AOV) { | ||||
| break_cycles(node, visited, on_stack); | break_cycles(node, visited, on_stack); | ||||
| } | } | ||||
| } | } | ||||
| /* disconnect unused nodes */ | /* disconnect unused nodes */ | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| if (!visited[node->id]) { | if (!visited[node->get_id()]) { | ||||
| foreach (ShaderInput *to, node->inputs) { | foreach (ShaderInput *to, node->get_inputs()) { | ||||
| ShaderOutput *from = to->link; | ShaderOutput *from = to->get_link(); | ||||
| if (from) { | if (from) { | ||||
| to->link = NULL; | to->get_link() = NULL; | ||||
| from->links.erase(remove(from->links.begin(), from->links.end(), to), from->links.end()); | from->get_links().erase(remove(from->get_links().begin(), from->get_links().end(), to), | ||||
| from->get_links().end()); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /* remove unused nodes */ | /* remove unused nodes */ | ||||
| list<ShaderNode *> newnodes; | list<ShaderNode *> newnodes; | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| if (visited[node->id]) | if (visited[node->get_id()]) | ||||
| newnodes.push_back(node); | newnodes.push_back(node); | ||||
| else | else | ||||
| delete_node(node); | delete_node(node); | ||||
| } | } | ||||
| nodes = newnodes; | nodes = newnodes; | ||||
| } | } | ||||
| Show All 9 Lines | |||||
| { | { | ||||
| /* nodes can specify default texture coordinates, for now we give | /* nodes can specify default texture coordinates, for now we give | ||||
| * everything the position by default, except for the sky texture */ | * everything the position by default, except for the sky texture */ | ||||
| ShaderNode *geom = NULL; | ShaderNode *geom = NULL; | ||||
| ShaderNode *texco = NULL; | ShaderNode *texco = NULL; | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| foreach (ShaderInput *input, node->inputs) { | foreach (ShaderInput *input, node->get_inputs()) { | ||||
| if (!input->link && (!(input->flags() & SocketType::OSL_INTERNAL) || do_osl)) { | if (!input->get_link() && (!(input->flags() & SocketType::OSL_INTERNAL) || do_osl)) { | ||||
| if (input->flags() & SocketType::LINK_TEXTURE_GENERATED) { | if (input->flags() & SocketType::LINK_TEXTURE_GENERATED) { | ||||
| if (!texco) | if (!texco) | ||||
| texco = create_node<TextureCoordinateNode>(); | texco = create_node<TextureCoordinateNode>(); | ||||
| connect(texco->output("Generated"), input); | connect(texco->output("Generated"), input); | ||||
| } | } | ||||
| if (input->flags() & SocketType::LINK_TEXTURE_NORMAL) { | if (input->flags() & SocketType::LINK_TEXTURE_NORMAL) { | ||||
| if (!texco) | if (!texco) | ||||
| ▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
| void ShaderGraph::refine_bump_nodes() | void ShaderGraph::refine_bump_nodes() | ||||
| { | { | ||||
| /* we transverse the node graph looking for bump nodes, when we find them, | /* we transverse the node graph looking for bump nodes, when we find them, | ||||
| * like in bump_from_displacement(), we copy the sub-graph defined from "bump" | * like in bump_from_displacement(), we copy the sub-graph defined from "bump" | ||||
| * input to the inputs "center","dx" and "dy" What is in "bump" input is moved | * input to the inputs "center","dx" and "dy" What is in "bump" input is moved | ||||
| * to "center" input. */ | * to "center" input. */ | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| if (node->special_type == SHADER_SPECIAL_TYPE_BUMP && node->input("Height")->link) { | if (node->get_special_type() == SHADER_SPECIAL_TYPE_BUMP && | ||||
| node->input("Height")->get_link()) { | |||||
| ShaderInput *bump_input = node->input("Height"); | ShaderInput *bump_input = node->input("Height"); | ||||
| ShaderNodeSet nodes_bump; | ShaderNodeSet nodes_bump; | ||||
| /* make 2 extra copies of the subgraph defined in Bump input */ | /* make 2 extra copies of the subgraph defined in Bump input */ | ||||
| ShaderNodeMap nodes_dx; | ShaderNodeMap nodes_dx; | ||||
| ShaderNodeMap nodes_dy; | ShaderNodeMap nodes_dy; | ||||
| /* find dependencies for the given input */ | /* find dependencies for the given input */ | ||||
| find_dependencies(nodes_bump, bump_input); | find_dependencies(nodes_bump, bump_input); | ||||
| copy_nodes(nodes_bump, nodes_dx); | copy_nodes(nodes_bump, nodes_dx); | ||||
| copy_nodes(nodes_bump, nodes_dy); | copy_nodes(nodes_bump, nodes_dy); | ||||
| /* mark nodes to indicate they are use for bump computation, so | /* mark nodes to indicate they are use for bump computation, so | ||||
| that any texture coordinates are shifted by dx/dy when sampling */ | that any texture coordinates are shifted by dx/dy when sampling */ | ||||
| foreach (ShaderNode *node, nodes_bump) | foreach (ShaderNode *node, nodes_bump) | ||||
| node->bump = SHADER_BUMP_CENTER; | node->get_bump() = SHADER_BUMP_CENTER; | ||||
| foreach (NodePair &pair, nodes_dx) | foreach (NodePair &pair, nodes_dx) | ||||
| pair.second->bump = SHADER_BUMP_DX; | pair.second->get_bump() = SHADER_BUMP_DX; | ||||
| foreach (NodePair &pair, nodes_dy) | foreach (NodePair &pair, nodes_dy) | ||||
| pair.second->bump = SHADER_BUMP_DY; | pair.second->get_bump() = SHADER_BUMP_DY; | ||||
| ShaderOutput *out = bump_input->link; | ShaderOutput *out = bump_input->get_link(); | ||||
| ShaderOutput *out_dx = nodes_dx[out->parent]->output(out->name()); | ShaderOutput *out_dx = nodes_dx[out->get_parent()]->output(out->name()); | ||||
| ShaderOutput *out_dy = nodes_dy[out->parent]->output(out->name()); | ShaderOutput *out_dy = nodes_dy[out->get_parent()]->output(out->name()); | ||||
| connect(out_dx, node->input("SampleX")); | connect(out_dx, node->input("SampleX")); | ||||
| connect(out_dy, node->input("SampleY")); | connect(out_dy, node->input("SampleY")); | ||||
| /* add generated nodes */ | /* add generated nodes */ | ||||
| foreach (NodePair &pair, nodes_dx) | foreach (NodePair &pair, nodes_dx) | ||||
| add(pair.second); | add(pair.second); | ||||
| foreach (NodePair &pair, nodes_dy) | foreach (NodePair &pair, nodes_dy) | ||||
| Show All 22 Lines | void ShaderGraph::bump_from_displacement(bool use_object_space) | ||||
| * with each different geometry and texture coordinate nodes that generate | * with each different geometry and texture coordinate nodes that generate | ||||
| * different shifted coordinates. | * different shifted coordinates. | ||||
| * | * | ||||
| * these 3 displacement values are then fed into the bump node, which will | * these 3 displacement values are then fed into the bump node, which will | ||||
| * output the perturbed normal. */ | * output the perturbed normal. */ | ||||
| ShaderInput *displacement_in = output()->input("Displacement"); | ShaderInput *displacement_in = output()->input("Displacement"); | ||||
| if (!displacement_in->link) | if (!displacement_in->get_link()) | ||||
| return; | return; | ||||
| /* find dependencies for the given input */ | /* find dependencies for the given input */ | ||||
| ShaderNodeSet nodes_displace; | ShaderNodeSet nodes_displace; | ||||
| find_dependencies(nodes_displace, displacement_in); | find_dependencies(nodes_displace, displacement_in); | ||||
| /* copy nodes for 3 bump samples */ | /* copy nodes for 3 bump samples */ | ||||
| ShaderNodeMap nodes_center; | ShaderNodeMap nodes_center; | ||||
| ShaderNodeMap nodes_dx; | ShaderNodeMap nodes_dx; | ||||
| ShaderNodeMap nodes_dy; | ShaderNodeMap nodes_dy; | ||||
| copy_nodes(nodes_displace, nodes_center); | copy_nodes(nodes_displace, nodes_center); | ||||
| copy_nodes(nodes_displace, nodes_dx); | copy_nodes(nodes_displace, nodes_dx); | ||||
| copy_nodes(nodes_displace, nodes_dy); | copy_nodes(nodes_displace, nodes_dy); | ||||
| /* mark nodes to indicate they are use for bump computation, so | /* mark nodes to indicate they are use for bump computation, so | ||||
| * that any texture coordinates are shifted by dx/dy when sampling */ | * that any texture coordinates are shifted by dx/dy when sampling */ | ||||
| foreach (NodePair &pair, nodes_center) | foreach (NodePair &pair, nodes_center) | ||||
| pair.second->bump = SHADER_BUMP_CENTER; | pair.second->get_bump() = SHADER_BUMP_CENTER; | ||||
| foreach (NodePair &pair, nodes_dx) | foreach (NodePair &pair, nodes_dx) | ||||
| pair.second->bump = SHADER_BUMP_DX; | pair.second->get_bump() = SHADER_BUMP_DX; | ||||
| foreach (NodePair &pair, nodes_dy) | foreach (NodePair &pair, nodes_dy) | ||||
| pair.second->bump = SHADER_BUMP_DY; | pair.second->get_bump() = SHADER_BUMP_DY; | ||||
| /* add set normal node and connect the bump normal output to the set normal | /* add set normal node and connect the bump normal output to the set normal | ||||
| * output, so it can finally set the shader normal, note we are only doing | * output, so it can finally set the shader normal, note we are only doing | ||||
| * this for bump from displacement, this will be the only bump allowed to | * this for bump from displacement, this will be the only bump allowed to | ||||
| * overwrite the shader normal */ | * overwrite the shader normal */ | ||||
| ShaderNode *set_normal = add(create_node<SetNormalNode>()); | ShaderNode *set_normal = add(create_node<SetNormalNode>()); | ||||
| /* add bump node and connect copied graphs to it */ | /* add bump node and connect copied graphs to it */ | ||||
| BumpNode *bump = (BumpNode *)add(create_node<BumpNode>()); | BumpNode *bump = (BumpNode *)add(create_node<BumpNode>()); | ||||
| bump->set_use_object_space(use_object_space); | bump->set_use_object_space(use_object_space); | ||||
| bump->set_distance(1.0f); | bump->set_distance(1.0f); | ||||
| ShaderOutput *out = displacement_in->link; | ShaderOutput *out = displacement_in->get_link(); | ||||
| ShaderOutput *out_center = nodes_center[out->parent]->output(out->name()); | ShaderOutput *out_center = nodes_center[out->get_parent()]->output(out->name()); | ||||
| ShaderOutput *out_dx = nodes_dx[out->parent]->output(out->name()); | ShaderOutput *out_dx = nodes_dx[out->get_parent()]->output(out->name()); | ||||
| ShaderOutput *out_dy = nodes_dy[out->parent]->output(out->name()); | ShaderOutput *out_dy = nodes_dy[out->get_parent()]->output(out->name()); | ||||
| /* convert displacement vector to height */ | /* convert displacement vector to height */ | ||||
| VectorMathNode *dot_center = (VectorMathNode *)add(create_node<VectorMathNode>()); | VectorMathNode *dot_center = (VectorMathNode *)add(create_node<VectorMathNode>()); | ||||
| VectorMathNode *dot_dx = (VectorMathNode *)add(create_node<VectorMathNode>()); | VectorMathNode *dot_dx = (VectorMathNode *)add(create_node<VectorMathNode>()); | ||||
| VectorMathNode *dot_dy = (VectorMathNode *)add(create_node<VectorMathNode>()); | VectorMathNode *dot_dy = (VectorMathNode *)add(create_node<VectorMathNode>()); | ||||
| dot_center->set_math_type(NODE_VECTOR_MATH_DOT_PRODUCT); | dot_center->set_math_type(NODE_VECTOR_MATH_DOT_PRODUCT); | ||||
| dot_dx->set_math_type(NODE_VECTOR_MATH_DOT_PRODUCT); | dot_dx->set_math_type(NODE_VECTOR_MATH_DOT_PRODUCT); | ||||
| Show All 30 Lines | |||||
| void ShaderGraph::transform_multi_closure(ShaderNode *node, ShaderOutput *weight_out, bool volume) | void ShaderGraph::transform_multi_closure(ShaderNode *node, ShaderOutput *weight_out, bool volume) | ||||
| { | { | ||||
| /* for SVM in multi closure mode, this transforms the shader mix/add part of | /* for SVM in multi closure mode, this transforms the shader mix/add part of | ||||
| * the graph into nodes that feed weights into closure nodes. this is too | * the graph into nodes that feed weights into closure nodes. this is too | ||||
| * avoid building a closure tree and then flattening it, and instead write it | * avoid building a closure tree and then flattening it, and instead write it | ||||
| * directly to an array */ | * directly to an array */ | ||||
| if (node->special_type == SHADER_SPECIAL_TYPE_COMBINE_CLOSURE) { | if (node->get_special_type() == SHADER_SPECIAL_TYPE_COMBINE_CLOSURE) { | ||||
| ShaderInput *fin = node->input("Fac"); | ShaderInput *fin = node->input("Fac"); | ||||
| ShaderInput *cl1in = node->input("Closure1"); | ShaderInput *cl1in = node->input("Closure1"); | ||||
| ShaderInput *cl2in = node->input("Closure2"); | ShaderInput *cl2in = node->input("Closure2"); | ||||
| ShaderOutput *weight1_out, *weight2_out; | ShaderOutput *weight1_out, *weight2_out; | ||||
| if (fin) { | if (fin) { | ||||
| /* mix closure: add node to mix closure weights */ | /* mix closure: add node to mix closure weights */ | ||||
| MixClosureWeightNode *mix_node = create_node<MixClosureWeightNode>(); | MixClosureWeightNode *mix_node = create_node<MixClosureWeightNode>(); | ||||
| add(mix_node); | add(mix_node); | ||||
| ShaderInput *fac_in = mix_node->input("Fac"); | ShaderInput *fac_in = mix_node->input("Fac"); | ||||
| ShaderInput *weight_in = mix_node->input("Weight"); | ShaderInput *weight_in = mix_node->input("Weight"); | ||||
| if (fin->link) | if (fin->get_link()) | ||||
| connect(fin->link, fac_in); | connect(fin->get_link(), fac_in); | ||||
| else | else | ||||
| mix_node->set_fac(node->get_float(fin->socket_type)); | mix_node->set_fac(node->get_float(fin->get_socket_type())); | ||||
| if (weight_out) | if (weight_out) | ||||
| connect(weight_out, weight_in); | connect(weight_out, weight_in); | ||||
| weight1_out = mix_node->output("Weight1"); | weight1_out = mix_node->output("Weight1"); | ||||
| weight2_out = mix_node->output("Weight2"); | weight2_out = mix_node->output("Weight2"); | ||||
| } | } | ||||
| else { | else { | ||||
| /* add closure: just pass on any weights */ | /* add closure: just pass on any weights */ | ||||
| weight1_out = weight_out; | weight1_out = weight_out; | ||||
| weight2_out = weight_out; | weight2_out = weight_out; | ||||
| } | } | ||||
| if (cl1in->link) | if (cl1in->get_link()) | ||||
| transform_multi_closure(cl1in->link->parent, weight1_out, volume); | transform_multi_closure(cl1in->get_link()->get_parent(), weight1_out, volume); | ||||
| if (cl2in->link) | if (cl2in->get_link()) | ||||
| transform_multi_closure(cl2in->link->parent, weight2_out, volume); | transform_multi_closure(cl2in->get_link()->get_parent(), weight2_out, volume); | ||||
| } | } | ||||
| else { | else { | ||||
| ShaderInput *weight_in = node->input((volume) ? "VolumeMixWeight" : "SurfaceMixWeight"); | ShaderInput *weight_in = node->input((volume) ? "VolumeMixWeight" : "SurfaceMixWeight"); | ||||
| /* not a closure node? */ | /* not a closure node? */ | ||||
| if (!weight_in) | if (!weight_in) | ||||
| return; | return; | ||||
| /* already has a weight connected to it? add weights */ | /* already has a weight connected to it? add weights */ | ||||
| float weight_value = node->get_float(weight_in->socket_type); | float weight_value = node->get_float(weight_in->get_socket_type()); | ||||
| if (weight_in->link || weight_value != 0.0f) { | if (weight_in->get_link() || weight_value != 0.0f) { | ||||
| MathNode *math_node = create_node<MathNode>(); | MathNode *math_node = create_node<MathNode>(); | ||||
| add(math_node); | add(math_node); | ||||
| if (weight_in->link) | if (weight_in->get_link()) | ||||
| connect(weight_in->link, math_node->input("Value1")); | connect(weight_in->get_link(), math_node->input("Value1")); | ||||
| else | else | ||||
| math_node->set_value1(weight_value); | math_node->set_value1(weight_value); | ||||
| if (weight_out) | if (weight_out) | ||||
| connect(weight_out, math_node->input("Value2")); | connect(weight_out, math_node->input("Value2")); | ||||
| else | else | ||||
| math_node->set_value2(1.0f); | math_node->set_value2(1.0f); | ||||
| weight_out = math_node->output("Value"); | weight_out = math_node->output("Value"); | ||||
| if (weight_in->link) | if (weight_in->get_link()) | ||||
| disconnect(weight_in); | disconnect(weight_in); | ||||
| } | } | ||||
| /* connected to closure mix weight */ | /* connected to closure mix weight */ | ||||
| if (weight_out) | if (weight_out) | ||||
| connect(weight_out, weight_in); | connect(weight_out, weight_in); | ||||
| else | else | ||||
| node->set(weight_in->socket_type, weight_value + 1.0f); | node->set(weight_in->get_socket_type(), weight_value + 1.0f); | ||||
| } | } | ||||
| } | } | ||||
| int ShaderGraph::get_num_closures() | int ShaderGraph::get_num_closures() | ||||
| { | { | ||||
| int num_closures = 0; | int num_closures = 0; | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| ClosureType closure_type = node->get_closure_type(); | ClosureType closure_type = node->get_closure_type(); | ||||
| Show All 37 Lines | void ShaderGraph::dump_graph(const char *filename) | ||||
| fprintf(fd, "digraph shader_graph {\n"); | fprintf(fd, "digraph shader_graph {\n"); | ||||
| fprintf(fd, "ranksep=1.5\n"); | fprintf(fd, "ranksep=1.5\n"); | ||||
| fprintf(fd, "rankdir=LR\n"); | fprintf(fd, "rankdir=LR\n"); | ||||
| fprintf(fd, "splines=false\n"); | fprintf(fd, "splines=false\n"); | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| fprintf(fd, "// NODE: %p\n", node); | fprintf(fd, "// NODE: %p\n", node); | ||||
| fprintf(fd, "\"%p\" [shape=record,label=\"{", node); | fprintf(fd, "\"%p\" [shape=record,label=\"{", node); | ||||
| if (node->inputs.size()) { | if (node->get_inputs().size()) { | ||||
| fprintf(fd, "{"); | fprintf(fd, "{"); | ||||
| foreach (ShaderInput *socket, node->inputs) { | foreach (ShaderInput *socket, node->get_inputs()) { | ||||
| if (socket != node->inputs[0]) { | if (socket != node->get_inputs()[0]) { | ||||
| fprintf(fd, "|"); | fprintf(fd, "|"); | ||||
| } | } | ||||
| fprintf(fd, "<IN_%p>%s", socket, socket->name().c_str()); | fprintf(fd, "<IN_%p>%s", socket, socket->name().c_str()); | ||||
| } | } | ||||
| fprintf(fd, "}|"); | fprintf(fd, "}|"); | ||||
| } | } | ||||
| fprintf(fd, "%s", node->name.c_str()); | fprintf(fd, "%s", node->get_name().c_str()); | ||||
| if (node->bump == SHADER_BUMP_CENTER) { | if (node->get_bump() == SHADER_BUMP_CENTER) { | ||||
| fprintf(fd, " (bump:center)"); | fprintf(fd, " (bump:center)"); | ||||
| } | } | ||||
| else if (node->bump == SHADER_BUMP_DX) { | else if (node->get_bump() == SHADER_BUMP_DX) { | ||||
| fprintf(fd, " (bump:dx)"); | fprintf(fd, " (bump:dx)"); | ||||
| } | } | ||||
| else if (node->bump == SHADER_BUMP_DY) { | else if (node->get_bump() == SHADER_BUMP_DY) { | ||||
| fprintf(fd, " (bump:dy)"); | fprintf(fd, " (bump:dy)"); | ||||
| } | } | ||||
| if (node->outputs.size()) { | if (node->get_outputs().size()) { | ||||
| fprintf(fd, "|{"); | fprintf(fd, "|{"); | ||||
| foreach (ShaderOutput *socket, node->outputs) { | foreach (ShaderOutput *socket, node->get_outputs()) { | ||||
| if (socket != node->outputs[0]) { | if (socket != node->get_outputs()[0]) { | ||||
| fprintf(fd, "|"); | fprintf(fd, "|"); | ||||
| } | } | ||||
| fprintf(fd, "<OUT_%p>%s", socket, socket->name().c_str()); | fprintf(fd, "<OUT_%p>%s", socket, socket->name().c_str()); | ||||
| } | } | ||||
| fprintf(fd, "}"); | fprintf(fd, "}"); | ||||
| } | } | ||||
| fprintf(fd, "}\"]"); | fprintf(fd, "}\"]"); | ||||
| } | } | ||||
| foreach (ShaderNode *node, nodes) { | foreach (ShaderNode *node, nodes) { | ||||
| foreach (ShaderOutput *output, node->outputs) { | foreach (ShaderOutput *output, node->get_outputs()) { | ||||
| foreach (ShaderInput *input, output->links) { | foreach (ShaderInput *input, output->get_links()) { | ||||
| fprintf(fd, | fprintf(fd, | ||||
| "// CONNECTION: OUT_%p->IN_%p (%s:%s)\n", | "// CONNECTION: OUT_%p->IN_%p (%s:%s)\n", | ||||
| output, | output, | ||||
| input, | input, | ||||
| output->name().c_str(), | output->name().c_str(), | ||||
| input->name().c_str()); | input->name().c_str()); | ||||
| fprintf(fd, | fprintf(fd, | ||||
| "\"%p\":\"OUT_%p\":e -> \"%p\":\"IN_%p\":w [label=\"\"]\n", | "\"%p\":\"OUT_%p\":e -> \"%p\":\"IN_%p\":w [label=\"\"]\n", | ||||
| output->parent, | output->get_parent(), | ||||
| output, | output, | ||||
| input->parent, | input->get_parent(), | ||||
| input); | input); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| fprintf(fd, "}\n"); | fprintf(fd, "}\n"); | ||||
| fclose(fd); | fclose(fd); | ||||
| } | } | ||||
| CCL_NAMESPACE_END | CCL_NAMESPACE_END | ||||