| 1 | /**************************************************************************/ |
| 2 | /* shader.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.h" |
| 32 | #include "shader.compat.inc" |
| 33 | |
| 34 | #include "core/io/file_access.h" |
| 35 | #include "scene/main/scene_tree.h" |
| 36 | #include "servers/rendering/rendering_server.h" |
| 37 | #include "servers/rendering/shader_language.h" |
| 38 | #include "servers/rendering/shader_preprocessor.h" |
| 39 | #include "texture.h" |
| 40 | |
| 41 | #ifdef TOOLS_ENABLED |
| 42 | #include "editor/doc/editor_help.h" |
| 43 | |
| 44 | #include "modules/modules_enabled.gen.h" // For regex. |
| 45 | #ifdef MODULE_REGEX_ENABLED |
| 46 | #include "modules/regex/regex.h" |
| 47 | #endif |
| 48 | #endif |
| 49 | |
| 50 | Shader::Mode Shader::get_mode() const { |
| 51 | return mode; |
| 52 | } |
| 53 | |
| 54 | void Shader::_check_shader_rid() const { |
| 55 | MutexLock lock(shader_rid_mutex); |
| 56 | if (shader_rid.is_null() && !preprocessed_code.is_empty()) { |
| 57 | shader_rid = RenderingServer::get_singleton()->shader_create_from_code(preprocessed_code, get_path()); |
| 58 | preprocessed_code = String(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void Shader::_dependency_changed() { |
| 63 | // Preprocess and compile the code again because a dependency has changed. It also calls emit_changed() for us. |
| 64 | _recompile(); |
| 65 | } |
| 66 | |
| 67 | void Shader::_recompile() { |
| 68 | set_code(get_code()); |
| 69 | } |
| 70 | |
| 71 | void Shader::set_path(const String &p_path, bool p_take_over) { |
| 72 | Resource::set_path(p_path, p_take_over); |
| 73 | |
| 74 | if (shader_rid.is_valid()) { |
| 75 | RS::get_singleton()->shader_set_path_hint(shader_rid, p_path); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void Shader::set_include_path(const String &p_path) { |
| 80 | // Used only if the shader does not have a resource path set, |
| 81 | // for example during loading stage or when created by code. |
| 82 | include_path = p_path; |
| 83 | } |
| 84 | |
| 85 | void Shader::set_code(const String &p_code) { |
| 86 | for (const Ref<ShaderInclude> &E : include_dependencies) { |
| 87 | E->disconnect_changed(callable_mp(this, &Shader::_dependency_changed)); |
| 88 | } |
| 89 | |
| 90 | code = p_code; |
| 91 | preprocessed_code = p_code; |
| 92 | |
| 93 | { |
| 94 | String path = get_path(); |
| 95 | if (path.is_empty()) { |
| 96 | path = include_path; |
| 97 | } |
| 98 | // Preprocessor must run here and not in the server because: |
| 99 | // 1) Need to keep track of include dependencies at resource level |
| 100 | // 2) Server does not do interaction with Resource filetypes, this is a scene level feature. |
| 101 | HashSet<Ref<ShaderInclude>> new_include_dependencies; |
| 102 | ShaderPreprocessor preprocessor; |
| 103 | Error result = preprocessor.preprocess(p_code, path, preprocessed_code, nullptr, nullptr, nullptr, &new_include_dependencies); |
| 104 | if (result == OK) { |
| 105 | // This ensures previous include resources are not freed and then re-loaded during parse (which would make compiling slower) |
| 106 | include_dependencies = new_include_dependencies; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Try to get the shader type from the final, fully preprocessed shader code. |
| 111 | String type = ShaderLanguage::get_shader_type(preprocessed_code); |
| 112 | |
| 113 | if (type == "canvas_item") { |
| 114 | mode = MODE_CANVAS_ITEM; |
| 115 | } else if (type == "particles") { |
| 116 | mode = MODE_PARTICLES; |
| 117 | } else if (type == "sky") { |
| 118 | mode = MODE_SKY; |
| 119 | } else if (type == "fog") { |
| 120 | mode = MODE_FOG; |
| 121 | } else { |
| 122 | mode = MODE_SPATIAL; |
| 123 | } |
| 124 | |
| 125 | for (const Ref<ShaderInclude> &E : include_dependencies) { |
| 126 | E->connect_changed(callable_mp(this, &Shader::_dependency_changed)); |
| 127 | } |
| 128 | |
| 129 | if (shader_rid.is_valid()) { |
| 130 | RenderingServer::get_singleton()->shader_set_code(shader_rid, preprocessed_code); |
| 131 | preprocessed_code = String(); |
| 132 | } |
| 133 | |
| 134 | emit_changed(); |
| 135 | } |
| 136 | |
| 137 | String Shader::get_code() const { |
| 138 | _update_shader(); |
| 139 | return code; |
| 140 | } |
| 141 | |
| 142 | void Shader::inspect_native_shader_code() { |
| 143 | SceneTree *st = SceneTree::get_singleton(); |
| 144 | RID _shader = get_rid(); |
| 145 | if (st && _shader.is_valid()) { |
| 146 | st->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_native_shader_source_visualizer", "_inspect_shader", _shader); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void Shader::get_shader_uniform_list(List<PropertyInfo> *p_params, bool p_get_groups) const { |
| 151 | _update_shader(); |
| 152 | _check_shader_rid(); |
| 153 | |
| 154 | List<PropertyInfo> local; |
| 155 | RenderingServer::get_singleton()->get_shader_parameter_list(shader_rid, &local); |
| 156 | |
| 157 | #ifdef TOOLS_ENABLED |
| 158 | DocData::ClassDoc class_doc; |
| 159 | bool generate_doc = Engine::get_singleton()->is_editor_hint() && !get_path().is_empty(); |
| 160 | if (generate_doc) { |
| 161 | class_doc.name = get_path().trim_prefix("res://").quote(); |
| 162 | class_doc.is_script_doc = true; |
| 163 | class_doc.inherits = "Shader"; |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | for (PropertyInfo &pi : local) { |
| 168 | bool is_group = pi.usage == PROPERTY_USAGE_GROUP || pi.usage == PROPERTY_USAGE_SUBGROUP; |
| 169 | if (!p_get_groups && is_group) { |
| 170 | continue; |
| 171 | } |
| 172 | if (!is_group) { |
| 173 | if (default_textures.has(pi.name)) { //do not show default textures |
| 174 | continue; |
| 175 | } |
| 176 | } |
| 177 | if (p_params) { |
| 178 | //small little hack |
| 179 | if (pi.type == Variant::RID) { |
| 180 | pi.type = Variant::OBJECT; |
| 181 | } |
| 182 | #ifdef TOOLS_ENABLED |
| 183 | if (generate_doc) { |
| 184 | DocData::PropertyDoc prop_doc; |
| 185 | prop_doc.name = "shader_parameter/" + pi.name; |
| 186 | const RegEx pattern("/\\*\\*\\s([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/\\s*uniform\\s+\\w+\\s+" + pi.name + "(?=[\\s:;=])"); |
| 187 | Ref<RegExMatch> pattern_ref = pattern.search(code); |
| 188 | if (pattern_ref.is_valid()) { |
| 189 | RegExMatch *match = pattern_ref.ptr(); |
| 190 | const RegEx pattern_tip("\\/\\*\\*([\\s\\S]*?)\\*/"); |
| 191 | Ref<RegExMatch> pattern_tip_ref = pattern_tip.search(match->get_string(0)); |
| 192 | RegExMatch *match_tip = pattern_tip_ref.ptr(); |
| 193 | const RegEx pattern_stripped("\\n\\s*\\*\\s*"); |
| 194 | prop_doc.description = pattern_stripped.sub(match_tip->get_string(1), "\n", true); |
| 195 | |
| 196 | pi.class_name = class_doc.name; |
| 197 | class_doc.properties.push_back(prop_doc); |
| 198 | } |
| 199 | } |
| 200 | #endif |
| 201 | p_params->push_back(pi); |
| 202 | } |
| 203 | } |
| 204 | #ifdef TOOLS_ENABLED |
| 205 | if (generate_doc && class_doc.properties.size() > 0) { |
| 206 | EditorHelp::add_doc(class_doc); |
| 207 | } |
| 208 | #endif |
| 209 | } |
| 210 | |
| 211 | RID Shader::get_rid() const { |
| 212 | _update_shader(); |
| 213 | _check_shader_rid(); |
| 214 | |
| 215 | return shader_rid; |
| 216 | } |
| 217 | |
| 218 | void Shader::set_default_texture_parameter(const StringName &p_name, const Ref<Texture> &p_texture, int p_index) { |
| 219 | _check_shader_rid(); |
| 220 | |
| 221 | if (p_texture.is_valid()) { |
| 222 | if (!default_textures.has(p_name)) { |
| 223 | default_textures[p_name] = HashMap<int, Ref<Texture>>(); |
| 224 | } |
| 225 | default_textures[p_name][p_index] = p_texture; |
| 226 | RS::get_singleton()->shader_set_default_texture_parameter(shader_rid, p_name, p_texture->get_rid(), p_index); |
| 227 | } else { |
| 228 | if (default_textures.has(p_name) && default_textures[p_name].has(p_index)) { |
| 229 | default_textures[p_name].erase(p_index); |
| 230 | |
| 231 | if (default_textures[p_name].is_empty()) { |
| 232 | default_textures.erase(p_name); |
| 233 | } |
| 234 | } |
| 235 | RS::get_singleton()->shader_set_default_texture_parameter(shader_rid, p_name, RID(), p_index); |
| 236 | } |
| 237 | |
| 238 | emit_changed(); |
| 239 | } |
| 240 | |
| 241 | Ref<Texture> Shader::get_default_texture_parameter(const StringName &p_name, int p_index) const { |
| 242 | if (default_textures.has(p_name) && default_textures[p_name].has(p_index)) { |
| 243 | return default_textures[p_name][p_index]; |
| 244 | } |
| 245 | return Ref<Texture2D>(); |
| 246 | } |
| 247 | |
| 248 | void Shader::get_default_texture_parameter_list(List<StringName> *r_textures) const { |
| 249 | for (const KeyValue<StringName, HashMap<int, Ref<Texture>>> &E : default_textures) { |
| 250 | r_textures->push_back(E.key); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | bool Shader::is_text_shader() const { |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | void Shader::_update_shader() const { |
| 259 | // Base implementation does nothing. |
| 260 | } |
| 261 | |
| 262 | Array Shader::_get_shader_uniform_list(bool p_get_groups) { |
| 263 | List<PropertyInfo> uniform_list; |
| 264 | get_shader_uniform_list(&uniform_list, p_get_groups); |
| 265 | Array ret; |
| 266 | for (const PropertyInfo &pi : uniform_list) { |
| 267 | ret.push_back(pi.operator Dictionary()); |
| 268 | } |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | void Shader::_bind_methods() { |
| 273 | ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode); |
| 274 | |
| 275 | ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code); |
| 276 | ClassDB::bind_method(D_METHOD("get_code"), &Shader::get_code); |
| 277 | |
| 278 | ClassDB::bind_method(D_METHOD("set_default_texture_parameter", "name", "texture", "index"), &Shader::set_default_texture_parameter, DEFVAL(0)); |
| 279 | ClassDB::bind_method(D_METHOD("get_default_texture_parameter", "name", "index"), &Shader::get_default_texture_parameter, DEFVAL(0)); |
| 280 | |
| 281 | ClassDB::bind_method(D_METHOD("get_shader_uniform_list", "get_groups"), &Shader::_get_shader_uniform_list, DEFVAL(false)); |
| 282 | |
| 283 | ClassDB::bind_method(D_METHOD("inspect_native_shader_code"), &Shader::inspect_native_shader_code); |
| 284 | ClassDB::set_method_flags(get_class_static(), StringName("inspect_native_shader_code"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); |
| 285 | |
| 286 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_code", "get_code"); |
| 287 | |
| 288 | BIND_ENUM_CONSTANT(MODE_SPATIAL); |
| 289 | BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM); |
| 290 | BIND_ENUM_CONSTANT(MODE_PARTICLES); |
| 291 | BIND_ENUM_CONSTANT(MODE_SKY); |
| 292 | BIND_ENUM_CONSTANT(MODE_FOG); |
| 293 | } |
| 294 | |
| 295 | Shader::Shader() { |
| 296 | // Shader RID will be empty until it is required. |
| 297 | } |
| 298 | |
| 299 | Shader::~Shader() { |
| 300 | if (shader_rid.is_valid()) { |
| 301 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 302 | RenderingServer::get_singleton()->free_rid(shader_rid); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | //////////// |
| 307 | |
| 308 | Ref<Resource> ResourceFormatLoaderShader::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) { |
| 309 | if (r_error) { |
| 310 | *r_error = ERR_FILE_CANT_OPEN; |
| 311 | } |
| 312 | |
| 313 | Error error = OK; |
| 314 | Vector<uint8_t> buffer = FileAccess::get_file_as_bytes(p_path, &error); |
| 315 | ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot load shader: " + p_path); |
| 316 | |
| 317 | String str; |
| 318 | if (buffer.size() > 0) { |
| 319 | error = str.append_utf8((const char *)buffer.ptr(), buffer.size()); |
| 320 | ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot parse shader: " + p_path); |
| 321 | } |
| 322 | |
| 323 | Ref<Shader> shader; |
| 324 | shader.instantiate(); |
| 325 | |
| 326 | shader->set_include_path(p_path); |
| 327 | shader->set_code(str); |
| 328 | |
| 329 | if (r_error) { |
| 330 | *r_error = OK; |
| 331 | } |
| 332 | |
| 333 | return shader; |
| 334 | } |
| 335 | |
| 336 | void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const { |
| 337 | p_extensions->push_back("gdshader"); |
| 338 | } |
| 339 | |
| 340 | bool ResourceFormatLoaderShader::handles_type(const String &p_type) const { |
| 341 | return (p_type == "Shader"); |
| 342 | } |
| 343 | |
| 344 | String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const { |
| 345 | if (p_path.has_extension("gdshader")) { |
| 346 | return "Shader"; |
| 347 | } |
| 348 | return ""; |
| 349 | } |
| 350 | |
| 351 | Error ResourceFormatSaverShader::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) { |
| 352 | Ref<Shader> shader = p_resource; |
| 353 | ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER); |
| 354 | |
| 355 | String source = shader->get_code(); |
| 356 | |
| 357 | Error err; |
| 358 | Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err); |
| 359 | |
| 360 | ERR_FAIL_COND_V_MSG(err, err, "Cannot save shader '" + p_path + "'."); |
| 361 | |
| 362 | file->store_string(source); |
| 363 | if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { |
| 364 | return ERR_CANT_CREATE; |
| 365 | } |
| 366 | |
| 367 | return OK; |
| 368 | } |
| 369 | |
| 370 | void ResourceFormatSaverShader::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const { |
| 371 | if (const Shader *shader = Object::cast_to<Shader>(*p_resource)) { |
| 372 | if (shader->is_text_shader()) { |
| 373 | p_extensions->push_back("gdshader"); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | bool ResourceFormatSaverShader::recognize(const Ref<Resource> &p_resource) const { |
| 379 | return p_resource->get_class_name() == "Shader"; //only shader, not inherited |
| 380 | } |