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/marshalls.h"
35#include "core/io/resource_saver.h"
36#include "editor/editor_node.h"
37#include "editor/plugins/shader_file_editor_plugin.h"
38#include "servers/rendering/rendering_device_binds.h"
39
40String ResourceImporterShaderFile::get_importer_name() const {
41 return "glsl";
42}
43
44String ResourceImporterShaderFile::get_visible_name() const {
45 return "GLSL Shader File";
46}
47
48void ResourceImporterShaderFile::get_recognized_extensions(List<String> *p_extensions) const {
49 p_extensions->push_back("glsl");
50}
51
52String ResourceImporterShaderFile::get_save_extension() const {
53 return "res";
54}
55
56String ResourceImporterShaderFile::get_resource_type() const {
57 return "RDShaderFile";
58}
59
60int ResourceImporterShaderFile::get_preset_count() const {
61 return 0;
62}
63
64String ResourceImporterShaderFile::get_preset_name(int p_idx) const {
65 return String();
66}
67
68void ResourceImporterShaderFile::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
69}
70
71bool ResourceImporterShaderFile::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
72 return true;
73}
74
75static String _include_function(const String &p_path, void *userpointer) {
76 Error err;
77
78 String *base_path = (String *)userpointer;
79
80 String include = p_path;
81 if (include.is_relative_path()) {
82 include = base_path->path_join(include);
83 }
84
85 Ref<FileAccess> file_inc = FileAccess::open(include, FileAccess::READ, &err);
86 if (err != OK) {
87 return String();
88 }
89 return file_inc->get_as_utf8_string();
90}
91
92Error 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) {
93 /* STEP 1, Read shader code */
94 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.");
95
96 Error err;
97 Ref<FileAccess> file = FileAccess::open(p_source_file, FileAccess::READ, &err);
98 ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
99 ERR_FAIL_COND_V(!file.operator->(), ERR_CANT_OPEN);
100
101 String file_txt = file->get_as_utf8_string();
102 Ref<RDShaderFile> shader_file;
103 shader_file.instantiate();
104 String base_path = p_source_file.get_base_dir();
105 err = shader_file->parse_versions_from_text(file_txt, "", _include_function, &base_path);
106
107 if (err != OK) {
108 if (!ShaderFileEditor::singleton->is_visible_in_tree()) {
109 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();
110 }
111 }
112
113 ResourceSaver::save(shader_file, p_save_path + ".res");
114
115 return OK;
116}
117
118ResourceImporterShaderFile::ResourceImporterShaderFile() {
119}
120