Debian Codegraph @

scene/resources/visual_shader.h

raw symbols SCIP
1 /**************************************************************************/
2 /* visual_shader.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31 #pragma once
32
33 #include "core/string/string_builder.h"
34 #include "core/templates/safe_refcount.h"
35 #include "scene/gui/control.h"
36 #include "scene/resources/shader.h"
37
38 class VisualShaderNodeParameter;
39 class VisualShaderNode;
40
41 class VisualShader : public Shader {
42 GDCLASS(VisualShader, Shader);
43
44 public:
45 enum Type {
46 TYPE_VERTEX,
47 TYPE_FRAGMENT,
48 TYPE_LIGHT,
49 TYPE_START,
50 TYPE_PROCESS,
51 TYPE_COLLIDE,
52 TYPE_START_CUSTOM,
53 TYPE_PROCESS_CUSTOM,
54 TYPE_SKY,
55 TYPE_FOG,
56 TYPE_MAX
57 };
58
59 struct Connection {
60 int from_node = 0;
61 int from_port = 0;
62 int to_node = 0;
63 int to_port = 0;
64 };
65
66 struct DefaultTextureParam {
67 StringName name;
68 List<Ref<Texture>> params;
69 };
70
71 enum VaryingMode {
72 VARYING_MODE_VERTEX_TO_FRAG_LIGHT,
73 VARYING_MODE_FRAG_TO_LIGHT,
74 VARYING_MODE_MAX,
75 };
76
77 enum VaryingType {
78 VARYING_TYPE_FLOAT,
79 VARYING_TYPE_INT,
80 VARYING_TYPE_UINT,
81 VARYING_TYPE_VECTOR_2D,
82 VARYING_TYPE_VECTOR_3D,
83 VARYING_TYPE_VECTOR_4D,
84 VARYING_TYPE_BOOLEAN,
85 VARYING_TYPE_TRANSFORM,
86 VARYING_TYPE_MAX,
87 };
88
89 struct Varying {
90 String name;
91 VaryingMode mode = VARYING_MODE_MAX;
92 VaryingType type = VARYING_TYPE_MAX;
93
94 Varying() {}
95
96 Varying(String p_name, VaryingMode p_mode, VaryingType p_type) :
97 name(p_name), mode(p_mode), type(p_type) {}
98
99 bool from_string(const String &p_str) {
100 if (p_str.get_slice_count(",") != 2) {
101 return false;
102 }
103
104 mode = (VaryingMode)p_str.get_slicec(',', 0).to_int();
105 type = (VaryingType)p_str.get_slicec(',', 1).to_int();
106
107 return true;
108 }
109
110 String to_string() const {
111 return vformat("%s,%s", itos((int)mode), itos((int)type));
112 }
113 };
114
115 private:
116 struct Node {
117 Ref<VisualShaderNode> node;
118 Vector2 position;
119 LocalVector<int> prev_connected_nodes;
120 LocalVector<int> next_connected_nodes;
121 };
122
123 struct Graph {
124 RBMap<int, Node> nodes;
125 List<Connection> connections;
126 } graph[TYPE_MAX];
127
128 Shader::Mode shader_mode = Shader::MODE_SPATIAL;
129 mutable String previous_code;
130
131 TypedArray<Dictionary> _get_node_connections(Type p_type) const;
132
133 HashMap<String, int> modes;
134 HashSet<StringName> flags;
135
136 bool stencil_enabled = false;
137 HashMap<String, int> stencil_modes;
138 HashSet<StringName> stencil_flags;
139 int stencil_reference = 1;
140
141 HashMap<String, Varying> varyings;
142 #ifdef TOOLS_ENABLED
143 HashMap<String, Variant> preview_params;
144 #endif
145 List<Varying> varyings_list;
146
147 mutable SafeFlag dirty;
148 void _queue_update();
149
150 union ConnectionKey {
151 struct {
152 uint64_t node : 32;
153 uint64_t port : 32;
154 };
155 uint64_t key = 0;
156
157 uint32_t hash() const { return HashMapHasherDefault::hash(key); }
158 bool is_same(const ConnectionKey &p_key) const { return HashMapComparatorDefault<uint64_t>::compare(key, p_key.key); }
159 };
160
161 Error _write_node(Type p_type, StringBuilder *p_global_code, StringBuilder *p_global_code_per_node, HashMap<Type, StringBuilder> *p_global_code_per_func, StringBuilder &r_code, Vector<DefaultTextureParam> &r_def_tex_params, const HashMap<ConnectionKey, const List<Connection>::Element *> &p_input_connections, int p_node, HashSet<int> &r_processed, bool p_for_preview, HashSet<StringName> &r_classes) const;
162
163 void _input_type_changed(Type p_type, int p_id);
164 bool has_func_name(RenderingServer::ShaderMode p_mode, const String &p_func_name) const;
165
166 bool _check_reroute_subgraph(Type p_type, int p_target_port_type, int p_reroute_node, List<int> *r_visited_reroute_nodes = nullptr) const;
167
168 protected:
169 virtual void _update_shader() const override;
170 static void _bind_methods();
171
172 bool _set(const StringName &p_name, const Variant &p_value);
173 bool _get(const StringName &p_name, Variant &r_ret) const;
174 void _get_property_list(List<PropertyInfo> *p_list) const;
175 void _validate_property(PropertyInfo &p_property) const;
176
177 virtual void reset_state() override;
178
179 public: // internal methods
180 enum {
181 NODE_ID_INVALID = -1,
182 NODE_ID_OUTPUT = 0,
183 };
184
185 void add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id);
186 void set_node_position(Type p_type, int p_id, const Vector2 &p_position);
187 int has_node_embeds() const;
188
189 void add_varying(const String &p_name, VaryingMode p_mode, VaryingType p_type);
190 void remove_varying(const String &p_name);
191 bool has_varying(const String &p_name) const;
192 int get_varyings_count() const;
193 const Varying *get_varying_by_index(int p_idx) const;
194
195 void set_varying_mode(const String &p_name, VaryingMode p_mode);
196 VaryingMode get_varying_mode(const String &p_name);
197
198 void set_varying_type(const String &p_name, VaryingType p_type);
199 VaryingType get_varying_type(const String &p_name);
200
201 void _set_preview_shader_parameter(const String &p_name, const Variant &p_value);
202 Variant _get_preview_shader_parameter(const String &p_name) const;
203 bool _has_preview_shader_parameter(const String &p_name) const;
204
205 Vector2 get_node_position(Type p_type, int p_id) const;
206 Ref<VisualShaderNode> get_node(Type p_type, int p_id) const;
207
208 _FORCE_INLINE_ Ref<VisualShaderNode> get_node_unchecked(Type p_type, int p_id) const {
209 return graph[p_type].nodes[p_id].node;
210 }
211 _FORCE_INLINE_ const LocalVector<int> &get_next_connected_nodes(Type p_type, int p_id) const {
212 return graph[p_type].nodes[p_id].next_connected_nodes;
213 }
214 _FORCE_INLINE_ const LocalVector<int> &get_prev_connected_nodes(Type p_type, int p_id) const {
215 return graph[p_type].nodes[p_id].prev_connected_nodes;
216 }
217
218 Vector<int> get_node_list(Type p_type) const;
219 int get_valid_node_id(Type p_type) const;
220
221 int find_node_id(Type p_type, const Ref<VisualShaderNode> &p_node) const;
222 void remove_node(Type p_type, int p_id);
223 void replace_node(Type p_type, int p_id, const StringName &p_new_class);
224
225 bool is_node_connection(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
226
227 bool is_nodes_connected_relatively(const Graph *p_graph, int p_node, int p_target) const;
228 bool can_connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
229 Error connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
230 void disconnect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
231 void connect_nodes_forced(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
232 bool is_port_types_compatible(int p_a, int p_b) const;
233
234 void attach_node_to_frame(Type p_type, int p_node, int p_frame);
235 void detach_node_from_frame(Type p_type, int p_node);
236
237 String get_reroute_parameter_name(Type p_type, int p_reroute_node) const;
238
239 void rebuild();
240 void get_node_connections(Type p_type, List<Connection> *r_connections) const;
241
242 void set_mode(Mode p_mode);
243 virtual Mode get_mode() const override;
244
245 virtual bool is_text_shader() const override;
246
247 #ifndef DISABLE_DEPRECATED
248 void set_graph_offset(const Vector2 &p_offset);
249 Vector2 get_graph_offset() const;
250 #endif
251
252 String generate_preview_shader(Type p_type, int p_node, int p_port, Vector<DefaultTextureParam> &r_default_tex_params) const;
253
254 String validate_port_name(const String &p_port_name, VisualShaderNode *p_node, int p_port_id, bool p_output) const;
255 String validate_parameter_name(const String &p_name, const Ref<VisualShaderNodeParameter> &p_parameter) const;
256
257 VisualShader();
258 };
259
260 VARIANT_ENUM_CAST(VisualShader::Type)
261 VARIANT_ENUM_CAST(VisualShader::VaryingMode)
262 VARIANT_ENUM_CAST(VisualShader::VaryingType)
263 ///
264 ///
265 ///
266
267 class VisualShaderNode : public Resource {
268 GDCLASS(VisualShaderNode, Resource);
269
270 public:
271 enum PortType {
272 PORT_TYPE_SCALAR,
273 PORT_TYPE_SCALAR_INT,
274 PORT_TYPE_SCALAR_UINT,
275 PORT_TYPE_VECTOR_2D,
276 PORT_TYPE_VECTOR_3D,
277 PORT_TYPE_VECTOR_4D,
278 PORT_TYPE_BOOLEAN,
279 PORT_TYPE_TRANSFORM,
280 PORT_TYPE_SAMPLER,
281 PORT_TYPE_MAX,
282 };
283
284 enum Category {
285 CATEGORY_NONE,
286 CATEGORY_OUTPUT,
287 CATEGORY_COLOR,
288 CATEGORY_CONDITIONAL,
289 CATEGORY_INPUT,
290 CATEGORY_SCALAR,
291 CATEGORY_TEXTURES,
292 CATEGORY_TRANSFORM,
293 CATEGORY_UTILITY,
294 CATEGORY_VECTOR,
295 CATEGORY_SPECIAL,
296 CATEGORY_PARTICLE,
297 CATEGORY_MAX
298 };
299
300 private:
301 int port_preview = -1;
302 int linked_parent_graph_frame = -1;
303
304 HashMap<int, bool> connected_input_ports;
305 HashMap<int, int> connected_output_ports;
306 HashMap<int, bool> expanded_output_ports;
307
308 protected:
309 HashMap<int, Variant> default_input_values;
310 bool simple_decl = true;
311 bool disabled = false;
312 bool closable = false;
313
314 static void _bind_methods();
315
316 public:
317 bool is_simple_decl() const;
318
319 virtual String get_caption() const = 0;
320
321 virtual int get_input_port_count() const = 0;
322 virtual PortType get_input_port_type(int p_port) const = 0;
323 virtual String get_input_port_name(int p_port) const = 0;
324 virtual int get_default_input_port(PortType p_type) const;
325
326 virtual void set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value = Variant());
327 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)
328 Array get_default_input_values() const;
329 virtual void set_default_input_values(const Array &p_values);
330 virtual void remove_input_port_default_value(int p_port);
331 virtual void clear_default_input_values();
332
333 virtual int get_output_port_count() const = 0;
334 virtual PortType get_output_port_type(int p_port) const = 0;
335 virtual String get_output_port_name(int p_port) const = 0;
336
337 virtual bool is_input_port_default(int p_port, Shader::Mode p_mode) const;
338
339 void set_output_port_for_preview(int p_index);
340 int get_output_port_for_preview() const;
341
342 virtual bool is_port_separator(int p_index) const;
343
344 bool is_output_port_connected(int p_port) const;
345 void set_output_port_connected(int p_port, bool p_connected);
346 bool is_input_port_connected(int p_port) const;
347 void set_input_port_connected(int p_port, bool p_connected);
348 bool is_any_port_connected() const;
349 virtual bool is_generate_input_var(int p_port) const;
350
351 virtual bool has_output_port_preview(int p_port) const;
352
353 virtual bool is_output_port_expandable(int p_port) const;
354 void _set_output_ports_expanded(const Array &p_data);
355 Array _get_output_ports_expanded() const;
356 void _set_output_port_expanded(int p_port, bool p_expanded);
357 bool _is_output_port_expanded(int p_port) const;
358 int get_expanded_output_port_count() const;
359
360 virtual bool is_code_generated() const;
361 virtual bool is_show_prop_names() const;
362 virtual bool is_use_prop_slots() const;
363
364 bool is_disabled() const;
365 void set_disabled(bool p_disabled = true);
366
367 bool is_deletable() const;
368 void set_deletable(bool p_closable = true);
369
370 void set_frame(int p_node);
371 int get_frame() const;
372
373 virtual Vector<StringName> get_editable_properties() const;
374 virtual HashMap<StringName, String> get_editable_properties_names() const;
375
376 virtual Vector<VisualShader::DefaultTextureParam> get_default_texture_parameters(VisualShader::Type p_type, int p_id) const;
377 virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const;
378 virtual String generate_global_per_node(Shader::Mode p_mode, int p_id) const;
379 virtual String generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const;
380 // 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.
381 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;
382
383 virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const;
384
385 virtual Category get_category() const;
386
387 VisualShaderNode();
388 };
389
390 VARIANT_ENUM_CAST(VisualShaderNode::PortType)
391
392 class VisualShaderNodeCustom : public VisualShaderNode {
393 GDCLASS(VisualShaderNodeCustom, VisualShaderNode);
394
395 struct Port {
396 String name;
397 int type = 0;
398 };
399
400 bool is_initialized = false;
401 List<Port> input_ports;
402 List<Port> output_ports;
403 struct Property {
404 String name;
405 };
406 struct DropDownListProperty : public Property {
407 Vector<String> options;
408 };
409 HashMap<int, int> dp_selected_cache;
410 HashMap<int, int> dp_default_cache;
411 List<DropDownListProperty> dp_props;
412 String properties;
413
414 friend class VisualShaderEditor;
415 friend class VisualShaderGraphPlugin;
416
417 protected:
418 virtual String get_caption() const override;
419
420 virtual int get_input_port_count() const override;
421 virtual PortType get_input_port_type(int p_port) const override;
422 virtual String get_input_port_name(int p_port) const override;
423 virtual int get_default_input_port(PortType p_type) const override;
424
425 virtual int get_output_port_count() const override;
426 virtual PortType get_output_port_type(int p_port) const override;
427 virtual String get_output_port_name(int p_port) const override;
428
429 virtual void set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value = Variant()) override;
430 virtual void set_default_input_values(const Array &p_values) override;
431 virtual void remove_input_port_default_value(int p_port) override;
432 virtual void clear_default_input_values() override;
433
434 GDVIRTUAL0RC(String, _get_name)
435 GDVIRTUAL0RC(String, _get_description)
436 GDVIRTUAL0RC(String, _get_category)
437 GDVIRTUAL0RC(PortType, _get_return_icon_type)
438 GDVIRTUAL0RC(int, _get_input_port_count)
439 GDVIRTUAL1RC(PortType, _get_input_port_type, int)
440 GDVIRTUAL1RC(String, _get_input_port_name, int)
441 GDVIRTUAL1RC(Variant, _get_input_port_default_value, int)
442 GDVIRTUAL1RC(int, _get_default_input_port, PortType)
443 GDVIRTUAL0RC(int, _get_output_port_count)
444 GDVIRTUAL1RC(PortType, _get_output_port_type, int)
445 GDVIRTUAL1RC(String, _get_output_port_name, int)
446 GDVIRTUAL0RC(int, _get_property_count)
447 GDVIRTUAL1RC(String, _get_property_name, int)
448 GDVIRTUAL1RC(int, _get_property_default_index, int)
449 GDVIRTUAL1RC(Vector<String>, _get_property_options, int)
450 GDVIRTUAL4RC(String, _get_code, TypedArray<String>, TypedArray<String>, Shader::Mode, VisualShader::Type)
451 GDVIRTUAL2RC(String, _get_func_code, Shader::Mode, VisualShader::Type)
452 GDVIRTUAL1RC(String, _get_global_code, Shader::Mode)
453 GDVIRTUAL0RC(bool, _is_highend)
454 GDVIRTUAL2RC(bool, _is_available, Shader::Mode, VisualShader::Type)
455
456 bool _is_valid_code(const String &p_code) const;
457
458 protected:
459 void _set_input_port_default_value(int p_port, const Variant &p_value);
460
461 bool is_available(Shader::Mode p_mode, VisualShader::Type p_type) const;
462 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;
463 virtual String generate_global_per_node(Shader::Mode p_mode, int p_id) const override;
464 virtual String generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const override;
465
466 static void _bind_methods();
467
468 public:
469 VisualShaderNodeCustom();
470 void update_property_default_values();
471 void update_input_port_default_values();
472 void update_ports();
473 void update_properties();
474
475 bool _is_initialized();
476 void _set_initialized(bool p_enabled);
477 void _set_properties(const String &p_properties);
478 String _get_properties() const;
479
480 String _get_name() const;
481 String _get_description() const;
482 String _get_category() const;
483 PortType _get_return_icon_type() const;
484 bool _is_highend() const;
485 void _set_option_index(int p_op, int p_index);
486
487 int get_option_index(int p_op) const;
488 };
489
490 /////
491
492 class VisualShaderNodeInput : public VisualShaderNode {
493 GDCLASS(VisualShaderNodeInput, VisualShaderNode);
494
495 friend class VisualShader;
496 VisualShader::Type shader_type = VisualShader::TYPE_MAX;
497 Shader::Mode shader_mode = Shader::MODE_MAX;
498
499 struct Port {
500 Shader::Mode mode = Shader::Mode::MODE_MAX;
501 VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX;
502 PortType type = PortType::PORT_TYPE_MAX;
503 const char *name;
504 const char *string;
505 };
506
507 static const Port ports[];
508 static const Port preview_ports[];
509
510 String input_name = "[None]";
511
512 public:
513 void set_shader_type(VisualShader::Type p_shader_type);
514 void set_shader_mode(Shader::Mode p_shader_mode);
515
516 protected:
517 static void _bind_methods();
518 void _validate_property(PropertyInfo &p_property) const;
519
520 public:
521 virtual int get_input_port_count() const override;
522 virtual PortType get_input_port_type(int p_port) const override;
523 virtual String get_input_port_name(int p_port) const override;
524
525 virtual int get_output_port_count() const override;
526 virtual PortType get_output_port_type(int p_port) const override;
527 virtual String get_output_port_name(int p_port) const override;
528 virtual bool is_output_port_expandable(int p_port) const override;
529
530 virtual String get_caption() const override;
531
532 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;
533
534 void set_input_name(String p_name);
535 String get_input_name() const;
536 String get_input_real_name() const;
537
538 int get_input_index_count() const;
539 PortType get_input_index_type(int p_index) const;
540 String get_input_index_name(int p_index) const;
541
542 PortType get_input_type_by_name(String p_name) const;
543
544 virtual Vector<StringName> get_editable_properties() const override;
545
546 virtual Category get_category() const override { return CATEGORY_INPUT; }
547
548 VisualShaderNodeInput();
549 };
550
551 ///
552
553 class VisualShaderNodeOutput : public VisualShaderNode {
554 GDCLASS(VisualShaderNodeOutput, VisualShaderNode);
555
556 public:
557 friend class VisualShader;
558 VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX;
559 Shader::Mode shader_mode = Shader::Mode::MODE_MAX;
560
561 struct Port {
562 Shader::Mode mode = Shader::Mode::MODE_MAX;
563 VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX;
564 PortType type = PortType::PORT_TYPE_MAX;
565 const char *name;
566 const char *string;
567 };
568
569 static const Port ports[];
570
571 public:
572 virtual int get_input_port_count() const override;
573 virtual PortType get_input_port_type(int p_port) const override;
574 virtual String get_input_port_name(int p_port) const override;
575 Variant get_input_port_default_value(int p_port) const;
576
577 virtual int get_output_port_count() const override;
578 virtual PortType get_output_port_type(int p_port) const override;
579 virtual String get_output_port_name(int p_port) const override;
580
581 virtual bool is_port_separator(int p_index) const override;
582
583 virtual String get_caption() const override;
584
585 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;
586
587 virtual Category get_category() const override { return CATEGORY_OUTPUT; }
588
589 VisualShaderNodeOutput();
590 };
591
592 class VisualShaderNodeParameter : public VisualShaderNode {
593 GDCLASS(VisualShaderNodeParameter, VisualShaderNode);
594
595 public:
596 enum Qualifier {
597 QUAL_NONE,
598 QUAL_GLOBAL,
599 QUAL_INSTANCE,
600 QUAL_INSTANCE_INDEX,
601 QUAL_MAX,
602 };
603
604 private:
605 String parameter_name = "";
606 Qualifier qualifier = QUAL_NONE;
607 bool global_code_generated = false;
608 int instance_index = 0;
609
610 protected:
611 static void _bind_methods();
612 String _get_qual_str() const;
613
614 #ifndef DISABLE_DEPRECATED
615 bool _set(const StringName &p_name, const Variant &p_value);
616 #endif
617
618 public:
619 void set_parameter_name(const String &p_name);
620 String get_parameter_name() const;
621
622 void set_qualifier(Qualifier p_qual);
623 Qualifier get_qualifier() const;
624
625 void set_global_code_generated(bool p_enabled);
626 bool is_global_code_generated() const;
627
628 void set_instance_index(int p_index);
629 int get_instance_index() const;
630
631 virtual bool is_qualifier_supported(Qualifier p_qual) const = 0;
632 virtual bool is_convertible_to_constant() const = 0;
633
634 virtual Vector<StringName> get_editable_properties() const override;
635 virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const override;
636
637 virtual Category get_category() const override { return CATEGORY_INPUT; }
638
639 VisualShaderNodeParameter();
640 };
641
642 VARIANT_ENUM_CAST(VisualShaderNodeParameter::Qualifier)
643
644 class VisualShaderNodeParameterRef : public VisualShaderNode {
645 GDCLASS(VisualShaderNodeParameterRef, VisualShaderNode);
646
647 public:
648 enum ParameterType {
649 PARAMETER_TYPE_FLOAT,
650 PARAMETER_TYPE_INT,
651 PARAMETER_TYPE_UINT,
652 PARAMETER_TYPE_BOOLEAN,
653 PARAMETER_TYPE_VECTOR2,
654 PARAMETER_TYPE_VECTOR3,
655 PARAMETER_TYPE_VECTOR4,
656 PARAMETER_TYPE_TRANSFORM,
657 PARAMETER_TYPE_COLOR,
658 UNIFORM_TYPE_SAMPLER,
659 };
660
661 struct Parameter {
662 String name;
663 ParameterType type;
664 };
665
666 private:
667 RID shader_rid;
668 String parameter_name = "[None]";
669 ParameterType param_type = ParameterType::PARAMETER_TYPE_FLOAT;
670
671 protected:
672 static void _bind_methods();
673
674 public:
675 static void add_parameter(RID p_shader_rid, const String &p_name, ParameterType p_type);
676 static void clear_parameters(RID p_shader_rid);
677 static bool has_parameter(RID p_shader_rid, const String &p_name);
678
679 public:
680 virtual String get_caption() const override;
681
682 virtual int get_input_port_count() const override;
683 virtual PortType get_input_port_type(int p_port) const override;
684 virtual String get_input_port_name(int p_port) const override;
685
686 virtual int get_output_port_count() const override;
687 virtual PortType get_output_port_type(int p_port) const override;
688 virtual String get_output_port_name(int p_port) const override;
689
690 bool is_shader_valid() const;
691 void set_shader_rid(const RID &p_shader);
692
693 void set_parameter_name(const String &p_name);
694 String get_parameter_name() const;
695
696 void update_parameter_type();
697
698 void _set_parameter_type(int p_parameter_type);
699 int _get_parameter_type() const;
700
701 int get_parameters_count() const;
702 String get_parameter_name_by_index(int p_idx) const;
703 ParameterType get_parameter_type_by_name(const String &p_name) const;
704 ParameterType get_parameter_type_by_index(int p_idx) const;
705 PortType get_port_type_by_index(int p_idx) const;
706
707 virtual Vector<StringName> get_editable_properties() const override;
708
709 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;
710
711 virtual Category get_category() const override { return CATEGORY_INPUT; }
712
713 VisualShaderNodeParameterRef();
714 };
715
716 class VisualShaderNodeResizableBase : public VisualShaderNode {
717 GDCLASS(VisualShaderNodeResizableBase, VisualShaderNode);
718
719 protected:
720 Size2 size = Size2(0, 0);
721 bool allow_v_resize = true;
722
723 protected:
724 static void _bind_methods();
725
726 public:
727 void set_size(const Size2 &p_size);
728 Size2 get_size() const;
729
730 bool is_allow_v_resize() const;
731 void set_allow_v_resize(bool p_enabled);
732
733 VisualShaderNodeResizableBase();
734 };
735
736 class VisualShaderNodeFrame : public VisualShaderNodeResizableBase {
737 GDCLASS(VisualShaderNodeFrame, VisualShaderNodeResizableBase);
738
739 protected:
740 String title = "Title";
741 bool tint_color_enabled = false;
742 Color tint_color = Color(0.3, 0.3, 0.3, 0.75);
743 bool autoshrink = true;
744 HashSet<int> attached_nodes;
745
746 protected:
747 static void _bind_methods();
748
749 public:
750 virtual String get_caption() const override;
751
752 virtual int get_input_port_count() const override;
753 virtual PortType get_input_port_type(int p_port) const override;
754 virtual String get_input_port_name(int p_port) const override;
755
756 virtual int get_output_port_count() const override;
757 virtual PortType get_output_port_type(int p_port) const override;
758 virtual String get_output_port_name(int p_port) const override;
759
760 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;
761
762 void set_title(const String &p_title);
763 String get_title() const;
764
765 void set_tint_color_enabled(bool p_enable);
766 bool is_tint_color_enabled() const;
767
768 void set_tint_color(const Color &p_color);
769 Color get_tint_color() const;
770
771 void set_autoshrink_enabled(bool p_enable);
772 bool is_autoshrink_enabled() const;
773
774 void add_attached_node(int p_node);
775 void remove_attached_node(int p_node);
776 void set_attached_nodes(const PackedInt32Array &p_nodes);
777 PackedInt32Array get_attached_nodes() const;
778
779 virtual Category get_category() const override { return CATEGORY_NONE; }
780
781 VisualShaderNodeFrame();
782 };
783
784 #ifndef DISABLE_DEPRECATED
785 // Deprecated, for compatibility only.
786 class VisualShaderNodeComment : public VisualShaderNodeFrame {
787 GDCLASS(VisualShaderNodeComment, VisualShaderNodeFrame);
788
789 String description;
790
791 protected:
792 static void _bind_methods();
793
794 public:
795 virtual String get_caption() const override { return "Comment(Deprecated)"; }
796
797 virtual Category get_category() const override { return CATEGORY_NONE; }
798
799 void set_description(const String &p_description);
800 String get_description() const;
801 };
802 #endif
803
804 class VisualShaderNodeGroupBase : public VisualShaderNodeResizableBase {
805 GDCLASS(VisualShaderNodeGroupBase, VisualShaderNodeResizableBase);
806
807 private:
808 void _apply_port_changes();
809
810 protected:
811 String inputs = "";
812 String outputs = "";
813 bool editable = false;
814
815 struct Port {
816 PortType type = PortType::PORT_TYPE_MAX;
817 String name;
818 };
819
820 HashMap<int, Port> input_ports;
821 HashMap<int, Port> output_ports;
822 HashMap<int, Control *> controls;
823
824 protected:
825 static void _bind_methods();
826
827 public:
828 void set_inputs(const String &p_inputs);
829 String get_inputs() const;
830
831 void set_outputs(const String &p_outputs);
832 String get_outputs() const;
833
834 bool is_valid_port_name(const String &p_name) const;
835
836 void add_input_port(int p_id, int p_type, const String &p_name);
837 void remove_input_port(int p_id);
838 virtual int get_input_port_count() const override;
839 bool has_input_port(int p_id) const;
840 void clear_input_ports();
841
842 void add_output_port(int p_id, int p_type, const String &p_name);
843 void remove_output_port(int p_id);
844 virtual int get_output_port_count() const override;
845 bool has_output_port(int p_id) const;
846 void clear_output_ports();
847
848 void set_input_port_type(int p_id, int p_type);
849 virtual PortType get_input_port_type(int p_id) const override;
850 void set_input_port_name(int p_id, const String &p_name);
851 virtual String get_input_port_name(int p_id) const override;
852
853 void set_output_port_type(int p_id, int p_type);
854 virtual PortType get_output_port_type(int p_id) const override;
855 void set_output_port_name(int p_id, const String &p_name);
856 virtual String get_output_port_name(int p_id) const override;
857
858 int get_free_input_port_id() const;
859 int get_free_output_port_id() const;
860
861 void set_ctrl_pressed(Control *p_control, int p_index);
862 Control *is_ctrl_pressed(int p_index);
863
864 void set_editable(bool p_enabled);
865 bool is_editable() const;
866
867 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;
868
869 virtual Category get_category() const override { return CATEGORY_SPECIAL; }
870
871 VisualShaderNodeGroupBase();
872 };
873
874 class VisualShaderNodeExpression : public VisualShaderNodeGroupBase {
875 GDCLASS(VisualShaderNodeExpression, VisualShaderNodeGroupBase);
876
877 private:
878 bool _is_valid_identifier_char(char32_t p_c) const;
879 String _replace_port_names(const Vector<Pair<String, String>> &p_pairs, const String &p_expression) const;
880
881 protected:
882 String expression = "";
883
884 static void _bind_methods();
885
886 public:
887 virtual String get_caption() const override;
888
889 void set_expression(const String &p_expression);
890 String get_expression() const;
891
892 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;
893 virtual bool is_output_port_expandable(int p_port) const override;
894
895 VisualShaderNodeExpression();
896 };
897
898 class VisualShaderNodeGlobalExpression : public VisualShaderNodeExpression {
899 GDCLASS(VisualShaderNodeGlobalExpression, VisualShaderNodeExpression);
900
901 public:
902 virtual String get_caption() const override;
903
904 virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const override;
905
906 VisualShaderNodeGlobalExpression();
907 };
908
909 class VisualShaderNodeVarying : public VisualShaderNode {
910 GDCLASS(VisualShaderNodeVarying, VisualShaderNode);
911
912 public:
913 struct Varying {
914 String name;
915 VisualShader::VaryingMode mode;
916 VisualShader::VaryingType type;
917 bool assigned = false;
918 };
919
920 protected:
921 RID shader_rid;
922 VisualShader::VaryingType varying_type = VisualShader::VARYING_TYPE_FLOAT;
923 String varying_name = "[None]";
924
925 public: // internal
926 static void add_varying(RID p_shader_rid, const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type);
927 static void clear_varyings(RID p_shader_rid);
928 static bool has_varying(RID p_shader_rid, const String &p_name);
929
930 void set_shader_rid(const RID &p_shader);
931
932 int get_varyings_count() const;
933 String get_varying_name_by_index(int p_idx) const;
934 VisualShader::VaryingMode get_varying_mode_by_name(const String &p_name) const;
935 VisualShader::VaryingMode get_varying_mode_by_index(int p_idx) const;
936 VisualShader::VaryingType get_varying_type_by_name(const String &p_name) const;
937 VisualShader::VaryingType get_varying_type_by_index(int p_idx) const;
938 PortType get_port_type_by_index(int p_idx) const;
939
940 protected:
941 static void _bind_methods();
942
943 protected:
944 String get_type_str() const;
945 PortType get_port_type(VisualShader::VaryingType p_type, int p_port) const;
946
947 public:
948 virtual String get_caption() const override = 0;
949
950 virtual int get_input_port_count() const override = 0;
951 virtual PortType get_input_port_type(int p_port) const override = 0;
952 virtual String get_input_port_name(int p_port) const override = 0;
953
954 virtual int get_output_port_count() const override = 0;
955 virtual PortType get_output_port_type(int p_port) const override = 0;
956 virtual String get_output_port_name(int p_port) const override = 0;
957
958 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 = 0;
959
960 void set_varying_name(String p_varying_name);
961 String get_varying_name() const;
962
963 void set_varying_type(VisualShader::VaryingType p_varying_type);
964 VisualShader::VaryingType get_varying_type() const;
965
966 VisualShaderNodeVarying();
967 };
968
969 class VisualShaderNodeVaryingSetter : public VisualShaderNodeVarying {
970 GDCLASS(VisualShaderNodeVaryingSetter, VisualShaderNodeVarying);
971
972 public:
973 virtual String get_caption() const override;
974
975 virtual int get_input_port_count() const override;
976 virtual PortType get_input_port_type(int p_port) const override;
977 virtual String get_input_port_name(int p_port) const override;
978
979 virtual int get_output_port_count() const override;
980 virtual PortType get_output_port_type(int p_port) const override;
981 virtual String get_output_port_name(int p_port) const override;
982
983 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;
984
985 virtual Category get_category() const override { return CATEGORY_OUTPUT; }
986
987 VisualShaderNodeVaryingSetter();
988 };
989
990 class VisualShaderNodeVaryingGetter : public VisualShaderNodeVarying {
991 GDCLASS(VisualShaderNodeVaryingGetter, VisualShaderNodeVarying);
992
993 public:
994 virtual String get_caption() const override;
995
996 virtual int get_input_port_count() const override;
997 virtual PortType get_input_port_type(int p_port) const override;
998 virtual String get_input_port_name(int p_port) const override;
999
1000 virtual int get_output_port_count() const override;
1001 virtual PortType get_output_port_type(int p_port) const override;
1002 virtual String get_output_port_name(int p_port) const override;
1003 virtual bool has_output_port_preview(int p_port) const override;
1004
1005 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;
1006
1007 virtual Category get_category() const override { return CATEGORY_INPUT; }
1008
1009 VisualShaderNodeVaryingGetter();
1010 };
1011
1012 extern String make_unique_id(VisualShader::Type p_type, int p_id, const String &p_name);