From 56021ed9995a4828b64e6a5f6b64cc2291478336 Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 12 Jun 2025 19:48:25 +0200 Subject: [PATCH 1/2] dont compile with software multithread if NOTHREADS/HAVE_THREADS=0 might be useful for smth ig --- src/d_netfil.c | 8 ++++++ src/r_draw_span.c | 4 +++ src/r_main.cpp | 2 -- src/r_plane.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++-- src/r_segs.cpp | 1 - src/r_things.cpp | 4 +++ src/sdl/i_system.cpp | 10 ++++++- 7 files changed, 85 insertions(+), 6 deletions(-) diff --git a/src/d_netfil.c b/src/d_netfil.c index 0650f3c34..d45570a0e 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -111,7 +111,9 @@ static fileused_t transferFiles[UINT8_MAX + 1]; // Receiver structure INT32 fileneedednum; // Number of files needed to join the server fileneeded_t fileneeded[MAX_WADFILES]; // List of needed files +#ifdef HAVE_THREADS static I_mutex downloadmutex; +#endif char downloaddir[512] = "DOWNLOAD"; file_download_t filedownload; @@ -1351,14 +1353,18 @@ void CURLAbortFile(void) { filedownload.http_running = false; +#ifdef HAVE_THREADS // lock and unlock to wait for the download thread to exit I_lock_mutex(&downloadmutex); I_unlock_mutex(downloadmutex); +#endif } void CURLGetFile(void) { +#ifdef HAVE_THREADS I_lock_mutex(&downloadmutex); +#endif CURLMcode mc; /* return code used by curl_multi_wait() */ CURLcode easyres; /* Return from easy interface */ CURLMsg *m; /* for picking up messages with the transfer status */ @@ -1453,7 +1459,9 @@ void CURLGetFile(void) } filedownload.http_running = false; +#ifdef HAVE_THREADS I_unlock_mutex(downloadmutex); +#endif } HTTP_login * diff --git a/src/r_draw_span.c b/src/r_draw_span.c index 15ae7b1a2..d7a9aaa68 100644 --- a/src/r_draw_span.c +++ b/src/r_draw_span.c @@ -15,6 +15,7 @@ // SPANS // ========================================================================== +#ifdef HAVE_THREADS #ifdef _WIN32 #include #define local_for_thread static __thread @@ -22,6 +23,9 @@ #include #define local_for_thread thread_local static #endif +#else +#define local_for_thread static +#endif #define SPANSIZE 16 #define INVSPAN 0.0625f diff --git a/src/r_main.cpp b/src/r_main.cpp index fa3293ee1..e0d75b6df 100644 --- a/src/r_main.cpp +++ b/src/r_main.cpp @@ -39,8 +39,6 @@ #include "r_fps.h" // Frame interpolation/uncapped #include "tables.h" -#include "core/thread_pool.h" - #ifdef HWRENDER #include "hardware/hw_main.h" #endif diff --git a/src/r_plane.cpp b/src/r_plane.cpp index 33935dda8..24c3cb086 100644 --- a/src/r_plane.cpp +++ b/src/r_plane.cpp @@ -32,7 +32,9 @@ #include "r_fps.h" #include "r_portal.h" +#ifdef HAVE_THREADS #include "core/thread_pool.h" +#endif // // opening @@ -248,6 +250,7 @@ static void R_MapPlane(drawspandata_t *ds, spandrawfunc_t *localspanfunc, INT32 pindex = distance >> LIGHTZSHIFT; if (pindex >= MAXLIGHTZ) pindex = MAXLIGHTZ - 1; + ds->colormap = ds->planezlight[pindex]; if (encoremap && !ds->currentplane->noencore) @@ -356,6 +359,7 @@ void R_ClearPlanes(void) static visplane_t *new_visplane(unsigned hash) { visplane_t *check = freetail; + if (!check) { check = static_cast(calloc(1, sizeof (*check))); @@ -372,8 +376,10 @@ static visplane_t *new_visplane(unsigned hash) if (!freetail) freehead = &freetail; } + check->next = visplanes[hash]; visplanes[hash] = check; + return check; } @@ -618,12 +624,14 @@ static void R_MakeSpans(void (*mapfunc)(drawspandata_t* ds, void(*spanfunc)(draw if (b2 >= vidheight) b2 = vidheight-1; if (x-1 >= vid.width) x = vid.width; + drawspandata_t dc_copy = *ds; + +#ifdef HAVE_THREADS // We want to draw N spans per subtask to ensure the work is // coarse enough to not be too slow due to task scheduling overhead. // To safely do this, we need to copy part of spanstart to a local. // This is essentially loop unrolling across threads. constexpr const int kSpanTaskGranularity = 8; - drawspandata_t dc_copy = *ds; while (t1 < t2 && t1 <= b1) { INT32 spanstartcopy[kSpanTaskGranularity] = {0}; @@ -682,6 +690,20 @@ static void R_MakeSpans(void (*mapfunc)(drawspandata_t* ds, void(*spanfunc)(draw } b1 -= taskspans; } +#else + (void)allow_parallel; + + while (t1 < t2 && t1 <= b1) + { + mapfunc(&dc_copy, localspanfunc, t1, spanstart[t1], x - 1, false); + t1++; + } + while (b1 > b2 && b1 >= t1) + { + mapfunc(&dc_copy, localspanfunc, b1, spanstart[b1], x - 1, false); + b1--; + } +#endif while (t2 < t1 && t2 <= b2) spanstart[t2++] = x; @@ -694,11 +716,15 @@ void R_DrawPlanes(void) visplane_t *pl; INT32 i; drawspandata_t ds = {}; +#ifdef HAVE_THREADS srb2::ThreadPool::Sema tp_sema; +#endif R_UpdatePlaneRipple(&ds); +#ifdef HAVE_THREADS srb2::g_main_threadpool->begin_sema(); +#endif for (i = 0; i < MAXVISPLANES; i++, pl++) { for (pl = visplanes[i]; pl; pl = pl->next) @@ -709,9 +735,11 @@ void R_DrawPlanes(void) R_DrawSinglePlane(&ds, pl, cv_parallelsoftware.value); } } +#ifdef HAVE_THREADS tp_sema = srb2::g_main_threadpool->end_sema(); srb2::g_main_threadpool->notify_sema(tp_sema); srb2::g_main_threadpool->wait_sema(tp_sema); +#endif } static void R_DrawSkyPlane(visplane_t *pl, void(*colfunc2)(drawcolumndata_t*), boolean allow_parallel); @@ -720,9 +748,11 @@ void R_DrawSkyPlanes(void) { visplane_t *pl; INT32 i; - srb2::ThreadPool::Sema tp_sema; +#ifdef HAVE_THREADS + srb2::ThreadPool::Sema tp_sema; srb2::g_main_threadpool->begin_sema(); +#endif for (i = 0; i < MAXVISPLANES; i++, pl++) { for (pl = visplanes[i]; pl; pl = pl->next) @@ -733,9 +763,11 @@ void R_DrawSkyPlanes(void) R_DrawSkyPlane(pl, colfunc, cv_parallelsoftware.value); } } +#ifdef HAVE_THREADS tp_sema = srb2::g_main_threadpool->end_sema(); srb2::g_main_threadpool->notify_sema(tp_sema); srb2::g_main_threadpool->wait_sema(tp_sema); +#endif } static void R_DrawSkyPlane(visplane_t *pl, void(*colfunc2)(drawcolumndata_t*), boolean allow_parallel) @@ -781,6 +813,7 @@ static void R_DrawSkyPlane(visplane_t *pl, void(*colfunc2)(drawcolumndata_t*), b R_GenerateTexture(texturetranslation[skytexture]); } +#ifdef HAVE_THREADS while (x <= pl->maxx) { // Tune concurrency granularity here to maximize throughput @@ -820,6 +853,31 @@ static void R_DrawSkyPlane(visplane_t *pl, void(*colfunc2)(drawcolumndata_t*), b x += kSkyPlaneMacroColumns; } +#else + (void)allow_parallel; + + for (x = pl->minx; x <= pl->maxx; x++) + { + dc.yl = pl->top[x]; + dc.yh = pl->bottom[x]; + + if (dc.yl > dc.yh) + { + continue; + } + + INT32 angle = (pl->viewangle + xtoviewangle[x])>>ANGLETOSKYSHIFT; + angle -= (skytextureoffset >> FRACBITS); + + dc.iscale = FixedMul(skyscale, FINECOSINE(xtoviewangle[x]>>ANGLETOFINESHIFT)); + dc.x = x; + dc.source = + R_GetColumn(texturetranslation[skytexture], + -angle); // get negative of angle for each column to display sky correct way round! --Monster Iestyn 27/01/18 + + colfunc2(&dc); + } +#endif } // Returns the height of the sloped plane at (x, y) as a 32.16 fixed_t diff --git a/src/r_segs.cpp b/src/r_segs.cpp index a4f384af4..df4eebf56 100644 --- a/src/r_segs.cpp +++ b/src/r_segs.cpp @@ -30,7 +30,6 @@ #include "screen.h" #include "core/memory.h" -#include "core/thread_pool.h" #define HEIGHTBITS 12 #define HEIGHTUNIT (1<prev; +#ifdef HAVE_THREADS srb2::ThreadPool::Sema tp_sema; srb2::g_main_threadpool->begin_sema(); +#endif R_DrawSinglePlane(&ds, r2->plane, cv_parallelsoftware.value); +#ifdef HAVE_THREADS tp_sema = srb2::g_main_threadpool->end_sema(); srb2::g_main_threadpool->notify_sema(tp_sema); srb2::g_main_threadpool->wait_sema(tp_sema); +#endif R_DoneWithNode(r2); r2 = next; } diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index faf1bcbff..5282fc9d3 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -190,7 +190,6 @@ static char returnWadPath[256]; #include "../r_fps.h" #include "../s_sound.h" -#include "../core/thread_pool.h" #ifdef MAC_ALERT #include "macosx/mac_alert.h" @@ -204,7 +203,10 @@ static char returnWadPath[256]; #include "../byteptr.h" #endif +#ifdef HAVE_THREADS +#include "../core/thread_pool.h" static std::thread::id g_main_thread_id; +#endif INT32 numcontrollers = 0; @@ -500,6 +502,7 @@ static void I_ReportSignal(int num, int coredumped) #ifndef NEWSIGNALHANDLER FUNCNORETURN static ATTRNORETURN void signal_handler(INT32 num) { +#ifdef HAVE_THREADS if (g_main_thread_id != std::this_thread::get_id()) { // Do not attempt any sort of recovery if this signal triggers off the main thread @@ -507,6 +510,7 @@ FUNCNORETURN static ATTRNORETURN void signal_handler(INT32 num) raise(num); exit(-2); } +#endif g_in_exiting_signal_handler = true; @@ -870,7 +874,9 @@ static inline void I_ShutdownConsole(void){} // static void I_RegisterSignals (void) { +#ifdef HAVE_THREADS g_main_thread_id = std::this_thread::get_id(); +#endif #ifdef SIGINT signal(SIGINT , quit_handler); @@ -2015,11 +2021,13 @@ void I_Error(const char *error, ...) va_list argptr; char buffer[8192]; +#ifdef HAVE_THREADS if (std::this_thread::get_id() != g_main_thread_id) { // Do not attempt a graceful shutdown. Errors off the main thread are unresolvable. exit(-2); } +#endif // recursive error detecting if (shutdowning) From ad15bf0ea3fcda6823af10f8ab58bb3d3d2fd9de Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 12 Jun 2025 19:48:44 +0200 Subject: [PATCH 2/2] readd some lost comments in r_bsp --- src/r_bsp.c | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/r_bsp.c b/src/r_bsp.c index 1957ec862..833e378c5 100644 --- a/src/r_bsp.c +++ b/src/r_bsp.c @@ -349,6 +349,8 @@ static void R_AddLine(seg_t *line) // The seg is in the view range, but not necessarily visible. angle1 = (angle1+ANGLE_90)>>ANGLETOFINESHIFT; angle2 = (angle2+ANGLE_90)>>ANGLETOFINESHIFT; + + // killough 1/31/98: Here is where "slime trails" can SOMETIMES occur: x1 = viewangletox[angle1]; x2 = viewangletox[angle2]; @@ -494,20 +496,10 @@ static boolean R_CheckBBox(const fixed_t *bspcoord) INT32 sx1, sx2, boxpos; const INT32* check; - // Find the corners of the box that define the edges from current viewpoint. - if (viewx <= bspcoord[BOXLEFT]) - boxpos = 0; - else if (viewx < bspcoord[BOXRIGHT]) - boxpos = 1; - else - boxpos = 2; - - if (viewy >= bspcoord[BOXTOP]) - boxpos |= 0; - else if (viewy > bspcoord[BOXBOTTOM]) - boxpos |= 1<<2; - else - boxpos |= 2<<2; + // Find the corners of the box + // that define the edges from current viewpoint. + boxpos = (viewx <= bspcoord[BOXLEFT] ? 0 : viewx < bspcoord[BOXRIGHT ] ? 1 : 2) + + (viewy >= bspcoord[BOXTOP ] ? 0 : viewy > bspcoord[BOXBOTTOM] ? 4 : 8); if (boxpos == 5) return true; @@ -518,18 +510,23 @@ static boolean R_CheckBBox(const fixed_t *bspcoord) angle1 = R_PointToAngle64(bspcoord[check[0]], bspcoord[check[1]]) - viewangle; angle2 = R_PointToAngle64(bspcoord[check[2]], bspcoord[check[3]]) - viewangle; - if ((signed)angle1 < (signed)angle2) + // cph - replaced old code, which was unclear and badly commented + // Much more efficient code now + if ((signed)angle1 < (signed)angle2) /* it's "behind" us */ { + /* Either angle1 or angle2 is behind us, so it doesn't matter if we + * change it to the corect sign + */ if ((angle1 >= ANGLE_180) && (angle1 < ANGLE_270)) angle1 = ANGLE_180-1; else angle2 = ANGLE_180; } - if ((signed)angle2 >= (signed)clipangle) return false; - if ((signed)angle1 <= -(signed)clipangle) return false; - if ((signed)angle1 >= (signed)clipangle) angle1 = clipangle; - if ((signed)angle2 <= -(signed)clipangle) angle2 = 0-clipangle; + if ((signed)angle2 >= (signed)clipangle) return false; // Both off left edge + if ((signed)angle1 <= -(signed)clipangle) return false; // Both off right edge + if ((signed)angle1 >= (signed)clipangle) angle1 = clipangle; // Clip at left edge + if ((signed)angle2 <= -(signed)clipangle) angle2 = -(signed)clipangle; // Clip at right edge // Find the first clippost that touches the source post (adjacent pixels are touching). angle1 = (angle1+ANGLE_90)>>ANGLETOFINESHIFT; @@ -550,10 +547,9 @@ static boolean R_CheckBBox(const fixed_t *bspcoord) return true; } - size_t numpolys; // number of polyobjects in current subsector size_t num_po_ptrs; // number of polyobject pointers allocated -polyobj_t **po_ptrs; // temp ptr array to sort polyobject pointers +polyobj_t **po_ptrs; // temp ptr array to sort polyobject pointers // // R_PolyobjCompare @@ -738,14 +734,15 @@ static void R_Subsector(size_t num) fixed_t floorcenterz, ceilingcenterz; ffloor_t *rover; -#ifdef RANGECHECK - if (num >= numsubsectors) - I_Error("R_Subsector: ss %s with numss = %s\n", sizeu1(num), sizeu2(numsubsectors)); -#endif - // subsectors added at run-time if (num >= numsubsectors) + { +#ifdef RANGECHECK + I_Error("R_Subsector: ss %s with numss = %s\n", sizeu1(num), sizeu2(numsubsectors)); +#else return; +#endif + } sub = &subsectors[num]; frontsector = sub->sector;