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