shader_file_editor_plugin.cpp 9.9 KB

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