2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2016-07-16 11:16:54 +00:00
|
|
|
* Copyright 2011-2016 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
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __GRAPH_H__
|
|
|
|
#define __GRAPH_H__
|
|
|
|
|
2016-05-07 23:32:09 +00:00
|
|
|
#include "node.h"
|
2016-05-07 17:48:28 +00:00
|
|
|
#include "node_type.h"
|
2016-05-07 23:32:09 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "kernel_types.h"
|
|
|
|
|
2011-08-21 10:32:15 +00:00
|
|
|
#include "util_list.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_map.h"
|
|
|
|
#include "util_param.h"
|
|
|
|
#include "util_set.h"
|
|
|
|
#include "util_types.h"
|
|
|
|
#include "util_vector.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
class AttributeRequestSet;
|
2015-11-20 13:18:27 +00:00
|
|
|
class Scene;
|
2013-12-31 16:30:34 +00:00
|
|
|
class Shader;
|
2011-04-27 11:58:34 +00:00
|
|
|
class ShaderInput;
|
|
|
|
class ShaderOutput;
|
|
|
|
class ShaderNode;
|
|
|
|
class ShaderGraph;
|
|
|
|
class SVMCompiler;
|
|
|
|
class OSLCompiler;
|
2013-12-31 16:30:34 +00:00
|
|
|
class OutputNode;
|
2016-07-16 11:16:54 +00:00
|
|
|
class ConstantFolder;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* Bump
|
|
|
|
*
|
|
|
|
* For bump mapping, a node may be evaluated multiple times, using different
|
|
|
|
* samples to reconstruct the normal, this indicates the sample position */
|
|
|
|
|
|
|
|
enum ShaderBump {
|
|
|
|
SHADER_BUMP_NONE,
|
|
|
|
SHADER_BUMP_CENTER,
|
|
|
|
SHADER_BUMP_DX,
|
|
|
|
SHADER_BUMP_DY
|
|
|
|
};
|
|
|
|
|
2012-09-03 11:38:15 +00:00
|
|
|
/* Identifiers for some special node types.
|
|
|
|
*
|
|
|
|
* The graph needs to identify these in the clean function.
|
|
|
|
* Cannot use dynamic_cast, as this is disabled for OSL. */
|
|
|
|
|
|
|
|
enum ShaderNodeSpecialType {
|
|
|
|
SHADER_SPECIAL_TYPE_NONE,
|
|
|
|
SHADER_SPECIAL_TYPE_PROXY,
|
2013-08-18 14:15:57 +00:00
|
|
|
SHADER_SPECIAL_TYPE_AUTOCONVERT,
|
2013-10-30 11:21:31 +00:00
|
|
|
SHADER_SPECIAL_TYPE_GEOMETRY,
|
2015-01-21 19:06:53 +00:00
|
|
|
SHADER_SPECIAL_TYPE_SCRIPT,
|
2015-04-02 15:37:57 +00:00
|
|
|
SHADER_SPECIAL_TYPE_IMAGE_SLOT,
|
2015-05-09 14:11:37 +00:00
|
|
|
SHADER_SPECIAL_TYPE_CLOSURE,
|
2016-05-07 22:41:01 +00:00
|
|
|
SHADER_SPECIAL_TYPE_COMBINE_CLOSURE,
|
|
|
|
SHADER_SPECIAL_TYPE_OUTPUT,
|
2015-06-11 21:09:38 +00:00
|
|
|
SHADER_SPECIAL_TYPE_BUMP,
|
2012-09-03 11:38:15 +00:00
|
|
|
};
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Input
|
|
|
|
*
|
|
|
|
* Input socket for a shader node. May be linked to an output or not. If not
|
|
|
|
* linked, it will either get a fixed default value, or e.g. a texture
|
|
|
|
* coordinate. */
|
|
|
|
|
|
|
|
class ShaderInput {
|
|
|
|
public:
|
2016-05-07 17:48:28 +00:00
|
|
|
ShaderInput(const SocketType& socket_type_, ShaderNode* parent_)
|
|
|
|
: socket_type(socket_type_), parent(parent_), link(NULL), stack_offset(SVM_STACK_INVALID)
|
|
|
|
{}
|
2016-05-07 23:32:09 +00:00
|
|
|
|
2016-05-07 17:48:28 +00:00
|
|
|
ustring name() { return socket_type.ui_name; }
|
|
|
|
int flags() { return socket_type.flags; }
|
|
|
|
SocketType::Type type() { return socket_type.type; }
|
2016-05-07 23:32:09 +00:00
|
|
|
|
2016-05-07 17:48:28 +00:00
|
|
|
void set(float f) { ((Node*)parent)->set(socket_type, f); }
|
|
|
|
void set(float3 f) { ((Node*)parent)->set(socket_type, f); }
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-05-07 17:48:28 +00:00
|
|
|
const SocketType& socket_type;
|
2011-04-27 11:58:34 +00:00
|
|
|
ShaderNode *parent;
|
|
|
|
ShaderOutput *link;
|
|
|
|
int stack_offset; /* for SVM compiler */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Output
|
|
|
|
*
|
|
|
|
* Output socket for a shader node. */
|
|
|
|
|
|
|
|
class ShaderOutput {
|
|
|
|
public:
|
2016-05-07 17:48:28 +00:00
|
|
|
ShaderOutput(const SocketType& socket_type_, ShaderNode* parent_)
|
|
|
|
: socket_type(socket_type_), parent(parent_), stack_offset(SVM_STACK_INVALID)
|
|
|
|
{}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-05-07 17:48:28 +00:00
|
|
|
ustring name() { return socket_type.ui_name; }
|
|
|
|
SocketType::Type type() { return socket_type.type; }
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-05-07 17:48:28 +00:00
|
|
|
const SocketType& socket_type;
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderNode *parent;
|
2011-04-27 11:58:34 +00:00
|
|
|
vector<ShaderInput*> links;
|
|
|
|
int stack_offset; /* for SVM compiler */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Node
|
|
|
|
*
|
|
|
|
* Shader node in graph, with input and output sockets. This is the virtual
|
|
|
|
* base class for all node types. */
|
|
|
|
|
2016-05-07 17:48:28 +00:00
|
|
|
class ShaderNode : public Node {
|
2011-04-27 11:58:34 +00:00
|
|
|
public:
|
2016-05-07 17:48:28 +00:00
|
|
|
explicit ShaderNode(const NodeType *type);
|
2011-04-27 11:58:34 +00:00
|
|
|
virtual ~ShaderNode();
|
|
|
|
|
2016-05-07 17:48:28 +00:00
|
|
|
void create_inputs_outputs(const NodeType *type);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
ShaderInput *input(const char *name);
|
|
|
|
ShaderOutput *output(const char *name);
|
2016-05-07 23:32:09 +00:00
|
|
|
ShaderInput *input(ustring name);
|
|
|
|
ShaderOutput *output(ustring name);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
virtual ShaderNode *clone() const = 0;
|
2013-12-31 16:30:34 +00:00
|
|
|
virtual void attributes(Shader *shader, AttributeRequestSet *attributes);
|
2011-04-27 11:58:34 +00:00
|
|
|
virtual void compile(SVMCompiler& compiler) = 0;
|
|
|
|
virtual void compile(OSLCompiler& compiler) = 0;
|
2015-11-25 12:52:39 +00:00
|
|
|
|
|
|
|
/* ** Node optimization ** */
|
|
|
|
/* Check whether the node can be replaced with single constant. */
|
2016-07-16 11:16:54 +00:00
|
|
|
virtual void constant_fold(const ConstantFolder& /*folder*/) {}
|
2016-06-19 14:50:25 +00:00
|
|
|
|
2015-11-25 12:52:39 +00:00
|
|
|
/* Simplify settings used by artists to the ones which are simpler to
|
|
|
|
* evaluate in the kernel but keep the final result unchanged.
|
|
|
|
*/
|
|
|
|
virtual void simplify_settings(Scene * /*scene*/) {};
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-12-12 06:51:06 +00:00
|
|
|
virtual bool has_surface_emission() { return false; }
|
|
|
|
virtual bool has_surface_transparent() { return false; }
|
2013-04-01 20:26:52 +00:00
|
|
|
virtual bool has_surface_bssrdf() { return false; }
|
2013-08-18 14:15:57 +00:00
|
|
|
virtual bool has_bssrdf_bump() { return false; }
|
2014-04-03 20:04:39 +00:00
|
|
|
virtual bool has_spatial_varying() { return false; }
|
2015-01-21 17:19:31 +00:00
|
|
|
virtual bool has_object_dependency() { return false; }
|
2015-11-20 13:18:27 +00:00
|
|
|
virtual bool has_integrator_dependency() { return false; }
|
2012-12-12 06:51:06 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
vector<ShaderInput*> inputs;
|
|
|
|
vector<ShaderOutput*> outputs;
|
|
|
|
|
|
|
|
int id; /* index in graph node array */
|
|
|
|
ShaderBump bump; /* for bump mapping utility */
|
2012-09-03 11:38:15 +00:00
|
|
|
|
|
|
|
ShaderNodeSpecialType special_type; /* special node type */
|
2015-05-09 14:22:16 +00:00
|
|
|
|
|
|
|
/* ** Selective nodes compilation ** */
|
|
|
|
|
|
|
|
/* TODO(sergey): More explicitly mention in the function names
|
|
|
|
* that those functions are for selective compilation only?
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Nodes are split into several groups, group of level 0 contains
|
|
|
|
* nodes which are most commonly used, further levels are extension
|
|
|
|
* of previous one and includes less commonly used nodes.
|
|
|
|
*/
|
|
|
|
virtual int get_group() { return NODE_GROUP_LEVEL_0; }
|
|
|
|
|
|
|
|
/* Node feature are used to disable huge nodes inside the group,
|
|
|
|
* so it's possible to disable huge nodes inside of the required
|
|
|
|
* nodes group.
|
|
|
|
*/
|
2015-06-01 10:26:36 +00:00
|
|
|
virtual int get_feature() { return bump == SHADER_BUMP_NONE ? 0 : NODE_FEATURE_BUMP; }
|
2015-12-15 16:56:27 +00:00
|
|
|
|
2016-05-23 12:09:27 +00:00
|
|
|
/* Get closure ID to which the node compiles into. */
|
|
|
|
virtual ClosureType get_closure_type() { return CLOSURE_NONE_ID; }
|
|
|
|
|
2015-12-15 16:56:27 +00:00
|
|
|
/* Check whether settings of the node equals to another one.
|
|
|
|
*
|
|
|
|
* This is mainly used to check whether two nodes can be merged
|
|
|
|
* together. Meaning, runtime stuff like node id and unbound slots
|
|
|
|
* will be ignored for comparison.
|
|
|
|
*
|
|
|
|
* NOTE: If some node can't be de-duplicated for whatever reason it
|
|
|
|
* is to be handled in the subclass.
|
|
|
|
*/
|
2016-05-07 17:48:28 +00:00
|
|
|
virtual bool equals(const ShaderNode& other);
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Node definition utility macros */
|
|
|
|
|
|
|
|
#define SHADER_NODE_CLASS(type) \
|
2017-03-06 13:16:45 +00:00
|
|
|
NODE_DECLARE \
|
2011-04-27 11:58:34 +00:00
|
|
|
type(); \
|
|
|
|
virtual ShaderNode *clone() const { return new type(*this); } \
|
|
|
|
virtual void compile(SVMCompiler& compiler); \
|
|
|
|
virtual void compile(OSLCompiler& compiler); \
|
|
|
|
|
|
|
|
#define SHADER_NODE_NO_CLONE_CLASS(type) \
|
2017-03-06 13:16:45 +00:00
|
|
|
NODE_DECLARE \
|
2011-04-27 11:58:34 +00:00
|
|
|
type(); \
|
|
|
|
virtual void compile(SVMCompiler& compiler); \
|
|
|
|
virtual void compile(OSLCompiler& compiler); \
|
|
|
|
|
|
|
|
#define SHADER_NODE_BASE_CLASS(type) \
|
|
|
|
virtual ShaderNode *clone() const { return new type(*this); } \
|
|
|
|
virtual void compile(SVMCompiler& compiler); \
|
|
|
|
virtual void compile(OSLCompiler& compiler); \
|
|
|
|
|
2015-11-25 08:07:29 +00:00
|
|
|
class ShaderNodeIDComparator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(const ShaderNode *n1, const ShaderNode *n2) const
|
|
|
|
{
|
|
|
|
return n1->id < n2->id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef set<ShaderNode*, ShaderNodeIDComparator> ShaderNodeSet;
|
|
|
|
typedef map<ShaderNode*, ShaderNode*, ShaderNodeIDComparator> ShaderNodeMap;
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Graph
|
|
|
|
*
|
|
|
|
* Shader graph of nodes. Also does graph manipulations for default inputs,
|
|
|
|
* bump mapping from displacement, and possibly other things in the future. */
|
|
|
|
|
|
|
|
class ShaderGraph {
|
|
|
|
public:
|
2011-08-21 10:32:15 +00:00
|
|
|
list<ShaderNode*> nodes;
|
2013-05-10 15:10:54 +00:00
|
|
|
size_t num_node_ids;
|
2011-04-27 11:58:34 +00:00
|
|
|
bool finalized;
|
|
|
|
|
|
|
|
ShaderGraph();
|
|
|
|
~ShaderGraph();
|
|
|
|
|
|
|
|
ShaderGraph *copy();
|
|
|
|
|
|
|
|
ShaderNode *add(ShaderNode *node);
|
2013-12-31 16:30:34 +00:00
|
|
|
OutputNode *output();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
void connect(ShaderOutput *from, ShaderInput *to);
|
2016-07-16 11:16:54 +00:00
|
|
|
void disconnect(ShaderOutput *from);
|
2011-04-27 11:58:34 +00:00
|
|
|
void disconnect(ShaderInput *to);
|
2016-05-01 22:05:16 +00:00
|
|
|
void relink(ShaderNode *node, ShaderOutput *from, ShaderOutput *to);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-05-01 22:05:16 +00:00
|
|
|
void remove_proxy_nodes();
|
2015-11-20 13:18:27 +00:00
|
|
|
void finalize(Scene *scene,
|
|
|
|
bool do_bump = false,
|
|
|
|
bool do_osl = false,
|
2016-09-02 04:41:04 +00:00
|
|
|
bool do_simplify = false,
|
|
|
|
bool bump_in_object_space = false);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-05-09 14:11:37 +00:00
|
|
|
int get_num_closures();
|
|
|
|
|
2014-09-19 09:21:32 +00:00
|
|
|
void dump_graph(const char *filename);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
protected:
|
|
|
|
typedef pair<ShaderNode* const, ShaderNode*> NodePair;
|
|
|
|
|
2015-11-25 08:07:29 +00:00
|
|
|
void find_dependencies(ShaderNodeSet& dependencies, ShaderInput *input);
|
2016-01-05 16:18:02 +00:00
|
|
|
void clear_nodes();
|
2015-11-25 08:07:29 +00:00
|
|
|
void copy_nodes(ShaderNodeSet& nodes, ShaderNodeMap& nnodemap);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
void break_cycles(ShaderNode *node, vector<bool>& visited, vector<bool>& on_stack);
|
2016-09-02 04:41:04 +00:00
|
|
|
void bump_from_displacement(bool use_object_space);
|
2012-10-10 15:56:43 +00:00
|
|
|
void refine_bump_nodes();
|
2011-04-27 11:58:34 +00:00
|
|
|
void default_inputs(bool do_osl);
|
2012-11-26 21:59:41 +00:00
|
|
|
void transform_multi_closure(ShaderNode *node, ShaderOutput *weight_out, bool volume);
|
2015-12-15 16:56:27 +00:00
|
|
|
|
|
|
|
/* Graph simplification routines. */
|
|
|
|
void clean(Scene *scene);
|
|
|
|
void constant_fold();
|
|
|
|
void simplify_settings(Scene *scene);
|
|
|
|
void deduplicate_nodes();
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __GRAPH_H__ */
|
|
|
|
|