Debian Codegraph @

scene/resources/shader_include.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* shader_include.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 "shader_include.h"
32
33 #include "core/io/file_access.h"
34 #include "servers/rendering/shader_preprocessor.h"
35
36 void ShaderInclude::_dependency_changed() {
37 emit_changed();
38 }
39
40 void ShaderInclude::set_code(const String &p_code) {
41 code = p_code;
42
43 for (const Ref<ShaderInclude> &E : dependencies) {
44 E->disconnect_changed(callable_mp(this, &ShaderInclude::_dependency_changed));
45 }
46
47 {
48 String path = get_path();
49 if (path.is_empty()) {
50 path = include_path;
51 }
52
53 String pp_code;
54 HashSet<Ref<ShaderInclude>> new_dependencies;
55 ShaderPreprocessor preprocessor;
56 Error result = preprocessor.preprocess(p_code, path, pp_code, nullptr, nullptr, nullptr, &new_dependencies);
57 if (result == OK) {
58 // This ensures previous include resources are not freed and then re-loaded during parse (which would make compiling slower)
59 dependencies = new_dependencies;
60 }
61 }
62
63 for (const Ref<ShaderInclude> &E : dependencies) {
64 E->connect_changed(callable_mp(this, &ShaderInclude::_dependency_changed));
65 }
66
67 emit_changed();
68 }
69
70 String ShaderInclude::get_code() const {
71 return code;
72 }
73
74 void ShaderInclude::set_include_path(const String &p_path) {
75 include_path = p_path;
76 }
77
78 void ShaderInclude::_bind_methods() {
79 ClassDB::bind_method(D_METHOD("set_code", "code"), &ShaderInclude::set_code);
80 ClassDB::bind_method(D_METHOD("get_code"), &ShaderInclude::get_code);
81
82 ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_code", "get_code");
83 }
84
85 // ResourceFormatLoaderShaderInclude
86
87 Ref<Resource> ResourceFormatLoaderShaderInclude::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
88 if (r_error) {
89 *r_error = ERR_FILE_CANT_OPEN;
90 }
91
92 Error error = OK;
93 Vector<uint8_t> buffer = FileAccess::get_file_as_bytes(p_path, &error);
94 ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot load shader include: " + p_path);
95
96 String str;
97 if (buffer.size() > 0) {
98 error = str.append_utf8((const char *)buffer.ptr(), buffer.size());
99 ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot parse shader include: " + p_path);
100 }
101
102 Ref<ShaderInclude> shader_inc;
103 shader_inc.instantiate();
104
105 shader_inc->set_include_path(p_path);
106 shader_inc->set_code(str);
107
108 if (r_error) {
109 *r_error = OK;
110 }
111
112 return shader_inc;
113 }
114
115 void ResourceFormatLoaderShaderInclude::get_recognized_extensions(List<String> *p_extensions) const {
116 p_extensions->push_back("gdshaderinc");
117 }
118
119 bool ResourceFormatLoaderShaderInclude::handles_type(const String &p_type) const {
120 return (p_type == "ShaderInclude");
121 }
122
123 String ResourceFormatLoaderShaderInclude::get_resource_type(const String &p_path) const {
124 if (p_path.has_extension("gdshaderinc")) {
125 return "ShaderInclude";
126 }
127 return "";
128 }
129
130 // ResourceFormatSaverShaderInclude
131
132 Error ResourceFormatSaverShaderInclude::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
133 Ref<ShaderInclude> shader_inc = p_resource;
134 ERR_FAIL_COND_V(shader_inc.is_null(), ERR_INVALID_PARAMETER);
135
136 String source = shader_inc->get_code();
137
138 Error error;
139 Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &error);
140
141 ERR_FAIL_COND_V_MSG(error, error, "Cannot save shader include '" + p_path + "'.");
142
143 file->store_string(source);
144 if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
145 return ERR_CANT_CREATE;
146 }
147
148 return OK;
149 }
150
151 void ResourceFormatSaverShaderInclude::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
152 const ShaderInclude *shader_inc = Object::cast_to<ShaderInclude>(*p_resource);
153 if (shader_inc != nullptr) {
154 p_extensions->push_back("gdshaderinc");
155 }
156 }
157
158 bool ResourceFormatSaverShaderInclude::recognize(const Ref<Resource> &p_resource) const {
159 return p_resource->get_class_name() == "ShaderInclude"; //only shader, not inherited
160 }