From e8b8f5c02d46b8d774e8e5260a83f6f08fd12aa8 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Wed, 5 Jun 2024 21:23:01 +0200 Subject: [PATCH] Always optimize code of some integrated libraries, even in Debug builds the stb libs, miniz and minizip are now always optimized to speed up performance, esp. level loading times, in debug builds. So far the stb libs implementation were dragged into the source file that uses them, now I created additional source files just for them so those can be optimized while the engine source files using those libs can remain unoptimized in debug builds. --- neo/CMakeLists.txt | 32 ++++++++++++++++++++++++ neo/renderer/Image_files.cpp | 6 ----- neo/renderer/RenderSystem_init.cpp | 21 ---------------- neo/renderer/stblib_impls.c | 40 ++++++++++++++++++++++++++++++ neo/sound/snd_decoder.cpp | 4 +-- neo/sound/stbvorbis_impl.c | 14 +++++++++++ 6 files changed, 87 insertions(+), 30 deletions(-) create mode 100644 neo/renderer/stblib_impls.c create mode 100644 neo/sound/stbvorbis_impl.c diff --git a/neo/CMakeLists.txt b/neo/CMakeLists.txt index 42228f34..53ffd8c3 100644 --- a/neo/CMakeLists.txt +++ b/neo/CMakeLists.txt @@ -469,6 +469,26 @@ if(WIN32) ) endif() +# optimize this source file even in debug builds, to make speed (esp. loading times) more bearable +# used for libs integrated here like stb_* and miniz(ip) +if(CMAKE_MAJOR_VERSION LESS 3 OR ( CMAKE_MAJOR_VERSION EQUAL 3 AND CMAKE_MINOR_VERSION LESS 11 )) + function(always_optimize_sourcefile srcfilename) + if(MSVC) + set_source_files_properties(${srcfilename} PROPERTIES COMPILE_FLAGS "/Ox") + else() + set_source_files_properties(${srcfilename} PROPERTIES COMPILE_FLAGS "-O2") + endif() + endfunction() +else() + function(always_optimize_sourcefile srcfilename) + if(MSVC) + set_source_files_properties(${srcfilename} PROPERTIES COMPILE_OPTIONS "/Ox") + else() + set_source_files_properties(${srcfilename} PROPERTIES COMPILE_OPTIONS "-O2") + endif() + endfunction() +endif() + # fallback for cmake versions without GNUInstallDirs if(GNUINSTALLDIRS MATCHES "NOTFOUND") set(CMAKE_INSTALL_BINDIR "bin" @@ -561,8 +581,12 @@ set(src_renderer renderer/tr_trace.cpp renderer/tr_trisurf.cpp renderer/tr_turboshadow.cpp + + renderer/stblib_impls.c ) +always_optimize_sourcefile(renderer/stblib_impls.c) + # I'm a bit sloppy with headers and just glob them in.. # they're only handled in CMake at all so they turn up in Visual Studio solutions.. @@ -609,6 +633,10 @@ set(src_framework framework/minizip/unzip.cpp ) +always_optimize_sourcefile(framework/miniz/miniz.c) +always_optimize_sourcefile(framework/minizip/ioapi.c) +always_optimize_sourcefile(framework/minizip/unzip.cpp) + add_globbed_headers(src_framework "framework") set(src_cm @@ -685,8 +713,12 @@ set(src_snd sound/snd_system.cpp sound/snd_wavefile.cpp sound/snd_world.cpp + + sound/stbvorbis_impl.c ) +always_optimize_sourcefile(sound/stbvorbis_impl.c) + add_globbed_headers(src_snd "sound") set(src_ui diff --git a/neo/renderer/Image_files.cpp b/neo/renderer/Image_files.cpp index 289f3b07..174d544d 100644 --- a/neo/renderer/Image_files.cpp +++ b/neo/renderer/Image_files.cpp @@ -27,12 +27,6 @@ If you have questions concerning this license or the applicable additional terms */ // DG: replace libjpeg with stb_image.h because it causes fewer headaches -// include this first, otherwise build breaks because of use_idStr_* #defines in Str.h -#if defined(__APPLE__) && !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5 - // Extra-Hack for ancient GCC 4.2-based Apple compilers that don't support __thread - #define STBI_NO_THREAD_LOCALS -#endif -#define STB_IMAGE_IMPLEMENTATION #define STBI_NO_HDR #define STBI_NO_LINEAR #define STBI_ONLY_JPEG // at least for now, only use it for JPEG diff --git a/neo/renderer/RenderSystem_init.cpp b/neo/renderer/RenderSystem_init.cpp index 35be44d1..02a5d1a6 100644 --- a/neo/renderer/RenderSystem_init.cpp +++ b/neo/renderer/RenderSystem_init.cpp @@ -47,27 +47,6 @@ If you have questions concerning this license or the applicable additional terms #include "sys/win32/win_local.h" #endif -#include "framework/miniz/miniz.h" - -static unsigned char* compress_for_stbiw(unsigned char* data, int data_len, int* out_len, int quality) -{ - uLongf bufSize = compressBound(data_len); - // note that buf will be free'd by stb_image_write.h - // with STBIW_FREE() (plain free() by default) - unsigned char* buf = (unsigned char*)malloc(bufSize); - if (buf == NULL) return NULL; - if (compress2(buf, &bufSize, data, data_len, quality) != Z_OK) - { - free(buf); - return NULL; - } - *out_len = bufSize; - - return buf; -} - -#define STB_IMAGE_WRITE_IMPLEMENTATION -#define STBIW_ZLIB_COMPRESS compress_for_stbiw #include "stb_image_write.h" // functions that are not called every frame diff --git a/neo/renderer/stblib_impls.c b/neo/renderer/stblib_impls.c new file mode 100644 index 00000000..0e8f2a63 --- /dev/null +++ b/neo/renderer/stblib_impls.c @@ -0,0 +1,40 @@ +// this source file includes the implementations of stb_image and stb_image_write +// having it in a separate source file allows optimizing it in debug builds (for faster load times) +// without hurting the debugability of the source files stb_image(_write) is used in + +// include this first, otherwise build breaks because of use_idStr_* #defines in Str.h +#if defined(__APPLE__) && !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5 + // Extra-Hack for ancient GCC 4.2-based Apple compilers that don't support __thread + #define STBI_NO_THREAD_LOCALS +#endif +#define STB_IMAGE_IMPLEMENTATION +#define STBI_NO_HDR +#define STBI_NO_LINEAR +#define STBI_ONLY_JPEG // at least for now, only use it for JPEG +#define STBI_NO_STDIO // images are passed as buffers +#include "stb_image.h" + + +#include "framework/miniz/miniz.h" + +static unsigned char* compress_for_stbiw(unsigned char* data, int data_len, int* out_len, int quality) +{ + uLongf bufSize = compressBound(data_len); + // note that buf will be free'd by stb_image_write.h + // with STBIW_FREE() (plain free() by default) + unsigned char* buf = (unsigned char*)malloc(bufSize); + if (buf == NULL) return NULL; + if (compress2(buf, &bufSize, data, data_len, quality) != Z_OK) + { + free(buf); + return NULL; + } + *out_len = bufSize; + + return buf; +} + +#define STB_IMAGE_WRITE_IMPLEMENTATION +#define STBIW_ZLIB_COMPRESS compress_for_stbiw +#include "stb_image_write.h" + diff --git a/neo/sound/snd_decoder.cpp b/neo/sound/snd_decoder.cpp index 4c044752..1ec1eac5 100644 --- a/neo/sound/snd_decoder.cpp +++ b/neo/sound/snd_decoder.cpp @@ -33,10 +33,8 @@ If you have questions concerning this license or the applicable additional terms #endif #define STB_VORBIS_NO_STDIO #define STB_VORBIS_NO_PUSHDATA_API // we're using the pulldata API +#define STB_VORBIS_HEADER_ONLY #include "stb_vorbis.h" -#undef L // the implementation part of stb_vorbis has these defines, they confuse other code.. -#undef C -#undef R #include "sys/platform.h" #include "framework/FileSystem.h" diff --git a/neo/sound/stbvorbis_impl.c b/neo/sound/stbvorbis_impl.c new file mode 100644 index 00000000..806f61ea --- /dev/null +++ b/neo/sound/stbvorbis_impl.c @@ -0,0 +1,14 @@ +// this source file includes the implementation of stb_vorbis +// having it in a separate source file allows optimizing it in debug builds (for faster load times) +// without hurting the debugability of the source files it's used in + +// (I'm doing this instead of renaming stb_vorbis.h to stb_vorbis.c so the configuration +// like STB_VORBIS_BIG_ENDIAN etc can be done here in code) + +#include "SDL_endian.h" +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + #define STB_VORBIS_BIG_ENDIAN +#endif +#define STB_VORBIS_NO_STDIO +#define STB_VORBIS_NO_PUSHDATA_API // we're using the pulldata API +#include "stb_vorbis.h"