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
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __GRAPH_H__
|
|
|
|
#define __GRAPH_H__
|
|
|
|
|
|
|
|
#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;
|
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;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* Socket Type
|
|
|
|
*
|
|
|
|
* Data type for inputs and outputs */
|
|
|
|
|
|
|
|
enum ShaderSocketType {
|
2013-03-18 16:34:57 +00:00
|
|
|
SHADER_SOCKET_UNDEFINED,
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
SHADER_SOCKET_FLOAT,
|
2012-10-20 13:11:45 +00:00
|
|
|
SHADER_SOCKET_INT,
|
2011-04-27 11:58:34 +00:00
|
|
|
SHADER_SOCKET_COLOR,
|
|
|
|
SHADER_SOCKET_VECTOR,
|
|
|
|
SHADER_SOCKET_POINT,
|
|
|
|
SHADER_SOCKET_NORMAL,
|
2012-11-06 21:36:44 +00:00
|
|
|
SHADER_SOCKET_CLOSURE,
|
|
|
|
SHADER_SOCKET_STRING
|
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-06-23 19:24:32 +00:00
|
|
|
SHADER_SPECIAL_TYPE_MIX_CLOSURE,
|
2014-09-24 10:52:19 +00:00
|
|
|
SHADER_SPECIAL_TYPE_MIX_RGB, /* Only Mix subtype */
|
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,
|
|
|
|
SHADER_SPECIAL_TYPE_BACKGROUND,
|
2012-09-03 11:38:15 +00:00
|
|
|
};
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Enum
|
|
|
|
*
|
|
|
|
* Utility class for enum values. */
|
|
|
|
|
|
|
|
class ShaderEnum {
|
|
|
|
public:
|
|
|
|
bool empty() const { return left.empty(); }
|
|
|
|
void insert(const char *x, int y) {
|
|
|
|
left[ustring(x)] = y;
|
|
|
|
right[y] = ustring(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool exists(ustring x) { return left.find(x) != left.end(); }
|
|
|
|
bool exists(int y) { return right.find(y) != right.end(); }
|
|
|
|
|
|
|
|
int operator[](const char *x) { return left[ustring(x)]; }
|
|
|
|
int operator[](ustring x) { return left[x]; }
|
|
|
|
ustring operator[](int y) { return right[y]; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
map<ustring, int> left;
|
|
|
|
map<int, ustring> right;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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:
|
|
|
|
enum DefaultValue {
|
2011-10-12 16:01:37 +00:00
|
|
|
TEXTURE_GENERATED,
|
|
|
|
TEXTURE_UV,
|
2011-04-27 11:58:34 +00:00
|
|
|
INCOMING,
|
|
|
|
NORMAL,
|
|
|
|
POSITION,
|
2012-10-10 13:02:20 +00:00
|
|
|
TANGENT,
|
2011-04-27 11:58:34 +00:00
|
|
|
NONE
|
|
|
|
};
|
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
enum Usage {
|
|
|
|
USE_SVM = 1,
|
|
|
|
USE_OSL = 2,
|
|
|
|
USE_ALL = USE_SVM|USE_OSL
|
|
|
|
};
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
ShaderInput(ShaderNode *parent, const char *name, ShaderSocketType type);
|
|
|
|
void set(const float3& v) { value = v; }
|
|
|
|
void set(float f) { value = make_float3(f, 0, 0); }
|
2012-11-06 21:36:44 +00:00
|
|
|
void set(const ustring v) { value_string = v; }
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
const char *name;
|
|
|
|
ShaderSocketType type;
|
|
|
|
|
|
|
|
ShaderNode *parent;
|
|
|
|
ShaderOutput *link;
|
|
|
|
|
|
|
|
DefaultValue default_value;
|
|
|
|
float3 value;
|
2012-11-06 21:36:44 +00:00
|
|
|
ustring value_string;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
int stack_offset; /* for SVM compiler */
|
2012-11-26 21:59:41 +00:00
|
|
|
int usage;
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Output
|
|
|
|
*
|
|
|
|
* Output socket for a shader node. */
|
|
|
|
|
|
|
|
class ShaderOutput {
|
|
|
|
public:
|
|
|
|
ShaderOutput(ShaderNode *parent, const char *name, ShaderSocketType type);
|
|
|
|
|
|
|
|
const char *name;
|
|
|
|
ShaderNode *parent;
|
|
|
|
ShaderSocketType type;
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
class ShaderNode {
|
|
|
|
public:
|
|
|
|
ShaderNode(const char *name);
|
|
|
|
virtual ~ShaderNode();
|
|
|
|
|
|
|
|
ShaderInput *input(const char *name);
|
|
|
|
ShaderOutput *output(const char *name);
|
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
ShaderInput *add_input(const char *name, ShaderSocketType type, float value=0.0f, int usage=ShaderInput::USE_ALL);
|
|
|
|
ShaderInput *add_input(const char *name, ShaderSocketType type, float3 value, int usage=ShaderInput::USE_ALL);
|
|
|
|
ShaderInput *add_input(const char *name, ShaderSocketType type, ShaderInput::DefaultValue value, int usage=ShaderInput::USE_ALL);
|
2011-04-27 11:58:34 +00:00
|
|
|
ShaderOutput *add_output(const char *name, ShaderSocketType type);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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-07-31 20:56:32 +00:00
|
|
|
virtual bool has_converter_blackbody() { 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; }
|
2012-12-12 06:51:06 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
vector<ShaderInput*> inputs;
|
|
|
|
vector<ShaderOutput*> outputs;
|
|
|
|
|
|
|
|
ustring name; /* name, not required to be unique */
|
|
|
|
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 */
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Node definition utility macros */
|
|
|
|
|
|
|
|
#define SHADER_NODE_CLASS(type) \
|
|
|
|
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) \
|
|
|
|
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); \
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
void disconnect(ShaderInput *to);
|
2015-01-22 08:58:11 +00:00
|
|
|
void relink(vector<ShaderInput*> inputs, vector<ShaderInput*> outputs, ShaderOutput *output);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-05-10 11:31:57 +00:00
|
|
|
void remove_unneeded_nodes();
|
2014-04-21 13:53:20 +00:00
|
|
|
void finalize(bool do_bump = false, bool do_osl = false);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
void find_dependencies(set<ShaderNode*>& dependencies, ShaderInput *input);
|
|
|
|
void copy_nodes(set<ShaderNode*>& nodes, map<ShaderNode*, ShaderNode*>& nnodemap);
|
|
|
|
|
|
|
|
void break_cycles(ShaderNode *node, vector<bool>& visited, vector<bool>& on_stack);
|
|
|
|
void clean();
|
|
|
|
void bump_from_displacement();
|
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);
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __GRAPH_H__ */
|
|
|
|
|