Debian Codegraph @

editor/import/resource_importer_shader_file.cpp

raw symbols SCIP
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
31 #include "resource_importer_shader_file.h"
32
33 #include "core/io/file_access.h"
34 #include "core/io/resource_saver.h"
35 #include "editor/editor_node.h"
36 #include "editor/shader/shader_file_editor_plugin.h"
37 #include "servers/rendering/rendering_device_binds.h"
38
39 String ResourceImporterShaderFile::get_importer_name() const {
40 return "glsl";
41 }
42
43 String ResourceImporterShaderFile::get_visible_name() const {
44 return "GLSL Shader File";
45 }
46
47 void ResourceImporterShaderFile::get_recognized_extensions(List<String> *p_extensions) const {
48 p_extensions->push_back("glsl");
49 }
50
51 String ResourceImporterShaderFile::get_save_extension() const {
52 return "res";
53 }
54
55 String ResourceImporterShaderFile::get_resource_type() const {
56 return "RDShaderFile";
57 }
58
59 int ResourceImporterShaderFile::get_preset_count() const {
60 return 0;
61 }
62
63 String ResourceImporterShaderFile::get_preset_name(int p_idx) const {
64 return String();
65 }
66
67 void ResourceImporterShaderFile::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
68 }
69
70 bool ResourceImporterShaderFile::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
71 return true;
72 }
73
74 static String _include_function(const String &p_path, void *userpointer) {
75 Error err;
76
77 String *base_path = (String *)userpointer;
78
79 String include = p_path;
80 if (include.is_relative_path()) {
81 include = base_path->path_join(include);
82 }
83
84 Ref<FileAccess> file_inc = FileAccess::open(include, FileAccess::READ, &err);
85 if (err != OK) {
86 return String();
87 }
88 return file_inc->get_as_utf8_string();
89 }
90
91 Error ResourceImporterShaderFile::import(ResourceUID::ID p_source_id, 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) {
92 Error err;
93 Ref<FileAccess> file = FileAccess::open(p_source_file, FileAccess::READ, &err);
94 ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
95 ERR_FAIL_COND_V(file.is_null(), ERR_CANT_OPEN);
96
97 String file_txt = file->get_as_utf8_string();
98 Ref<RDShaderFile> shader_file;
99 shader_file.instantiate();
100 String base_path = p_source_file.get_base_dir();
101 err = shader_file->parse_versions_from_text(file_txt, "", _include_function, &base_path);
102
103 if (err != OK) {
104 if (!ShaderFileEditor::singleton->is_visible_in_tree()) {
105 callable_mp_static(&EditorNode::add_io_error).call_deferred(vformat(TTR("Error importing GLSL shader file: '%s'. Open the file in the filesystem dock in order to see the reason."), p_source_file));
106 }
107 }
108
109 ResourceSaver::save(shader_file, p_save_path + ".res");
110
111 return OK;
112 }