Path: blob/master/editor/shader/shader_file_editor_plugin.cpp
20936 views
/**************************************************************************/1/* shader_file_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "shader_file_editor_plugin.h"3132#include "editor/docks/editor_dock_manager.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/settings/editor_command_palette.h"36#include "editor/themes/editor_scale.h"37#include "scene/gui/flow_container.h"38#include "scene/gui/item_list.h"39#include "scene/gui/split_container.h"40#include "servers/display/display_server.h"4142/*** SHADER SCRIPT EDITOR ****/4344/*** SCRIPT EDITOR ******/4546void ShaderFileEditor::_update_version(const StringName &p_version_txt, const RD::ShaderStage p_stage) {47}4849void ShaderFileEditor::_version_selected(int p_option) {50int c = versions->get_current();51StringName version_txt = versions->get_item_metadata(c);5253RD::ShaderStage stage = RD::SHADER_STAGE_MAX;54int first_found = -1;5556Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(version_txt);57ERR_FAIL_COND(bytecode.is_null());5859for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {60if (bytecode->get_stage_bytecode(RD::ShaderStage(i)).is_empty() && bytecode->get_stage_compile_error(RD::ShaderStage(i)) == String()) {61stages[i]->set_button_icon(Ref<Texture2D>());62continue;63}6465Ref<Texture2D> outcome_icon;66if (bytecode->get_stage_compile_error(RD::ShaderStage(i)) != String()) {67outcome_icon = get_editor_theme_icon(SNAME("ImportFail"));68} else {69outcome_icon = get_editor_theme_icon(SNAME("ImportCheck"));70}71stages[i]->set_button_icon(outcome_icon);7273if (first_found == -1) {74first_found = i;75}7677if (stages[i]->is_pressed()) {78stage = RD::ShaderStage(i);79break;80}81}8283error_text->clear();8485if (stage == RD::SHADER_STAGE_MAX) { //need to change stage, does not have it86if (first_found == -1) {87error_text->add_text(TTR("No valid shader stages found."));88return; //well you did not put any stage I guess?89}90stages[first_found]->set_pressed(true);91stage = RD::ShaderStage(first_found);92}9394String error = bytecode->get_stage_compile_error(stage);9596error_text->push_font(get_theme_font(SNAME("source"), EditorStringName(EditorFonts)));9798if (error.is_empty()) {99error_text->add_text(TTR("Shader stage compiled without errors."));100} else {101error_text->add_text(error);102}103}104105void ShaderFileEditor::_update_options() {106ERR_FAIL_COND(shader_file.is_null());107108if (!shader_file->get_base_error().is_empty()) {109stage_hb->hide();110versions->hide();111error_text->clear();112error_text->push_font(get_theme_font(SNAME("source"), EditorStringName(EditorFonts)));113error_text->add_text(vformat(TTR("File structure for '%s' contains unrecoverable errors:\n\n"), shader_file->get_path().get_file()));114error_text->add_text(shader_file->get_base_error());115return;116}117118stage_hb->show();119versions->show();120121int c = versions->get_current();122//remember current123versions->clear();124TypedArray<StringName> version_list = shader_file->get_version_list();125126if (c >= version_list.size()) {127c = version_list.size() - 1;128}129if (c < 0) {130c = 0;131}132133StringName current_version;134135for (int i = 0; i < version_list.size(); i++) {136String version_title = version_list[i];137if (version_title.is_empty()) {138version_title = "default";139}140141Ref<Texture2D> outcome_icon;142143Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(version_list[i]);144ERR_FAIL_COND(bytecode.is_null());145146bool failed = false;147for (int j = 0; j < RD::SHADER_STAGE_MAX; j++) {148String error = bytecode->get_stage_compile_error(RD::ShaderStage(j));149if (!error.is_empty()) {150failed = true;151}152}153154if (failed) {155outcome_icon = get_editor_theme_icon(SNAME("ImportFail"));156} else {157outcome_icon = get_editor_theme_icon(SNAME("ImportCheck"));158}159160versions->add_item(version_title, outcome_icon);161versions->set_item_metadata(i, version_list[i]);162163if (i == c) {164versions->select(i);165current_version = version_list[i];166}167}168169if (version_list.is_empty()) {170for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {171stages[i]->set_disabled(true);172}173return;174}175176Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(current_version);177ERR_FAIL_COND(bytecode.is_null());178int first_valid = -1;179int current = -1;180for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {181Vector<uint8_t> bc = bytecode->get_stage_bytecode(RD::ShaderStage(i));182String error = bytecode->get_stage_compile_error(RD::ShaderStage(i));183bool disable = error.is_empty() && bc.is_empty();184stages[i]->set_disabled(disable);185if (!disable) {186if (stages[i]->is_pressed()) {187current = i;188}189first_valid = i;190}191}192193if (current == -1 && first_valid != -1) {194stages[first_valid]->set_pressed(true);195}196197_version_selected(0);198}199200void ShaderFileEditor::_notification(int p_what) {201switch (p_what) {202case NOTIFICATION_WM_WINDOW_FOCUS_IN: {203if (is_visible_in_tree() && shader_file.is_valid()) {204_update_options();205}206} break;207}208}209210void ShaderFileEditor::_editor_settings_changed() {211if (is_visible_in_tree() && shader_file.is_valid()) {212_update_options();213}214}215216void ShaderFileEditor::edit(const Ref<RDShaderFile> &p_shader) {217if (p_shader.is_null()) {218if (shader_file.is_valid()) {219shader_file->disconnect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));220}221return;222}223224if (shader_file == p_shader) {225return;226}227228shader_file = p_shader;229230if (shader_file.is_valid()) {231shader_file->connect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));232}233234_update_options();235}236237void ShaderFileEditor::_shader_changed() {238if (is_visible_in_tree()) {239_update_options();240}241}242243ShaderFileEditor *ShaderFileEditor::singleton = nullptr;244245ShaderFileEditor::ShaderFileEditor() {246singleton = this;247248set_name(TTRC("ShaderFile"));249set_icon_name("RDShaderFile");250set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_shader_file_bottom_panel", TTRC("Toggle ShaderFile Dock")));251set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);252set_available_layouts(EditorDock::DOCK_LAYOUT_ALL);253set_global(false);254set_transient(true);255set_custom_minimum_size(Size2(300, 200) * EDSCALE);256257HSplitContainer *main_hs = memnew(HSplitContainer);258259add_child(main_hs);260261versions = memnew(ItemList);262versions->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);263versions->connect(SceneStringName(item_selected), callable_mp(this, &ShaderFileEditor::_version_selected));264versions->set_custom_minimum_size(Size2i(100 * EDSCALE, 0));265versions->set_theme_type_variation("TreeSecondary");266main_hs->add_child(versions);267268VBoxContainer *main_vb = memnew(VBoxContainer);269main_vb->set_h_size_flags(SIZE_EXPAND_FILL);270main_hs->add_child(main_vb);271272static const char *stage_str[RD::SHADER_STAGE_MAX] = {273"Vertex",274"Fragment",275"TessControl",276"TessEval",277"Compute",278"Raygen",279"AnyHit",280"ClosestHit",281"Miss",282"Intersection",283};284285stage_hb = memnew(HFlowContainer);286main_vb->add_child(stage_hb);287288Ref<ButtonGroup> bg;289bg.instantiate();290for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {291Button *button = memnew(Button(stage_str[i]));292button->set_toggle_mode(true);293button->set_focus_mode(FOCUS_ACCESSIBILITY);294stage_hb->add_child(button);295stages[i] = button;296button->set_button_group(bg);297button->connect(SceneStringName(pressed), callable_mp(this, &ShaderFileEditor::_version_selected).bind(i));298}299300error_text = memnew(RichTextLabel);301error_text->set_v_size_flags(SIZE_EXPAND_FILL);302error_text->set_selection_enabled(true);303error_text->set_context_menu_enabled(true);304main_vb->add_child(error_text);305}306307void ShaderFileEditorPlugin::edit(Object *p_object) {308RDShaderFile *s = Object::cast_to<RDShaderFile>(p_object);309shader_editor->edit(s);310}311312bool ShaderFileEditorPlugin::handles(Object *p_object) const {313RDShaderFile *shader = Object::cast_to<RDShaderFile>(p_object);314return shader != nullptr;315}316317void ShaderFileEditorPlugin::make_visible(bool p_visible) {318if (p_visible) {319shader_editor->make_visible();320} else {321shader_editor->close();322}323}324325ShaderFileEditorPlugin::ShaderFileEditorPlugin() {326shader_editor = memnew(ShaderFileEditor);327EditorDockManager::get_singleton()->add_dock(shader_editor);328shader_editor->close();329}330331332