| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  shader.cpp                                                           */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2021-01-01 20:13:46 +01:00
										 |  |  | /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "shader.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/os/file_access.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "scene/scene_string_names.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | #include "servers/visual/shader_language.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "servers/visual_server.h"
 | 
					
						
							|  |  |  | #include "texture.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Shader::Mode Shader::get_mode() const { | 
					
						
							| 
									
										
										
										
											2015-06-06 22:06:58 -03:00
										 |  |  | 	return mode; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void Shader::set_code(const String &p_code) { | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | 	String type = ShaderLanguage::get_shader_type(p_code); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (type == "canvas_item") { | 
					
						
							|  |  |  | 		mode = MODE_CANVAS_ITEM; | 
					
						
							|  |  |  | 	} else if (type == "particles") { | 
					
						
							|  |  |  | 		mode = MODE_PARTICLES; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		mode = MODE_SPATIAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	VisualServer::get_singleton()->shader_set_code(shader, p_code); | 
					
						
							|  |  |  | 	params_cache_dirty = true; | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	emit_changed(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | String Shader::get_code() const { | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 	_update_shader(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	return VisualServer::get_singleton()->shader_get_code(shader); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Shader::get_param_list(List<PropertyInfo> *p_params) const { | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 	_update_shader(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	List<PropertyInfo> local; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	VisualServer::get_singleton()->shader_get_param_list(shader, &local); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	params_cache.clear(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	params_cache_dirty = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (List<PropertyInfo>::Element *E = local.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		PropertyInfo pi = E->get(); | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 		if (default_textures.has(pi.name)) { //do not show default textures
 | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		pi.name = "shader_param/" + pi.name; | 
					
						
							|  |  |  | 		params_cache[pi.name] = E->get().name; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		if (p_params) { | 
					
						
							|  |  |  | 			//small little hack
 | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 			if (pi.type == Variant::_RID) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				pi.type = Variant::OBJECT; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			p_params->push_back(pi); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RID Shader::get_rid() const { | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 	_update_shader(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return shader; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void Shader::set_default_texture_param(const StringName &p_param, const Ref<Texture> &p_texture) { | 
					
						
							| 
									
										
										
										
											2015-01-07 01:45:46 -03:00
										 |  |  | 	if (p_texture.is_valid()) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		default_textures[p_param] = p_texture; | 
					
						
							|  |  |  | 		VS::get_singleton()->shader_set_default_texture_param(shader, p_param, p_texture->get_rid()); | 
					
						
							| 
									
										
										
										
											2015-01-07 01:45:46 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 		default_textures.erase(p_param); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		VS::get_singleton()->shader_set_default_texture_param(shader, p_param, RID()); | 
					
						
							| 
									
										
										
										
											2015-01-07 01:45:46 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	emit_changed(); | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Ref<Texture> Shader::get_default_texture_param(const StringName &p_param) const { | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (default_textures.has(p_param)) { | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 		return default_textures[p_param]; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 		return Ref<Texture>(); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void Shader::get_default_texture_param_list(List<StringName> *r_textures) const { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:20:36 +02:00
										 |  |  | 	for (const Map<StringName, Ref<Texture>>::Element *E = default_textures.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 		r_textures->push_back(E->key()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-14 21:54:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 23:14:10 -08:00
										 |  |  | void Shader::set_custom_defines(const String &p_defines) { | 
					
						
							| 
									
										
										
										
											2020-09-06 21:31:09 -07:00
										 |  |  | 	if (shader_custom_defines == p_defines) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-01-18 23:14:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 21:31:09 -07:00
										 |  |  | 	if (!shader_custom_defines.empty()) { | 
					
						
							|  |  |  | 		VS::get_singleton()->shader_remove_custom_define(shader, shader_custom_defines); | 
					
						
							| 
									
										
										
										
											2020-01-18 23:14:10 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 21:31:09 -07:00
										 |  |  | 	shader_custom_defines = p_defines; | 
					
						
							|  |  |  | 	VS::get_singleton()->shader_add_custom_define(shader, shader_custom_defines); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String Shader::get_custom_defines() const { | 
					
						
							|  |  |  | 	return shader_custom_defines; | 
					
						
							| 
									
										
										
										
											2020-01-18 23:14:10 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 21:54:59 -04:00
										 |  |  | bool Shader::is_text_shader() const { | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | bool Shader::has_param(const StringName &p_param) const { | 
					
						
							| 
									
										
										
										
											2020-06-04 17:41:10 +02:00
										 |  |  | 	return params_cache.has("shader_param/" + p_param); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | void Shader::_update_shader() const { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void Shader::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_code"), &Shader::get_code); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_default_texture_param", "param", "texture"), &Shader::set_default_texture_param); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_default_texture_param", "param"), &Shader::get_default_texture_param); | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 23:14:10 -08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_custom_defines", "custom_defines"), &Shader::set_custom_defines); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_custom_defines"), &Shader::get_custom_defines); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("has_param", "name"), &Shader::has_param); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 12:47:24 +01:00
										 |  |  | 	//ClassDB::bind_method(D_METHOD("get_param_list"),&Shader::get_fragment_code);
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_code", "get_code"); | 
					
						
							| 
									
										
										
										
											2020-05-18 01:07:38 -07:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_defines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_custom_defines", "get_custom_defines"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-20 17:45:01 +02:00
										 |  |  | 	BIND_ENUM_CONSTANT(MODE_SPATIAL); | 
					
						
							|  |  |  | 	BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM); | 
					
						
							|  |  |  | 	BIND_ENUM_CONSTANT(MODE_PARTICLES); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | Shader::Shader() { | 
					
						
							|  |  |  | 	mode = MODE_SPATIAL; | 
					
						
							|  |  |  | 	shader = VisualServer::get_singleton()->shader_create(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	params_cache_dirty = true; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Shader::~Shader() { | 
					
						
							|  |  |  | 	VisualServer::get_singleton()->free(shader); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | ////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RES ResourceFormatLoaderShader::load(const String &p_path, const String &p_original_path, Error *r_error) { | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (r_error) { | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 		*r_error = ERR_FILE_CANT_OPEN; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Ref<Shader> shader; | 
					
						
							|  |  |  | 	shader.instance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vector<uint8_t> buffer = FileAccess::get_file_as_array(p_path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String str; | 
					
						
							|  |  |  | 	str.parse_utf8((const char *)buffer.ptr(), buffer.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader->set_code(str); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (r_error) { | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 		*r_error = OK; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return shader; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const { | 
					
						
							| 
									
										
										
										
											2021-06-29 01:13:05 -04:00
										 |  |  | 	p_extensions->push_back("gdshader"); | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 	p_extensions->push_back("shader"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ResourceFormatLoaderShader::handles_type(const String &p_type) const { | 
					
						
							|  |  |  | 	return (p_type == "Shader"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const { | 
					
						
							|  |  |  | 	String el = p_path.get_extension().to_lower(); | 
					
						
							| 
									
										
										
										
											2021-06-29 01:13:05 -04:00
										 |  |  | 	if (el == "gdshader" || el == "shader") { | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 		return "Shader"; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 	return ""; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error ResourceFormatSaverShader::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { | 
					
						
							|  |  |  | 	Ref<Shader> shader = p_resource; | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String source = shader->get_code(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Error err; | 
					
						
							|  |  |  | 	FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(err, err, "Cannot save shader '" + p_path + "'."); | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	file->store_string(source); | 
					
						
							|  |  |  | 	if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { | 
					
						
							|  |  |  | 		memdelete(file); | 
					
						
							|  |  |  | 		return ERR_CANT_CREATE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	file->close(); | 
					
						
							|  |  |  | 	memdelete(file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourceFormatSaverShader::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { | 
					
						
							| 
									
										
										
										
											2018-09-14 21:54:59 -04:00
										 |  |  | 	if (const Shader *shader = Object::cast_to<Shader>(*p_resource)) { | 
					
						
							|  |  |  | 		if (shader->is_text_shader()) { | 
					
						
							| 
									
										
										
										
											2021-06-29 01:13:05 -04:00
										 |  |  | 			p_extensions->push_back("gdshader"); | 
					
						
							| 
									
										
										
										
											2018-09-14 21:54:59 -04:00
										 |  |  | 			p_extensions->push_back("shader"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-29 01:13:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | bool ResourceFormatSaverShader::recognize(const RES &p_resource) const { | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 	return p_resource->get_class_name() == "Shader"; //only shader, not inherited
 | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | } |