graph.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright 2011-2016 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __GRAPH_H__
  17. #define __GRAPH_H__
  18. #include "graph/node.h"
  19. #include "graph/node_type.h"
  20. #include "kernel/kernel_types.h"
  21. #include "util/util_list.h"
  22. #include "util/util_map.h"
  23. #include "util/util_param.h"
  24. #include "util/util_set.h"
  25. #include "util/util_types.h"
  26. #include "util/util_vector.h"
  27. CCL_NAMESPACE_BEGIN
  28. class AttributeRequestSet;
  29. class Scene;
  30. class Shader;
  31. class ShaderInput;
  32. class ShaderOutput;
  33. class ShaderNode;
  34. class ShaderGraph;
  35. class SVMCompiler;
  36. class OSLCompiler;
  37. class OutputNode;
  38. class ConstantFolder;
  39. class MD5Hash;
  40. /* Bump
  41. *
  42. * For bump mapping, a node may be evaluated multiple times, using different
  43. * samples to reconstruct the normal, this indicates the sample position */
  44. enum ShaderBump { SHADER_BUMP_NONE, SHADER_BUMP_CENTER, SHADER_BUMP_DX, SHADER_BUMP_DY };
  45. /* Identifiers for some special node types.
  46. *
  47. * The graph needs to identify these in the clean function.
  48. * Cannot use dynamic_cast, as this is disabled for OSL. */
  49. enum ShaderNodeSpecialType {
  50. SHADER_SPECIAL_TYPE_NONE,
  51. SHADER_SPECIAL_TYPE_PROXY,
  52. SHADER_SPECIAL_TYPE_AUTOCONVERT,
  53. SHADER_SPECIAL_TYPE_GEOMETRY,
  54. SHADER_SPECIAL_TYPE_OSL,
  55. SHADER_SPECIAL_TYPE_IMAGE_SLOT,
  56. SHADER_SPECIAL_TYPE_CLOSURE,
  57. SHADER_SPECIAL_TYPE_COMBINE_CLOSURE,
  58. SHADER_SPECIAL_TYPE_OUTPUT,
  59. SHADER_SPECIAL_TYPE_BUMP,
  60. };
  61. /* Input
  62. *
  63. * Input socket for a shader node. May be linked to an output or not. If not
  64. * linked, it will either get a fixed default value, or e.g. a texture
  65. * coordinate. */
  66. class ShaderInput {
  67. public:
  68. ShaderInput(const SocketType &socket_type_, ShaderNode *parent_)
  69. : socket_type(socket_type_), parent(parent_), link(NULL), stack_offset(SVM_STACK_INVALID)
  70. {
  71. }
  72. ustring name()
  73. {
  74. return socket_type.ui_name;
  75. }
  76. int flags()
  77. {
  78. return socket_type.flags;
  79. }
  80. SocketType::Type type()
  81. {
  82. return socket_type.type;
  83. }
  84. void set(float f)
  85. {
  86. ((Node *)parent)->set(socket_type, f);
  87. }
  88. void set(float3 f)
  89. {
  90. ((Node *)parent)->set(socket_type, f);
  91. }
  92. const SocketType &socket_type;
  93. ShaderNode *parent;
  94. ShaderOutput *link;
  95. int stack_offset; /* for SVM compiler */
  96. };
  97. /* Output
  98. *
  99. * Output socket for a shader node. */
  100. class ShaderOutput {
  101. public:
  102. ShaderOutput(const SocketType &socket_type_, ShaderNode *parent_)
  103. : socket_type(socket_type_), parent(parent_), stack_offset(SVM_STACK_INVALID)
  104. {
  105. }
  106. ustring name()
  107. {
  108. return socket_type.ui_name;
  109. }
  110. SocketType::Type type()
  111. {
  112. return socket_type.type;
  113. }
  114. const SocketType &socket_type;
  115. ShaderNode *parent;
  116. vector<ShaderInput *> links;
  117. int stack_offset; /* for SVM compiler */
  118. };
  119. /* Node
  120. *
  121. * Shader node in graph, with input and output sockets. This is the virtual
  122. * base class for all node types. */
  123. class ShaderNode : public Node {
  124. public:
  125. explicit ShaderNode(const NodeType *type);
  126. virtual ~ShaderNode();
  127. void create_inputs_outputs(const NodeType *type);
  128. void remove_input(ShaderInput *input);
  129. ShaderInput *input(const char *name);
  130. ShaderOutput *output(const char *name);
  131. ShaderInput *input(ustring name);
  132. ShaderOutput *output(ustring name);
  133. virtual ShaderNode *clone() const = 0;
  134. virtual void attributes(Shader *shader, AttributeRequestSet *attributes);
  135. virtual void compile(SVMCompiler &compiler) = 0;
  136. virtual void compile(OSLCompiler &compiler) = 0;
  137. /* Expand node into additional nodes. */
  138. virtual void expand(ShaderGraph * /* graph */)
  139. {
  140. }
  141. /* ** Node optimization ** */
  142. /* Check whether the node can be replaced with single constant. */
  143. virtual void constant_fold(const ConstantFolder & /*folder*/)
  144. {
  145. }
  146. /* Simplify settings used by artists to the ones which are simpler to
  147. * evaluate in the kernel but keep the final result unchanged.
  148. */
  149. virtual void simplify_settings(Scene * /*scene*/){};
  150. virtual bool has_surface_emission()
  151. {
  152. return false;
  153. }
  154. virtual bool has_surface_transparent()
  155. {
  156. return false;
  157. }
  158. virtual bool has_surface_bssrdf()
  159. {
  160. return false;
  161. }
  162. virtual bool has_bump()
  163. {
  164. return false;
  165. }
  166. virtual bool has_bssrdf_bump()
  167. {
  168. return false;
  169. }
  170. virtual bool has_spatial_varying()
  171. {
  172. return false;
  173. }
  174. virtual bool has_object_dependency()
  175. {
  176. return false;
  177. }
  178. virtual bool has_attribute_dependency()
  179. {
  180. return false;
  181. }
  182. virtual bool has_integrator_dependency()
  183. {
  184. return false;
  185. }
  186. virtual bool has_volume_support()
  187. {
  188. return false;
  189. }
  190. virtual bool has_raytrace()
  191. {
  192. return false;
  193. }
  194. vector<ShaderInput *> inputs;
  195. vector<ShaderOutput *> outputs;
  196. int id; /* index in graph node array */
  197. ShaderBump bump; /* for bump mapping utility */
  198. ShaderNodeSpecialType special_type; /* special node type */
  199. /* ** Selective nodes compilation ** */
  200. /* TODO(sergey): More explicitly mention in the function names
  201. * that those functions are for selective compilation only?
  202. */
  203. /* Nodes are split into several groups, group of level 0 contains
  204. * nodes which are most commonly used, further levels are extension
  205. * of previous one and includes less commonly used nodes.
  206. */
  207. virtual int get_group()
  208. {
  209. return NODE_GROUP_LEVEL_0;
  210. }
  211. /* Node feature are used to disable huge nodes inside the group,
  212. * so it's possible to disable huge nodes inside of the required
  213. * nodes group.
  214. */
  215. virtual int get_feature()
  216. {
  217. return bump == SHADER_BUMP_NONE ? 0 : NODE_FEATURE_BUMP;
  218. }
  219. /* Get closure ID to which the node compiles into. */
  220. virtual ClosureType get_closure_type()
  221. {
  222. return CLOSURE_NONE_ID;
  223. }
  224. /* Check whether settings of the node equals to another one.
  225. *
  226. * This is mainly used to check whether two nodes can be merged
  227. * together. Meaning, runtime stuff like node id and unbound slots
  228. * will be ignored for comparison.
  229. *
  230. * NOTE: If some node can't be de-duplicated for whatever reason it
  231. * is to be handled in the subclass.
  232. */
  233. virtual bool equals(const ShaderNode &other);
  234. };
  235. /* Node definition utility macros */
  236. #define SHADER_NODE_CLASS(type) \
  237. NODE_DECLARE \
  238. type(); \
  239. virtual ShaderNode *clone() const \
  240. { \
  241. return new type(*this); \
  242. } \
  243. virtual void compile(SVMCompiler &compiler); \
  244. virtual void compile(OSLCompiler &compiler);
  245. #define SHADER_NODE_NO_CLONE_CLASS(type) \
  246. NODE_DECLARE \
  247. type(); \
  248. virtual void compile(SVMCompiler &compiler); \
  249. virtual void compile(OSLCompiler &compiler);
  250. #define SHADER_NODE_BASE_CLASS(type) \
  251. virtual ShaderNode *clone() const \
  252. { \
  253. return new type(*this); \
  254. } \
  255. virtual void compile(SVMCompiler &compiler); \
  256. virtual void compile(OSLCompiler &compiler);
  257. class ShaderNodeIDComparator {
  258. public:
  259. bool operator()(const ShaderNode *n1, const ShaderNode *n2) const
  260. {
  261. return n1->id < n2->id;
  262. }
  263. };
  264. typedef set<ShaderNode *, ShaderNodeIDComparator> ShaderNodeSet;
  265. typedef map<ShaderNode *, ShaderNode *, ShaderNodeIDComparator> ShaderNodeMap;
  266. /* Graph
  267. *
  268. * Shader graph of nodes. Also does graph manipulations for default inputs,
  269. * bump mapping from displacement, and possibly other things in the future. */
  270. class ShaderGraph {
  271. public:
  272. list<ShaderNode *> nodes;
  273. size_t num_node_ids;
  274. bool finalized;
  275. bool simplified;
  276. string displacement_hash;
  277. ShaderGraph();
  278. ~ShaderGraph();
  279. ShaderNode *add(ShaderNode *node);
  280. OutputNode *output();
  281. void connect(ShaderOutput *from, ShaderInput *to);
  282. void disconnect(ShaderOutput *from);
  283. void disconnect(ShaderInput *to);
  284. void relink(ShaderInput *from, ShaderInput *to);
  285. void relink(ShaderOutput *from, ShaderOutput *to);
  286. void relink(ShaderNode *node, ShaderOutput *from, ShaderOutput *to);
  287. void remove_proxy_nodes();
  288. void compute_displacement_hash();
  289. void simplify(Scene *scene);
  290. void finalize(Scene *scene,
  291. bool do_bump = false,
  292. bool do_simplify = false,
  293. bool bump_in_object_space = false);
  294. int get_num_closures();
  295. void dump_graph(const char *filename);
  296. protected:
  297. typedef pair<ShaderNode *const, ShaderNode *> NodePair;
  298. void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
  299. void clear_nodes();
  300. void copy_nodes(ShaderNodeSet &nodes, ShaderNodeMap &nnodemap);
  301. void break_cycles(ShaderNode *node, vector<bool> &visited, vector<bool> &on_stack);
  302. void bump_from_displacement(bool use_object_space);
  303. void refine_bump_nodes();
  304. void expand();
  305. void default_inputs(bool do_osl);
  306. void transform_multi_closure(ShaderNode *node, ShaderOutput *weight_out, bool volume);
  307. /* Graph simplification routines. */
  308. void clean(Scene *scene);
  309. void constant_fold(Scene *scene);
  310. void simplify_settings(Scene *scene);
  311. void deduplicate_nodes();
  312. void verify_volume_output();
  313. };
  314. CCL_NAMESPACE_END
  315. #endif /* __GRAPH_H__ */