2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "attribute.h"
|
|
|
|
#include "graph.h"
|
|
|
|
#include "nodes.h"
|
2013-12-31 16:33:55 +00:00
|
|
|
#include "shader.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
#include "util_algorithm.h"
|
|
|
|
#include "util_debug.h"
|
|
|
|
#include "util_foreach.h"
|
2015-12-02 11:19:39 +00:00
|
|
|
#include "util_queue.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2015-12-02 11:19:39 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool check_node_inputs_has_links(const ShaderNode *node)
|
|
|
|
{
|
|
|
|
foreach(const ShaderInput *in, node->inputs) {
|
|
|
|
if(in->link) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool check_node_inputs_traversed(const ShaderNode *node,
|
|
|
|
const ShaderNodeSet& done)
|
|
|
|
{
|
|
|
|
foreach(const ShaderInput *in, node->inputs) {
|
|
|
|
if(in->link) {
|
|
|
|
if(done.find(in->link->parent) == done.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-15 16:56:27 +00:00
|
|
|
bool check_node_inputs_equals(const ShaderNode *node_a,
|
|
|
|
const ShaderNode *node_b)
|
|
|
|
{
|
|
|
|
if(node_a->inputs.size() != node_b->inputs.size()) {
|
|
|
|
/* Happens with BSDF closure nodes which are currently sharing the same
|
|
|
|
* name for all the BSDF types, making it impossible to filter out
|
|
|
|
* incompatible nodes.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for(int i = 0; i < node_a->inputs.size(); ++i) {
|
|
|
|
ShaderInput *input_a = node_a->inputs[i],
|
|
|
|
*input_b = node_b->inputs[i];
|
|
|
|
if(input_a->link == NULL && input_b->link == NULL) {
|
|
|
|
/* Unconnected inputs are expected to have the same value. */
|
2016-05-07 23:32:09 +00:00
|
|
|
if(input_a->value() != input_b->value()) {
|
2015-12-15 16:56:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(input_a->link != NULL && input_b->link != NULL) {
|
|
|
|
/* Expect links are to come from the same exact socket. */
|
|
|
|
if(input_a->link != input_b->link) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* One socket has a link and another has not, inputs can't be
|
|
|
|
* considered equal.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-02 11:19:39 +00:00
|
|
|
} /* namespace */
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Input and Output */
|
|
|
|
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderInput::ShaderInput(ShaderNode *parent_, const char *name, SocketType::Type type)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
parent = parent_;
|
2016-05-07 23:32:09 +00:00
|
|
|
name_ = name;
|
|
|
|
type_ = type;
|
2011-04-27 11:58:34 +00:00
|
|
|
link = NULL;
|
2016-05-07 23:32:09 +00:00
|
|
|
value_ = make_float3(0.0f, 0.0f, 0.0f);
|
2011-04-27 11:58:34 +00:00
|
|
|
stack_offset = SVM_STACK_INVALID;
|
2016-05-07 23:32:09 +00:00
|
|
|
flags_ = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderOutput::ShaderOutput(ShaderNode *parent_, const char *name, SocketType::Type type)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
parent = parent_;
|
2016-05-07 23:32:09 +00:00
|
|
|
name_ = name;
|
|
|
|
type_ = type;
|
2011-04-27 11:58:34 +00:00
|
|
|
stack_offset = SVM_STACK_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Node */
|
|
|
|
|
|
|
|
ShaderNode::ShaderNode(const char *name_)
|
|
|
|
{
|
|
|
|
name = name_;
|
|
|
|
id = -1;
|
|
|
|
bump = SHADER_BUMP_NONE;
|
2012-09-03 11:38:15 +00:00
|
|
|
special_type = SHADER_SPECIAL_TYPE_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderNode::~ShaderNode()
|
|
|
|
{
|
|
|
|
foreach(ShaderInput *socket, inputs)
|
|
|
|
delete socket;
|
|
|
|
|
|
|
|
foreach(ShaderOutput *socket, outputs)
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderInput *ShaderNode::input(const char *name)
|
|
|
|
{
|
2015-12-15 15:04:00 +00:00
|
|
|
foreach(ShaderInput *socket, inputs) {
|
2016-05-07 23:32:09 +00:00
|
|
|
if(socket->name() == name)
|
2011-04-27 11:58:34 +00:00
|
|
|
return socket;
|
2015-12-15 15:04:00 +00:00
|
|
|
}
|
2015-12-15 16:01:56 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderOutput *ShaderNode::output(const char *name)
|
|
|
|
{
|
|
|
|
foreach(ShaderOutput *socket, outputs)
|
2016-05-07 23:32:09 +00:00
|
|
|
if(socket->name() == name)
|
2011-04-27 11:58:34 +00:00
|
|
|
return socket;
|
2015-12-15 16:01:56 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderInput *ShaderNode::input(ustring name)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2016-05-07 23:32:09 +00:00
|
|
|
foreach(ShaderInput *socket, inputs) {
|
|
|
|
if(socket->name() == name)
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderOutput *ShaderNode::output(ustring name)
|
|
|
|
{
|
|
|
|
foreach(ShaderOutput *socket, outputs)
|
|
|
|
if(socket->name() == name)
|
|
|
|
return socket;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderInput *ShaderNode::add_input(const char *name, SocketType::Type type, float value, int flags)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
ShaderInput *input = new ShaderInput(this, name, type);
|
2016-05-07 23:32:09 +00:00
|
|
|
input->value_.x = value;
|
|
|
|
input->flags_ = flags;
|
2011-04-27 11:58:34 +00:00
|
|
|
inputs.push_back(input);
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderInput *ShaderNode::add_input(const char *name, SocketType::Type type, float3 value, int flags)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderInput *input = new ShaderInput(this, name, type);
|
|
|
|
input->value_ = value;
|
|
|
|
input->flags_ = flags;
|
|
|
|
inputs.push_back(input);
|
2011-04-27 11:58:34 +00:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderOutput *ShaderNode::add_output(const char *name, SocketType::Type type)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
ShaderOutput *output = new ShaderOutput(this, name, type);
|
|
|
|
outputs.push_back(output);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2013-12-31 16:30:34 +00:00
|
|
|
void ShaderNode::attributes(Shader *shader, AttributeRequestSet *attributes)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-10-12 16:01:37 +00:00
|
|
|
foreach(ShaderInput *input, inputs) {
|
|
|
|
if(!input->link) {
|
2016-05-07 23:32:09 +00:00
|
|
|
if(input->flags() & SocketType::LINK_TEXTURE_GENERATED) {
|
2013-12-31 16:33:55 +00:00
|
|
|
if(shader->has_surface)
|
|
|
|
attributes->add(ATTR_STD_GENERATED);
|
|
|
|
if(shader->has_volume)
|
|
|
|
attributes->add(ATTR_STD_GENERATED_TRANSFORM);
|
|
|
|
}
|
2016-05-07 23:32:09 +00:00
|
|
|
else if(input->flags() & SocketType::LINK_TEXTURE_UV) {
|
2013-12-31 16:33:55 +00:00
|
|
|
if(shader->has_surface)
|
|
|
|
attributes->add(ATTR_STD_UV);
|
2013-12-31 16:33:55 +00:00
|
|
|
}
|
2011-10-12 16:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Graph */
|
|
|
|
|
|
|
|
ShaderGraph::ShaderGraph()
|
|
|
|
{
|
|
|
|
finalized = false;
|
2013-05-10 15:10:54 +00:00
|
|
|
num_node_ids = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
add(new OutputNode());
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderGraph::~ShaderGraph()
|
|
|
|
{
|
2016-01-05 16:18:02 +00:00
|
|
|
clear_nodes();
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderNode *ShaderGraph::add(ShaderNode *node)
|
|
|
|
{
|
|
|
|
assert(!finalized);
|
2013-05-10 15:10:54 +00:00
|
|
|
node->id = num_node_ids++;
|
2011-04-27 11:58:34 +00:00
|
|
|
nodes.push_back(node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2013-12-31 16:30:34 +00:00
|
|
|
OutputNode *ShaderGraph::output()
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2013-12-31 16:30:34 +00:00
|
|
|
return (OutputNode*)nodes.front();
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderGraph *ShaderGraph::copy()
|
|
|
|
{
|
|
|
|
ShaderGraph *newgraph = new ShaderGraph();
|
|
|
|
|
|
|
|
/* copy nodes */
|
2015-11-25 08:07:29 +00:00
|
|
|
ShaderNodeSet nodes_all;
|
2011-04-27 11:58:34 +00:00
|
|
|
foreach(ShaderNode *node, nodes)
|
|
|
|
nodes_all.insert(node);
|
|
|
|
|
2015-11-25 08:07:29 +00:00
|
|
|
ShaderNodeMap nodes_copy;
|
2011-04-27 11:58:34 +00:00
|
|
|
copy_nodes(nodes_all, nodes_copy);
|
|
|
|
|
|
|
|
/* add nodes (in same order, so output is still first) */
|
2016-01-05 16:18:02 +00:00
|
|
|
newgraph->clear_nodes();
|
2011-04-27 11:58:34 +00:00
|
|
|
foreach(ShaderNode *node, nodes)
|
|
|
|
newgraph->add(nodes_copy[node]);
|
|
|
|
|
|
|
|
return newgraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGraph::connect(ShaderOutput *from, ShaderInput *to)
|
|
|
|
{
|
|
|
|
assert(!finalized);
|
|
|
|
assert(from && to);
|
|
|
|
|
|
|
|
if(to->link) {
|
2012-10-04 20:12:16 +00:00
|
|
|
fprintf(stderr, "Cycles shader graph connect: input already connected.\n");
|
2011-04-27 11:58:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-07 23:32:09 +00:00
|
|
|
if(from->type() != to->type()) {
|
2011-04-27 11:58:34 +00:00
|
|
|
/* for closures we can't do automatic conversion */
|
2016-05-07 23:32:09 +00:00
|
|
|
if(from->type() == SocketType::CLOSURE || to->type() == SocketType::CLOSURE) {
|
2012-10-04 20:12:16 +00:00
|
|
|
fprintf(stderr, "Cycles shader graph connect: can only connect closure to closure "
|
2013-06-05 15:54:39 +00:00
|
|
|
"(%s.%s to %s.%s).\n",
|
2016-05-07 23:32:09 +00:00
|
|
|
from->parent->name.c_str(), from->name().c_str(),
|
|
|
|
to->parent->name.c_str(), to->name().c_str());
|
2011-04-27 11:58:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add automatic conversion node in case of type mismatch */
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderNode *convert = add(new ConvertNode(from->type(), to->type(), true));
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
connect(from, convert->inputs[0]);
|
|
|
|
connect(convert->outputs[0], to);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* types match, just connect */
|
|
|
|
to->link = from;
|
|
|
|
from->links.push_back(to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGraph::disconnect(ShaderInput *to)
|
|
|
|
{
|
|
|
|
assert(!finalized);
|
|
|
|
assert(to->link);
|
|
|
|
|
|
|
|
ShaderOutput *from = to->link;
|
|
|
|
|
|
|
|
to->link = NULL;
|
|
|
|
from->links.erase(remove(from->links.begin(), from->links.end(), to), from->links.end());
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:05:16 +00:00
|
|
|
void ShaderGraph::relink(ShaderNode *node, ShaderOutput *from, ShaderOutput *to)
|
2015-01-22 08:58:11 +00:00
|
|
|
{
|
2016-05-01 22:05:16 +00:00
|
|
|
/* Copy because disconnect modifies this list */
|
|
|
|
vector<ShaderInput*> outputs = from->links;
|
|
|
|
|
|
|
|
/* Bypass node by moving all links from "from" to "to" */
|
|
|
|
foreach(ShaderInput *sock, node->inputs) {
|
2015-01-22 08:58:11 +00:00
|
|
|
if(sock->link)
|
|
|
|
disconnect(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(ShaderInput *sock, outputs) {
|
|
|
|
disconnect(sock);
|
2016-05-01 22:05:16 +00:00
|
|
|
if(to)
|
|
|
|
connect(to, sock);
|
2015-01-22 08:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:18:27 +00:00
|
|
|
void ShaderGraph::finalize(Scene *scene,
|
|
|
|
bool do_bump,
|
|
|
|
bool do_osl,
|
|
|
|
bool do_simplify)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* before compiling, the shader graph may undergo a number of modifications.
|
|
|
|
* currently we set default geometry shader inputs, and create automatic bump
|
|
|
|
* from displacement. a graph can be finalized only once, and should not be
|
|
|
|
* modified afterwards. */
|
|
|
|
|
|
|
|
if(!finalized) {
|
2015-11-20 13:18:27 +00:00
|
|
|
clean(scene);
|
2011-04-27 11:58:34 +00:00
|
|
|
default_inputs(do_osl);
|
2012-10-10 15:56:43 +00:00
|
|
|
refine_bump_nodes();
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
if(do_bump)
|
|
|
|
bump_from_displacement();
|
|
|
|
|
2014-04-21 13:53:20 +00:00
|
|
|
ShaderInput *surface_in = output()->input("Surface");
|
|
|
|
ShaderInput *volume_in = output()->input("Volume");
|
2012-11-26 21:59:41 +00:00
|
|
|
|
2014-04-21 13:53:20 +00:00
|
|
|
/* todo: make this work when surface and volume closures are tangled up */
|
2012-11-26 21:59:41 +00:00
|
|
|
|
2014-04-21 13:53:20 +00:00
|
|
|
if(surface_in->link)
|
|
|
|
transform_multi_closure(surface_in->link->parent, NULL, false);
|
|
|
|
if(volume_in->link)
|
|
|
|
transform_multi_closure(volume_in->link->parent, NULL, true);
|
2012-11-26 21:59:41 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
finalized = true;
|
|
|
|
}
|
2015-11-20 13:18:27 +00:00
|
|
|
else if(do_simplify) {
|
2015-11-25 12:52:39 +00:00
|
|
|
simplify_settings(scene);
|
2015-11-20 13:18:27 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 08:07:29 +00:00
|
|
|
void ShaderGraph::find_dependencies(ShaderNodeSet& dependencies, ShaderInput *input)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2013-08-13 08:43:31 +00:00
|
|
|
/* find all nodes that this input depends on directly and indirectly */
|
2011-04-27 11:58:34 +00:00
|
|
|
ShaderNode *node = (input->link)? input->link->parent: NULL;
|
|
|
|
|
2015-11-25 08:46:51 +00:00
|
|
|
if(node != NULL && dependencies.find(node) == dependencies.end()) {
|
2011-04-27 11:58:34 +00:00
|
|
|
foreach(ShaderInput *in, node->inputs)
|
|
|
|
find_dependencies(dependencies, in);
|
|
|
|
|
|
|
|
dependencies.insert(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:18:02 +00:00
|
|
|
void ShaderGraph::clear_nodes()
|
|
|
|
{
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
nodes.clear();
|
|
|
|
}
|
|
|
|
|
2015-11-25 08:07:29 +00:00
|
|
|
void ShaderGraph::copy_nodes(ShaderNodeSet& nodes, ShaderNodeMap& nnodemap)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* copy a set of nodes, and the links between them. the assumption is
|
|
|
|
* made that all nodes that inputs are linked to are in the set too. */
|
|
|
|
|
|
|
|
/* copy nodes */
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
ShaderNode *nnode = node->clone();
|
|
|
|
nnodemap[node] = nnode;
|
|
|
|
|
|
|
|
nnode->inputs.clear();
|
|
|
|
nnode->outputs.clear();
|
|
|
|
|
|
|
|
foreach(ShaderInput *input, node->inputs) {
|
|
|
|
ShaderInput *ninput = new ShaderInput(*input);
|
|
|
|
nnode->inputs.push_back(ninput);
|
|
|
|
|
|
|
|
ninput->parent = nnode;
|
|
|
|
ninput->link = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(ShaderOutput *output, node->outputs) {
|
|
|
|
ShaderOutput *noutput = new ShaderOutput(*output);
|
|
|
|
nnode->outputs.push_back(noutput);
|
|
|
|
|
|
|
|
noutput->parent = nnode;
|
|
|
|
noutput->links.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* recreate links */
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
foreach(ShaderInput *input, node->inputs) {
|
|
|
|
if(input->link) {
|
|
|
|
/* find new input and output */
|
|
|
|
ShaderNode *nfrom = nnodemap[input->link->parent];
|
|
|
|
ShaderNode *nto = nnodemap[input->parent];
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderOutput *noutput = nfrom->output(input->link->name());
|
|
|
|
ShaderInput *ninput = nto->input(input->name());
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* connect */
|
|
|
|
connect(noutput, ninput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 16:56:27 +00:00
|
|
|
|
2015-11-18 17:47:56 +00:00
|
|
|
/* Graph simplification */
|
|
|
|
/* ******************** */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-05-01 22:05:16 +00:00
|
|
|
/* Step 1: Remove proxy nodes.
|
|
|
|
* These only exists temporarily when exporting groups, and we must remove them
|
|
|
|
* early so that node->attributes() and default links do not see them.
|
2015-11-18 17:47:56 +00:00
|
|
|
*/
|
2016-05-01 22:05:16 +00:00
|
|
|
void ShaderGraph::remove_proxy_nodes()
|
2011-12-18 15:34:06 +00:00
|
|
|
{
|
2013-05-10 15:10:54 +00:00
|
|
|
vector<bool> removed(num_node_ids, false);
|
2013-05-10 11:31:57 +00:00
|
|
|
bool any_node_removed = false;
|
2014-09-24 10:52:19 +00:00
|
|
|
|
2011-12-18 15:34:06 +00:00
|
|
|
foreach(ShaderNode *node, nodes) {
|
2013-05-10 11:31:57 +00:00
|
|
|
if(node->special_type == SHADER_SPECIAL_TYPE_PROXY) {
|
2016-05-01 22:05:16 +00:00
|
|
|
ConvertNode *proxy = static_cast<ConvertNode*>(node);
|
2011-12-18 15:34:06 +00:00
|
|
|
ShaderInput *input = proxy->inputs[0];
|
|
|
|
ShaderOutput *output = proxy->outputs[0];
|
2014-09-24 10:52:19 +00:00
|
|
|
|
2011-12-18 15:34:06 +00:00
|
|
|
/* bypass the proxy node */
|
2016-05-01 22:05:16 +00:00
|
|
|
if(input->link) {
|
|
|
|
relink(proxy, output, input->link);
|
2011-12-18 15:34:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-05-01 22:05:16 +00:00
|
|
|
/* Copy because disconnect modifies this list */
|
|
|
|
vector<ShaderInput*> links(output->links);
|
|
|
|
|
2011-12-18 15:34:06 +00:00
|
|
|
foreach(ShaderInput *to, links) {
|
2013-06-23 19:24:32 +00:00
|
|
|
/* remove any autoconvert nodes too if they lead to
|
|
|
|
* sockets with an automatically set default value */
|
|
|
|
ShaderNode *tonode = to->parent;
|
|
|
|
|
|
|
|
if(tonode->special_type == SHADER_SPECIAL_TYPE_AUTOCONVERT) {
|
|
|
|
bool all_links_removed = true;
|
2013-07-10 10:50:19 +00:00
|
|
|
vector<ShaderInput*> links = tonode->outputs[0]->links;
|
2013-06-23 19:24:32 +00:00
|
|
|
|
2013-07-10 10:50:19 +00:00
|
|
|
foreach(ShaderInput *autoin, links) {
|
2016-05-07 23:32:09 +00:00
|
|
|
if(autoin->flags() & SocketType::DEFAULT_LINK_MASK)
|
2013-06-23 19:24:32 +00:00
|
|
|
disconnect(autoin);
|
2016-05-07 23:32:09 +00:00
|
|
|
else
|
|
|
|
all_links_removed = false;
|
2013-06-23 19:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(all_links_removed)
|
|
|
|
removed[tonode->id] = true;
|
|
|
|
}
|
|
|
|
|
2011-12-18 15:34:06 +00:00
|
|
|
disconnect(to);
|
2016-05-01 22:05:16 +00:00
|
|
|
|
2011-12-18 15:34:06 +00:00
|
|
|
/* transfer the default input value to the target socket */
|
2016-05-29 11:26:41 +00:00
|
|
|
to->set(input->value());
|
|
|
|
to->set(input->value_string());
|
2011-12-18 15:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-01 22:05:16 +00:00
|
|
|
|
2011-12-18 15:34:06 +00:00
|
|
|
removed[proxy->id] = true;
|
2013-05-10 11:31:57 +00:00
|
|
|
any_node_removed = true;
|
2011-12-18 15:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-10 11:31:57 +00:00
|
|
|
|
|
|
|
/* remove nodes */
|
2014-09-19 11:21:25 +00:00
|
|
|
if(any_node_removed) {
|
2013-05-10 11:31:57 +00:00
|
|
|
list<ShaderNode*> newnodes;
|
|
|
|
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
if(!removed[node->id])
|
|
|
|
newnodes.push_back(node);
|
|
|
|
else
|
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = newnodes;
|
|
|
|
}
|
2011-12-18 15:34:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 12:52:39 +00:00
|
|
|
/* Step 2: Constant folding.
|
|
|
|
* Try to constant fold some nodes, and pipe result directly to
|
|
|
|
* the input socket of connected nodes.
|
|
|
|
*/
|
2015-12-02 11:19:39 +00:00
|
|
|
void ShaderGraph::constant_fold()
|
2015-11-25 12:52:39 +00:00
|
|
|
{
|
2015-12-02 11:19:39 +00:00
|
|
|
ShaderNodeSet done, scheduled;
|
|
|
|
queue<ShaderNode*> traverse_queue;
|
2015-11-25 12:52:39 +00:00
|
|
|
|
2015-12-02 11:19:39 +00:00
|
|
|
/* Schedule nodes which doesn't have any dependencies. */
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
if(!check_node_inputs_has_links(node)) {
|
|
|
|
traverse_queue.push(node);
|
|
|
|
scheduled.insert(node);
|
2015-11-25 12:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 11:19:39 +00:00
|
|
|
while(!traverse_queue.empty()) {
|
|
|
|
ShaderNode *node = traverse_queue.front();
|
|
|
|
traverse_queue.pop();
|
|
|
|
done.insert(node);
|
|
|
|
foreach(ShaderOutput *output, node->outputs) {
|
2016-05-17 20:08:34 +00:00
|
|
|
if(output->links.size() == 0) {
|
2016-05-01 22:05:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-12-02 11:19:39 +00:00
|
|
|
/* Schedule node which was depending on the value,
|
|
|
|
* when possible. Do it before disconnect.
|
|
|
|
*/
|
|
|
|
foreach(ShaderInput *input, output->links) {
|
|
|
|
if(scheduled.find(input->parent) != scheduled.end()) {
|
2015-12-06 22:47:38 +00:00
|
|
|
/* Node might not be optimized yet but scheduled already
|
2015-12-02 11:19:39 +00:00
|
|
|
* by other dependencies. No need to re-schedule it.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Schedule node if its inputs are fully done. */
|
|
|
|
if(check_node_inputs_traversed(input->parent, done)) {
|
|
|
|
traverse_queue.push(input->parent);
|
|
|
|
scheduled.insert(input->parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Optimize current node. */
|
2016-05-29 11:26:41 +00:00
|
|
|
if(node->constant_fold(this, output, output->links[0])) {
|
|
|
|
/* Apply optimized value to other connected sockets and disconnect. */
|
2015-12-02 11:19:39 +00:00
|
|
|
vector<ShaderInput*> links(output->links);
|
2016-05-29 11:26:41 +00:00
|
|
|
for(size_t i = 0; i < links.size(); i++) {
|
|
|
|
if(i > 0)
|
|
|
|
links[i]->set(links[0]->value());
|
|
|
|
disconnect(links[i]);
|
2015-12-02 11:19:39 +00:00
|
|
|
}
|
2015-11-25 12:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 16:56:27 +00:00
|
|
|
/* Step 3: Simplification. */
|
2015-11-25 12:52:39 +00:00
|
|
|
void ShaderGraph::simplify_settings(Scene *scene)
|
2015-11-18 17:47:56 +00:00
|
|
|
{
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
2015-11-25 12:52:39 +00:00
|
|
|
node->simplify_settings(scene);
|
2015-11-18 17:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 16:56:27 +00:00
|
|
|
/* Step 4: Deduplicate nodes with same settings. */
|
|
|
|
void ShaderGraph::deduplicate_nodes()
|
|
|
|
{
|
|
|
|
/* NOTES:
|
|
|
|
* - Deduplication happens for nodes which has same exact settings and same
|
|
|
|
* exact input links configuration (either connected to same output or has
|
|
|
|
* the same exact default value).
|
|
|
|
* - Deduplication happens in the bottom-top manner, so we know for fact that
|
|
|
|
* all traversed nodes are either can not be deduplicated at all or were
|
|
|
|
* already deduplicated.
|
|
|
|
*/
|
|
|
|
|
2015-12-29 15:27:00 +00:00
|
|
|
ShaderNodeSet scheduled;
|
|
|
|
map<ustring, ShaderNodeSet> done;
|
2015-12-15 16:56:27 +00:00
|
|
|
queue<ShaderNode*> traverse_queue;
|
|
|
|
|
|
|
|
/* Schedule nodes which doesn't have any dependencies. */
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
if(!check_node_inputs_has_links(node)) {
|
|
|
|
traverse_queue.push(node);
|
|
|
|
scheduled.insert(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(!traverse_queue.empty()) {
|
|
|
|
ShaderNode *node = traverse_queue.front();
|
|
|
|
traverse_queue.pop();
|
2015-12-29 15:27:00 +00:00
|
|
|
done[node->name].insert(node);
|
2015-12-15 16:56:27 +00:00
|
|
|
/* Schedule the nodes which were depending on the current node. */
|
|
|
|
foreach(ShaderOutput *output, node->outputs) {
|
|
|
|
foreach(ShaderInput *input, output->links) {
|
|
|
|
if(scheduled.find(input->parent) != scheduled.end()) {
|
|
|
|
/* Node might not be optimized yet but scheduled already
|
|
|
|
* by other dependencies. No need to re-schedule it.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Schedule node if its inputs are fully done. */
|
2015-12-29 15:27:00 +00:00
|
|
|
if(check_node_inputs_traversed(input->parent, done[input->parent->name])) {
|
2015-12-15 16:56:27 +00:00
|
|
|
traverse_queue.push(input->parent);
|
|
|
|
scheduled.insert(input->parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Try to merge this node with another one. */
|
2015-12-29 15:27:00 +00:00
|
|
|
foreach(ShaderNode *other_node, done[node->name]) {
|
2015-12-15 16:56:27 +00:00
|
|
|
if(node == other_node) {
|
|
|
|
/* Don't merge with self. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(node->name != other_node->name) {
|
|
|
|
/* Can only de-duplicate nodes of the same type. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!check_node_inputs_equals(node, other_node)) {
|
|
|
|
/* Node inputs are different, can't merge them, */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!node->equals(other_node)) {
|
|
|
|
/* Node settings are different. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* TODO(sergey): Consider making it an utility function. */
|
|
|
|
for(int i = 0; i < node->outputs.size(); ++i) {
|
2016-05-01 22:05:16 +00:00
|
|
|
relink(node, node->outputs[i], other_node->outputs[i]);
|
2015-12-15 16:56:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
void ShaderGraph::break_cycles(ShaderNode *node, vector<bool>& visited, vector<bool>& on_stack)
|
|
|
|
{
|
|
|
|
visited[node->id] = true;
|
|
|
|
on_stack[node->id] = true;
|
|
|
|
|
|
|
|
foreach(ShaderInput *input, node->inputs) {
|
|
|
|
if(input->link) {
|
|
|
|
ShaderNode *depnode = input->link->parent;
|
|
|
|
|
|
|
|
if(on_stack[depnode->id]) {
|
|
|
|
/* break cycle */
|
|
|
|
disconnect(input);
|
2012-10-04 20:12:16 +00:00
|
|
|
fprintf(stderr, "Cycles shader graph: detected cycle in graph, connection removed.\n");
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else if(!visited[depnode->id]) {
|
|
|
|
/* visit dependencies */
|
|
|
|
break_cycles(depnode, visited, on_stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
on_stack[node->id] = false;
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:18:27 +00:00
|
|
|
void ShaderGraph::clean(Scene *scene)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2016-05-01 22:05:16 +00:00
|
|
|
/* Graph simplification */
|
2015-11-18 16:20:39 +00:00
|
|
|
|
2016-05-01 22:05:16 +00:00
|
|
|
/* 1: Remove proxy nodes was already done. */
|
2013-05-10 11:31:57 +00:00
|
|
|
|
2015-11-18 16:20:39 +00:00
|
|
|
/* 2: Constant folding. */
|
2015-12-02 11:19:39 +00:00
|
|
|
constant_fold();
|
2015-11-18 16:20:39 +00:00
|
|
|
|
|
|
|
/* 3: Simplification. */
|
2015-11-25 12:52:39 +00:00
|
|
|
simplify_settings(scene);
|
2015-11-18 16:20:39 +00:00
|
|
|
|
|
|
|
/* 4: De-duplication. */
|
2015-12-15 16:56:27 +00:00
|
|
|
deduplicate_nodes();
|
2015-11-18 16:20:39 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* we do two things here: find cycles and break them, and remove unused
|
2012-06-09 17:22:52 +00:00
|
|
|
* nodes that don't feed into the output. how cycles are broken is
|
|
|
|
* undefined, they are invalid input, the important thing is to not crash */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-05-10 15:10:54 +00:00
|
|
|
vector<bool> visited(num_node_ids, false);
|
|
|
|
vector<bool> on_stack(num_node_ids, false);
|
2011-12-18 15:34:06 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* break cycles */
|
|
|
|
break_cycles(output(), visited, on_stack);
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
/* disconnect unused nodes */
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
if(!visited[node->id]) {
|
|
|
|
foreach(ShaderInput *to, node->inputs) {
|
|
|
|
ShaderOutput *from = to->link;
|
|
|
|
|
2013-05-10 11:31:57 +00:00
|
|
|
if(from) {
|
2012-09-04 13:29:07 +00:00
|
|
|
to->link = NULL;
|
|
|
|
from->links.erase(remove(from->links.begin(), from->links.end(), to), from->links.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* remove unused nodes */
|
2013-05-10 15:10:54 +00:00
|
|
|
list<ShaderNode*> newnodes;
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
if(visited[node->id])
|
|
|
|
newnodes.push_back(node);
|
|
|
|
else
|
|
|
|
delete node;
|
|
|
|
}
|
2015-06-11 21:09:38 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
nodes = newnodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGraph::default_inputs(bool do_osl)
|
|
|
|
{
|
|
|
|
/* nodes can specify default texture coordinates, for now we give
|
|
|
|
* everything the position by default, except for the sky texture */
|
|
|
|
|
|
|
|
ShaderNode *geom = NULL;
|
|
|
|
ShaderNode *texco = NULL;
|
|
|
|
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
foreach(ShaderInput *input, node->inputs) {
|
2016-05-07 23:32:09 +00:00
|
|
|
if(!input->link && (!(input->flags() & SocketType::OSL_INTERNAL) || do_osl)) {
|
|
|
|
if(input->flags() & SocketType::LINK_TEXTURE_GENERATED) {
|
2011-04-27 11:58:34 +00:00
|
|
|
if(!texco)
|
|
|
|
texco = new TextureCoordinateNode();
|
|
|
|
|
|
|
|
connect(texco->output("Generated"), input);
|
|
|
|
}
|
2016-05-07 23:32:09 +00:00
|
|
|
else if(input->flags() & SocketType::LINK_TEXTURE_UV) {
|
2011-10-12 16:01:37 +00:00
|
|
|
if(!texco)
|
|
|
|
texco = new TextureCoordinateNode();
|
|
|
|
|
|
|
|
connect(texco->output("UV"), input);
|
|
|
|
}
|
2016-05-07 23:32:09 +00:00
|
|
|
else if(input->flags() & SocketType::LINK_INCOMING) {
|
2011-04-27 11:58:34 +00:00
|
|
|
if(!geom)
|
|
|
|
geom = new GeometryNode();
|
|
|
|
|
|
|
|
connect(geom->output("Incoming"), input);
|
|
|
|
}
|
2016-05-07 23:32:09 +00:00
|
|
|
else if(input->flags() & SocketType::LINK_NORMAL) {
|
2011-04-27 11:58:34 +00:00
|
|
|
if(!geom)
|
|
|
|
geom = new GeometryNode();
|
|
|
|
|
|
|
|
connect(geom->output("Normal"), input);
|
|
|
|
}
|
2016-05-07 23:32:09 +00:00
|
|
|
else if(input->flags() & SocketType::LINK_POSITION) {
|
2011-04-27 11:58:34 +00:00
|
|
|
if(!geom)
|
|
|
|
geom = new GeometryNode();
|
|
|
|
|
|
|
|
connect(geom->output("Position"), input);
|
|
|
|
}
|
2016-05-07 23:32:09 +00:00
|
|
|
else if(input->flags() & SocketType::LINK_TANGENT) {
|
2012-10-10 13:02:20 +00:00
|
|
|
if(!geom)
|
|
|
|
geom = new GeometryNode();
|
|
|
|
|
|
|
|
connect(geom->output("Tangent"), input);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(geom)
|
|
|
|
add(geom);
|
|
|
|
if(texco)
|
|
|
|
add(texco);
|
|
|
|
}
|
|
|
|
|
2012-10-10 15:56:43 +00:00
|
|
|
void ShaderGraph::refine_bump_nodes()
|
|
|
|
{
|
|
|
|
/* 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"
|
|
|
|
* input to the inputs "center","dx" and "dy" What is in "bump" input is moved
|
|
|
|
* to "center" input. */
|
|
|
|
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
2016-05-01 22:05:16 +00:00
|
|
|
if(node->special_type == SHADER_SPECIAL_TYPE_BUMP && node->input("Height")->link) {
|
2012-10-10 15:56:43 +00:00
|
|
|
ShaderInput *bump_input = node->input("Height");
|
2015-11-25 08:07:29 +00:00
|
|
|
ShaderNodeSet nodes_bump;
|
2012-10-10 15:56:43 +00:00
|
|
|
|
|
|
|
/* make 2 extra copies of the subgraph defined in Bump input */
|
2015-11-25 08:07:29 +00:00
|
|
|
ShaderNodeMap nodes_dx;
|
|
|
|
ShaderNodeMap nodes_dy;
|
2012-10-10 15:56:43 +00:00
|
|
|
|
|
|
|
/* find dependencies for the given input */
|
2015-11-25 08:07:29 +00:00
|
|
|
find_dependencies(nodes_bump, bump_input);
|
2012-10-10 15:56:43 +00:00
|
|
|
|
|
|
|
copy_nodes(nodes_bump, nodes_dx);
|
|
|
|
copy_nodes(nodes_bump, nodes_dy);
|
|
|
|
|
|
|
|
/* mark nodes to indicate they are use for bump computation, so
|
|
|
|
that any texture coordinates are shifted by dx/dy when sampling */
|
|
|
|
foreach(ShaderNode *node, nodes_bump)
|
|
|
|
node->bump = SHADER_BUMP_CENTER;
|
|
|
|
foreach(NodePair& pair, nodes_dx)
|
|
|
|
pair.second->bump = SHADER_BUMP_DX;
|
|
|
|
foreach(NodePair& pair, nodes_dy)
|
|
|
|
pair.second->bump = SHADER_BUMP_DY;
|
|
|
|
|
|
|
|
ShaderOutput *out = bump_input->link;
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderOutput *out_dx = nodes_dx[out->parent]->output(out->name());
|
|
|
|
ShaderOutput *out_dy = nodes_dy[out->parent]->output(out->name());
|
2012-10-10 15:56:43 +00:00
|
|
|
|
|
|
|
connect(out_dx, node->input("SampleX"));
|
|
|
|
connect(out_dy, node->input("SampleY"));
|
|
|
|
|
|
|
|
/* add generated nodes */
|
|
|
|
foreach(NodePair& pair, nodes_dx)
|
|
|
|
add(pair.second);
|
|
|
|
foreach(NodePair& pair, nodes_dy)
|
|
|
|
add(pair.second);
|
|
|
|
|
2014-01-17 06:35:03 +00:00
|
|
|
/* connect what is connected is bump to samplecenter input*/
|
2012-10-10 15:56:43 +00:00
|
|
|
connect(out , node->input("SampleCenter"));
|
|
|
|
|
|
|
|
/* bump input is just for connectivity purpose for the graph input,
|
2014-01-17 06:35:03 +00:00
|
|
|
* we re-connected this input to samplecenter, so lets disconnect it
|
2012-10-10 15:56:43 +00:00
|
|
|
* from bump input */
|
|
|
|
disconnect(bump_input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
void ShaderGraph::bump_from_displacement()
|
|
|
|
{
|
|
|
|
/* generate bump mapping automatically from displacement. bump mapping is
|
|
|
|
* done using a 3-tap filter, computing the displacement at the center,
|
|
|
|
* and two other positions shifted by ray differentials.
|
|
|
|
*
|
|
|
|
* since the input to displacement is a node graph, we need to ensure that
|
|
|
|
* all texture coordinates use are shift by the ray differentials. for this
|
|
|
|
* reason we make 3 copies of the node subgraph defining the displacement,
|
|
|
|
* with each different geometry and texture coordinate nodes that generate
|
|
|
|
* different shifted coordinates.
|
|
|
|
*
|
|
|
|
* these 3 displacement values are then fed into the bump node, which will
|
2014-10-29 13:11:19 +00:00
|
|
|
* output the perturbed normal. */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
ShaderInput *displacement_in = output()->input("Displacement");
|
|
|
|
|
|
|
|
if(!displacement_in->link)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* find dependencies for the given input */
|
2015-11-25 08:07:29 +00:00
|
|
|
ShaderNodeSet nodes_displace;
|
2011-04-27 11:58:34 +00:00
|
|
|
find_dependencies(nodes_displace, displacement_in);
|
|
|
|
|
|
|
|
/* copy nodes for 3 bump samples */
|
2015-11-25 08:07:29 +00:00
|
|
|
ShaderNodeMap nodes_center;
|
|
|
|
ShaderNodeMap nodes_dx;
|
|
|
|
ShaderNodeMap nodes_dy;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
copy_nodes(nodes_displace, nodes_center);
|
|
|
|
copy_nodes(nodes_displace, nodes_dx);
|
|
|
|
copy_nodes(nodes_displace, nodes_dy);
|
|
|
|
|
|
|
|
/* mark nodes to indicate they are use for bump computation, so
|
2012-06-09 17:22:52 +00:00
|
|
|
* that any texture coordinates are shifted by dx/dy when sampling */
|
2011-04-27 11:58:34 +00:00
|
|
|
foreach(NodePair& pair, nodes_center)
|
|
|
|
pair.second->bump = SHADER_BUMP_CENTER;
|
|
|
|
foreach(NodePair& pair, nodes_dx)
|
|
|
|
pair.second->bump = SHADER_BUMP_DX;
|
|
|
|
foreach(NodePair& pair, nodes_dy)
|
|
|
|
pair.second->bump = SHADER_BUMP_DY;
|
|
|
|
|
2012-10-10 15:56:43 +00:00
|
|
|
/* add set normal node and connect the bump normal ouput to the set normal
|
|
|
|
* 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
|
|
|
|
* overwrite the shader normal */
|
|
|
|
ShaderNode *set_normal = add(new SetNormalNode());
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* add bump node and connect copied graphs to it */
|
|
|
|
ShaderNode *bump = add(new BumpNode());
|
|
|
|
|
|
|
|
ShaderOutput *out = displacement_in->link;
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderOutput *out_center = nodes_center[out->parent]->output(out->name());
|
|
|
|
ShaderOutput *out_dx = nodes_dx[out->parent]->output(out->name());
|
|
|
|
ShaderOutput *out_dy = nodes_dy[out->parent]->output(out->name());
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
connect(out_center, bump->input("SampleCenter"));
|
|
|
|
connect(out_dx, bump->input("SampleX"));
|
|
|
|
connect(out_dy, bump->input("SampleY"));
|
2012-10-10 15:56:43 +00:00
|
|
|
|
|
|
|
/* connect the bump out to the set normal in: */
|
|
|
|
connect(bump->output("Normal"), set_normal->input("Direction"));
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* connect bump output to normal input nodes that aren't set yet. actually
|
2012-06-09 17:22:52 +00:00
|
|
|
* this will only set the normal input to the geometry node that we created
|
|
|
|
* and connected to all other normal inputs already. */
|
2015-03-17 14:36:29 +00:00
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
/* Don't connect normal to the bump node we're coming from,
|
|
|
|
* otherwise it'll be a cycle in graph.
|
|
|
|
*/
|
|
|
|
if(node == bump) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach(ShaderInput *input, node->inputs) {
|
2016-05-07 23:32:09 +00:00
|
|
|
if(!input->link && (input->flags() & SocketType::LINK_NORMAL))
|
2012-10-10 15:56:43 +00:00
|
|
|
connect(set_normal->output("Normal"), input);
|
2015-03-17 14:36:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-10 15:56:43 +00:00
|
|
|
|
|
|
|
/* for displacement bump, clear the normal input in case the above loop
|
|
|
|
* connected the setnormal out to the bump normalin */
|
2013-05-08 13:23:13 +00:00
|
|
|
ShaderInput *bump_normal_in = bump->input("Normal");
|
2012-10-10 15:56:43 +00:00
|
|
|
if(bump_normal_in)
|
|
|
|
bump_normal_in->link = NULL;
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* finally, add the copied nodes to the graph. we can't do this earlier
|
2012-06-09 17:22:52 +00:00
|
|
|
* because we would create dependency cycles in the above loop */
|
2011-04-27 11:58:34 +00:00
|
|
|
foreach(NodePair& pair, nodes_center)
|
|
|
|
add(pair.second);
|
|
|
|
foreach(NodePair& pair, nodes_dx)
|
|
|
|
add(pair.second);
|
|
|
|
foreach(NodePair& pair, nodes_dy)
|
|
|
|
add(pair.second);
|
|
|
|
}
|
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
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
|
|
|
|
* 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
|
|
|
|
* directly to an array */
|
|
|
|
|
2016-05-07 22:41:01 +00:00
|
|
|
if(node->special_type == SHADER_SPECIAL_TYPE_COMBINE_CLOSURE) {
|
2012-11-26 21:59:41 +00:00
|
|
|
ShaderInput *fin = node->input("Fac");
|
|
|
|
ShaderInput *cl1in = node->input("Closure1");
|
|
|
|
ShaderInput *cl2in = node->input("Closure2");
|
|
|
|
ShaderOutput *weight1_out, *weight2_out;
|
|
|
|
|
|
|
|
if(fin) {
|
|
|
|
/* mix closure: add node to mix closure weights */
|
|
|
|
ShaderNode *mix_node = add(new MixClosureWeightNode());
|
|
|
|
ShaderInput *fac_in = mix_node->input("Fac");
|
|
|
|
ShaderInput *weight_in = mix_node->input("Weight");
|
|
|
|
|
|
|
|
if(fin->link)
|
|
|
|
connect(fin->link, fac_in);
|
|
|
|
else
|
2016-05-29 11:26:41 +00:00
|
|
|
fac_in->set(fin->value_float());
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
if(weight_out)
|
|
|
|
connect(weight_out, weight_in);
|
|
|
|
|
|
|
|
weight1_out = mix_node->output("Weight1");
|
|
|
|
weight2_out = mix_node->output("Weight2");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* add closure: just pass on any weights */
|
|
|
|
weight1_out = weight_out;
|
|
|
|
weight2_out = weight_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cl1in->link)
|
|
|
|
transform_multi_closure(cl1in->link->parent, weight1_out, volume);
|
|
|
|
if(cl2in->link)
|
|
|
|
transform_multi_closure(cl2in->link->parent, weight2_out, volume);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ShaderInput *weight_in = node->input((volume)? "VolumeMixWeight": "SurfaceMixWeight");
|
|
|
|
|
|
|
|
/* not a closure node? */
|
|
|
|
if(!weight_in)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* already has a weight connected to it? add weights */
|
2016-05-07 23:32:09 +00:00
|
|
|
if(weight_in->link || weight_in->value_float() != 0.0f) {
|
2012-11-26 21:59:41 +00:00
|
|
|
ShaderNode *math_node = add(new MathNode());
|
|
|
|
ShaderInput *value1_in = math_node->input("Value1");
|
|
|
|
ShaderInput *value2_in = math_node->input("Value2");
|
|
|
|
|
|
|
|
if(weight_in->link)
|
|
|
|
connect(weight_in->link, value1_in);
|
|
|
|
else
|
2016-05-29 11:26:41 +00:00
|
|
|
value1_in->set(weight_in->value_float());
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
if(weight_out)
|
|
|
|
connect(weight_out, value2_in);
|
|
|
|
else
|
2016-05-29 11:26:41 +00:00
|
|
|
value2_in->set(1.0f);
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
weight_out = math_node->output("Value");
|
2013-01-23 13:13:20 +00:00
|
|
|
if(weight_in->link)
|
|
|
|
disconnect(weight_in);
|
2012-11-26 21:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* connected to closure mix weight */
|
|
|
|
if(weight_out)
|
|
|
|
connect(weight_out, weight_in);
|
|
|
|
else
|
2016-05-29 11:26:41 +00:00
|
|
|
weight_in->set(weight_in->value_float() + 1.0f);
|
2012-11-26 21:59:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 14:11:37 +00:00
|
|
|
int ShaderGraph::get_num_closures()
|
|
|
|
{
|
|
|
|
int num_closures = 0;
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
2016-05-23 12:09:27 +00:00
|
|
|
ClosureType closure_type = node->get_closure_type();
|
|
|
|
if(closure_type == CLOSURE_NONE_ID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(CLOSURE_IS_BSSRDF(closure_type)) {
|
|
|
|
num_closures += 3;
|
|
|
|
}
|
|
|
|
else if(CLOSURE_IS_GLASS(closure_type)) {
|
|
|
|
num_closures += 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++num_closures;
|
2015-05-09 14:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return num_closures;
|
|
|
|
}
|
|
|
|
|
2014-09-19 09:21:32 +00:00
|
|
|
void ShaderGraph::dump_graph(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *fd = fopen(filename, "w");
|
|
|
|
|
|
|
|
if(fd == NULL) {
|
|
|
|
printf("Error opening file for dumping the graph: %s\n", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-20 13:30:13 +00:00
|
|
|
fprintf(fd, "digraph shader_graph {\n");
|
2014-09-19 09:21:32 +00:00
|
|
|
fprintf(fd, "ranksep=1.5\n");
|
2015-03-17 16:15:17 +00:00
|
|
|
fprintf(fd, "rankdir=LR\n");
|
2014-09-19 09:21:32 +00:00
|
|
|
fprintf(fd, "splines=false\n");
|
|
|
|
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
fprintf(fd, "// NODE: %p\n", node);
|
2015-03-17 16:15:17 +00:00
|
|
|
fprintf(fd, "\"%p\" [shape=record,label=\"{", node);
|
|
|
|
if(node->inputs.size()) {
|
|
|
|
fprintf(fd, "{");
|
|
|
|
foreach(ShaderInput *socket, node->inputs) {
|
|
|
|
if(socket != node->inputs[0]) {
|
|
|
|
fprintf(fd, "|");
|
|
|
|
}
|
2016-05-07 23:32:09 +00:00
|
|
|
fprintf(fd, "<IN_%p>%s", socket, socket->name().c_str());
|
2015-03-17 16:15:17 +00:00
|
|
|
}
|
|
|
|
fprintf(fd, "}|");
|
|
|
|
}
|
|
|
|
fprintf(fd, "%s", node->name.c_str());
|
|
|
|
if(node->bump == SHADER_BUMP_CENTER) {
|
|
|
|
fprintf(fd, " (bump:center)");
|
|
|
|
}
|
|
|
|
else if(node->bump == SHADER_BUMP_DX) {
|
|
|
|
fprintf(fd, " (bump:dx)");
|
|
|
|
}
|
|
|
|
else if(node->bump == SHADER_BUMP_DY) {
|
|
|
|
fprintf(fd, " (bump:dy)");
|
|
|
|
}
|
|
|
|
if(node->outputs.size()) {
|
|
|
|
fprintf(fd, "|{");
|
|
|
|
foreach(ShaderOutput *socket, node->outputs) {
|
|
|
|
if(socket != node->outputs[0]) {
|
|
|
|
fprintf(fd, "|");
|
|
|
|
}
|
2016-05-07 23:32:09 +00:00
|
|
|
fprintf(fd, "<OUT_%p>%s", socket, socket->name().c_str());
|
2015-03-17 16:15:17 +00:00
|
|
|
}
|
|
|
|
fprintf(fd, "}");
|
|
|
|
}
|
|
|
|
fprintf(fd, "}\"]");
|
2014-09-19 09:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach(ShaderNode *node, nodes) {
|
|
|
|
foreach(ShaderOutput *output, node->outputs) {
|
|
|
|
foreach(ShaderInput *input, output->links) {
|
|
|
|
fprintf(fd,
|
2015-03-17 16:15:17 +00:00
|
|
|
"// CONNECTION: OUT_%p->IN_%p (%s:%s)\n",
|
2014-09-19 09:21:32 +00:00
|
|
|
output,
|
|
|
|
input,
|
2016-05-07 23:32:09 +00:00
|
|
|
output->name().c_str(), input->name().c_str());
|
2014-09-19 09:21:32 +00:00
|
|
|
fprintf(fd,
|
2015-04-07 20:13:16 +00:00
|
|
|
"\"%p\":\"OUT_%p\":e -> \"%p\":\"IN_%p\":w [label=\"\"]\n",
|
2014-09-19 09:21:32 +00:00
|
|
|
output->parent,
|
2015-03-17 16:15:17 +00:00
|
|
|
output,
|
2014-09-19 09:21:32 +00:00
|
|
|
input->parent,
|
2015-03-17 16:15:17 +00:00
|
|
|
input);
|
2014-09-19 09:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fd, "}\n");
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|