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#ifndef VISUAL_SHADER_H
32#define VISUAL_SHADER_H
33
34#include "core/string/string_builder.h"
35#include "core/templates/safe_refcount.h"
36#include "scene/gui/control.h"
37#include "scene/resources/shader.h"
38
39class VisualShaderNodeParameter;
40class VisualShaderNode;
41
42class VisualShader : public Shader {
43 GDCLASS(VisualShader, Shader);
44
45 friend class VisualShaderNodeVersionChecker;
46
47public:
48 enum Type {
49 TYPE_VERTEX,
50 TYPE_FRAGMENT,
51 TYPE_LIGHT,
52 TYPE_START,
53 TYPE_PROCESS,
54 TYPE_COLLIDE,
55 TYPE_START_CUSTOM,
56 TYPE_PROCESS_CUSTOM,
57 TYPE_SKY,
58 TYPE_FOG,
59 TYPE_MAX
60 };
61
62 struct Connection {
63 int from_node = 0;
64 int from_port = 0;
65 int to_node = 0;
66 int to_port = 0;
67 };
68
69 struct DefaultTextureParam {
70 StringName name;
71 List<Ref<Texture2D>> params;
72 };
73
74 enum VaryingMode {
75 VARYING_MODE_VERTEX_TO_FRAG_LIGHT,
76 VARYING_MODE_FRAG_TO_LIGHT,
77 VARYING_MODE_MAX,
78 };
79
80 enum VaryingType {
81 VARYING_TYPE_FLOAT,
82 VARYING_TYPE_INT,
83 VARYING_TYPE_UINT,
84 VARYING_TYPE_VECTOR_2D,
85 VARYING_TYPE_VECTOR_3D,
86 VARYING_TYPE_VECTOR_4D,
87 VARYING_TYPE_BOOLEAN,
88 VARYING_TYPE_TRANSFORM,
89 VARYING_TYPE_MAX,
90 };
91
92 struct Varying {
93 String name;
94 VaryingMode mode = VARYING_MODE_MAX;
95 VaryingType type = VARYING_TYPE_MAX;
96
97 Varying() {}
98
99 Varying(String p_name, VaryingMode p_mode, VaryingType p_type) :
100 name(p_name), mode(p_mode), type(p_type) {}
101
102 bool from_string(const String &p_str) {
103 Vector<String> arr = p_str.split(",");
104 if (arr.size() != 2) {
105 return false;
106 }
107
108 mode = (VaryingMode)arr[0].to_int();
109 type = (VaryingType)arr[1].to_int();
110
111 return true;
112 }
113
114 String to_string() const {
115 return vformat("%s,%s", itos((int)mode), itos((int)type));
116 }
117 };
118
119private:
120 Type current_type;
121
122 struct Node {
123 Ref<VisualShaderNode> node;
124 Vector2 position;
125 LocalVector<int> prev_connected_nodes;
126 LocalVector<int> next_connected_nodes;
127 };
128
129 struct Graph {
130 RBMap<int, Node> nodes;
131 List<Connection> connections;
132 } graph[TYPE_MAX];
133
134 Shader::Mode shader_mode = Shader::MODE_SPATIAL;
135 mutable String previous_code;
136
137 TypedArray<Dictionary> _get_node_connections(Type p_type) const;
138
139 Vector2 graph_offset;
140
141 HashMap<String, int> modes;
142 HashSet<StringName> flags;
143
144 HashMap<String, Varying> varyings;
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 bool operator<(const ConnectionKey &p_key) const {
157 return key < p_key.key;
158 }
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 VMap<ConnectionKey, const List<Connection>::Element *> &p_input_connections, const VMap<ConnectionKey, const List<Connection>::Element *> &p_output_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
166protected:
167 virtual void _update_shader() const override;
168 static void _bind_methods();
169
170 bool _set(const StringName &p_name, const Variant &p_value);
171 bool _get(const StringName &p_name, Variant &r_ret) const;
172 void _get_property_list(List<PropertyInfo> *p_list) const;
173
174 virtual void reset_state() override;
175
176public: // internal methods
177 void set_shader_type(Type p_type);
178 Type get_shader_type() const;
179
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
188 void add_varying(const String &p_name, VaryingMode p_mode, VaryingType p_type);
189 void remove_varying(const String &p_name);
190 bool has_varying(const String &p_name) const;
191 int get_varyings_count() const;
192 const Varying *get_varying_by_index(int p_idx) const;
193
194 void set_varying_mode(const String &p_name, VaryingMode p_mode);
195 VaryingMode get_varying_mode(const String &p_name);
196
197 void set_varying_type(const String &p_name, VaryingType p_type);
198 VaryingType get_varying_type(const String &p_name);
199
200 Vector2 get_node_position(Type p_type, int p_id) const;
201 Ref<VisualShaderNode> get_node(Type p_type, int p_id) const;
202
203 _FORCE_INLINE_ Ref<VisualShaderNode> get_node_unchecked(Type p_type, int p_id) const {
204 return graph[p_type].nodes[p_id].node;
205 }
206 _FORCE_INLINE_ const LocalVector<int> &get_next_connected_nodes(Type p_type, int p_id) const {
207 return graph[p_type].nodes[p_id].next_connected_nodes;
208 }
209 _FORCE_INLINE_ const LocalVector<int> &get_prev_connected_nodes(Type p_type, int p_id) const {
210 return graph[p_type].nodes[p_id].prev_connected_nodes;
211 }
212
213 Vector<int> get_node_list(Type p_type) const;
214 int get_valid_node_id(Type p_type) const;
215
216 int find_node_id(Type p_type, const Ref<VisualShaderNode> &p_node) const;
217 void remove_node(Type p_type, int p_id);
218 void replace_node(Type p_type, int p_id, const StringName &p_new_class);
219
220 bool is_node_connection(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
221
222 bool is_nodes_connected_relatively(const Graph *p_graph, int p_node, int p_target) const;
223 bool can_connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
224 Error connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
225 void disconnect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
226 void connect_nodes_forced(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
227 bool is_port_types_compatible(int p_a, int p_b) const;
228
229 void rebuild();
230 void get_node_connections(Type p_type, List<Connection> *r_connections) const;
231
232 void set_mode(Mode p_mode);
233 virtual Mode get_mode() const override;
234
235 virtual bool is_text_shader() const override;
236
237 void set_graph_offset(const Vector2 &p_offset);
238 Vector2 get_graph_offset() const;
239
240 String generate_preview_shader(Type p_type, int p_node, int p_port, Vector<DefaultTextureParam> &r_default_tex_params) const;
241
242 String validate_port_name(const String &p_port_name, VisualShaderNode *p_node, int p_port_id, bool p_output) const;
243 String validate_parameter_name(const String &p_name, const Ref<VisualShaderNodeParameter> &p_parameter) const;
244
245 VisualShader();
246};
247
248VARIANT_ENUM_CAST(VisualShader::Type)
249VARIANT_ENUM_CAST(VisualShader::VaryingMode)
250VARIANT_ENUM_CAST(VisualShader::VaryingType)
251///
252///
253///
254
255class VisualShaderNode : public Resource {
256 GDCLASS(VisualShaderNode, Resource);
257
258 int port_preview = -1;
259
260 HashMap<int, Variant> default_input_values;
261 HashMap<int, bool> connected_input_ports;
262 HashMap<int, int> connected_output_ports;
263 HashMap<int, bool> expanded_output_ports;
264
265protected:
266 bool simple_decl = true;
267 bool disabled = false;
268 bool closable = false;
269
270 static void _bind_methods();
271
272public:
273 enum PortType {
274 PORT_TYPE_SCALAR,
275 PORT_TYPE_SCALAR_INT,
276 PORT_TYPE_SCALAR_UINT,
277 PORT_TYPE_VECTOR_2D,
278 PORT_TYPE_VECTOR_3D,
279 PORT_TYPE_VECTOR_4D,
280 PORT_TYPE_BOOLEAN,
281 PORT_TYPE_TRANSFORM,
282 PORT_TYPE_SAMPLER,
283 PORT_TYPE_MAX,
284 };
285
286 bool is_simple_decl() const;
287
288 virtual String get_caption() const = 0;
289
290 virtual int get_input_port_count() const = 0;
291 virtual PortType get_input_port_type(int p_port) const = 0;
292 virtual String get_input_port_name(int p_port) const = 0;
293 virtual int get_default_input_port(PortType p_type) const;
294
295 virtual void set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value = Variant());
296 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)
297 Array get_default_input_values() const;
298 virtual void set_default_input_values(const Array &p_values);
299 virtual void remove_input_port_default_value(int p_port);
300 virtual void clear_default_input_values();
301
302 virtual int get_output_port_count() const = 0;
303 virtual PortType get_output_port_type(int p_port) const = 0;
304 virtual String get_output_port_name(int p_port) const = 0;
305
306 virtual bool is_input_port_default(int p_port, Shader::Mode p_mode) const;
307
308 void set_output_port_for_preview(int p_index);
309 int get_output_port_for_preview() const;
310
311 virtual bool is_port_separator(int p_index) const;
312
313 bool is_output_port_connected(int p_port) const;
314 void set_output_port_connected(int p_port, bool p_connected);
315 bool is_input_port_connected(int p_port) const;
316 void set_input_port_connected(int p_port, bool p_connected);
317 virtual bool is_generate_input_var(int p_port) const;
318
319 virtual bool has_output_port_preview(int p_port) const;
320
321 virtual bool is_output_port_expandable(int p_port) const;
322 void _set_output_ports_expanded(const Array &p_data);
323 Array _get_output_ports_expanded() const;
324 void _set_output_port_expanded(int p_port, bool p_expanded);
325 bool _is_output_port_expanded(int p_port) const;
326 int get_expanded_output_port_count() const;
327
328 virtual bool is_code_generated() const;
329 virtual bool is_show_prop_names() const;
330 virtual bool is_use_prop_slots() const;
331
332 bool is_disabled() const;
333 void set_disabled(bool p_disabled = true);
334
335 bool is_closable() const;
336 void set_closable(bool p_closable = true);
337
338 virtual Vector<StringName> get_editable_properties() const;
339 virtual HashMap<StringName, String> get_editable_properties_names() const;
340
341 virtual Vector<VisualShader::DefaultTextureParam> get_default_texture_parameters(VisualShader::Type p_type, int p_id) const;
342 virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const;
343 virtual String generate_global_per_node(Shader::Mode p_mode, int p_id) const;
344 virtual String generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const;
345 // 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.
346 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;
347
348 virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const;
349
350 VisualShaderNode();
351};
352
353VARIANT_ENUM_CAST(VisualShaderNode::PortType)
354
355class VisualShaderNodeCustom : public VisualShaderNode {
356 GDCLASS(VisualShaderNodeCustom, VisualShaderNode);
357
358 struct Port {
359 String name;
360 int type = 0;
361 };
362
363 bool is_initialized = false;
364 List<Port> input_ports;
365 List<Port> output_ports;
366
367 friend class VisualShaderEditor;
368
369protected:
370 virtual String get_caption() const override;
371
372 virtual int get_input_port_count() const override;
373 virtual PortType get_input_port_type(int p_port) const override;
374 virtual String get_input_port_name(int p_port) const override;
375 virtual int get_default_input_port(PortType p_type) const override;
376
377 virtual int get_output_port_count() const override;
378 virtual PortType get_output_port_type(int p_port) const override;
379 virtual String get_output_port_name(int p_port) const override;
380
381 virtual void set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value = Variant()) override;
382 virtual void set_default_input_values(const Array &p_values) override;
383 virtual void remove_input_port_default_value(int p_port) override;
384 virtual void clear_default_input_values() override;
385
386 GDVIRTUAL0RC(String, _get_name)
387 GDVIRTUAL0RC(String, _get_description)
388 GDVIRTUAL0RC(String, _get_category)
389 GDVIRTUAL0RC(PortType, _get_return_icon_type)
390 GDVIRTUAL0RC(int, _get_input_port_count)
391 GDVIRTUAL1RC(PortType, _get_input_port_type, int)
392 GDVIRTUAL1RC(String, _get_input_port_name, int)
393 GDVIRTUAL1RC(int, _get_default_input_port, PortType)
394 GDVIRTUAL0RC(int, _get_output_port_count)
395 GDVIRTUAL1RC(PortType, _get_output_port_type, int)
396 GDVIRTUAL1RC(String, _get_output_port_name, int)
397 GDVIRTUAL4RC(String, _get_code, TypedArray<String>, TypedArray<String>, Shader::Mode, VisualShader::Type)
398 GDVIRTUAL2RC(String, _get_func_code, Shader::Mode, VisualShader::Type)
399 GDVIRTUAL1RC(String, _get_global_code, Shader::Mode)
400 GDVIRTUAL0RC(bool, _is_highend)
401 GDVIRTUAL2RC(bool, _is_available, Shader::Mode, VisualShader::Type)
402
403 bool _is_valid_code(const String &p_code) const;
404
405protected:
406 void _set_input_port_default_value(int p_port, const Variant &p_value);
407
408 bool is_available(Shader::Mode p_mode, VisualShader::Type p_type) const;
409 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;
410 virtual String generate_global_per_node(Shader::Mode p_mode, int p_id) const override;
411 virtual String generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const override;
412
413 static void _bind_methods();
414
415public:
416 VisualShaderNodeCustom();
417 void update_ports();
418
419 bool _is_initialized();
420 void _set_initialized(bool p_enabled);
421
422 String _get_name() const;
423 String _get_description() const;
424 String _get_category() const;
425 PortType _get_return_icon_type() const;
426 bool _is_highend() const;
427};
428
429/////
430
431class VisualShaderNodeInput : public VisualShaderNode {
432 GDCLASS(VisualShaderNodeInput, VisualShaderNode);
433
434 friend class VisualShader;
435 VisualShader::Type shader_type = VisualShader::TYPE_MAX;
436 Shader::Mode shader_mode = Shader::MODE_MAX;
437
438 struct Port {
439 Shader::Mode mode = Shader::Mode::MODE_MAX;
440 VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX;
441 PortType type = PortType::PORT_TYPE_MAX;
442 const char *name;
443 const char *string;
444 };
445
446 static const Port ports[];
447 static const Port preview_ports[];
448
449 String input_name = "[None]";
450
451public:
452 void set_shader_type(VisualShader::Type p_shader_type);
453 void set_shader_mode(Shader::Mode p_shader_mode);
454
455protected:
456 static void _bind_methods();
457 void _validate_property(PropertyInfo &p_property) const;
458
459public:
460 virtual int get_input_port_count() const override;
461 virtual PortType get_input_port_type(int p_port) const override;
462 virtual String get_input_port_name(int p_port) const override;
463
464 virtual int get_output_port_count() const override;
465 virtual PortType get_output_port_type(int p_port) const override;
466 virtual String get_output_port_name(int p_port) const override;
467 virtual bool is_output_port_expandable(int p_port) const override;
468
469 virtual String get_caption() const override;
470
471 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;
472
473 void set_input_name(String p_name);
474 String get_input_name() const;
475 String get_input_real_name() const;
476
477 int get_input_index_count() const;
478 PortType get_input_index_type(int p_index) const;
479 String get_input_index_name(int p_index) const;
480
481 PortType get_input_type_by_name(String p_name) const;
482
483 virtual Vector<StringName> get_editable_properties() const override;
484
485 VisualShaderNodeInput();
486};
487
488///
489
490class VisualShaderNodeOutput : public VisualShaderNode {
491 GDCLASS(VisualShaderNodeOutput, VisualShaderNode);
492
493public:
494 friend class VisualShader;
495 VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX;
496 Shader::Mode shader_mode = Shader::Mode::MODE_MAX;
497
498 struct Port {
499 Shader::Mode mode = Shader::Mode::MODE_MAX;
500 VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX;
501 PortType type = PortType::PORT_TYPE_MAX;
502 const char *name;
503 const char *string;
504 };
505
506 static const Port ports[];
507
508public:
509 virtual int get_input_port_count() const override;
510 virtual PortType get_input_port_type(int p_port) const override;
511 virtual String get_input_port_name(int p_port) const override;
512 Variant get_input_port_default_value(int p_port) const;
513
514 virtual int get_output_port_count() const override;
515 virtual PortType get_output_port_type(int p_port) const override;
516 virtual String get_output_port_name(int p_port) const override;
517
518 virtual bool is_port_separator(int p_index) const override;
519
520 virtual String get_caption() const override;
521
522 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;
523
524 VisualShaderNodeOutput();
525};
526
527class VisualShaderNodeParameter : public VisualShaderNode {
528 GDCLASS(VisualShaderNodeParameter, VisualShaderNode);
529
530public:
531 enum Qualifier {
532 QUAL_NONE,
533 QUAL_GLOBAL,
534 QUAL_INSTANCE,
535 QUAL_MAX,
536 };
537
538private:
539 String parameter_name = "";
540 Qualifier qualifier = QUAL_NONE;
541 bool global_code_generated = false;
542
543protected:
544 static void _bind_methods();
545 String _get_qual_str() const;
546
547#ifndef DISABLE_DEPRECATED
548 bool _set(const StringName &p_name, const Variant &p_value);
549#endif
550
551public:
552 void set_parameter_name(const String &p_name);
553 String get_parameter_name() const;
554
555 void set_qualifier(Qualifier p_qual);
556 Qualifier get_qualifier() const;
557
558 void set_global_code_generated(bool p_enabled);
559 bool is_global_code_generated() const;
560
561 virtual bool is_qualifier_supported(Qualifier p_qual) const = 0;
562 virtual bool is_convertible_to_constant() const = 0;
563
564 virtual Vector<StringName> get_editable_properties() const override;
565 virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const override;
566
567 VisualShaderNodeParameter();
568};
569
570VARIANT_ENUM_CAST(VisualShaderNodeParameter::Qualifier)
571
572class VisualShaderNodeParameterRef : public VisualShaderNode {
573 GDCLASS(VisualShaderNodeParameterRef, VisualShaderNode);
574
575public:
576 enum ParameterType {
577 PARAMETER_TYPE_FLOAT,
578 PARAMETER_TYPE_INT,
579 PARAMETER_TYPE_UINT,
580 PARAMETER_TYPE_BOOLEAN,
581 PARAMETER_TYPE_VECTOR2,
582 PARAMETER_TYPE_VECTOR3,
583 PARAMETER_TYPE_VECTOR4,
584 PARAMETER_TYPE_TRANSFORM,
585 PARAMETER_TYPE_COLOR,
586 UNIFORM_TYPE_SAMPLER,
587 };
588
589 struct Parameter {
590 String name;
591 ParameterType type;
592 };
593
594private:
595 RID shader_rid;
596 String parameter_name = "[None]";
597 ParameterType param_type = ParameterType::PARAMETER_TYPE_FLOAT;
598
599protected:
600 static void _bind_methods();
601
602public:
603 static void add_parameter(RID p_shader_rid, const String &p_name, ParameterType p_type);
604 static void clear_parameters(RID p_shader_rid);
605 static bool has_parameter(RID p_shader_rid, const String &p_name);
606
607public:
608 virtual String get_caption() const override;
609
610 virtual int get_input_port_count() const override;
611 virtual PortType get_input_port_type(int p_port) const override;
612 virtual String get_input_port_name(int p_port) const override;
613
614 virtual int get_output_port_count() const override;
615 virtual PortType get_output_port_type(int p_port) const override;
616 virtual String get_output_port_name(int p_port) const override;
617
618 void set_shader_rid(const RID &p_shader);
619
620 void set_parameter_name(const String &p_name);
621 String get_parameter_name() const;
622
623 void update_parameter_type();
624
625 void _set_parameter_type(int p_parameter_type);
626 int _get_parameter_type() const;
627
628 int get_parameters_count() const;
629 String get_parameter_name_by_index(int p_idx) const;
630 ParameterType get_parameter_type_by_name(const String &p_name) const;
631 ParameterType get_parameter_type_by_index(int p_idx) const;
632 PortType get_port_type_by_index(int p_idx) const;
633
634 virtual Vector<StringName> get_editable_properties() const override;
635
636 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;
637
638 VisualShaderNodeParameterRef();
639};
640
641class VisualShaderNodeResizableBase : public VisualShaderNode {
642 GDCLASS(VisualShaderNodeResizableBase, VisualShaderNode);
643
644protected:
645 Size2 size = Size2(0, 0);
646 bool allow_v_resize = true;
647
648protected:
649 static void _bind_methods();
650
651public:
652 void set_size(const Size2 &p_size);
653 Size2 get_size() const;
654
655 bool is_allow_v_resize() const;
656 void set_allow_v_resize(bool p_enabled);
657
658 VisualShaderNodeResizableBase();
659};
660
661class VisualShaderNodeComment : public VisualShaderNodeResizableBase {
662 GDCLASS(VisualShaderNodeComment, VisualShaderNodeResizableBase);
663
664protected:
665 String title = "Comment";
666 String description = "";
667
668protected:
669 static void _bind_methods();
670
671public:
672 virtual String get_caption() const override;
673
674 virtual int get_input_port_count() const override;
675 virtual PortType get_input_port_type(int p_port) const override;
676 virtual String get_input_port_name(int p_port) const override;
677
678 virtual int get_output_port_count() const override;
679 virtual PortType get_output_port_type(int p_port) const override;
680 virtual String get_output_port_name(int p_port) const override;
681
682 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;
683
684 void set_title(const String &p_title);
685 String get_title() const;
686
687 void set_description(const String &p_description);
688 String get_description() const;
689
690 VisualShaderNodeComment();
691};
692
693class VisualShaderNodeGroupBase : public VisualShaderNodeResizableBase {
694 GDCLASS(VisualShaderNodeGroupBase, VisualShaderNodeResizableBase);
695
696private:
697 void _apply_port_changes();
698
699protected:
700 String inputs = "";
701 String outputs = "";
702 bool editable = false;
703
704 struct Port {
705 PortType type = PortType::PORT_TYPE_MAX;
706 String name;
707 };
708
709 HashMap<int, Port> input_ports;
710 HashMap<int, Port> output_ports;
711 HashMap<int, Control *> controls;
712
713protected:
714 static void _bind_methods();
715
716public:
717 void set_inputs(const String &p_inputs);
718 String get_inputs() const;
719
720 void set_outputs(const String &p_outputs);
721 String get_outputs() const;
722
723 bool is_valid_port_name(const String &p_name) const;
724
725 void add_input_port(int p_id, int p_type, const String &p_name);
726 void remove_input_port(int p_id);
727 virtual int get_input_port_count() const override;
728 bool has_input_port(int p_id) const;
729 void clear_input_ports();
730
731 void add_output_port(int p_id, int p_type, const String &p_name);
732 void remove_output_port(int p_id);
733 virtual int get_output_port_count() const override;
734 bool has_output_port(int p_id) const;
735 void clear_output_ports();
736
737 void set_input_port_type(int p_id, int p_type);
738 virtual PortType get_input_port_type(int p_id) const override;
739 void set_input_port_name(int p_id, const String &p_name);
740 virtual String get_input_port_name(int p_id) const override;
741
742 void set_output_port_type(int p_id, int p_type);
743 virtual PortType get_output_port_type(int p_id) const override;
744 void set_output_port_name(int p_id, const String &p_name);
745 virtual String get_output_port_name(int p_id) const override;
746
747 int get_free_input_port_id() const;
748 int get_free_output_port_id() const;
749
750 void set_ctrl_pressed(Control *p_control, int p_index);
751 Control *is_ctrl_pressed(int p_index);
752
753 void set_editable(bool p_enabled);
754 bool is_editable() const;
755
756 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;
757
758 VisualShaderNodeGroupBase();
759};
760
761class VisualShaderNodeExpression : public VisualShaderNodeGroupBase {
762 GDCLASS(VisualShaderNodeExpression, VisualShaderNodeGroupBase);
763
764protected:
765 String expression = "";
766
767 static void _bind_methods();
768
769public:
770 virtual String get_caption() const override;
771
772 void set_expression(const String &p_expression);
773 String get_expression() const;
774
775 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;
776
777 VisualShaderNodeExpression();
778};
779
780class VisualShaderNodeGlobalExpression : public VisualShaderNodeExpression {
781 GDCLASS(VisualShaderNodeGlobalExpression, VisualShaderNodeExpression);
782
783public:
784 virtual String get_caption() const override;
785
786 virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const override;
787
788 VisualShaderNodeGlobalExpression();
789};
790
791class VisualShaderNodeVarying : public VisualShaderNode {
792 GDCLASS(VisualShaderNodeVarying, VisualShaderNode);
793
794public:
795 struct Varying {
796 String name;
797 VisualShader::VaryingMode mode;
798 VisualShader::VaryingType type;
799 bool assigned = false;
800 };
801
802protected:
803 VisualShader::VaryingType varying_type = VisualShader::VARYING_TYPE_FLOAT;
804 String varying_name = "[None]";
805
806public: // internal
807 static void add_varying(const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type);
808 static void clear_varyings();
809 static bool has_varying(const String &p_name);
810
811 int get_varyings_count() const;
812 String get_varying_name_by_index(int p_idx) const;
813 VisualShader::VaryingMode get_varying_mode_by_name(const String &p_name) const;
814 VisualShader::VaryingMode get_varying_mode_by_index(int p_idx) const;
815 VisualShader::VaryingType get_varying_type_by_name(const String &p_name) const;
816 VisualShader::VaryingType get_varying_type_by_index(int p_idx) const;
817 PortType get_port_type_by_index(int p_idx) const;
818
819protected:
820 static void _bind_methods();
821
822protected:
823 String get_type_str() const;
824 PortType get_port_type(VisualShader::VaryingType p_type, int p_port) const;
825
826public:
827 virtual String get_caption() const override = 0;
828
829 virtual int get_input_port_count() const override = 0;
830 virtual PortType get_input_port_type(int p_port) const override = 0;
831 virtual String get_input_port_name(int p_port) const override = 0;
832
833 virtual int get_output_port_count() const override = 0;
834 virtual PortType get_output_port_type(int p_port) const override = 0;
835 virtual String get_output_port_name(int p_port) const override = 0;
836
837 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;
838
839 void set_varying_name(String p_varying_name);
840 String get_varying_name() const;
841
842 void set_varying_type(VisualShader::VaryingType p_varying_type);
843 VisualShader::VaryingType get_varying_type() const;
844
845 VisualShaderNodeVarying();
846};
847
848class VisualShaderNodeVaryingSetter : public VisualShaderNodeVarying {
849 GDCLASS(VisualShaderNodeVaryingSetter, VisualShaderNodeVarying);
850
851public:
852 virtual String get_caption() const override;
853
854 virtual int get_input_port_count() const override;
855 virtual PortType get_input_port_type(int p_port) const override;
856 virtual String get_input_port_name(int p_port) const override;
857
858 virtual int get_output_port_count() const override;
859 virtual PortType get_output_port_type(int p_port) const override;
860 virtual String get_output_port_name(int p_port) const override;
861
862 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;
863
864 VisualShaderNodeVaryingSetter();
865};
866
867class VisualShaderNodeVaryingGetter : public VisualShaderNodeVarying {
868 GDCLASS(VisualShaderNodeVaryingGetter, VisualShaderNodeVarying);
869
870public:
871 virtual String get_caption() const override;
872
873 virtual int get_input_port_count() const override;
874 virtual PortType get_input_port_type(int p_port) const override;
875 virtual String get_input_port_name(int p_port) const override;
876
877 virtual int get_output_port_count() const override;
878 virtual PortType get_output_port_type(int p_port) const override;
879 virtual String get_output_port_name(int p_port) const override;
880 virtual bool has_output_port_preview(int p_port) const override;
881
882 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;
883
884 VisualShaderNodeVaryingGetter();
885};
886
887extern String make_unique_id(VisualShader::Type p_type, int p_id, const String &p_name);
888
889#endif // VISUAL_SHADER_H
890