diff options
Diffstat (limited to 'scene/resources/visual_shader.h')
-rw-r--r-- | scene/resources/visual_shader.h | 183 |
1 files changed, 143 insertions, 40 deletions
diff --git a/scene/resources/visual_shader.h b/scene/resources/visual_shader.h index e3d5200e6b..b3efac02aa 100644 --- a/scene/resources/visual_shader.h +++ b/scene/resources/visual_shader.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,8 @@ #ifndef VISUAL_SHADER_H #define VISUAL_SHADER_H -#include "core/string_builder.h" +#include "core/string/string_builder.h" +#include "core/templates/safe_refcount.h" #include "scene/gui/control.h" #include "scene/resources/shader.h" @@ -43,24 +44,27 @@ class VisualShader : public Shader { friend class VisualShaderNodeVersionChecker; - String version = ""; + Dictionary engine_version; public: enum Type { TYPE_VERTEX, TYPE_FRAGMENT, TYPE_LIGHT, - TYPE_EMIT, + TYPE_START, TYPE_PROCESS, - TYPE_END, + TYPE_COLLIDE, + TYPE_START_CUSTOM, + TYPE_PROCESS_CUSTOM, + TYPE_SKY, TYPE_MAX }; struct Connection { - int from_node; - int from_port; - int to_node; - int to_port; + int from_node = 0; + int from_port = 0; + int to_node = 0; + int to_port = 0; }; struct DefaultTextureParam { @@ -90,7 +94,7 @@ private: Vector2 graph_offset; struct RenderModeEnums { - Shader::Mode mode; + Shader::Mode mode = Shader::Mode::MODE_MAX; const char *string; }; @@ -99,7 +103,7 @@ private: static RenderModeEnums render_mode_enums[]; - volatile mutable bool dirty = true; + mutable SafeFlag dirty; void _queue_update(); union ConnectionKey { @@ -107,7 +111,7 @@ private: uint64_t node : 32; uint64_t port : 32; }; - uint64_t key; + uint64_t key = 0; bool operator<(const ConnectionKey &p_key) const { return key < p_key.key; } @@ -126,15 +130,19 @@ protected: bool _get(const StringName &p_name, Variant &r_ret) const; void _get_property_list(List<PropertyInfo> *p_list) const; + virtual void reset_state() override; + public: // internal methods void set_shader_type(Type p_type); Type get_shader_type() const; public: - void set_version(const String &p_version); - String get_version() const; + void set_engine_version(const Dictionary &p_version); + Dictionary get_engine_version() const; - void update_version(const String &p_new_version); +#ifndef DISABLE_DEPRECATED + void update_engine_version(const Dictionary &p_new_version); +#endif /* DISABLE_DEPRECATED */ enum { NODE_ID_INVALID = -1, @@ -152,6 +160,7 @@ public: int find_node_id(Type p_type, const Ref<VisualShaderNode> &p_node) const; void remove_node(Type p_type, int p_id); + void replace_node(Type p_type, int p_id, const StringName &p_new_class); bool is_node_connection(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const; @@ -175,7 +184,7 @@ public: String generate_preview_shader(Type p_type, int p_node, int p_port, Vector<DefaultTextureParam> &r_default_tex_params) const; - String validate_port_name(const String &p_name, const List<String> &p_input_ports, const List<String> &p_output_ports) const; + String validate_port_name(const String &p_port_name, VisualShaderNode *p_node, int p_port_id, bool p_output) const; String validate_uniform_name(const String &p_name, const Ref<VisualShaderNodeUniform> &p_uniform) const; VisualShader(); @@ -194,9 +203,12 @@ class VisualShaderNode : public Resource { Map<int, Variant> default_input_values; Map<int, bool> connected_input_ports; Map<int, int> connected_output_ports; + Map<int, bool> expanded_output_ports; protected: bool simple_decl = true; + bool disabled = false; + static void _bind_methods(); public: @@ -218,10 +230,12 @@ public: virtual PortType get_input_port_type(int p_port) const = 0; virtual String get_input_port_name(int p_port) const = 0; - void set_input_port_default_value(int p_port, const Variant &p_value); + virtual void set_input_port_default_value(int p_port, const Variant &p_value); 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) Array get_default_input_values() const; - void set_default_input_values(const Array &p_values); + virtual void set_default_input_values(const Array &p_values); + virtual void remove_input_port_default_value(int p_port); + virtual void clear_default_input_values(); virtual int get_output_port_count() const = 0; virtual PortType get_output_port_type(int p_port) const = 0; @@ -240,17 +254,28 @@ public: void set_input_port_connected(int p_port, bool p_connected); virtual bool is_generate_input_var(int p_port) const; + virtual bool is_output_port_expandable(int p_port) const; + void _set_output_ports_expanded(const Array &p_data); + Array _get_output_ports_expanded() const; + void _set_output_port_expanded(int p_port, bool p_expanded); + bool _is_output_port_expanded(int p_port) const; + int get_expanded_output_port_count() const; + virtual bool is_code_generated() const; virtual bool is_show_prop_names() const; virtual bool is_use_prop_slots() const; + bool is_disabled() const; + void set_disabled(bool p_disabled = true); + virtual Vector<StringName> get_editable_properties() const; virtual Vector<VisualShader::DefaultTextureParam> get_default_texture_parameters(VisualShader::Type p_type, int p_id) const; virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const; virtual String generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const; virtual String generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const; - 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, bool p_for_preview = false) 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 + // 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. + 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, bool p_for_preview = false) const = 0; virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const; @@ -264,9 +289,10 @@ class VisualShaderNodeCustom : public VisualShaderNode { struct Port { String name; - int type; + int type = 0; }; + bool is_initialized = false; List<Port> input_ports; List<Port> output_ports; @@ -283,7 +309,28 @@ protected: virtual PortType get_output_port_type(int p_port) const override; virtual String get_output_port_name(int p_port) const override; + virtual void set_input_port_default_value(int p_port, const Variant &p_value) override; + virtual void set_default_input_values(const Array &p_values) override; + virtual void remove_input_port_default_value(int p_port) override; + virtual void clear_default_input_values() override; + + GDVIRTUAL0RC(String, _get_name) + GDVIRTUAL0RC(String, _get_description) + GDVIRTUAL0RC(String, _get_category) + GDVIRTUAL0RC(int, _get_return_icon_type) + GDVIRTUAL0RC(int, _get_input_port_count) + GDVIRTUAL1RC(int, _get_input_port_type, int) + GDVIRTUAL1RC(String, _get_input_port_name, int) + GDVIRTUAL0RC(int, _get_output_port_count) + GDVIRTUAL1RC(int, _get_output_port_type, int) + GDVIRTUAL1RC(String, _get_output_port_name, int) + GDVIRTUAL4RC(String, _get_code, Vector<String>, TypedArray<String>, int, int) + GDVIRTUAL1RC(String, _get_global_code, int) + GDVIRTUAL0RC(bool, _is_highend) + protected: + void _set_input_port_default_value(int p_port, const Variant &p_value); + 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, bool p_for_preview = false) const override; virtual String generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const override; @@ -292,6 +339,9 @@ protected: public: VisualShaderNodeCustom(); void update_ports(); + + bool _is_initialized(); + void _set_initialized(bool p_enabled); }; ///// @@ -304,9 +354,9 @@ class VisualShaderNodeInput : public VisualShaderNode { Shader::Mode shader_mode = Shader::MODE_MAX; struct Port { - Shader::Mode mode; - VisualShader::Type shader_type; - PortType type; + Shader::Mode mode = Shader::Mode::MODE_MAX; + VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX; + PortType type = PortType::PORT_TYPE_MAX; const char *name; const char *string; }; @@ -316,6 +366,10 @@ class VisualShaderNodeInput : public VisualShaderNode { String input_name = "[None]"; +public: + void set_shader_type(VisualShader::Type p_shader_type); + void set_shader_mode(Shader::Mode p_shader_mode); + protected: static void _bind_methods(); void _validate_property(PropertyInfo &property) const override; @@ -355,13 +409,13 @@ class VisualShaderNodeOutput : public VisualShaderNode { public: friend class VisualShader; - VisualShader::Type shader_type; - Shader::Mode shader_mode; + VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX; + Shader::Mode shader_mode = Shader::Mode::MODE_MAX; struct Port { - Shader::Mode mode; - VisualShader::Type shader_type; - PortType type; + Shader::Mode mode = Shader::Mode::MODE_MAX; + VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX; + PortType type = PortType::PORT_TYPE_MAX; const char *name; const char *string; }; @@ -395,6 +449,7 @@ public: QUAL_NONE, QUAL_GLOBAL, QUAL_INSTANCE, + QUAL_MAX, }; private: @@ -417,6 +472,7 @@ public: bool is_global_code_generated() const; virtual bool is_qualifier_supported(Qualifier p_qual) const = 0; + virtual bool is_convertible_to_constant() const = 0; virtual Vector<StringName> get_editable_properties() const override; virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const override; @@ -478,6 +534,7 @@ public: String get_uniform_name_by_index(int p_idx) const; UniformType get_uniform_type_by_name(const String &p_name) const; UniformType get_uniform_type_by_index(int p_idx) const; + PortType get_port_type_by_index(int p_idx) const; virtual Vector<StringName> get_editable_properties() const override; @@ -486,20 +543,71 @@ public: VisualShaderNodeUniformRef(); }; -class VisualShaderNodeGroupBase : public VisualShaderNode { - GDCLASS(VisualShaderNodeGroupBase, VisualShaderNode); +class VisualShaderNodeResizableBase : public VisualShaderNode { + GDCLASS(VisualShaderNodeResizableBase, VisualShaderNode); + +protected: + Vector2 size = Size2(0, 0); + bool allow_v_resize = true; + +protected: + static void _bind_methods(); + +public: + void set_size(const Vector2 &p_size); + Vector2 get_size() const; + + bool is_allow_v_resize() const; + void set_allow_v_resize(bool p_enabled); + + VisualShaderNodeResizableBase(); +}; + +class VisualShaderNodeComment : public VisualShaderNodeResizableBase { + GDCLASS(VisualShaderNodeComment, VisualShaderNodeResizableBase); + +protected: + String title = "Comment"; + String description = ""; + +protected: + static void _bind_methods(); + +public: + virtual String get_caption() const override; + + virtual int get_input_port_count() const override; + virtual PortType get_input_port_type(int p_port) const override; + virtual String get_input_port_name(int p_port) const override; + + virtual int get_output_port_count() const override; + virtual PortType get_output_port_type(int p_port) const override; + virtual String get_output_port_name(int p_port) const override; + + 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, bool p_for_preview = false) const override; + + void set_title(const String &p_title); + String get_title() const; + + void set_description(const String &p_description); + String get_description() const; + + VisualShaderNodeComment(); +}; + +class VisualShaderNodeGroupBase : public VisualShaderNodeResizableBase { + GDCLASS(VisualShaderNodeGroupBase, VisualShaderNodeResizableBase); private: void _apply_port_changes(); protected: - Vector2 size = Size2(0, 0); String inputs = ""; String outputs = ""; bool editable = false; struct Port { - PortType type; + PortType type = PortType::PORT_TYPE_MAX; String name; }; @@ -511,11 +619,6 @@ protected: static void _bind_methods(); public: - virtual String get_caption() const override; - - void set_size(const Vector2 &p_size); - Vector2 get_size() const; - void set_inputs(const String &p_inputs); String get_inputs() const; @@ -549,8 +652,8 @@ public: int get_free_input_port_id() const; int get_free_output_port_id() const; - void set_control(Control *p_control, int p_index); - Control *get_control(int p_index); + void set_ctrl_pressed(Control *p_control, int p_index); + Control *is_ctrl_pressed(int p_index); void set_editable(bool p_enabled); bool is_editable() const; |