2016-10-03 19:33:42 +00:00
|
|
|
/*************************************************************************/
|
2017-03-05 14:47:28 +00:00
|
|
|
/* shader_gles3.cpp */
|
2016-10-03 19:33:42 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-10-03 19:33:42 +00:00
|
|
|
/*************************************************************************/
|
2022-01-13 08:45:09 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-10-03 19:33:42 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
#include "shader_gles3.h"
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
#include "core/local_vector.h"
|
|
|
|
#include "core/os/os.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/print_string.h"
|
2021-09-26 19:31:17 +00:00
|
|
|
#include "core/threaded_callable_queue.h"
|
|
|
|
#include "drivers/gles3/shader_cache_gles3.h"
|
|
|
|
#include "servers/visual_server.h"
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
//#define DEBUG_OPENGL
|
|
|
|
|
|
|
|
#ifdef DEBUG_OPENGL
|
|
|
|
|
2017-03-05 15:44:50 +00: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 19:33:42 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define DEBUG_TEST_ERROR(m_section)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
ShaderGLES3 *ShaderGLES3::active = nullptr;
|
2021-09-26 19:31:17 +00:00
|
|
|
SelfList<ShaderGLES3::Version>::List ShaderGLES3::versions_compiling;
|
|
|
|
|
|
|
|
ShaderCacheGLES3 *ShaderGLES3::shader_cache;
|
|
|
|
ThreadedCallableQueue<GLuint> *ShaderGLES3::cache_write_queue;
|
|
|
|
|
|
|
|
ThreadedCallableQueue<GLuint> *ShaderGLES3::compile_queue;
|
|
|
|
bool ShaderGLES3::parallel_compile_supported;
|
|
|
|
|
|
|
|
bool ShaderGLES3::async_hidden_forbidden;
|
2022-06-13 16:01:19 +00:00
|
|
|
uint32_t *ShaderGLES3::compiles_started_this_frame;
|
|
|
|
uint32_t *ShaderGLES3::max_frame_compiles_in_progress;
|
|
|
|
uint32_t ShaderGLES3::max_simultaneous_compiles;
|
|
|
|
uint32_t ShaderGLES3::active_compiles_count;
|
2021-09-26 19:31:17 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
bool ShaderGLES3::log_active_async_compiles_count;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint64_t ShaderGLES3::current_frame;
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
//#define DEBUG_SHADER
|
|
|
|
|
|
|
|
#ifdef DEBUG_SHADER
|
|
|
|
#define DEBUG_PRINT(m_text) print_line(m_text);
|
|
|
|
#else
|
|
|
|
#define DEBUG_PRINT(m_text)
|
|
|
|
#endif
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
#define _EXT_COMPLETION_STATUS 0x91B1
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
GLint ShaderGLES3::get_uniform_location(int p_index) const {
|
|
|
|
ERR_FAIL_COND_V(!version, -1);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
return version->uniform_location[p_index];
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
bool ShaderGLES3::bind() {
|
|
|
|
return _bind(false);
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
bool ShaderGLES3::_bind(bool p_binding_fallback) {
|
|
|
|
// Same base shader and version valid version?
|
|
|
|
if (active == this && version) {
|
|
|
|
if (new_conditional_version.code_version == conditional_version.code_version) {
|
|
|
|
if (new_conditional_version.version == conditional_version.version) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// From ubershader to ubershader of the same code?
|
|
|
|
if ((conditional_version.version & VersionKey::UBERSHADER_FLAG) && (new_conditional_version.version & VersionKey::UBERSHADER_FLAG)) {
|
|
|
|
conditional_version.version = new_conditional_version.version;
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2021-09-26 19:31:17 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
bool must_be_ready_now = !is_async_compilation_supported() || p_binding_fallback;
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
conditional_version = new_conditional_version;
|
|
|
|
version = get_current_version(must_be_ready_now);
|
|
|
|
ERR_FAIL_COND_V(!version, false);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
bool ready = false;
|
|
|
|
ready = _process_program_state(version, must_be_ready_now);
|
|
|
|
if (version->compile_status == Version::COMPILE_STATUS_RESTART_NEEDED) {
|
|
|
|
get_current_version(must_be_ready_now); // Trigger recompile
|
|
|
|
ready = _process_program_state(version, must_be_ready_now);
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (ready) {
|
2021-12-11 13:25:38 +00:00
|
|
|
if (VS::get_singleton()->is_force_shader_fallbacks_enabled() && !must_be_ready_now && get_ubershader_flags_uniform() != -1) {
|
2021-09-26 19:31:17 +00:00
|
|
|
ready = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (ready) {
|
|
|
|
glUseProgram(version->ids.main);
|
|
|
|
if (!version->uniforms_ready) {
|
|
|
|
_setup_uniforms(custom_code_map.getptr(conditional_version.code_version));
|
|
|
|
version->uniforms_ready = true;
|
|
|
|
}
|
|
|
|
DEBUG_TEST_ERROR("Use Program");
|
|
|
|
active = this;
|
|
|
|
return true;
|
|
|
|
} else if (!must_be_ready_now && version->async_mode == ASYNC_MODE_VISIBLE && !p_binding_fallback && get_ubershader_flags_uniform() != -1) {
|
|
|
|
// We can and have to fall back to the ubershader
|
|
|
|
return _bind_ubershader();
|
2016-10-03 19:33:42 +00:00
|
|
|
} else {
|
2021-09-26 19:31:17 +00:00
|
|
|
// We have a compile error or must fall back by skipping render
|
|
|
|
unbind();
|
2016-10-03 19:33:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-26 19:31:17 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2022-04-12 18:08:09 +00:00
|
|
|
bool ShaderGLES3::is_custom_code_ready_for_render(uint32_t p_code_id) {
|
|
|
|
if (p_code_id == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!is_async_compilation_supported() || get_ubershader_flags_uniform() == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomCode *cc = custom_code_map.getptr(p_code_id);
|
|
|
|
ERR_FAIL_COND_V(!cc, false);
|
|
|
|
if (cc->async_mode == ASYNC_MODE_HIDDEN) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (VS::get_singleton()->is_force_shader_fallbacks_enabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
VersionKey effective_version;
|
|
|
|
effective_version.version = new_conditional_version.version;
|
|
|
|
effective_version.code_version = p_code_id;
|
|
|
|
Version *v = version_map.getptr(effective_version);
|
|
|
|
if (!v || cc->version != v->code_version || v->compile_status != Version::COMPILE_STATUS_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
bool ShaderGLES3::_bind_ubershader() {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
ERR_FAIL_COND_V(!is_async_compilation_supported(), false);
|
|
|
|
ERR_FAIL_COND_V(get_ubershader_flags_uniform() == -1, false);
|
|
|
|
#endif
|
|
|
|
new_conditional_version.version |= VersionKey::UBERSHADER_FLAG;
|
|
|
|
bool bound = _bind(true);
|
|
|
|
int conditionals_uniform = _get_uniform(get_ubershader_flags_uniform());
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
ERR_FAIL_COND_V(conditionals_uniform == -1, false);
|
|
|
|
#endif
|
|
|
|
new_conditional_version.version &= ~VersionKey::UBERSHADER_FLAG;
|
2021-12-11 13:25:38 +00:00
|
|
|
glUniform1ui(conditionals_uniform, new_conditional_version.version);
|
2021-09-26 19:31:17 +00:00
|
|
|
return bound;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
void ShaderGLES3::advance_async_shaders_compilation() {
|
|
|
|
SelfList<ShaderGLES3::Version> *curr = versions_compiling.first();
|
|
|
|
while (curr) {
|
|
|
|
SelfList<ShaderGLES3::Version> *next = curr->next();
|
2018-09-23 15:12:30 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
ShaderGLES3::Version *v = curr->self();
|
|
|
|
// Only if it didn't already have a chance to be processed in this frame
|
|
|
|
if (v->last_frame_processed != current_frame) {
|
|
|
|
v->shader->_process_program_state(v, false);
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
curr = next;
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
void ShaderGLES3::_log_active_compiles() {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (log_active_async_compiles_count) {
|
|
|
|
if (parallel_compile_supported) {
|
|
|
|
print_line("Async. shader compiles: " + itos(active_compiles_count));
|
|
|
|
} else if (compile_queue) {
|
|
|
|
print_line("Queued shader compiles: " + itos(active_compiles_count));
|
|
|
|
} else {
|
|
|
|
CRASH_NOW();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderGLES3::_process_program_state(Version *p_version, bool p_async_forbidden) {
|
|
|
|
bool ready = false;
|
|
|
|
bool run_next_step = true;
|
|
|
|
while (run_next_step) {
|
|
|
|
run_next_step = false;
|
|
|
|
switch (p_version->compile_status) {
|
|
|
|
case Version::COMPILE_STATUS_OK: {
|
|
|
|
// Yeaaah!
|
|
|
|
ready = true;
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_ERROR: {
|
|
|
|
// Sad, but we have to accept it
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_PENDING:
|
|
|
|
case Version::COMPILE_STATUS_RESTART_NEEDED: {
|
|
|
|
// These lead to nowhere unless other piece of code starts the compile process
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_SOURCE_PROVIDED: {
|
2022-06-13 16:01:19 +00:00
|
|
|
uint32_t start_compiles_count = p_async_forbidden ? 2 : 0;
|
2021-09-26 19:31:17 +00:00
|
|
|
if (!start_compiles_count) {
|
2022-06-14 09:43:58 +00:00
|
|
|
uint32_t used_async_slots = MAX(active_compiles_count, *compiles_started_this_frame);
|
2022-06-15 11:39:25 +00:00
|
|
|
uint32_t free_async_slots = used_async_slots < max_simultaneous_compiles ? max_simultaneous_compiles - used_async_slots : 0;
|
2021-09-26 19:31:17 +00:00
|
|
|
start_compiles_count = MIN(2, free_async_slots);
|
|
|
|
}
|
|
|
|
if (start_compiles_count >= 1) {
|
|
|
|
glCompileShader(p_version->ids.vert);
|
|
|
|
if (start_compiles_count == 1) {
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_COMPILING_VERTEX;
|
|
|
|
} else {
|
|
|
|
glCompileShader(p_version->ids.frag);
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT;
|
|
|
|
}
|
|
|
|
if (!p_async_forbidden) {
|
|
|
|
versions_compiling.add_last(&p_version->compiling_list);
|
|
|
|
// Vertex and fragment shaders take independent compile slots
|
|
|
|
active_compiles_count += start_compiles_count;
|
2022-06-13 16:01:19 +00:00
|
|
|
*max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
|
2021-09-26 19:31:17 +00:00
|
|
|
_log_active_compiles();
|
|
|
|
}
|
|
|
|
(*compiles_started_this_frame) += start_compiles_count;
|
|
|
|
run_next_step = p_async_forbidden;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_COMPILING_VERTEX: {
|
|
|
|
bool must_compile_frag_now = p_async_forbidden;
|
|
|
|
if (!must_compile_frag_now) {
|
|
|
|
if (active_compiles_count < max_simultaneous_compiles && *compiles_started_this_frame < max_simultaneous_compiles) {
|
|
|
|
must_compile_frag_now = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (must_compile_frag_now) {
|
|
|
|
glCompileShader(p_version->ids.frag);
|
|
|
|
if (p_version->compiling_list.in_list()) {
|
|
|
|
active_compiles_count++;
|
2022-06-13 16:01:19 +00:00
|
|
|
*max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
|
2021-09-26 19:31:17 +00:00
|
|
|
_log_active_compiles();
|
|
|
|
}
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT;
|
|
|
|
} else if (parallel_compile_supported) {
|
|
|
|
GLint completed = 0;
|
|
|
|
glGetShaderiv(p_version->ids.vert, _EXT_COMPLETION_STATUS, &completed);
|
|
|
|
if (completed) {
|
|
|
|
// Not touching compiles count since the same slot used for vertex is now used for fragment
|
|
|
|
glCompileShader(p_version->ids.frag);
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_COMPILING_FRAGMENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
run_next_step = p_async_forbidden;
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_COMPILING_FRAGMENT:
|
|
|
|
case Version::COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT: {
|
|
|
|
bool must_complete_now = p_async_forbidden;
|
|
|
|
if (!must_complete_now && parallel_compile_supported) {
|
|
|
|
GLint vertex_completed = 0;
|
|
|
|
if (p_version->compile_status == Version::COMPILE_STATUS_COMPILING_FRAGMENT) {
|
|
|
|
vertex_completed = true;
|
|
|
|
} else {
|
|
|
|
glGetShaderiv(p_version->ids.vert, _EXT_COMPLETION_STATUS, &vertex_completed);
|
|
|
|
if (p_version->compiling_list.in_list()) {
|
|
|
|
active_compiles_count--;
|
2022-06-13 16:01:19 +00:00
|
|
|
#ifdef DEV_ENABLED
|
|
|
|
CRASH_COND(active_compiles_count == UINT32_MAX);
|
|
|
|
#endif
|
|
|
|
*max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
|
2021-09-26 19:31:17 +00:00
|
|
|
_log_active_compiles();
|
|
|
|
}
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_COMPILING_FRAGMENT;
|
|
|
|
}
|
|
|
|
if (vertex_completed) {
|
|
|
|
GLint frag_completed = 0;
|
|
|
|
glGetShaderiv(p_version->ids.frag, _EXT_COMPLETION_STATUS, &frag_completed);
|
|
|
|
if (frag_completed) {
|
|
|
|
must_complete_now = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (must_complete_now) {
|
|
|
|
bool must_save_to_cache = p_version->version_key.is_subject_to_caching() && p_version->program_binary.source != Version::ProgramBinary::SOURCE_CACHE && shader_cache;
|
|
|
|
bool ok = p_version->shader->_complete_compile(p_version->ids, must_save_to_cache);
|
|
|
|
if (ok) {
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_LINKING;
|
|
|
|
run_next_step = p_async_forbidden;
|
|
|
|
} else {
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_ERROR;
|
|
|
|
if (p_version->compiling_list.in_list()) {
|
|
|
|
p_version->compiling_list.remove_from_list();
|
|
|
|
active_compiles_count--;
|
2022-06-13 16:01:19 +00:00
|
|
|
#ifdef DEV_ENABLED
|
|
|
|
CRASH_COND(active_compiles_count == UINT32_MAX);
|
|
|
|
#endif
|
2021-09-26 19:31:17 +00:00
|
|
|
_log_active_compiles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_PROCESSING_AT_QUEUE: {
|
|
|
|
// This is from the async. queue
|
|
|
|
switch (p_version->program_binary.result_from_queue.get()) {
|
|
|
|
case -1: { // Error
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_ERROR;
|
|
|
|
p_version->compiling_list.remove_from_list();
|
|
|
|
active_compiles_count--;
|
2022-06-13 16:01:19 +00:00
|
|
|
#ifdef DEV_ENABLED
|
|
|
|
CRASH_COND(active_compiles_count == UINT32_MAX);
|
|
|
|
#endif
|
2021-09-26 19:31:17 +00:00
|
|
|
_log_active_compiles();
|
|
|
|
} break;
|
|
|
|
case 0: { // In progress
|
|
|
|
if (p_async_forbidden) {
|
|
|
|
OS::get_singleton()->delay_usec(1000);
|
|
|
|
run_next_step = true;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 1: { // Complete
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_BINARY_READY;
|
|
|
|
run_next_step = true;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_BINARY_READY_FROM_CACHE: {
|
|
|
|
bool eat_binary_now = p_async_forbidden;
|
|
|
|
if (!eat_binary_now) {
|
|
|
|
if (active_compiles_count < max_simultaneous_compiles && *compiles_started_this_frame < max_simultaneous_compiles) {
|
|
|
|
eat_binary_now = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (eat_binary_now) {
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_BINARY_READY;
|
|
|
|
run_next_step = true;
|
|
|
|
if (!p_async_forbidden) {
|
|
|
|
versions_compiling.add_last(&p_version->compiling_list);
|
|
|
|
active_compiles_count++;
|
2022-06-13 16:01:19 +00:00
|
|
|
*max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
|
2021-09-26 19:31:17 +00:00
|
|
|
_log_active_compiles();
|
|
|
|
(*compiles_started_this_frame)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_BINARY_READY: {
|
|
|
|
PoolByteArray::Read r = p_version->program_binary.data.read();
|
|
|
|
glProgramBinary(p_version->ids.main, static_cast<GLenum>(p_version->program_binary.format), r.ptr(), p_version->program_binary.data.size());
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_LINKING;
|
|
|
|
run_next_step = true;
|
|
|
|
} break;
|
|
|
|
case Version::COMPILE_STATUS_LINKING: {
|
|
|
|
bool must_complete_now = p_async_forbidden || p_version->program_binary.source == Version::ProgramBinary::SOURCE_QUEUE;
|
|
|
|
if (!must_complete_now && parallel_compile_supported) {
|
|
|
|
GLint link_completed;
|
|
|
|
glGetProgramiv(p_version->ids.main, _EXT_COMPLETION_STATUS, &link_completed);
|
|
|
|
must_complete_now = link_completed;
|
|
|
|
}
|
|
|
|
if (must_complete_now) {
|
|
|
|
bool must_save_to_cache = p_version->version_key.is_subject_to_caching() && p_version->program_binary.source != Version::ProgramBinary::SOURCE_CACHE && shader_cache;
|
|
|
|
bool ok = false;
|
|
|
|
if (must_save_to_cache && p_version->program_binary.source == Version::ProgramBinary::SOURCE_LOCAL) {
|
|
|
|
ok = p_version->shader->_complete_link(p_version->ids, &p_version->program_binary.format, &p_version->program_binary.data);
|
|
|
|
} else {
|
|
|
|
ok = p_version->shader->_complete_link(p_version->ids);
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
#if 0
|
|
|
|
// Simulate GL rejecting program from cache
|
|
|
|
if (p_version->program_binary.source == Version::ProgramBinary::SOURCE_CACHE) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
if (must_save_to_cache) {
|
|
|
|
String &tmp_hash = p_version->program_binary.cache_hash;
|
|
|
|
GLenum &tmp_format = p_version->program_binary.format;
|
|
|
|
PoolByteArray &tmp_data = p_version->program_binary.data;
|
|
|
|
cache_write_queue->enqueue(p_version->ids.main, [=]() {
|
|
|
|
shader_cache->store(tmp_hash, static_cast<uint32_t>(tmp_format), tmp_data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_OK;
|
|
|
|
ready = true;
|
|
|
|
} else {
|
|
|
|
if (p_version->program_binary.source == Version::ProgramBinary::SOURCE_CACHE) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
WARN_PRINT("Program binary from cache has been rejected by the GL. Removing from cache.");
|
|
|
|
#endif
|
|
|
|
shader_cache->remove(p_version->program_binary.cache_hash);
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_RESTART_NEEDED;
|
|
|
|
} else {
|
|
|
|
if (p_version->program_binary.source == Version::ProgramBinary::SOURCE_QUEUE) {
|
|
|
|
ERR_PRINT("Program binary from compile queue has been rejected by the GL. Bug?");
|
|
|
|
}
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p_version->program_binary.data = PoolByteArray();
|
|
|
|
p_version->program_binary.cache_hash.clear();
|
|
|
|
if (p_version->compiling_list.in_list()) {
|
|
|
|
p_version->compiling_list.remove_from_list();
|
|
|
|
active_compiles_count--;
|
2022-06-13 16:01:19 +00:00
|
|
|
#ifdef DEV_ENABLED
|
|
|
|
CRASH_COND(active_compiles_count == UINT32_MAX);
|
|
|
|
#endif
|
2021-09-26 19:31:17 +00:00
|
|
|
_log_active_compiles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ready;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGLES3::unbind() {
|
2021-05-04 14:00:45 +00:00
|
|
|
version = nullptr;
|
2016-10-03 19:33:42 +00:00
|
|
|
glUseProgram(0);
|
2021-05-04 14:00:45 +00:00
|
|
|
active = nullptr;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
static void _display_error_with_code(const String &p_error, GLuint p_shader_id) {
|
2017-03-05 15:44:50 +00:00
|
|
|
int line = 1;
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
GLint source_len;
|
|
|
|
glGetShaderiv(p_shader_id, GL_SHADER_SOURCE_LENGTH, &source_len);
|
|
|
|
LocalVector<GLchar> source_buffer;
|
|
|
|
source_buffer.resize(source_len);
|
|
|
|
glGetShaderSource(p_shader_id, source_len, NULL, source_buffer.ptr());
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
String total_code(source_buffer.ptr());
|
2016-10-03 19:33:42 +00:00
|
|
|
Vector<String> lines = String(total_code).split("\n");
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < lines.size(); j++) {
|
2021-07-17 00:37:01 +00:00
|
|
|
print_line(vformat("%4d | %s", line, lines[j]));
|
2016-10-03 19:33:42 +00:00
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:56:25 +00:00
|
|
|
ERR_PRINT(p_error);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
static CharString _prepare_ubershader_chunk(const CharString &p_chunk) {
|
|
|
|
String s(p_chunk.get_data());
|
|
|
|
Vector<String> lines = s.split("\n");
|
|
|
|
s.clear();
|
|
|
|
for (int i = 0; i < lines.size(); ++i) {
|
|
|
|
if (lines[i].ends_with("//ubershader-skip")) {
|
|
|
|
continue;
|
|
|
|
} else if (lines[i].ends_with("//ubershader-runtime")) {
|
|
|
|
// Move from the preprocessor world to the true code realm
|
|
|
|
String l = lines[i].trim_suffix("//ubershader-runtime").strip_edges();
|
|
|
|
{
|
|
|
|
// Ignore other comments
|
|
|
|
Vector<String> pieces = l.split("//");
|
|
|
|
l = pieces[0].strip_edges();
|
|
|
|
}
|
|
|
|
if (l == "#else") {
|
|
|
|
s += "} else {\n";
|
|
|
|
} else if (l == "#endif") {
|
|
|
|
s += "}\n";
|
|
|
|
} else if (l.begins_with("#ifdef")) {
|
|
|
|
Vector<String> pieces = l.split_spaces();
|
|
|
|
CRASH_COND(pieces.size() != 2);
|
2021-12-11 13:25:38 +00:00
|
|
|
s += "if ((ubershader_flags & FLAG_" + pieces[1] + ") != 0u) {\n";
|
2021-09-26 19:31:17 +00:00
|
|
|
} else if (l.begins_with("#ifndef")) {
|
|
|
|
Vector<String> pieces = l.split_spaces();
|
|
|
|
CRASH_COND(pieces.size() != 2);
|
2021-12-11 13:25:38 +00:00
|
|
|
s += "if ((ubershader_flags & FLAG_" + pieces[1] + ") == 0u) {\n";
|
2021-09-26 19:31:17 +00:00
|
|
|
} else {
|
|
|
|
CRASH_NOW_MSG("The shader template is using too complex syntax in a line marked with ubershader-runtime.");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
s += lines[i] + "\n";
|
|
|
|
}
|
|
|
|
return s.ascii();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possible source-status pairs after this:
|
|
|
|
// Local - Source provided
|
|
|
|
// Queue - Processing / Binary ready / Error
|
|
|
|
// Cache - Binary ready
|
|
|
|
ShaderGLES3::Version *ShaderGLES3::get_current_version(bool &r_async_forbidden) {
|
|
|
|
VersionKey effective_version;
|
|
|
|
effective_version.key = conditional_version.key;
|
|
|
|
// Store and look up ubershader with all other version bits set to zero
|
|
|
|
if ((conditional_version.version & VersionKey::UBERSHADER_FLAG)) {
|
|
|
|
effective_version.version = VersionKey::UBERSHADER_FLAG;
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
Version *_v = version_map.getptr(effective_version);
|
|
|
|
CustomCode *cc = nullptr;
|
2016-10-03 19:33:42 +00:00
|
|
|
if (_v) {
|
2021-09-26 19:31:17 +00:00
|
|
|
if (_v->compile_status == Version::COMPILE_STATUS_RESTART_NEEDED) {
|
|
|
|
_v->program_binary.source = Version::ProgramBinary::SOURCE_NONE;
|
|
|
|
} else {
|
|
|
|
if (effective_version.code_version != 0) {
|
|
|
|
cc = custom_code_map.getptr(effective_version.code_version);
|
|
|
|
ERR_FAIL_COND_V(!cc, _v);
|
|
|
|
if (cc->version == _v->code_version) {
|
|
|
|
return _v;
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-03 19:33:42 +00:00
|
|
|
return _v;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!_v) {
|
2021-09-26 19:31:17 +00:00
|
|
|
_v = &version_map[effective_version];
|
|
|
|
_v->version_key = effective_version;
|
|
|
|
_v->shader = this;
|
|
|
|
_v->uniform_location = memnew_arr(GLint, uniform_count);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
Version &v = *_v;
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
/* SETUP CONDITIONALS */
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
LocalVector<const char *> strings_common;
|
2016-11-22 23:51:56 +00:00
|
|
|
#ifdef GLES_OVER_GL
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_common.push_back("#version 330\n");
|
|
|
|
strings_common.push_back("#define GLES_OVER_GL\n");
|
2016-10-03 19:33:42 +00:00
|
|
|
#else
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_common.push_back("#version 300 es\n");
|
2016-10-03 19:33:42 +00:00
|
|
|
#endif
|
|
|
|
|
2020-01-19 07:14:10 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_common.push_back("#define ANDROID_ENABLED\n");
|
2020-01-19 07:14:10 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < custom_defines.size(); i++) {
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_common.push_back(custom_defines[i].get_data());
|
|
|
|
strings_common.push_back("\n");
|
2016-11-11 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
LocalVector<CharString> flag_macros;
|
|
|
|
bool build_ubershader = get_ubershader_flags_uniform() != -1 && (effective_version.version & VersionKey::UBERSHADER_FLAG);
|
|
|
|
if (build_ubershader) {
|
|
|
|
strings_common.push_back("#define IS_UBERSHADER\n");
|
|
|
|
for (int i = 0; i < conditional_count; i++) {
|
2021-12-11 13:25:38 +00:00
|
|
|
String s = vformat("#define FLAG_%s (1u << %du)\n", String(conditional_defines[i]).strip_edges().trim_prefix("#define "), i);
|
2021-09-26 19:31:17 +00:00
|
|
|
CharString cs = s.ascii();
|
|
|
|
flag_macros.push_back(cs);
|
|
|
|
strings_common.push_back(cs.ptr());
|
|
|
|
}
|
|
|
|
strings_common.push_back("\n");
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < conditional_count; i++) {
|
|
|
|
bool enable = ((1 << i) & effective_version.version);
|
|
|
|
strings_common.push_back(enable ? conditional_defines[i] : "");
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (enable) {
|
|
|
|
DEBUG_PRINT(conditional_defines[i]);
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 15:27:52 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
//keep them around during the function
|
2021-09-26 19:31:17 +00:00
|
|
|
struct {
|
|
|
|
CharString code_string;
|
|
|
|
CharString code_globals;
|
|
|
|
CharString material_string;
|
|
|
|
} vert;
|
|
|
|
struct {
|
|
|
|
CharString code_string;
|
|
|
|
CharString code_string2;
|
|
|
|
CharString code_globals;
|
|
|
|
CharString material_string;
|
|
|
|
} frag;
|
|
|
|
|
|
|
|
if (effective_version.code_version != 0) {
|
|
|
|
ERR_FAIL_COND_V(!custom_code_map.has(effective_version.code_version), nullptr);
|
|
|
|
if (!cc) {
|
|
|
|
cc = &custom_code_map[effective_version.code_version];
|
|
|
|
}
|
|
|
|
if (cc->version != v.code_version) {
|
|
|
|
v.code_version = cc->version;
|
|
|
|
v.async_mode = cc->async_mode;
|
|
|
|
v.uniforms_ready = false;
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CREATE PROGRAM */
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
v.ids.main = glCreateProgram();
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(v.ids.main == 0, nullptr);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
// To create the ubershader we need to modify the static strings;
|
|
|
|
// they'll go in this array
|
|
|
|
LocalVector<CharString> filtered_strings;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
/* VERTEX SHADER */
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
if (cc) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < cc->custom_defines.size(); i++) {
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_common.push_back(cc->custom_defines[i].get_data());
|
2017-03-05 15:44:50 +00:00
|
|
|
DEBUG_PRINT("CD #" + itos(i) + ": " + String(cc->custom_defines[i]));
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
LocalVector<const char *> strings_vertex(strings_common);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
//vertex precision is high
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_vertex.push_back("precision highp float;\n");
|
|
|
|
strings_vertex.push_back("precision highp int;\n");
|
2017-08-14 18:00:16 +00:00
|
|
|
#ifndef GLES_OVER_GL
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_vertex.push_back("precision highp sampler2D;\n");
|
|
|
|
strings_vertex.push_back("precision highp samplerCube;\n");
|
|
|
|
strings_vertex.push_back("precision highp sampler2DArray;\n");
|
2017-08-14 18:00:16 +00:00
|
|
|
#endif
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(vertex_code0);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_vertex.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_vertex.push_back(vertex_code0.get_data());
|
|
|
|
}
|
2017-06-27 01:58:03 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
if (cc) {
|
2021-09-26 19:31:17 +00:00
|
|
|
vert.material_string = cc->uniforms.ascii();
|
|
|
|
strings_vertex.push_back(vert.material_string.get_data());
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2016-10-10 21:31:01 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(vertex_code1);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_vertex.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_vertex.push_back(vertex_code1.get_data());
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2016-10-10 21:31:01 +00:00
|
|
|
if (cc) {
|
2021-09-26 19:31:17 +00:00
|
|
|
vert.code_globals = cc->vertex_globals.ascii();
|
|
|
|
strings_vertex.push_back(vert.code_globals.get_data());
|
2016-10-10 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(vertex_code2);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_vertex.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_vertex.push_back(vertex_code2.get_data());
|
|
|
|
}
|
2016-10-10 21:31:01 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
if (cc) {
|
2021-09-26 19:31:17 +00:00
|
|
|
vert.code_string = cc->vertex.ascii();
|
|
|
|
strings_vertex.push_back(vert.code_string.get_data());
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(vertex_code3);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_vertex.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_vertex.push_back(vertex_code3.get_data());
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
#ifdef DEBUG_SHADER
|
2017-03-05 15:44:50 +00:00
|
|
|
DEBUG_PRINT("\nVertex Code:\n\n" + String(code_string.get_data()));
|
2021-09-26 19:31:17 +00:00
|
|
|
for (int i = 0; i < strings_vertex.size(); i++) {
|
|
|
|
//print_line("vert strings "+itos(i)+":"+String(strings_vertex[i]));
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* FRAGMENT SHADER */
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
LocalVector<const char *> strings_fragment(strings_common);
|
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
//fragment precision is medium
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_fragment.push_back("precision highp float;\n");
|
|
|
|
strings_fragment.push_back("precision highp int;\n");
|
2017-08-14 18:00:16 +00:00
|
|
|
#ifndef GLES_OVER_GL
|
2021-09-26 19:31:17 +00:00
|
|
|
strings_fragment.push_back("precision highp sampler2D;\n");
|
|
|
|
strings_fragment.push_back("precision highp samplerCube;\n");
|
|
|
|
strings_fragment.push_back("precision highp sampler2DArray;\n");
|
2017-08-14 18:00:16 +00:00
|
|
|
#endif
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(fragment_code0);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_fragment.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_fragment.push_back(fragment_code0.get_data());
|
|
|
|
}
|
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
if (cc) {
|
2021-09-26 19:31:17 +00:00
|
|
|
frag.material_string = cc->uniforms.ascii();
|
|
|
|
strings_fragment.push_back(frag.material_string.get_data());
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2016-10-10 21:31:01 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(fragment_code1);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_fragment.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_fragment.push_back(fragment_code1.get_data());
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2016-10-10 21:31:01 +00:00
|
|
|
if (cc) {
|
2021-09-26 19:31:17 +00:00
|
|
|
frag.code_globals = cc->fragment_globals.ascii();
|
|
|
|
strings_fragment.push_back(frag.code_globals.get_data());
|
2016-10-10 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(fragment_code2);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_fragment.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_fragment.push_back(fragment_code2.get_data());
|
|
|
|
}
|
2016-10-10 21:31:01 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
if (cc) {
|
2021-09-26 19:31:17 +00:00
|
|
|
frag.code_string = cc->light.ascii();
|
|
|
|
strings_fragment.push_back(frag.code_string.get_data());
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(fragment_code3);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_fragment.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_fragment.push_back(fragment_code3.get_data());
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
if (cc) {
|
2021-09-26 19:31:17 +00:00
|
|
|
frag.code_string2 = cc->fragment.ascii();
|
|
|
|
strings_fragment.push_back(frag.code_string2.get_data());
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (build_ubershader) {
|
|
|
|
CharString s = _prepare_ubershader_chunk(fragment_code4);
|
|
|
|
filtered_strings.push_back(s);
|
|
|
|
strings_fragment.push_back(s.get_data());
|
|
|
|
} else {
|
|
|
|
strings_fragment.push_back(fragment_code4.get_data());
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SHADER
|
2017-07-08 11:06:13 +00:00
|
|
|
DEBUG_PRINT("\nFragment Globals:\n\n" + String(code_globals.get_data()));
|
|
|
|
DEBUG_PRINT("\nFragment Code:\n\n" + String(code_string2.get_data()));
|
2021-09-26 19:31:17 +00:00
|
|
|
for (int i = 0; i < strings_fragment.size(); i++) {
|
|
|
|
//print_line("frag strings "+itos(i)+":"+String(strings_fragment[i]));
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (!r_async_forbidden) {
|
|
|
|
r_async_forbidden =
|
|
|
|
(v.async_mode == ASYNC_MODE_HIDDEN && async_hidden_forbidden) ||
|
|
|
|
(v.async_mode == ASYNC_MODE_VISIBLE && get_ubershader_flags_uniform() == -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool in_cache = false;
|
|
|
|
if (shader_cache && effective_version.is_subject_to_caching()) {
|
|
|
|
const char *strings_platform[] = {
|
|
|
|
reinterpret_cast<const char *>(glGetString(GL_VENDOR)),
|
|
|
|
reinterpret_cast<const char *>(glGetString(GL_RENDERER)),
|
|
|
|
reinterpret_cast<const char *>(glGetString(GL_VERSION)),
|
|
|
|
nullptr,
|
|
|
|
};
|
|
|
|
v.program_binary.cache_hash = ShaderCacheGLES3::hash_program(strings_platform, strings_vertex, strings_fragment);
|
|
|
|
if (shader_cache->retrieve(v.program_binary.cache_hash, &v.program_binary.format, &v.program_binary.data)) {
|
|
|
|
in_cache = true;
|
|
|
|
v.program_binary.source = Version::ProgramBinary::SOURCE_CACHE;
|
|
|
|
v.compile_status = Version::COMPILE_STATUS_BINARY_READY_FROM_CACHE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!in_cache) {
|
|
|
|
if (compile_queue && !r_async_forbidden) {
|
|
|
|
// Asynchronous compilation via queue (secondary context)
|
|
|
|
// Remarks:
|
|
|
|
// 1. We need to save vertex and fragment strings because they will not live beyond this function.
|
|
|
|
// 2. We'll create another program since the other GL context is not shared.
|
|
|
|
// We are doing it that way since GL drivers can implement context sharing via locking, which
|
|
|
|
// would render (no pun intended) this whole effort to asynchronous useless.
|
|
|
|
|
|
|
|
auto concat_shader_strings = [](const LocalVector<const char *> &p_shader_strings, LocalVector<char> *r_out) {
|
|
|
|
r_out->clear();
|
|
|
|
for (uint32_t i = 0; i < p_shader_strings.size(); i++) {
|
|
|
|
uint32_t initial_size = r_out->size();
|
|
|
|
uint32_t piece_len = strlen(reinterpret_cast<const char *>(p_shader_strings[i]));
|
|
|
|
r_out->resize(initial_size + piece_len + 1);
|
|
|
|
memcpy(r_out->ptr() + initial_size, p_shader_strings[i], piece_len);
|
|
|
|
*(r_out->ptr() + initial_size + piece_len) = '\n';
|
|
|
|
}
|
|
|
|
*(r_out->ptr() + r_out->size() - 1) = '\0';
|
|
|
|
};
|
|
|
|
|
|
|
|
LocalVector<char> vertex_code;
|
|
|
|
concat_shader_strings(strings_vertex, &vertex_code);
|
|
|
|
LocalVector<char> fragment_code;
|
|
|
|
concat_shader_strings(strings_fragment, &fragment_code);
|
|
|
|
|
|
|
|
v.program_binary.source = Version::ProgramBinary::SOURCE_QUEUE;
|
|
|
|
v.compile_status = Version::COMPILE_STATUS_PROCESSING_AT_QUEUE;
|
|
|
|
versions_compiling.add_last(&v.compiling_list);
|
|
|
|
active_compiles_count++;
|
2022-06-13 16:01:19 +00:00
|
|
|
*max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
|
2021-09-26 19:31:17 +00:00
|
|
|
_log_active_compiles();
|
|
|
|
(*compiles_started_this_frame)++;
|
|
|
|
|
|
|
|
compile_queue->enqueue(v.ids.main, [this, &v, vertex_code, fragment_code]() {
|
|
|
|
Version::Ids async_ids;
|
|
|
|
async_ids.main = glCreateProgram();
|
|
|
|
async_ids.vert = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
async_ids.frag = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
|
|
|
|
LocalVector<const char *> async_strings_vertex;
|
|
|
|
async_strings_vertex.push_back(vertex_code.ptr());
|
|
|
|
LocalVector<const char *> async_strings_fragment;
|
|
|
|
async_strings_fragment.push_back(fragment_code.ptr());
|
|
|
|
|
|
|
|
_set_source(async_ids, async_strings_vertex, async_strings_fragment);
|
|
|
|
glCompileShader(async_ids.vert);
|
|
|
|
glCompileShader(async_ids.frag);
|
|
|
|
if (_complete_compile(async_ids, true) && _complete_link(async_ids, &v.program_binary.format, &v.program_binary.data)) {
|
|
|
|
glDeleteShader(async_ids.frag);
|
|
|
|
glDeleteShader(async_ids.vert);
|
|
|
|
glDeleteProgram(async_ids.main);
|
|
|
|
v.program_binary.result_from_queue.set(1);
|
|
|
|
} else {
|
|
|
|
v.program_binary.result_from_queue.set(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Synchronous compilation, or async. via native support
|
|
|
|
v.ids.vert = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
v.ids.frag = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
_set_source(v.ids, strings_vertex, strings_fragment);
|
|
|
|
v.program_binary.source = Version::ProgramBinary::SOURCE_LOCAL;
|
|
|
|
v.compile_status = Version::COMPILE_STATUS_SOURCE_PROVIDED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cc) {
|
|
|
|
cc->versions.insert(effective_version.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGLES3::_set_source(Version::Ids p_ids, const LocalVector<const char *> &p_vertex_strings, const LocalVector<const char *> &p_fragment_strings) const {
|
|
|
|
glShaderSource(p_ids.vert, p_vertex_strings.size(), p_vertex_strings.ptr(), nullptr);
|
|
|
|
glShaderSource(p_ids.frag, p_fragment_strings.size(), p_fragment_strings.ptr(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderGLES3::_complete_compile(Version::Ids p_ids, bool p_retrievable) const {
|
|
|
|
GLint status;
|
|
|
|
|
|
|
|
glGetShaderiv(p_ids.vert, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status == GL_FALSE) {
|
|
|
|
// error compiling
|
|
|
|
GLsizei iloglen;
|
|
|
|
glGetShaderiv(p_ids.vert, GL_INFO_LOG_LENGTH, &iloglen);
|
|
|
|
|
|
|
|
if (iloglen < 0) {
|
|
|
|
glDeleteShader(p_ids.frag);
|
|
|
|
glDeleteShader(p_ids.vert);
|
|
|
|
glDeleteProgram(p_ids.main);
|
|
|
|
|
|
|
|
ERR_PRINT("Vertex shader compilation failed with empty log");
|
|
|
|
} else {
|
|
|
|
if (iloglen == 0) {
|
|
|
|
iloglen = 4096; //buggy driver (Adreno 220+....)
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ilogmem = (char *)memalloc(iloglen + 1);
|
|
|
|
ilogmem[iloglen] = 0;
|
|
|
|
glGetShaderInfoLog(p_ids.vert, iloglen, &iloglen, ilogmem);
|
|
|
|
|
|
|
|
String err_string = get_shader_name() + ": Vertex Program Compilation Failed:\n";
|
|
|
|
|
|
|
|
err_string += ilogmem;
|
|
|
|
_display_error_with_code(err_string, p_ids.vert);
|
|
|
|
ERR_PRINT(err_string.ascii().get_data());
|
|
|
|
memfree(ilogmem);
|
|
|
|
glDeleteShader(p_ids.frag);
|
|
|
|
glDeleteShader(p_ids.vert);
|
|
|
|
glDeleteProgram(p_ids.main);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
glGetShaderiv(p_ids.frag, GL_COMPILE_STATUS, &status);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (status == GL_FALSE) {
|
|
|
|
// error compiling
|
2016-10-03 19:33:42 +00:00
|
|
|
GLsizei iloglen;
|
2021-09-26 19:31:17 +00:00
|
|
|
glGetShaderiv(p_ids.frag, GL_INFO_LOG_LENGTH, &iloglen);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
if (iloglen < 0) {
|
2021-09-26 19:31:17 +00:00
|
|
|
glDeleteShader(p_ids.frag);
|
|
|
|
glDeleteShader(p_ids.vert);
|
|
|
|
glDeleteProgram(p_ids.main);
|
2017-12-05 14:41:38 +00:00
|
|
|
ERR_PRINT("Fragment shader compilation failed with empty log");
|
2016-10-03 19:33:42 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (iloglen == 0) {
|
2016-10-03 19:33:42 +00:00
|
|
|
iloglen = 4096; //buggy driver (Adreno 220+....)
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
char *ilogmem = (char *)memalloc(iloglen + 1);
|
|
|
|
ilogmem[iloglen] = 0;
|
2021-09-26 19:31:17 +00:00
|
|
|
glGetShaderInfoLog(p_ids.frag, iloglen, &iloglen, ilogmem);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
String err_string = get_shader_name() + ": Fragment Program Compilation Failed:\n";
|
|
|
|
|
|
|
|
err_string += ilogmem;
|
2021-09-26 19:31:17 +00:00
|
|
|
_display_error_with_code(err_string, p_ids.frag);
|
2016-10-03 19:33:42 +00:00
|
|
|
ERR_PRINT(err_string.ascii().get_data());
|
2017-01-06 13:15:44 +00:00
|
|
|
memfree(ilogmem);
|
2021-09-26 19:31:17 +00:00
|
|
|
glDeleteShader(p_ids.frag);
|
|
|
|
glDeleteShader(p_ids.vert);
|
|
|
|
glDeleteProgram(p_ids.main);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2016-10-10 21:31:01 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
return false;
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
glAttachShader(p_ids.main, p_ids.frag);
|
|
|
|
glAttachShader(p_ids.main, p_ids.vert);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
// bind attributes before linking
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < attribute_pair_count; i++) {
|
2021-09-26 19:31:17 +00:00
|
|
|
glBindAttribLocation(p_ids.main, attribute_pairs[i].index, attribute_pairs[i].name);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 23:46:55 +00:00
|
|
|
//if feedback exists, set it up
|
|
|
|
|
|
|
|
if (feedback_count) {
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<const char *> feedback;
|
|
|
|
for (int i = 0; i < feedback_count; i++) {
|
|
|
|
if (feedbacks[i].conditional == -1 || (1 << feedbacks[i].conditional) & conditional_version.version) {
|
2016-11-24 23:46:55 +00:00
|
|
|
//conditional for this feedback is enabled
|
|
|
|
feedback.push_back(feedbacks[i].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feedback.size()) {
|
2021-09-26 19:31:17 +00:00
|
|
|
glTransformFeedbackVaryings(p_ids.main, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS);
|
2016-11-24 23:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (p_retrievable) {
|
|
|
|
glProgramParameteri(p_ids.main, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
|
|
|
|
}
|
|
|
|
glLinkProgram(p_ids.main);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
bool ShaderGLES3::_complete_link(Version::Ids p_ids, GLenum *r_program_format, PoolByteArray *r_program_binary) const {
|
|
|
|
GLint status;
|
|
|
|
glGetProgramiv(p_ids.main, GL_LINK_STATUS, &status);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
if (status == GL_FALSE) {
|
|
|
|
// error linking
|
2016-10-03 19:33:42 +00:00
|
|
|
GLsizei iloglen;
|
2021-09-26 19:31:17 +00:00
|
|
|
glGetProgramiv(p_ids.main, GL_INFO_LOG_LENGTH, &iloglen);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
if (iloglen < 0) {
|
2021-09-26 19:31:17 +00:00
|
|
|
glDeleteShader(p_ids.frag);
|
|
|
|
glDeleteShader(p_ids.vert);
|
|
|
|
glDeleteProgram(p_ids.main);
|
|
|
|
ERR_FAIL_COND_V(iloglen < 0, false);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (iloglen == 0) {
|
2016-10-03 19:33:42 +00:00
|
|
|
iloglen = 4096; //buggy driver (Adreno 220+....)
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
|
|
|
|
ilogmem[iloglen] = 0;
|
2021-09-26 19:31:17 +00:00
|
|
|
glGetProgramInfoLog(p_ids.main, iloglen, &iloglen, ilogmem);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
String err_string = get_shader_name() + ": Program LINK FAILED:\n";
|
|
|
|
|
|
|
|
err_string += ilogmem;
|
2016-10-03 19:33:42 +00:00
|
|
|
ERR_PRINT(err_string.ascii().get_data());
|
|
|
|
Memory::free_static(ilogmem);
|
2021-09-26 19:31:17 +00:00
|
|
|
glDeleteShader(p_ids.frag);
|
|
|
|
glDeleteShader(p_ids.vert);
|
|
|
|
glDeleteProgram(p_ids.main);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
return false;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (r_program_binary) {
|
|
|
|
GLint program_len;
|
|
|
|
glGetProgramiv(p_ids.main, GL_PROGRAM_BINARY_LENGTH, &program_len);
|
|
|
|
r_program_binary->resize(program_len);
|
|
|
|
PoolByteArray::Write w = r_program_binary->write();
|
|
|
|
glGetProgramBinary(p_ids.main, program_len, NULL, r_program_format, w.ptr());
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
void ShaderGLES3::_setup_uniforms(CustomCode *p_cc) const {
|
2016-10-03 19:33:42 +00:00
|
|
|
//print_line("uniforms: ");
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < uniform_count; j++) {
|
2021-09-26 19:31:17 +00:00
|
|
|
version->uniform_location[j] = glGetUniformLocation(version->ids.main, uniform_names[j]);
|
|
|
|
//print_line("uniform "+String(uniform_names[j])+" location "+itos(version->uniform_location[j]));
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
// set texture uniforms
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < texunit_pair_count; i++) {
|
2021-09-26 19:31:17 +00:00
|
|
|
GLint loc = glGetUniformLocation(version->ids.main, texunit_pairs[i].name);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (loc >= 0) {
|
|
|
|
if (texunit_pairs[i].index < 0) {
|
|
|
|
glUniform1i(loc, max_image_units + texunit_pairs[i].index); //negative, goes down
|
2016-10-03 19:33:42 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
glUniform1i(loc, texunit_pairs[i].index);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign uniform block bind points
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < ubo_count; i++) {
|
2021-09-26 19:31:17 +00:00
|
|
|
GLint loc = glGetUniformBlockIndex(version->ids.main, ubo_pairs[i].name);
|
|
|
|
if (loc >= 0)
|
|
|
|
glUniformBlockBinding(version->ids.main, loc, ubo_pairs[i].index);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
if (p_cc) {
|
|
|
|
version->texture_uniform_locations.resize(p_cc->texture_uniforms.size());
|
|
|
|
for (int i = 0; i < p_cc->texture_uniforms.size(); i++) {
|
|
|
|
version->texture_uniform_locations.write[i] = glGetUniformLocation(version->ids.main, String(p_cc->texture_uniforms[i]).ascii().get_data());
|
|
|
|
glUniform1i(version->texture_uniform_locations[i], i + base_material_tex_index);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 19:31:17 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
void ShaderGLES3::_dispose_program(Version *p_version) {
|
|
|
|
if (compile_queue) {
|
|
|
|
if (p_version->compile_status == Version::COMPILE_STATUS_PROCESSING_AT_QUEUE) {
|
|
|
|
compile_queue->cancel(p_version->ids.main);
|
|
|
|
}
|
2019-01-25 22:28:27 +00:00
|
|
|
}
|
2021-09-26 19:31:17 +00:00
|
|
|
glDeleteShader(p_version->ids.vert);
|
|
|
|
glDeleteShader(p_version->ids.frag);
|
|
|
|
glDeleteProgram(p_version->ids.main);
|
|
|
|
|
|
|
|
if (p_version->compiling_list.in_list()) {
|
|
|
|
p_version->compiling_list.remove_from_list();
|
|
|
|
active_compiles_count--;
|
2022-06-13 16:01:19 +00:00
|
|
|
#ifdef DEV_ENABLED
|
|
|
|
CRASH_COND(active_compiles_count == UINT32_MAX);
|
|
|
|
#endif
|
2021-09-26 19:31:17 +00:00
|
|
|
if (p_version->compile_status == Version::COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT) {
|
|
|
|
active_compiles_count--;
|
2022-06-13 16:01:19 +00:00
|
|
|
#ifdef DEV_ENABLED
|
|
|
|
CRASH_COND(active_compiles_count == UINT32_MAX);
|
|
|
|
#endif
|
2021-09-26 19:31:17 +00:00
|
|
|
}
|
|
|
|
_log_active_compiles();
|
|
|
|
}
|
|
|
|
p_version->compile_status = Version::COMPILE_STATUS_ERROR;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
GLint ShaderGLES3::get_uniform_location(const String &p_name) const {
|
|
|
|
ERR_FAIL_COND_V(!version, -1);
|
2021-09-26 19:31:17 +00:00
|
|
|
return glGetUniformLocation(version->ids.main, p_name.ascii().get_data());
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00: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 19:33:42 +00:00
|
|
|
ERR_FAIL_COND(version);
|
2017-03-05 15:44:50 +00: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 19:33:42 +00:00
|
|
|
|
2017-12-05 14:41:38 +00:00
|
|
|
//split vertex and shader code (thank you, shader compiler programmers from you know what company).
|
2016-10-03 19:33:42 +00:00
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
String globals_tag = "\nVERTEX_SHADER_GLOBALS";
|
|
|
|
String material_tag = "\nMATERIAL_UNIFORMS";
|
|
|
|
String code_tag = "\nVERTEX_SHADER_CODE";
|
|
|
|
String code = vertex_code;
|
2017-06-27 01:58:03 +00:00
|
|
|
int cpos = code.find(material_tag);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cpos == -1) {
|
|
|
|
vertex_code0 = code.ascii();
|
2016-10-03 19:33:42 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
vertex_code0 = code.substr(0, cpos).ascii();
|
2017-06-27 01:58:03 +00:00
|
|
|
code = code.substr(cpos + material_tag.length(), code.length());
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2017-06-27 01:58:03 +00:00
|
|
|
cpos = code.find(globals_tag);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cpos == -1) {
|
|
|
|
vertex_code1 = code.ascii();
|
2016-10-03 19:33:42 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
vertex_code1 = code.substr(0, cpos).ascii();
|
2017-06-27 01:58:03 +00:00
|
|
|
String code2 = code.substr(cpos + globals_tag.length(), code.length());
|
2016-10-10 21:31:01 +00:00
|
|
|
|
|
|
|
cpos = code2.find(code_tag);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cpos == -1) {
|
|
|
|
vertex_code2 = code2.ascii();
|
2016-10-10 21:31:01 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
vertex_code2 = code2.substr(0, cpos).ascii();
|
|
|
|
vertex_code3 = code2.substr(cpos + code_tag.length(), code2.length()).ascii();
|
2016-10-10 21:31:01 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-03-05 15:44:50 +00: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-27 01:58:03 +00:00
|
|
|
int cpos = code.find(material_tag);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cpos == -1) {
|
|
|
|
fragment_code0 = code.ascii();
|
2016-10-03 19:33:42 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
fragment_code0 = code.substr(0, cpos).ascii();
|
2016-10-19 14:14:41 +00:00
|
|
|
//print_line("CODE0:\n"+String(fragment_code0.get_data()));
|
2017-06-27 01:58:03 +00:00
|
|
|
code = code.substr(cpos + material_tag.length(), code.length());
|
|
|
|
cpos = code.find(globals_tag);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cpos == -1) {
|
|
|
|
fragment_code1 = code.ascii();
|
2016-10-03 19:33:42 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
fragment_code1 = code.substr(0, cpos).ascii();
|
2016-10-19 14:14:41 +00:00
|
|
|
//print_line("CODE1:\n"+String(fragment_code1.get_data()));
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2017-06-27 01:58:03 +00:00
|
|
|
String code2 = code.substr(cpos + globals_tag.length(), code.length());
|
2017-06-01 21:55:24 +00:00
|
|
|
cpos = code2.find(light_code_tag);
|
2016-10-19 14:14:41 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cpos == -1) {
|
|
|
|
fragment_code2 = code2.ascii();
|
2016-10-03 19:33:42 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
fragment_code2 = code2.substr(0, cpos).ascii();
|
2016-10-19 14:14:41 +00:00
|
|
|
//print_line("CODE2:\n"+String(fragment_code2.get_data()));
|
|
|
|
|
2017-06-01 21:55:24 +00:00
|
|
|
String code3 = code2.substr(cpos + light_code_tag.length(), code2.length());
|
2016-10-10 21:31:01 +00:00
|
|
|
|
2017-06-01 21:55:24 +00:00
|
|
|
cpos = code3.find(code_tag);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cpos == -1) {
|
|
|
|
fragment_code3 = code3.ascii();
|
2016-10-10 21:31:01 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
fragment_code3 = code3.substr(0, cpos).ascii();
|
2017-01-14 11:26:56 +00:00
|
|
|
//print_line("CODE3:\n"+String(fragment_code3.get_data()));
|
2017-06-01 21:55:24 +00:00
|
|
|
fragment_code4 = code3.substr(cpos + code_tag.length(), code3.length()).ascii();
|
2016-10-19 14:14:41 +00:00
|
|
|
//print_line("CODE4:\n"+String(fragment_code4.get_data()));
|
2016-10-10 21:31:01 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_image_units);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
void ShaderGLES3::init_async_compilation() {
|
|
|
|
if (is_async_compilation_supported() && get_ubershader_flags_uniform() != -1) {
|
|
|
|
// Warm up the ubershader for the case of no custom code
|
|
|
|
new_conditional_version.code_version = 0;
|
|
|
|
_bind_ubershader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderGLES3::is_async_compilation_supported() {
|
|
|
|
return max_simultaneous_compiles > 0 && (compile_queue || parallel_compile_supported);
|
|
|
|
}
|
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
void ShaderGLES3::finish() {
|
2021-05-04 14:00:45 +00:00
|
|
|
const VersionKey *V = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
while ((V = version_map.next(V))) {
|
|
|
|
Version &v = version_map[*V];
|
2021-09-26 19:31:17 +00:00
|
|
|
_dispose_program(&v);
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete_arr(v.uniform_location);
|
|
|
|
}
|
2021-09-26 19:31:17 +00:00
|
|
|
ERR_FAIL_COND(versions_compiling.first());
|
|
|
|
ERR_FAIL_COND(active_compiles_count != 0);
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
void ShaderGLES3::clear_caches() {
|
2021-05-04 14:00:45 +00:00
|
|
|
const VersionKey *V = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
while ((V = version_map.next(V))) {
|
|
|
|
Version &v = version_map[*V];
|
2021-09-26 19:31:17 +00:00
|
|
|
_dispose_program(&v);
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete_arr(v.uniform_location);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2021-09-26 19:31:17 +00:00
|
|
|
ERR_FAIL_COND(versions_compiling.first());
|
|
|
|
ERR_FAIL_COND(active_compiles_count != 0);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
version_map.clear();
|
|
|
|
|
|
|
|
custom_code_map.clear();
|
2021-05-04 14:00:45 +00:00
|
|
|
version = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
last_custom_code = 1;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ShaderGLES3::create_custom_shader() {
|
2017-03-05 15:44:50 +00:00
|
|
|
custom_code_map[last_custom_code] = CustomCode();
|
|
|
|
custom_code_map[last_custom_code].version = 1;
|
2016-10-03 19:33:42 +00:00
|
|
|
return last_custom_code++;
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00: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, AsyncMode p_async_mode) {
|
2016-10-03 19:33:42 +00:00
|
|
|
ERR_FAIL_COND(!custom_code_map.has(p_code_id));
|
2017-03-05 15:44:50 +00: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;
|
2021-09-26 19:31:17 +00:00
|
|
|
cc->async_mode = p_async_mode;
|
2016-10-03 19:33:42 +00:00
|
|
|
cc->version++;
|
2021-09-26 19:31:17 +00:00
|
|
|
|
|
|
|
if (p_async_mode == ASYNC_MODE_VISIBLE && is_async_compilation_supported() && get_ubershader_flags_uniform() != -1) {
|
|
|
|
// Warm up the ubershader for this custom code
|
|
|
|
new_conditional_version.code_version = p_code_id;
|
|
|
|
_bind_ubershader();
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGLES3::set_custom_shader(uint32_t p_code_id) {
|
2017-03-05 15:44:50 +00:00
|
|
|
new_conditional_version.code_version = p_code_id;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGLES3::free_custom_shader(uint32_t p_code_id) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(!custom_code_map.has(p_code_id));
|
2019-01-26 13:39:03 +00:00
|
|
|
if (conditional_version.code_version == p_code_id) {
|
2019-01-25 22:28:27 +00:00
|
|
|
conditional_version.code_version = 0; //do not keep using a version that is going away
|
2019-01-26 13:39:03 +00:00
|
|
|
unbind();
|
|
|
|
}
|
2019-01-25 22:28:27 +00:00
|
|
|
|
|
|
|
VersionKey key;
|
|
|
|
key.code_version = p_code_id;
|
|
|
|
for (Set<uint32_t>::Element *E = custom_code_map[p_code_id].versions.front(); E; E = E->next()) {
|
|
|
|
key.version = E->get();
|
|
|
|
ERR_CONTINUE(!version_map.has(key));
|
|
|
|
Version &v = version_map[key];
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
_dispose_program(&v);
|
2019-01-25 22:28:27 +00:00
|
|
|
memdelete_arr(v.uniform_location);
|
|
|
|
|
|
|
|
version_map.erase(key);
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
custom_code_map.erase(p_code_id);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:31:01 +00:00
|
|
|
void ShaderGLES3::set_base_material_tex_index(int p_idx) {
|
2017-03-05 15:44:50 +00:00
|
|
|
base_material_tex_index = p_idx;
|
2016-10-10 21:31:01 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
ShaderGLES3::ShaderGLES3() {
|
2021-05-04 14:00:45 +00:00
|
|
|
version = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
last_custom_code = 1;
|
|
|
|
base_material_tex_index = 0;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderGLES3::~ShaderGLES3() {
|
|
|
|
finish();
|
|
|
|
}
|