Godot Engine – Multi-platform 2D and 3D game engine
Compress embedded RD and GLES3 shader code to reduce binary size
Rémi Verscheldeb289b6f4ed97
diff --git a/drivers/gles3/shader_gles3.cpp b/drivers/gles3/shader_gles3.cppindex 0325a37d345..968cd15d430 100644--- a/drivers/gles3/shader_gles3.cpp+++ b/drivers/gles3/shader_gles3.cpp@@ -32,6 +32,7 @@#ifdef GLES3_ENABLED+#include "core/io/compression.h"#include "core/io/dir_access.h"#include "core/string/string_builder.h"#include "drivers/gles3/rasterizer_util_gles3.h"@@ -102,6 +103,14 @@ void ShaderGLES3::_add_stage(const char *p_code, StageType p_stage_type) {}}+CharString ShaderGLES3::_decompress_code(const uint8_t *p_compressed, int p_compressed_size, int p_size) {+ CharString code;+ code.resize_uninitialized(p_size);+ const int64_t ret = Compression::decompress((uint8_t *)code.ptrw(), p_size, p_compressed, p_compressed_size, Compression::MODE_DEFLATE);+ ERR_FAIL_COND_V_MSG(ret != p_size, CharString(), "Embedded shader code is corrupt.");+ return code;+}+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) {name = p_name;diff --git a/drivers/gles3/shader_gles3.h b/drivers/gles3/shader_gles3.hindex 4ff35c1b08e..d7155a711a7 100644--- a/drivers/gles3/shader_gles3.h+++ b/drivers/gles3/shader_gles3.h@@ -178,6 +178,8 @@ private:protected:ShaderGLES3();+ // Decompresses the shader code embedded in the headers generated by `gles3_builders.py`.+ static CharString _decompress_code(const uint8_t *p_compressed, int p_compressed_size, int p_size);void _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);_FORCE_INLINE_ bool _version_bind_shader(RID p_version, int p_variant, uint64_t p_specialization) {diff --git a/gles3_builders.py b/gles3_builders.pyindex c0cf7aca82d..41d9dd97b99 100644--- a/gles3_builders.py+++ b/gles3_builders.py@@ -2,7 +2,7 @@import os.path-from methods import generated_wrapper, print_error, to_raw_cstring+from methods import compress_buffer, format_buffer, generated_wrapper, print_errorclass GLES3HeaderStruct:@@ -551,16 +551,19 @@ protected:static const Feedback *_feedbacks = nullptr;""")- file.write(f"""\- static const char _vertex_code[] = {{-{to_raw_cstring(header_data.vertex_lines)}- }};+ # Store the code of both stages in a single compressed buffer, as null-terminated strings.+ vertex_code = "\n".join(header_data.vertex_lines).encode() + b"\n\0"+ fragment_code = "\n".join(header_data.fragment_lines).encode() + b"\n\0"+ buffer = vertex_code + fragment_code+ compressed = compress_buffer(buffer)- static const char _fragment_code[] = {{-{to_raw_cstring(header_data.fragment_lines)}+ file.write(f"""\+ static const uint8_t _code_compressed[] = {{+ {format_buffer(compressed, 3)}}};+ const CharString _code = _decompress_code(_code_compressed, {len(compressed)}, {len(buffer)});- _setup(_vertex_code, _fragment_code, "{out_file_class}",+ _setup(_code.get_data(), _code.get_data() + {len(vertex_code)}, "{out_file_class}",{len(header_data.uniforms)}, _uniform_strings, {len(header_data.ubos)}, _ubo_pairs,{len(header_data.feedbacks)}, _feedbacks, {len(header_data.texunits)}, _texunit_pairs,{len(header_data.specialization_names)}, _spec_pairs, {variant_count}, _variant_defines);diff --git a/glsl_builders.py b/glsl_builders.pyindex ce4545f6775..03f7f15e34f 100644--- a/glsl_builders.py+++ b/glsl_builders.py@@ -2,7 +2,7 @@import os.path-from methods import generated_wrapper, print_error, to_raw_cstring+from methods import compress_buffer, format_buffer, generated_wrapper, print_error, to_raw_cstringclass RDHeaderStruct:@@ -176,23 +176,43 @@ def include_file_in_rd_header(filename: str, header_data: RDHeaderStruct, depth:return header_data-def build_rd_header_lines_for_raytracing_stage(lines, stage: str):- if lines:- return f"""\- static const char _{stage}_code[] = {{-{to_raw_cstring(lines)}- }};-"""- else:- return f"""\- static const char *_{stage}_code = nullptr;-"""--def build_rd_header(filename: str, shader: str) -> None:include_file_in_rd_header(shader, header_data := RDHeaderStruct(), 0)class_name = os.path.basename(shader).replace(".glsl", "").title().replace("_", "").replace(".", "") + "ShaderRD"+ if (+ header_data.raygen_lines+ or header_data.any_hit_lines+ or header_data.closest_hit_lines+ or header_data.miss_lines+ or header_data.intersection_lines+ ): # Raytracing+ setup_function = "setup_raytracing"+ stages = [+ header_data.raygen_lines or None,+ header_data.any_hit_lines or None,+ header_data.closest_hit_lines or None,+ header_data.miss_lines or None,+ header_data.intersection_lines or None,+ ]+ elif header_data.compute_lines: # Compute+ setup_function = "setup"+ stages = [None, None, header_data.compute_lines]+ else: # Vertex/Fragment+ setup_function = "setup"+ stages = [header_data.vertex_lines, header_data.fragment_lines, None]++ # Store the code of all stages in a single compressed buffer, as null-terminated strings.+ buffer = b""+ setup_args = []+ for lines in stages:+ if lines is None:+ setup_args.append("nullptr")+ else:+ setup_args.append(f"_code.get_data() + {len(buffer)}")+ buffer += "\n".join(lines).encode() + b"\n\0"+ compressed = compress_buffer(buffer)+with generated_wrapper(filename) as file:file.write(f"""\#include "servers/rendering/renderer_rd/shader_rd.h"@@ -200,47 +220,14 @@ def build_rd_header(filename: str, shader: str) -> None:class {class_name} : public ShaderRD {{public:{class_name}() {{-""")-- if (- header_data.raygen_lines- or header_data.any_hit_lines- or header_data.closest_hit_lines- or header_data.miss_lines- or header_data.intersection_lines- ):- file.write(build_rd_header_lines_for_raytracing_stage(header_data.raygen_lines, "raygen"))- file.write(build_rd_header_lines_for_raytracing_stage(header_data.any_hit_lines, "any_hit"))- file.write(build_rd_header_lines_for_raytracing_stage(header_data.closest_hit_lines, "closest_hit"))- file.write(build_rd_header_lines_for_raytracing_stage(header_data.miss_lines, "miss"))- file.write(build_rd_header_lines_for_raytracing_stage(header_data.intersection_lines, "intersection"))- file.write(f"""\- setup_raytracing(_raygen_code, _any_hit_code, _closest_hit_code, _miss_code, _intersection_code, "{class_name}");-""")- elif header_data.compute_lines:- file.write(f"""\- static const char *_vertex_code = nullptr;- static const char *_fragment_code = nullptr;- static const char _compute_code[] = {{-{to_raw_cstring(header_data.compute_lines)}- }};- setup(_vertex_code, _fragment_code, _compute_code, "{class_name}");-""")- else:- file.write(f"""\- static const char _vertex_code[] = {{-{to_raw_cstring(header_data.vertex_lines)}+ static const uint8_t _code_compressed[] = {{+ {format_buffer(compressed, 3)}}};- static const char _fragment_code[] = {{-{to_raw_cstring(header_data.fragment_lines)}- }};- static const char *_compute_code = nullptr;- setup(_vertex_code, _fragment_code, _compute_code, "{class_name}");-""")+ const CharString _code = _decompress_code(_code_compressed, {len(compressed)}, {len(buffer)});- file.write("""\- }-};+ {setup_function}({", ".join(setup_args)}, "{class_name}");+ }}+}};""")diff --git a/servers/rendering/renderer_rd/shader_rd.cpp b/servers/rendering/renderer_rd/shader_rd.cppindex 0d69463d821..b4e30f68485 100644--- a/servers/rendering/renderer_rd/shader_rd.cpp+++ b/servers/rendering/renderer_rd/shader_rd.cpp@@ -31,6 +31,7 @@#include "shader_rd.h"#include "core/config/engine.h"+#include "core/io/compression.h"#include "core/io/dir_access.h"#include "core/io/file_access.h"#include "core/object/worker_thread_pool.h"@@ -148,6 +149,14 @@ void ShaderRD::_add_stage(const char *p_code, StageType p_stage_type) {}}+CharString ShaderRD::_decompress_code(const uint8_t *p_compressed, int p_compressed_size, int p_size) {+ CharString code;+ code.resize_uninitialized(p_size);+ const int64_t ret = Compression::decompress((uint8_t *)code.ptrw(), p_size, p_compressed, p_compressed_size, Compression::MODE_DEFLATE);+ ERR_FAIL_COND_V_MSG(ret != p_size, CharString(), "Embedded shader code is corrupt.");+ return code;+}+void ShaderRD::setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name) {name = p_name;diff --git a/servers/rendering/renderer_rd/shader_rd.h b/servers/rendering/renderer_rd/shader_rd.hindex 1a2dde3fa84..2393321008c 100644--- a/servers/rendering/renderer_rd/shader_rd.h+++ b/servers/rendering/renderer_rd/shader_rd.h@@ -187,6 +187,8 @@ private:protected:ShaderRD();+ // Decompresses the shader code embedded in the headers generated by `glsl_builders.py`.+ static CharString _decompress_code(const uint8_t *p_compressed, int p_compressed_size, int p_size);void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name);void setup_raytracing(const char *p_raygen_code, const char *p_any_hit_code, const char *p_closest_hit_code, const char *p_miss_code, const char *p_intersection_code, const char *p_name);diff --git a/tests/python_build/fixtures/gles3/vertex_fragment.out b/tests/python_build/fixtures/gles3/vertex_fragment.outindex c10f706d9fe..749cce0cb50 100644--- a/tests/python_build/fixtures/gles3/vertex_fragment.out+++ b/tests/python_build/fixtures/gles3/vertex_fragment.out@@ -26,37 +26,20 @@ protected:{ "DISABLE_LIGHTING", false },};static const Feedback *_feedbacks = nullptr;- static const char _vertex_code[] = {-R"<!>(-precision highp float;-precision highp int;-/* clang-format on */-layout(location = 0) in highp vec3 vertex;--out highp vec4 position_interp;--void main() {- position_interp = vec4(vertex.x, 1, 0, 1);-}--)<!>"- };-- static const char _fragment_code[] = {-R"<!>(-precision highp float;-precision highp int;--in highp vec4 position_interp;--void main() {- highp float depth = ((position_interp.z / position_interp.w) + 1.0);- frag_color = vec4(depth);-}-)<!>"+ static const uint8_t _code_compressed[] = {+ 120, 218, 149, 143, 205, 14, 130, 48, 16, 132, 189, 186, 79, 177, 199, 130, 200, 79, 244, 70, 124, 22, 210,+ 212, 2, 77, 42, 219, 148, 138, 168, 241, 221, 93, 252, 137, 134, 147, 94, 246, 48, 59, 243, 101, 6, 156,+ 215, 202, 244, 134, 58, 108, 77, 211, 58, 172, 45, 201, 80, 194, 92, 54, 29, 139, 89, 140, 202, 202, 174,+ 89, 215, 228, 15, 50, 32, 127, 227, 12, 172, 60, 211, 49, 8, 75, 74, 134, 41, 176, 195, 60, 98, 255, 43,+ 56, 104, 181, 225, 227, 131, 30, 75, 0, 54, 126, 244, 45, 58, 234, 205, 148, 169, 24, 175, 189, 99, 195,+ 64, 102, 143, 7, 105, 58, 17, 225, 21, 150, 51, 3, 179, 167, 156, 120, 242, 210, 49, 193, 34, 193, 156,+ 111, 84, 194, 13, 96, 1, 255, 172, 129, 239, 142, 63, 116, 249, 34, 226, 94, 187, 208, 114, 27, 33, 102,+ 177, 244, 130, 217, 28, 149, 158, 34, 92, 97, 145, 230, 220, 114, 89, 123, 217, 84, 138, 44, 249, 247, 152,+ 7, 235, 49, 96, 113, 7, 44, 120, 132, 251};+ const CharString _code = _decompress_code(_code_compressed, 195, 398);- _setup(_vertex_code, _fragment_code, "VertexFragmentShaderGLES3",+ _setup(_code.get_data(), _code.get_data() + 206, "VertexFragmentShaderGLES3",0, _uniform_strings, 0, _ubo_pairs,0, _feedbacks, 0, _texunit_pairs,1, _spec_pairs, 1, _variant_defines);diff --git a/tests/python_build/fixtures/rd_glsl/compute.out b/tests/python_build/fixtures/rd_glsl/compute.outindex c57461f7c9a..06be2985f20 100644--- a/tests/python_build/fixtures/rd_glsl/compute.out+++ b/tests/python_build/fixtures/rd_glsl/compute.out@@ -3,23 +3,15 @@class ComputeShaderRD : public ShaderRD {public:ComputeShaderRD() {- static const char *_vertex_code = nullptr;- static const char *_fragment_code = nullptr;- static const char _compute_code[] = {-R"<!>(-#version 450--#VERSION_DEFINES--#define BLOCK_SIZE 8--#define M_PI 3.14159265359--void main() {- uint t = BLOCK_SIZE + 1;-}-)<!>"+ static const uint8_t _code_compressed[] = {+ 120, 218, 227, 82, 46, 75, 45, 42, 206, 204, 207, 83, 48, 49, 53, 224, 226, 82, 14, 115, 13, 10, 246, 244,+ 247, 139, 119, 113, 117, 243, 244, 115, 13, 6, 138, 164, 164, 166, 101, 230, 165, 42, 56, 249, 248, 59,+ 123, 199, 7, 123, 70, 185, 42, 88, 32, 68, 125, 227, 3, 60, 21, 140, 245, 12, 77, 12, 77, 45, 141, 204,+ 76, 141, 77, 45, 185, 184, 202, 242, 51, 83, 20, 114, 19, 51, 243, 52, 52, 21, 170, 185, 56, 75, 51, 243,+ 74, 20, 74, 20, 108, 145, 77, 208, 86, 48, 180, 230, 170, 229, 98, 0, 0, 129, 6, 33, 88};- setup(_vertex_code, _fragment_code, _compute_code, "ComputeShaderRD");+ const CharString _code = _decompress_code(_code_compressed, 115, 126);++ setup(nullptr, nullptr, _code.get_data() + 0, "ComputeShaderRD");}};diff --git a/tests/python_build/fixtures/rd_glsl/vertex_fragment.out b/tests/python_build/fixtures/rd_glsl/vertex_fragment.outindex a053b3e3215..ceafe3a13e9 100644--- a/tests/python_build/fixtures/rd_glsl/vertex_fragment.out+++ b/tests/python_build/fixtures/rd_glsl/vertex_fragment.out@@ -3,36 +3,17 @@class VertexFragmentShaderRD : public ShaderRD {public:VertexFragmentShaderRD() {- static const char _vertex_code[] = {-R"<!>(-#version 450--#VERSION_DEFINES--#define M_PI 3.14159265359--layout(location = 0) out vec2 uv_interp;--void main() {- uv_interp = vec2(0, 1);-}--)<!>"+ static const uint8_t _code_compressed[] = {+ 120, 218, 227, 82, 46, 75, 45, 42, 206, 204, 207, 83, 48, 49, 53, 224, 226, 82, 14, 115, 13, 10, 246, 244,+ 247, 139, 119, 113, 117, 243, 244, 115, 13, 6, 138, 164, 164, 166, 101, 230, 165, 42, 248, 198, 7, 120,+ 42, 24, 235, 25, 154, 24, 154, 90, 26, 153, 153, 26, 155, 90, 114, 113, 229, 36, 86, 230, 151, 150, 104,+ 228, 228, 39, 39, 150, 128, 140, 176, 85, 48, 208, 84, 0, 138, 40, 148, 165, 38, 27, 41, 148, 150, 197,+ 103, 230, 149, 164, 22, 21, 88, 115, 113, 149, 229, 103, 166, 40, 228, 38, 102, 230, 105, 104, 42, 84, 115,+ 113, 194, 165, 128, 90, 64, 106, 53, 12, 116, 20, 12, 53, 173, 185, 106, 185, 184, 24, 184, 8, 185, 8, 171,+ 173, 153, 121, 164, 91, 106, 168, 3, 212, 9, 178, 148, 1, 0, 5, 173, 76, 106};- static const char _fragment_code[] = {-R"<!>(-#version 450--#VERSION_DEFINES+ const CharString _code = _decompress_code(_code_compressed, 157, 262);-layout(location = 0) in vec2 uv_interp;--void main() {- uv_interp = vec2(1, 0);-}-)<!>"- };- static const char *_compute_code = nullptr;- setup(_vertex_code, _fragment_code, _compute_code, "VertexFragmentShaderRD");+ setup(_code.get_data() + 0, _code.get_data() + 146, nullptr, "VertexFragmentShaderRD");}};