From: havoc Date: Fri, 1 Apr 2011 14:59:17 +0000 (+0000) Subject: WGL client can now use DPSOFTRAST, added thread_win.c to avoid SDL dependency for... X-Git-Tag: xonotic-v0.6.0~163^2~541 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=0675986e0c28e85906a0c9196b3e9aae9bfb3f34;p=xonotic%2Fdarkplaces.git WGL client can now use DPSOFTRAST, added thread_win.c to avoid SDL dependency for WGL client added Sys_HaveSSE and Sys_HaveSSE2 functions in sys_shared.c and cleaned up that mess in model_alias.c dpsoftrast now compiles if SSE_POSSIBLE rather than SSE2_PRESENT, and uses Sys_HaveSSE2 for runtime detection on x86 git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11005 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/darkplaces-wgl.vcproj b/darkplaces-wgl.vcproj index 95e10ad3..fd905ff2 100644 --- a/darkplaces-wgl.vcproj +++ b/darkplaces-wgl.vcproj @@ -685,7 +685,7 @@ > #define ALIGN(var) var __attribute__((__aligned__(16))) @@ -66,7 +70,7 @@ typedef qboolean bool; #define ATOMIC_ADD(counter, val) ((void)((counter) += (val))) #endif -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE #include #define MM_MALLOC(size) _mm_malloc(size, ATOMIC_SIZE) @@ -1372,7 +1376,7 @@ static void DPSOFTRAST_Interpret_UniformMatrix4f(DPSOFTRAST_State_Thread *thread } void DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM uniform, int arraysize, int transpose, const float *v) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int i, index; for (i = 0, index = (int)uniform;i < arraysize;i++, index += 4, v += 16) { @@ -1431,7 +1435,7 @@ void DPSOFTRAST_Uniform1i(DPSOFTRAST_UNIFORM index, int i0) dpsoftrast.uniform1i[command->index] = i0; } -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE static void DPSOFTRAST_Load4fTo4f(float *dst, const unsigned char *src, int size, int stride) { float *end = dst + size*4; @@ -1627,7 +1631,7 @@ static void DPSOFTRAST_Fill4f(float *dst, const float *src, int size) void DPSOFTRAST_Vertex_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE static const float identitymatrix[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}; __m128 m0, m1, m2, m3; float *end; @@ -1678,7 +1682,7 @@ void DPSOFTRAST_Vertex_Copy(float *out4f, const float *in4f, int numitems) memcpy(out4f, in4f, numitems * sizeof(float[4])); } -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE #define DPSOFTRAST_PROJECTVERTEX(out, in, viewportcenter, viewportscale) \ { \ __m128 p = (in), w = _mm_shuffle_ps(p, p, _MM_SHUFFLE(3, 3, 3, 3)); \ @@ -1889,7 +1893,7 @@ static int DPSOFTRAST_Vertex_TransformProject(float *out4f, float *screen4f, int static float *DPSOFTRAST_Array_Load(int outarray, int inarray) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE float *outf = dpsoftrast.post_array4f[outarray]; const unsigned char *inb; int firstvertex = dpsoftrast.firstvertex; @@ -1956,7 +1960,7 @@ static float *DPSOFTRAST_Array_Transform(int outarray, int inarray, const float #if 0 static float *DPSOFTRAST_Array_Project(int outarray, int inarray) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE float *data = inarray >= 0 ? DPSOFTRAST_Array_Load(outarray, inarray) : dpsoftrast.post_array4f[outarray]; dpsoftrast.drawclipped = DPSOFTRAST_Vertex_Project(data, dpsoftrast.screencoord4f, &dpsoftrast.drawstarty, &dpsoftrast.drawendy, data, dpsoftrast.numvertices); return data; @@ -1968,7 +1972,7 @@ static float *DPSOFTRAST_Array_Project(int outarray, int inarray) static float *DPSOFTRAST_Array_TransformProject(int outarray, int inarray, const float *inmatrix16f) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE float *data = inarray >= 0 ? DPSOFTRAST_Array_Load(outarray, inarray) : dpsoftrast.post_array4f[outarray]; dpsoftrast.drawclipped = DPSOFTRAST_Vertex_TransformProject(data, dpsoftrast.screencoord4f, &dpsoftrast.drawstarty, &dpsoftrast.drawendy, data, dpsoftrast.numvertices, inmatrix16f); return data; @@ -2178,7 +2182,7 @@ void DPSOFTRAST_Draw_Span_Finish(DPSOFTRAST_State_Thread *thread, const DPSOFTRA void DPSOFTRAST_Draw_Span_FinishBGRA8(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, const unsigned char* RESTRICT in4ub) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x; int startx = span->startx; int endx = span->endx; @@ -2507,7 +2511,7 @@ void DPSOFTRAST_Draw_Span_Texture2DVarying(DPSOFTRAST_State_Thread *thread, cons void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x; int startx = span->startx; int endx = span->endx; @@ -2923,7 +2927,7 @@ void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Triangle * REST void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, int arrayindex, const float *zf) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x; int startx = span->startx; int endx = span->endx; @@ -2970,7 +2974,7 @@ void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Triangle * void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char *out4ub, int arrayindex, const float *zf) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x; int startx = span->startx; int endx = span->endx; @@ -3015,7 +3019,7 @@ void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Triangle * RESTRIC void DPSOFTRAST_Draw_Span_AddBloomBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub, const float *subcolor) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x, startx = span->startx, endx = span->endx; __m128i localcolor = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(subcolor), _mm_set1_ps(255.0f))), _MM_SHUFFLE(3, 0, 1, 2)); localcolor = _mm_packs_epi32(localcolor, localcolor); @@ -3038,7 +3042,7 @@ void DPSOFTRAST_Draw_Span_AddBloomBGRA8(const DPSOFTRAST_State_Triangle * RESTRI void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x, startx = span->startx, endx = span->endx; for (x = startx;x+2 <= endx;x+=2) { @@ -3059,7 +3063,7 @@ void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Triangle * void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x, startx = span->startx, endx = span->endx; for (x = startx;x+2 <= endx;x+=2) { @@ -3080,7 +3084,7 @@ void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Triangle * REST void DPSOFTRAST_Draw_Span_TintedAddBuffersBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub, const float *inbtintbgra) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x, startx = span->startx, endx = span->endx; __m128i tint = _mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(inbtintbgra), _mm_set1_ps(256.0f))); tint = _mm_packs_epi32(tint, tint); @@ -3103,7 +3107,7 @@ void DPSOFTRAST_Draw_Span_TintedAddBuffersBGRA8(const DPSOFTRAST_State_Triangle void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x, startx = span->startx, endx = span->endx; for (x = startx;x+2 <= endx;x+=2) { @@ -3126,7 +3130,7 @@ void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Triangle * REST void DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, const float *color) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int x, startx = span->startx, endx = span->endx; __m128i localcolor = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(color), _mm_set1_ps(255.0f))), _MM_SHUFFLE(3, 0, 1, 2)), blend; localcolor = _mm_packs_epi32(localcolor, localcolor); @@ -3254,7 +3258,7 @@ void DPSOFTRAST_VertexShader_FlatColor(void) void DPSOFTRAST_PixelShader_FlatColor(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE unsigned char * RESTRICT pixelmask = span->pixelmask; unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + (span->y * dpsoftrast.fb_width + span->x) * 4; int x, startx = span->startx, endx = span->endx; @@ -3305,7 +3309,7 @@ void DPSOFTRAST_VertexShader_VertexColor(void) void DPSOFTRAST_PixelShader_VertexColor(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE unsigned char * RESTRICT pixelmask = span->pixelmask; unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + (span->y * dpsoftrast.fb_width + span->x) * 4; int x, startx = span->startx, endx = span->endx; @@ -3379,7 +3383,7 @@ void DPSOFTRAST_VertexShader_Lightmap(void) void DPSOFTRAST_PixelShader_Lightmap(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE unsigned char * RESTRICT pixelmask = span->pixelmask; unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + (span->y * dpsoftrast.fb_width + span->x) * 4; int x, startx = span->startx, endx = span->endx; @@ -4073,7 +4077,7 @@ void DPSOFTRAST_VertexShader_LightSource(void) void DPSOFTRAST_PixelShader_LightSource(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_texture_normalbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; @@ -4679,7 +4683,7 @@ DEFCOMMAND(22, Draw, int datasize; int starty; int endy; ATOMIC_COUNTER refcount static void DPSOFTRAST_Interpret_Draw(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_Draw *command) { -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE int cullface = thread->cullface; int minx, maxx, miny, maxy; int miny1, maxy1, miny2, maxy2; diff --git a/model_alias.c b/model_alias.c index df272aca..98b0673f 100644 --- a/model_alias.c +++ b/model_alias.c @@ -74,55 +74,6 @@ void Mod_Skeletal_AnimateVertices(const dp_model_t * RESTRICT model, const frame Mod_Skeletal_AnimateVertices_Generic(model, frameblend, skeleton, vertex3f, normal3f, svector3f, tvector3f); } -#ifdef SSE_POSSIBLE -#ifndef SSE_PRESENT -// code from SDL, shortened as we can expect CPUID to work -static int CPUID_Features(void) -{ - int features = 0; -# if defined(__GNUC__) && defined(__i386__) - __asm__ ( -" movl %%ebx,%%edi\n" -" xorl %%eax,%%eax \n" -" incl %%eax \n" -" cpuid # Get family/model/stepping/features\n" -" movl %%edx,%0 \n" -" movl %%edi,%%ebx\n" - : "=m" (features) - : - : "%eax", "%ecx", "%edx", "%edi" - ); -# elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) - __asm { - xor eax, eax - inc eax - cpuid ; Get family/model/stepping/features - mov features, edx - } -# else -# error SSE_POSSIBLE set but no CPUID implementation -# endif - return features; -} -#endif -static qboolean Have_SSE(void) -{ - // COMMANDLINEOPTION: SSE: -nosse disables SSE support and detection - if(COM_CheckParm("-nosse")) - return false; - // COMMANDLINEOPTION: SSE: -forcesse enables SSE support and disables detection -#ifdef SSE_PRESENT - return true; -#else - if(COM_CheckParm("-forcesse")) - return true; - if(CPUID_Features() & (1 << 25)) - return true; - return false; -#endif -} -#endif - void Mod_AliasInit (void) { int i; @@ -136,16 +87,14 @@ void Mod_AliasInit (void) for (i = 0;i < 320;i++) mod_md3_sin[i] = sin(i * M_PI * 2.0f / 256.0); #ifdef SSE_POSSIBLE + if(Sys_HaveSSE()) { - if(Have_SSE()) - { - Con_Printf("Skeletal animation uses SSE code path\n"); - r_skeletal_use_sse_defined = true; - Cvar_RegisterVariable(&r_skeletal_use_sse); - } - else - Con_Printf("Skeletal animation uses generic code path (SSE disabled or not detected)\n"); + Con_Printf("Skeletal animation uses SSE code path\n"); + r_skeletal_use_sse_defined = true; + Cvar_RegisterVariable(&r_skeletal_use_sse); } + else + Con_Printf("Skeletal animation uses generic code path (SSE disabled or not detected)\n"); #else Con_Printf("Skeletal animation uses generic code path (SSE not compiled in)\n"); #endif diff --git a/quakedef.h b/quakedef.h index 12a953e4..03b87f73 100644 --- a/quakedef.h +++ b/quakedef.h @@ -475,6 +475,12 @@ extern cvar_t developer_loading; # undef SSE2_PRESENT #endif +// for x86 cpus only... (x64 has SSE2_PRESENT) +#if defined(SSE_POSSIBLE) && !defined(SSE2_PRESENT) +qboolean Sys_HaveSSE(void); +qboolean Sys_HaveSSE2(void); +#endif + /// incremented every frame, never reset extern int host_framecount; /// not bounded in any way, changed at start of every frame, never reset diff --git a/sys_shared.c b/sys_shared.c index 695f3465..4ed624e4 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -510,3 +510,61 @@ void Sys_ProvideSelfFD(void) return; com_selffd = FS_SysOpenFD(Sys_FindExecutableName(), "rb", false); } + +// for x86 cpus only... (x64 has SSE2_PRESENT) +#if defined(SSE_POSSIBLE) && !defined(SSE2_PRESENT) +// code from SDL, shortened as we can expect CPUID to work +static int CPUID_Features(void) +{ + int features = 0; +# if defined(__GNUC__) && defined(__i386__) + __asm__ ( +" movl %%ebx,%%edi\n" +" xorl %%eax,%%eax \n" +" incl %%eax \n" +" cpuid # Get family/model/stepping/features\n" +" movl %%edx,%0 \n" +" movl %%edi,%%ebx\n" + : "=m" (features) + : + : "%eax", "%ecx", "%edx", "%edi" + ); +# elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) + __asm { + xor eax, eax + inc eax + cpuid ; Get family/model/stepping/features + mov features, edx + } +# else +# error SSE_POSSIBLE set but no CPUID implementation +# endif + return features; +} + +qboolean Sys_HaveSSE(void) +{ + // COMMANDLINEOPTION: SSE: -nosse disables SSE support and detection + if(COM_CheckParm("-nosse")) + return false; + // COMMANDLINEOPTION: SSE: -forcesse enables SSE support and disables detection + if(COM_CheckParm("-forcesse") || COM_CheckParm("-forcesse2")) + return true; + if(CPUID_Features() & (1 << 25)) + return true; + return false; +} + +qboolean Sys_HaveSSE2(void) +{ + // COMMANDLINEOPTION: SSE2: -nosse2 disables SSE2 support and detection + if(COM_CheckParm("-nosse") || COM_CheckParm("-nosse2")) + return false; + // COMMANDLINEOPTION: SSE2: -forcesse2 enables SSE2 support and disables detection + if(COM_CheckParm("-forcesse2")) + return true; + if((CPUID_Features() & (3 << 25)) == (3 << 25)) // SSE is 1<<25, SSE2 is 1<<26 + return true; + return false; +} +#endif diff --git a/thread_win.c b/thread_win.c new file mode 100644 index 00000000..a55e6b4c --- /dev/null +++ b/thread_win.c @@ -0,0 +1,211 @@ +#include "quakedef.h" +#include "thread.h" +#include + +int Thread_Init(void) +{ + return 0; +} + +void Thread_Shutdown(void) +{ +} + +qboolean Thread_HasThreads(void) +{ + return true; +} + +void *Thread_CreateMutex(void) +{ + return (void *)CreateMutex(NULL, FALSE, NULL); +} + +void Thread_DestroyMutex(void *mutex) +{ + CloseHandle(mutex); +} + +int Thread_LockMutex(void *mutex) +{ + return (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) ? -1 : 0; +} + +int Thread_UnlockMutex(void *mutex) +{ + return (ReleaseMutex(mutex) == FALSE) ? -1 : 0; +} + +typedef struct thread_semaphore_s +{ + HANDLE semaphore; + volatile LONG value; +} +thread_semaphore_t; + +static thread_semaphore_t *Thread_CreateSemaphore(unsigned int v) +{ + thread_semaphore_t *s = (thread_semaphore_t *)calloc(sizeof(*s), 1); + s->semaphore = CreateSemaphore(NULL, v, 32768, NULL); + s->value = v; + return s; +} + +static void Thread_DestroySemaphore(thread_semaphore_t *s) +{ + CloseHandle(s->semaphore); + free(s); +} + +static int Thread_WaitSemaphore(thread_semaphore_t *s, unsigned int msec) +{ + int r = WaitForSingleObject(s->semaphore, msec); + if (r == WAIT_OBJECT_0) + { + InterlockedDecrement(&s->value); + return 0; + } + if (r == WAIT_TIMEOUT) + return 1; + return -1; +} + +static int Thread_PostSemaphore(thread_semaphore_t *s) +{ + InterlockedIncrement(&s->value); + if (ReleaseSemaphore(s->semaphore, 1, NULL)) + return 0; + InterlockedDecrement(&s->value); + return -1; +} + +typedef struct thread_cond_s +{ + HANDLE mutex; + int waiting; + int signals; + thread_semaphore_t *sem; + thread_semaphore_t *done; +} +thread_cond_t; + +void *Thread_CreateCond(void) +{ + thread_cond_t *c = (thread_cond_t *)calloc(sizeof(*c), 1); + c->mutex = CreateMutex(NULL, FALSE, NULL); + c->sem = Thread_CreateSemaphore(0); + c->done = Thread_CreateSemaphore(0); + c->waiting = 0; + c->signals = 0; + return c; +} + +void Thread_DestroyCond(void *cond) +{ + thread_cond_t *c = (thread_cond_t *)cond; + Thread_DestroySemaphore(c->sem); + Thread_DestroySemaphore(c->done); + CloseHandle(c->mutex); +} + +int Thread_CondSignal(void *cond) +{ + thread_cond_t *c = (thread_cond_t *)cond; + int n; + WaitForSingleObject(c->mutex, INFINITE); + n = c->waiting - c->signals; + if (n > 0) + { + c->signals++; + Thread_PostSemaphore(c->sem); + } + ReleaseMutex(c->mutex); + if (n > 0) + Thread_WaitSemaphore(c->done, INFINITE); + return 0; +} + +int Thread_CondBroadcast(void *cond) +{ + thread_cond_t *c = (thread_cond_t *)cond; + int i = 0; + int n = 0; + WaitForSingleObject(c->mutex, INFINITE); + n = c->waiting - c->signals; + if (n > 0) + { + c->signals += n; + for (i = 0;i < n;i++) + Thread_PostSemaphore(c->sem); + } + ReleaseMutex(c->mutex); + for (i = 0;i < n;i++) + Thread_WaitSemaphore(c->done, INFINITE); + return 0; +} + +int Thread_CondWait(void *cond, void *mutex) +{ + thread_cond_t *c = (thread_cond_t *)cond; + int waitresult; + + WaitForSingleObject(c->mutex, INFINITE); + c->waiting++; + ReleaseMutex(c->mutex); + + ReleaseMutex(mutex); + + waitresult = Thread_WaitSemaphore(c->sem, INFINITE); + WaitForSingleObject(c->mutex, INFINITE); + if (c->signals > 0) + { + if (waitresult > 0) + Thread_WaitSemaphore(c->sem, INFINITE); + Thread_PostSemaphore(c->done); + c->signals--; + } + c->waiting--; + ReleaseMutex(c->mutex); + + WaitForSingleObject(mutex, INFINITE); + return waitresult; +} + +typedef struct threadwrapper_s +{ + HANDLE handle; + unsigned int threadid; + int result; + int (*fn)(void *); + void *data; +} +threadwrapper_t; + +unsigned int __stdcall Thread_WrapperFunc(void *d) +{ + threadwrapper_t *w = (threadwrapper_t *)d; + w->result = w->fn(w->data); + _endthreadex(w->result); + return w->result; +} + +void *Thread_CreateThread(int (*fn)(void *), void *data) +{ + threadwrapper_t *w = (threadwrapper_t *)calloc(sizeof(*w), 1); + w->fn = fn; + w->data = data; + w->threadid = 0; + w->result = 0; + w->handle = (HANDLE)_beginthreadex(NULL, 0, Thread_WrapperFunc, (void *)w, 0, &w->threadid); + return (void *)w; +} + +int Thread_WaitThread(void *d, int retval) +{ + threadwrapper_t *w = (threadwrapper_t *)d; + WaitForSingleObject(w->handle, INFINITE); + CloseHandle(w->handle); + retval = w->result; + free(w); + return retval; +} diff --git a/vid.h b/vid.h index 30327583..ebde4ff4 100644 --- a/vid.h +++ b/vid.h @@ -135,6 +135,10 @@ extern cvar_t vid_hardwaregammasupported; extern qboolean vid_usinghwgamma; extern qboolean vid_supportrefreshrate; +extern cvar_t vid_soft; +extern cvar_t vid_soft_threads; +extern cvar_t vid_soft_interlace; + extern cvar_t vid_fullscreen; extern cvar_t vid_width; extern cvar_t vid_height; @@ -246,5 +250,6 @@ typedef struct vid_mode_t; size_t VID_ListModes(vid_mode_t *modes, size_t maxcount); size_t VID_SortModes(vid_mode_t *modes, size_t count, qboolean usebpp, qboolean userefreshrate, qboolean useaspect); +void VID_Soft_SharedSetup(void); #endif diff --git a/vid_sdl.c b/vid_sdl.c index e7b770d6..223ccbe9 100644 --- a/vid_sdl.c +++ b/vid_sdl.c @@ -66,9 +66,6 @@ int cl_available = true; qboolean vid_supportrefreshrate = false; -cvar_t vid_soft = {CVAR_SAVE, "vid_soft", "0", "enables use of the DarkPlaces Software Rasterizer rather than OpenGL or Direct3D"}; -cvar_t vid_soft_threads = {CVAR_SAVE, "vid_soft_threads", "2", "the number of threads the DarkPlaces Software Rasterizer should use"}; -cvar_t vid_soft_interlace = {CVAR_SAVE, "vid_soft_interlace", "1", "whether the DarkPlaces Software Rasterizer should interlace the screen bands occupied by each thread"}; cvar_t joy_detected = {CVAR_READONLY, "joy_detected", "0", "number of joysticks detected by engine"}; cvar_t joy_enable = {CVAR_SAVE, "joy_enable", "0", "enables joystick support"}; cvar_t joy_index = {0, "joy_index", "0", "selects which joystick to use if you have multiple"}; @@ -1774,9 +1771,6 @@ void VID_Init (void) Cvar_RegisterVariable(&apple_mouse_noaccel); #endif #endif - Cvar_RegisterVariable(&vid_soft); - Cvar_RegisterVariable(&vid_soft_threads); - Cvar_RegisterVariable(&vid_soft_interlace); Cvar_RegisterVariable(&joy_detected); Cvar_RegisterVariable(&joy_enable); Cvar_RegisterVariable(&joy_index); @@ -2384,62 +2378,7 @@ qboolean VID_InitModeSoft(viddef_mode_t *mode) // enable key repeat since everyone expects it SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - gl_platform = "SDLSoft"; - gl_platformextensions = ""; - - gl_renderer = "DarkPlaces-Soft"; - gl_vendor = "Forest Hale"; - gl_version = "0.0"; - gl_extensions = ""; - - // clear the extension flags - memset(&vid.support, 0, sizeof(vid.support)); - Cvar_SetQuick(&gl_info_extensions, ""); - - vid.forcevbo = false; - vid.support.arb_depth_texture = true; - vid.support.arb_draw_buffers = true; - vid.support.arb_occlusion_query = true; - vid.support.arb_shadow = true; - //vid.support.arb_texture_compression = true; - vid.support.arb_texture_cube_map = true; - vid.support.arb_texture_non_power_of_two = false; - vid.support.arb_vertex_buffer_object = true; - vid.support.ext_blend_subtract = true; - vid.support.ext_draw_range_elements = true; - vid.support.ext_framebuffer_object = true; - vid.support.ext_texture_3d = true; - //vid.support.ext_texture_compression_s3tc = true; - vid.support.ext_texture_filter_anisotropic = true; - vid.support.ati_separate_stencil = true; - - vid.maxtexturesize_2d = 16384; - vid.maxtexturesize_3d = 512; - vid.maxtexturesize_cubemap = 16384; - vid.texunits = 4; - vid.teximageunits = 32; - vid.texarrayunits = 8; - vid.max_anisotropy = 1; - vid.maxdrawbuffers = 4; - - vid.texunits = bound(4, vid.texunits, MAX_TEXTUREUNITS); - vid.teximageunits = bound(16, vid.teximageunits, MAX_TEXTUREUNITS); - vid.texarrayunits = bound(8, vid.texarrayunits, MAX_TEXTUREUNITS); - Con_DPrintf("Using DarkPlaces Software Rasterizer rendering path\n"); - vid.renderpath = RENDERPATH_SOFT; - vid.useinterleavedarrays = false; - - Cvar_SetQuick(&gl_info_vendor, gl_vendor); - Cvar_SetQuick(&gl_info_renderer, gl_renderer); - Cvar_SetQuick(&gl_info_version, gl_version); - Cvar_SetQuick(&gl_info_platform, gl_platform ? gl_platform : ""); - Cvar_SetQuick(&gl_info_driver, gl_driver); - - // LordHavoc: report supported extensions - Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions ); - - // clear to black (loading plaque will be seen over this) - GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 128); + VID_Soft_SharedSetup(); vid_numjoysticks = SDL_NumJoysticks(); vid_numjoysticks = bound(0, vid_numjoysticks, MAX_JOYSTICKS); @@ -2474,7 +2413,7 @@ qboolean VID_InitMode(viddef_mode_t *mode) { if (!SDL_WasInit(SDL_INIT_VIDEO) && SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError()); -#ifdef SSE2_PRESENT +#ifdef SSE_POSSIBLE if (vid_soft.integer) return VID_InitModeSoft(mode); else diff --git a/vid_shared.c b/vid_shared.c index b092f487..b2e2636e 100644 --- a/vid_shared.c +++ b/vid_shared.c @@ -31,6 +31,11 @@ qboolean vid_hidden = true; // let go of the mouse, turn off sound, and restore system gamma ramps... qboolean vid_activewindow = true; +// cvars for DPSOFTRAST +cvar_t vid_soft = {CVAR_SAVE, "vid_soft", "0", "enables use of the DarkPlaces Software Rasterizer rather than OpenGL or Direct3D"}; +cvar_t vid_soft_threads = {CVAR_SAVE, "vid_soft_threads", "2", "the number of threads the DarkPlaces Software Rasterizer should use"}; +cvar_t vid_soft_interlace = {CVAR_SAVE, "vid_soft_interlace", "1", "whether the DarkPlaces Software Rasterizer should interlace the screen bands occupied by each thread"}; + // we don't know until we try it! cvar_t vid_hardwaregammasupported = {CVAR_READONLY,"vid_hardwaregammasupported","1", "indicates whether hardware gamma is supported (updated by attempts to set hardware gamma ramps)"}; @@ -1193,6 +1198,20 @@ void VID_RestoreSystemGamma(void) void VID_Shared_Init(void) { +#ifdef SSE_POSSIBLE + if (Sys_HaveSSE2()) + { + Con_Printf("DPSOFTRAST available (SSE2 instructions detected)\n"); + Cvar_RegisterVariable(&vid_soft); + Cvar_RegisterVariable(&vid_soft_threads); + Cvar_RegisterVariable(&vid_soft_interlace); + } + else + Con_Printf("DPSOFTRAST not available (SSE2 disabled or not detected)\n"); +#else + Con_Printf("DPSOFTRAST not available (SSE2 not compiled in)\n"); +#endif + Cvar_RegisterVariable(&vid_hardwaregammasupported); Cvar_RegisterVariable(&gl_info_vendor); Cvar_RegisterVariable(&gl_info_renderer); @@ -1458,3 +1477,63 @@ size_t VID_SortModes(vid_mode_t *modes, size_t count, qboolean usebpp, qboolean } return count; } + +void VID_Soft_SharedSetup(void) +{ + gl_platform = "DPSOFTRAST"; + gl_platformextensions = ""; + + gl_renderer = "DarkPlaces-Soft"; + gl_vendor = "Forest Hale"; + gl_version = "0.0"; + gl_extensions = ""; + + // clear the extension flags + memset(&vid.support, 0, sizeof(vid.support)); + Cvar_SetQuick(&gl_info_extensions, ""); + + vid.forcevbo = false; + vid.support.arb_depth_texture = true; + vid.support.arb_draw_buffers = true; + vid.support.arb_occlusion_query = true; + vid.support.arb_shadow = true; + //vid.support.arb_texture_compression = true; + vid.support.arb_texture_cube_map = true; + vid.support.arb_texture_non_power_of_two = false; + vid.support.arb_vertex_buffer_object = true; + vid.support.ext_blend_subtract = true; + vid.support.ext_draw_range_elements = true; + vid.support.ext_framebuffer_object = true; + vid.support.ext_texture_3d = true; + //vid.support.ext_texture_compression_s3tc = true; + vid.support.ext_texture_filter_anisotropic = true; + vid.support.ati_separate_stencil = true; + + vid.maxtexturesize_2d = 16384; + vid.maxtexturesize_3d = 512; + vid.maxtexturesize_cubemap = 16384; + vid.texunits = 4; + vid.teximageunits = 32; + vid.texarrayunits = 8; + vid.max_anisotropy = 1; + vid.maxdrawbuffers = 4; + + vid.texunits = bound(4, vid.texunits, MAX_TEXTUREUNITS); + vid.teximageunits = bound(16, vid.teximageunits, MAX_TEXTUREUNITS); + vid.texarrayunits = bound(8, vid.texarrayunits, MAX_TEXTUREUNITS); + Con_DPrintf("Using DarkPlaces Software Rasterizer rendering path\n"); + vid.renderpath = RENDERPATH_SOFT; + vid.useinterleavedarrays = false; + + Cvar_SetQuick(&gl_info_vendor, gl_vendor); + Cvar_SetQuick(&gl_info_renderer, gl_renderer); + Cvar_SetQuick(&gl_info_version, gl_version); + Cvar_SetQuick(&gl_info_platform, gl_platform ? gl_platform : ""); + Cvar_SetQuick(&gl_info_driver, gl_driver); + + // LordHavoc: report supported extensions + Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions ); + + // clear to black (loading plaque will be seen over this) + GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 128); +} \ No newline at end of file diff --git a/vid_wgl.c b/vid_wgl.c index 1eb65241..acfece3a 100644 --- a/vid_wgl.c +++ b/vid_wgl.c @@ -45,6 +45,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifdef SUPPORTDIRECTX #include #endif +#include "dpsoftrast.h" #ifdef SUPPORTD3D #include @@ -139,6 +140,11 @@ HWND mainwindow; static HDC baseDC; static HGLRC baseRC; +static HDC vid_softhdc; +static HGDIOBJ vid_softhdc_backup; +static BITMAPINFO vid_softbmi; +static HBITMAP vid_softdibhandle; + //HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow); static qboolean vid_isfullscreen; @@ -294,76 +300,92 @@ qboolean vid_begunscene = false; void VID_Finish (void) { #ifdef SUPPORTD3D - if (vid_d3d9dev) - { - HRESULT hr; - if (vid_begunscene) - { - IDirect3DDevice9_EndScene(vid_d3d9dev); - vid_begunscene = false; - } - if (vid_reallyhidden) - return; - if (!vid_d3ddevicelost) - { - vid_hidden = vid_reallyhidden; - hr = IDirect3DDevice9_Present(vid_d3d9dev, NULL, NULL, NULL, NULL); - if (hr == D3DERR_DEVICELOST) - { - vid_d3ddevicelost = true; - vid_hidden = true; - Sleep(100); - } - } - else - { - hr = IDirect3DDevice9_TestCooperativeLevel(vid_d3d9dev); - switch(hr) - { - case D3DERR_DEVICELOST: - vid_d3ddevicelost = true; - vid_hidden = true; - Sleep(100); - break; - case D3DERR_DEVICENOTRESET: - vid_d3ddevicelost = false; - vid_hidden = vid_reallyhidden; - R_Modules_DeviceLost(); - IDirect3DDevice9_Reset(vid_d3d9dev, &vid_d3dpresentparameters); - R_Modules_DeviceRestored(); - break; - case D3D_OK: - vid_hidden = vid_reallyhidden; - IDirect3DDevice9_Present(vid_d3d9dev, NULL, NULL, NULL, NULL); - break; - } - } - if (!vid_begunscene && !vid_hidden) - { - IDirect3DDevice9_BeginScene(vid_d3d9dev); - vid_begunscene = true; - } - return; - } + HRESULT hr; #endif - vid_hidden = vid_reallyhidden; vid_usevsync = vid_vsync.integer && !cls.timedemo && qwglSwapIntervalEXT; - if (vid_usingvsync != vid_usevsync) - { - vid_usingvsync = vid_usevsync; - qwglSwapIntervalEXT (vid_usevsync); - } if (!vid_hidden) { - CHECKGLERROR - if (r_speeds.integer == 2 || gl_finish.integer) + switch(vid.renderpath) { - qglFinish();CHECKGLERROR + case RENDERPATH_GL11: + case RENDERPATH_GL13: + case RENDERPATH_GL20: + case RENDERPATH_GLES2: + if (vid_usingvsync != vid_usevsync) + { + vid_usingvsync = vid_usevsync; + qwglSwapIntervalEXT (vid_usevsync); + } + if (r_speeds.integer == 2 || gl_finish.integer) + GL_Finish(); + SwapBuffers(baseDC); + break; + case RENDERPATH_D3D9: +#ifdef SUPPORTD3D + if (vid_begunscene) + { + IDirect3DDevice9_EndScene(vid_d3d9dev); + vid_begunscene = false; + } + if (!vid_reallyhidden) + { + if (!vid_d3ddevicelost) + { + vid_hidden = vid_reallyhidden; + hr = IDirect3DDevice9_Present(vid_d3d9dev, NULL, NULL, NULL, NULL); + if (hr == D3DERR_DEVICELOST) + { + vid_d3ddevicelost = true; + vid_hidden = true; + Sleep(100); + } + } + else + { + hr = IDirect3DDevice9_TestCooperativeLevel(vid_d3d9dev); + switch(hr) + { + case D3DERR_DEVICELOST: + vid_d3ddevicelost = true; + vid_hidden = true; + Sleep(100); + break; + case D3DERR_DEVICENOTRESET: + vid_d3ddevicelost = false; + vid_hidden = vid_reallyhidden; + R_Modules_DeviceLost(); + IDirect3DDevice9_Reset(vid_d3d9dev, &vid_d3dpresentparameters); + R_Modules_DeviceRestored(); + break; + case D3D_OK: + vid_hidden = vid_reallyhidden; + IDirect3DDevice9_Present(vid_d3d9dev, NULL, NULL, NULL, NULL); + break; + } + } + if (!vid_begunscene && !vid_hidden) + { + IDirect3DDevice9_BeginScene(vid_d3d9dev); + vid_begunscene = true; + } + } +#endif + break; + case RENDERPATH_D3D10: + break; + case RENDERPATH_D3D11: + break; + case RENDERPATH_SOFT: + DPSOFTRAST_Finish(); +// baseDC = GetDC(mainwindow); + BitBlt(baseDC, 0, 0, vid.width, vid.height, vid_softhdc, 0, 0, SRCCOPY); +// ReleaseDC(mainwindow, baseDC); +// baseDC = NULL; + break; } - SwapBuffers(baseDC); } // make sure a context switch can happen every frame - Logitech drivers @@ -1613,8 +1635,293 @@ qboolean VID_InitModeDX(viddef_mode_t *mode, int version) } #endif +qboolean VID_InitModeSOFT(viddef_mode_t *mode) +{ + int i; + HDC hdc; + RECT rect; + MSG msg; + int pixelformat, newpixelformat; + DWORD WindowStyle, ExWindowStyle; + int CenterX, CenterY; + int depth; + DEVMODE thismode; + qboolean foundmode, foundgoodmode; + int bpp = mode->bitsperpixel; + int width = mode->width; + int height = mode->height; + int refreshrate = (int)floor(mode->refreshrate+0.5); + int fullscreen = mode->fullscreen; + + if (vid_initialized) + Sys_Error("VID_InitMode called when video is already initialised"); + + memset(&gdevmode, 0, sizeof(gdevmode)); + + vid_isfullscreen = false; + if (fullscreen) + { + if(vid_forcerefreshrate.integer) + { + foundmode = true; + gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; + gdevmode.dmBitsPerPel = bpp; + gdevmode.dmPelsWidth = width; + gdevmode.dmPelsHeight = height; + gdevmode.dmSize = sizeof (gdevmode); + if(refreshrate) + { + gdevmode.dmFields |= DM_DISPLAYFREQUENCY; + gdevmode.dmDisplayFrequency = refreshrate; + } + } + else + { + if(refreshrate == 0) + refreshrate = initialdevmode.dmDisplayFrequency; // default vid_refreshrate to the rate of the desktop + + foundmode = false; + foundgoodmode = false; + + thismode.dmSize = sizeof(thismode); + thismode.dmDriverExtra = 0; + for(i = 0; EnumDisplaySettings(NULL, i, &thismode); ++i) + { + if(~thismode.dmFields & (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY)) + { + Con_DPrintf("enumerating modes yielded a bogus item... please debug this\n"); + continue; + } + if(developer_extra.integer) + Con_DPrintf("Found mode %dx%dx%dbpp %dHz... ", (int)thismode.dmPelsWidth, (int)thismode.dmPelsHeight, (int)thismode.dmBitsPerPel, (int)thismode.dmDisplayFrequency); + if(thismode.dmBitsPerPel != (DWORD)bpp) + { + if(developer_extra.integer) + Con_DPrintf("wrong bpp\n"); + continue; + } + if(thismode.dmPelsWidth != (DWORD)width) + { + if(developer_extra.integer) + Con_DPrintf("wrong width\n"); + continue; + } + if(thismode.dmPelsHeight != (DWORD)height) + { + if(developer_extra.integer) + Con_DPrintf("wrong height\n"); + continue; + } + + if(foundgoodmode) + { + // if we have a good mode, make sure this mode is better than the previous one, and allowed by the refreshrate + if(thismode.dmDisplayFrequency > (DWORD)refreshrate) + { + if(developer_extra.integer) + Con_DPrintf("too high refresh rate\n"); + continue; + } + else if(thismode.dmDisplayFrequency <= gdevmode.dmDisplayFrequency) + { + if(developer_extra.integer) + Con_DPrintf("doesn't beat previous best match (too low)\n"); + continue; + } + } + else if(foundmode) + { + // we do have one, but it isn't good... make sure it has a lower frequency than the previous one + if(thismode.dmDisplayFrequency >= gdevmode.dmDisplayFrequency) + { + if(developer_extra.integer) + Con_DPrintf("doesn't beat previous best match (too high)\n"); + continue; + } + } + // otherwise, take anything + + memcpy(&gdevmode, &thismode, sizeof(gdevmode)); + if(thismode.dmDisplayFrequency <= (DWORD)refreshrate) + foundgoodmode = true; + else + { + if(developer_extra.integer) + Con_DPrintf("(out of range)\n"); + } + foundmode = true; + if(developer_extra.integer) + Con_DPrintf("accepted\n"); + } + } + + if (!foundmode) + { + VID_Shutdown(); + Con_Printf("Unable to find the requested mode %dx%dx%dbpp\n", width, height, bpp); + return false; + } + else if(ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) + { + VID_Shutdown(); + Con_Printf("Unable to change to requested mode %dx%dx%dbpp\n", width, height, bpp); + return false; + } + + vid_isfullscreen = true; + WindowStyle = WS_POPUP; + ExWindowStyle = WS_EX_TOPMOST; + } + else + { + hdc = GetDC (NULL); + i = GetDeviceCaps(hdc, RASTERCAPS); + depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL); + ReleaseDC (NULL, hdc); + if (i & RC_PALETTE) + { + VID_Shutdown(); + Con_Print("Can't run in non-RGB mode\n"); + return false; + } + if (bpp > depth) + { + VID_Shutdown(); + Con_Print("A higher desktop depth is required to run this video mode\n"); + return false; + } + + WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX; + ExWindowStyle = 0; + } + + rect.top = 0; + rect.left = 0; + rect.right = width; + rect.bottom = height; + AdjustWindowRectEx(&rect, WindowStyle, false, 0); + + if (fullscreen) + { + CenterX = 0; + CenterY = 0; + } + else + { + CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2; + CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2; + } + CenterX = max(0, CenterX); + CenterY = max(0, CenterY); + + // x and y may be changed by WM_MOVE messages + window_x = CenterX; + window_y = CenterY; + rect.left += CenterX; + rect.right += CenterX; + rect.top += CenterY; + rect.bottom += CenterY; + + pixelformat = 0; + newpixelformat = 0; + gl_extensions = ""; + gl_platformextensions = ""; + + mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL); + if (!mainwindow) + { + Con_Printf("CreateWindowEx(%d, %s, %s, %d, %d, %d, %d, %d, %p, %p, %p, %p) failed\n", (int)ExWindowStyle, "DarkPlacesWindowClass", gamename, (int)WindowStyle, (int)(rect.left), (int)(rect.top), (int)(rect.right - rect.left), (int)(rect.bottom - rect.top), (void *)NULL, (void *)NULL, (void *)global_hInstance, (void *)NULL); + VID_Shutdown(); + return false; + } + + baseDC = GetDC(mainwindow); + vid.softpixels = NULL; + memset(&vid_softbmi, 0, sizeof(vid_softbmi)); + vid_softbmi.bmiHeader.biSize = sizeof(vid_softbmi.bmiHeader); + vid_softbmi.bmiHeader.biWidth = width; + vid_softbmi.bmiHeader.biHeight = -height; // negative to make a top-down bitmap + vid_softbmi.bmiHeader.biPlanes = 1; + vid_softbmi.bmiHeader.biBitCount = 32; + vid_softbmi.bmiHeader.biCompression = BI_RGB; + vid_softbmi.bmiHeader.biSizeImage = width*height*4; + vid_softbmi.bmiHeader.biClrUsed = 256; + vid_softbmi.bmiHeader.biClrImportant = 256; + vid_softdibhandle = CreateDIBSection(baseDC, &vid_softbmi, DIB_RGB_COLORS, (void **)&vid.softpixels, NULL, 0); + if (!vid_softdibhandle) + { + Con_Printf("CreateDIBSection failed\n"); + VID_Shutdown(); + return false; + } + + vid_softhdc = CreateCompatibleDC(baseDC); + vid_softhdc_backup = SelectObject(vid_softhdc, vid_softdibhandle); + if (!vid_softhdc_backup) + { + Con_Printf("SelectObject failed\n"); + VID_Shutdown(); + return false; + } +// ReleaseDC(mainwindow, baseDC); +// baseDC = NULL; + + vid.softdepthpixels = (unsigned int *)calloc(1, mode->width * mode->height * 4); + if (DPSOFTRAST_Init(mode->width, mode->height, vid_soft_threads.integer, vid_soft_interlace.integer, (unsigned int *)vid.softpixels, (unsigned int *)vid.softdepthpixels) < 0) + { + Con_Printf("Failed to initialize software rasterizer\n"); + VID_Shutdown(); + return false; + } + + VID_Soft_SharedSetup(); + + ShowWindow (mainwindow, SW_SHOWDEFAULT); + UpdateWindow (mainwindow); + + // now we try to make sure we get the focus on the mode switch, because + // sometimes in some systems we don't. We grab the foreground, then + // finish setting up, pump all our messages, and sleep for a little while + // to let messages finish bouncing around the system, then we put + // ourselves at the top of the z order, then grab the foreground again, + // Who knows if it helps, but it probably doesn't hurt + SetForegroundWindow (mainwindow); + + while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) + { + TranslateMessage (&msg); + DispatchMessage (&msg); + } + + Sleep (100); + + SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS); + + SetForegroundWindow (mainwindow); + + // fix the leftover Alt from any Alt-Tab or the like that switched us away + ClearAllStates (); + + //vid_menudrawfn = VID_MenuDraw; + //vid_menukeyfn = VID_MenuKey; + vid_usingmouse = false; + vid_usinghidecursor = false; + vid_usingvsync = false; + vid_reallyhidden = vid_hidden = false; + vid_initialized = true; + + IN_StartupMouse (); + IN_StartupJoystick (); + + return true; +} + qboolean VID_InitMode(viddef_mode_t *mode) { +#ifdef SSE_POSSIBLE + if (vid_soft.integer) + return VID_InitModeSOFT(mode); +#endif #ifdef SUPPORTD3D // if (vid_dx11.integer) // return VID_InitModeDX(mode, 11); @@ -1644,6 +1951,20 @@ void VID_Shutdown (void) gl_extensions = ""; gl_platform = ""; gl_platformextensions = ""; + if (vid_softhdc) + { + SelectObject(vid_softhdc, vid_softhdc_backup); + ReleaseDC(mainwindow, vid_softhdc); + } + vid_softhdc = NULL; + vid_softhdc_backup = NULL; + if (vid_softdibhandle) + DeleteObject(vid_softdibhandle); + vid_softdibhandle = NULL; + vid.softpixels = NULL; + if (vid.softdepthpixels) + free(vid.softdepthpixels); + vid.softdepthpixels = NULL; #ifdef SUPPORTD3D if (vid_d3d9dev) { @@ -1669,6 +1990,7 @@ void VID_Shutdown (void) GL_CloseLibrary(); if (baseDC && mainwindow) ReleaseDC(mainwindow, baseDC); + baseDC = NULL; AppActivate(false, false); if (mainwindow) DestroyWindow(mainwindow);