Path: blob/master/editor/shader/shader_file_editor_plugin.cpp
9896 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/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/gui/editor_bottom_panel.h"35#include "editor/settings/editor_command_palette.h"36#include "editor/themes/editor_scale.h"37#include "scene/gui/item_list.h"38#include "scene/gui/split_container.h"39#include "servers/display_server.h"4041/*** SHADER SCRIPT EDITOR ****/4243/*** SCRIPT EDITOR ******/4445void ShaderFileEditor::_update_version(const StringName &p_version_txt, const RD::ShaderStage p_stage) {46}4748void ShaderFileEditor::_version_selected(int p_option) {49int c = versions->get_current();50StringName version_txt = versions->get_item_metadata(c);5152RD::ShaderStage stage = RD::SHADER_STAGE_MAX;53int first_found = -1;5455Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(version_txt);56ERR_FAIL_COND(bytecode.is_null());5758for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {59if (bytecode->get_stage_bytecode(RD::ShaderStage(i)).is_empty() && bytecode->get_stage_compile_error(RD::ShaderStage(i)) == String()) {60stages[i]->set_button_icon(Ref<Texture2D>());61continue;62}6364Ref<Texture2D> icon;65if (bytecode->get_stage_compile_error(RD::ShaderStage(i)) != String()) {66icon = get_editor_theme_icon(SNAME("ImportFail"));67} else {68icon = get_editor_theme_icon(SNAME("ImportCheck"));69}70stages[i]->set_button_icon(icon);7172if (first_found == -1) {73first_found = i;74}7576if (stages[i]->is_pressed()) {77stage = RD::ShaderStage(i);78break;79}80}8182error_text->clear();8384if (stage == RD::SHADER_STAGE_MAX) { //need to change stage, does not have it85if (first_found == -1) {86error_text->add_text(TTR("No valid shader stages found."));87return; //well you did not put any stage I guess?88}89stages[first_found]->set_pressed(true);90stage = RD::ShaderStage(first_found);91}9293String error = bytecode->get_stage_compile_error(stage);9495error_text->push_font(get_theme_font(SNAME("source"), EditorStringName(EditorFonts)));9697if (error.is_empty()) {98error_text->add_text(TTR("Shader stage compiled without errors."));99} else {100error_text->add_text(error);101}102}103104void ShaderFileEditor::_update_options() {105ERR_FAIL_COND(shader_file.is_null());106107if (!shader_file->get_base_error().is_empty()) {108stage_hb->hide();109versions->hide();110error_text->clear();111error_text->push_font(get_theme_font(SNAME("source"), EditorStringName(EditorFonts)));112error_text->add_text(vformat(TTR("File structure for '%s' contains unrecoverable errors:\n\n"), shader_file->get_path().get_file()));113error_text->add_text(shader_file->get_base_error());114return;115}116117stage_hb->show();118versions->show();119120int c = versions->get_current();121//remember current122versions->clear();123TypedArray<StringName> version_list = shader_file->get_version_list();124125if (c >= version_list.size()) {126c = version_list.size() - 1;127}128if (c < 0) {129c = 0;130}131132StringName current_version;133134for (int i = 0; i < version_list.size(); i++) {135String title = version_list[i];136if (title.is_empty()) {137title = "default";138}139140Ref<Texture2D> icon;141142Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(version_list[i]);143ERR_FAIL_COND(bytecode.is_null());144145bool failed = false;146for (int j = 0; j < RD::SHADER_STAGE_MAX; j++) {147String error = bytecode->get_stage_compile_error(RD::ShaderStage(j));148if (!error.is_empty()) {149failed = true;150}151}152153if (failed) {154icon = get_editor_theme_icon(SNAME("ImportFail"));155} else {156icon = get_editor_theme_icon(SNAME("ImportCheck"));157}158159versions->add_item(title, icon);160versions->set_item_metadata(i, version_list[i]);161162if (i == c) {163versions->select(i);164current_version = version_list[i];165}166}167168if (version_list.is_empty()) {169for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {170stages[i]->set_disabled(true);171}172return;173}174175Ref<RDShaderSPIRV> bytecode = shader_file->get_spirv(current_version);176ERR_FAIL_COND(bytecode.is_null());177int first_valid = -1;178int current = -1;179for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {180Vector<uint8_t> bc = bytecode->get_stage_bytecode(RD::ShaderStage(i));181String error = bytecode->get_stage_compile_error(RD::ShaderStage(i));182bool disable = error.is_empty() && bc.is_empty();183stages[i]->set_disabled(disable);184if (!disable) {185if (stages[i]->is_pressed()) {186current = i;187}188first_valid = i;189}190}191192if (current == -1 && first_valid != -1) {193stages[first_valid]->set_pressed(true);194}195196_version_selected(0);197}198199void ShaderFileEditor::_notification(int p_what) {200switch (p_what) {201case NOTIFICATION_WM_WINDOW_FOCUS_IN: {202if (is_visible_in_tree() && shader_file.is_valid()) {203_update_options();204}205} break;206}207}208209void ShaderFileEditor::_editor_settings_changed() {210if (is_visible_in_tree() && shader_file.is_valid()) {211_update_options();212}213}214215void ShaderFileEditor::edit(const Ref<RDShaderFile> &p_shader) {216if (p_shader.is_null()) {217if (shader_file.is_valid()) {218shader_file->disconnect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));219}220return;221}222223if (shader_file == p_shader) {224return;225}226227shader_file = p_shader;228229if (shader_file.is_valid()) {230shader_file->connect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));231}232233_update_options();234}235236void ShaderFileEditor::_shader_changed() {237if (is_visible_in_tree()) {238_update_options();239}240}241242ShaderFileEditor *ShaderFileEditor::singleton = nullptr;243244ShaderFileEditor::ShaderFileEditor() {245singleton = this;246HSplitContainer *main_hs = memnew(HSplitContainer);247248add_child(main_hs);249250versions = memnew(ItemList);251versions->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);252versions->connect(SceneStringName(item_selected), callable_mp(this, &ShaderFileEditor::_version_selected));253versions->set_custom_minimum_size(Size2i(200 * EDSCALE, 0));254versions->set_theme_type_variation("TreeSecondary");255main_hs->add_child(versions);256257VBoxContainer *main_vb = memnew(VBoxContainer);258main_vb->set_h_size_flags(SIZE_EXPAND_FILL);259main_hs->add_child(main_vb);260261static const char *stage_str[RD::SHADER_STAGE_MAX] = {262"Vertex",263"Fragment",264"TessControl",265"TessEval",266"Compute"267};268269stage_hb = memnew(HBoxContainer);270main_vb->add_child(stage_hb);271272Ref<ButtonGroup> bg;273bg.instantiate();274for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {275Button *button = memnew(Button(stage_str[i]));276button->set_toggle_mode(true);277button->set_focus_mode(FOCUS_ACCESSIBILITY);278stage_hb->add_child(button);279stages[i] = button;280button->set_button_group(bg);281button->connect(SceneStringName(pressed), callable_mp(this, &ShaderFileEditor::_version_selected).bind(i));282}283284error_text = memnew(RichTextLabel);285error_text->set_v_size_flags(SIZE_EXPAND_FILL);286error_text->set_selection_enabled(true);287error_text->set_context_menu_enabled(true);288main_vb->add_child(error_text);289}290291void ShaderFileEditorPlugin::edit(Object *p_object) {292RDShaderFile *s = Object::cast_to<RDShaderFile>(p_object);293shader_editor->edit(s);294}295296bool ShaderFileEditorPlugin::handles(Object *p_object) const {297RDShaderFile *shader = Object::cast_to<RDShaderFile>(p_object);298return shader != nullptr;299}300301void ShaderFileEditorPlugin::make_visible(bool p_visible) {302if (p_visible) {303button->show();304EditorNode::get_bottom_panel()->make_item_visible(shader_editor);305306} else {307button->hide();308if (shader_editor->is_visible_in_tree()) {309EditorNode::get_bottom_panel()->hide_bottom_panel();310}311}312}313314ShaderFileEditorPlugin::ShaderFileEditorPlugin() {315shader_editor = memnew(ShaderFileEditor);316317shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);318button = 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")));319button->hide();320}321322323