1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
/*************************************************************************/
/* visual_shader.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* 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 */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef VISUAL_SHADER_H
#define VISUAL_SHADER_H
#include "core/string/string_builder.h"
#include "scene/gui/control.h"
#include "scene/resources/shader.h"
class VisualShaderNodeUniform;
class VisualShaderNode;
class VisualShader : public Shader {
GDCLASS(VisualShader, Shader);
friend class VisualShaderNodeVersionChecker;
String version = "";
public:
enum Type {
TYPE_VERTEX,
TYPE_FRAGMENT,
TYPE_LIGHT,
TYPE_EMIT,
TYPE_PROCESS,
TYPE_END,
TYPE_MAX
};
struct Connection {
int from_node = 0;
int from_port = 0;
int to_node = 0;
int to_port = 0;
};
struct DefaultTextureParam {
StringName name;
Ref<Texture2D> param;
};
private:
Type current_type;
struct Node {
Ref<VisualShaderNode> node;
Vector2 position;
List<int> prev_connected_nodes;
};
struct Graph {
Map<int, Node> nodes;
List<Connection> connections;
} graph[TYPE_MAX];
Shader::Mode shader_mode = Shader::MODE_SPATIAL;
mutable String previous_code;
Array _get_node_connections(Type p_type) const;
Vector2 graph_offset;
struct RenderModeEnums {
Shader::Mode mode = Shader::Mode::MODE_MAX;
const char *string;
};
HashMap<String, int> modes;
Set<StringName> flags;
static RenderModeEnums render_mode_enums[];
volatile mutable bool dirty = true;
void _queue_update();
union ConnectionKey {
struct {
uint64_t node : 32;
uint64_t port : 32;
};
uint64_t key = 0;
bool operator<(const ConnectionKey &p_key) const {
return key < p_key.key;
}
};
Error _write_node(Type p_type, StringBuilder &global_code, StringBuilder &global_code_per_node, Map<Type, StringBuilder> &global_code_per_func, 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, Set<StringName> &r_classes) const;
void _input_type_changed(Type p_type, int p_id);
bool has_func_name(RenderingServer::ShaderMode p_mode, const String &p_func_name) const;
protected:
virtual void _update_shader() const override;
static void _bind_methods();
bool _set(const StringName &p_name, const Variant &p_value);
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 update_version(const String &p_new_version);
enum {
NODE_ID_INVALID = -1,
NODE_ID_OUTPUT = 0,
};
void add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id);
void set_node_position(Type p_type, int p_id, const Vector2 &p_position);
Vector2 get_node_position(Type p_type, int p_id) const;
Ref<VisualShaderNode> get_node(Type p_type, int p_id) const;
Vector<int> get_node_list(Type p_type) const;
int get_valid_node_id(Type p_type) const;
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;
bool is_nodes_connected_relatively(const Graph *p_graph, int p_node, int p_target) const;
bool can_connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
Error connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
void disconnect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
void connect_nodes_forced(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
bool is_port_types_compatible(int p_a, int p_b) const;
void rebuild();
void get_node_connections(Type p_type, List<Connection> *r_connections) const;
void set_mode(Mode p_mode);
virtual Mode get_mode() const override;
virtual bool is_text_shader() const override;
void set_graph_offset(const Vector2 &p_offset);
Vector2 get_graph_offset() const;
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_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();
};
VARIANT_ENUM_CAST(VisualShader::Type)
///
///
///
class VisualShaderNode : public Resource {
GDCLASS(VisualShaderNode, Resource);
int port_preview = -1;
Map<int, Variant> default_input_values;
Map<int, bool> connected_input_ports;
Map<int, int> connected_output_ports;
protected:
bool simple_decl = true;
static void _bind_methods();
public:
enum PortType {
PORT_TYPE_SCALAR,
PORT_TYPE_SCALAR_INT,
PORT_TYPE_VECTOR,
PORT_TYPE_BOOLEAN,
PORT_TYPE_TRANSFORM,
PORT_TYPE_SAMPLER,
PORT_TYPE_MAX,
};
bool is_simple_decl() const;
virtual String get_caption() const = 0;
virtual int get_input_port_count() const = 0;
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);
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 int get_output_port_count() const = 0;
virtual PortType get_output_port_type(int p_port) const = 0;
virtual String get_output_port_name(int p_port) const = 0;
virtual String get_input_port_default_hint(int p_port) const;
void set_output_port_for_preview(int p_index);
int get_output_port_for_preview() const;
virtual bool is_port_separator(int p_index) const;
bool is_output_port_connected(int p_port) const;
void set_output_port_connected(int p_port, bool p_connected);
bool is_input_port_connected(int p_port) const;
void set_input_port_connected(int p_port, bool p_connected);
virtual bool is_generate_input_var(int p_port) const;
virtual bool is_code_generated() const;
virtual bool is_show_prop_names() const;
virtual bool is_use_prop_slots() const;
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
virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const;
VisualShaderNode();
};
VARIANT_ENUM_CAST(VisualShaderNode::PortType)
class VisualShaderNodeCustom : public VisualShaderNode {
GDCLASS(VisualShaderNodeCustom, VisualShaderNode);
struct Port {
String name;
int type = 0;
};
List<Port> input_ports;
List<Port> output_ports;
friend class VisualShaderEditor;
protected:
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;
protected:
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;
static void _bind_methods();
public:
VisualShaderNodeCustom();
void update_ports();
};
/////
class VisualShaderNodeInput : public VisualShaderNode {
GDCLASS(VisualShaderNodeInput, VisualShaderNode);
friend class VisualShader;
VisualShader::Type shader_type = VisualShader::TYPE_MAX;
Shader::Mode shader_mode = Shader::MODE_MAX;
struct Port {
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;
};
static const Port ports[];
static const Port preview_ports[];
String input_name = "[None]";
protected:
static void _bind_methods();
void _validate_property(PropertyInfo &property) const override;
public:
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 get_caption() 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_input_name(String p_name);
String get_input_name() const;
String get_input_real_name() const;
int get_input_index_count() const;
PortType get_input_index_type(int p_index) const;
String get_input_index_name(int p_index) const;
PortType get_input_type_by_name(String p_name) const;
virtual Vector<StringName> get_editable_properties() const override;
VisualShaderNodeInput();
};
///
class VisualShaderNodeOutput : public VisualShaderNode {
GDCLASS(VisualShaderNodeOutput, VisualShaderNode);
public:
friend class VisualShader;
VisualShader::Type shader_type = VisualShader::Type::TYPE_MAX;
Shader::Mode shader_mode = Shader::Mode::MODE_MAX;
struct Port {
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;
};
static const Port ports[];
public:
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;
Variant get_input_port_default_value(int p_port) const;
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 bool is_port_separator(int p_index) const override;
virtual String get_caption() 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;
VisualShaderNodeOutput();
};
class VisualShaderNodeUniform : public VisualShaderNode {
GDCLASS(VisualShaderNodeUniform, VisualShaderNode);
public:
enum Qualifier {
QUAL_NONE,
QUAL_GLOBAL,
QUAL_INSTANCE,
};
private:
String uniform_name = "";
Qualifier qualifier = QUAL_NONE;
bool global_code_generated = false;
protected:
static void _bind_methods();
String _get_qual_str() const;
public:
void set_uniform_name(const String &p_name);
String get_uniform_name() const;
void set_qualifier(Qualifier p_qual);
Qualifier get_qualifier() const;
void set_global_code_generated(bool p_enabled);
bool is_global_code_generated() const;
virtual bool is_qualifier_supported(Qualifier p_qual) const = 0;
virtual Vector<StringName> get_editable_properties() const override;
virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const override;
VisualShaderNodeUniform();
};
VARIANT_ENUM_CAST(VisualShaderNodeUniform::Qualifier)
class VisualShaderNodeUniformRef : public VisualShaderNode {
GDCLASS(VisualShaderNodeUniformRef, VisualShaderNode);
public:
enum UniformType {
UNIFORM_TYPE_FLOAT,
UNIFORM_TYPE_INT,
UNIFORM_TYPE_BOOLEAN,
UNIFORM_TYPE_VECTOR,
UNIFORM_TYPE_TRANSFORM,
UNIFORM_TYPE_COLOR,
UNIFORM_TYPE_SAMPLER,
};
struct Uniform {
String name;
UniformType type;
};
private:
String uniform_name = "[None]";
UniformType uniform_type = UniformType::UNIFORM_TYPE_FLOAT;
protected:
static void _bind_methods();
public:
static void add_uniform(const String &p_name, UniformType p_type);
static void clear_uniforms();
static bool has_uniform(const String &p_name);
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;
void set_uniform_name(const String &p_name);
String get_uniform_name() const;
void _set_uniform_type(int p_uniform_type);
int _get_uniform_type() const;
int get_uniforms_count() const;
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;
virtual Vector<StringName> get_editable_properties() 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;
VisualShaderNodeUniformRef();
};
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 VisualShaderNodeGroupBase : public VisualShaderNodeResizableBase {
GDCLASS(VisualShaderNodeGroupBase, VisualShaderNodeResizableBase);
private:
void _apply_port_changes();
protected:
String inputs = "";
String outputs = "";
bool editable = false;
struct Port {
PortType type = PortType::PORT_TYPE_MAX;
String name;
};
Map<int, Port> input_ports;
Map<int, Port> output_ports;
Map<int, Control *> controls;
protected:
static void _bind_methods();
public:
void set_inputs(const String &p_inputs);
String get_inputs() const;
void set_outputs(const String &p_outputs);
String get_outputs() const;
bool is_valid_port_name(const String &p_name) const;
void add_input_port(int p_id, int p_type, const String &p_name);
void remove_input_port(int p_id);
virtual int get_input_port_count() const override;
bool has_input_port(int p_id) const;
void clear_input_ports();
void add_output_port(int p_id, int p_type, const String &p_name);
void remove_output_port(int p_id);
virtual int get_output_port_count() const override;
bool has_output_port(int p_id) const;
void clear_output_ports();
void set_input_port_type(int p_id, int p_type);
virtual PortType get_input_port_type(int p_id) const override;
void set_input_port_name(int p_id, const String &p_name);
virtual String get_input_port_name(int p_id) const override;
void set_output_port_type(int p_id, int p_type);
virtual PortType get_output_port_type(int p_id) const override;
void set_output_port_name(int p_id, const String &p_name);
virtual String get_output_port_name(int p_id) const override;
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_editable(bool p_enabled);
bool is_editable() 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 override;
VisualShaderNodeGroupBase();
};
class VisualShaderNodeExpression : public VisualShaderNodeGroupBase {
GDCLASS(VisualShaderNodeExpression, VisualShaderNodeGroupBase);
protected:
String expression = "";
static void _bind_methods();
public:
virtual String get_caption() const override;
void set_expression(const String &p_expression);
String get_expression() 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 override;
VisualShaderNodeExpression();
};
class VisualShaderNodeGlobalExpression : public VisualShaderNodeExpression {
GDCLASS(VisualShaderNodeGlobalExpression, VisualShaderNodeExpression);
public:
virtual String get_caption() const override;
virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const override;
VisualShaderNodeGlobalExpression();
};
#endif // VISUAL_SHADER_H
|