| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /*  shader_gles3.cpp                                                     */ | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							| 
									
										
										
										
											2017-04-08 00:11:42 +02:00
										 |  |  | /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | #include "shader_gles3.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "print_string.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //#define DEBUG_OPENGL
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef DEBUG_OPENGL
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #define DEBUG_TEST_ERROR(m_section)                                         \
 | 
					
						
							|  |  |  | 	{                                                                       \ | 
					
						
							|  |  |  | 		uint32_t err = glGetError();                                        \ | 
					
						
							|  |  |  | 		if (err) {                                                          \ | 
					
						
							|  |  |  | 			print_line("OpenGL Error #" + itos(err) + " at: " + m_section); \ | 
					
						
							|  |  |  | 		}                                                                   \ | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define DEBUG_TEST_ERROR(m_section)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | ShaderGLES3 *ShaderGLES3::active = NULL; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | //#define DEBUG_SHADER
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef DEBUG_SHADER
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define DEBUG_PRINT(m_text) print_line(m_text);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define DEBUG_PRINT(m_text)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderGLES3::bind_uniforms() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!uniforms_dirty) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// upload default uniforms
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	const Map<uint32_t, Variant>::Element *E = uniform_defaults.front(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	while (E) { | 
					
						
							|  |  |  | 		int idx = E->key(); | 
					
						
							|  |  |  | 		int location = version->uniform_location[idx]; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (location < 0) { | 
					
						
							|  |  |  | 			E = E->next(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		const Variant &v = E->value(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		_set_uniform_variant(location, v); | 
					
						
							|  |  |  | 		//print_line("uniform "+itos(location)+" value "+v+ " type "+Variant::get_type_name(v.get_type()));
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		E = E->next(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	const Map<uint32_t, CameraMatrix>::Element *C = uniform_cameras.front(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	while (C) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		int location = version->uniform_location[C->key()]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (location < 0) { | 
					
						
							|  |  |  | 			C = C->next(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		glUniformMatrix4fv(location, 1, false, &(C->get().matrix[0][0])); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		C = C->next(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uniforms_dirty = false; | 
					
						
							| 
									
										
										
										
											2017-06-01 18:55:24 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 15:10:05 -04:00
										 |  |  | GLint ShaderGLES3::get_uniform_location(int p_index) const { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!version, -1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 15:10:05 -04:00
										 |  |  | 	return version->uniform_location[p_index]; | 
					
						
							| 
									
										
										
										
											2017-06-01 18:55:24 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool ShaderGLES3::bind() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (active != this || !version || new_conditional_version.key != conditional_version.key) { | 
					
						
							|  |  |  | 		conditional_version = new_conditional_version; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		version = get_current_version(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(!version, false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glUseProgram(version->id); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	DEBUG_TEST_ERROR("Use Program"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	active = this; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	uniforms_dirty = true; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	/*
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  |  *	why on earth is this code here? | 
					
						
							|  |  |  | 	for (int i=0;i<texunit_pair_count;i++) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		glUniform1i(texunit_pairs[i].location, texunit_pairs[i].index); | 
					
						
							|  |  |  | 		DEBUG_TEST_ERROR("Uniform 1 i"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderGLES3::unbind() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	version = NULL; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	glUseProgram(0); | 
					
						
							|  |  |  | 	uniforms_dirty = true; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	active = NULL; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | static void _display_error_with_code(const String &p_error, const Vector<const char *> &p_code) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int line = 1; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	String total_code; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < p_code.size(); i++) { | 
					
						
							|  |  |  | 		total_code += String(p_code[i]); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vector<String> lines = String(total_code).split("\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int j = 0; j < lines.size(); j++) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		print_line(itos(line) + ": " + lines[j]); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		line++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_PRINTS(p_error); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | ShaderGLES3::Version *ShaderGLES3::get_current_version() { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Version *_v = version_map.getptr(conditional_version); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (_v) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (conditional_version.code_version != 0) { | 
					
						
							|  |  |  | 			CustomCode *cc = custom_code_map.getptr(conditional_version.code_version); | 
					
						
							|  |  |  | 			ERR_FAIL_COND_V(!cc, _v); | 
					
						
							|  |  |  | 			if (cc->version == _v->code_version) | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 				return _v; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return _v; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!_v) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		version_map[conditional_version] = Version(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Version &v = version_map[conditional_version]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!_v) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		v.uniform_location = memnew_arr(GLint, uniform_count); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if (v.ok) { | 
					
						
							|  |  |  | 			//bye bye shaders
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			glDeleteShader(v.vert_id); | 
					
						
							|  |  |  | 			glDeleteShader(v.frag_id); | 
					
						
							|  |  |  | 			glDeleteProgram(v.id); | 
					
						
							|  |  |  | 			v.id = 0; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	v.ok = false; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	/* SETUP CONDITIONALS */ | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vector<const char *> strings; | 
					
						
							| 
									
										
										
										
											2016-11-22 20:51:56 -03:00
										 |  |  | #ifdef GLES_OVER_GL
 | 
					
						
							| 
									
										
										
										
											2016-11-22 20:53:37 -03:00
										 |  |  | 	strings.push_back("#version 330\n"); | 
					
						
							| 
									
										
										
										
											2017-07-22 14:07:38 -03:00
										 |  |  | 	strings.push_back("#define GLES_OVER_GL\n"); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2016-11-23 07:04:55 -03:00
										 |  |  | 	strings.push_back("#version 300 es\n"); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int define_line_ofs = 1; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < custom_defines.size(); i++) { | 
					
						
							| 
									
										
										
										
											2016-11-11 12:27:52 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		strings.push_back(custom_defines[i].get_data()); | 
					
						
							|  |  |  | 		define_line_ofs++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int j = 0; j < conditional_count; j++) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		bool enable = ((1 << j) & conditional_version.version); | 
					
						
							|  |  |  | 		strings.push_back(enable ? conditional_defines[j] : ""); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		if (enable) | 
					
						
							|  |  |  | 			define_line_ofs++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (enable) { | 
					
						
							|  |  |  | 			DEBUG_PRINT(conditional_defines[j]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-11 12:27:52 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	//keep them around during the function
 | 
					
						
							|  |  |  | 	CharString code_string; | 
					
						
							|  |  |  | 	CharString code_string2; | 
					
						
							|  |  |  | 	CharString code_globals; | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 	CharString material_string; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//print_line("code version? "+itos(conditional_version.code_version));
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	CustomCode *cc = NULL; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (conditional_version.code_version > 0) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		//do custom code related stuff
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ERR_FAIL_COND_V(!custom_code_map.has(conditional_version.code_version), NULL); | 
					
						
							|  |  |  | 		cc = &custom_code_map[conditional_version.code_version]; | 
					
						
							|  |  |  | 		v.code_version = cc->version; | 
					
						
							|  |  |  | 		define_line_ofs += 2; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* CREATE PROGRAM */ | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	v.id = glCreateProgram(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(v.id == 0, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* VERTEX SHADER */ | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < cc->custom_defines.size(); i++) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 			strings.push_back(cc->custom_defines[i].get_data()); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			DEBUG_PRINT("CD #" + itos(i) + ": " + String(cc->custom_defines[i])); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int strings_base_size = strings.size(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//vertex precision is high
 | 
					
						
							|  |  |  | 	strings.push_back("precision highp float;\n"); | 
					
						
							|  |  |  | 	strings.push_back("precision highp int;\n"); | 
					
						
							| 
									
										
										
										
											2017-08-14 20:00:16 +02:00
										 |  |  | #ifndef GLES_OVER_GL
 | 
					
						
							| 
									
										
										
										
											2017-08-11 19:09:55 +02:00
										 |  |  | 	strings.push_back("precision highp sampler2D;\n"); | 
					
						
							|  |  |  | 	strings.push_back("precision highp samplerCube;\n"); | 
					
						
							|  |  |  | 	strings.push_back("precision highp sampler2DArray;\n"); | 
					
						
							| 
									
										
										
										
											2017-08-14 20:00:16 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if 0
 | 
					
						
							|  |  |  | 	if (cc) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		String _code_string = "#define VERTEX_SHADER_CODE "+cc->vertex+"\n"; | 
					
						
							|  |  |  | 		String _code_globals = "#define VERTEX_SHADER_GLOBALS "+cc->vertex_globals+"\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		code_string=_code_string.ascii(); | 
					
						
							|  |  |  | 		code_globals=_code_globals.ascii(); | 
					
						
							|  |  |  | 		DEBUG_PRINT( code_globals.get_data() ); | 
					
						
							|  |  |  | 		DEBUG_PRINT( code_string.get_data() ); | 
					
						
							|  |  |  | 		strings.push_back(code_globals); | 
					
						
							|  |  |  | 		strings.push_back(code_string); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	strings.push_back(vertex_code0.get_data()); | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 		material_string = cc->uniforms.ascii(); | 
					
						
							|  |  |  | 		strings.push_back(material_string.get_data()); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	strings.push_back(vertex_code1.get_data()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 		code_globals = cc->vertex_globals.ascii(); | 
					
						
							|  |  |  | 		strings.push_back(code_globals.get_data()); | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	strings.push_back(vertex_code2.get_data()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		code_string = cc->vertex.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		strings.push_back(code_string.get_data()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 	strings.push_back(vertex_code3.get_data()); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | #ifdef DEBUG_SHADER
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	DEBUG_PRINT("\nVertex Code:\n\n" + String(code_string.get_data())); | 
					
						
							|  |  |  | 	for (int i = 0; i < strings.size(); i++) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		//print_line("vert strings "+itos(i)+":"+String(strings[i]));
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	v.vert_id = glCreateShader(GL_VERTEX_SHADER); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	glShaderSource(v.vert_id, strings.size(), &strings[0], NULL); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	glCompileShader(v.vert_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	GLint status; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	glGetShaderiv(v.vert_id, GL_COMPILE_STATUS, &status); | 
					
						
							|  |  |  | 	if (status == GL_FALSE) { | 
					
						
							|  |  |  | 		// error compiling
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		GLsizei iloglen; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		glGetShaderiv(v.vert_id, GL_INFO_LOG_LENGTH, &iloglen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (iloglen < 0) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			glDeleteShader(v.vert_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			glDeleteProgram(v.id); | 
					
						
							|  |  |  | 			v.id = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			ERR_PRINT("NO LOG, WTF"); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			if (iloglen == 0) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				iloglen = 4096; //buggy driver (Adreno 220+....)
 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			char *ilogmem = (char *)memalloc(iloglen + 1); | 
					
						
							|  |  |  | 			ilogmem[iloglen] = 0; | 
					
						
							|  |  |  | 			glGetShaderInfoLog(v.vert_id, iloglen, &iloglen, ilogmem); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			String err_string = get_shader_name() + ": Vertex Program Compilation Failed:\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			err_string += ilogmem; | 
					
						
							|  |  |  | 			_display_error_with_code(err_string, strings); | 
					
						
							| 
									
										
										
										
											2017-01-06 10:15:44 -03:00
										 |  |  | 			memfree(ilogmem); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			glDeleteShader(v.vert_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			glDeleteProgram(v.id); | 
					
						
							|  |  |  | 			v.id = 0; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ERR_FAIL_V(NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	//_display_error_with_code("pepo", strings);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	/* FRAGMENT SHADER */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	strings.resize(strings_base_size); | 
					
						
							|  |  |  | 	//fragment precision is medium
 | 
					
						
							| 
									
										
										
										
											2016-11-29 19:55:12 -03:00
										 |  |  | 	strings.push_back("precision highp float;\n"); | 
					
						
							|  |  |  | 	strings.push_back("precision highp int;\n"); | 
					
						
							| 
									
										
										
										
											2017-08-14 20:00:16 +02:00
										 |  |  | #ifndef GLES_OVER_GL
 | 
					
						
							| 
									
										
										
										
											2017-08-11 19:09:55 +02:00
										 |  |  | 	strings.push_back("precision highp sampler2D;\n"); | 
					
						
							|  |  |  | 	strings.push_back("precision highp samplerCube;\n"); | 
					
						
							|  |  |  | 	strings.push_back("precision highp sampler2DArray;\n"); | 
					
						
							| 
									
										
										
										
											2017-08-14 20:00:16 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if 0
 | 
					
						
							|  |  |  | 	if (cc) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		String _code_string = "#define FRAGMENT_SHADER_CODE "+cc->fragment+"\n"; | 
					
						
							|  |  |  | 		String _code_globals = "#define FRAGMENT_SHADER_GLOBALS "+cc->fragment_globals+"\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		code_string=_code_string.ascii(); | 
					
						
							|  |  |  | 		code_globals=_code_globals.ascii(); | 
					
						
							|  |  |  | 		DEBUG_PRINT( code_globals.get_data() ); | 
					
						
							|  |  |  | 		DEBUG_PRINT( code_string.get_data() ); | 
					
						
							|  |  |  | 		strings.push_back(code_globals); | 
					
						
							|  |  |  | 		strings.push_back(code_string); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	strings.push_back(fragment_code0.get_data()); | 
					
						
							|  |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 		material_string = cc->uniforms.ascii(); | 
					
						
							|  |  |  | 		strings.push_back(material_string.get_data()); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	strings.push_back(fragment_code1.get_data()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 		code_globals = cc->fragment_globals.ascii(); | 
					
						
							|  |  |  | 		strings.push_back(code_globals.get_data()); | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	strings.push_back(fragment_code2.get_data()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2017-06-01 18:55:24 -03:00
										 |  |  | 		code_string = cc->light.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		strings.push_back(code_string.get_data()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 	strings.push_back(fragment_code3.get_data()); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2017-06-01 18:55:24 -03:00
										 |  |  | 		code_string2 = cc->fragment.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		strings.push_back(code_string2.get_data()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 	strings.push_back(fragment_code4.get_data()); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef DEBUG_SHADER
 | 
					
						
							| 
									
										
										
										
											2017-07-08 08:06:13 -03:00
										 |  |  | 	DEBUG_PRINT("\nFragment Globals:\n\n" + String(code_globals.get_data())); | 
					
						
							|  |  |  | 	DEBUG_PRINT("\nFragment Code:\n\n" + String(code_string2.get_data())); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < strings.size(); i++) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		//print_line("frag strings "+itos(i)+":"+String(strings[i]));
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	v.frag_id = glCreateShader(GL_FRAGMENT_SHADER); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	glShaderSource(v.frag_id, strings.size(), &strings[0], NULL); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	glCompileShader(v.frag_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	glGetShaderiv(v.frag_id, GL_COMPILE_STATUS, &status); | 
					
						
							|  |  |  | 	if (status == GL_FALSE) { | 
					
						
							|  |  |  | 		// error compiling
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		GLsizei iloglen; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		glGetShaderiv(v.frag_id, GL_INFO_LOG_LENGTH, &iloglen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (iloglen < 0) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			glDeleteShader(v.frag_id); | 
					
						
							|  |  |  | 			glDeleteShader(v.vert_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			glDeleteProgram(v.id); | 
					
						
							|  |  |  | 			v.id = 0; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			ERR_PRINT("NO LOG, WTF"); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (iloglen == 0) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				iloglen = 4096; //buggy driver (Adreno 220+....)
 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			char *ilogmem = (char *)memalloc(iloglen + 1); | 
					
						
							|  |  |  | 			ilogmem[iloglen] = 0; | 
					
						
							|  |  |  | 			glGetShaderInfoLog(v.frag_id, iloglen, &iloglen, ilogmem); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			String err_string = get_shader_name() + ": Fragment Program Compilation Failed:\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			err_string += ilogmem; | 
					
						
							|  |  |  | 			_display_error_with_code(err_string, strings); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			ERR_PRINT(err_string.ascii().get_data()); | 
					
						
							| 
									
										
										
										
											2017-01-06 10:15:44 -03:00
										 |  |  | 			memfree(ilogmem); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			glDeleteShader(v.frag_id); | 
					
						
							|  |  |  | 			glDeleteShader(v.vert_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			glDeleteProgram(v.id); | 
					
						
							|  |  |  | 			v.id = 0; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ERR_FAIL_V(NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glAttachShader(v.id, v.frag_id); | 
					
						
							|  |  |  | 	glAttachShader(v.id, v.vert_id); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// bind attributes before linking
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < attribute_pair_count; i++) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		glBindAttribLocation(v.id, attribute_pairs[i].index, attribute_pairs[i].name); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:55 -03:00
										 |  |  | 	//if feedback exists, set it up
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (feedback_count) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Vector<const char *> feedback; | 
					
						
							|  |  |  | 		for (int i = 0; i < feedback_count; i++) { | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:55 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			if (feedbacks[i].conditional == -1 || (1 << feedbacks[i].conditional) & conditional_version.version) { | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:55 -03:00
										 |  |  | 				//conditional for this feedback is enabled
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				print_line("tf varying: " + itos(feedback.size()) + " " + String(feedbacks[i].name)); | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:55 -03:00
										 |  |  | 				feedback.push_back(feedbacks[i].name); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (feedback.size()) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			glTransformFeedbackVaryings(v.id, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS); | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:55 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	glLinkProgram(v.id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	glGetProgramiv(v.id, GL_LINK_STATUS, &status); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (status == GL_FALSE) { | 
					
						
							|  |  |  | 		// error linking
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		GLsizei iloglen; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		glGetProgramiv(v.id, GL_INFO_LOG_LENGTH, &iloglen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (iloglen < 0) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			glDeleteShader(v.frag_id); | 
					
						
							|  |  |  | 			glDeleteShader(v.vert_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			glDeleteProgram(v.id); | 
					
						
							|  |  |  | 			v.id = 0; | 
					
						
							|  |  |  | 			ERR_FAIL_COND_V(iloglen <= 0, NULL); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (iloglen == 0) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			iloglen = 4096; //buggy driver (Adreno 220+....)
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		char *ilogmem = (char *)Memory::alloc_static(iloglen + 1); | 
					
						
							|  |  |  | 		ilogmem[iloglen] = 0; | 
					
						
							|  |  |  | 		glGetProgramInfoLog(v.id, iloglen, &iloglen, ilogmem); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		String err_string = get_shader_name() + ": Program LINK FAILED:\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		err_string += ilogmem; | 
					
						
							|  |  |  | 		_display_error_with_code(err_string, strings); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		ERR_PRINT(err_string.ascii().get_data()); | 
					
						
							|  |  |  | 		Memory::free_static(ilogmem); | 
					
						
							|  |  |  | 		glDeleteShader(v.frag_id); | 
					
						
							|  |  |  | 		glDeleteShader(v.vert_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		glDeleteProgram(v.id); | 
					
						
							|  |  |  | 		v.id = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		ERR_FAIL_V(NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	/* UNIFORMS */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glUseProgram(v.id); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//print_line("uniforms:  ");
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int j = 0; j < uniform_count; j++) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		v.uniform_location[j] = glGetUniformLocation(v.id, uniform_names[j]); | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 		//print_line("uniform "+String(uniform_names[j])+" location "+itos(v.uniform_location[j]));
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	// set texture uniforms
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < texunit_pair_count; i++) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		GLint loc = glGetUniformLocation(v.id, texunit_pairs[i].name); | 
					
						
							|  |  |  | 		if (loc >= 0) { | 
					
						
							|  |  |  | 			if (texunit_pairs[i].index < 0) { | 
					
						
							|  |  |  | 				glUniform1i(loc, max_image_units + texunit_pairs[i].index); //negative, goes down
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				glUniform1i(loc, texunit_pairs[i].index); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// assign uniform block bind points
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < ubo_count; i++) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		GLint loc = glGetUniformBlockIndex(v.id, ubo_pairs[i].name); | 
					
						
							|  |  |  | 		if (loc >= 0) | 
					
						
							|  |  |  | 			glUniformBlockBinding(v.id, loc, ubo_pairs[i].index); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (cc) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 		v.texture_uniform_locations.resize(cc->texture_uniforms.size()); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < cc->texture_uniforms.size(); i++) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			v.texture_uniform_locations[i] = glGetUniformLocation(v.id, String(cc->texture_uniforms[i]).ascii().get_data()); | 
					
						
							|  |  |  | 			glUniform1i(v.texture_uniform_locations[i], i + base_material_tex_index); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glUseProgram(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	v.ok = true; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return &v; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | GLint ShaderGLES3::get_uniform_location(const String &p_name) const { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(!version, -1); | 
					
						
							|  |  |  | 	return glGetUniformLocation(version->id, p_name.ascii().get_data()); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ShaderGLES3::setup(const char **p_conditional_defines, int p_conditional_count, const char **p_uniform_names, int p_uniform_count, const AttributePair *p_attribute_pairs, int p_attribute_count, const TexUnitPair *p_texunit_pairs, int p_texunit_pair_count, const UBOPair *p_ubo_pairs, int p_ubo_pair_count, const Feedback *p_feedback, int p_feedback_count, const char *p_vertex_code, const char *p_fragment_code, int p_vertex_code_start, int p_fragment_code_start) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND(version); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	conditional_version.key = 0; | 
					
						
							|  |  |  | 	new_conditional_version.key = 0; | 
					
						
							|  |  |  | 	uniform_count = p_uniform_count; | 
					
						
							|  |  |  | 	conditional_count = p_conditional_count; | 
					
						
							|  |  |  | 	conditional_defines = p_conditional_defines; | 
					
						
							|  |  |  | 	uniform_names = p_uniform_names; | 
					
						
							|  |  |  | 	vertex_code = p_vertex_code; | 
					
						
							|  |  |  | 	fragment_code = p_fragment_code; | 
					
						
							|  |  |  | 	texunit_pairs = p_texunit_pairs; | 
					
						
							|  |  |  | 	texunit_pair_count = p_texunit_pair_count; | 
					
						
							|  |  |  | 	vertex_code_start = p_vertex_code_start; | 
					
						
							|  |  |  | 	fragment_code_start = p_fragment_code_start; | 
					
						
							|  |  |  | 	attribute_pairs = p_attribute_pairs; | 
					
						
							|  |  |  | 	attribute_pair_count = p_attribute_count; | 
					
						
							|  |  |  | 	ubo_pairs = p_ubo_pairs; | 
					
						
							|  |  |  | 	ubo_count = p_ubo_pair_count; | 
					
						
							|  |  |  | 	feedbacks = p_feedback; | 
					
						
							|  |  |  | 	feedback_count = p_feedback_count; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//split vertex and shader code (thank you, retarded shader compiler programmers from you know what company).
 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		String globals_tag = "\nVERTEX_SHADER_GLOBALS"; | 
					
						
							|  |  |  | 		String material_tag = "\nMATERIAL_UNIFORMS"; | 
					
						
							|  |  |  | 		String code_tag = "\nVERTEX_SHADER_CODE"; | 
					
						
							|  |  |  | 		String code = vertex_code; | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 		int cpos = code.find(material_tag); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (cpos == -1) { | 
					
						
							|  |  |  | 			vertex_code0 = code.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			vertex_code0 = code.substr(0, cpos).ascii(); | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 			code = code.substr(cpos + material_tag.length(), code.length()); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 			cpos = code.find(globals_tag); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			if (cpos == -1) { | 
					
						
							|  |  |  | 				vertex_code1 = code.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				vertex_code1 = code.substr(0, cpos).ascii(); | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 				String code2 = code.substr(cpos + globals_tag.length(), code.length()); | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				cpos = code2.find(code_tag); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				if (cpos == -1) { | 
					
						
							|  |  |  | 					vertex_code2 = code2.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 					vertex_code2 = code2.substr(0, cpos).ascii(); | 
					
						
							|  |  |  | 					vertex_code3 = code2.substr(cpos + code_tag.length(), code2.length()).ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		String globals_tag = "\nFRAGMENT_SHADER_GLOBALS"; | 
					
						
							|  |  |  | 		String material_tag = "\nMATERIAL_UNIFORMS"; | 
					
						
							|  |  |  | 		String code_tag = "\nFRAGMENT_SHADER_CODE"; | 
					
						
							|  |  |  | 		String light_code_tag = "\nLIGHT_SHADER_CODE"; | 
					
						
							|  |  |  | 		String code = fragment_code; | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 		int cpos = code.find(material_tag); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (cpos == -1) { | 
					
						
							|  |  |  | 			fragment_code0 = code.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			fragment_code0 = code.substr(0, cpos).ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-19 11:14:41 -03:00
										 |  |  | 			//print_line("CODE0:\n"+String(fragment_code0.get_data()));
 | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 			code = code.substr(cpos + material_tag.length(), code.length()); | 
					
						
							|  |  |  | 			cpos = code.find(globals_tag); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			if (cpos == -1) { | 
					
						
							|  |  |  | 				fragment_code1 = code.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				fragment_code1 = code.substr(0, cpos).ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-19 11:14:41 -03:00
										 |  |  | 				//print_line("CODE1:\n"+String(fragment_code1.get_data()));
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | 				String code2 = code.substr(cpos + globals_tag.length(), code.length()); | 
					
						
							| 
									
										
										
										
											2017-06-01 18:55:24 -03:00
										 |  |  | 				cpos = code2.find(light_code_tag); | 
					
						
							| 
									
										
										
										
											2016-10-19 11:14:41 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				if (cpos == -1) { | 
					
						
							|  |  |  | 					fragment_code2 = code2.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 					fragment_code2 = code2.substr(0, cpos).ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-19 11:14:41 -03:00
										 |  |  | 					//print_line("CODE2:\n"+String(fragment_code2.get_data()));
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-01 18:55:24 -03:00
										 |  |  | 					String code3 = code2.substr(cpos + light_code_tag.length(), code2.length()); | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-01 18:55:24 -03:00
										 |  |  | 					cpos = code3.find(code_tag); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 					if (cpos == -1) { | 
					
						
							|  |  |  | 						fragment_code3 = code3.ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 						fragment_code3 = code3.substr(0, cpos).ascii(); | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 						//print_line("CODE3:\n"+String(fragment_code3.get_data()));
 | 
					
						
							| 
									
										
										
										
											2017-06-01 18:55:24 -03:00
										 |  |  | 						fragment_code4 = code3.substr(cpos + code_tag.length(), code3.length()).ascii(); | 
					
						
							| 
									
										
										
										
											2016-10-19 11:14:41 -03:00
										 |  |  | 						//print_line("CODE4:\n"+String(fragment_code4.get_data()));
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_image_units); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderGLES3::finish() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	const VersionKey *V = NULL; | 
					
						
							|  |  |  | 	while ((V = version_map.next(V))) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Version &v = version_map[*V]; | 
					
						
							|  |  |  | 		glDeleteShader(v.vert_id); | 
					
						
							|  |  |  | 		glDeleteShader(v.frag_id); | 
					
						
							|  |  |  | 		glDeleteProgram(v.id); | 
					
						
							|  |  |  | 		memdelete_arr(v.uniform_location); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void ShaderGLES3::clear_caches() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	const VersionKey *V = NULL; | 
					
						
							|  |  |  | 	while ((V = version_map.next(V))) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Version &v = version_map[*V]; | 
					
						
							|  |  |  | 		glDeleteShader(v.vert_id); | 
					
						
							|  |  |  | 		glDeleteShader(v.frag_id); | 
					
						
							|  |  |  | 		glDeleteProgram(v.id); | 
					
						
							|  |  |  | 		memdelete_arr(v.uniform_location); | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	version_map.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	custom_code_map.clear(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	version = NULL; | 
					
						
							|  |  |  | 	last_custom_code = 1; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	uniforms_dirty = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint32_t ShaderGLES3::create_custom_shader() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	custom_code_map[last_custom_code] = CustomCode(); | 
					
						
							|  |  |  | 	custom_code_map[last_custom_code].version = 1; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	return last_custom_code++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ShaderGLES3::set_custom_shader_code(uint32_t p_code_id, const String &p_vertex, const String &p_vertex_globals, const String &p_fragment, const String &p_light, const String &p_fragment_globals, const String &p_uniforms, const Vector<StringName> &p_texture_uniforms, const Vector<CharString> &p_custom_defines) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!custom_code_map.has(p_code_id)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	CustomCode *cc = &custom_code_map[p_code_id]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cc->vertex = p_vertex; | 
					
						
							|  |  |  | 	cc->vertex_globals = p_vertex_globals; | 
					
						
							|  |  |  | 	cc->fragment = p_fragment; | 
					
						
							|  |  |  | 	cc->fragment_globals = p_fragment_globals; | 
					
						
							|  |  |  | 	cc->light = p_light; | 
					
						
							|  |  |  | 	cc->texture_uniforms = p_texture_uniforms; | 
					
						
							|  |  |  | 	cc->uniforms = p_uniforms; | 
					
						
							|  |  |  | 	cc->custom_defines = p_custom_defines; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	cc->version++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderGLES3::set_custom_shader(uint32_t p_code_id) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	new_conditional_version.code_version = p_code_id; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderGLES3::free_custom_shader(uint32_t p_code_id) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	/*  if (! custom_code_map.has( p_code_id )) {
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  |         print_line("no code id "+itos(p_code_id)); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         print_line("freed code id "+itos(p_code_id)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND(!custom_code_map.has(p_code_id)); | 
					
						
							|  |  |  | 	if (conditional_version.code_version == p_code_id) | 
					
						
							|  |  |  | 		conditional_version.code_version = 0; //bye
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	custom_code_map.erase(p_code_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | void ShaderGLES3::set_base_material_tex_index(int p_idx) { | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	base_material_tex_index = p_idx; | 
					
						
							| 
									
										
										
										
											2016-10-10 18:31:01 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ShaderGLES3::ShaderGLES3() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	version = NULL; | 
					
						
							|  |  |  | 	last_custom_code = 1; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	uniforms_dirty = true; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	base_material_tex_index = 0; | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ShaderGLES3::~ShaderGLES3() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 16:33:42 -03:00
										 |  |  | 	finish(); | 
					
						
							|  |  |  | } |