Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/shader/shader_file_editor_plugin.cpp
9896 views
1
/**************************************************************************/
2
/* shader_file_editor_plugin.cpp */
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
#include "shader_file_editor_plugin.h"
32
33
#include "editor/editor_node.h"
34
#include "editor/editor_string_names.h"
35
#include "editor/gui/editor_bottom_panel.h"
36
#include "editor/settings/editor_command_palette.h"
37
#include "editor/themes/editor_scale.h"
38
#include "scene/gui/item_list.h"
39
#include "scene/gui/split_container.h"
40
#include "servers/display_server.h"
41
42
/*** SHADER SCRIPT EDITOR ****/
43
44
/*** SCRIPT EDITOR ******/
45
46
void ShaderFileEditor::_update_version(const StringName &p_version_txt, const RD::ShaderStage p_stage) {
47
}
48
49
void ShaderFileEditor::_version_selected(int p_option) {
50
int c = versions->get_current();
51
StringName version_txt = versions->get_item_metadata(c);
52
53
RD::ShaderStage stage = RD::SHADER_STAGE_MAX;
54
int first_found = -1;
55
56
Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(version_txt);
57
ERR_FAIL_COND(bytecode.is_null());
58
59
for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
60
if (bytecode->get_stage_bytecode(RD::ShaderStage(i)).is_empty() && bytecode->get_stage_compile_error(RD::ShaderStage(i)) == String()) {
61
stages[i]->set_button_icon(Ref<Texture2D>());
62
continue;
63
}
64
65
Ref<Texture2D> icon;
66
if (bytecode->get_stage_compile_error(RD::ShaderStage(i)) != String()) {
67
icon = get_editor_theme_icon(SNAME("ImportFail"));
68
} else {
69
icon = get_editor_theme_icon(SNAME("ImportCheck"));
70
}
71
stages[i]->set_button_icon(icon);
72
73
if (first_found == -1) {
74
first_found = i;
75
}
76
77
if (stages[i]->is_pressed()) {
78
stage = RD::ShaderStage(i);
79
break;
80
}
81
}
82
83
error_text->clear();
84
85
if (stage == RD::SHADER_STAGE_MAX) { //need to change stage, does not have it
86
if (first_found == -1) {
87
error_text->add_text(TTR("No valid shader stages found."));
88
return; //well you did not put any stage I guess?
89
}
90
stages[first_found]->set_pressed(true);
91
stage = RD::ShaderStage(first_found);
92
}
93
94
String error = bytecode->get_stage_compile_error(stage);
95
96
error_text->push_font(get_theme_font(SNAME("source"), EditorStringName(EditorFonts)));
97
98
if (error.is_empty()) {
99
error_text->add_text(TTR("Shader stage compiled without errors."));
100
} else {
101
error_text->add_text(error);
102
}
103
}
104
105
void ShaderFileEditor::_update_options() {
106
ERR_FAIL_COND(shader_file.is_null());
107
108
if (!shader_file->get_base_error().is_empty()) {
109
stage_hb->hide();
110
versions->hide();
111
error_text->clear();
112
error_text->push_font(get_theme_font(SNAME("source"), EditorStringName(EditorFonts)));
113
error_text->add_text(vformat(TTR("File structure for '%s' contains unrecoverable errors:\n\n"), shader_file->get_path().get_file()));
114
error_text->add_text(shader_file->get_base_error());
115
return;
116
}
117
118
stage_hb->show();
119
versions->show();
120
121
int c = versions->get_current();
122
//remember current
123
versions->clear();
124
TypedArray<StringName> version_list = shader_file->get_version_list();
125
126
if (c >= version_list.size()) {
127
c = version_list.size() - 1;
128
}
129
if (c < 0) {
130
c = 0;
131
}
132
133
StringName current_version;
134
135
for (int i = 0; i < version_list.size(); i++) {
136
String title = version_list[i];
137
if (title.is_empty()) {
138
title = "default";
139
}
140
141
Ref<Texture2D> icon;
142
143
Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(version_list[i]);
144
ERR_FAIL_COND(bytecode.is_null());
145
146
bool failed = false;
147
for (int j = 0; j < RD::SHADER_STAGE_MAX; j++) {
148
String error = bytecode->get_stage_compile_error(RD::ShaderStage(j));
149
if (!error.is_empty()) {
150
failed = true;
151
}
152
}
153
154
if (failed) {
155
icon = get_editor_theme_icon(SNAME("ImportFail"));
156
} else {
157
icon = get_editor_theme_icon(SNAME("ImportCheck"));
158
}
159
160
versions->add_item(title, icon);
161
versions->set_item_metadata(i, version_list[i]);
162
163
if (i == c) {
164
versions->select(i);
165
current_version = version_list[i];
166
}
167
}
168
169
if (version_list.is_empty()) {
170
for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
171
stages[i]->set_disabled(true);
172
}
173
return;
174
}
175
176
Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(current_version);
177
ERR_FAIL_COND(bytecode.is_null());
178
int first_valid = -1;
179
int current = -1;
180
for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
181
Vector<uint8_t> bc = bytecode->get_stage_bytecode(RD::ShaderStage(i));
182
String error = bytecode->get_stage_compile_error(RD::ShaderStage(i));
183
bool disable = error.is_empty() && bc.is_empty();
184
stages[i]->set_disabled(disable);
185
if (!disable) {
186
if (stages[i]->is_pressed()) {
187
current = i;
188
}
189
first_valid = i;
190
}
191
}
192
193
if (current == -1 && first_valid != -1) {
194
stages[first_valid]->set_pressed(true);
195
}
196
197
_version_selected(0);
198
}
199
200
void ShaderFileEditor::_notification(int p_what) {
201
switch (p_what) {
202
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
203
if (is_visible_in_tree() && shader_file.is_valid()) {
204
_update_options();
205
}
206
} break;
207
}
208
}
209
210
void ShaderFileEditor::_editor_settings_changed() {
211
if (is_visible_in_tree() && shader_file.is_valid()) {
212
_update_options();
213
}
214
}
215
216
void ShaderFileEditor::edit(const Ref<RDShaderFile> &p_shader) {
217
if (p_shader.is_null()) {
218
if (shader_file.is_valid()) {
219
shader_file->disconnect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));
220
}
221
return;
222
}
223
224
if (shader_file == p_shader) {
225
return;
226
}
227
228
shader_file = p_shader;
229
230
if (shader_file.is_valid()) {
231
shader_file->connect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));
232
}
233
234
_update_options();
235
}
236
237
void ShaderFileEditor::_shader_changed() {
238
if (is_visible_in_tree()) {
239
_update_options();
240
}
241
}
242
243
ShaderFileEditor *ShaderFileEditor::singleton = nullptr;
244
245
ShaderFileEditor::ShaderFileEditor() {
246
singleton = this;
247
HSplitContainer *main_hs = memnew(HSplitContainer);
248
249
add_child(main_hs);
250
251
versions = memnew(ItemList);
252
versions->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
253
versions->connect(SceneStringName(item_selected), callable_mp(this, &ShaderFileEditor::_version_selected));
254
versions->set_custom_minimum_size(Size2i(200 * EDSCALE, 0));
255
versions->set_theme_type_variation("TreeSecondary");
256
main_hs->add_child(versions);
257
258
VBoxContainer *main_vb = memnew(VBoxContainer);
259
main_vb->set_h_size_flags(SIZE_EXPAND_FILL);
260
main_hs->add_child(main_vb);
261
262
static const char *stage_str[RD::SHADER_STAGE_MAX] = {
263
"Vertex",
264
"Fragment",
265
"TessControl",
266
"TessEval",
267
"Compute"
268
};
269
270
stage_hb = memnew(HBoxContainer);
271
main_vb->add_child(stage_hb);
272
273
Ref<ButtonGroup> bg;
274
bg.instantiate();
275
for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
276
Button *button = memnew(Button(stage_str[i]));
277
button->set_toggle_mode(true);
278
button->set_focus_mode(FOCUS_ACCESSIBILITY);
279
stage_hb->add_child(button);
280
stages[i] = button;
281
button->set_button_group(bg);
282
button->connect(SceneStringName(pressed), callable_mp(this, &ShaderFileEditor::_version_selected).bind(i));
283
}
284
285
error_text = memnew(RichTextLabel);
286
error_text->set_v_size_flags(SIZE_EXPAND_FILL);
287
error_text->set_selection_enabled(true);
288
error_text->set_context_menu_enabled(true);
289
main_vb->add_child(error_text);
290
}
291
292
void ShaderFileEditorPlugin::edit(Object *p_object) {
293
RDShaderFile *s = Object::cast_to<RDShaderFile>(p_object);
294
shader_editor->edit(s);
295
}
296
297
bool ShaderFileEditorPlugin::handles(Object *p_object) const {
298
RDShaderFile *shader = Object::cast_to<RDShaderFile>(p_object);
299
return shader != nullptr;
300
}
301
302
void ShaderFileEditorPlugin::make_visible(bool p_visible) {
303
if (p_visible) {
304
button->show();
305
EditorNode::get_bottom_panel()->make_item_visible(shader_editor);
306
307
} else {
308
button->hide();
309
if (shader_editor->is_visible_in_tree()) {
310
EditorNode::get_bottom_panel()->hide_bottom_panel();
311
}
312
}
313
}
314
315
ShaderFileEditorPlugin::ShaderFileEditorPlugin() {
316
shader_editor = memnew(ShaderFileEditor);
317
318
shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
319
button = EditorNode::get_bottom_panel()->add_item(TTRC("ShaderFile"), shader_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_shader_file_bottom_panel", TTRC("Toggle ShaderFile Bottom Panel")));
320
button->hide();
321
}
322
323