shader_graph.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*************************************************************************/
  2. /* shader_graph.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SHADER_GRAPH_H
  31. #define SHADER_GRAPH_H
  32. // FIXME: Needs to be ported to the new 3.0 shader API
  33. #if 0
  34. #include "map.h"
  35. #include "scene/resources/shader.h"
  36. class ShaderGraph : public Shader {
  37. GDCLASS( ShaderGraph, Shader );
  38. RES_BASE_EXTENSION("vshader");
  39. public:
  40. enum NodeType {
  41. NODE_INPUT, // all inputs (shader type dependent)
  42. NODE_SCALAR_CONST, //scalar constant
  43. NODE_VEC_CONST, //vec3 constant
  44. NODE_RGB_CONST, //rgb constant (shows a color picker instead)
  45. NODE_XFORM_CONST, // 4x4 matrix constant
  46. NODE_TIME, // time in seconds
  47. NODE_SCREEN_TEX, // screen texture sampler (takes UV) (only usable in fragment shader)
  48. NODE_SCALAR_OP, // scalar vs scalar op (mul, add, div, etc)
  49. NODE_VEC_OP, // vec3 vs vec3 op (mul,ad,div,crossprod,etc)
  50. NODE_VEC_SCALAR_OP, // vec3 vs scalar op (mul, add, div, etc)
  51. NODE_RGB_OP, // vec3 vs vec3 rgb op (with scalar amount), like brighten, darken, burn, dodge, multiply, etc.
  52. NODE_XFORM_MULT, // mat4 x mat4
  53. NODE_XFORM_VEC_MULT, // mat4 x vec3 mult (with no-translation option)
  54. NODE_XFORM_VEC_INV_MULT, // mat4 x vec3 inverse mult (with no-translation option)
  55. NODE_SCALAR_FUNC, // scalar function (sin, cos, etc)
  56. NODE_VEC_FUNC, // vector function (normalize, negate, reciprocal, rgb2hsv, hsv2rgb, etc, etc)
  57. NODE_VEC_LEN, // vec3 length
  58. NODE_DOT_PROD, // vec3 . vec3 (dot product -> scalar output)
  59. NODE_VEC_TO_SCALAR, // 1 vec3 input, 3 scalar outputs
  60. NODE_SCALAR_TO_VEC, // 3 scalar input, 1 vec3 output
  61. NODE_XFORM_TO_VEC, // 3 vec input, 1 xform output
  62. NODE_VEC_TO_XFORM, // 3 vec input, 1 xform output
  63. NODE_SCALAR_INTERP, // scalar interpolation (with optional curve)
  64. NODE_VEC_INTERP, // vec3 interpolation (with optional curve)
  65. NODE_COLOR_RAMP, //take scalar, output vec3
  66. NODE_CURVE_MAP, //take scalar, otput scalar
  67. NODE_SCALAR_INPUT, // scalar uniform (assignable in material)
  68. NODE_VEC_INPUT, // vec3 uniform (assignable in material)
  69. NODE_RGB_INPUT, // color uniform (assignable in material)
  70. NODE_XFORM_INPUT, // mat4 uniform (assignable in material)
  71. NODE_TEXTURE_INPUT, // texture input (assignable in material)
  72. NODE_CUBEMAP_INPUT, // cubemap input (assignable in material)
  73. NODE_DEFAULT_TEXTURE,
  74. NODE_OUTPUT, // output (shader type dependent)
  75. NODE_COMMENT, // comment
  76. NODE_TYPE_MAX
  77. };
  78. struct Connection {
  79. int src_id;
  80. int src_slot;
  81. int dst_id;
  82. int dst_slot;
  83. };
  84. enum SlotType {
  85. SLOT_TYPE_SCALAR,
  86. SLOT_TYPE_VEC,
  87. SLOT_TYPE_XFORM,
  88. SLOT_TYPE_TEXTURE,
  89. SLOT_MAX
  90. };
  91. enum ShaderType {
  92. SHADER_TYPE_VERTEX,
  93. SHADER_TYPE_FRAGMENT,
  94. SHADER_TYPE_LIGHT,
  95. SHADER_TYPE_MAX
  96. };
  97. enum SlotDir {
  98. SLOT_IN,
  99. SLOT_OUT
  100. };
  101. enum GraphError {
  102. GRAPH_OK,
  103. GRAPH_ERROR_CYCLIC,
  104. GRAPH_ERROR_MISSING_CONNECTIONS
  105. };
  106. private:
  107. String _find_unique_name(const String& p_base);
  108. enum {SLOT_DEFAULT_VALUE = 0x7FFFFFFF};
  109. struct SourceSlot {
  110. int id;
  111. int slot;
  112. bool operator==(const SourceSlot& p_slot) const {
  113. return id==p_slot.id && slot==p_slot.slot;
  114. }
  115. };
  116. struct Node {
  117. Vector2 pos;
  118. NodeType type;
  119. Variant param1;
  120. Variant param2;
  121. Map<int, Variant> defaults;
  122. int id;
  123. mutable int order; // used for sorting
  124. int sort_order;
  125. Map<int,SourceSlot> connections;
  126. };
  127. struct ShaderData {
  128. Map<int,Node> node_map;
  129. GraphError error;
  130. } shader[3];
  131. struct InOutParamInfo {
  132. Mode shader_mode;
  133. ShaderType shader_type;
  134. const char *name;
  135. const char *variable;
  136. const char *postfix;
  137. SlotType slot_type;
  138. SlotDir dir;
  139. };
  140. static const InOutParamInfo inout_param_info[];
  141. struct NodeSlotInfo {
  142. enum { MAX_INS=3, MAX_OUTS=3 };
  143. NodeType type;
  144. const SlotType ins[MAX_INS];
  145. const SlotType outs[MAX_OUTS];
  146. };
  147. static const NodeSlotInfo node_slot_info[];
  148. bool _pending_update_shader;
  149. void _update_shader();
  150. void _request_update();
  151. void _plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d,uint8_t* p_heights,bool *p_useds);
  152. void _add_node_code(ShaderType p_type,Node *p_node,const Vector<String>& p_inputs,String& code);
  153. Array _get_node_list(ShaderType p_type) const;
  154. Array _get_connections(ShaderType p_type) const;
  155. void _set_data(const Dictionary& p_data);
  156. Dictionary _get_data() const;
  157. protected:
  158. static void _bind_methods();
  159. public:
  160. void node_add(ShaderType p_type, NodeType p_node_type, int p_id);
  161. void node_remove(ShaderType p_which,int p_id);
  162. void node_set_position(ShaderType p_which,int p_id,const Point2& p_pos);
  163. Point2 node_get_position(ShaderType p_which,int p_id) const;
  164. void get_node_list(ShaderType p_which,List<int> *p_node_list) const;
  165. NodeType node_get_type(ShaderType p_which,int p_id) const;
  166. void scalar_const_node_set_value(ShaderType p_which,int p_id,float p_value);
  167. float scalar_const_node_get_value(ShaderType p_which,int p_id) const;
  168. void vec_const_node_set_value(ShaderType p_which,int p_id,const Vector3& p_value);
  169. Vector3 vec_const_node_get_value(ShaderType p_which,int p_id) const;
  170. void rgb_const_node_set_value(ShaderType p_which,int p_id,const Color& p_value);
  171. Color rgb_const_node_get_value(ShaderType p_which,int p_id) const;
  172. void xform_const_node_set_value(ShaderType p_which,int p_id,const Transform& p_value);
  173. Transform xform_const_node_get_value(ShaderType p_which,int p_id) const;
  174. void texture_node_set_filter_size(ShaderType p_which,int p_id,int p_size);
  175. int texture_node_get_filter_size(ShaderType p_which,int p_id) const;
  176. void texture_node_set_filter_strength(ShaderType p_which,float p_id,float p_strength);
  177. float texture_node_get_filter_strength(ShaderType p_which,float p_id) const;
  178. void duplicate_nodes(ShaderType p_which, List<int> &p_nodes);
  179. List<int> generate_ids(ShaderType p_type, int count);
  180. enum ScalarOp {
  181. SCALAR_OP_ADD,
  182. SCALAR_OP_SUB,
  183. SCALAR_OP_MUL,
  184. SCALAR_OP_DIV,
  185. SCALAR_OP_MOD,
  186. SCALAR_OP_POW,
  187. SCALAR_OP_MAX,
  188. SCALAR_OP_MIN,
  189. SCALAR_OP_ATAN2,
  190. SCALAR_MAX_OP
  191. };
  192. void scalar_op_node_set_op(ShaderType p_which,float p_id,ScalarOp p_op);
  193. ScalarOp scalar_op_node_get_op(ShaderType p_which,float p_id) const;
  194. enum VecOp {
  195. VEC_OP_ADD,
  196. VEC_OP_SUB,
  197. VEC_OP_MUL,
  198. VEC_OP_DIV,
  199. VEC_OP_MOD,
  200. VEC_OP_POW,
  201. VEC_OP_MAX,
  202. VEC_OP_MIN,
  203. VEC_OP_CROSS,
  204. VEC_MAX_OP
  205. };
  206. void vec_op_node_set_op(ShaderType p_which,float p_id,VecOp p_op);
  207. VecOp vec_op_node_get_op(ShaderType p_which,float p_id) const;
  208. enum VecScalarOp {
  209. VEC_SCALAR_OP_MUL,
  210. VEC_SCALAR_OP_DIV,
  211. VEC_SCALAR_OP_POW,
  212. VEC_SCALAR_MAX_OP
  213. };
  214. void vec_scalar_op_node_set_op(ShaderType p_which,float p_id,VecScalarOp p_op);
  215. VecScalarOp vec_scalar_op_node_get_op(ShaderType p_which,float p_id) const;
  216. enum RGBOp {
  217. RGB_OP_SCREEN,
  218. RGB_OP_DIFFERENCE,
  219. RGB_OP_DARKEN,
  220. RGB_OP_LIGHTEN,
  221. RGB_OP_OVERLAY,
  222. RGB_OP_DODGE,
  223. RGB_OP_BURN,
  224. RGB_OP_SOFT_LIGHT,
  225. RGB_OP_HARD_LIGHT,
  226. RGB_MAX_OP
  227. };
  228. void rgb_op_node_set_op(ShaderType p_which,float p_id,RGBOp p_op);
  229. RGBOp rgb_op_node_get_op(ShaderType p_which,float p_id) const;
  230. void xform_vec_mult_node_set_no_translation(ShaderType p_which,int p_id,bool p_no_translation);
  231. bool xform_vec_mult_node_get_no_translation(ShaderType p_which,int p_id) const;
  232. enum ScalarFunc {
  233. SCALAR_FUNC_SIN,
  234. SCALAR_FUNC_COS,
  235. SCALAR_FUNC_TAN,
  236. SCALAR_FUNC_ASIN,
  237. SCALAR_FUNC_ACOS,
  238. SCALAR_FUNC_ATAN,
  239. SCALAR_FUNC_SINH,
  240. SCALAR_FUNC_COSH,
  241. SCALAR_FUNC_TANH,
  242. SCALAR_FUNC_LOG,
  243. SCALAR_FUNC_EXP,
  244. SCALAR_FUNC_SQRT,
  245. SCALAR_FUNC_ABS,
  246. SCALAR_FUNC_SIGN,
  247. SCALAR_FUNC_FLOOR,
  248. SCALAR_FUNC_ROUND,
  249. SCALAR_FUNC_CEIL,
  250. SCALAR_FUNC_FRAC,
  251. SCALAR_FUNC_SATURATE,
  252. SCALAR_FUNC_NEGATE,
  253. SCALAR_MAX_FUNC
  254. };
  255. void scalar_func_node_set_function(ShaderType p_which,int p_id,ScalarFunc p_func);
  256. ScalarFunc scalar_func_node_get_function(ShaderType p_which,int p_id) const;
  257. enum VecFunc {
  258. VEC_FUNC_NORMALIZE,
  259. VEC_FUNC_SATURATE,
  260. VEC_FUNC_NEGATE,
  261. VEC_FUNC_RECIPROCAL,
  262. VEC_FUNC_RGB2HSV,
  263. VEC_FUNC_HSV2RGB,
  264. VEC_MAX_FUNC
  265. };
  266. void default_set_value(ShaderType p_which,int p_id,int p_param, const Variant& p_value);
  267. Variant default_get_value(ShaderType p_which,int p_id,int p_param);
  268. void vec_func_node_set_function(ShaderType p_which,int p_id,VecFunc p_func);
  269. VecFunc vec_func_node_get_function(ShaderType p_which,int p_id) const;
  270. void color_ramp_node_set_ramp(ShaderType p_which,int p_id,const PoolVector<Color>& p_colors, const PoolVector<real_t>& p_offsets);
  271. PoolVector<Color> color_ramp_node_get_colors(ShaderType p_which,int p_id) const;
  272. PoolVector<real_t> color_ramp_node_get_offsets(ShaderType p_which,int p_id) const;
  273. void curve_map_node_set_points(ShaderType p_which, int p_id, const PoolVector<Vector2>& p_points);
  274. PoolVector<Vector2> curve_map_node_get_points(ShaderType p_which,int p_id) const;
  275. void input_node_set_name(ShaderType p_which,int p_id,const String& p_name);
  276. String input_node_get_name(ShaderType p_which,int p_id);
  277. void scalar_input_node_set_value(ShaderType p_which,int p_id,float p_value);
  278. float scalar_input_node_get_value(ShaderType p_which,int p_id) const;
  279. void vec_input_node_set_value(ShaderType p_which,int p_id,const Vector3& p_value);
  280. Vector3 vec_input_node_get_value(ShaderType p_which,int p_id) const;
  281. void rgb_input_node_set_value(ShaderType p_which,int p_id,const Color& p_value);
  282. Color rgb_input_node_get_value(ShaderType p_which,int p_id) const;
  283. void xform_input_node_set_value(ShaderType p_which,int p_id,const Transform& p_value);
  284. Transform xform_input_node_get_value(ShaderType p_which,int p_id) const;
  285. void texture_input_node_set_value(ShaderType p_which,int p_id,const Ref<Texture>& p_texture);
  286. Ref<Texture> texture_input_node_get_value(ShaderType p_which,int p_id) const;
  287. void cubemap_input_node_set_value(ShaderType p_which,int p_id,const Ref<CubeMap>& p_cubemap);
  288. Ref<CubeMap> cubemap_input_node_get_value(ShaderType p_which,int p_id) const;
  289. void comment_node_set_text(ShaderType p_which,int p_id,const String& p_comment);
  290. String comment_node_get_text(ShaderType p_which,int p_id) const;
  291. Error connect_node(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot);
  292. bool is_node_connected(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) const;
  293. void disconnect_node(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot);
  294. void get_node_connections(ShaderType p_which,List<Connection> *p_connections) const;
  295. bool is_slot_connected(ShaderType p_which,int p_dst_id,int slot_id);
  296. void clear(ShaderType p_which);
  297. Variant node_get_state(ShaderType p_type, int p_node) const;
  298. void node_set_state(ShaderType p_type, int p_id, const Variant& p_state);
  299. GraphError get_graph_error(ShaderType p_type) const;
  300. int node_count(ShaderType p_which, int p_type);
  301. static int get_type_input_count(NodeType p_type);
  302. static int get_type_output_count(NodeType p_type);
  303. static SlotType get_type_input_type(NodeType p_type,int p_idx);
  304. static SlotType get_type_output_type(NodeType p_type,int p_idx);
  305. static bool is_type_valid(Mode p_mode,ShaderType p_type);
  306. struct SlotInfo {
  307. String name;
  308. SlotType type;
  309. SlotDir dir;
  310. };
  311. static void get_input_output_node_slot_info(Mode p_mode, ShaderType p_type, List<SlotInfo> *r_slots);
  312. static int get_node_input_slot_count(Mode p_mode, ShaderType p_shader_type,NodeType p_type);
  313. static int get_node_output_slot_count(Mode p_mode, ShaderType p_shader_type,NodeType p_type);
  314. static SlotType get_node_input_slot_type(Mode p_mode, ShaderType p_shader_type,NodeType p_type,int p_idx);
  315. static SlotType get_node_output_slot_type(Mode p_mode, ShaderType p_shader_type,NodeType p_type,int p_idx);
  316. ShaderGraph(Mode p_mode);
  317. ~ShaderGraph();
  318. };
  319. //helper functions
  320. VARIANT_ENUM_CAST( ShaderGraph::NodeType );
  321. VARIANT_ENUM_CAST( ShaderGraph::ShaderType );
  322. VARIANT_ENUM_CAST( ShaderGraph::SlotType );
  323. VARIANT_ENUM_CAST( ShaderGraph::ScalarOp );
  324. VARIANT_ENUM_CAST( ShaderGraph::VecOp );
  325. VARIANT_ENUM_CAST( ShaderGraph::VecScalarOp );
  326. VARIANT_ENUM_CAST( ShaderGraph::RGBOp );
  327. VARIANT_ENUM_CAST( ShaderGraph::ScalarFunc );
  328. VARIANT_ENUM_CAST( ShaderGraph::VecFunc );
  329. VARIANT_ENUM_CAST( ShaderGraph::GraphError );
  330. class MaterialShaderGraph : public ShaderGraph {
  331. GDCLASS( MaterialShaderGraph, ShaderGraph );
  332. public:
  333. MaterialShaderGraph() : ShaderGraph(MODE_MATERIAL) {
  334. }
  335. };
  336. class CanvasItemShaderGraph : public ShaderGraph {
  337. GDCLASS( CanvasItemShaderGraph, ShaderGraph );
  338. public:
  339. CanvasItemShaderGraph() : ShaderGraph(MODE_CANVAS_ITEM) {
  340. }
  341. };
  342. #endif
  343. #endif // SHADER_GRAPH_H