diff --git a/src/r_draw.cpp b/src/r_draw.cpp index 833e64ec2..3525ff785 100644 --- a/src/r_draw.cpp +++ b/src/r_draw.cpp @@ -36,6 +36,15 @@ #ifdef HAVE_THREADS #include "core/thread_pool.h" +#ifdef _WIN32 +#include +#define local_for_thread static __thread +#else +#include +#define local_for_thread thread_local static +#endif +#else +#define local_for_thread static #endif // ========================================================================== @@ -81,19 +90,7 @@ floatv3_t *ds_su, *ds_sv, *ds_sz; float focallengthf; // For, uh, tilted lighting, duh. -#ifdef HAVE_THREADS -#ifdef _WIN32 -#include -#define local_for_thread static __thread -#else -#include -#define local_for_thread thread_local static -#endif -#else -#define local_for_thread static -#endif - -local_for_thread INT32 *tiltlighting; +local_for_thread std::unique_ptr tiltlight; // -------------------------------------------- // c drawer routines @@ -486,13 +483,12 @@ static void R_AllocViewMemory(void) xtoviewangle = static_cast(Z_Realloc(xtoviewangle, sizeof(*xtoviewangle) * (viewwidth + 1), PU_STATIC, NULL)); - auto init_tiltlighting = [] { - tiltlighting = static_cast(realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth)); // dont use Zone memory since its not thread safe + auto init_tiltlighting = [](INT32 size) { + tiltlight = std::make_unique(size); }; - - init_tiltlighting(); + init_tiltlighting(viewwidth); // allocate for main thread #ifdef HAVE_THREADS - srb2::g_main_threadpool->for_each(std::move(init_tiltlighting)); + srb2::g_main_threadpool->for_each([=] { init_tiltlighting(viewwidth); }); #endif R_AllocSegMemory(); diff --git a/src/r_draw_span.cpp b/src/r_draw_span.cpp index 833b5c1eb..c8b051913 100644 --- a/src/r_draw_span.cpp +++ b/src/r_draw_span.cpp @@ -204,6 +204,7 @@ static void R_DrawTiltedSpanTemplate(drawspandata_t* ds) const INT32 nflatyshift = ds->nflatyshift; const INT32 nflatmask = ds->nflatmask; const INT32 stride = vid.width; + INT32 * __restrict tiltlighting = tiltlight.get(); iz = ds->szp.z + ds->szp.y*(centery-ds->y) + ds->szp.x*(ds->x1-centerx); @@ -392,6 +393,7 @@ void R_DrawFogSpan_Tilted(drawspandata_t* ds) dest = R_Address(ds->x1, ds->y); const INT32 stride = vid.width; + INT32 * __restrict tiltlighting = tiltlight.get(); // Lighting is simple. It's just linear interpolation from start to end {