2020-05-10 12:53:46 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* shader_file_editor_plugin.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2020-04-20 04:19:21 +02:00
|
|
|
#include "shader_file_editor_plugin.h"
|
|
|
|
|
|
|
|
#include "core/io/resource_loader.h"
|
|
|
|
#include "core/io/resource_saver.h"
|
|
|
|
#include "core/os/keyboard.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "editor/editor_node.h"
|
|
|
|
#include "editor/editor_settings.h"
|
2023-08-13 02:33:39 +02:00
|
|
|
#include "editor/editor_string_names.h"
|
2024-01-30 23:22:22 +01:00
|
|
|
#include "editor/gui/editor_bottom_panel.h"
|
2024-01-15 13:14:55 +01:00
|
|
|
#include "editor/themes/editor_scale.h"
|
2022-11-19 12:45:49 +01:00
|
|
|
#include "scene/gui/item_list.h"
|
|
|
|
#include "scene/gui/split_container.h"
|
2020-04-20 04:19:21 +02:00
|
|
|
#include "servers/display_server.h"
|
|
|
|
#include "servers/rendering/shader_types.h"
|
|
|
|
|
|
|
|
/*** SHADER SCRIPT EDITOR ****/
|
|
|
|
|
|
|
|
/*** SCRIPT EDITOR ******/
|
|
|
|
|
|
|
|
void ShaderFileEditor::_update_version(const StringName &p_version_txt, const RD::ShaderStage p_stage) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditor::_version_selected(int p_option) {
|
|
|
|
int c = versions->get_current();
|
|
|
|
StringName version_txt = versions->get_item_metadata(c);
|
|
|
|
|
|
|
|
RD::ShaderStage stage = RD::SHADER_STAGE_MAX;
|
|
|
|
int first_found = -1;
|
|
|
|
|
Implement Binary Shader Compilation
* Added an extra stage before compiling shader, which is generating a binary blob.
* On Vulkan, this allows caching the SPIRV reflection information, which is expensive to parse.
* On other (future) RenderingDevices, it allows caching converted binary data, such as DXIL or MSL.
This PR makes the shader cache include the reflection information, hence editor startup times are significantly improved.
I tested this well and it appears to work, and I added a lot of consistency checks, but because it includes writing and reading binary information, rare bugs may pop up, so be aware.
There was not much of a choice for storing the reflection information, given shaders can be a lot, take a lot of space and take time to parse.
2021-07-25 16:22:55 +02:00
|
|
|
Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(version_txt);
|
2020-04-20 04:19:21 +02:00
|
|
|
ERR_FAIL_COND(bytecode.is_null());
|
|
|
|
|
|
|
|
for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
|
2020-12-15 13:04:21 +01:00
|
|
|
if (bytecode->get_stage_bytecode(RD::ShaderStage(i)).is_empty() && bytecode->get_stage_compile_error(RD::ShaderStage(i)) == String()) {
|
2020-04-20 04:19:21 +02:00
|
|
|
stages[i]->set_icon(Ref<Texture2D>());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Texture2D> icon;
|
|
|
|
if (bytecode->get_stage_compile_error(RD::ShaderStage(i)) != String()) {
|
2023-08-13 02:33:39 +02:00
|
|
|
icon = get_editor_theme_icon(SNAME("ImportFail"));
|
2020-04-20 04:19:21 +02:00
|
|
|
} else {
|
2023-08-13 02:33:39 +02:00
|
|
|
icon = get_editor_theme_icon(SNAME("ImportCheck"));
|
2020-04-20 04:19:21 +02:00
|
|
|
}
|
|
|
|
stages[i]->set_icon(icon);
|
|
|
|
|
|
|
|
if (first_found == -1) {
|
|
|
|
first_found = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stages[i]->is_pressed()) {
|
|
|
|
stage = RD::ShaderStage(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error_text->clear();
|
|
|
|
|
|
|
|
if (stage == RD::SHADER_STAGE_MAX) { //need to change stage, does not have it
|
|
|
|
if (first_found == -1) {
|
|
|
|
error_text->add_text(TTR("No valid shader stages found."));
|
|
|
|
return; //well you did not put any stage I guess?
|
|
|
|
}
|
|
|
|
stages[first_found]->set_pressed(true);
|
|
|
|
stage = RD::ShaderStage(first_found);
|
|
|
|
}
|
|
|
|
|
|
|
|
String error = bytecode->get_stage_compile_error(stage);
|
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
error_text->push_font(get_theme_font(SNAME("source"), EditorStringName(EditorFonts)));
|
2020-04-20 04:19:21 +02:00
|
|
|
|
2021-12-09 10:42:46 +01:00
|
|
|
if (error.is_empty()) {
|
2020-04-20 04:19:21 +02:00
|
|
|
error_text->add_text(TTR("Shader stage compiled without errors."));
|
|
|
|
} else {
|
|
|
|
error_text->add_text(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditor::_update_options() {
|
|
|
|
ERR_FAIL_COND(shader_file.is_null());
|
|
|
|
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!shader_file->get_base_error().is_empty()) {
|
2020-04-20 04:19:21 +02:00
|
|
|
stage_hb->hide();
|
|
|
|
versions->hide();
|
|
|
|
error_text->clear();
|
2023-08-13 02:33:39 +02:00
|
|
|
error_text->push_font(get_theme_font(SNAME("source"), EditorStringName(EditorFonts)));
|
2020-04-20 04:19:21 +02:00
|
|
|
error_text->add_text(vformat(TTR("File structure for '%s' contains unrecoverable errors:\n\n"), shader_file->get_path().get_file()));
|
|
|
|
error_text->add_text(shader_file->get_base_error());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stage_hb->show();
|
|
|
|
versions->show();
|
|
|
|
|
|
|
|
int c = versions->get_current();
|
|
|
|
//remember current
|
|
|
|
versions->clear();
|
2023-04-25 00:21:32 +02:00
|
|
|
TypedArray<StringName> version_list = shader_file->get_version_list();
|
2020-04-20 04:19:21 +02:00
|
|
|
|
|
|
|
if (c >= version_list.size()) {
|
|
|
|
c = version_list.size() - 1;
|
|
|
|
}
|
|
|
|
if (c < 0) {
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringName current_version;
|
|
|
|
|
|
|
|
for (int i = 0; i < version_list.size(); i++) {
|
|
|
|
String title = version_list[i];
|
2021-12-09 10:42:46 +01:00
|
|
|
if (title.is_empty()) {
|
2020-04-20 04:19:21 +02:00
|
|
|
title = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Texture2D> icon;
|
|
|
|
|
Implement Binary Shader Compilation
* Added an extra stage before compiling shader, which is generating a binary blob.
* On Vulkan, this allows caching the SPIRV reflection information, which is expensive to parse.
* On other (future) RenderingDevices, it allows caching converted binary data, such as DXIL or MSL.
This PR makes the shader cache include the reflection information, hence editor startup times are significantly improved.
I tested this well and it appears to work, and I added a lot of consistency checks, but because it includes writing and reading binary information, rare bugs may pop up, so be aware.
There was not much of a choice for storing the reflection information, given shaders can be a lot, take a lot of space and take time to parse.
2021-07-25 16:22:55 +02:00
|
|
|
Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(version_list[i]);
|
2020-04-20 04:19:21 +02:00
|
|
|
ERR_FAIL_COND(bytecode.is_null());
|
|
|
|
|
|
|
|
bool failed = false;
|
|
|
|
for (int j = 0; j < RD::SHADER_STAGE_MAX; j++) {
|
|
|
|
String error = bytecode->get_stage_compile_error(RD::ShaderStage(j));
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!error.is_empty()) {
|
2020-04-20 04:19:21 +02:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed) {
|
2023-08-13 02:33:39 +02:00
|
|
|
icon = get_editor_theme_icon(SNAME("ImportFail"));
|
2020-04-20 04:19:21 +02:00
|
|
|
} else {
|
2023-08-13 02:33:39 +02:00
|
|
|
icon = get_editor_theme_icon(SNAME("ImportCheck"));
|
2020-04-20 04:19:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
versions->add_item(title, icon);
|
|
|
|
versions->set_item_metadata(i, version_list[i]);
|
|
|
|
|
|
|
|
if (i == c) {
|
|
|
|
versions->select(i);
|
|
|
|
current_version = version_list[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version_list.size() == 0) {
|
|
|
|
for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
|
|
|
|
stages[i]->set_disabled(true);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Implement Binary Shader Compilation
* Added an extra stage before compiling shader, which is generating a binary blob.
* On Vulkan, this allows caching the SPIRV reflection information, which is expensive to parse.
* On other (future) RenderingDevices, it allows caching converted binary data, such as DXIL or MSL.
This PR makes the shader cache include the reflection information, hence editor startup times are significantly improved.
I tested this well and it appears to work, and I added a lot of consistency checks, but because it includes writing and reading binary information, rare bugs may pop up, so be aware.
There was not much of a choice for storing the reflection information, given shaders can be a lot, take a lot of space and take time to parse.
2021-07-25 16:22:55 +02:00
|
|
|
Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(current_version);
|
2020-04-20 04:19:21 +02:00
|
|
|
ERR_FAIL_COND(bytecode.is_null());
|
|
|
|
int first_valid = -1;
|
|
|
|
int current = -1;
|
|
|
|
for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
|
|
|
|
Vector<uint8_t> bc = bytecode->get_stage_bytecode(RD::ShaderStage(i));
|
|
|
|
String error = bytecode->get_stage_compile_error(RD::ShaderStage(i));
|
2021-12-09 10:42:46 +01:00
|
|
|
bool disable = error.is_empty() && bc.is_empty();
|
2020-04-20 04:19:21 +02:00
|
|
|
stages[i]->set_disabled(disable);
|
|
|
|
if (!disable) {
|
|
|
|
if (stages[i]->is_pressed()) {
|
|
|
|
current = i;
|
|
|
|
}
|
|
|
|
first_valid = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current == -1 && first_valid != -1) {
|
|
|
|
stages[first_valid]->set_pressed(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_version_selected(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditor::_notification(int p_what) {
|
2022-02-16 15:17:55 +01:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
|
|
|
|
if (is_visible_in_tree() && shader_file.is_valid()) {
|
|
|
|
_update_options();
|
|
|
|
}
|
|
|
|
} break;
|
2020-04-20 04:19:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditor::_editor_settings_changed() {
|
|
|
|
if (is_visible_in_tree() && shader_file.is_valid()) {
|
|
|
|
_update_options();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditor::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditor::edit(const Ref<RDShaderFile> &p_shader) {
|
|
|
|
if (p_shader.is_null()) {
|
|
|
|
if (shader_file.is_valid()) {
|
2023-07-03 21:29:37 +02:00
|
|
|
shader_file->disconnect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));
|
2020-04-20 04:19:21 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (shader_file == p_shader) {
|
2020-04-20 04:19:21 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-04-20 04:19:21 +02:00
|
|
|
|
|
|
|
shader_file = p_shader;
|
|
|
|
|
|
|
|
if (shader_file.is_valid()) {
|
2023-07-03 21:29:37 +02:00
|
|
|
shader_file->connect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));
|
2020-04-20 04:19:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_update_options();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditor::_shader_changed() {
|
|
|
|
if (is_visible_in_tree()) {
|
|
|
|
_update_options();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderFileEditor *ShaderFileEditor::singleton = nullptr;
|
|
|
|
|
2022-01-27 10:36:51 +01:00
|
|
|
ShaderFileEditor::ShaderFileEditor() {
|
2020-04-20 04:19:21 +02:00
|
|
|
singleton = this;
|
|
|
|
HSplitContainer *main_hs = memnew(HSplitContainer);
|
|
|
|
|
|
|
|
add_child(main_hs);
|
|
|
|
|
|
|
|
versions = memnew(ItemList);
|
2024-01-23 22:29:45 +01:00
|
|
|
versions->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
|
2020-04-20 04:19:21 +02:00
|
|
|
versions->connect("item_selected", callable_mp(this, &ShaderFileEditor::_version_selected));
|
|
|
|
versions->set_custom_minimum_size(Size2i(200 * EDSCALE, 0));
|
|
|
|
main_hs->add_child(versions);
|
|
|
|
|
|
|
|
VBoxContainer *main_vb = memnew(VBoxContainer);
|
|
|
|
main_vb->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
main_hs->add_child(main_vb);
|
|
|
|
|
|
|
|
static const char *stage_str[RD::SHADER_STAGE_MAX] = {
|
|
|
|
"Vertex",
|
|
|
|
"Fragment",
|
|
|
|
"TessControl",
|
|
|
|
"TessEval",
|
|
|
|
"Compute"
|
|
|
|
};
|
|
|
|
|
|
|
|
stage_hb = memnew(HBoxContainer);
|
|
|
|
main_vb->add_child(stage_hb);
|
|
|
|
|
|
|
|
Ref<ButtonGroup> bg;
|
2021-06-18 00:03:09 +02:00
|
|
|
bg.instantiate();
|
2020-04-20 04:19:21 +02:00
|
|
|
for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
|
|
|
|
Button *button = memnew(Button(stage_str[i]));
|
|
|
|
button->set_toggle_mode(true);
|
|
|
|
button->set_focus_mode(FOCUS_NONE);
|
|
|
|
stage_hb->add_child(button);
|
|
|
|
stages[i] = button;
|
|
|
|
button->set_button_group(bg);
|
2022-07-28 22:56:41 +02:00
|
|
|
button->connect("pressed", callable_mp(this, &ShaderFileEditor::_version_selected).bind(i));
|
2020-04-20 04:19:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
error_text = memnew(RichTextLabel);
|
|
|
|
error_text->set_v_size_flags(SIZE_EXPAND_FILL);
|
2022-11-20 20:15:39 +01:00
|
|
|
error_text->set_selection_enabled(true);
|
2023-02-28 14:19:48 +01:00
|
|
|
error_text->set_context_menu_enabled(true);
|
2020-04-20 04:19:21 +02:00
|
|
|
main_vb->add_child(error_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditorPlugin::edit(Object *p_object) {
|
|
|
|
RDShaderFile *s = Object::cast_to<RDShaderFile>(p_object);
|
|
|
|
shader_editor->edit(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderFileEditorPlugin::handles(Object *p_object) const {
|
|
|
|
RDShaderFile *shader = Object::cast_to<RDShaderFile>(p_object);
|
|
|
|
return shader != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderFileEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
if (p_visible) {
|
|
|
|
button->show();
|
2024-01-30 23:22:22 +01:00
|
|
|
EditorNode::get_bottom_panel()->make_item_visible(shader_editor);
|
2020-04-20 04:19:21 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
button->hide();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (shader_editor->is_visible_in_tree()) {
|
2024-01-30 23:22:22 +01:00
|
|
|
EditorNode::get_bottom_panel()->hide_bottom_panel();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-04-20 04:19:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 10:36:51 +01:00
|
|
|
ShaderFileEditorPlugin::ShaderFileEditorPlugin() {
|
|
|
|
shader_editor = memnew(ShaderFileEditor);
|
2020-04-20 04:19:21 +02:00
|
|
|
|
|
|
|
shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
|
2024-01-30 23:22:22 +01:00
|
|
|
button = EditorNode::get_bottom_panel()->add_item(TTR("ShaderFile"), shader_editor);
|
2020-04-20 04:19:21 +02:00
|
|
|
button->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderFileEditorPlugin::~ShaderFileEditorPlugin() {
|
|
|
|
}
|