2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* shader_gles3.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-10-26 17:18:39 +02:00
|
|
|
#include "shader_gles3.h"
|
2022-02-16 13:56:32 +01:00
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
#ifdef GLES3_ENABLED
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
#include "core/io/compression.h"
|
|
|
|
#include "core/io/dir_access.h"
|
|
|
|
#include "core/io/file_access.h"
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
#include "drivers/gles3/rasterizer_gles3.h"
|
|
|
|
|
2022-12-06 03:01:24 +01:00
|
|
|
static String _mkid(const String &p_id) {
|
|
|
|
String id = "m_" + p_id.replace("__", "_dus_");
|
|
|
|
return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::_add_stage(const char *p_code, StageType p_stage_type) {
|
|
|
|
Vector<String> lines = String(p_code).split("\n");
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
String text;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
2023-11-18 23:40:56 +01:00
|
|
|
const String &l = lines[i];
|
2021-11-13 12:54:58 +01:00
|
|
|
bool push_chunk = false;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
StageTemplate::Chunk chunk;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
if (l.begins_with("#GLOBALS")) {
|
|
|
|
switch (p_stage_type) {
|
|
|
|
case STAGE_TYPE_VERTEX:
|
|
|
|
chunk.type = StageTemplate::Chunk::TYPE_VERTEX_GLOBALS;
|
|
|
|
break;
|
|
|
|
case STAGE_TYPE_FRAGMENT:
|
|
|
|
chunk.type = StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS;
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
push_chunk = true;
|
|
|
|
} else if (l.begins_with("#MATERIAL_UNIFORMS")) {
|
|
|
|
chunk.type = StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS;
|
|
|
|
push_chunk = true;
|
|
|
|
} else if (l.begins_with("#CODE")) {
|
|
|
|
chunk.type = StageTemplate::Chunk::TYPE_CODE;
|
|
|
|
push_chunk = true;
|
|
|
|
chunk.code = l.replace_first("#CODE", String()).replace(":", "").strip_edges().to_upper();
|
|
|
|
} else {
|
|
|
|
text += l + "\n";
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
if (push_chunk) {
|
|
|
|
if (text != String()) {
|
|
|
|
StageTemplate::Chunk text_chunk;
|
|
|
|
text_chunk.type = StageTemplate::Chunk::TYPE_TEXT;
|
|
|
|
text_chunk.text = text.utf8();
|
|
|
|
stage_templates[p_stage_type].chunks.push_back(text_chunk);
|
|
|
|
text = String();
|
|
|
|
}
|
|
|
|
stage_templates[p_stage_type].chunks.push_back(chunk);
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
if (text != String()) {
|
|
|
|
StageTemplate::Chunk text_chunk;
|
|
|
|
text_chunk.type = StageTemplate::Chunk::TYPE_TEXT;
|
|
|
|
text_chunk.text = text.utf8();
|
|
|
|
stage_templates[p_stage_type].chunks.push_back(text_chunk);
|
|
|
|
text = String();
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 07:40:03 +01:00
|
|
|
void ShaderGLES3::_setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_name, int p_uniform_count, const char **p_uniform_names, int p_ubo_count, const UBOPair *p_ubos, int p_feedback_count, const Feedback *p_feedback, int p_texture_count, const TexUnitPair *p_tex_units, int p_specialization_count, const Specialization *p_specializations, int p_variant_count, const char **p_variants) {
|
2021-11-13 12:54:58 +01:00
|
|
|
name = p_name;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
if (p_vertex_code) {
|
|
|
|
_add_stage(p_vertex_code, STAGE_TYPE_VERTEX);
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
if (p_fragment_code) {
|
|
|
|
_add_stage(p_fragment_code, STAGE_TYPE_FRAGMENT);
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
uniform_names = p_uniform_names;
|
|
|
|
uniform_count = p_uniform_count;
|
|
|
|
ubo_pairs = p_ubos;
|
|
|
|
ubo_count = p_ubo_count;
|
|
|
|
texunit_pairs = p_tex_units;
|
|
|
|
texunit_pair_count = p_texture_count;
|
|
|
|
specializations = p_specializations;
|
|
|
|
specialization_count = p_specialization_count;
|
|
|
|
specialization_default_mask = 0;
|
|
|
|
for (int i = 0; i < specialization_count; i++) {
|
2021-11-16 16:25:42 +01:00
|
|
|
if (specializations[i].default_value) {
|
|
|
|
specialization_default_mask |= (uint64_t(1) << uint64_t(i));
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
variant_defines = p_variants;
|
|
|
|
variant_count = p_variant_count;
|
2022-11-08 07:40:03 +01:00
|
|
|
feedbacks = p_feedback;
|
|
|
|
feedback_count = p_feedback_count;
|
2021-11-13 12:54:58 +01:00
|
|
|
|
|
|
|
StringBuilder tohash;
|
|
|
|
/*
|
|
|
|
tohash.append("[SpirvCacheKey]");
|
|
|
|
tohash.append(RenderingDevice::get_singleton()->shader_get_spirv_cache_key());
|
|
|
|
tohash.append("[BinaryCacheKey]");
|
|
|
|
tohash.append(RenderingDevice::get_singleton()->shader_get_binary_cache_key());
|
|
|
|
*/
|
|
|
|
tohash.append("[Vertex]");
|
|
|
|
tohash.append(p_vertex_code ? p_vertex_code : "");
|
|
|
|
tohash.append("[Fragment]");
|
|
|
|
tohash.append(p_fragment_code ? p_fragment_code : "");
|
|
|
|
|
2023-09-26 12:01:10 +02:00
|
|
|
tohash.append("[gl_implementation]");
|
2023-10-30 16:24:31 +01:00
|
|
|
const String &vendor = String::utf8((const char *)glGetString(GL_VENDOR));
|
|
|
|
tohash.append(vendor.is_empty() ? "unknown" : vendor);
|
|
|
|
const String &renderer = String::utf8((const char *)glGetString(GL_RENDERER));
|
|
|
|
tohash.append(renderer.is_empty() ? "unknown" : renderer);
|
|
|
|
const String &version = String::utf8((const char *)glGetString(GL_VERSION));
|
|
|
|
tohash.append(version.is_empty() ? "unknown" : version);
|
2023-09-26 12:01:10 +02:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
base_sha256 = tohash.as_string().sha256_text();
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
RID ShaderGLES3::version_create() {
|
|
|
|
//initialize() was never called
|
|
|
|
ERR_FAIL_COND_V(variant_count == 0, RID());
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
Version version;
|
|
|
|
return version_owner.make_rid(version);
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2022-09-04 16:56:24 +02:00
|
|
|
void ShaderGLES3::_build_variant_code(StringBuilder &builder, uint32_t p_variant, const Version *p_version, StageType p_stage_type, uint64_t p_specialization) {
|
2021-11-12 13:49:49 +01:00
|
|
|
if (RasterizerGLES3::is_gles_over_gl()) {
|
|
|
|
builder.append("#version 330\n");
|
|
|
|
builder.append("#define USE_GLES_OVER_GL\n");
|
|
|
|
} else {
|
|
|
|
builder.append("#version 300 es\n");
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
for (int i = 0; i < specialization_count; i++) {
|
2021-11-16 16:25:42 +01:00
|
|
|
if (p_specialization & (uint64_t(1) << uint64_t(i))) {
|
2021-11-13 12:54:58 +01:00
|
|
|
builder.append("#define " + String(specializations[i].name) + "\n");
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
if (p_version->uniforms.size()) {
|
|
|
|
builder.append("#define MATERIAL_UNIFORMS_USED\n");
|
|
|
|
}
|
|
|
|
for (const KeyValue<StringName, CharString> &E : p_version->code_sections) {
|
|
|
|
builder.append(String("#define ") + String(E.key) + "_CODE_USED\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append("\n"); //make sure defines begin at newline
|
|
|
|
builder.append(general_defines.get_data());
|
|
|
|
builder.append(variant_defines[p_variant]);
|
2022-02-20 01:08:53 +01:00
|
|
|
builder.append("\n");
|
2021-11-13 12:54:58 +01:00
|
|
|
for (int j = 0; j < p_version->custom_defines.size(); j++) {
|
|
|
|
builder.append(p_version->custom_defines[j].get_data());
|
|
|
|
}
|
|
|
|
builder.append("\n"); //make sure defines begin at newline
|
|
|
|
|
2022-09-04 16:56:24 +02:00
|
|
|
// Insert multiview extension loading, because it needs to appear before
|
|
|
|
// any non-preprocessor code (like the "precision highp..." lines below).
|
|
|
|
builder.append("#ifdef USE_MULTIVIEW\n");
|
|
|
|
builder.append("#if defined(GL_OVR_multiview2)\n");
|
|
|
|
builder.append("#extension GL_OVR_multiview2 : require\n");
|
|
|
|
builder.append("#elif defined(GL_OVR_multiview)\n");
|
|
|
|
builder.append("#extension GL_OVR_multiview : require\n");
|
|
|
|
builder.append("#endif\n");
|
|
|
|
if (p_stage_type == StageType::STAGE_TYPE_VERTEX) {
|
|
|
|
builder.append("layout(num_views=2) in;\n");
|
|
|
|
}
|
|
|
|
builder.append("#define ViewIndex gl_ViewID_OVR\n");
|
|
|
|
builder.append("#define MAX_VIEWS 2\n");
|
|
|
|
builder.append("#else\n");
|
2023-01-15 06:07:59 +01:00
|
|
|
builder.append("#define ViewIndex uint(0)\n");
|
2022-09-04 16:56:24 +02:00
|
|
|
builder.append("#define MAX_VIEWS 1\n");
|
|
|
|
builder.append("#endif\n");
|
|
|
|
|
2022-05-10 19:02:44 +02:00
|
|
|
// Default to highp precision unless specified otherwise.
|
|
|
|
builder.append("precision highp float;\n");
|
|
|
|
builder.append("precision highp int;\n");
|
2021-11-12 13:49:49 +01:00
|
|
|
if (!RasterizerGLES3::is_gles_over_gl()) {
|
|
|
|
builder.append("precision highp sampler2D;\n");
|
|
|
|
builder.append("precision highp samplerCube;\n");
|
|
|
|
builder.append("precision highp sampler2DArray;\n");
|
2023-08-07 10:41:09 +02:00
|
|
|
builder.append("precision highp sampler3D;\n");
|
2021-11-12 13:49:49 +01:00
|
|
|
}
|
2022-05-10 19:02:44 +02:00
|
|
|
|
2022-09-04 16:56:24 +02:00
|
|
|
const StageTemplate &stage_template = stage_templates[p_stage_type];
|
|
|
|
for (uint32_t i = 0; i < stage_template.chunks.size(); i++) {
|
|
|
|
const StageTemplate::Chunk &chunk = stage_template.chunks[i];
|
2021-11-13 12:54:58 +01:00
|
|
|
switch (chunk.type) {
|
|
|
|
case StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS: {
|
|
|
|
builder.append(p_version->uniforms.get_data()); //uniforms (same for vertex and fragment)
|
|
|
|
} break;
|
|
|
|
case StageTemplate::Chunk::TYPE_VERTEX_GLOBALS: {
|
|
|
|
builder.append(p_version->vertex_globals.get_data()); // vertex globals
|
|
|
|
} break;
|
|
|
|
case StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS: {
|
|
|
|
builder.append(p_version->fragment_globals.get_data()); // fragment globals
|
|
|
|
} break;
|
|
|
|
case StageTemplate::Chunk::TYPE_CODE: {
|
|
|
|
if (p_version->code_sections.has(chunk.code)) {
|
|
|
|
builder.append(p_version->code_sections[chunk.code].get_data());
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case StageTemplate::Chunk::TYPE_TEXT: {
|
|
|
|
builder.append(chunk.text.get_data());
|
|
|
|
} break;
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
static void _display_error_with_code(const String &p_error, const String &p_code) {
|
|
|
|
int line = 1;
|
|
|
|
Vector<String> lines = p_code.split("\n");
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
for (int j = 0; j < lines.size(); j++) {
|
|
|
|
print_line(itos(line) + ": " + lines[j]);
|
|
|
|
line++;
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
ERR_PRINT(p_error);
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
void ShaderGLES3::_get_uniform_locations(Version::Specialization &spec, Version *p_version) {
|
|
|
|
glUseProgram(spec.id);
|
|
|
|
|
|
|
|
spec.uniform_location.resize(uniform_count);
|
|
|
|
for (int i = 0; i < uniform_count; i++) {
|
|
|
|
spec.uniform_location[i] = glGetUniformLocation(spec.id, uniform_names[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < texunit_pair_count; i++) {
|
|
|
|
GLint loc = glGetUniformLocation(spec.id, texunit_pairs[i].name);
|
|
|
|
if (loc >= 0) {
|
|
|
|
if (texunit_pairs[i].index < 0) {
|
|
|
|
glUniform1i(loc, max_image_units + texunit_pairs[i].index);
|
|
|
|
} else {
|
|
|
|
glUniform1i(loc, texunit_pairs[i].index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < ubo_count; i++) {
|
|
|
|
GLint loc = glGetUniformBlockIndex(spec.id, ubo_pairs[i].name);
|
|
|
|
if (loc >= 0) {
|
|
|
|
glUniformBlockBinding(spec.id, loc, ubo_pairs[i].index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// textures
|
2023-03-25 10:46:33 +01:00
|
|
|
int texture_index = 0;
|
|
|
|
for (uint32_t i = 0; i < p_version->texture_uniforms.size(); i++) {
|
|
|
|
String native_uniform_name = _mkid(p_version->texture_uniforms[i].name);
|
2023-04-15 16:07:51 +02:00
|
|
|
GLint location = glGetUniformLocation(spec.id, (native_uniform_name).ascii().get_data());
|
2023-03-25 10:46:33 +01:00
|
|
|
Vector<int32_t> texture_uniform_bindings;
|
|
|
|
int texture_count = p_version->texture_uniforms[i].array_size;
|
|
|
|
for (int j = 0; j < texture_count; j++) {
|
|
|
|
texture_uniform_bindings.append(texture_index + base_texture_index);
|
|
|
|
texture_index++;
|
|
|
|
}
|
|
|
|
glUniform1iv(location, texture_uniform_bindings.size(), texture_uniform_bindings.ptr());
|
2023-04-15 16:07:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
glUseProgram(0);
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_t p_variant, Version *p_version, uint64_t p_specialization) {
|
|
|
|
spec.id = glCreateProgram();
|
|
|
|
spec.ok = false;
|
2020-11-18 19:11:30 +01:00
|
|
|
GLint status;
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
//vertex stage
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
2022-09-04 16:56:24 +02:00
|
|
|
_build_variant_code(builder, p_variant, p_version, STAGE_TYPE_VERTEX, p_specialization);
|
2021-11-13 12:54:58 +01:00
|
|
|
|
|
|
|
spec.vert_id = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
String builder_string = builder.as_string();
|
|
|
|
CharString cs = builder_string.utf8();
|
|
|
|
const char *cstr = cs.ptr();
|
|
|
|
glShaderSource(spec.vert_id, 1, &cstr, nullptr);
|
|
|
|
glCompileShader(spec.vert_id);
|
|
|
|
|
|
|
|
glGetShaderiv(spec.vert_id, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status == GL_FALSE) {
|
|
|
|
GLsizei iloglen;
|
|
|
|
glGetShaderiv(spec.vert_id, GL_INFO_LOG_LENGTH, &iloglen);
|
|
|
|
|
|
|
|
if (iloglen < 0) {
|
|
|
|
glDeleteShader(spec.vert_id);
|
|
|
|
glDeleteProgram(spec.id);
|
|
|
|
spec.id = 0;
|
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
ERR_PRINT("No OpenGL vertex shader compiler log.");
|
2021-11-13 12:54:58 +01:00
|
|
|
} else {
|
|
|
|
if (iloglen == 0) {
|
|
|
|
iloglen = 4096; // buggy driver (Adreno 220+)
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
|
2023-11-11 07:56:46 +01:00
|
|
|
memset(ilogmem, 0, iloglen + 1);
|
2021-11-13 12:54:58 +01:00
|
|
|
glGetShaderInfoLog(spec.vert_id, iloglen, &iloglen, ilogmem);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
String err_string = name + ": Vertex shader compilation failed:\n";
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
err_string += ilogmem;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
_display_error_with_code(err_string, builder_string);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
Memory::free_static(ilogmem);
|
|
|
|
glDeleteShader(spec.vert_id);
|
|
|
|
glDeleteProgram(spec.id);
|
|
|
|
spec.id = 0;
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
ERR_FAIL();
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
//fragment stage
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
2022-09-04 16:56:24 +02:00
|
|
|
_build_variant_code(builder, p_variant, p_version, STAGE_TYPE_FRAGMENT, p_specialization);
|
2021-11-13 12:54:58 +01:00
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
spec.frag_id = glCreateShader(GL_FRAGMENT_SHADER);
|
2021-11-13 12:54:58 +01:00
|
|
|
String builder_string = builder.as_string();
|
|
|
|
CharString cs = builder_string.utf8();
|
|
|
|
const char *cstr = cs.ptr();
|
2021-11-16 16:25:42 +01:00
|
|
|
glShaderSource(spec.frag_id, 1, &cstr, nullptr);
|
|
|
|
glCompileShader(spec.frag_id);
|
2021-11-13 12:54:58 +01:00
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
glGetShaderiv(spec.frag_id, GL_COMPILE_STATUS, &status);
|
2021-11-13 12:54:58 +01:00
|
|
|
if (status == GL_FALSE) {
|
|
|
|
GLsizei iloglen;
|
2021-11-16 16:25:42 +01:00
|
|
|
glGetShaderiv(spec.frag_id, GL_INFO_LOG_LENGTH, &iloglen);
|
2021-11-13 12:54:58 +01:00
|
|
|
|
|
|
|
if (iloglen < 0) {
|
2021-11-16 16:25:42 +01:00
|
|
|
glDeleteShader(spec.frag_id);
|
2021-11-13 12:54:58 +01:00
|
|
|
glDeleteProgram(spec.id);
|
|
|
|
spec.id = 0;
|
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
ERR_PRINT("No OpenGL fragment shader compiler log.");
|
2021-11-13 12:54:58 +01:00
|
|
|
} else {
|
|
|
|
if (iloglen == 0) {
|
|
|
|
iloglen = 4096; // buggy driver (Adreno 220+)
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
|
2023-11-11 07:56:46 +01:00
|
|
|
memset(ilogmem, 0, iloglen + 1);
|
2021-11-16 16:25:42 +01:00
|
|
|
glGetShaderInfoLog(spec.frag_id, iloglen, &iloglen, ilogmem);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
String err_string = name + ": Fragment shader compilation failed:\n";
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
err_string += ilogmem;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
_display_error_with_code(err_string, builder_string);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
Memory::free_static(ilogmem);
|
2021-11-16 16:25:42 +01:00
|
|
|
glDeleteShader(spec.frag_id);
|
2021-11-13 12:54:58 +01:00
|
|
|
glDeleteProgram(spec.id);
|
|
|
|
spec.id = 0;
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
ERR_FAIL();
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
glAttachShader(spec.id, spec.frag_id);
|
|
|
|
glAttachShader(spec.id, spec.vert_id);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2022-11-08 07:40:03 +01:00
|
|
|
// If feedback exists, set it up.
|
|
|
|
|
|
|
|
if (feedback_count) {
|
|
|
|
Vector<const char *> feedback;
|
|
|
|
for (int i = 0; i < feedback_count; i++) {
|
|
|
|
if (feedbacks[i].specialization == 0 || (feedbacks[i].specialization & p_specialization)) {
|
|
|
|
// Specialization for this feedback is enabled
|
|
|
|
feedback.push_back(feedbacks[i].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feedback.size()) {
|
|
|
|
glTransformFeedbackVaryings(spec.id, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS);
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 16:25:42 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
glLinkProgram(spec.id);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
glGetProgramiv(spec.id, GL_LINK_STATUS, &status);
|
2020-11-18 19:11:30 +01:00
|
|
|
if (status == GL_FALSE) {
|
|
|
|
GLsizei iloglen;
|
2021-11-13 12:54:58 +01:00
|
|
|
glGetProgramiv(spec.id, GL_INFO_LOG_LENGTH, &iloglen);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
|
|
|
if (iloglen < 0) {
|
2021-11-13 12:54:58 +01:00
|
|
|
glDeleteShader(spec.frag_id);
|
|
|
|
glDeleteShader(spec.vert_id);
|
|
|
|
glDeleteProgram(spec.id);
|
|
|
|
spec.id = 0;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2022-02-20 01:08:53 +01:00
|
|
|
ERR_PRINT("No OpenGL program link log. Something is wrong.");
|
2021-11-13 12:54:58 +01:00
|
|
|
ERR_FAIL();
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iloglen == 0) {
|
|
|
|
iloglen = 4096; // buggy driver (Adreno 220+)
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
|
|
|
|
ilogmem[iloglen] = '\0';
|
2021-11-13 12:54:58 +01:00
|
|
|
glGetProgramInfoLog(spec.id, iloglen, &iloglen, ilogmem);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
String err_string = name + ": Program linking failed:\n";
|
2020-11-18 19:11:30 +01:00
|
|
|
|
|
|
|
err_string += ilogmem;
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
_display_error_with_code(err_string, String());
|
2020-11-18 19:11:30 +01:00
|
|
|
|
|
|
|
Memory::free_static(ilogmem);
|
2021-11-13 12:54:58 +01:00
|
|
|
glDeleteShader(spec.frag_id);
|
|
|
|
glDeleteShader(spec.vert_id);
|
|
|
|
glDeleteProgram(spec.id);
|
|
|
|
spec.id = 0;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
ERR_FAIL();
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
_get_uniform_locations(spec, p_version);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
spec.ok = true;
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
RS::ShaderNativeSourceCode ShaderGLES3::version_get_native_source_code(RID p_version) {
|
|
|
|
Version *version = version_owner.get_or_null(p_version);
|
|
|
|
RS::ShaderNativeSourceCode source_code;
|
2023-09-09 17:46:44 +02:00
|
|
|
ERR_FAIL_NULL_V(version, source_code);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
source_code.versions.resize(variant_count);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
for (int i = 0; i < source_code.versions.size(); i++) {
|
|
|
|
//vertex stage
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
2022-09-04 16:56:24 +02:00
|
|
|
_build_variant_code(builder, i, version, STAGE_TYPE_VERTEX, specialization_default_mask);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
RS::ShaderNativeSourceCode::Version::Stage stage;
|
|
|
|
stage.name = "vertex";
|
|
|
|
stage.code = builder.as_string();
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
source_code.versions.write[i].stages.push_back(stage);
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
//fragment stage
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
2022-09-04 16:56:24 +02:00
|
|
|
_build_variant_code(builder, i, version, STAGE_TYPE_FRAGMENT, specialization_default_mask);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
RS::ShaderNativeSourceCode::Version::Stage stage;
|
|
|
|
stage.name = "fragment";
|
|
|
|
stage.code = builder.as_string();
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
source_code.versions.write[i].stages.push_back(stage);
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
return source_code;
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
String ShaderGLES3::_version_get_sha1(Version *p_version) const {
|
|
|
|
StringBuilder hash_build;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
hash_build.append("[uniforms]");
|
|
|
|
hash_build.append(p_version->uniforms.get_data());
|
|
|
|
hash_build.append("[vertex_globals]");
|
|
|
|
hash_build.append(p_version->vertex_globals.get_data());
|
|
|
|
hash_build.append("[fragment_globals]");
|
|
|
|
hash_build.append(p_version->fragment_globals.get_data());
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
Vector<StringName> code_sections;
|
|
|
|
for (const KeyValue<StringName, CharString> &E : p_version->code_sections) {
|
|
|
|
code_sections.push_back(E.key);
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
code_sections.sort_custom<StringName::AlphCompare>();
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
for (int i = 0; i < code_sections.size(); i++) {
|
|
|
|
hash_build.append(String("[code:") + String(code_sections[i]) + "]");
|
|
|
|
hash_build.append(p_version->code_sections[code_sections[i]].get_data());
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
for (int i = 0; i < p_version->custom_defines.size(); i++) {
|
|
|
|
hash_build.append("[custom_defines:" + itos(i) + "]");
|
|
|
|
hash_build.append(p_version->custom_defines[i].get_data());
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
2021-11-12 13:49:49 +01:00
|
|
|
if (RasterizerGLES3::is_gles_over_gl()) {
|
|
|
|
hash_build.append("[gl]");
|
|
|
|
} else {
|
|
|
|
hash_build.append("[gles]");
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
return hash_build.as_string().sha1_text();
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-29 15:29:33 +02:00
|
|
|
#ifndef WEB_ENABLED // not supported in webgl
|
2023-04-15 16:07:51 +02:00
|
|
|
static const char *shader_file_header = "GLSC";
|
|
|
|
static const uint32_t cache_file_version = 3;
|
|
|
|
#endif
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
bool ShaderGLES3::_load_from_cache(Version *p_version) {
|
2023-04-29 15:29:33 +02:00
|
|
|
#ifdef WEB_ENABLED // not supported in webgl
|
2023-04-15 16:07:51 +02:00
|
|
|
return false;
|
|
|
|
#else
|
2021-11-12 13:49:49 +01:00
|
|
|
#if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED)
|
2024-03-12 15:40:40 +01:00
|
|
|
if (RasterizerGLES3::is_gles_over_gl() && (glProgramBinary == nullptr)) { // ARB_get_program_binary extension not available.
|
2023-04-29 15:29:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-13 12:54:58 +01:00
|
|
|
String sha1 = _version_get_sha1(p_version);
|
2022-08-30 02:34:01 +02:00
|
|
|
String path = shader_cache_dir.path_join(name).path_join(base_sha256).path_join(sha1) + ".cache";
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<FileAccess> f = FileAccess::open(path, FileAccess::READ);
|
|
|
|
if (f.is_null()) {
|
2021-11-13 12:54:58 +01:00
|
|
|
return false;
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
char header[5] = {};
|
2021-11-13 12:54:58 +01:00
|
|
|
f->get_buffer((uint8_t *)header, 4);
|
|
|
|
ERR_FAIL_COND_V(header != String(shader_file_header), false);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
uint32_t file_version = f->get_32();
|
|
|
|
if (file_version != cache_file_version) {
|
|
|
|
return false; // wrong version
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-29 15:29:33 +02:00
|
|
|
int cache_variant_count = static_cast<int>(f->get_32());
|
2024-01-28 21:51:39 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(cache_variant_count != variant_count, false, "shader cache variant count mismatch, expected " + itos(variant_count) + " got " + itos(cache_variant_count)); //should not happen but check
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
LocalVector<OAHashMap<uint64_t, Version::Specialization>> variants;
|
2023-04-29 15:29:33 +02:00
|
|
|
for (int i = 0; i < cache_variant_count; i++) {
|
|
|
|
uint32_t cache_specialization_count = f->get_32();
|
2023-04-15 16:07:51 +02:00
|
|
|
OAHashMap<uint64_t, Version::Specialization> variant;
|
2023-04-29 15:29:33 +02:00
|
|
|
for (uint32_t j = 0; j < cache_specialization_count; j++) {
|
2023-04-15 16:07:51 +02:00
|
|
|
uint64_t specialization_key = f->get_64();
|
|
|
|
uint32_t variant_size = f->get_32();
|
|
|
|
if (variant_size == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint32_t variant_format = f->get_32();
|
|
|
|
Vector<uint8_t> variant_bytes;
|
|
|
|
variant_bytes.resize(variant_size);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
uint32_t br = f->get_buffer(variant_bytes.ptrw(), variant_size);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
ERR_FAIL_COND_V(br != variant_size, false);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
Version::Specialization specialization;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
specialization.id = glCreateProgram();
|
2024-02-20 14:24:05 +01:00
|
|
|
if (feedback_count) {
|
|
|
|
Vector<const char *> feedback;
|
|
|
|
for (int feedback_index = 0; feedback_index < feedback_count; feedback_index++) {
|
|
|
|
if (feedbacks[feedback_index].specialization == 0 || (feedbacks[feedback_index].specialization & specialization_key)) {
|
|
|
|
// Specialization for this feedback is enabled.
|
|
|
|
feedback.push_back(feedbacks[feedback_index].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!feedback.is_empty()) {
|
|
|
|
glTransformFeedbackVaryings(specialization.id, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS);
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 16:07:51 +02:00
|
|
|
glProgramBinary(specialization.id, variant_format, variant_bytes.ptr(), variant_bytes.size());
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-05-17 18:02:17 +02:00
|
|
|
GLint link_status = 0;
|
|
|
|
glGetProgramiv(specialization.id, GL_LINK_STATUS, &link_status);
|
|
|
|
if (link_status != GL_TRUE) {
|
|
|
|
WARN_PRINT_ONCE("Failed to load cached shader, recompiling.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
_get_uniform_locations(specialization, p_version);
|
|
|
|
|
|
|
|
specialization.ok = true;
|
|
|
|
|
|
|
|
variant.insert(specialization_key, specialization);
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
2023-04-15 16:07:51 +02:00
|
|
|
variants.push_back(variant);
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
2023-04-15 16:07:51 +02:00
|
|
|
p_version->variants = variants;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
return true;
|
2023-04-29 15:29:33 +02:00
|
|
|
#endif // WEB_ENABLED
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::_save_to_cache(Version *p_version) {
|
2023-04-29 15:29:33 +02:00
|
|
|
#ifdef WEB_ENABLED // not supported in webgl
|
2023-04-15 16:07:51 +02:00
|
|
|
return;
|
|
|
|
#else
|
2024-01-12 04:17:57 +01:00
|
|
|
ERR_FAIL_COND(!shader_cache_dir_valid);
|
2021-11-12 13:49:49 +01:00
|
|
|
#if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED)
|
2024-03-12 15:40:40 +01:00
|
|
|
if (RasterizerGLES3::is_gles_over_gl() && (glGetProgramBinary == nullptr)) { // ARB_get_program_binary extension not available.
|
2023-04-29 15:29:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-13 12:54:58 +01:00
|
|
|
String sha1 = _version_get_sha1(p_version);
|
2022-08-30 02:34:01 +02:00
|
|
|
String path = shader_cache_dir.path_join(name).path_join(base_sha256).path_join(sha1) + ".cache";
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
Error error;
|
|
|
|
Ref<FileAccess> f = FileAccess::open(path, FileAccess::WRITE, &error);
|
2022-03-23 10:08:58 +01:00
|
|
|
ERR_FAIL_COND(f.is_null());
|
2021-11-13 12:54:58 +01:00
|
|
|
f->store_buffer((const uint8_t *)shader_file_header, 4);
|
2023-04-15 16:07:51 +02:00
|
|
|
f->store_32(cache_file_version);
|
|
|
|
f->store_32(variant_count);
|
|
|
|
|
|
|
|
for (int i = 0; i < variant_count; i++) {
|
2023-04-29 15:29:33 +02:00
|
|
|
int cache_specialization_count = p_version->variants[i].get_num_elements();
|
|
|
|
f->store_32(cache_specialization_count);
|
2023-04-15 16:07:51 +02:00
|
|
|
|
|
|
|
for (OAHashMap<uint64_t, ShaderGLES3::Version::Specialization>::Iterator it = p_version->variants[i].iter(); it.valid; it = p_version->variants[i].next_iter(it)) {
|
|
|
|
const uint64_t specialization_key = *it.key;
|
|
|
|
f->store_64(specialization_key);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-04-15 16:07:51 +02:00
|
|
|
const Version::Specialization *specialization = it.value;
|
|
|
|
if (specialization == nullptr) {
|
|
|
|
f->store_32(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GLint program_size = 0;
|
|
|
|
glGetProgramiv(specialization->id, GL_PROGRAM_BINARY_LENGTH, &program_size);
|
|
|
|
if (program_size == 0) {
|
|
|
|
f->store_32(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PackedByteArray compiled_program;
|
|
|
|
compiled_program.resize(program_size);
|
|
|
|
GLenum binary_format = 0;
|
|
|
|
glGetProgramBinary(specialization->id, program_size, nullptr, &binary_format, compiled_program.ptrw());
|
|
|
|
if (program_size != compiled_program.size()) {
|
|
|
|
f->store_32(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f->store_32(program_size);
|
|
|
|
f->store_32(binary_format);
|
|
|
|
f->store_buffer(compiled_program.ptr(), compiled_program.size());
|
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
2023-04-29 15:29:33 +02:00
|
|
|
#endif // WEB_ENABLED
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::_clear_version(Version *p_version) {
|
2021-11-16 16:25:42 +01:00
|
|
|
// Variants not compiled yet, just return
|
|
|
|
if (p_version->variants.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
for (int i = 0; i < variant_count; i++) {
|
|
|
|
for (OAHashMap<uint64_t, Version::Specialization>::Iterator it = p_version->variants[i].iter(); it.valid; it = p_version->variants[i].next_iter(it)) {
|
2022-02-20 01:08:53 +01:00
|
|
|
if (it.value->id != 0) {
|
|
|
|
glDeleteShader(it.value->vert_id);
|
|
|
|
glDeleteShader(it.value->frag_id);
|
|
|
|
glDeleteProgram(it.value->id);
|
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
p_version->variants.clear();
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::_initialize_version(Version *p_version) {
|
|
|
|
ERR_FAIL_COND(p_version->variants.size() > 0);
|
2023-07-25 14:33:37 +02:00
|
|
|
if (shader_cache_dir_valid && _load_from_cache(p_version)) {
|
2023-04-15 16:07:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
p_version->variants.reserve(variant_count);
|
|
|
|
for (int i = 0; i < variant_count; i++) {
|
2021-11-16 16:25:42 +01:00
|
|
|
OAHashMap<uint64_t, Version::Specialization> variant;
|
|
|
|
p_version->variants.push_back(variant);
|
2021-11-13 12:54:58 +01:00
|
|
|
Version::Specialization spec;
|
|
|
|
_compile_specialization(spec, i, p_version, specialization_default_mask);
|
|
|
|
p_version->variants[i].insert(specialization_default_mask, spec);
|
|
|
|
}
|
2023-07-25 14:33:37 +02:00
|
|
|
if (shader_cache_dir_valid) {
|
|
|
|
_save_to_cache(p_version);
|
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-03-25 10:46:33 +01:00
|
|
|
void ShaderGLES3::version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines, const LocalVector<ShaderGLES3::TextureUniformData> &p_texture_uniforms, bool p_initialize) {
|
2021-11-13 12:54:58 +01:00
|
|
|
Version *version = version_owner.get_or_null(p_version);
|
2023-09-09 17:46:44 +02:00
|
|
|
ERR_FAIL_NULL(version);
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
_clear_version(version); //clear if existing
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
version->vertex_globals = p_vertex_globals.utf8();
|
|
|
|
version->fragment_globals = p_fragment_globals.utf8();
|
|
|
|
version->uniforms = p_uniforms.utf8();
|
|
|
|
version->code_sections.clear();
|
|
|
|
version->texture_uniforms = p_texture_uniforms;
|
|
|
|
for (const KeyValue<String, String> &E : p_code) {
|
|
|
|
version->code_sections[StringName(E.key.to_upper())] = E.value.utf8();
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
version->custom_defines.clear();
|
|
|
|
for (int i = 0; i < p_custom_defines.size(); i++) {
|
|
|
|
version->custom_defines.push_back(p_custom_defines[i].utf8());
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
if (p_initialize) {
|
|
|
|
_initialize_version(version);
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
bool ShaderGLES3::version_is_valid(RID p_version) {
|
|
|
|
Version *version = version_owner.get_or_null(p_version);
|
|
|
|
return version != nullptr;
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
bool ShaderGLES3::version_free(RID p_version) {
|
|
|
|
if (version_owner.owns(p_version)) {
|
|
|
|
Version *version = version_owner.get_or_null(p_version);
|
|
|
|
_clear_version(version);
|
|
|
|
version_owner.free(p_version);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
bool ShaderGLES3::shader_cache_cleanup_on_start = false;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
ShaderGLES3::ShaderGLES3() {
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::initialize(const String &p_general_defines, int p_base_texture_index) {
|
|
|
|
general_defines = p_general_defines.utf8();
|
|
|
|
base_texture_index = p_base_texture_index;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
_init();
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
if (shader_cache_dir != String()) {
|
|
|
|
StringBuilder hash_build;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
hash_build.append("[base_hash]");
|
|
|
|
hash_build.append(base_sha256);
|
|
|
|
hash_build.append("[general_defines]");
|
|
|
|
hash_build.append(general_defines.get_data());
|
|
|
|
for (int i = 0; i < variant_count; i++) {
|
|
|
|
hash_build.append("[variant_defines:" + itos(i) + "]");
|
|
|
|
hash_build.append(variant_defines[i]);
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
base_sha256 = hash_build.as_string().sha256_text();
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> d = DirAccess::open(shader_cache_dir);
|
|
|
|
ERR_FAIL_COND(d.is_null());
|
2021-11-13 12:54:58 +01:00
|
|
|
if (d->change_dir(name) != OK) {
|
|
|
|
Error err = d->make_dir(name);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
d->change_dir(name);
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
//erase other versions?
|
|
|
|
if (shader_cache_cleanup_on_start) {
|
|
|
|
}
|
|
|
|
//
|
|
|
|
if (d->change_dir(base_sha256) != OK) {
|
|
|
|
Error err = d->make_dir(base_sha256);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
|
|
|
shader_cache_dir_valid = true;
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
print_verbose("Shader '" + name + "' SHA256: " + base_sha256);
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2023-08-23 01:32:37 +02:00
|
|
|
glGetInteger64v(GL_MAX_TEXTURE_IMAGE_UNITS, &max_image_units);
|
2021-11-13 12:54:58 +01:00
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::set_shader_cache_dir(const String &p_dir) {
|
|
|
|
shader_cache_dir = p_dir;
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::set_shader_cache_save_compressed(bool p_enable) {
|
|
|
|
shader_cache_save_compressed = p_enable;
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::set_shader_cache_save_compressed_zstd(bool p_enable) {
|
|
|
|
shader_cache_save_compressed_zstd = p_enable;
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
void ShaderGLES3::set_shader_cache_save_debug(bool p_enable) {
|
|
|
|
shader_cache_save_debug = p_enable;
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:54:58 +01:00
|
|
|
String ShaderGLES3::shader_cache_dir;
|
|
|
|
bool ShaderGLES3::shader_cache_save_compressed = true;
|
|
|
|
bool ShaderGLES3::shader_cache_save_compressed_zstd = true;
|
|
|
|
bool ShaderGLES3::shader_cache_save_debug = true;
|
|
|
|
|
2021-10-26 17:18:39 +02:00
|
|
|
ShaderGLES3::~ShaderGLES3() {
|
2021-11-13 12:54:58 +01:00
|
|
|
List<RID> remaining;
|
|
|
|
version_owner.get_owned_list(&remaining);
|
|
|
|
if (remaining.size()) {
|
|
|
|
ERR_PRINT(itos(remaining.size()) + " shaders of type " + name + " were never freed");
|
|
|
|
while (remaining.size()) {
|
|
|
|
version_free(remaining.front()->get());
|
|
|
|
remaining.pop_front();
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 19:11:30 +01:00
|
|
|
}
|
2021-11-13 12:54:58 +01:00
|
|
|
#endif
|