visual_shader.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*************************************************************************/
  2. /* visual_shader.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 VISUAL_SHADER_H
  31. #define VISUAL_SHADER_H
  32. #include "core/string_builder.h"
  33. #include "scene/resources/shader.h"
  34. class VisualShaderNodeUniform;
  35. class VisualShaderNode;
  36. class VisualShader : public Shader {
  37. GDCLASS(VisualShader, Shader)
  38. public:
  39. enum Type {
  40. TYPE_VERTEX,
  41. TYPE_FRAGMENT,
  42. TYPE_LIGHT,
  43. TYPE_MAX
  44. };
  45. struct Connection {
  46. int from_node;
  47. int from_port;
  48. int to_node;
  49. int to_port;
  50. };
  51. struct DefaultTextureParam {
  52. StringName name;
  53. Ref<Texture> param;
  54. };
  55. private:
  56. struct Node {
  57. Ref<VisualShaderNode> node;
  58. Vector2 position;
  59. };
  60. struct Graph {
  61. Map<int, Node> nodes;
  62. List<Connection> connections;
  63. } graph[TYPE_MAX];
  64. Shader::Mode shader_mode;
  65. Array _get_node_connections(Type p_type) const;
  66. Vector2 graph_offset;
  67. struct RenderModeEnums {
  68. Shader::Mode mode;
  69. const char *string;
  70. };
  71. HashMap<String, int> modes;
  72. Set<StringName> flags;
  73. static RenderModeEnums render_mode_enums[];
  74. volatile mutable bool dirty;
  75. void _queue_update();
  76. union ConnectionKey {
  77. struct {
  78. uint64_t node : 32;
  79. uint64_t port : 32;
  80. };
  81. uint64_t key;
  82. bool operator<(const ConnectionKey &p_key) const {
  83. return key < p_key.key;
  84. }
  85. };
  86. Error _write_node(Type p_type, StringBuilder &global_code, StringBuilder &code, Vector<DefaultTextureParam> &def_tex_params, const VMap<ConnectionKey, const List<Connection>::Element *> &input_connections, const VMap<ConnectionKey, const List<Connection>::Element *> &output_connections, int node, Set<int> &processed, bool for_preview) const;
  87. void _input_type_changed(Type p_type, int p_id);
  88. protected:
  89. virtual void _update_shader() const;
  90. static void _bind_methods();
  91. bool _set(const StringName &p_name, const Variant &p_value);
  92. bool _get(const StringName &p_name, Variant &r_ret) const;
  93. void _get_property_list(List<PropertyInfo> *p_list) const;
  94. public:
  95. enum {
  96. NODE_ID_INVALID = -1,
  97. NODE_ID_OUTPUT = 0,
  98. };
  99. void add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id);
  100. void set_node_position(Type p_type, int p_id, const Vector2 &p_position);
  101. Vector2 get_node_position(Type p_type, int p_id) const;
  102. Ref<VisualShaderNode> get_node(Type p_type, int p_id) const;
  103. Vector<int> get_node_list(Type p_type) const;
  104. int get_valid_node_id(Type p_type) const;
  105. int find_node_id(Type p_type, const Ref<VisualShaderNode> &p_node) const;
  106. void remove_node(Type p_type, int p_id);
  107. bool is_node_connection(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
  108. bool can_connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
  109. Error connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
  110. void disconnect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
  111. void get_node_connections(Type p_type, List<Connection> *r_connections) const;
  112. void set_mode(Mode p_mode);
  113. virtual Mode get_mode() const;
  114. virtual bool is_text_shader() const;
  115. void set_graph_offset(const Vector2 &p_offset);
  116. Vector2 get_graph_offset() const;
  117. String generate_preview_shader(Type p_type, int p_node, int p_port, Vector<DefaultTextureParam> &r_default_tex_params) const;
  118. String validate_uniform_name(const String &p_name, const Ref<VisualShaderNodeUniform> &p_uniform) const;
  119. VisualShader();
  120. };
  121. VARIANT_ENUM_CAST(VisualShader::Type)
  122. ///
  123. ///
  124. ///
  125. class VisualShaderNode : public Resource {
  126. GDCLASS(VisualShaderNode, Resource)
  127. int port_preview;
  128. Map<int, Variant> default_input_values;
  129. Array _get_default_input_values() const;
  130. void _set_default_input_values(const Array &p_values);
  131. protected:
  132. static void _bind_methods();
  133. public:
  134. enum PortType {
  135. PORT_TYPE_SCALAR,
  136. PORT_TYPE_VECTOR,
  137. PORT_TYPE_TRANSFORM,
  138. };
  139. virtual String get_caption() const = 0;
  140. virtual int get_input_port_count() const = 0;
  141. virtual PortType get_input_port_type(int p_port) const = 0;
  142. virtual String get_input_port_name(int p_port) const = 0;
  143. void set_input_port_default_value(int p_port, const Variant &p_value);
  144. Variant get_input_port_default_value(int p_port) const; // if NIL (default if node does not set anything) is returned, it means no default value is wanted if disconnected, thus no input var must be supplied (empty string will be supplied)
  145. virtual int get_output_port_count() const = 0;
  146. virtual PortType get_output_port_type(int p_port) const = 0;
  147. virtual String get_output_port_name(int p_port) const = 0;
  148. void set_output_port_for_preview(int p_index);
  149. int get_output_port_for_preview() const;
  150. virtual bool is_port_separator(int p_index) const;
  151. virtual Vector<StringName> get_editable_properties() const;
  152. virtual Vector<VisualShader::DefaultTextureParam> get_default_texture_parameters(VisualShader::Type p_type, int p_id) const;
  153. virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const;
  154. virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const = 0; //if no output is connected, the output var passed will be empty. if no input is connected and input is NIL, the input var passed will be empty
  155. virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const;
  156. VisualShaderNode();
  157. };
  158. /////
  159. class VisualShaderNodeInput : public VisualShaderNode {
  160. GDCLASS(VisualShaderNodeInput, VisualShaderNode)
  161. friend class VisualShader;
  162. VisualShader::Type shader_type;
  163. Shader::Mode shader_mode;
  164. struct Port {
  165. Shader::Mode mode;
  166. VisualShader::Type shader_type;
  167. PortType type;
  168. const char *name;
  169. const char *string;
  170. };
  171. static const Port ports[];
  172. static const Port preview_ports[];
  173. String input_name;
  174. protected:
  175. static void _bind_methods();
  176. void _validate_property(PropertyInfo &property) const;
  177. public:
  178. virtual int get_input_port_count() const;
  179. virtual PortType get_input_port_type(int p_port) const;
  180. virtual String get_input_port_name(int p_port) const;
  181. virtual int get_output_port_count() const;
  182. virtual PortType get_output_port_type(int p_port) const;
  183. virtual String get_output_port_name(int p_port) const;
  184. virtual String get_caption() const;
  185. virtual String generate_code_for_preview(VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const;
  186. virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const;
  187. void set_input_name(String p_name);
  188. String get_input_name() const;
  189. int get_input_index_count() const;
  190. PortType get_input_index_type(int p_index) const;
  191. String get_input_index_name(int p_index) const;
  192. PortType get_input_type_by_name(String p_name) const;
  193. virtual Vector<StringName> get_editable_properties() const;
  194. VisualShaderNodeInput();
  195. };
  196. ///
  197. class VisualShaderNodeOutput : public VisualShaderNode {
  198. GDCLASS(VisualShaderNodeOutput, VisualShaderNode)
  199. public:
  200. friend class VisualShader;
  201. VisualShader::Type shader_type;
  202. Shader::Mode shader_mode;
  203. struct Port {
  204. Shader::Mode mode;
  205. VisualShader::Type shader_type;
  206. PortType type;
  207. const char *name;
  208. const char *string;
  209. };
  210. static const Port ports[];
  211. public:
  212. virtual int get_input_port_count() const;
  213. virtual PortType get_input_port_type(int p_port) const;
  214. virtual String get_input_port_name(int p_port) const;
  215. Variant get_input_port_default_value(int p_port) const;
  216. virtual int get_output_port_count() const;
  217. virtual PortType get_output_port_type(int p_port) const;
  218. virtual String get_output_port_name(int p_port) const;
  219. virtual bool is_port_separator(int p_index) const;
  220. virtual String get_caption() const;
  221. virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const;
  222. VisualShaderNodeOutput();
  223. };
  224. class VisualShaderNodeUniform : public VisualShaderNode {
  225. GDCLASS(VisualShaderNodeUniform, VisualShaderNode)
  226. String uniform_name;
  227. protected:
  228. static void _bind_methods();
  229. public:
  230. void set_uniform_name(const String &p_name);
  231. String get_uniform_name() const;
  232. VisualShaderNodeUniform();
  233. };
  234. #endif // VISUAL_SHADER_H