resource_importer_shader_file.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "resource_importer_shader_file.h"
  2. #include "core/io/marshalls.h"
  3. #include "core/io/resource_saver.h"
  4. #include "core/os/file_access.h"
  5. #include "editor/editor_node.h"
  6. #include "editor/plugins/shader_file_editor_plugin.h"
  7. #include "servers/rendering/rendering_device_binds.h"
  8. String ResourceImporterShaderFile::get_importer_name() const {
  9. return "glsl";
  10. }
  11. String ResourceImporterShaderFile::get_visible_name() const {
  12. return "GLSL Shader File";
  13. }
  14. void ResourceImporterShaderFile::get_recognized_extensions(List<String> *p_extensions) const {
  15. p_extensions->push_back("glsl");
  16. }
  17. String ResourceImporterShaderFile::get_save_extension() const {
  18. return "res";
  19. }
  20. String ResourceImporterShaderFile::get_resource_type() const {
  21. return "RDShaderFile";
  22. }
  23. int ResourceImporterShaderFile::get_preset_count() const {
  24. return 0;
  25. }
  26. String ResourceImporterShaderFile::get_preset_name(int p_idx) const {
  27. return String();
  28. }
  29. void ResourceImporterShaderFile::get_import_options(List<ImportOption> *r_options, int p_preset) const {
  30. }
  31. bool ResourceImporterShaderFile::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  32. return true;
  33. }
  34. static String _include_function(const String &p_path, void *userpointer) {
  35. Error err;
  36. String *base_path = (String *)userpointer;
  37. String include = p_path;
  38. if (include.is_rel_path()) {
  39. include = base_path->plus_file(include);
  40. }
  41. FileAccessRef file_inc = FileAccess::open(include, FileAccess::READ, &err);
  42. if (err != OK) {
  43. return String();
  44. }
  45. return file_inc->get_as_utf8_string();
  46. }
  47. Error ResourceImporterShaderFile::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  48. /* STEP 1, Read shader code */
  49. Error err;
  50. FileAccessRef file = FileAccess::open(p_source_file, FileAccess::READ, &err);
  51. ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
  52. ERR_FAIL_COND_V(!file.operator->(), ERR_CANT_OPEN);
  53. String file_txt = file->get_as_utf8_string();
  54. Ref<RDShaderFile> shader_file;
  55. shader_file.instance();
  56. String base_path = p_source_file.get_base_dir();
  57. err = shader_file->parse_versions_from_text(file_txt, _include_function, &base_path);
  58. if (err != OK) {
  59. if (!ShaderFileEditor::singleton->is_visible_in_tree()) {
  60. EditorNode::get_singleton()->add_io_error(vformat(TTR("Error importing GLSL shader file: '%s'. Open the file in the filesystem dock in order to see the reason."), p_source_file));
  61. }
  62. }
  63. ResourceSaver::save(p_save_path + ".res", shader_file);
  64. return OK;
  65. }
  66. ResourceImporterShaderFile::ResourceImporterShaderFile() {
  67. }