| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  shader.cpp                                                            */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* 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"
 | 
					
						
							| 
									
										
										
										
											2020-11-09 14:53:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-11 14:51:48 +02:00
										 |  |  | #include "core/io/file_access.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "scene/scene_string_names.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | #include "servers/rendering/shader_language.h"
 | 
					
						
							| 
									
										
										
										
											2022-06-29 11:31:18 +02:00
										 |  |  | #include "servers/rendering/shader_preprocessor.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | #include "servers/rendering_server.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "texture.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Shader::Mode Shader::get_mode() const { | 
					
						
							| 
									
										
										
										
											2015-06-06 22:06:58 -03:00
										 |  |  | 	return mode; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-08 13:39:16 +03:00
										 |  |  | void Shader::_dependency_changed() { | 
					
						
							| 
									
										
										
										
											2023-01-22 22:19:52 +02:00
										 |  |  | 	// Preprocess and compile the code again because a dependency has changed. It also calls emit_changed() for us.
 | 
					
						
							|  |  |  | 	_recompile(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-03-08 13:39:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 22:19:52 +02:00
										 |  |  | void Shader::_recompile() { | 
					
						
							|  |  |  | 	set_code(get_code()); | 
					
						
							| 
									
										
										
										
											2022-03-08 13:39:16 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 11:31:18 +02:00
										 |  |  | void Shader::set_path(const String &p_path, bool p_take_over) { | 
					
						
							|  |  |  | 	Resource::set_path(p_path, p_take_over); | 
					
						
							|  |  |  | 	RS::get_singleton()->shader_set_path_hint(shader, p_path); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-03-08 13:39:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 14:35:20 +02:00
										 |  |  | void Shader::set_include_path(const String &p_path) { | 
					
						
							|  |  |  | 	// Used only if the shader does not have a resource path set,
 | 
					
						
							|  |  |  | 	// for example during loading stage or when created by code.
 | 
					
						
							|  |  |  | 	include_path = p_path; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 11:31:18 +02:00
										 |  |  | void Shader::set_code(const String &p_code) { | 
					
						
							| 
									
										
										
										
											2022-03-08 13:39:16 +03:00
										 |  |  | 	for (Ref<ShaderInclude> E : include_dependencies) { | 
					
						
							|  |  |  | 		E->disconnect(SNAME("changed"), callable_mp(this, &Shader::_dependency_changed)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 11:31:18 +02:00
										 |  |  | 	String type = ShaderLanguage::get_shader_type(p_code); | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (type == "canvas_item") { | 
					
						
							|  |  |  | 		mode = MODE_CANVAS_ITEM; | 
					
						
							|  |  |  | 	} else if (type == "particles") { | 
					
						
							|  |  |  | 		mode = MODE_PARTICLES; | 
					
						
							| 
									
										
										
										
											2019-09-15 19:58:38 +10:00
										 |  |  | 	} else if (type == "sky") { | 
					
						
							|  |  |  | 		mode = MODE_SKY; | 
					
						
							| 
									
										
										
										
											2021-10-03 04:28:55 -07:00
										 |  |  | 	} else if (type == "fog") { | 
					
						
							|  |  |  | 		mode = MODE_FOG; | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		mode = MODE_SPATIAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 11:31:18 +02:00
										 |  |  | 	code = p_code; | 
					
						
							|  |  |  | 	String pp_code = p_code; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	HashSet<Ref<ShaderInclude>> new_include_dependencies; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2023-01-27 14:35:20 +02:00
										 |  |  | 		String path = get_path(); | 
					
						
							|  |  |  | 		if (path.is_empty()) { | 
					
						
							|  |  |  | 			path = include_path; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-06-29 11:31:18 +02:00
										 |  |  | 		// Preprocessor must run here and not in the server because:
 | 
					
						
							|  |  |  | 		// 1) Need to keep track of include dependencies at resource level
 | 
					
						
							|  |  |  | 		// 2) Server does not do interaction with Resource filetypes, this is a scene level feature.
 | 
					
						
							|  |  |  | 		ShaderPreprocessor preprocessor; | 
					
						
							| 
									
										
										
										
											2023-01-27 14:35:20 +02:00
										 |  |  | 		preprocessor.preprocess(p_code, path, pp_code, nullptr, nullptr, nullptr, &new_include_dependencies); | 
					
						
							| 
									
										
										
										
											2022-06-29 11:31:18 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// This ensures previous include resources are not freed and then re-loaded during parse (which would make compiling slower)
 | 
					
						
							|  |  |  | 	include_dependencies = new_include_dependencies; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (Ref<ShaderInclude> E : include_dependencies) { | 
					
						
							|  |  |  | 		E->connect(SNAME("changed"), callable_mp(this, &Shader::_dependency_changed)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	RenderingServer::get_singleton()->shader_set_code(shader, pp_code); | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2022-06-29 11:31:18 +02:00
										 |  |  | 	return code; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 19:09:09 +02:00
										 |  |  | void Shader::get_shader_uniform_list(List<PropertyInfo> *p_params, bool p_get_groups) const { | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 	_update_shader(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	List<PropertyInfo> local; | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | 	RenderingServer::get_singleton()->get_shader_parameter_list(shader, &local); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 23:45:57 -04:00
										 |  |  | 	for (PropertyInfo &pi : local) { | 
					
						
							| 
									
										
										
										
											2022-07-13 11:31:27 +03:00
										 |  |  | 		bool is_group = pi.usage == PROPERTY_USAGE_GROUP || pi.usage == PROPERTY_USAGE_SUBGROUP; | 
					
						
							|  |  |  | 		if (!p_get_groups && is_group) { | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-07-13 11:31:27 +03:00
										 |  |  | 		if (!is_group) { | 
					
						
							|  |  |  | 			if (default_textures.has(pi.name)) { //do not show default textures
 | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		if (p_params) { | 
					
						
							|  |  |  | 			//small little hack
 | 
					
						
							| 
									
										
										
										
											2020-11-09 14:53:05 +01:00
										 |  |  | 			if (pi.type == Variant::RID) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				pi.type = Variant::OBJECT; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | void Shader::set_default_texture_parameter(const StringName &p_name, const Ref<Texture2D> &p_texture, int p_index) { | 
					
						
							| 
									
										
										
										
											2015-01-07 01:45:46 -03:00
										 |  |  | 	if (p_texture.is_valid()) { | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | 		if (!default_textures.has(p_name)) { | 
					
						
							|  |  |  | 			default_textures[p_name] = HashMap<int, Ref<Texture2D>>(); | 
					
						
							| 
									
										
										
										
											2021-10-17 14:38:26 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | 		default_textures[p_name][p_index] = p_texture; | 
					
						
							|  |  |  | 		RS::get_singleton()->shader_set_default_texture_parameter(shader, p_name, p_texture->get_rid(), p_index); | 
					
						
							| 
									
										
										
										
											2015-01-07 01:45:46 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | 		if (default_textures.has(p_name) && default_textures[p_name].has(p_index)) { | 
					
						
							|  |  |  | 			default_textures[p_name].erase(p_index); | 
					
						
							| 
									
										
										
										
											2021-10-17 14:38:26 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | 			if (default_textures[p_name].is_empty()) { | 
					
						
							|  |  |  | 				default_textures.erase(p_name); | 
					
						
							| 
									
										
										
										
											2021-10-17 14:38:26 +03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | 		RS::get_singleton()->shader_set_default_texture_parameter(shader, p_name, RID(), p_index); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | Ref<Texture2D> Shader::get_default_texture_parameter(const StringName &p_name, int p_index) const { | 
					
						
							|  |  |  | 	if (default_textures.has(p_name) && default_textures[p_name].has(p_index)) { | 
					
						
							|  |  |  | 		return default_textures[p_name][p_index]; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-17 14:38:26 +03:00
										 |  |  | 	return Ref<Texture2D>(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | void Shader::get_default_texture_parameter_list(List<StringName> *r_textures) const { | 
					
						
							| 
									
										
										
										
											2022-05-13 15:04:37 +02:00
										 |  |  | 	for (const KeyValue<StringName, HashMap<int, Ref<Texture2D>>> &E : default_textures) { | 
					
						
							| 
									
										
										
										
											2021-08-09 14:13:42 -06:00
										 |  |  | 		r_textures->push_back(E.key); | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-14 21:54:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool Shader::is_text_shader() const { | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 10:24:45 +01:00
										 |  |  | void Shader::_update_shader() const { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 10:24:45 +01:00
										 |  |  | Array Shader::_get_shader_uniform_list(bool p_get_groups) { | 
					
						
							|  |  |  | 	List<PropertyInfo> uniform_list; | 
					
						
							|  |  |  | 	get_shader_uniform_list(&uniform_list, p_get_groups); | 
					
						
							|  |  |  | 	Array ret; | 
					
						
							|  |  |  | 	for (const PropertyInfo &pi : uniform_list) { | 
					
						
							|  |  |  | 		ret.push_back(pi.operator Dictionary()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ret; | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void Shader::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-02-13 12:47:24 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 12:47:24 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 12:22:43 +03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_default_texture_parameter", "name", "texture", "index"), &Shader::set_default_texture_parameter, DEFVAL(0)); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_default_texture_parameter", "name", "index"), &Shader::get_default_texture_parameter, DEFVAL(0)); | 
					
						
							| 
									
										
										
										
											2014-12-21 11:42:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 10:24:45 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_shader_uniform_list", "get_groups"), &Shader::_get_shader_uniform_list, DEFVAL(false)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-03 23:06:17 +01:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_code", "get_code"); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2019-09-15 19:58:38 +10:00
										 |  |  | 	BIND_ENUM_CONSTANT(MODE_SKY); | 
					
						
							| 
									
										
										
										
											2021-10-03 04:28:55 -07:00
										 |  |  | 	BIND_ENUM_CONSTANT(MODE_FOG); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | Shader::Shader() { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 	shader = RenderingServer::get_singleton()->shader_create(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Shader::~Shader() { | 
					
						
							| 
									
										
										
										
											2022-12-12 12:42:37 -05:00
										 |  |  | 	ERR_FAIL_NULL(RenderingServer::get_singleton()); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 	RenderingServer::get_singleton()->free(shader); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | ////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 01:43:50 +02:00
										 |  |  | 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) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (r_error) { | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 		*r_error = ERR_FILE_CANT_OPEN; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Ref<Shader> shader; | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 	shader.instantiate(); | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-21 15:04:01 +02:00
										 |  |  | 	Vector<uint8_t> buffer = FileAccess::get_file_as_bytes(p_path); | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	String str; | 
					
						
							|  |  |  | 	str.parse_utf8((const char *)buffer.ptr(), buffer.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 14:35:20 +02:00
										 |  |  | 	shader->set_include_path(p_path); | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 	shader->set_code(str); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (r_error) { | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 		*r_error = OK; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return shader; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const { | 
					
						
							| 
									
										
										
										
											2021-03-24 19:50:41 +01:00
										 |  |  | 	p_extensions->push_back("gdshader"); | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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-03-24 19:50:41 +01:00
										 |  |  | 	if (el == "gdshader") { | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 		return "Shader"; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 	return ""; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 01:33:42 +02:00
										 |  |  | Error ResourceFormatSaverShader::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) { | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 	Ref<Shader> shader = p_resource; | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String source = shader->get_code(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Error err; | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err); | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 		return ERR_CANT_CREATE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 01:43:50 +02:00
										 |  |  | void ResourceFormatSaverShader::get_recognized_extensions(const Ref<Resource> &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-03-24 19:50:41 +01:00
										 |  |  | 			p_extensions->push_back("gdshader"); | 
					
						
							| 
									
										
										
										
											2018-09-14 21:54:59 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-12-26 13:52:09 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 01:43:50 +02:00
										 |  |  | bool ResourceFormatSaverShader::recognize(const Ref<Resource> &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
										 |  |  | } |