0
0
Fork 0
mirror of https://github.com/dhewm/dhewm3.git synced 2025-03-22 10:41:08 +00:00

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.
This commit is contained in:
Daniel Gibson 2024-06-05 21:23:01 +02:00
parent af34aba577
commit e8b8f5c02d
6 changed files with 87 additions and 30 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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"

View file

@ -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"