shader_graph.h 14 KB

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