shader_file_editor_plugin.cpp 10 KB

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