Patch in thread local storage for stb_image vflip

This makes loading images in multiple threads concurrently safe.

Adds a new CMake variable LOVR_USE_THREADLOCAL, by default on
This commit is contained in:
mcc 2019-12-20 16:09:18 -05:00
parent 735d2e1011
commit 21a52368f5
3 changed files with 18 additions and 1 deletions

View File

@ -36,6 +36,8 @@ option(LOVR_BUILD_EXE "Build an executable" ON)
option(LOVR_BUILD_SHARED "Build a shared library (takes precedence over LOVR_BUILD_EXE)" OFF)
option(LOVR_BUILD_BUNDLE "On macOS, build a .app bundle instead of a raw program" OFF)
option(LOVR_USE_THREADLOCAL "Allow use of thread local storage; disable to run on Windows XP as a DLL" ON)
# Setup
if(EMSCRIPTEN)
string(CONCAT LOVR_EMSCRIPTEN_FLAGS
@ -286,6 +288,9 @@ if(LOVR_ENABLE_THREAD)
find_package(Threads REQUIRED)
set(LOVR_PTHREADS Threads::Threads)
endif()
if (LOVR_USE_THREADLOCAL)
add_definitions(-DUSE_LOVR_STBI_THREADLOCAL)
endif()
endif()
# LÖVR

View File

@ -5,3 +5,7 @@
#define STBI_ONLY_HDR
#define STBI_ASSERT(x)
#include "stb_image.h"
#ifndef LOVR_STBI_VFLIP_PATCH
#error "Somebody updated stb_image.h without replacing the thread-local patch"
#endif

View File

@ -995,7 +995,15 @@ static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);
#endif
static int stbi__vertically_flip_on_load = 0;
#define LOVR_STBI_VFLIP_PATCH
#ifdef USE_LOVR_STBI_THREADLOCAL
#include "lib/tinycthread/tinycthread.h"
#define THREADLOCAL_IF_AVAILABLE _Thread_local
#else
#define THREADLOCAL_IF_AVAILABLE
#endif
static THREADLOCAL_IF_AVAILABLE int stbi__vertically_flip_on_load = 0;
STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
{