From 1f235f06de684fe5a3626b1dc51f638a1ff4cdb0 Mon Sep 17 00:00:00 2001 From: divverent Date: Thu, 16 Feb 2012 21:04:22 +0000 Subject: [PATCH] Add support for barriers to the threading interface; fix skeletal animation appearing as part of visibility in r_speeds 2 git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11692 d7cf8633-e32d-0410-b094-e92efae38249 --- gl_rmain.c | 10 ++++-- thread.h | 7 ++++- thread_null.c | 13 ++++++++ thread_pthread.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ thread_sdl.c | 50 ++++++++++++++++++++++++++++++ thread_win.c | 51 +++++++++++++++++++++++++++++++ 6 files changed, 207 insertions(+), 3 deletions(-) diff --git a/gl_rmain.c b/gl_rmain.c index 6b5095cf..c425e06f 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -5293,7 +5293,6 @@ static void R_View_UpdateWithScissor(const int *myscissor) R_View_WorldVisibility(r_refdef.view.useclipplane); R_View_UpdateEntityVisible(); R_View_UpdateEntityLighting(); - R_AnimCache_CacheVisibleEntities(); } static void R_View_Update(void) @@ -5303,7 +5302,6 @@ static void R_View_Update(void) R_View_WorldVisibility(r_refdef.view.useclipplane); R_View_UpdateEntityVisible(); R_View_UpdateEntityLighting(); - R_AnimCache_CacheVisibleEntities(); } float viewscalefpsadjusted = 1.0f; @@ -5847,6 +5845,7 @@ static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t R_View_UpdateWithScissor(myscissor); else R_View_Update(); + R_AnimCache_CacheVisibleEntities(); if(r_water_scissormode.integer & 1) GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]); R_RenderScene(p->fbo_reflection, r_fb.water.depthtexture, p->texture_reflection); @@ -5895,6 +5894,7 @@ static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t R_View_UpdateWithScissor(myscissor); else R_View_Update(); + R_AnimCache_CacheVisibleEntities(); if(r_water_scissormode.integer & 1) GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]); R_RenderScene(p->fbo_refraction, r_fb.water.depthtexture, p->texture_refraction); @@ -5949,6 +5949,7 @@ static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t R_ResetViewRendering3D(p->fbo_camera, r_fb.water.depthtexture, p->texture_camera); R_ClearScreen(r_refdef.fogenabled); R_View_Update(); + R_AnimCache_CacheVisibleEntities(); R_RenderScene(p->fbo_camera, r_fb.water.depthtexture, p->texture_camera); if (!p->fbo_camera) @@ -5964,6 +5965,7 @@ static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t if (!r_fb.water.depthtexture) R_ClearScreen(r_refdef.fogenabled); R_View_Update(); + R_AnimCache_CacheVisibleEntities(); goto finish; error: r_refdef.view = originalview; @@ -6943,6 +6945,10 @@ void R_RenderView(void) if (r_timereport_active) R_TimeReport("visibility"); + R_AnimCache_CacheVisibleEntities(); + if (r_timereport_active) + R_TimeReport("animcache"); + R_Shadow_UpdateBounceGridTexture(); if (r_timereport_active && r_shadow_bouncegrid.integer) R_TimeReport("bouncegrid"); diff --git a/thread.h b/thread.h index 7f88d8b4..7f590a47 100644 --- a/thread.h +++ b/thread.h @@ -17,6 +17,9 @@ #define Thread_CondWait(cond, mutex) (_Thread_CondWait(cond, mutex, __FILE__, __LINE__)) #define Thread_CreateThread(fn, data) (_Thread_CreateThread(fn, data, __FILE__, __LINE__)) #define Thread_WaitThread(thread, retval) (_Thread_WaitThread(thread, retval, __FILE__, __LINE__)) +#define Thread_CreateBarrier(count) (_Thread_CreateBarrier(count, __FILE__, __LINE__)) +#define Thread_DestroyBarrier(barrier) (_Thread_DestroyBarrier(barrier, __FILE__, __LINE__)) +#define Thread_WaitBarrier(barrier) (_Thread_WaitBarrier(barrier, __FILE__, __LINE__)) int Thread_Init(void); void Thread_Shutdown(void); @@ -32,6 +35,8 @@ int _Thread_CondBroadcast(void *cond, const char *filename, int fileline); int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline); void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline); int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline); +void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline); +void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline); +void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline); #endif - diff --git a/thread_null.c b/thread_null.c index c7d439c9..35c4e4b5 100644 --- a/thread_null.c +++ b/thread_null.c @@ -67,3 +67,16 @@ int _Thread_WaitThread(void *thread, int retval, const char *filename, int filel { return retval; } + +void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +{ + return NULL; +} + +void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +{ +} + +void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +{ +} diff --git a/thread_pthread.c b/thread_pthread.c index 9340403e..27ccf5fb 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -142,4 +142,83 @@ int _Thread_WaitThread(void *thread, int retval, const char *filename, int filel return (int) (intptr_t) status; } +#ifdef PTHREAD_BARRIER_SERIAL_THREAD +void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +{ + pthread_barrier_t *b = (pthread_barrier_t *) Z_Malloc(sizeof(pthread_barrier_t)); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); +#endif + pthread_barrier_init(b, NULL, count); + return (void *) b; +} + +void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +{ + pthread_barrier_t *b = (pthread_barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline); +#endif + pthread_barrier_destroy(b); +} + +void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +{ + pthread_barrier_t *b = (pthread_barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline); +#endif + pthread_barrier_wait(b); +} +#else +// standard barrier implementation using conds and mutexes +// see: http://www.howforge.com/implementing-barrier-in-pthreads +typedef struct { + unsigned int needed; + unsigned int called; + void *mutex; + void *cond; +} barrier_t; + +void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +{ + barrier_t *b = Z_Malloc(sizeof(barrier_t)); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); +#endif + b->needed = count; + b->called = 0; + b->mutex = Thread_CreateMutex(); + b->cond = Thread_CreateCond(); + return (void *) b; +} +void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +{ + barrier_t *b = (barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline); +#endif + Thread_DestroyMutex(b->mutex); + Thread_DestroyCond(b->cond); +} + +void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +{ + barrier_t *b = (barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline); +#endif + Thread_LockMutex(b->mutex); + b->called++; + if (b->called == b->needed) { + b->called = 0; + Thread_CondBroadcast(b->cond); + } else { + do { + Thread_CondWait(b->cond, b->mutex); + } while(b->called); + } + Thread_UnlockMutex(b->mutex); +} +#endif diff --git a/thread_sdl.c b/thread_sdl.c index b49069c3..9df9ffc9 100644 --- a/thread_sdl.c +++ b/thread_sdl.c @@ -117,4 +117,54 @@ int _Thread_WaitThread(void *thread, int retval, const char *filename, int filel return status; } +// standard barrier implementation using conds and mutexes +// see: http://www.howforge.com/implementing-barrier-in-pthreads +typedef struct { + unsigned int needed; + unsigned int called; + void *mutex; + void *cond; +} barrier_t; + +void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +{ + barrier_t *b = Z_Malloc(sizeof(barrier_t)); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); +#endif + b->needed = count; + b->called = 0; + b->mutex = Thread_CreateMutex(); + b->cond = Thread_CreateCond(); + return (void *) b; +} + +void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +{ + barrier_t *b = (barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline); +#endif + Thread_DestroyMutex(b->mutex); + Thread_DestroyCond(b->cond); +} + +void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +{ + barrier_t *b = (barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline); +#endif + Thread_LockMutex(b->mutex); + b->called++; + if (b->called == b->needed) { + b->called = 0; + Thread_CondBroadcast(b->cond); + } else { + do { + Thread_CondWait(b->cond, b->mutex); + } while(b->called); + } + Thread_UnlockMutex(b->mutex); +} diff --git a/thread_win.c b/thread_win.c index 77fe939c..692a6f11 100644 --- a/thread_win.c +++ b/thread_win.c @@ -250,3 +250,54 @@ int _Thread_WaitThread(void *d, int retval, const char *filename, int fileline) free(w); return retval; } + +// standard barrier implementation using conds and mutexes +// see: http://www.howforge.com/implementing-barrier-in-pthreads +typedef struct { + unsigned int needed; + unsigned int called; + void *mutex; + void *cond; +} barrier_t; + +void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +{ + barrier_t *b = Z_Malloc(sizeof(barrier_t)); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); +#endif + b->needed = count; + b->called = 0; + b->mutex = Thread_CreateMutex(); + b->cond = Thread_CreateCond(); + return (void *) b; +} + +void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +{ + barrier_t *b = (barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline); +#endif + Thread_DestroyMutex(b->mutex); + Thread_DestroyCond(b->cond); +} + +void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +{ + barrier_t *b = (barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline); +#endif + Thread_LockMutex(b->mutex); + b->called++; + if (b->called == b->needed) { + b->called = 0; + Thread_CondBroadcast(b->cond); + } else { + do { + Thread_CondWait(b->cond, b->mutex); + } while(b->called); + } + Thread_UnlockMutex(b->mutex); +} -- 2.39.2