resource_importer_shader_file.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**************************************************************************/
  2. /* resource_importer_shader_file.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 "resource_importer_shader_file.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/io/resource_saver.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/plugins/shader_file_editor_plugin.h"
  36. #include "servers/rendering/rendering_device_binds.h"
  37. String ResourceImporterShaderFile::get_importer_name() const {
  38. return "glsl";
  39. }
  40. String ResourceImporterShaderFile::get_visible_name() const {
  41. return "GLSL Shader File";
  42. }
  43. void ResourceImporterShaderFile::get_recognized_extensions(List<String> *p_extensions) const {
  44. p_extensions->push_back("glsl");
  45. }
  46. String ResourceImporterShaderFile::get_save_extension() const {
  47. return "res";
  48. }
  49. String ResourceImporterShaderFile::get_resource_type() const {
  50. return "RDShaderFile";
  51. }
  52. int ResourceImporterShaderFile::get_preset_count() const {
  53. return 0;
  54. }
  55. String ResourceImporterShaderFile::get_preset_name(int p_idx) const {
  56. return String();
  57. }
  58. void ResourceImporterShaderFile::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  59. }
  60. bool ResourceImporterShaderFile::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  61. return true;
  62. }
  63. static String _include_function(const String &p_path, void *userpointer) {
  64. Error err;
  65. String *base_path = (String *)userpointer;
  66. String include = p_path;
  67. if (include.is_relative_path()) {
  68. include = base_path->path_join(include);
  69. }
  70. Ref<FileAccess> file_inc = FileAccess::open(include, FileAccess::READ, &err);
  71. if (err != OK) {
  72. return String();
  73. }
  74. return file_inc->get_as_utf8_string();
  75. }
  76. Error ResourceImporterShaderFile::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  77. /* STEP 1, Read shader code */
  78. ERR_FAIL_COND_V_EDMSG((OS::get_singleton()->get_current_rendering_method() == "gl_compatibility"), ERR_UNAVAILABLE, "Cannot import custom .glsl shaders when using the gl_compatibility rendering_method. Please switch to the forward_plus or mobile rendering methods to use custom shaders.");
  79. Error err;
  80. Ref<FileAccess> file = FileAccess::open(p_source_file, FileAccess::READ, &err);
  81. ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
  82. ERR_FAIL_COND_V(!file.is_valid(), ERR_CANT_OPEN);
  83. String file_txt = file->get_as_utf8_string();
  84. Ref<RDShaderFile> shader_file;
  85. shader_file.instantiate();
  86. String base_path = p_source_file.get_base_dir();
  87. err = shader_file->parse_versions_from_text(file_txt, "", _include_function, &base_path);
  88. if (err != OK) {
  89. if (!ShaderFileEditor::singleton->is_visible_in_tree()) {
  90. callable_mp_static(&EditorNode::add_io_error).bind(vformat(TTR("Error importing GLSL shader file: '%s'. Open the file in the filesystem dock in order to see the reason."), p_source_file)).call_deferred();
  91. }
  92. }
  93. ResourceSaver::save(shader_file, p_save_path + ".res");
  94. return OK;
  95. }
  96. ResourceImporterShaderFile::ResourceImporterShaderFile() {
  97. }