From 6e32ef9b2e2b8ed0cb9862869d18e80d60c7b958 Mon Sep 17 00:00:00 2001 From: eihrul Date: Thu, 3 Feb 2011 08:33:03 +0000 Subject: [PATCH] threading and more SSE2 optimizations for dpsoftrast git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10793 d7cf8633-e32d-0410-b094-e92efae38249 --- dpsoftrast.c | 3038 +++++++++++++++++++++++++++++++------------------- dpsoftrast.h | 23 +- gl_rmain.c | 156 +-- vid_sdl.c | 5 +- 4 files changed, 1989 insertions(+), 1233 deletions(-) diff --git a/dpsoftrast.c b/dpsoftrast.c index 52376179..84785eec 100644 --- a/dpsoftrast.c +++ b/dpsoftrast.c @@ -1,4 +1,3 @@ - #include #include #define _USE_MATH_DEFINES @@ -6,26 +5,48 @@ #include "quakedef.h" #include "dpsoftrast.h" +//#define USETHREADS +#ifdef USETHREADS +#include +#include +#endif + #ifndef __cplusplus typedef qboolean bool; #endif +#define ALIGN_SIZE 16 +#define ATOMIC_SIZE 32 + #if defined(__GNUC__) #define ALIGN(var) var __attribute__((__aligned__(16))) +#define ATOMIC(var) var __attribute__((__aligned__(32))) +#define MEMORY_BARRIER (_mm_sfence()) +//(__sync_synchronize()) #elif defined(_MSC_VER) #define ALIGN(var) __declspec(align(16)) var +#define ATOMIC(var) __declspec(align(32)) var +#define MEMORY_BARRIER (_mm_sfence()) +//(MemoryBarrier()) #else #define ALIGN(var) var +#define ATOMIC(var) var +#define MEMORY_BARRIER ((void)0) +#endif + +#ifndef USETHREADS +#undef MEMORY_BARRIER +#define MEMORY_BARRIER ((void)0) #endif #ifdef SSE2_PRESENT #include -#define MM_MALLOC(size) _mm_malloc(size, 16) +#define MM_MALLOC(size) _mm_malloc(size, ATOMIC_SIZE) static void *MM_CALLOC(size_t nmemb, size_t size) { - void *ptr = _mm_malloc(nmemb*size, 16); + void *ptr = _mm_malloc(nmemb*size, ATOMIC_SIZE); if(ptr != NULL) memset(ptr, 0, nmemb*size); return ptr; } @@ -68,63 +89,92 @@ typedef struct DPSOFTRAST_Texture_s } DPSOFTRAST_Texture; -typedef struct DPSOFTRAST_State_User_s +#define COMMAND_SIZE ALIGN_SIZE +#define COMMAND_ALIGN(var) ALIGN(var) + +typedef COMMAND_ALIGN(struct DPSOFTRAST_Command_s { - int colormask[4]; - int blendfunc[2]; - int blendsubtract; - int depthmask; - int depthtest; - int depthfunc; - int scissortest; - int cullface; - int alphatest; - int alphafunc; - float alphavalue; - int scissor[4]; - int viewport[4]; - float depthrange[2]; - float polygonoffset[2]; - float color[4]; + int opcode; } -DPSOFTRAST_State_User; +DPSOFTRAST_Command); -#define DPSOFTRAST_MAXSUBSPAN 16 +enum { DPSOFTRAST_OPCODE_Reset = 0 }; -typedef ALIGN(struct DPSOFTRAST_State_Draw_Span_s +#define DEFCOMMAND(opcodeval, name, fields) \ + enum { DPSOFTRAST_OPCODE_##name = opcodeval }; \ + typedef COMMAND_ALIGN(struct DPSOFTRAST_Command_##name##_s \ + { \ + int opcode; \ + fields \ + } DPSOFTRAST_Command_##name ); + +#define DPSOFTRAST_DRAW_MAXCOMMANDPOOL 2097152 + +typedef ATOMIC(struct DPSOFTRAST_State_Command_Pool_s +{ + int freecommand; + int usedcommands; + ATOMIC(unsigned char commands[DPSOFTRAST_DRAW_MAXCOMMANDPOOL]); +} +DPSOFTRAST_State_Command_Pool); + +typedef ATOMIC(struct DPSOFTRAST_State_Triangle_s { - int start; // pixel index + int commandoffset; + unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; // texcoord to screen space density values (for picking mipmap of textures) + int starty; + int endy; + int numpoints; + float w[3]; + ALIGN(float coords[4][4]); + ALIGN(int ycoords[4]); + ALIGN(float attribs[DPSOFTRAST_ARRAY_TOTAL][3][4]); +} +DPSOFTRAST_State_Triangle); + +#define DPSOFTRAST_CALCATTRIB(triangle, span, data, slope, arrayindex) { \ + slope = _mm_load_ps((triangle)->attribs[arrayindex][0]); \ + data = _mm_add_ps(_mm_load_ps((triangle)->attribs[arrayindex][2]), \ + _mm_add_ps(_mm_mul_ps(_mm_set1_ps((span)->x), slope), \ + _mm_mul_ps(_mm_set1_ps((span)->y), _mm_load_ps((triangle)->attribs[arrayindex][1])))); \ +} +#define DPSOFTRAST_CALCATTRIB4F(triangle, span, data, slope, arrayindex) { \ + slope[0] = (triangle)->attribs[arrayindex][0][0]; \ + slope[1] = (triangle)->attribs[arrayindex][0][1]; \ + slope[2] = (triangle)->attribs[arrayindex][0][2]; \ + slope[3] = (triangle)->attribs[arrayindex][0][3]; \ + data[0] = (triangle)->attribs[arrayindex][2][0] + (span->x)*slope[0] + (span->y)*(triangle)->attribs[arrayindex][1][0]; \ + data[1] = (triangle)->attribs[arrayindex][2][1] + (span->x)*slope[1] + (span->y)*(triangle)->attribs[arrayindex][1][1]; \ + data[2] = (triangle)->attribs[arrayindex][2][2] + (span->x)*slope[2] + (span->y)*(triangle)->attribs[arrayindex][1][2]; \ + data[3] = (triangle)->attribs[arrayindex][2][3] + (span->x)*slope[3] + (span->y)*(triangle)->attribs[arrayindex][1][3]; \ +} + +#define DPSOFTRAST_DRAW_MAXSUBSPAN 16 + +typedef ALIGN(struct DPSOFTRAST_State_Span_s +{ + int triangle; // triangle this span was generated by + int x; // framebuffer x coord + int y; // framebuffer y coord int length; // pixel count int startx; // usable range (according to pixelmask) int endx; // usable range (according to pixelmask) - unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; // texcoord to screen space density values (for picking mipmap of textures) unsigned char *pixelmask; // true for pixels that passed depth test, false for others - // [0][n][] is start interpolant values (projected) - // [1][n][] is end interpolant values (projected) - // [0][DPSOFTRAST_ARRAY_TOTAL][] is start screencoord4f - // [1][DPSOFTRAST_ARRAY_TOTAL][] is end screencoord4f - // NOTE: screencoord4f[3] is W (basically 1/Z), useful for depthbuffer - ALIGN(float data[2][DPSOFTRAST_ARRAY_TOTAL+1][4]); } -DPSOFTRAST_State_Draw_Span); +DPSOFTRAST_State_Span); -#define DPSOFTRAST_DRAW_MAXSPANQUEUE 1024 +#define DPSOFTRAST_DRAW_MAXSPANS 1024 -typedef struct DPSOFTRAST_State_Draw_s -{ - int numvertices; - int maxvertices; - float *in_array4f[DPSOFTRAST_ARRAY_TOTAL]; - float *post_array4f[DPSOFTRAST_ARRAY_TOTAL]; - float *screencoord4f; +#define DPSOFTRAST_DRAW_MAXTRIANGLEPOOL 4096 +#define DPSOFTRAST_DRAW_FLUSHPROCESSTRIANGLES 64 - // spans are queued in this structure for dispatch to the pixel shader, - // partly to improve cache locality, partly for batching purposes, spans - // are flushed before DrawTriangles returns to caller - int numspans; - DPSOFTRAST_State_Draw_Span spanqueue[DPSOFTRAST_DRAW_MAXSPANQUEUE]; +typedef ATOMIC(struct DPSOFTRAST_State_Triangle_Pool_s +{ + int freetriangle; + int usedtriangles; + ATOMIC(DPSOFTRAST_State_Triangle triangles[DPSOFTRAST_DRAW_MAXTRIANGLEPOOL]); } -DPSOFTRAST_State_Draw; +DPSOFTRAST_State_Triangle_Pool); #define DPSOFTRAST_VALIDATE_FB 1 #define DPSOFTRAST_VALIDATE_DEPTHFUNC 2 @@ -146,17 +196,79 @@ typedef enum DPSOFTRAST_BLENDMODE_e } DPSOFTRAST_BLENDMODE; -typedef ALIGN(struct DPSOFTRAST_State_s +typedef ATOMIC(struct DPSOFTRAST_State_Thread_s { +#ifdef USETHREADS + SDL_Thread *thread; +#endif + int index; + + int colormask[4]; + int blendfunc[2]; + int blendsubtract; + int depthmask; + int depthtest; + int depthfunc; + int scissortest; + int alphatest; + int alphafunc; + float alphavalue; + int scissor[4]; + int viewport[4]; + float depthrange[2]; + float polygonoffset[2]; + + int shader_mode; + int shader_permutation; + + DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS]; + + ALIGN(float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4]); + int uniform1i[DPSOFTRAST_UNIFORM_TOTAL]; + // DPSOFTRAST_VALIDATE_ flags int validate; + // derived values (DPSOFTRAST_VALIDATE_FB) int fb_colormask; + int fb_clearscissor[4]; + + // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC) + int fb_depthfunc; + + // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC) + int fb_blendmode; + + ATOMIC(int commandoffset); + int triangleoffset; + + bool waiting; +#ifdef USETHREADS + SDL_cond *waitcond; +#endif + + int numspans; + DPSOFTRAST_State_Span spans[DPSOFTRAST_DRAW_MAXSPANS]; +} +DPSOFTRAST_State_Thread); + +typedef ATOMIC(struct DPSOFTRAST_State_s +{ int fb_width; int fb_height; unsigned int *fb_depthpixels; unsigned int *fb_colorpixels[4]; + int viewport[4]; + ALIGN(float fb_viewportcenter[4]); + ALIGN(float fb_viewportscale[4]); + + float color[4]; + ALIGN(float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4]); + int uniform1i[DPSOFTRAST_UNIFORM_TOTAL]; + + int cullface; + const float *pointer_vertex3f; const float *pointer_color4f; const unsigned char *pointer_color4ub; @@ -167,23 +279,14 @@ typedef ALIGN(struct DPSOFTRAST_State_s int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS]; DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS]; + int numvertices; + int maxvertices; + float *in_array4f[DPSOFTRAST_ARRAY_TOTAL]; + float *post_array4f[DPSOFTRAST_ARRAY_TOTAL]; + float *screencoord4f; + int shader_mode; int shader_permutation; - ALIGN(float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4]); - int uniform1i[DPSOFTRAST_UNIFORM_TOTAL]; - - // derived values (DPSOFTRAST_VALIDATE_FB) - int fb_clearscissor[4]; - int fb_viewport[4]; - int fb_viewportscissor[4]; - ALIGN(float fb_viewportcenter[4]); - ALIGN(float fb_viewportscale[4]); - - // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC) - int fb_depthfunc; - - // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC) - int fb_blendmode; int texture_max; int texture_end; @@ -195,9 +298,17 @@ typedef ALIGN(struct DPSOFTRAST_State_s // error reporting const char *errorstring; - DPSOFTRAST_State_User user; + int numthreads; + DPSOFTRAST_State_Thread *threads; +#ifdef USETHREADS + SDL_mutex *trianglemutex; + SDL_cond *trianglecond; +#endif - DPSOFTRAST_State_Draw draw; + ATOMIC(int drawtriangle); + + DPSOFTRAST_State_Command_Pool commandpool; + DPSOFTRAST_State_Triangle_Pool trianglepool; } DPSOFTRAST_State); @@ -211,109 +322,84 @@ extern int dpsoftrast_test; #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d))) #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256 -void DPSOFTRAST_RecalcFB(void) +void DPSOFTRAST_RecalcFB(DPSOFTRAST_State_Thread *thread) { // calculate framebuffer scissor, viewport, viewport clipped by scissor, // and viewport projection values - int x1, x2, x3, x4, x5, x6; - int y1, y2, y3, y4, y5, y6; - x1 = dpsoftrast.user.scissor[0]; - x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2]; - x3 = dpsoftrast.user.viewport[0]; - x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2]; - y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3]; - y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1]; - y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3]; - y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1]; - if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;} + int x1, x2; + int y1, y2; + x1 = thread->scissor[0]; + x2 = thread->scissor[0] + thread->scissor[2]; + y1 = dpsoftrast.fb_height - thread->scissor[1] - thread->scissor[3]; + y2 = dpsoftrast.fb_height - thread->scissor[1]; + if (!thread->scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;} if (x1 < 0) x1 = 0; if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width; - if (x3 < 0) x1 = 0; - if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width; if (y1 < 0) y1 = 0; if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height; - if (y3 < 0) y1 = 0; - if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height; - x5 = x1;if (x5 < x3) x5 = x3; - x6 = x2;if (x6 > x4) x4 = x4; - y5 = y1;if (y5 < y3) y5 = y3; - y6 = y2;if (y6 > y4) y6 = y4; - dpsoftrast.fb_clearscissor[0] = x1; - dpsoftrast.fb_clearscissor[1] = y1; - dpsoftrast.fb_clearscissor[2] = x2 - x1; - dpsoftrast.fb_clearscissor[3] = y2 - y1; - dpsoftrast.fb_viewport[0] = x3; - dpsoftrast.fb_viewport[1] = y3; - dpsoftrast.fb_viewport[2] = x4 - x3; - dpsoftrast.fb_viewport[3] = y4 - y3; - dpsoftrast.fb_viewportscissor[0] = x5; - dpsoftrast.fb_viewportscissor[1] = y5; - dpsoftrast.fb_viewportscissor[2] = x6 - x5; - dpsoftrast.fb_viewportscissor[3] = y6 - y5; - dpsoftrast.fb_viewportcenter[1] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f; - dpsoftrast.fb_viewportcenter[2] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f; - dpsoftrast.fb_viewportcenter[3] = 0.5f; - dpsoftrast.fb_viewportcenter[0] = 0.0f; - dpsoftrast.fb_viewportscale[1] = 0.5f * dpsoftrast.user.viewport[2]; - dpsoftrast.fb_viewportscale[2] = -0.5f * dpsoftrast.user.viewport[3]; - dpsoftrast.fb_viewportscale[3] = 0.5f; - dpsoftrast.fb_viewportscale[0] = 1.0f; + thread->fb_clearscissor[0] = x1; + thread->fb_clearscissor[1] = y1; + thread->fb_clearscissor[2] = x2 - x1; + thread->fb_clearscissor[3] = y2 - y1; } -void DPSOFTRAST_RecalcDepthFunc(void) +void DPSOFTRAST_RecalcDepthFunc(DPSOFTRAST_State_Thread *thread) { - dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS; + thread->fb_depthfunc = thread->depthtest ? thread->depthfunc : GL_ALWAYS; } -int blendmodetable[][4] = +void DPSOFTRAST_RecalcBlendFunc(DPSOFTRAST_State_Thread *thread) { - {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false}, - {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false}, - {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false}, - {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false}, - {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false}, - {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false}, - {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false}, - {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false}, - {DPSOFTRAST_BLENDMODE_PSEUDOALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, false}, - {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true} -}; - -void DPSOFTRAST_RecalcBlendFunc(void) -{ - int i; - dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE; - for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++) + if (thread->blendsubtract) { - if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3]) + switch ((thread->blendfunc[0]<<16)|thread->blendfunc[1]) { - dpsoftrast.fb_blendmode = blendmodetable[i][0]; - break; + #define BLENDFUNC(sfactor, dfactor, blendmode) \ + case (sfactor<<16)|dfactor: thread->fb_blendmode = blendmode; break; + BLENDFUNC(GL_SRC_COLOR, GL_ONE, DPSOFTRAST_BLENDMODE_SUBALPHA) + default: thread->fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE; break; } } + else + { + switch ((thread->blendfunc[0]<<16)|thread->blendfunc[1]) + { + BLENDFUNC(GL_ONE, GL_ZERO, DPSOFTRAST_BLENDMODE_OPAQUE) + BLENDFUNC(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, DPSOFTRAST_BLENDMODE_ALPHA) + BLENDFUNC(GL_SRC_ALPHA, GL_ONE, DPSOFTRAST_BLENDMODE_ADDALPHA) + BLENDFUNC(GL_ONE, GL_ONE, DPSOFTRAST_BLENDMODE_ADD) + BLENDFUNC(GL_ZERO, GL_ONE_MINUS_SRC_COLOR, DPSOFTRAST_BLENDMODE_INVMOD) + BLENDFUNC(GL_ZERO, GL_SRC_COLOR, DPSOFTRAST_BLENDMODE_MUL) + BLENDFUNC(GL_DST_COLOR, GL_ZERO, DPSOFTRAST_BLENDMODE_MUL) + BLENDFUNC(GL_DST_COLOR, GL_SRC_COLOR, DPSOFTRAST_BLENDMODE_MUL2) + BLENDFUNC(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, DPSOFTRAST_BLENDMODE_PSEUDOALPHA) + BLENDFUNC(GL_SRC_COLOR, GL_ONE, DPSOFTRAST_BLENDMODE_SUBALPHA) + default: thread->fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE; break; + } + } } -#define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0) +#define DPSOFTRAST_ValidateQuick(thread, f) ((thread->validate & (f)) ? (DPSOFTRAST_Validate(thread, f), 0) : 0) -void DPSOFTRAST_Validate(int mask) +void DPSOFTRAST_Validate(DPSOFTRAST_State_Thread *thread, int mask) { - mask &= dpsoftrast.validate; + mask &= thread->validate; if (!mask) return; if (mask & DPSOFTRAST_VALIDATE_FB) { - dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB; - DPSOFTRAST_RecalcFB(); + thread->validate &= ~DPSOFTRAST_VALIDATE_FB; + DPSOFTRAST_RecalcFB(thread); } if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC) { - dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC; - DPSOFTRAST_RecalcDepthFunc(); + thread->validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC; + DPSOFTRAST_RecalcDepthFunc(thread); } if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC) { - dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC; - DPSOFTRAST_RecalcBlendFunc(); + thread->validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC; + DPSOFTRAST_RecalcBlendFunc(thread); } } @@ -395,6 +481,7 @@ int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth) dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two"; return 0; } + DPSOFTRAST_Flush(); // find first empty slot in texture array for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++) if (!dpsoftrast.texture[texnum].bytes) @@ -454,6 +541,7 @@ void DPSOFTRAST_Texture_Free(int index) { DPSOFTRAST_Texture *texture; texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return; + DPSOFTRAST_Flush(); if (texture->bytes) MM_FREE(texture->bytes); texture->bytes = NULL; @@ -548,7 +636,7 @@ void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *p DPSOFTRAST_Texture *texture; unsigned char *dst; texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return; - + DPSOFTRAST_Flush(); dst = texture->bytes + (blocky * texture->mipmap[0][2] + blockx) * 4; while (blockheight > 0) { @@ -563,7 +651,7 @@ void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels) { DPSOFTRAST_Texture *texture; texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return; - + DPSOFTRAST_Flush(); memcpy(texture->bytes, pixels, texture->mipmap[0][1]); DPSOFTRAST_Texture_CalculateMipmaps(index); } @@ -589,6 +677,7 @@ unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip) { DPSOFTRAST_Texture *texture; texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0; + DPSOFTRAST_Flush(); return texture->bytes + texture->mipmap[mip][0]; } void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter) @@ -600,11 +689,16 @@ void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter) dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps"; return; } + DPSOFTRAST_Flush(); texture->filter = filter; } void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3) { + if (width != dpsoftrast.fb_width || height != dpsoftrast.fb_height || depthpixels != dpsoftrast.fb_depthpixels || + colorpixels0 != dpsoftrast.fb_colorpixels[0] || colorpixels1 != dpsoftrast.fb_colorpixels[1] || + colorpixels2 != dpsoftrast.fb_colorpixels[2] || colorpixels3 != dpsoftrast.fb_colorpixels[3]) + DPSOFTRAST_Flush(); dpsoftrast.fb_width = width; dpsoftrast.fb_height = height; dpsoftrast.fb_depthpixels = depthpixels; @@ -613,30 +707,208 @@ void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixel dpsoftrast.fb_colorpixels[2] = colorpixels2; dpsoftrast.fb_colorpixels[3] = colorpixels3; } + +void DPSOFTRAST_Draw_FlushThreads(void); + +void DPSOFTRAST_Draw_FreeTrianglePool(int space) +{ + DPSOFTRAST_State_Thread *thread; + int i; + int freetriangle = dpsoftrast.trianglepool.freetriangle; + int usedtriangles = dpsoftrast.trianglepool.usedtriangles; + if (usedtriangles <= DPSOFTRAST_DRAW_MAXTRIANGLEPOOL-space) + return; +#ifdef USETHREADS + SDL_LockMutex(dpsoftrast.trianglemutex); +#endif + for(;;) + { + int waitindex = -1; + int triangleoffset; + usedtriangles = 0; + for (i = 0; i < dpsoftrast.numthreads; i++) + { + thread = &dpsoftrast.threads[i]; + triangleoffset = freetriangle - thread->triangleoffset; + if (triangleoffset < 0) + triangleoffset += DPSOFTRAST_DRAW_MAXTRIANGLEPOOL; + if (triangleoffset > usedtriangles) + { + waitindex = i; + usedtriangles = triangleoffset; + } + } + if (usedtriangles <= DPSOFTRAST_DRAW_MAXTRIANGLEPOOL-space || waitindex < 0) + break; +#ifdef USETHREADS + thread = &dpsoftrast.threads[waitindex]; + thread->waiting = true; + SDL_CondBroadcast(dpsoftrast.trianglecond); + SDL_CondWait(thread->waitcond, dpsoftrast.trianglemutex); + thread->waiting = false; +#endif + } +#ifdef USETHREADS + SDL_UnlockMutex(dpsoftrast.trianglemutex); +#endif + dpsoftrast.trianglepool.usedtriangles = usedtriangles; +} + +void DPSOFTRAST_Draw_SyncCommands(void) +{ + DPSOFTRAST_State_Triangle *triangle; + if (dpsoftrast.trianglepool.usedtriangles >= DPSOFTRAST_DRAW_MAXTRIANGLEPOOL-1) +#ifdef USETHREADS + DPSOFTRAST_Draw_FreeTrianglePool(DPSOFTRAST_DRAW_MAXTRIANGLEPOOL/8); +#else + DPSOFTRAST_Draw_FlushThreads(); +#endif + triangle = &dpsoftrast.trianglepool.triangles[dpsoftrast.trianglepool.freetriangle]; + triangle->commandoffset = dpsoftrast.commandpool.freecommand; + triangle->starty = -1; + triangle->endy = -1; + dpsoftrast.trianglepool.freetriangle = dpsoftrast.trianglepool.freetriangle < DPSOFTRAST_DRAW_MAXTRIANGLEPOOL-1 ? dpsoftrast.trianglepool.freetriangle + 1 : 0; + dpsoftrast.trianglepool.usedtriangles++; + MEMORY_BARRIER; + dpsoftrast.drawtriangle = dpsoftrast.trianglepool.freetriangle; +} + +void DPSOFTRAST_Draw_FreeCommandPool(int space) +{ + DPSOFTRAST_State_Thread *thread; + int i; + int freecommand = dpsoftrast.commandpool.freecommand; + int usedcommands = dpsoftrast.commandpool.usedcommands; + if (usedcommands <= DPSOFTRAST_DRAW_MAXCOMMANDPOOL-space) + return; + DPSOFTRAST_Draw_SyncCommands(); +#ifdef USETHREADS + SDL_LockMutex(dpsoftrast.trianglemutex); +#endif + for(;;) + { + int waitindex = -1; + int commandoffset; + usedcommands = 0; + for (i = 0; i < dpsoftrast.numthreads; i++) + { + thread = &dpsoftrast.threads[i]; + commandoffset = freecommand - thread->commandoffset; + if (commandoffset < 0) + commandoffset += DPSOFTRAST_DRAW_MAXCOMMANDPOOL; + if (commandoffset > usedcommands) + { + waitindex = i; + usedcommands = commandoffset; + } + } + if (usedcommands <= DPSOFTRAST_DRAW_MAXCOMMANDPOOL-space || waitindex < 0) + break; +#ifdef USETHREADS + thread = &dpsoftrast.threads[waitindex]; + thread->waiting = true; + SDL_CondBroadcast(dpsoftrast.trianglecond); + SDL_CondWait(thread->waitcond, dpsoftrast.trianglemutex); + thread->waiting = false; +#endif + } +#ifdef USETHREADS + SDL_UnlockMutex(dpsoftrast.trianglemutex); +#endif + dpsoftrast.commandpool.usedcommands = usedcommands; +} + +#define DPSOFTRAST_ALLOCATECOMMAND(name) \ + ((DPSOFTRAST_Command_##name *) DPSOFTRAST_AllocateCommand(sizeof( DPSOFTRAST_Command_##name ) + ((COMMAND_SIZE - (sizeof( DPSOFTRAST_Command_##name )&(COMMAND_SIZE-1))) & (COMMAND_SIZE-1)))) + +static void *DPSOFTRAST_AllocateCommand(int size) +{ + DPSOFTRAST_Command *command; + int freecommand = dpsoftrast.commandpool.freecommand; + int usedcommands = dpsoftrast.commandpool.usedcommands; + int extra = sizeof(DPSOFTRAST_Command); + if(DPSOFTRAST_DRAW_MAXCOMMANDPOOL - freecommand < size) + extra += DPSOFTRAST_DRAW_MAXCOMMANDPOOL - freecommand; + if(usedcommands > DPSOFTRAST_DRAW_MAXCOMMANDPOOL - (size + extra)) + { +#ifdef USETHREADS + DPSOFTRAST_Draw_FreeCommandPool(size + extra); +#else + DPSOFTRAST_Draw_FlushThreads(); +#endif + freecommand = dpsoftrast.commandpool.freecommand; + usedcommands = dpsoftrast.commandpool.usedcommands; + } + if(DPSOFTRAST_DRAW_MAXCOMMANDPOOL - freecommand < size) + { + command = (DPSOFTRAST_Command *) &dpsoftrast.commandpool.commands[freecommand]; + command->opcode = DPSOFTRAST_OPCODE_Reset; + usedcommands += DPSOFTRAST_DRAW_MAXCOMMANDPOOL - freecommand; + freecommand = 0; + } + command = (DPSOFTRAST_Command *) &dpsoftrast.commandpool.commands[freecommand]; + freecommand += size; + if (freecommand >= DPSOFTRAST_DRAW_MAXCOMMANDPOOL) + freecommand = 0; + + dpsoftrast.commandpool.freecommand = freecommand; + dpsoftrast.commandpool.usedcommands = usedcommands + size; + return command; +} + +DEFCOMMAND(1, Viewport, int x; int y; int width; int height;) +static void DPSOFTRAST_Interpret_Viewport(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_Command_Viewport *command) +{ + thread->viewport[0] = command->x; + thread->viewport[1] = command->y; + thread->viewport[2] = command->width; + thread->viewport[3] = command->height; + thread->validate |= DPSOFTRAST_VALIDATE_FB; +} void DPSOFTRAST_Viewport(int x, int y, int width, int height) { - dpsoftrast.user.viewport[0] = x; - dpsoftrast.user.viewport[1] = y; - dpsoftrast.user.viewport[2] = width; - dpsoftrast.user.viewport[3] = height; - dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB; + DPSOFTRAST_Command_Viewport *command = DPSOFTRAST_ALLOCATECOMMAND(Viewport); + command->opcode = DPSOFTRAST_OPCODE_Viewport; + command->x = x; + command->y = y; + command->width = width; + command->height = height; + + dpsoftrast.viewport[0] = x; + dpsoftrast.viewport[1] = y; + dpsoftrast.viewport[2] = width; + dpsoftrast.viewport[3] = height; + dpsoftrast.fb_viewportcenter[1] = dpsoftrast.viewport[0] + 0.5f * dpsoftrast.viewport[2] - 0.5f; + dpsoftrast.fb_viewportcenter[2] = dpsoftrast.fb_height - dpsoftrast.viewport[1] - 0.5f * dpsoftrast.viewport[3] - 0.5f; + dpsoftrast.fb_viewportcenter[3] = 0.5f; + dpsoftrast.fb_viewportcenter[0] = 0.0f; + dpsoftrast.fb_viewportscale[1] = 0.5f * dpsoftrast.viewport[2]; + dpsoftrast.fb_viewportscale[2] = -0.5f * dpsoftrast.viewport[3]; + dpsoftrast.fb_viewportscale[3] = 0.5f; + dpsoftrast.fb_viewportscale[0] = 1.0f; } -void DPSOFTRAST_ClearColor(float r, float g, float b, float a) + +DEFCOMMAND(2, ClearColor, float r; float g; float b; float a;) +static void DPSOFTRAST_Interpret_ClearColor(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_Command_ClearColor *command) { - int i, x1, y1, x2, y2, w, h, x, y; + int i, x1, y1, x2, y2, w, h, x, y, t1, t2; unsigned int *p; unsigned int c; - DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB); - x1 = dpsoftrast.fb_clearscissor[0]; - y1 = dpsoftrast.fb_clearscissor[1]; - x2 = dpsoftrast.fb_clearscissor[2]; - y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3]; + DPSOFTRAST_Validate(thread, DPSOFTRAST_VALIDATE_FB); + x1 = thread->fb_clearscissor[0]; + y1 = thread->fb_clearscissor[1]; + x2 = thread->fb_clearscissor[2]; + y2 = thread->fb_clearscissor[1] + thread->fb_clearscissor[3]; + t1 = (thread->index*dpsoftrast.fb_height)/dpsoftrast.numthreads; + t2 = ((thread->index+1)*dpsoftrast.fb_height)/dpsoftrast.numthreads; + if(y1 < t1) y1 = t1; + if(y2 > t2) y2 = t2; w = x2 - x1; h = y2 - y1; if (w < 1 || h < 1) return; - // FIXME: honor dpsoftrast.fb_colormask? - c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a); + // FIXME: honor fb_colormask? + c = DPSOFTRAST_BGRA8_FROM_RGBA32F(command->r,command->g,command->b,command->a); for (i = 0;i < 4;i++) { if (!dpsoftrast.fb_colorpixels[i]) @@ -649,21 +921,36 @@ void DPSOFTRAST_ClearColor(float r, float g, float b, float a) } } } -void DPSOFTRAST_ClearDepth(float d) +void DPSOFTRAST_ClearColor(float r, float g, float b, float a) { - int x1, y1, x2, y2, w, h, x, y; + DPSOFTRAST_Command_ClearColor *command = DPSOFTRAST_ALLOCATECOMMAND(ClearColor); + command->opcode = DPSOFTRAST_OPCODE_ClearColor; + command->r = r; + command->g = g; + command->b = b; + command->a = a; +} + +DEFCOMMAND(3, ClearDepth, float depth;) +static void DPSOFTRAST_Interpret_ClearDepth(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_ClearDepth *command) +{ + int x1, y1, x2, y2, w, h, x, y, t1, t2; unsigned int *p; unsigned int c; - DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB); - x1 = dpsoftrast.fb_clearscissor[0]; - y1 = dpsoftrast.fb_clearscissor[1]; - x2 = dpsoftrast.fb_clearscissor[2]; - y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3]; + DPSOFTRAST_Validate(thread, DPSOFTRAST_VALIDATE_FB); + x1 = thread->fb_clearscissor[0]; + y1 = thread->fb_clearscissor[1]; + x2 = thread->fb_clearscissor[2]; + y2 = thread->fb_clearscissor[1] + thread->fb_clearscissor[3]; + t1 = (thread->index*dpsoftrast.fb_height)/dpsoftrast.numthreads; + t2 = ((thread->index+1)*dpsoftrast.fb_height)/dpsoftrast.numthreads; + if(y1 < t1) y1 = t1; + if(y2 > t2) y2 = t2; w = x2 - x1; h = y2 - y1; if (w < 1 || h < 1) return; - c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d); + c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(command->depth); for (y = y1;y < y2;y++) { p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width; @@ -671,86 +958,196 @@ void DPSOFTRAST_ClearDepth(float d) p[x] = c; } } +void DPSOFTRAST_ClearDepth(float d) +{ + DPSOFTRAST_Command_ClearDepth *command = DPSOFTRAST_ALLOCATECOMMAND(ClearDepth); + command->opcode = DPSOFTRAST_OPCODE_ClearDepth; + command->depth = d; +} + +DEFCOMMAND(4, ColorMask, int r; int g; int b; int a;) +static void DPSOFTRAST_Interpret_ColorMask(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_ColorMask *command) +{ + thread->colormask[0] = command->r != 0; + thread->colormask[1] = command->g != 0; + thread->colormask[2] = command->b != 0; + thread->colormask[3] = command->a != 0; + thread->fb_colormask = ((-thread->colormask[0]) & 0x00FF0000) | ((-thread->colormask[1]) & 0x0000FF00) | ((-thread->colormask[2]) & 0x000000FF) | ((-thread->colormask[3]) & 0xFF000000); +} void DPSOFTRAST_ColorMask(int r, int g, int b, int a) { - dpsoftrast.user.colormask[0] = r != 0; - dpsoftrast.user.colormask[1] = g != 0; - dpsoftrast.user.colormask[2] = b != 0; - dpsoftrast.user.colormask[3] = a != 0; - dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000); + DPSOFTRAST_Command_ColorMask *command = DPSOFTRAST_ALLOCATECOMMAND(ColorMask); + command->opcode = DPSOFTRAST_OPCODE_ColorMask; + command->r = r; + command->g = g; + command->b = b; + command->a = a; +} + +DEFCOMMAND(5, DepthTest, int enable;) +static void DPSOFTRAST_Interpret_DepthTest(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_DepthTest *command) +{ + thread->depthtest = command->enable; + thread->validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC; } void DPSOFTRAST_DepthTest(int enable) { - dpsoftrast.user.depthtest = enable; - dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC; + DPSOFTRAST_Command_DepthTest *command = DPSOFTRAST_ALLOCATECOMMAND(DepthTest); + command->opcode = DPSOFTRAST_OPCODE_DepthTest; + command->enable = enable; +} + +DEFCOMMAND(6, ScissorTest, int enable;) +static void DPSOFTRAST_Interpret_ScissorTest(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_ScissorTest *command) +{ + thread->scissortest = command->enable; + thread->validate |= DPSOFTRAST_VALIDATE_FB; } void DPSOFTRAST_ScissorTest(int enable) { - dpsoftrast.user.scissortest = enable; - dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB; + DPSOFTRAST_Command_ScissorTest *command = DPSOFTRAST_ALLOCATECOMMAND(ScissorTest); + command->opcode = DPSOFTRAST_OPCODE_ScissorTest; + command->enable = enable; +} + +DEFCOMMAND(7, Scissor, float x; float y; float width; float height;) +static void DPSOFTRAST_Interpret_Scissor(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_Scissor *command) +{ + thread->scissor[0] = command->x; + thread->scissor[1] = command->y; + thread->scissor[2] = command->width; + thread->scissor[3] = command->height; + thread->validate |= DPSOFTRAST_VALIDATE_FB; } void DPSOFTRAST_Scissor(float x, float y, float width, float height) { - dpsoftrast.user.scissor[0] = x; - dpsoftrast.user.scissor[1] = y; - dpsoftrast.user.scissor[2] = width; - dpsoftrast.user.scissor[3] = height; - dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB; + DPSOFTRAST_Command_Scissor *command = DPSOFTRAST_ALLOCATECOMMAND(Scissor); + command->opcode = DPSOFTRAST_OPCODE_Scissor; + command->x = x; + command->y = y; + command->width = width; + command->height = height; } -void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate) +DEFCOMMAND(8, BlendFunc, int sfactor; int dfactor;) +static void DPSOFTRAST_Interpret_BlendFunc(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_BlendFunc *command) +{ + thread->blendfunc[0] = command->sfactor; + thread->blendfunc[1] = command->dfactor; + thread->validate |= DPSOFTRAST_VALIDATE_BLENDFUNC; +} +void DPSOFTRAST_BlendFunc(int sfactor, int dfactor) { - // FIXME: validate - dpsoftrast.user.blendfunc[0] = smodulate; - dpsoftrast.user.blendfunc[1] = dmodulate; - dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC; + DPSOFTRAST_Command_BlendFunc *command = DPSOFTRAST_ALLOCATECOMMAND(BlendFunc); + command->opcode = DPSOFTRAST_OPCODE_BlendFunc; + command->sfactor = sfactor; + command->dfactor = dfactor; +} + +DEFCOMMAND(9, BlendSubtract, int enable;) +static void DPSOFTRAST_Interpret_BlendSubtract(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_BlendSubtract *command) +{ + thread->blendsubtract = command->enable; + thread->validate |= DPSOFTRAST_VALIDATE_BLENDFUNC; } void DPSOFTRAST_BlendSubtract(int enable) { - dpsoftrast.user.blendsubtract = enable != 0; - dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC; + DPSOFTRAST_Command_BlendSubtract *command = DPSOFTRAST_ALLOCATECOMMAND(BlendSubtract); + command->opcode = DPSOFTRAST_OPCODE_BlendSubtract; + command->enable = enable; +} + +DEFCOMMAND(10, DepthMask, int enable;) +static void DPSOFTRAST_Interpret_DepthMask(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_DepthMask *command) +{ + thread->depthmask = command->enable; } void DPSOFTRAST_DepthMask(int enable) { - dpsoftrast.user.depthmask = enable; + DPSOFTRAST_Command_DepthMask *command = DPSOFTRAST_ALLOCATECOMMAND(DepthMask); + command->opcode = DPSOFTRAST_OPCODE_DepthMask; + command->enable = enable; } -void DPSOFTRAST_DepthFunc(int comparemode) + +DEFCOMMAND(11, DepthFunc, int func;) +static void DPSOFTRAST_Interpret_DepthFunc(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_DepthFunc *command) { - // FIXME: validate - dpsoftrast.user.depthfunc = comparemode; + thread->depthfunc = command->func; } -void DPSOFTRAST_DepthRange(float range0, float range1) +void DPSOFTRAST_DepthFunc(int func) { - dpsoftrast.user.depthrange[0] = range0; - dpsoftrast.user.depthrange[1] = range1; + DPSOFTRAST_Command_DepthFunc *command = DPSOFTRAST_ALLOCATECOMMAND(DepthFunc); + command->opcode = DPSOFTRAST_OPCODE_DepthFunc; + command->func = func; +} + +DEFCOMMAND(12, DepthRange, float nearval; float farval;) +static void DPSOFTRAST_Interpret_DepthRange(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_DepthRange *command) +{ + thread->depthrange[0] = command->nearval; + thread->depthrange[1] = command->farval; +} +void DPSOFTRAST_DepthRange(float nearval, float farval) +{ + DPSOFTRAST_Command_DepthRange *command = DPSOFTRAST_ALLOCATECOMMAND(DepthRange); + command->opcode = DPSOFTRAST_OPCODE_DepthRange; + command->nearval = nearval; + command->farval = farval; +} + +DEFCOMMAND(13, PolygonOffset, float alongnormal; float intoview;) +static void DPSOFTRAST_Interpret_PolygonOffset(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_PolygonOffset *command) +{ + thread->polygonoffset[0] = command->alongnormal; + thread->polygonoffset[1] = command->intoview; } void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview) { - dpsoftrast.user.polygonoffset[0] = alongnormal; - dpsoftrast.user.polygonoffset[1] = intoview; + DPSOFTRAST_Command_PolygonOffset *command = DPSOFTRAST_ALLOCATECOMMAND(PolygonOffset); + command->opcode = DPSOFTRAST_OPCODE_PolygonOffset; + command->alongnormal = alongnormal; + command->intoview = intoview; } + void DPSOFTRAST_CullFace(int mode) { - // FIXME: validate - dpsoftrast.user.cullface = mode; + dpsoftrast.cullface = mode; +} + +DEFCOMMAND(15, AlphaTest, int enable;) +static void DPSOFTRAST_Interpret_AlphaTest(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_AlphaTest *command) +{ + thread->alphatest = command->enable; +} +void DPSOFTRAST_AlphaTest(int enable) +{ + DPSOFTRAST_Command_AlphaTest *command = DPSOFTRAST_ALLOCATECOMMAND(AlphaTest); + command->opcode = DPSOFTRAST_OPCODE_AlphaTest; + command->enable = enable; } -void DPSOFTRAST_AlphaTest(float enable) + +DEFCOMMAND(16, AlphaFunc, int func; float ref;) +static void DPSOFTRAST_Interpret_AlphaFunc(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_AlphaFunc *command) { - dpsoftrast.user.alphatest = enable; + thread->alphafunc = command->func; + thread->alphavalue = command->ref; } -void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue) +void DPSOFTRAST_AlphaFunc(int func, float ref) { - // FIXME: validate - dpsoftrast.user.alphafunc = alphafunc; - dpsoftrast.user.alphavalue = alphavalue; + DPSOFTRAST_Command_AlphaFunc *command = DPSOFTRAST_ALLOCATECOMMAND(AlphaFunc); + command->opcode = DPSOFTRAST_OPCODE_AlphaFunc; + command->func = func; + command->ref = ref; } + void DPSOFTRAST_Color4f(float r, float g, float b, float a) { - dpsoftrast.user.color[0] = r; - dpsoftrast.user.color[1] = g; - dpsoftrast.user.color[2] = b; - dpsoftrast.user.color[3] = a; + dpsoftrast.color[0] = r; + dpsoftrast.color[1] = g; + dpsoftrast.color[2] = b; + dpsoftrast.color[3] = a; } + void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels) { int outstride = blockwidth * 4; @@ -766,6 +1163,7 @@ void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockh unsigned char *inpixels; unsigned char *b; unsigned char *o; + DPSOFTRAST_Flush(); if (bx1 < 0) bx1 = 0; if (by1 < 0) by1 = 0; if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width; @@ -825,6 +1223,7 @@ void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int s DPSOFTRAST_Texture *texture; texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return; if (mip < 0 || mip >= texture->mipmaps) return; + DPSOFTRAST_Flush(); spixels = dpsoftrast.fb_colorpixels[0]; swidth = dpsoftrast.fb_width; sheight = dpsoftrast.fb_height; @@ -852,8 +1251,15 @@ void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int s if (texture->mipmaps > 1) DPSOFTRAST_Texture_CalculateMipmaps(index); } + +DEFCOMMAND(17, SetTexture, int unitnum; DPSOFTRAST_Texture *texture;) +static void DPSOFTRAST_Interpret_SetTexture(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_SetTexture *command) +{ + thread->texbound[command->unitnum] = command->texture; +} void DPSOFTRAST_SetTexture(int unitnum, int index) { + DPSOFTRAST_Command_SetTexture *command; DPSOFTRAST_Texture *texture; if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS) { @@ -866,6 +1272,12 @@ void DPSOFTRAST_SetTexture(int unitnum, int index) dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle"; return; } + + command = DPSOFTRAST_ALLOCATECOMMAND(SetTexture); + command->opcode = DPSOFTRAST_OPCODE_SetTexture; + command->unitnum = unitnum; + command->texture = texture; + dpsoftrast.texbound[unitnum] = texture; } @@ -893,77 +1305,318 @@ void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride dpsoftrast.stride_texcoord[unitnum] = stride; } -void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation) +DEFCOMMAND(18, SetShader, int mode; int permutation;) +static void DPSOFTRAST_Interpret_SetShader(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_SetShader *command) { + thread->shader_mode = command->mode; + thread->shader_permutation = command->permutation; +} +void DPSOFTRAST_SetShader(int mode, int permutation) +{ + DPSOFTRAST_Command_SetShader *command = DPSOFTRAST_ALLOCATECOMMAND(SetShader); + command->opcode = DPSOFTRAST_OPCODE_SetShader; + command->mode = mode; + command->permutation = permutation; + dpsoftrast.shader_mode = mode; dpsoftrast.shader_permutation = permutation; } -void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3) + +DEFCOMMAND(19, Uniform4f, DPSOFTRAST_UNIFORM index; float val[4];) +static void DPSOFTRAST_Interpret_Uniform4f(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_Uniform4f *command) +{ + memcpy(&thread->uniform4f[command->index*4], command->val, sizeof(command->val)); +} +void DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3) { + DPSOFTRAST_Command_Uniform4f *command = DPSOFTRAST_ALLOCATECOMMAND(Uniform4f); + command->opcode = DPSOFTRAST_OPCODE_Uniform4f; + command->index = index; + command->val[0] = v0; + command->val[1] = v1; + command->val[2] = v2; + command->val[3] = v3; + dpsoftrast.uniform4f[index*4+0] = v0; dpsoftrast.uniform4f[index*4+1] = v1; dpsoftrast.uniform4f[index*4+2] = v2; dpsoftrast.uniform4f[index*4+3] = v3; } -void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v) +void DPSOFTRAST_Uniform4fv(DPSOFTRAST_UNIFORM index, const float *v) +{ + DPSOFTRAST_Command_Uniform4f *command = DPSOFTRAST_ALLOCATECOMMAND(Uniform4f); + command->opcode = DPSOFTRAST_OPCODE_Uniform4f; + command->index = index; + memcpy(command->val, v, sizeof(command->val)); + + memcpy(&dpsoftrast.uniform4f[index*4], v, sizeof(float[4])); +} + +DEFCOMMAND(20, UniformMatrix4f, DPSOFTRAST_UNIFORM index; ALIGN(float val[16]);) +static void DPSOFTRAST_Interpret_UniformMatrix4f(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_UniformMatrix4f *command) { - dpsoftrast.uniform4f[index*4+0] = v[0]; - dpsoftrast.uniform4f[index*4+1] = v[1]; - dpsoftrast.uniform4f[index*4+2] = v[2]; - dpsoftrast.uniform4f[index*4+3] = v[3]; + memcpy(&thread->uniform4f[command->index*4], command->val, sizeof(command->val)); } -void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM uniform, int arraysize, int transpose, const float *v) +void DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM uniform, int arraysize, int transpose, const float *v) { +#ifdef SSE2_PRESENT int i, index; for (i = 0, index = (int)uniform;i < arraysize;i++, index += 4, v += 16) { + __m128 m0, m1, m2, m3; + DPSOFTRAST_Command_UniformMatrix4f *command = DPSOFTRAST_ALLOCATECOMMAND(UniformMatrix4f); + command->opcode = DPSOFTRAST_OPCODE_UniformMatrix4f; + command->index = index; + if (((size_t)v)&(ALIGN_SIZE-1)) + { + m0 = _mm_loadu_ps(v); + m1 = _mm_loadu_ps(v+4); + m2 = _mm_loadu_ps(v+8); + m3 = _mm_loadu_ps(v+12); + } + else + { + m0 = _mm_load_ps(v); + m1 = _mm_load_ps(v+4); + m2 = _mm_load_ps(v+8); + m3 = _mm_load_ps(v+12); + } if (transpose) { - dpsoftrast.uniform4f[index*4+0] = v[0]; - dpsoftrast.uniform4f[index*4+1] = v[4]; - dpsoftrast.uniform4f[index*4+2] = v[8]; - dpsoftrast.uniform4f[index*4+3] = v[12]; - dpsoftrast.uniform4f[index*4+4] = v[1]; - dpsoftrast.uniform4f[index*4+5] = v[5]; - dpsoftrast.uniform4f[index*4+6] = v[9]; - dpsoftrast.uniform4f[index*4+7] = v[13]; - dpsoftrast.uniform4f[index*4+8] = v[2]; - dpsoftrast.uniform4f[index*4+9] = v[6]; - dpsoftrast.uniform4f[index*4+10] = v[10]; - dpsoftrast.uniform4f[index*4+11] = v[14]; - dpsoftrast.uniform4f[index*4+12] = v[3]; - dpsoftrast.uniform4f[index*4+13] = v[7]; - dpsoftrast.uniform4f[index*4+14] = v[11]; - dpsoftrast.uniform4f[index*4+15] = v[15]; + __m128 t0, t1, t2, t3; + t0 = _mm_unpacklo_ps(m0, m1); + t1 = _mm_unpacklo_ps(m2, m3); + t2 = _mm_unpackhi_ps(m0, m1); + t3 = _mm_unpackhi_ps(m2, m3); + m0 = _mm_movelh_ps(t0, t1); + m1 = _mm_movehl_ps(t1, t0); + m2 = _mm_movelh_ps(t2, t3); + m3 = _mm_movehl_ps(t3, t2); + } + _mm_store_ps(command->val, m0); + _mm_store_ps(command->val+4, m1); + _mm_store_ps(command->val+8, m2); + _mm_store_ps(command->val+12, m3); + _mm_store_ps(&dpsoftrast.uniform4f[index*4+0], m0); + _mm_store_ps(&dpsoftrast.uniform4f[index*4+4], m1); + _mm_store_ps(&dpsoftrast.uniform4f[index*4+8], m2); + _mm_store_ps(&dpsoftrast.uniform4f[index*4+12], m3); + } +#endif +} + +DEFCOMMAND(21, Uniform1i, DPSOFTRAST_UNIFORM index; int val;) +static void DPSOFTRAST_Interpret_Uniform1i(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_Uniform1i *command) +{ + thread->uniform1i[command->index] = command->val; +} +void DPSOFTRAST_Uniform1i(DPSOFTRAST_UNIFORM index, int i0) +{ + DPSOFTRAST_Command_Uniform1i *command = DPSOFTRAST_ALLOCATECOMMAND(Uniform1i); + command->opcode = DPSOFTRAST_OPCODE_Uniform1i; + command->index = index; + command->val = i0; + + dpsoftrast.uniform1i[command->index] = i0; +} + +#ifdef SSE2_PRESENT +static void DPSOFTRAST_Load4fTo4f(float *dst, const unsigned char *src, int size, int stride) +{ + float *end = dst + size*4; + if ((((size_t)src)|stride)&(ALIGN_SIZE - 1)) // check for alignment + { + while (dst < end) + { + _mm_store_ps(dst, _mm_loadu_ps((const float *)src)); + dst += 4; + src += stride; + } + } + else + { + while (dst < end) + { + _mm_store_ps(dst, _mm_load_ps((const float *)src)); + dst += 4; + src += stride; + } + } +} + +static void DPSOFTRAST_Load3fTo4f(float *dst, const unsigned char *src, int size, int stride) +{ + float *end = dst + size*4; + if (stride == sizeof(float[3])) + { + float *end4 = dst + (size&~3)*4; + if (((size_t)src)&(ALIGN_SIZE - 1)) // check for alignment + { + while (dst < end4) + { + __m128 v1 = _mm_loadu_ps((const float *)src), v2 = _mm_loadu_ps((const float *)src + 4), v3 = _mm_loadu_ps((const float *)src + 8), dv; + dv = _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(2, 1, 0, 3)); + dv = _mm_move_ss(dv, _mm_set_ss(1.0f)); + _mm_store_ps(dst, _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(0, 3, 2, 1))); + dv = _mm_shuffle_ps(v1, v2, _MM_SHUFFLE(1, 0, 3, 3)); + dv = _mm_move_ss(dv, _mm_set_ss(1.0f)); + _mm_store_ps(dst + 4, _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(0, 3, 2, 1))); + dv = _mm_shuffle_ps(v2, v3, _MM_SHUFFLE(0, 0, 3, 2)); + dv = _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(2, 1, 0, 3)); + dv = _mm_move_ss(dv, _mm_set_ss(1.0f)); + _mm_store_ps(dst + 8, _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(0, 3, 2, 1))); + dv = _mm_move_ss(v3, _mm_set_ss(1.0f)); + _mm_store_ps(dst + 12, _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(0, 3, 2, 1))); + dst += 16; + src += 4*sizeof(float[3]); + } + } + else + { + while (dst < end4) + { + __m128 v1 = _mm_load_ps((const float *)src), v2 = _mm_load_ps((const float *)src + 4), v3 = _mm_load_ps((const float *)src + 8), dv; + dv = _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(2, 1, 0, 3)); + dv = _mm_move_ss(dv, _mm_set_ss(1.0f)); + _mm_store_ps(dst, _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(0, 3, 2, 1))); + dv = _mm_shuffle_ps(v1, v2, _MM_SHUFFLE(1, 0, 3, 3)); + dv = _mm_move_ss(dv, _mm_set_ss(1.0f)); + _mm_store_ps(dst + 4, _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(0, 3, 2, 1))); + dv = _mm_shuffle_ps(v2, v3, _MM_SHUFFLE(0, 0, 3, 2)); + dv = _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(2, 1, 0, 3)); + dv = _mm_move_ss(dv, _mm_set_ss(1.0f)); + _mm_store_ps(dst + 8, _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(0, 3, 2, 1))); + dv = _mm_move_ss(v3, _mm_set_ss(1.0f)); + _mm_store_ps(dst + 12, _mm_shuffle_ps(dv, dv, _MM_SHUFFLE(0, 3, 2, 1))); + dst += 16; + src += 4*sizeof(float[3]); + } + } + } + if ((((size_t)src)|stride)&(ALIGN_SIZE - 1)) + { + while (dst < end) + { + __m128 v = _mm_loadu_ps((const float *)src); + v = _mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)); + v = _mm_move_ss(v, _mm_set_ss(1.0f)); + v = _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1)); + _mm_store_ps(dst, v); + dst += 4; + src += stride; + } + } + else + { + while (dst < end) + { + __m128 v = _mm_load_ps((const float *)src); + v = _mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)); + v = _mm_move_ss(v, _mm_set_ss(1.0f)); + v = _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1)); + _mm_store_ps(dst, v); + dst += 4; + src += stride; + } + } +} + +static void DPSOFTRAST_Load2fTo4f(float *dst, const unsigned char *src, int size, int stride) +{ + float *end = dst + size*4; + __m128 v2 = _mm_setr_ps(0.0f, 0.0f, 0.0f, 1.0f); + if (stride == sizeof(float[2])) + { + float *end2 = dst + (size&~1)*4; + if (((size_t)src)&(ALIGN_SIZE - 1)) // check for alignment + { + while (dst < end2) + { + __m128 v = _mm_loadu_ps((const float *)src); + _mm_store_ps(dst, _mm_shuffle_ps(v, v2, _MM_SHUFFLE(3, 2, 1, 0))); + _mm_store_ps(dst + 4, _mm_movehl_ps(v2, v)); + dst += 8; + src += 2*sizeof(float[2]); + } + } + else + { + while (dst < end2) + { + __m128 v = _mm_load_ps((const float *)src); + _mm_store_ps(dst, _mm_shuffle_ps(v, v2, _MM_SHUFFLE(3, 2, 1, 0))); + _mm_store_ps(dst + 4, _mm_movehl_ps(v2, v)); + dst += 8; + src += 2*sizeof(float[2]); + } + } + } + while (dst < end) + { + _mm_store_ps(dst, _mm_loadl_pi(v2, (__m64 *)src)); + dst += 4; + src += stride; + } +} + +static void DPSOFTRAST_Load4bTo4f(float *dst, const unsigned char *src, int size, int stride) +{ + float *end = dst + size*4; + __m128 scale = _mm_set1_ps(1.0f/255.0f); + if (stride == sizeof(unsigned char[4])) + { + float *end4 = dst + (size&~3)*4; + if (((size_t)src)&(ALIGN_SIZE - 1)) // check for alignment + { + while (dst < end4) + { + __m128i v = _mm_loadu_si128((const __m128i *)src), v1 = _mm_unpacklo_epi8(v, _mm_setzero_si128()), v2 = _mm_unpackhi_epi8(v, _mm_setzero_si128()); + _mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(v1, _mm_setzero_si128())), scale)); + _mm_store_ps(dst + 4, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(v1, _mm_setzero_si128())), scale)); + _mm_store_ps(dst + 8, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(v2, _mm_setzero_si128())), scale)); + _mm_store_ps(dst + 12, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(v2, _mm_setzero_si128())), scale)); + dst += 16; + src += 4*sizeof(unsigned char[4]); + } } else { - dpsoftrast.uniform4f[index*4+0] = v[0]; - dpsoftrast.uniform4f[index*4+1] = v[1]; - dpsoftrast.uniform4f[index*4+2] = v[2]; - dpsoftrast.uniform4f[index*4+3] = v[3]; - dpsoftrast.uniform4f[index*4+4] = v[4]; - dpsoftrast.uniform4f[index*4+5] = v[5]; - dpsoftrast.uniform4f[index*4+6] = v[6]; - dpsoftrast.uniform4f[index*4+7] = v[7]; - dpsoftrast.uniform4f[index*4+8] = v[8]; - dpsoftrast.uniform4f[index*4+9] = v[9]; - dpsoftrast.uniform4f[index*4+10] = v[10]; - dpsoftrast.uniform4f[index*4+11] = v[11]; - dpsoftrast.uniform4f[index*4+12] = v[12]; - dpsoftrast.uniform4f[index*4+13] = v[13]; - dpsoftrast.uniform4f[index*4+14] = v[14]; - dpsoftrast.uniform4f[index*4+15] = v[15]; + while (dst < end4) + { + __m128i v = _mm_load_si128((const __m128i *)src), v1 = _mm_unpacklo_epi8(v, _mm_setzero_si128()), v2 = _mm_unpackhi_epi8(v, _mm_setzero_si128()); + _mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(v1, _mm_setzero_si128())), scale)); + _mm_store_ps(dst + 4, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(v1, _mm_setzero_si128())), scale)); + _mm_store_ps(dst + 8, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(v2, _mm_setzero_si128())), scale)); + _mm_store_ps(dst + 12, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(v2, _mm_setzero_si128())), scale)); + dst += 16; + src += 4*sizeof(unsigned char[4]); + } } } + while (dst < end) + { + __m128i v = _mm_cvtsi32_si128(*(const int *)src); + _mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(_mm_unpacklo_epi8(v, _mm_setzero_si128()), _mm_setzero_si128())), scale)); + dst += 4; + src += stride; + } } -void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0) + +static void DPSOFTRAST_Fill4f(float *dst, const float *src, int size) { - dpsoftrast.uniform1i[index] = i0; + float *end = dst + 4*size; + __m128 v = _mm_loadu_ps(src); + while (dst < end) + { + _mm_store_ps(dst, v); + dst += 4; + } } +#endif void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors) { +#ifdef SSE2_PRESENT int i; int j; int stride; @@ -971,79 +1624,47 @@ void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcol float *p; float *data; const unsigned char *b; - dpsoftrast.draw.numvertices = numvertices; - if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices) - { - if (dpsoftrast.draw.maxvertices < 4096) - dpsoftrast.draw.maxvertices = 4096; - while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices) - dpsoftrast.draw.maxvertices *= 2; - if (dpsoftrast.draw.in_array4f[0]) - MM_FREE(dpsoftrast.draw.in_array4f[0]); - data = (float *)MM_CALLOC(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1)); - for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4) - dpsoftrast.draw.in_array4f[i] = data; - for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4) - dpsoftrast.draw.post_array4f[i] = data; - dpsoftrast.draw.screencoord4f = data; - data += dpsoftrast.draw.maxvertices * 4; + dpsoftrast.numvertices = numvertices; + if (dpsoftrast.maxvertices < dpsoftrast.numvertices) + { + if (dpsoftrast.maxvertices < 4096) + dpsoftrast.maxvertices = 4096; + while (dpsoftrast.maxvertices < dpsoftrast.numvertices) + dpsoftrast.maxvertices *= 2; + if (dpsoftrast.in_array4f[0]) + MM_FREE(dpsoftrast.in_array4f[0]); + data = (float *)MM_CALLOC(1, dpsoftrast.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1)); + for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.maxvertices * 4) + dpsoftrast.in_array4f[i] = data; + for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.maxvertices * 4) + dpsoftrast.post_array4f[i] = data; + dpsoftrast.screencoord4f = data; + data += dpsoftrast.maxvertices * 4; } stride = dpsoftrast.stride_vertex; v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride); - p = dpsoftrast.draw.in_array4f[0]; - for (i = 0;i < numvertices;i++) - { - p[0] = v[0]; - p[1] = v[1]; - p[2] = v[2]; - p[3] = 1.0f; - p += 4; - v = (const float *)((const unsigned char *)v + stride); - } + p = dpsoftrast.in_array4f[0]; + DPSOFTRAST_Load3fTo4f(p, (const unsigned char *)v, numvertices, stride); if (needcolors) { if (dpsoftrast.pointer_color4f) { stride = dpsoftrast.stride_color; v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride); - p = dpsoftrast.draw.in_array4f[1]; - for (i = 0;i < numvertices;i++) - { - p[0] = v[0]; - p[1] = v[1]; - p[2] = v[2]; - p[3] = v[3]; - p += 4; - v = (const float *)((const unsigned char *)v + stride); - } + p = dpsoftrast.in_array4f[1]; + DPSOFTRAST_Load4fTo4f(p, (const unsigned char *)v, numvertices, stride); } else if (dpsoftrast.pointer_color4ub) { stride = dpsoftrast.stride_color; b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride); - p = dpsoftrast.draw.in_array4f[1]; - for (i = 0;i < numvertices;i++) - { - p[0] = b[0] * (1.0f / 255.0f); - p[1] = b[1] * (1.0f / 255.0f); - p[2] = b[2] * (1.0f / 255.0f); - p[3] = b[3] * (1.0f / 255.0f); - p += 4; - b = (const unsigned char *)((const unsigned char *)b + stride); - } + p = dpsoftrast.in_array4f[1]; + DPSOFTRAST_Load4bTo4f(p, b, numvertices, stride); } else { - v = dpsoftrast.user.color; - p = dpsoftrast.draw.in_array4f[1]; - for (i = 0;i < numvertices;i++) - { - p[0] = v[0]; - p[1] = v[1]; - p[2] = v[2]; - p[3] = v[3]; - p += 4; - } + p = dpsoftrast.in_array4f[1]; + DPSOFTRAST_Fill4f(p, dpsoftrast.color, numvertices); } } for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL-2;j++) @@ -1052,67 +1673,69 @@ void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcol { stride = dpsoftrast.stride_texcoord[j]; v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride); - p = dpsoftrast.draw.in_array4f[j+2]; + p = dpsoftrast.in_array4f[j+2]; switch(dpsoftrast.components_texcoord[j]) { case 2: - for (i = 0;i < numvertices;i++) - { - p[0] = v[0]; - p[1] = v[1]; - p[2] = 0.0f; - p[3] = 1.0f; - p += 4; - v = (const float *)((const unsigned char *)v + stride); - } + DPSOFTRAST_Load2fTo4f(p, (const unsigned char *)v, numvertices, stride); break; case 3: - for (i = 0;i < numvertices;i++) - { - p[0] = v[0]; - p[1] = v[1]; - p[2] = v[2]; - p[3] = 1.0f; - p += 4; - v = (const float *)((const unsigned char *)v + stride); - } + DPSOFTRAST_Load3fTo4f(p, (const unsigned char *)v, numvertices, stride); break; case 4: - for (i = 0;i < numvertices;i++) - { - p[0] = v[0]; - p[1] = v[1]; - p[2] = v[2]; - p[3] = v[3]; - p += 4; - v = (const float *)((const unsigned char *)v + stride); - } + DPSOFTRAST_Load4fTo4f(p, (const unsigned char *)v, numvertices, stride); break; } } } +#endif } void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f) { +#ifdef SSE2_PRESENT static const float identitymatrix[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}; - // TODO: SIMD - float matrix[4][4]; - int i; - memcpy(matrix, inmatrix16f, sizeof(float[16])); - if (!memcmp(identitymatrix, matrix, sizeof(float[16]))) + __m128 m0, m1, m2, m3; + float *end = out4f + numitems*4; + if (!memcmp(identitymatrix, inmatrix16f, sizeof(float[16]))) { // fast case for identity matrix memcpy(out4f, in4f, numitems * sizeof(float[4])); return; } - for (i = 0;i < numitems;i++, out4f += 4, in4f += 4) + m0 = _mm_loadu_ps(inmatrix16f); + m1 = _mm_loadu_ps(inmatrix16f + 4); + m2 = _mm_loadu_ps(inmatrix16f + 8); + m3 = _mm_loadu_ps(inmatrix16f + 12); + if (((size_t)in4f)&(ALIGN_SIZE-1)) // check alignment { - out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0]; - out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1]; - out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2]; - out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3]; + while (out4f < end) + { + __m128 v = _mm_loadu_ps(in4f); + _mm_store_ps(out4f, + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, 0)), m0), + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(1, 1, 1, 1)), m1), + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 2, 2, 2)), m2), + _mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3)), m3))))); + out4f += 4; + in4f += 4; + } } + else + { + while (out4f < end) + { + __m128 v = _mm_load_ps(in4f); + _mm_store_ps(out4f, + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, 0)), m0), + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(1, 1, 1, 1)), m1), + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 2, 2, 2)), m2), + _mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3)), m3))))); + out4f += 4; + in4f += 4; + } + } +#endif } void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems) @@ -1125,79 +1748,101 @@ static __m128 DPSOFTRAST_Draw_ProjectVertex(__m128 v) { __m128 viewportcenter = _mm_load_ps(dpsoftrast.fb_viewportcenter), viewportscale = _mm_load_ps(dpsoftrast.fb_viewportscale); __m128 w = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3)); - v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set1_ps(1.0f)); + v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set_ss(1.0f)); v = _mm_add_ps(viewportcenter, _mm_div_ps(_mm_mul_ps(viewportscale, v), w)); v = _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1)); return v; } #endif -void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems) +void DPSOFTRAST_Array_Project(float *out4f, float *screen4f, const float *in4f, int numitems) { #ifdef SSE2_PRESENT - // NOTE: this is used both as a whole mesh transform function and a - // per-triangle transform function (for clipped triangles), accordingly - // it should not crash on divide by 0 but the result of divide by 0 is - // unimportant... - // TODO: SIMD - int i; + float *end = out4f + numitems*4; __m128 viewportcenter = _mm_load_ps(dpsoftrast.fb_viewportcenter), viewportscale = _mm_load_ps(dpsoftrast.fb_viewportscale); - for (i = 0;i < numitems;i++) + while (out4f < end) { __m128 v = _mm_load_ps(in4f), w = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3)); - v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set1_ps(1.0f)); + _mm_store_ps(out4f, v); + v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set_ss(1.0f)); v = _mm_add_ps(viewportcenter, _mm_div_ps(_mm_mul_ps(viewportscale, v), w)); - _mm_store_ps(out4f, _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1))); + _mm_store_ps(screen4f, _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1))); in4f += 4; out4f += 4; + screen4f += 4; } #endif } -void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1) +void DPSOFTRAST_Array_TransformProject(float *out4f, float *screen4f, const float *in4f, int numitems, const float *inmatrix16f) { - int i; - int x; - int y; - int w = dpsoftrast.fb_width; - int bounds[4]; - float v0[2], v1[2]; - unsigned int *pixels = dpsoftrast.fb_colorpixels[0]; - //const float *c4f; - bounds[0] = dpsoftrast.fb_viewportscissor[0]; - bounds[1] = dpsoftrast.fb_viewportscissor[1]; - bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2]; - bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3]; - v0[0] = screen0[0]; - v0[1] = screen0[1]; - v1[0] = screen1[0]; - v1[1] = screen1[1]; - for (i = 0;i <= 128;i++) - { - // check nearclip - //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f) - // continue; - x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f)); - y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f)); - if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3]) - continue; - //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4; - //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]); - pixels[y*w+x] = 0xFFFFFFFF; +#ifdef SSE2_PRESENT + 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, viewportcenter, viewportscale; + float *end = out4f + numitems*4; + if (!memcmp(identitymatrix, inmatrix16f, sizeof(float[16]))) + { + DPSOFTRAST_Array_Project(out4f, screen4f, in4f, numitems); + return; + } + viewportcenter = _mm_load_ps(dpsoftrast.fb_viewportcenter); + viewportscale = _mm_load_ps(dpsoftrast.fb_viewportscale); + m0 = _mm_loadu_ps(inmatrix16f); + m1 = _mm_loadu_ps(inmatrix16f + 4); + m2 = _mm_loadu_ps(inmatrix16f + 8); + m3 = _mm_loadu_ps(inmatrix16f + 12); + if (((size_t)in4f)&(ALIGN_SIZE-1)) // check alignment + { + while (out4f < end) + { + __m128 v = _mm_loadu_ps(in4f), w; + v = _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, 0)), m0), + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(1, 1, 1, 1)), m1), + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 2, 2, 2)), m2), + _mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3)), m3)))); + _mm_store_ps(out4f, v); + w = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3)); + v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set_ss(1.0f)); + v = _mm_add_ps(viewportcenter, _mm_div_ps(_mm_mul_ps(viewportscale, v), w)); + _mm_store_ps(screen4f, _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1))); + in4f += 4; + out4f += 4; + screen4f += 4; + } + } + else + { + while (out4f < end) + { + __m128 v = _mm_load_ps(in4f), w; + v = _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, 0)), m0), + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(1, 1, 1, 1)), m1), + _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 2, 2, 2)), m2), + _mm_mul_ps(_mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3)), m3)))); + _mm_store_ps(out4f, v); + w = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3)); + v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set_ss(1.0f)); + v = _mm_add_ps(viewportcenter, _mm_div_ps(_mm_mul_ps(viewportscale, v), w)); + _mm_store_ps(screen4f, _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1))); + in4f += 4; + out4f += 4; + screen4f += 4; + } } +#endif } -void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *zf) +void DPSOFTRAST_Draw_Span_Begin(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float *zf) { int x; int startx = span->startx; int endx = span->endx; - float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3]; - float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3]; + float wslope = triangle->w[0]; + float w = triangle->w[2] + span->x*wslope + span->y*triangle->w[1]; float endz = 1.0f / (w + wslope * startx); for (x = startx;x < endx;) { - int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1; + int nextsub = x + DPSOFTRAST_DRAW_MAXSUBSPAN, endsub = nextsub - 1; float z = endz, dz; if(nextsub >= endx) nextsub = endsub = endx-1; endz = 1.0f / (w + wslope * nextsub); @@ -1207,7 +1852,7 @@ void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span * RESTRICT span } } -void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const float * RESTRICT in4f) +void DPSOFTRAST_Draw_Span_Finish(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, const float * RESTRICT in4f) { int x; int startx = span->startx; @@ -1218,14 +1863,14 @@ void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span * RESTRICT spa unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0]; if (!pixel) return; - pixel += span->start * 4; + pixel += (span->y * dpsoftrast.fb_width + span->x) * 4; // handle alphatest now (this affects depth writes too) - if (dpsoftrast.user.alphatest) + if (thread->alphatest) for (x = startx;x < endx;x++) if (in4f[x*4+3] < 0.5f) pixelmask[x] = false; // FIXME: this does not handle bigendian - switch(dpsoftrast.fb_blendmode) + switch(thread->fb_blendmode) { case DPSOFTRAST_BLENDMODE_OPAQUE: for (x = startx;x < endx;x++) @@ -1371,7 +2016,7 @@ void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span * RESTRICT spa } } -void DPSOFTRAST_Draw_Span_FinishBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const unsigned char* RESTRICT in4ub) +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 int x; @@ -1383,15 +2028,15 @@ void DPSOFTRAST_Draw_Span_FinishBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRIC unsigned int * RESTRICT pixeli = (unsigned int *)dpsoftrast.fb_colorpixels[0]; if (!pixel) return; - pixel += span->start * 4; - pixeli += span->start; + pixel += (span->y * dpsoftrast.fb_width + span->x) * 4; + pixeli += span->y * dpsoftrast.fb_width + span->x; // handle alphatest now (this affects depth writes too) - if (dpsoftrast.user.alphatest) + if (thread->alphatest) for (x = startx;x < endx;x++) if (in4ub[x*4+3] < 0.5f) pixelmask[x] = false; // FIXME: this does not handle bigendian - switch(dpsoftrast.fb_blendmode) + switch(thread->fb_blendmode) { case DPSOFTRAST_BLENDMODE_OPAQUE: for (x = startx;x + 4 <= endx;) @@ -1506,7 +2151,7 @@ void DPSOFTRAST_Draw_Span_FinishBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRIC #endif } -void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float * RESTRICT out4f, int texunitindex, int arrayindex, const float * RESTRICT zf) +void DPSOFTRAST_Draw_Span_Texture2DVarying(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float * RESTRICT out4f, int texunitindex, int arrayindex, const float * RESTRICT zf) { int x; int startx = span->startx; @@ -1527,7 +2172,7 @@ void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RE int mip; const unsigned char * RESTRICT pixelbase; const unsigned char * RESTRICT pixel[4]; - DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex]; + DPSOFTRAST_Texture *texture = thread->texbound[texunitindex]; // if no texture is bound, just fill it with white if (!texture) { @@ -1540,7 +2185,7 @@ void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RE } return; } - mip = span->mip[texunitindex]; + mip = triangle->mip[texunitindex]; pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0]; // if this mipmap of the texture is 1 pixel, just fill it with that color if (texture->mipmap[mip][1] == 4) @@ -1559,14 +2204,7 @@ void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RE return; } filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR; - data[0] = span->data[0][arrayindex][0]; - data[1] = span->data[0][arrayindex][1]; - data[2] = span->data[0][arrayindex][2]; - data[3] = span->data[0][arrayindex][3]; - slope[0] = span->data[1][arrayindex][0]; - slope[1] = span->data[1][arrayindex][1]; - slope[2] = span->data[1][arrayindex][2]; - slope[3] = span->data[1][arrayindex][3]; + DPSOFTRAST_CALCATTRIB4F(triangle, span, data, slope, arrayindex); flags = texture->flags; tcscale[0] = texture->mipmap[mip][2]; tcscale[1] = texture->mipmap[mip][3]; @@ -1583,8 +2221,8 @@ void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RE { unsigned int subtc[2]; unsigned int substep[2]; - float subscale = 65536.0f/DPSOFTRAST_MAXSUBSPAN; - int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1; + float subscale = 65536.0f/DPSOFTRAST_DRAW_MAXSUBSPAN; + int nextsub = x + DPSOFTRAST_DRAW_MAXSUBSPAN, endsub = nextsub - 1; if(nextsub >= endx) { nextsub = endsub = endx-1; @@ -1700,7 +2338,7 @@ void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RE } } -void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf) +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 int x; @@ -1715,14 +2353,14 @@ void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span int mip; unsigned int *outi = (unsigned int *)out4ub; const unsigned char * RESTRICT pixelbase; - DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex]; + DPSOFTRAST_Texture *texture = thread->texbound[texunitindex]; // if no texture is bound, just fill it with white if (!texture) { memset(out4ub + startx*4, 255, span->length*4); return; } - mip = span->mip[texunitindex]; + mip = triangle->mip[texunitindex]; pixelbase = (const unsigned char *)texture->bytes + texture->mipmap[mip][0]; // if this mipmap of the texture is 1 pixel, just fill it with that color if (texture->mipmap[mip][1] == 4) @@ -1733,22 +2371,21 @@ void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span return; } filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR; - data = _mm_load_ps(span->data[0][arrayindex]); - slope = _mm_load_ps(span->data[1][arrayindex]); + DPSOFTRAST_CALCATTRIB(triangle, span, data, slope, arrayindex); flags = texture->flags; tcsize = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i *)&texture->mipmap[mip][0]), _MM_SHUFFLE(3, 2, 3, 2)); tcmask = _mm_sub_epi32(tcsize, _mm_set1_epi32(1)); tcscale = _mm_cvtepi32_ps(tcsize); - data = _mm_mul_ps(_mm_shuffle_ps(data, data, _MM_SHUFFLE(1, 0, 1, 0)), tcscale); - slope = _mm_mul_ps(_mm_shuffle_ps(slope, slope, _MM_SHUFFLE(1, 0, 1, 0)), tcscale); + data = _mm_mul_ps(_mm_movelh_ps(data, data), tcscale); + slope = _mm_mul_ps(_mm_movelh_ps(slope, slope), tcscale); endtc = _mm_sub_ps(_mm_mul_ps(_mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx))), _mm_load1_ps(&zf[startx])), _mm_set1_ps(0.5f)); endsubtc = _mm_cvtps_epi32(_mm_mul_ps(endtc, _mm_set1_ps(65536.0f))); tcoffset = _mm_add_epi32(_mm_slli_epi32(_mm_shuffle_epi32(tcsize, _MM_SHUFFLE(0, 0, 0, 0)), 18), _mm_set1_epi32(4)); tcmax = filter ? _mm_packs_epi32(tcmask, tcmask) : _mm_slli_epi32(tcmask, 16); for (x = startx;x < endx;) { - int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1; - __m128 subscale = _mm_set1_ps(65536.0f/DPSOFTRAST_MAXSUBSPAN); + int nextsub = x + DPSOFTRAST_DRAW_MAXSUBSPAN, endsub = nextsub - 1; + __m128 subscale = _mm_set1_ps(65536.0f/DPSOFTRAST_DRAW_MAXSUBSPAN); if(nextsub >= endx) { nextsub = endsub = endx-1; @@ -1970,7 +2607,7 @@ void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span #endif } -void DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf) +void DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf) { // TODO: IMPLEMENT memset(out4ub, 255, span->length*4); @@ -1982,7 +2619,7 @@ float DPSOFTRAST_SampleShadowmap(const float *vector) return 1.0f; } -void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, int arrayindex, const float *zf) +void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float *out4f, const float *in4f, int arrayindex, const float *zf) { int x; int startx = span->startx; @@ -1991,14 +2628,7 @@ void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span * RES float data[4]; float slope[4]; float z; - data[0] = span->data[0][arrayindex][0]; - data[1] = span->data[0][arrayindex][1]; - data[2] = span->data[0][arrayindex][2]; - data[3] = span->data[0][arrayindex][3]; - slope[0] = span->data[1][arrayindex][0]; - slope[1] = span->data[1][arrayindex][1]; - slope[2] = span->data[1][arrayindex][2]; - slope[3] = span->data[1][arrayindex][3]; + DPSOFTRAST_CALCATTRIB4F(triangle, span, data, slope, arrayindex); for (x = startx;x < endx;x++) { z = zf[x]; @@ -2013,7 +2643,7 @@ void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span * RES } } -void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, int arrayindex, const float *zf) +void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float *out4f, int arrayindex, const float *zf) { int x; int startx = span->startx; @@ -2022,14 +2652,7 @@ void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span * RESTRICT sp float data[4]; float slope[4]; float z; - data[0] = span->data[0][arrayindex][0]; - data[1] = span->data[0][arrayindex][1]; - data[2] = span->data[0][arrayindex][2]; - data[3] = span->data[0][arrayindex][3]; - slope[0] = span->data[1][arrayindex][0]; - slope[1] = span->data[1][arrayindex][1]; - slope[2] = span->data[1][arrayindex][2]; - slope[3] = span->data[1][arrayindex][3]; + DPSOFTRAST_CALCATTRIB4F(triangle, span, data, slope, arrayindex); for (x = startx;x < endx;x++) { z = zf[x]; @@ -2044,7 +2667,7 @@ void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span * RESTRICT sp } } -void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor) +void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor) { int x, startx = span->startx, endx = span->endx; float c[4], localcolor[4]; @@ -2065,7 +2688,7 @@ void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span * RESTRICT s } } -void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f) +void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f) { int x, startx = span->startx, endx = span->endx; for (x = startx;x < endx;x++) @@ -2077,7 +2700,7 @@ void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Draw_Span * RES } } -void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f) +void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f) { int x, startx = span->startx, endx = span->endx; for (x = startx;x < endx;x++) @@ -2089,7 +2712,7 @@ void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT } } -void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f) +void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f) { int x, startx = span->startx, endx = span->endx; float a, b; @@ -2104,7 +2727,7 @@ void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT } } -void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, const float *color) +void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span, float *out4f, const float *in4f, const float *color) { int x, startx = span->startx, endx = span->endx; float localcolor[4], ilerp, lerp; @@ -2125,13 +2748,14 @@ void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span * RES -void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, int arrayindex, const float *zf) +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 int x; int startx = span->startx; int endx = span->endx; - __m128 data = _mm_load_ps(span->data[0][arrayindex]), slope = _mm_load_ps(span->data[1][arrayindex]); + __m128 data, slope; + DPSOFTRAST_CALCATTRIB(triangle, span, data, slope, arrayindex); data = _mm_shuffle_ps(data, data, _MM_SHUFFLE(3, 0, 1, 2)); slope = _mm_shuffle_ps(slope, slope, _MM_SHUFFLE(3, 0, 1, 2)); data = _mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx))); @@ -2158,13 +2782,14 @@ void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Draw_Span #endif } -void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, int arrayindex, const float *zf) +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 int x; int startx = span->startx; int endx = span->endx; - __m128 data = _mm_load_ps(span->data[0][arrayindex]), slope = _mm_load_ps(span->data[1][arrayindex]); + __m128 data, slope; + DPSOFTRAST_CALCATTRIB(triangle, span, data, slope, arrayindex); data = _mm_shuffle_ps(data, data, _MM_SHUFFLE(3, 0, 1, 2)); slope = _mm_shuffle_ps(slope, slope, _MM_SHUFFLE(3, 0, 1, 2)); data = _mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx))); @@ -2187,7 +2812,7 @@ void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRI #endif } -void DPSOFTRAST_Draw_Span_AddBloomBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub, const float *subcolor) +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 int x, startx = span->startx, endx = span->endx; @@ -2210,7 +2835,7 @@ void DPSOFTRAST_Draw_Span_AddBloomBGRA8(const DPSOFTRAST_State_Draw_Span * RESTR #endif } -void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub) +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 int x, startx = span->startx, endx = span->endx; @@ -2231,7 +2856,7 @@ void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Draw_Span #endif } -void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub) +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 int x, startx = span->startx, endx = span->endx; @@ -2252,7 +2877,7 @@ void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RES #endif } -void DPSOFTRAST_Draw_Span_TintedAddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub, const float *inbtintbgra) +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 int x, startx = span->startx, endx = span->endx; @@ -2275,7 +2900,7 @@ void DPSOFTRAST_Draw_Span_TintedAddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span #endif } -void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub) +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 int x, startx = span->startx, endx = span->endx; @@ -2298,7 +2923,7 @@ void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RES #endif } -void DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, const float *color) +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 int x, startx = span->startx, endx = span->endx; @@ -2324,121 +2949,121 @@ void DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(const DPSOFTRAST_State_Draw_Span void DPSOFTRAST_VertexShader_Generic(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); - DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices); - DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Copy(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.numvertices); + DPSOFTRAST_Array_Copy(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.numvertices); if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR) - DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices); + DPSOFTRAST_Array_Copy(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.numvertices); } -void DPSOFTRAST_PixelShader_Generic(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_Generic(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE) + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); + if (thread->shader_permutation & SHADERPERMUTATION_DIFFUSE) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_FIRST, 2, buffer_z); - DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(span, buffer_FragColorbgra8, buffer_texture_colorbgra8, 1, buffer_z); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR) + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_colorbgra8, GL20TU_FIRST, 2, buffer_z); + DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(triangle, span, buffer_FragColorbgra8, buffer_texture_colorbgra8, 1, buffer_z); + if (thread->shader_permutation & SHADERPERMUTATION_SPECULAR) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_SECOND, 2, buffer_z); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING) + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_lightmapbgra8, GL20TU_SECOND, 2, buffer_z); + if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING) { // multiply - DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8); + DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(triangle, span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8); } - else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING) + else if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING) { // add - DPSOFTRAST_Draw_Span_AddBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8); + DPSOFTRAST_Draw_Span_AddBuffersBGRA8(triangle, span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8); } - else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) + else if (thread->shader_permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) { // alphablend - DPSOFTRAST_Draw_Span_MixBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8); + DPSOFTRAST_Draw_Span_MixBuffersBGRA8(triangle, span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8); } } } else - DPSOFTRAST_Draw_Span_VaryingBGRA8(span, buffer_FragColorbgra8, 1, buffer_z); - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_VaryingBGRA8(triangle, span, buffer_FragColorbgra8, 1, buffer_z); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_PostProcess(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); - DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices); - DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Copy(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.numvertices); + DPSOFTRAST_Array_Copy(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.numvertices); } -void DPSOFTRAST_PixelShader_PostProcess(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_PostProcess(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { // TODO: optimize!! at the very least there is no reason to use texture sampling on the frame texture float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_FragColorbgra8, GL20TU_FIRST, 2, buffer_z); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM) + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_FragColorbgra8, GL20TU_FIRST, 2, buffer_z); + if (thread->shader_permutation & SHADERPERMUTATION_BLOOM) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_SECOND, 3, buffer_z); - DPSOFTRAST_Draw_Span_AddBloomBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_colorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_colorbgra8, GL20TU_SECOND, 3, buffer_z); + DPSOFTRAST_Draw_Span_AddBloomBGRA8(triangle, span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_colorbgra8, thread->uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4); } - DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION) + DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(triangle, span, buffer_FragColorbgra8, buffer_FragColorbgra8, thread->uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4); + if (thread->shader_permutation & SHADERPERMUTATION_SATURATION) { // TODO: implement saturation } - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS) + if (thread->shader_permutation & SHADERPERMUTATION_GAMMARAMPS) { // TODO: implement gammaramps } - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_Depth_Or_Shadow(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); } -void DPSOFTRAST_PixelShader_Depth_Or_Shadow(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_Depth_Or_Shadow(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { // this is never called (because colormask is off when this shader is used) float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); memset(buffer_FragColorbgra8, 0, span->length*4); - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_FlatColor(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Transform(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); } -void DPSOFTRAST_PixelShader_FlatColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_FlatColor(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { int x, startx = span->startx, endx = span->endx; int Color_Ambienti[4]; float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - Color_Ambienti[2] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]*256.0f); - Color_Ambienti[1] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]*256.0f); - Color_Ambienti[0] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]*256.0f); - Color_Ambienti[3] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0] *256.0f); - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z); + Color_Ambienti[2] = (int)(thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]*256.0f); + Color_Ambienti[1] = (int)(thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]*256.0f); + Color_Ambienti[0] = (int)(thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]*256.0f); + Color_Ambienti[3] = (int)(thread->uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0] *256.0f); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z); for (x = startx;x < endx;x++) { buffer_FragColorbgra8[x*4+0] = (buffer_texture_colorbgra8[x*4+0] * Color_Ambienti[0])>>8; @@ -2446,23 +3071,23 @@ void DPSOFTRAST_PixelShader_FlatColor(const DPSOFTRAST_State_Draw_Span * RESTRIC buffer_FragColorbgra8[x*4+2] = (buffer_texture_colorbgra8[x*4+2] * Color_Ambienti[2])>>8; buffer_FragColorbgra8[x*4+3] = (buffer_texture_colorbgra8[x*4+3] * Color_Ambienti[3])>>8; } - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_VertexColor(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); - DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices); - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Copy(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.numvertices); + DPSOFTRAST_Array_Transform(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); } -void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_VertexColor(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { #ifdef SSE2_PRESENT unsigned char * RESTRICT pixelmask = span->pixelmask; - unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + span->start * 4; + 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; __m128i Color_Ambientm, Color_Diffusem; __m128 data, slope; @@ -2470,19 +3095,18 @@ void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTR unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; int arrayindex = DPSOFTRAST_ARRAY_COLOR; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z); - if (dpsoftrast.user.alphatest || dpsoftrast.fb_blendmode != DPSOFTRAST_BLENDMODE_OPAQUE) + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z); + if (thread->alphatest || thread->fb_blendmode != DPSOFTRAST_BLENDMODE_OPAQUE) pixel = buffer_FragColorbgra8; - Color_Ambientm = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2)); + Color_Ambientm = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2)); Color_Ambientm = _mm_and_si128(Color_Ambientm, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0)); - Color_Ambientm = _mm_or_si128(Color_Ambientm, _mm_setr_epi32(0, 0, 0, (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]*255.0f))); + Color_Ambientm = _mm_or_si128(Color_Ambientm, _mm_setr_epi32(0, 0, 0, (int)(thread->uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]*255.0f))); Color_Ambientm = _mm_packs_epi32(Color_Ambientm, Color_Ambientm); - Color_Diffusem = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4]), _mm_set1_ps(4096.0f))), _MM_SHUFFLE(3, 0, 1, 2)); + Color_Diffusem = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4]), _mm_set1_ps(4096.0f))), _MM_SHUFFLE(3, 0, 1, 2)); Color_Diffusem = _mm_and_si128(Color_Diffusem, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0)); Color_Diffusem = _mm_packs_epi32(Color_Diffusem, Color_Diffusem); - data = _mm_load_ps(span->data[0][arrayindex]); - slope = _mm_load_ps(span->data[1][arrayindex]); + DPSOFTRAST_CALCATTRIB(triangle, span, data, slope, arrayindex); data = _mm_shuffle_ps(data, data, _MM_SHUFFLE(3, 0, 1, 2)); slope = _mm_shuffle_ps(slope, slope, _MM_SHUFFLE(3, 0, 1, 2)); data = _mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx))); @@ -2520,7 +3144,7 @@ void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTR *(int *)&pixel[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix)); } if(pixel == buffer_FragColorbgra8) - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); #endif } @@ -2528,16 +3152,16 @@ void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTR void DPSOFTRAST_VertexShader_Lightmap(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); - DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Transform(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); + DPSOFTRAST_Array_Copy(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.numvertices); } -void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_Lightmap(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { #ifdef SSE2_PRESENT unsigned char * RESTRICT pixelmask = span->pixelmask; - unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + span->start * 4; + 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; __m128i Color_Ambientm, Color_Diffusem, Color_Glowm, Color_AmbientGlowm; float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; @@ -2545,22 +3169,22 @@ void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_texture_glowbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_LIGHTMAP, DPSOFTRAST_ARRAY_TEXCOORD4, buffer_z); - if (dpsoftrast.user.alphatest || dpsoftrast.fb_blendmode != DPSOFTRAST_BLENDMODE_OPAQUE) + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_lightmapbgra8, GL20TU_LIGHTMAP, DPSOFTRAST_ARRAY_TEXCOORD4, buffer_z); + if (thread->alphatest || thread->fb_blendmode != DPSOFTRAST_BLENDMODE_OPAQUE) pixel = buffer_FragColorbgra8; - Color_Ambientm = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2)); + Color_Ambientm = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2)); Color_Ambientm = _mm_and_si128(Color_Ambientm, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0)); - Color_Ambientm = _mm_or_si128(Color_Ambientm, _mm_setr_epi32(0, 0, 0, (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]*255.0f))); + Color_Ambientm = _mm_or_si128(Color_Ambientm, _mm_setr_epi32(0, 0, 0, (int)(thread->uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]*255.0f))); Color_Ambientm = _mm_packs_epi32(Color_Ambientm, Color_Ambientm); - Color_Diffusem = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2)); + Color_Diffusem = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2)); Color_Diffusem = _mm_and_si128(Color_Diffusem, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0)); Color_Diffusem = _mm_packs_epi32(Color_Diffusem, Color_Diffusem); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW) + if (thread->shader_permutation & SHADERPERMUTATION_GLOW) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glowbgra8, GL20TU_GLOW, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); - Color_Glowm = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2)); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_glowbgra8, GL20TU_GLOW, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + Color_Glowm = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2)); Color_Glowm = _mm_and_si128(Color_Glowm, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0)); Color_Glowm = _mm_packs_epi32(Color_Glowm, Color_Glowm); Color_AmbientGlowm = _mm_unpacklo_epi64(Color_Ambientm, Color_Glowm); @@ -2620,7 +3244,7 @@ void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT } } if(pixel == buffer_FragColorbgra8) - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); #endif } @@ -2628,17 +3252,17 @@ void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT void DPSOFTRAST_VertexShader_FakeLight(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); } -void DPSOFTRAST_PixelShader_FakeLight(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_FakeLight(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { // TODO: IMPLEMENT float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); memset(buffer_FragColorbgra8, 0, span->length*4); - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } @@ -2648,9 +3272,9 @@ void DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace(void) DPSOFTRAST_VertexShader_Lightmap(); } -void DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { - DPSOFTRAST_PixelShader_Lightmap(span); + DPSOFTRAST_PixelShader_Lightmap(thread, triangle, span); // TODO: IMPLEMENT } @@ -2661,9 +3285,9 @@ void DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace(void) DPSOFTRAST_VertexShader_Lightmap(); } -void DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { - DPSOFTRAST_PixelShader_Lightmap(span); + DPSOFTRAST_PixelShader_Lightmap(thread, triangle, span); // TODO: IMPLEMENT } @@ -2672,7 +3296,7 @@ void DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace(const DPSOFTRAST_Stat void DPSOFTRAST_VertexShader_LightDirection(void) { int i; - int numvertices = dpsoftrast.draw.numvertices; + int numvertices = dpsoftrast.numvertices; float LightDir[4]; float LightVector[4]; float EyePosition[4]; @@ -2690,39 +3314,39 @@ void DPSOFTRAST_VertexShader_LightDirection(void) EyePosition[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+1]; EyePosition[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+2]; EyePosition[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+3]; - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Transform(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); for (i = 0;i < numvertices;i++) { - position[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0]; - position[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1]; - position[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2]; - svector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0]; - svector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1]; - svector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2]; - tvector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0]; - tvector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1]; - tvector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2]; - normal[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0]; - normal[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1]; - normal[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2]; + position[0] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0]; + position[1] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1]; + position[2] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2]; + svector[0] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0]; + svector[1] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1]; + svector[2] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2]; + tvector[0] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0]; + tvector[1] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1]; + tvector[2] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2]; + normal[0] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0]; + normal[1] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1]; + normal[2] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2]; LightVector[0] = svector[0] * LightDir[0] + svector[1] * LightDir[1] + svector[2] * LightDir[2]; LightVector[1] = tvector[0] * LightDir[0] + tvector[1] * LightDir[1] + tvector[2] * LightDir[2]; LightVector[2] = normal[0] * LightDir[0] + normal[1] * LightDir[1] + normal[2] * LightDir[2]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0] = LightVector[0]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1] = LightVector[1]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2] = LightVector[2]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+3] = 0.0f; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0] = LightVector[0]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1] = LightVector[1]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2] = LightVector[2]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+3] = 0.0f; EyeVectorModelSpace[0] = EyePosition[0] - position[0]; EyeVectorModelSpace[1] = EyePosition[1] - position[1]; EyeVectorModelSpace[2] = EyePosition[2] - position[2]; EyeVector[0] = svector[0] * EyeVectorModelSpace[0] + svector[1] * EyeVectorModelSpace[1] + svector[2] * EyeVectorModelSpace[2]; EyeVector[1] = tvector[0] * EyeVectorModelSpace[0] + tvector[1] * EyeVectorModelSpace[1] + tvector[2] * EyeVectorModelSpace[2]; EyeVector[2] = normal[0] * EyeVectorModelSpace[0] + normal[1] * EyeVectorModelSpace[1] + normal[2] * EyeVectorModelSpace[2]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0] = EyeVector[0]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1] = EyeVector[1]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2] = EyeVector[2]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+3] = 0.0f; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0] = EyeVector[0]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1] = EyeVector[1]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2] = EyeVector[2]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+3] = 0.0f; } } @@ -2745,7 +3369,7 @@ do\ }\ while(0) -void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_LightDirection(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; @@ -2772,66 +3396,52 @@ void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RE float specular; float SpecularPower; int d[4]; - Color_Glow[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0]; - Color_Glow[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1]; - Color_Glow[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2]; + Color_Glow[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0]; + Color_Glow[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1]; + Color_Glow[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2]; Color_Glow[3] = 0.0f; - Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]; - Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]; - Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]; - Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]; - Color_Pants[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0]; - Color_Pants[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1]; - Color_Pants[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2]; + Color_Ambient[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]; + Color_Ambient[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]; + Color_Ambient[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]; + Color_Ambient[3] = thread->uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]; + Color_Pants[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0]; + Color_Pants[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1]; + Color_Pants[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2]; Color_Pants[3] = 0.0f; - Color_Shirt[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0]; - Color_Shirt[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1]; - Color_Shirt[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2]; + Color_Shirt[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0]; + Color_Shirt[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1]; + Color_Shirt[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2]; Color_Shirt[3] = 0.0f; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING) + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); } - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW) + if (thread->shader_permutation & SHADERPERMUTATION_GLOW) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glowbgra8, GL20TU_GLOW, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_glowbgra8, GL20TU_GLOW, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); } - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR) + if (thread->shader_permutation & SHADERPERMUTATION_SPECULAR) { - Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0]; - Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1]; - Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2]; + Color_Diffuse[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0]; + Color_Diffuse[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1]; + Color_Diffuse[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2]; Color_Diffuse[3] = 0.0f; - LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0]; - LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1]; - LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2]; + LightColor[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0]; + LightColor[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1]; + LightColor[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2]; LightColor[3] = 0.0f; - LightVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0]; - LightVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1]; - LightVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2]; - LightVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3]; - LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0]; - LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1]; - LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2]; - LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3]; - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); - Color_Specular[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0]; - Color_Specular[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1]; - Color_Specular[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2]; + DPSOFTRAST_CALCATTRIB4F(triangle, span, LightVectordata, LightVectorslope, DPSOFTRAST_ARRAY_TEXCOORD1); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + Color_Specular[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0]; + Color_Specular[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1]; + Color_Specular[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2]; Color_Specular[3] = 0.0f; - SpecularPower = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f); - EyeVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][0]; - EyeVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][1]; - EyeVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][2]; - EyeVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][3]; - EyeVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][0]; - EyeVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][1]; - EyeVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][2]; - EyeVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][3]; - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + SpecularPower = thread->uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f); + DPSOFTRAST_CALCATTRIB4F(triangle, span, EyeVectordata, EyeVectorslope, DPSOFTRAST_ARRAY_TEXCOORD2); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); for (x = startx;x < endx;x++) { z = buffer_z[x]; @@ -2839,7 +3449,7 @@ void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RE diffusetex[1] = buffer_texture_colorbgra8[x*4+1]; diffusetex[2] = buffer_texture_colorbgra8[x*4+2]; diffusetex[3] = buffer_texture_colorbgra8[x*4+3]; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING) + if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING) { diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0]; diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1]; @@ -2873,7 +3483,7 @@ void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RE diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f; specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f; specular = pow(specular, SpecularPower * glosstex[3]); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW) + if (thread->shader_permutation & SHADERPERMUTATION_GLOW) { d[0] = (int)(buffer_texture_glowbgra8[x*4+0] * Color_Glow[0] + diffusetex[0] * Color_Ambient[0] + (diffusetex[0] * Color_Diffuse[0] * diffuse + glosstex[0] * Color_Specular[0] * specular) * LightColor[0]);if (d[0] > 255) d[0] = 255; d[1] = (int)(buffer_texture_glowbgra8[x*4+1] * Color_Glow[1] + diffusetex[1] * Color_Ambient[1] + (diffusetex[1] * Color_Diffuse[1] * diffuse + glosstex[1] * Color_Specular[1] * specular) * LightColor[1]);if (d[1] > 255) d[1] = 255; @@ -2893,25 +3503,18 @@ void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RE buffer_FragColorbgra8[x*4+3] = d[3]; } } - else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE) + else if (thread->shader_permutation & SHADERPERMUTATION_DIFFUSE) { - Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0]; - Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1]; - Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2]; + Color_Diffuse[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0]; + Color_Diffuse[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1]; + Color_Diffuse[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2]; Color_Diffuse[3] = 0.0f; - LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0]; - LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1]; - LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2]; + LightColor[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0]; + LightColor[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1]; + LightColor[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2]; LightColor[3] = 0.0f; - LightVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0]; - LightVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1]; - LightVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2]; - LightVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3]; - LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0]; - LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1]; - LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2]; - LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3]; - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_CALCATTRIB4F(triangle, span, LightVectordata, LightVectorslope, DPSOFTRAST_ARRAY_TEXCOORD1); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); for (x = startx;x < endx;x++) { z = buffer_z[x]; @@ -2930,7 +3533,7 @@ void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RE DPSOFTRAST_Vector3Normalize(lightnormal); diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW) + if (thread->shader_permutation & SHADERPERMUTATION_GLOW) { d[0] = (int)(buffer_texture_glowbgra8[x*4+0] * Color_Glow[0] + diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse * LightColor[0]));if (d[0] > 255) d[0] = 255; d[1] = (int)(buffer_texture_glowbgra8[x*4+1] * Color_Glow[1] + diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse * LightColor[1]));if (d[1] > 255) d[1] = 255; @@ -2960,7 +3563,7 @@ void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RE diffusetex[2] = buffer_texture_colorbgra8[x*4+2]; diffusetex[3] = buffer_texture_colorbgra8[x*4+3]; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW) + if (thread->shader_permutation & SHADERPERMUTATION_GLOW) { d[0] = (int)(buffer_texture_glowbgra8[x*4+0] * Color_Glow[0] + diffusetex[0] * Color_Ambient[0]);if (d[0] > 255) d[0] = 255; d[1] = (int)(buffer_texture_glowbgra8[x*4+1] * Color_Glow[1] + diffusetex[1] * Color_Ambient[1]);if (d[1] > 255) d[1] = 255; @@ -2980,7 +3583,7 @@ void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RE buffer_FragColorbgra8[x*4+3] = d[3]; } } - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } @@ -2988,7 +3591,7 @@ void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RE void DPSOFTRAST_VertexShader_LightSource(void) { int i; - int numvertices = dpsoftrast.draw.numvertices; + int numvertices = dpsoftrast.numvertices; float LightPosition[4]; float LightVector[4]; float LightVectorModelSpace[4]; @@ -3007,48 +3610,48 @@ void DPSOFTRAST_VertexShader_LightSource(void) EyePosition[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+1]; EyePosition[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+2]; EyePosition[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+3]; - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD3], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelToLightM1); - DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Transform(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); + DPSOFTRAST_Array_Transform(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD3], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelToLightM1); + DPSOFTRAST_Array_Copy(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.numvertices); for (i = 0;i < numvertices;i++) { - position[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0]; - position[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1]; - position[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2]; - svector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0]; - svector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1]; - svector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2]; - tvector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0]; - tvector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1]; - tvector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2]; - normal[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0]; - normal[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1]; - normal[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2]; + position[0] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0]; + position[1] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1]; + position[2] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2]; + svector[0] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0]; + svector[1] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1]; + svector[2] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2]; + tvector[0] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0]; + tvector[1] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1]; + tvector[2] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2]; + normal[0] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0]; + normal[1] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1]; + normal[2] = dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2]; LightVectorModelSpace[0] = LightPosition[0] - position[0]; LightVectorModelSpace[1] = LightPosition[1] - position[1]; LightVectorModelSpace[2] = LightPosition[2] - position[2]; LightVector[0] = svector[0] * LightVectorModelSpace[0] + svector[1] * LightVectorModelSpace[1] + svector[2] * LightVectorModelSpace[2]; LightVector[1] = tvector[0] * LightVectorModelSpace[0] + tvector[1] * LightVectorModelSpace[1] + tvector[2] * LightVectorModelSpace[2]; LightVector[2] = normal[0] * LightVectorModelSpace[0] + normal[1] * LightVectorModelSpace[1] + normal[2] * LightVectorModelSpace[2]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0] = LightVector[0]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1] = LightVector[1]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2] = LightVector[2]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+3] = 0.0f; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0] = LightVector[0]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1] = LightVector[1]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2] = LightVector[2]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+3] = 0.0f; EyeVectorModelSpace[0] = EyePosition[0] - position[0]; EyeVectorModelSpace[1] = EyePosition[1] - position[1]; EyeVectorModelSpace[2] = EyePosition[2] - position[2]; EyeVector[0] = svector[0] * EyeVectorModelSpace[0] + svector[1] * EyeVectorModelSpace[1] + svector[2] * EyeVectorModelSpace[2]; EyeVector[1] = tvector[0] * EyeVectorModelSpace[0] + tvector[1] * EyeVectorModelSpace[1] + tvector[2] * EyeVectorModelSpace[2]; EyeVector[2] = normal[0] * EyeVectorModelSpace[0] + normal[1] * EyeVectorModelSpace[1] + normal[2] * EyeVectorModelSpace[2]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0] = EyeVector[0]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1] = EyeVector[1]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2] = EyeVector[2]; - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+3] = 0.0f; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0] = EyeVector[0]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1] = EyeVector[1]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2] = EyeVector[2]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+3] = 0.0f; } } -void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_LightSource(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; @@ -3079,73 +3682,52 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR float CubeVector[4]; float attenuation; int d[4]; - Color_Glow[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0]; - Color_Glow[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1]; - Color_Glow[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2]; + Color_Glow[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0]; + Color_Glow[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1]; + Color_Glow[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2]; Color_Glow[3] = 0.0f; - Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]; - Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]; - Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]; - Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]; - Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0]; - Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1]; - Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2]; + Color_Ambient[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]; + Color_Ambient[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]; + Color_Ambient[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]; + Color_Ambient[3] = thread->uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]; + Color_Diffuse[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0]; + Color_Diffuse[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1]; + Color_Diffuse[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2]; Color_Diffuse[3] = 0.0f; - Color_Specular[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0]; - Color_Specular[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1]; - Color_Specular[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2]; + Color_Specular[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0]; + Color_Specular[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1]; + Color_Specular[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2]; Color_Specular[3] = 0.0f; - Color_Pants[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0]; - Color_Pants[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1]; - Color_Pants[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2]; + Color_Pants[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0]; + Color_Pants[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1]; + Color_Pants[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2]; Color_Pants[3] = 0.0f; - Color_Shirt[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0]; - Color_Shirt[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1]; - Color_Shirt[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2]; + Color_Shirt[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0]; + Color_Shirt[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1]; + Color_Shirt[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2]; Color_Shirt[3] = 0.0f; - LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0]; - LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1]; - LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2]; + LightColor[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0]; + LightColor[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1]; + LightColor[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2]; LightColor[3] = 0.0f; - SpecularPower = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f); - EyeVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][0]; - EyeVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][1]; - EyeVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][2]; - EyeVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][3]; - EyeVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][0]; - EyeVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][1]; - EyeVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][2]; - EyeVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][3]; - LightVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0]; - LightVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1]; - LightVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2]; - LightVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3]; - LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0]; - LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1]; - LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2]; - LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3]; - CubeVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][0]; - CubeVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][1]; - CubeVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][2]; - CubeVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][3]; - CubeVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][0]; - CubeVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][1]; - CubeVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][2]; - CubeVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][3]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); + SpecularPower = thread->uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f); + DPSOFTRAST_CALCATTRIB4F(triangle, span, LightVectordata, LightVectorslope, DPSOFTRAST_ARRAY_TEXCOORD1); + DPSOFTRAST_CALCATTRIB4F(triangle, span, EyeVectordata, EyeVectorslope, DPSOFTRAST_ARRAY_TEXCOORD2); + DPSOFTRAST_CALCATTRIB4F(triangle, span, CubeVectordata, CubeVectorslope, DPSOFTRAST_ARRAY_TEXCOORD3); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); memset(buffer_FragColorbgra8 + startx*4, 0, (endx-startx)*4); // clear first, because we skip writing black pixels, and there are a LOT of them... - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING) + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); } - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER) - DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(span, buffer_texture_cubebgra8, GL20TU_CUBE, DPSOFTRAST_ARRAY_TEXCOORD3, buffer_z); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR) + if (thread->shader_permutation & SHADERPERMUTATION_CUBEFILTER) + DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(triangle, span, buffer_texture_cubebgra8, GL20TU_CUBE, DPSOFTRAST_ARRAY_TEXCOORD3, buffer_z); + if (thread->shader_permutation & SHADERPERMUTATION_SPECULAR) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); for (x = startx;x < endx;x++) { z = buffer_z[x]; @@ -3155,7 +3737,7 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector); if (attenuation < 0.01f) continue; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D) + if (thread->shader_permutation & SHADERPERMUTATION_SHADOWMAP2D) { attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector); if (attenuation < 0.01f) @@ -3166,7 +3748,7 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR diffusetex[1] = buffer_texture_colorbgra8[x*4+1]; diffusetex[2] = buffer_texture_colorbgra8[x*4+2]; diffusetex[3] = buffer_texture_colorbgra8[x*4+3]; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING) + if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING) { diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0]; diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1]; @@ -3200,7 +3782,7 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f; specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f; specular = pow(specular, SpecularPower * glosstex[3]); - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER) + if (thread->shader_permutation & SHADERPERMUTATION_CUBEFILTER) { // scale down the attenuation to account for the cubefilter multiplying everything by 255 attenuation *= (1.0f / 255.0f); @@ -3222,9 +3804,9 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR buffer_FragColorbgra8[x*4+3] = d[3]; } } - else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE) + else if (thread->shader_permutation & SHADERPERMUTATION_DIFFUSE) { - DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); for (x = startx;x < endx;x++) { z = buffer_z[x]; @@ -3234,7 +3816,7 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector); if (attenuation < 0.01f) continue; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D) + if (thread->shader_permutation & SHADERPERMUTATION_SHADOWMAP2D) { attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector); if (attenuation < 0.01f) @@ -3245,7 +3827,7 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR diffusetex[1] = buffer_texture_colorbgra8[x*4+1]; diffusetex[2] = buffer_texture_colorbgra8[x*4+2]; diffusetex[3] = buffer_texture_colorbgra8[x*4+3]; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING) + if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING) { diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0]; diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1]; @@ -3263,7 +3845,7 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR DPSOFTRAST_Vector3Normalize(lightnormal); diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER) + if (thread->shader_permutation & SHADERPERMUTATION_CUBEFILTER) { // scale down the attenuation to account for the cubefilter multiplying everything by 255 attenuation *= (1.0f / 255.0f); @@ -3296,7 +3878,7 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector); if (attenuation < 0.01f) continue; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D) + if (thread->shader_permutation & SHADERPERMUTATION_SHADOWMAP2D) { attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector); if (attenuation < 0.01f) @@ -3307,14 +3889,14 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR diffusetex[1] = buffer_texture_colorbgra8[x*4+1]; diffusetex[2] = buffer_texture_colorbgra8[x*4+2]; diffusetex[3] = buffer_texture_colorbgra8[x*4+3]; - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING) + if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING) { diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0]; diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1]; diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2]; diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3]; } - if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER) + if (thread->shader_permutation & SHADERPERMUTATION_CUBEFILTER) { // scale down the attenuation to account for the cubefilter multiplying everything by 255 attenuation *= (1.0f / 255.0f); @@ -3336,93 +3918,93 @@ void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTR buffer_FragColorbgra8[x*4+3] = d[3]; } } - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_Refraction(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); } -void DPSOFTRAST_PixelShader_Refraction(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_Refraction(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { // TODO: IMPLEMENT float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); memset(buffer_FragColorbgra8, 0, span->length*4); - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_Water(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); } -void DPSOFTRAST_PixelShader_Water(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_Water(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { // TODO: IMPLEMENT float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); memset(buffer_FragColorbgra8, 0, span->length*4); - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_ShowDepth(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); } -void DPSOFTRAST_PixelShader_ShowDepth(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_ShowDepth(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { // TODO: IMPLEMENT float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); memset(buffer_FragColorbgra8, 0, span->length*4); - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_DeferredGeometry(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); } -void DPSOFTRAST_PixelShader_DeferredGeometry(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_DeferredGeometry(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { // TODO: IMPLEMENT float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); memset(buffer_FragColorbgra8, 0, span->length*4); - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } void DPSOFTRAST_VertexShader_DeferredLightSource(void) { - DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_TransformProject(dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.screencoord4f, dpsoftrast.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); } -void DPSOFTRAST_PixelShader_DeferredLightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span) +void DPSOFTRAST_PixelShader_DeferredLightSource(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span) { // TODO: IMPLEMENT float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; - DPSOFTRAST_Draw_Span_Begin(span, buffer_z); + DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); memset(buffer_FragColorbgra8, 0, span->length*4); - DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8); + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } @@ -3431,39 +4013,82 @@ typedef struct DPSOFTRAST_ShaderModeInfo_s { int lodarrayindex; void (*Vertex)(void); - void (*Span)(const DPSOFTRAST_State_Draw_Span * RESTRICT span); + void (*Span)(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Triangle * RESTRICT triangle, const DPSOFTRAST_State_Span * RESTRICT span); + unsigned char arrays[DPSOFTRAST_ARRAY_TOTAL]; + unsigned char texunits[DPSOFTRAST_MAXTEXTUREUNITS]; } DPSOFTRAST_ShaderModeInfo; -DPSOFTRAST_ShaderModeInfo DPSOFTRAST_ShaderModeTable[SHADERMODE_COUNT] = -{ - {2, DPSOFTRAST_VertexShader_Generic, DPSOFTRAST_PixelShader_Generic, }, - {2, DPSOFTRAST_VertexShader_PostProcess, DPSOFTRAST_PixelShader_PostProcess, }, - {2, DPSOFTRAST_VertexShader_Depth_Or_Shadow, DPSOFTRAST_PixelShader_Depth_Or_Shadow, }, - {2, DPSOFTRAST_VertexShader_FlatColor, DPSOFTRAST_PixelShader_FlatColor, }, - {2, DPSOFTRAST_VertexShader_VertexColor, DPSOFTRAST_PixelShader_VertexColor, }, - {2, DPSOFTRAST_VertexShader_Lightmap, DPSOFTRAST_PixelShader_Lightmap, }, - {2, DPSOFTRAST_VertexShader_FakeLight, DPSOFTRAST_PixelShader_FakeLight, }, - {2, DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace, DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace, }, - {2, DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace, DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace}, - {2, DPSOFTRAST_VertexShader_LightDirection, DPSOFTRAST_PixelShader_LightDirection, }, - {2, DPSOFTRAST_VertexShader_LightSource, DPSOFTRAST_PixelShader_LightSource, }, - {2, DPSOFTRAST_VertexShader_Refraction, DPSOFTRAST_PixelShader_Refraction, }, - {2, DPSOFTRAST_VertexShader_Water, DPSOFTRAST_PixelShader_Water, }, - {2, DPSOFTRAST_VertexShader_ShowDepth, DPSOFTRAST_PixelShader_ShowDepth, }, - {2, DPSOFTRAST_VertexShader_DeferredGeometry, DPSOFTRAST_PixelShader_DeferredGeometry, }, - {2, DPSOFTRAST_VertexShader_DeferredLightSource, DPSOFTRAST_PixelShader_DeferredLightSource, } +static const DPSOFTRAST_ShaderModeInfo DPSOFTRAST_ShaderModeTable[SHADERMODE_COUNT] = +{ + {2, DPSOFTRAST_VertexShader_Generic, DPSOFTRAST_PixelShader_Generic, {DPSOFTRAST_ARRAY_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD1, ~0}, {GL20TU_FIRST, GL20TU_SECOND, ~0}}, + {2, DPSOFTRAST_VertexShader_PostProcess, DPSOFTRAST_PixelShader_PostProcess, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD1, ~0}, {GL20TU_FIRST, GL20TU_SECOND, ~0}}, + {2, DPSOFTRAST_VertexShader_Depth_Or_Shadow, DPSOFTRAST_PixelShader_Depth_Or_Shadow, {~0}, {~0}}, + {2, DPSOFTRAST_VertexShader_FlatColor, DPSOFTRAST_PixelShader_FlatColor, {DPSOFTRAST_ARRAY_TEXCOORD0, ~0}, {GL20TU_COLOR, ~0}}, + {2, DPSOFTRAST_VertexShader_VertexColor, DPSOFTRAST_PixelShader_VertexColor, {DPSOFTRAST_ARRAY_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, ~0}, {GL20TU_COLOR, ~0}}, + {2, DPSOFTRAST_VertexShader_Lightmap, DPSOFTRAST_PixelShader_Lightmap, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD4, ~0}, {GL20TU_COLOR, GL20TU_LIGHTMAP, GL20TU_GLOW, ~0}}, + {2, DPSOFTRAST_VertexShader_FakeLight, DPSOFTRAST_PixelShader_FakeLight, {~0}, {~0}}, + {2, DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace, DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD4, ~0}, {GL20TU_COLOR, GL20TU_LIGHTMAP, GL20TU_GLOW, ~0}}, + {2, DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace, DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD4, ~0}, {GL20TU_COLOR, GL20TU_LIGHTMAP, GL20TU_GLOW, ~0}}, + {2, DPSOFTRAST_VertexShader_LightDirection, DPSOFTRAST_PixelShader_LightDirection, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD1, DPSOFTRAST_ARRAY_TEXCOORD2, DPSOFTRAST_ARRAY_TEXCOORD3, ~0}, {GL20TU_COLOR, GL20TU_PANTS, GL20TU_SHIRT, GL20TU_GLOW, GL20TU_NORMAL, GL20TU_GLOSS, ~0}}, + {2, DPSOFTRAST_VertexShader_LightSource, DPSOFTRAST_PixelShader_LightSource, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD1, DPSOFTRAST_ARRAY_TEXCOORD2, DPSOFTRAST_ARRAY_TEXCOORD3, DPSOFTRAST_ARRAY_TEXCOORD4, ~0}, {GL20TU_COLOR, GL20TU_PANTS, GL20TU_SHIRT, GL20TU_GLOW, GL20TU_NORMAL, GL20TU_GLOSS, GL20TU_CUBE, ~0}}, + {2, DPSOFTRAST_VertexShader_Refraction, DPSOFTRAST_PixelShader_Refraction, {~0}}, + {2, DPSOFTRAST_VertexShader_Water, DPSOFTRAST_PixelShader_Water, {~0}}, + {2, DPSOFTRAST_VertexShader_ShowDepth, DPSOFTRAST_PixelShader_ShowDepth, {~0}}, + {2, DPSOFTRAST_VertexShader_DeferredGeometry, DPSOFTRAST_PixelShader_DeferredGeometry, {~0}}, + {2, DPSOFTRAST_VertexShader_DeferredLightSource, DPSOFTRAST_PixelShader_DeferredLightSource, {~0}} }; - -void DPSOFTRAST_Draw_ProcessSpans(void) +int DPSOFTRAST_Draw_InterpretCommands(DPSOFTRAST_State_Thread *thread, int commandoffset, int endoffset) +{ + while (commandoffset != endoffset) + { + DPSOFTRAST_Command *command = (DPSOFTRAST_Command *)&dpsoftrast.commandpool.commands[commandoffset]; + switch (command->opcode) + { +#define INTERPCOMMAND(name) \ + case DPSOFTRAST_OPCODE_##name : \ + DPSOFTRAST_Interpret_##name (thread, (DPSOFTRAST_Command_##name *)command); \ + commandoffset += sizeof( DPSOFTRAST_Command_##name ) + ((COMMAND_SIZE - (sizeof( DPSOFTRAST_Command_##name )&(COMMAND_SIZE-1))) & (COMMAND_SIZE-1)); \ + if (commandoffset >= DPSOFTRAST_DRAW_MAXCOMMANDPOOL) \ + commandoffset = 0; \ + break; + INTERPCOMMAND(Viewport) + INTERPCOMMAND(ClearColor) + INTERPCOMMAND(ClearDepth) + INTERPCOMMAND(ColorMask) + INTERPCOMMAND(DepthTest) + INTERPCOMMAND(ScissorTest) + INTERPCOMMAND(Scissor) + INTERPCOMMAND(BlendFunc) + INTERPCOMMAND(BlendSubtract) + INTERPCOMMAND(DepthMask) + INTERPCOMMAND(DepthFunc) + INTERPCOMMAND(DepthRange) + INTERPCOMMAND(PolygonOffset) + INTERPCOMMAND(AlphaTest) + INTERPCOMMAND(AlphaFunc) + INTERPCOMMAND(SetTexture) + INTERPCOMMAND(SetShader) + INTERPCOMMAND(Uniform4f) + INTERPCOMMAND(UniformMatrix4f) + INTERPCOMMAND(Uniform1i) + + case DPSOFTRAST_OPCODE_Reset: + commandoffset = 0; + break; + } + } + return commandoffset; +} + +int DPSOFTRAST_Draw_ProcessSpans(DPSOFTRAST_State_Thread *thread, int commandoffset) { int i; int x; int startx; int endx; - int numspans = dpsoftrast.draw.numspans; // unsigned int c; // unsigned int *colorpixel; unsigned int *depthpixel; @@ -3472,18 +4097,26 @@ void DPSOFTRAST_Draw_ProcessSpans(void) int depth; int depthslope; unsigned int d; - DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue; + DPSOFTRAST_State_Triangle *triangle; + DPSOFTRAST_State_Span *span; unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH]; - for (i = 0;i < numspans;i++, span++) + for (i = 0; i < thread->numspans; i++) { - w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3]; - wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3]; - if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels) + span = &thread->spans[i]; + triangle = &dpsoftrast.trianglepool.triangles[span->triangle]; + if (commandoffset != triangle->commandoffset) + { + commandoffset = DPSOFTRAST_Draw_InterpretCommands(thread, commandoffset, triangle->commandoffset); + DPSOFTRAST_ValidateQuick(thread, DPSOFTRAST_VALIDATE_DRAW); + } + if (thread->depthtest && dpsoftrast.fb_depthpixels) { + wslope = triangle->w[0]; + w = triangle->w[2] + span->x*wslope + span->y*triangle->w[1]; depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE); - depth = (int)(w*DPSOFTRAST_DEPTHSCALE - DPSOFTRAST_DEPTHOFFSET*(dpsoftrast.user.polygonoffset[1] + fabs(wslope)*dpsoftrast.user.polygonoffset[0])); - depthpixel = dpsoftrast.fb_depthpixels + span->start; - switch(dpsoftrast.fb_depthfunc) + depth = (int)(w*DPSOFTRAST_DEPTHSCALE - DPSOFTRAST_DEPTHOFFSET*(thread->polygonoffset[1] + fabs(wslope)*thread->polygonoffset[0])); + depthpixel = dpsoftrast.fb_depthpixels + span->y * dpsoftrast.fb_width + span->x; + switch(thread->fb_depthfunc) { default: case GL_ALWAYS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break; @@ -3494,7 +4127,7 @@ void DPSOFTRAST_Draw_ProcessSpans(void) case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break; case GL_NEVER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break; } - //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start; + //colorpixel = dpsoftrast.fb_colorpixels[0] + (span->y * dpsoftrast.fb_width + span->x) * 4;; //for (x = 0;x < span->length;x++) // colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000); // if there is no color buffer, skip pixel shader @@ -3512,9 +4145,9 @@ void DPSOFTRAST_Draw_ProcessSpans(void) // run pixel shader if appropriate // do this before running depthmask code, to allow the pixelshader // to clear pixelmask values for alpha testing - if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask) - DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span); - if (dpsoftrast.user.depthmask) + if (dpsoftrast.fb_colorpixels[0] && thread->fb_colormask) + DPSOFTRAST_ShaderModeTable[thread->shader_mode].Span(thread, triangle, span); + if (thread->depthmask) for (x = startx, d = depth + depthslope*startx;x < endx;x++, d += depthslope) if (pixelmask[x]) depthpixel[x] = d; @@ -3523,25 +4156,226 @@ void DPSOFTRAST_Draw_ProcessSpans(void) { // no depth testing means we're just dealing with color... // if there is no color buffer, skip pixel shader - if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask) + if (dpsoftrast.fb_colorpixels[0] && thread->fb_colormask) { memset(pixelmask, 1, span->length); span->pixelmask = pixelmask; span->startx = 0; span->endx = span->length; - DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span); + DPSOFTRAST_ShaderModeTable[thread->shader_mode].Span(thread, triangle, span); + } + } + } + thread->numspans = 0; + return commandoffset; +} + +void DPSOFTRAST_Draw_GenerateSpans(DPSOFTRAST_State_Thread *thread, int freetriangle) +{ + int miny = (thread->index*dpsoftrast.fb_height)/dpsoftrast.numthreads; + int maxy = ((thread->index+1)*dpsoftrast.fb_height)/dpsoftrast.numthreads; + int commandoffset = thread->commandoffset; + int triangleoffset = thread->triangleoffset; + DPSOFTRAST_State_Triangle *triangle = NULL; + int starty; + int endy; + int y; + int numpoints; + __m128 coords[4]; + __m128i ycoords; + while (triangleoffset != freetriangle) + { + triangle = &dpsoftrast.trianglepool.triangles[triangleoffset]; + if (++triangleoffset >= DPSOFTRAST_DRAW_MAXTRIANGLEPOOL) + triangleoffset = 0; + starty = triangle->starty + 1; + endy = triangle->endy; + if (starty >= maxy || endy <= miny) + continue; + numpoints = triangle->numpoints; + coords[0] = _mm_load_ps(triangle->coords[0]); + coords[1] = _mm_load_ps(triangle->coords[1]); + coords[2] = _mm_load_ps(triangle->coords[2]); + coords[3] = _mm_load_ps(triangle->coords[3]); + ycoords = _mm_load_si128((const __m128i *)triangle->ycoords); + if (starty < miny) + starty = miny; + if (endy > maxy) + endy = maxy; + for (y = starty; y < endy;) + { + __m128 xcoords, xslope; + __m128i ycc = _mm_cmpgt_epi32(_mm_set1_epi32(y), ycoords); + int yccmask = _mm_movemask_epi8(ycc); + int edge0p, edge0n, edge1p, edge1n; + int nexty; + if (numpoints == 4) + { + switch(yccmask) + { + default: + case 0xFFFF: /*0000*/ y = endy; continue; + case 0xFFF0: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 0;break; + case 0xFF0F: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 1;break; + case 0xFF00: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 1;break; + case 0xF0FF: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 2;break; + case 0xF0F0: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 2;break; // concave - nonsense + case 0xF00F: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 2;break; + case 0xF000: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 3;edge1n = 2;break; + case 0x0FFF: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 3;break; + case 0x0FF0: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 0;break; + case 0x0F0F: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 2;edge1n = 1;break; // concave - nonsense + case 0x0F00: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 2;edge1n = 1;break; + case 0x00FF: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 3;break; + case 0x00F0: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 1;edge1n = 0;break; + case 0x000F: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 0;edge1n = 3;break; + case 0x0000: /*1111*/ y++; continue; + } + } + else + { + switch(yccmask) + { + default: + case 0xFFFF: /*000*/ y = endy; continue; + case 0xFFF0: /*100*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 0;break; + case 0xFF0F: /*010*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 1;break; + case 0xFF00: /*110*/ edge0p = 2;edge0n = 0;edge1p = 2;edge1n = 1;break; + case 0x00FF: /*001*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 2;break; + case 0x00F0: /*101*/ edge0p = 1;edge0n = 2;edge1p = 1;edge1n = 0;break; + case 0x000F: /*011*/ edge0p = 0;edge0n = 1;edge1p = 0;edge1n = 2;break; + case 0x0000: /*111*/ y++; continue; + } + } + ycc = _mm_max_epi16(_mm_srli_epi16(ycc, 1), ycoords); + ycc = _mm_min_epi16(ycc, _mm_shuffle_epi32(ycc, _MM_SHUFFLE(1, 0, 3, 2))); + ycc = _mm_min_epi16(ycc, _mm_shuffle_epi32(ycc, _MM_SHUFFLE(2, 3, 0, 1))); + nexty = _mm_extract_epi16(ycc, 0); + if(nexty >= endy) nexty = endy-1; + if (_mm_ucomigt_ss(_mm_max_ss(coords[edge0n], coords[edge0p]), _mm_min_ss(coords[edge1n], coords[edge1p]))) + { + int tmp = edge0n; + edge0n = edge1n; + edge1n = tmp; + tmp = edge0p; + edge0p = edge1p; + edge1p = tmp; } + xslope = _mm_sub_ps(_mm_movelh_ps(coords[edge0n], coords[edge1n]), _mm_movelh_ps(coords[edge0p], coords[edge1p])); + xslope = _mm_div_ps(xslope, _mm_shuffle_ps(xslope, xslope, _MM_SHUFFLE(3, 3, 1, 1))); + xcoords = _mm_add_ps(_mm_movelh_ps(coords[edge0p], coords[edge1p]), + _mm_mul_ps(xslope, _mm_sub_ps(_mm_set1_ps(y), _mm_shuffle_ps(coords[edge0p], coords[edge1p], _MM_SHUFFLE(1, 1, 1, 1))))); + xcoords = _mm_add_ps(xcoords, _mm_set1_ps(0.5f)); + for(; y <= nexty; y++, xcoords = _mm_add_ps(xcoords, xslope)) + { + int startx, endx, offset; + startx = _mm_cvtss_si32(xcoords); + endx = _mm_cvtss_si32(_mm_movehl_ps(xcoords, xcoords)); + if (startx < 0) startx = 0; + if (endx > dpsoftrast.fb_width) endx = dpsoftrast.fb_width; + if (startx >= endx) continue; + for (offset = startx; offset < endx;) + { + DPSOFTRAST_State_Span *span = &thread->spans[thread->numspans]; + span->triangle = (int)(triangle - dpsoftrast.trianglepool.triangles); + span->x = offset; + span->y = y; + span->length = endx - offset; + if (span -> length > DPSOFTRAST_DRAW_MAXSPANLENGTH) + span -> length = DPSOFTRAST_DRAW_MAXSPANLENGTH; + offset += span->length; + if (++thread->numspans >= DPSOFTRAST_DRAW_MAXSPANS) + commandoffset = DPSOFTRAST_Draw_ProcessSpans(thread, commandoffset); + } + } + } + } + + if (thread->numspans > 0) + commandoffset = DPSOFTRAST_Draw_ProcessSpans(thread, commandoffset); + if (commandoffset != triangle->commandoffset) + { + commandoffset = DPSOFTRAST_Draw_InterpretCommands(thread, commandoffset, triangle->commandoffset); + DPSOFTRAST_ValidateQuick(thread, DPSOFTRAST_VALIDATE_DRAW); + } + + MEMORY_BARRIER; + + thread->commandoffset = commandoffset; + thread->triangleoffset = triangleoffset; +} + +void DPSOFTRAST_Draw_FlushThreads(void) +{ + DPSOFTRAST_State_Thread *thread; + int i; + if(dpsoftrast.drawtriangle != dpsoftrast.trianglepool.freetriangle) + { + MEMORY_BARRIER; + dpsoftrast.drawtriangle = dpsoftrast.trianglepool.freetriangle; + } +#ifdef USETHREADS + SDL_LockMutex(dpsoftrast.trianglemutex); +#endif + for (i = 0; i < dpsoftrast.numthreads; i++) + { + thread = &dpsoftrast.threads[i]; +#ifdef USETHREADS + while (thread->triangleoffset != dpsoftrast.drawtriangle) + { + thread->waiting = true; + SDL_CondBroadcast(dpsoftrast.trianglecond); + SDL_CondWait(thread->waitcond, dpsoftrast.trianglemutex); + thread->waiting = false; } +#else + if (thread->triangleoffset != dpsoftrast.drawtriangle) + DPSOFTRAST_Draw_GenerateSpans(thread, dpsoftrast.drawtriangle); +#endif } +#ifdef USETHREADS + SDL_UnlockMutex(dpsoftrast.trianglemutex); +#endif + dpsoftrast.trianglepool.usedtriangles = 0; + dpsoftrast.commandpool.usedcommands = 0; } -void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask) +#ifdef USETHREADS +static int DPSOFTRAST_Draw_Thread(void *data) +{ + DPSOFTRAST_State_Thread *thread = (DPSOFTRAST_State_Thread *)data; + while(thread->index >= 0) + { + if (thread->triangleoffset != dpsoftrast.drawtriangle) + { + DPSOFTRAST_Draw_GenerateSpans(thread, dpsoftrast.drawtriangle); + } + else + { + SDL_LockMutex(dpsoftrast.trianglemutex); + if (thread->triangleoffset != dpsoftrast.drawtriangle) + { + SDL_UnlockMutex(dpsoftrast.trianglemutex); + continue; + } + if (thread->waiting) SDL_CondSignal(thread->waitcond); + SDL_CondWait(dpsoftrast.trianglecond, dpsoftrast.trianglemutex); + SDL_UnlockMutex(dpsoftrast.trianglemutex); + } + } + return 0; +} +#endif + +void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask, int numarrays) { #ifdef SSE2_PRESENT - int cullface = dpsoftrast.user.cullface; + int cullface = dpsoftrast.cullface; int width = dpsoftrast.fb_width; int height = dpsoftrast.fb_height; __m128i fbmax = _mm_sub_epi16(_mm_setr_epi16(width, height, width, height, width, height, width, height), _mm_set1_epi16(1)); + DPSOFTRAST_State_Triangle *triangle; + int numqueued = 0; int i; int j; int k; @@ -3550,22 +4384,14 @@ void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const i __m128i screeny; int starty, endy; int numpoints; - int edge0p; - int edge0n; - int edge1p; - int edge1n; - int startx; - int endx; - unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; - __m128 mipedgescale; + int clipcase; float clipdist[4]; - __m128 clipfrac[4]; - __m128 clipped[DPSOFTRAST_ARRAY_TOTAL][4]; + __m128 triangleedge1, triangleedge2, trianglenormal; + __m128 clipfrac[3]; __m128 screen[4]; - __m128 proj[DPSOFTRAST_ARRAY_TOTAL][4]; DPSOFTRAST_Texture *texture; - DPSOFTRAST_State_Draw_Span *span; - DPSOFTRAST_State_Draw_Span *oldspan; + screen[3] = _mm_setzero_ps(); + clipfrac[0] = clipfrac[1] = clipfrac[2] = _mm_setzero_ps(); for (i = 0;i < numtriangles;i++) { // generate the 3 edges of this triangle @@ -3590,27 +4416,21 @@ void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const i } #define SKIPBACKFACE \ - if(cullface != GL_NONE) \ + triangleedge1 = _mm_sub_ps(screen[0], screen[1]); \ + triangleedge2 = _mm_sub_ps(screen[2], screen[1]); \ + /* store normal in 2, 0, 1 order instead of 0, 1, 2 as it requires fewer shuffles and leaves z component accessible as scalar */ \ + trianglenormal = _mm_sub_ss(_mm_mul_ss(triangleedge1, _mm_shuffle_ps(triangleedge2, triangleedge2, _MM_SHUFFLE(3, 0, 2, 1))), \ + _mm_mul_ss(_mm_shuffle_ps(triangleedge1, triangleedge1, _MM_SHUFFLE(3, 0, 2, 1)), triangleedge2)); \ + switch(cullface) \ { \ - __m128 triangleedge[2]; \ - __m128 trianglenormal; \ - triangleedge[0] = _mm_sub_ps(screen[0], screen[1]); \ - triangleedge[1] = _mm_sub_ps(screen[2], screen[1]); \ - /* store normal in 2, 0, 1 order instead of 0, 1, 2 as it requires fewer shuffles and leaves z component accessible as scalar */ \ - trianglenormal = _mm_sub_ss(_mm_mul_ss(triangleedge[0], _mm_shuffle_ps(triangleedge[1], triangleedge[1], _MM_SHUFFLE(3, 0, 2, 1))), \ - _mm_mul_ss(_mm_shuffle_ps(triangleedge[0], triangleedge[0], _MM_SHUFFLE(3, 0, 2, 1)), triangleedge[1])); \ - /* apply current cullface mode (this culls many triangles) */ \ - switch(cullface) \ - { \ - case GL_BACK: \ - if (_mm_ucomilt_ss(trianglenormal, _mm_setzero_ps())) \ - continue; \ - break; \ - case GL_FRONT: \ - if (_mm_ucomigt_ss(trianglenormal, _mm_setzero_ps())) \ - continue; \ - break; \ - } \ + case GL_BACK: \ + if (_mm_ucomilt_ss(trianglenormal, _mm_setzero_ps())) \ + continue; \ + break; \ + case GL_FRONT: \ + if (_mm_ucomigt_ss(trianglenormal, _mm_setzero_ps())) \ + continue; \ + break; \ } //trianglenormal = _mm_sub_ps(_mm_mul_ps(triangleedge[0], _mm_shuffle_ps(triangleedge[1], triangleedge[1], _MM_SHUFFLE(3, 0, 2, 1))), // _mm_mul_ps(_mm_shuffle_ps(triangleedge[0], triangleedge[0], _MM_SHUFFLE(3, 0, 2, 1)), triangleedge[1])); @@ -3619,37 +4439,40 @@ void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const i //trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2]; // macros for clipping vertices + #define CLIPPEDVERTEXLERP(k,p1, p2) \ - clipfrac[k] = _mm_set1_ps(clipdist[p1] / (clipdist[p1] - clipdist[p2])); \ + clipfrac[p1] = _mm_set1_ps(clipdist[p1] / (clipdist[p1] - clipdist[p2])); \ { \ - __m128 v1 = _mm_load_ps(&dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[p1]*4]), v2 = _mm_load_ps(&dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[p2]*4]); \ - clipped[DPSOFTRAST_ARRAY_POSITION][k] = _mm_add_ps(v1, _mm_mul_ps(_mm_sub_ps(v2, v1), clipfrac[k])); \ - } \ - screen[k] = DPSOFTRAST_Draw_ProjectVertex(clipped[DPSOFTRAST_ARRAY_POSITION][k]); -#define CLIPPEDATTRIBSLERP(k,p1,p2) \ - for (j = DPSOFTRAST_ARRAY_POSITION+1;j < DPSOFTRAST_ARRAY_TOTAL;j++)\ - {\ - /*if (arraymask[j])*/\ - {\ - __m128 v1 = _mm_load_ps(&dpsoftrast.draw.post_array4f[j][e[p1]*4]), v2 = _mm_load_ps(&dpsoftrast.draw.post_array4f[j][e[p2]*4]); \ - clipped[j][k] = _mm_add_ps(v1, _mm_mul_ps(_mm_sub_ps(v2, v1), clipfrac[k])); \ - }\ + __m128 v1 = _mm_load_ps(&dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[p1]*4]), v2 = _mm_load_ps(&dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[p2]*4]); \ + screen[k] = DPSOFTRAST_Draw_ProjectVertex(_mm_add_ps(v1, _mm_mul_ps(_mm_sub_ps(v2, v1), clipfrac[p1]))); \ } #define CLIPPEDVERTEXCOPY(k,p1) \ - screen[k] = _mm_load_ps(&dpsoftrast.draw.screencoord4f[e[p1]*4]); -#define CLIPPEDATTRIBSCOPY(k,p1) \ - for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\ - {\ - /*if (arraymask[j])*/\ - {\ - clipped[j][k] = _mm_load_ps(&dpsoftrast.draw.post_array4f[j][e[p1]*4]); \ - }\ - } + screen[k] = _mm_load_ps(&dpsoftrast.screencoord4f[e[p1]*4]); + +#define GENATTRIBCOPY(j, attrib, p1) \ + attrib = _mm_load_ps(&dpsoftrast.post_array4f[j][e[p1]*4]); +#define GENATTRIBLERP(j, attrib, p1, p2) \ + { \ + __m128 v1 = _mm_load_ps(&dpsoftrast.post_array4f[j][e[p1]*4]), v2 = _mm_load_ps(&dpsoftrast.post_array4f[j][e[p2]*4]); \ + attrib = _mm_add_ps(v1, _mm_mul_ps(_mm_sub_ps(v2, v1), clipfrac[p1])); \ + } +#define GENATTRIBS(j, attrib0, attrib1, attrib2) \ + switch(clipcase) \ + { \ + default: \ + case 0: GENATTRIBCOPY(j, attrib0, 0); GENATTRIBCOPY(j, attrib1, 1); GENATTRIBCOPY(j, attrib2, 2); break; \ + case 1: GENATTRIBCOPY(j, attrib0, 0); GENATTRIBCOPY(j, attrib1, 1); GENATTRIBLERP(j, attrib2, 1, 2); break; \ + case 2: GENATTRIBCOPY(j, attrib0, 0); GENATTRIBLERP(j, attrib1, 0, 1); GENATTRIBLERP(j, attrib2, 1, 2); break; \ + case 3: GENATTRIBCOPY(j, attrib0, 0); GENATTRIBLERP(j, attrib1, 0, 1); GENATTRIBLERP(j, attrib2, 2, 0); break; \ + case 4: GENATTRIBLERP(j, attrib0, 0, 1); GENATTRIBCOPY(j, attrib1, 1); GENATTRIBCOPY(j, attrib2, 2); break; \ + case 5: GENATTRIBLERP(j, attrib0, 0, 1); GENATTRIBCOPY(j, attrib1, 1); GENATTRIBLERP(j, attrib2, 1, 2); break; \ + case 6: GENATTRIBLERP(j, attrib0, 1, 2); GENATTRIBCOPY(j, attrib1, 2); GENATTRIBLERP(j, attrib2, 2, 0); break; \ + } // calculate distance from nearplane - clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+3]; - clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+3]; - clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+3]; + clipdist[0] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+3]; + clipdist[1] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+3]; + clipdist[2] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+3]; if (clipdist[0] >= 0.0f) { if (clipdist[1] >= 0.0f) @@ -3658,16 +4481,16 @@ void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const i { // triangle is entirely in front of nearplane CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXCOPY(2,2); - numpoints = 3; SKIPBACKFACE; - CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSCOPY(2,2); + numpoints = 3; + clipcase = 0; } else { CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXLERP(2,1,2); CLIPPEDVERTEXLERP(3,2,0); - numpoints = 4; SKIPBACKFACE; - CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSLERP(2,1,2); CLIPPEDATTRIBSLERP(3,2,0); + numpoints = 4; + clipcase = 1; } } else @@ -3675,16 +4498,16 @@ void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const i if (clipdist[2] >= 0.0f) { CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXLERP(1,0,1); CLIPPEDVERTEXLERP(2,1,2); CLIPPEDVERTEXCOPY(3,2); - numpoints = 4; SKIPBACKFACE; - CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSLERP(1,0,1); CLIPPEDATTRIBSLERP(2,1,2); CLIPPEDATTRIBSCOPY(3,2); + numpoints = 4; + clipcase = 2; } else { CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXLERP(1,0,1); CLIPPEDVERTEXLERP(2,2,0); - numpoints = 3; SKIPBACKFACE; - CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSLERP(1,0,1); CLIPPEDATTRIBSLERP(2,2,0); + numpoints = 3; + clipcase = 3; } } } @@ -3693,31 +4516,30 @@ void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const i if (clipdist[2] >= 0.0f) { CLIPPEDVERTEXLERP(0,0,1); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXCOPY(2,2); CLIPPEDVERTEXLERP(3,2,0); - numpoints = 4; SKIPBACKFACE; - CLIPPEDATTRIBSLERP(0,0,1); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSCOPY(2,2); CLIPPEDATTRIBSLERP(3,2,0); + numpoints = 4; + clipcase = 4; } else { CLIPPEDVERTEXLERP(0,0,1); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXLERP(2,1,2); - numpoints = 3; SKIPBACKFACE; - CLIPPEDATTRIBSLERP(0,0,1); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSLERP(2,1,2); + numpoints = 3; + clipcase = 5; } } else if (clipdist[2] >= 0.0f) { CLIPPEDVERTEXLERP(0,1,2); CLIPPEDVERTEXCOPY(1,2); CLIPPEDVERTEXLERP(2,2,0); - numpoints = 3; SKIPBACKFACE; - CLIPPEDATTRIBSLERP(0,1,2); CLIPPEDATTRIBSCOPY(1,2); CLIPPEDATTRIBSLERP(2,2,0); + numpoints = 3; + clipcase = 6; } else continue; // triangle is entirely behind nearplane { // calculate integer y coords for triangle points - __m128i screeni = _mm_packs_epi32(_mm_cvttps_epi32(_mm_shuffle_ps(screen[0], screen[1], _MM_SHUFFLE(1, 0, 1, 0))), - _mm_cvttps_epi32(_mm_shuffle_ps(screen[2], numpoints <= 3 ? screen[2] : screen[3], _MM_SHUFFLE(1, 0, 1, 0)))), + __m128i screeni = _mm_packs_epi32(_mm_cvttps_epi32(_mm_movelh_ps(screen[0], screen[1])), _mm_cvttps_epi32(_mm_movelh_ps(screen[2], numpoints > 3 ? screen[3] : screen[2]))), screenir = _mm_shuffle_epi32(screeni, _MM_SHUFFLE(1, 0, 3, 2)), screenmin = _mm_min_epi16(screeni, screenir), screenmax = _mm_max_epi16(screeni, screenir); @@ -3733,299 +4555,182 @@ void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const i screeny = _mm_srai_epi32(screeni, 16); } + if (dpsoftrast.trianglepool.usedtriangles >= DPSOFTRAST_DRAW_MAXTRIANGLEPOOL-1) +#ifdef USETHREADS + DPSOFTRAST_Draw_FreeTrianglePool(DPSOFTRAST_DRAW_MAXTRIANGLEPOOL/8); +#else + DPSOFTRAST_Draw_FlushThreads(); +#endif + + triangle = &dpsoftrast.trianglepool.triangles[dpsoftrast.trianglepool.freetriangle]; + triangle->commandoffset = dpsoftrast.commandpool.freecommand; + triangle->starty = starty; + triangle->endy = endy; + triangle->numpoints = numpoints; + _mm_store_ps(triangle->coords[0], screen[0]); + _mm_store_ps(triangle->coords[1], screen[1]); + _mm_store_ps(triangle->coords[2], screen[2]); + _mm_store_ps(triangle->coords[3], numpoints > 3 ? screen[3] : screen[2]); + _mm_store_si128((__m128i *)triangle->ycoords, screeny); + + // calculate attribute plans for triangle data... // okay, this triangle is going to produce spans, we'd better project // the interpolants now (this is what gives perspective texturing), // this consists of simply multiplying all arrays by the W coord // (which is basically 1/Z), which will be undone per-pixel // (multiplying by Z again) to get the perspective-correct array // values - for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++) { - //if (arraymask[j]) + __m128 attribuvslope, attribuxslope, attribuyslope, attribvxslope, attribvyslope, attriborigin, attribedge1, attribedge2, attribxslope, attribyslope, w0, w1, w2, x1, y1; + attribuvslope = _mm_div_ps(_mm_movelh_ps(triangleedge1, triangleedge2), _mm_shuffle_ps(trianglenormal, trianglenormal, _MM_SHUFFLE(0, 0, 0, 0))); + attribuxslope = _mm_shuffle_ps(attribuvslope, attribuvslope, _MM_SHUFFLE(3, 3, 3, 3)); + attribuyslope = _mm_shuffle_ps(attribuvslope, attribuvslope, _MM_SHUFFLE(2, 2, 2, 2)); + attribvxslope = _mm_shuffle_ps(attribuvslope, attribuvslope, _MM_SHUFFLE(1, 1, 1, 1)); + attribvyslope = _mm_shuffle_ps(attribuvslope, attribuvslope, _MM_SHUFFLE(0, 0, 0, 0)); + w0 = _mm_shuffle_ps(screen[0], screen[0], _MM_SHUFFLE(3, 3, 3, 3)); + w1 = _mm_shuffle_ps(screen[1], screen[1], _MM_SHUFFLE(3, 3, 3, 3)); + w2 = _mm_shuffle_ps(screen[2], screen[2], _MM_SHUFFLE(3, 3, 3, 3)); + attribedge1 = _mm_sub_ss(w0, w1); + attribedge2 = _mm_sub_ss(w2, w1); + attribxslope = _mm_sub_ss(_mm_mul_ss(attribuxslope, attribedge1), _mm_mul_ss(attribvxslope, attribedge2)); + attribyslope = _mm_sub_ss(_mm_mul_ss(attribvyslope, attribedge2), _mm_mul_ss(attribuyslope, attribedge1)); + x1 = _mm_shuffle_ps(screen[1], screen[1], _MM_SHUFFLE(0, 0, 0, 0)); + y1 = _mm_shuffle_ps(screen[1], screen[1], _MM_SHUFFLE(1, 1, 1, 1)); + attriborigin = _mm_sub_ss(w1, _mm_add_ss(_mm_mul_ss(attribxslope, x1), _mm_mul_ss(attribyslope, y1))); + _mm_store_ss(&triangle->w[0], attribxslope); + _mm_store_ss(&triangle->w[1], attribyslope); + _mm_store_ss(&triangle->w[2], attriborigin); + for (j = 0;j < numarrays;j++) { - for (k = 0;k < numpoints;k++) + if (arraymask[j]) { - proj[j][k] = _mm_mul_ps(clipped[j][k], _mm_shuffle_ps(screen[k], screen[k], _MM_SHUFFLE(3, 3, 3, 3))); - } - } - } + __m128 attrib0, attrib1, attrib2; + GENATTRIBS(j, attrib0, attrib1, attrib2); + attriborigin = _mm_mul_ps(attrib1, w1); + attribedge1 = _mm_sub_ps(_mm_mul_ps(attrib0, w0), attriborigin); + attribedge2 = _mm_sub_ps(_mm_mul_ps(attrib2, w2), attriborigin); + attribxslope = _mm_sub_ps(_mm_mul_ps(attribuxslope, attribedge1), _mm_mul_ps(attribvxslope, attribedge2)); + attribyslope = _mm_sub_ps(_mm_mul_ps(attribvyslope, attribedge2), _mm_mul_ps(attribuyslope, attribedge1)); + attriborigin = _mm_sub_ps(attriborigin, _mm_add_ps(_mm_mul_ps(attribxslope, x1), _mm_mul_ps(attribyslope, y1))); + _mm_stream_ps(triangle->attribs[j][0], attribxslope); + _mm_stream_ps(triangle->attribs[j][1], attribyslope); + _mm_stream_ps(triangle->attribs[j][2], attriborigin); + } + } + } + // adjust texture LOD by texture density, in the simplest way possible... - mipedgescale = _mm_sub_ps(_mm_shuffle_ps(screen[0], screen[2], _MM_SHUFFLE(1, 0, 1, 0)), _mm_shuffle_ps(screen[1], screen[1], _MM_SHUFFLE(1, 0, 1, 0))); - mipedgescale = _mm_mul_ps(mipedgescale, mipedgescale); - mipedgescale = _mm_div_ps(_mm_set1_ps(1.0f), _mm_add_ps(mipedgescale, _mm_shuffle_ps(mipedgescale, mipedgescale, _MM_SHUFFLE(2, 3, 0, 1)))); - for (j = 0;j < DPSOFTRAST_MAXTEXTUREUNITS;j++) { - texture = dpsoftrast.texbound[j]; - if (texture) - { - __m128 mipedgetc; - if (texture->filter <= DPSOFTRAST_TEXTURE_FILTER_LINEAR) - { - mip[j] = 0; - continue; - } - k = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].lodarrayindex; - mipedgetc = _mm_sub_ps(_mm_shuffle_ps(clipped[k][0], clipped[k][2], _MM_SHUFFLE(1, 0, 1, 0)), - _mm_shuffle_ps(clipped[k][1], clipped[k][1], _MM_SHUFFLE(1, 0, 1, 0))); - mipedgetc = _mm_mul_ps(mipedgetc, _mm_cvtepi32_ps(_mm_shuffle_epi32(_mm_loadl_epi64((const __m128i *)&texture->mipmap[0][2]), _MM_SHUFFLE(1, 0, 1, 0)))); - mipedgetc = _mm_mul_ps(mipedgetc, mipedgetc); - mipedgetc = _mm_add_ps(mipedgetc, _mm_shuffle_ps(mipedgetc, mipedgetc, _MM_SHUFFLE(2, 3, 0, 1))); - mipedgetc = _mm_mul_ps(mipedgetc, mipedgescale); - mipedgetc = _mm_min_ss(mipedgetc, _mm_shuffle_ps(mipedgetc, mipedgetc, _MM_SHUFFLE(2, 2, 2, 2))); - // this will be multiplied in the texturing routine by the texture resolution - y = _mm_cvtss_si32(mipedgetc); - if (y > 0) - { - y = (int)(log((float)y)/M_LN2); - if (y > texture->mipmaps - 1) - y = texture->mipmaps - 1; - } - else y = 0; - mip[j] = y; - } - } - // iterate potential spans - // TODO: optimize? if we figured out the edge order beforehand, this - // could do loops over the edges in the proper order rather than - // selecting them for each span - // TODO: optimize? the edges could have data slopes calculated - // TODO: optimize? the data slopes could be calculated as a plane - // (2D slopes) to avoid any interpolation along edges at all - for (y = starty+1;y < endy;) - { - int nexty = -1; - __m128 edge0offset, edge1offset, edge0scale, edge1scale, data[DPSOFTRAST_ARRAY_TOTAL+1][2], slope[DPSOFTRAST_ARRAY_TOTAL+1][2]; - __m128i screenycc = _mm_cmpgt_epi32(_mm_set1_epi32(y), screeny); - int screenymask = _mm_movemask_epi8(screenycc); - if (numpoints == 4) - { - switch(screenymask) - { - default: - case 0xFFFF: /*0000*/ y++; continue; - case 0xFFF0: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 0;break; - case 0xFF0F: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 1;break; - case 0xFF00: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 1;break; - case 0xF0FF: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 2;break; - case 0xF0F0: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 2;break; // concave - nonsense - case 0xF00F: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 2;break; - case 0xF000: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 3;edge1n = 2;break; - case 0x0FFF: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 3;break; - case 0x0FF0: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 0;break; - case 0x0F0F: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 2;edge1n = 1;break; // concave - nonsense - case 0x0F00: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 2;edge1n = 1;break; - case 0x00FF: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 3;break; - case 0x00F0: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 1;edge1n = 0;break; - case 0x000F: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 0;edge1n = 3;break; - case 0x0000: /*1111*/ y++; continue; - } - } - else - { - switch(screenymask) - { - default: - case 0xFFFF: /*000*/ y++; continue; - case 0xFFF0: /*100*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 0;break; - case 0xFF0F: /*010*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 1;break; - case 0xFF00: /*110*/ edge0p = 2;edge0n = 0;edge1p = 2;edge1n = 1;break; - case 0x00FF: /*001*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 2;break; - case 0x00F0: /*101*/ edge0p = 1;edge0n = 2;edge1p = 1;edge1n = 0;break; - case 0x000F: /*011*/ edge0p = 0;edge0n = 1;edge1p = 0;edge1n = 2;break; - case 0x0000: /*111*/ y++; continue; - } - } - screenycc = _mm_max_epi16(_mm_srli_epi16(screenycc, 1), screeny); - screenycc = _mm_min_epi16(screenycc, _mm_shuffle_epi32(screenycc, _MM_SHUFFLE(1, 0, 3, 2))); - screenycc = _mm_min_epi16(screenycc, _mm_shuffle_epi32(screenycc, _MM_SHUFFLE(2, 3, 0, 1))); - nexty = _mm_extract_epi16(screenycc, 0); - if(nexty >= endy) nexty = endy-1; - if (_mm_ucomigt_ss(_mm_max_ss(screen[edge0n], screen[edge0p]), _mm_min_ss(screen[edge1n], screen[edge1p]))) - { - int tmp = edge0n; - edge0n = edge1n; - edge1n = tmp; - tmp = edge0p; - edge0p = edge1p; - edge1p = tmp; - } - edge0offset = _mm_shuffle_ps(screen[edge0p], screen[edge0p], _MM_SHUFFLE(1, 1, 1, 1)); - edge0scale = _mm_div_ss(_mm_set1_ps(1.0f), _mm_sub_ss(_mm_shuffle_ps(screen[edge0n], screen[edge0n], _MM_SHUFFLE(1, 1, 1, 1)), edge0offset)); - edge0scale = _mm_shuffle_ps(edge0scale, edge0scale, _MM_SHUFFLE(0, 0, 0, 0)); - edge0offset = _mm_sub_ps(_mm_set1_ps(y), edge0offset); - edge1offset = _mm_shuffle_ps(screen[edge1p], screen[edge1p], _MM_SHUFFLE(1, 1, 1, 1)); - edge1scale = _mm_div_ss(_mm_set1_ps(1.0f), _mm_sub_ss(_mm_shuffle_ps(screen[edge1n], screen[edge1n], _MM_SHUFFLE(1, 1, 1, 1)), edge1offset)); - edge1offset = _mm_sub_ps(_mm_set1_ps(y), edge1offset); - edge1scale = _mm_shuffle_ps(edge1scale, edge1scale, _MM_SHUFFLE(0, 0, 0, 0)); - j = DPSOFTRAST_ARRAY_TOTAL; - slope[j][0] = _mm_mul_ps(_mm_sub_ps(screen[edge0n], screen[edge0p]), edge0scale); - slope[j][1] = _mm_mul_ps(_mm_sub_ps(screen[edge1n], screen[edge1p]), edge1scale); - data[j][0] = _mm_add_ps(_mm_mul_ps(slope[j][0], edge0offset), screen[edge0p]); - data[j][1] = _mm_add_ps(_mm_mul_ps(slope[j][1], edge1offset), screen[edge1p]); - data[j][1] = _mm_sub_ps(data[j][1], data[j][0]); - slope[j][1] = _mm_sub_ps(slope[j][1], slope[j][0]); - for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++) - { - //if (arraymask[j]) - { - slope[j][0] = _mm_mul_ps(_mm_sub_ps(proj[j][edge0n], proj[j][edge0p]), edge0scale); - slope[j][1] = _mm_mul_ps(_mm_sub_ps(proj[j][edge1n], proj[j][edge1p]), edge1scale); - data[j][0] = _mm_add_ps(_mm_mul_ps(slope[j][0], edge0offset), proj[j][edge0p]); - data[j][1] = _mm_add_ps(_mm_mul_ps(slope[j][1], edge1offset), proj[j][edge1p]); - data[j][1] = _mm_sub_ps(data[j][1], data[j][0]); - slope[j][1] = _mm_sub_ps(slope[j][1], slope[j][0]); - } - } - goto firstspan; - for(; y <= nexty; y++) + __m128 mipedgescale, mipedgetc, mipdensity, attrib0, attrib1, attrib2; + memset(triangle->mip, 0, sizeof(triangle->mip)); + mipedgescale = _mm_movelh_ps(triangleedge1, triangleedge2); + mipedgescale = _mm_mul_ps(mipedgescale, mipedgescale); + mipedgescale = _mm_rsqrt_ps(_mm_add_ps(mipedgescale, _mm_shuffle_ps(mipedgescale, mipedgescale, _MM_SHUFFLE(2, 3, 0, 1)))); + k = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].lodarrayindex; + GENATTRIBS(k, attrib0, attrib1, attrib2); + mipedgetc = _mm_sub_ps(_mm_movelh_ps(attrib0, attrib2), _mm_movelh_ps(attrib1, attrib1)); + mipedgetc = _mm_mul_ps(mipedgetc, mipedgescale); + for (j = 0;j < DPSOFTRAST_MAXTEXTUREUNITS;j++) { - __m128 data0, data1, spanilength, startxlerp; - j = DPSOFTRAST_ARRAY_TOTAL; - data[j][0] = _mm_add_ps(data[j][0], slope[j][0]); - data[j][1] = _mm_add_ps(data[j][1], slope[j][1]); - for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++) + int texunit = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].texunits[j]; + if (texunit >= DPSOFTRAST_MAXTEXTUREUNITS) + break; + texture = dpsoftrast.texbound[texunit]; + if (texture && texture->filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR) { - //if (arraymask[j]) + mipdensity = _mm_mul_ps(mipedgetc, _mm_cvtepi32_ps(_mm_shuffle_epi32(_mm_loadl_epi64((const __m128i *)&texture->mipmap[0][2]), _MM_SHUFFLE(1, 0, 1, 0)))); + mipdensity = _mm_mul_ps(mipdensity, mipdensity); + mipdensity = _mm_add_ps(mipdensity, _mm_shuffle_ps(mipdensity, mipdensity, _MM_SHUFFLE(2, 3, 0, 1))); + mipdensity = _mm_min_ss(mipdensity, _mm_shuffle_ps(mipdensity, mipdensity, _MM_SHUFFLE(2, 2, 2, 2))); + // this will be multiplied in the texturing routine by the texture resolution + y = _mm_cvtss_si32(mipdensity); + if (y > 0) { - data[j][0] = _mm_add_ps(data[j][0], slope[j][0]); - data[j][1] = _mm_add_ps(data[j][1], slope[j][1]); + y = (int)(log((float)y)*0.5f/M_LN2); + if (y > texture->mipmaps - 1) + y = texture->mipmaps - 1; + triangle->mip[texunit] = y; } } + } + } - firstspan: - startx = _mm_cvtss_si32(_mm_add_ss(data[DPSOFTRAST_ARRAY_TOTAL][0], _mm_set1_ps(0.5f))); - endx = _mm_cvtss_si32(_mm_add_ss(_mm_add_ss(data[DPSOFTRAST_ARRAY_TOTAL][0], data[DPSOFTRAST_ARRAY_TOTAL][1]), _mm_set1_ps(0.5f))); - if (startx < 0) startx = 0; - if (endx > width) endx = width; - if (startx >= endx) continue; -#if 0 - _mm_store_ss(&startxf, data0); - _mm_store_ss(&endxf, data1); - if (startxf > startx || endxf < endx-1) { printf("%s:%i X wrong (%i to %i is outside %f to %f)\n", __FILE__, __LINE__, startx, endx, startxf, endxf); } + dpsoftrast.trianglepool.freetriangle = dpsoftrast.trianglepool.freetriangle < DPSOFTRAST_DRAW_MAXTRIANGLEPOOL-1 ? dpsoftrast.trianglepool.freetriangle + 1 : 0; + dpsoftrast.trianglepool.usedtriangles++; + + numqueued++; + if (numqueued >= DPSOFTRAST_DRAW_FLUSHPROCESSTRIANGLES) + { + MEMORY_BARRIER; + dpsoftrast.drawtriangle = dpsoftrast.trianglepool.freetriangle; + +#ifdef USETHREADS + SDL_LockMutex(dpsoftrast.trianglemutex); + SDL_CondBroadcast(dpsoftrast.trianglecond); + SDL_UnlockMutex(dpsoftrast.trianglemutex); +#else + DPSOFTRAST_Draw_FlushThreads(); #endif - spanilength = _mm_div_ss(_mm_set1_ps(1.0f), data[DPSOFTRAST_ARRAY_TOTAL][1]); - spanilength = _mm_shuffle_ps(spanilength, spanilength, _MM_SHUFFLE(0, 0, 0, 0)); - startxlerp = _mm_sub_ss(_mm_cvtsi32_ss(_mm_setzero_ps(), startx), data[DPSOFTRAST_ARRAY_TOTAL][0]); - startxlerp = _mm_shuffle_ps(startxlerp, startxlerp, _MM_SHUFFLE(0, 0, 0, 0)); - span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++]; - memcpy(span->mip, mip, sizeof(span->mip)); - span->start = y * width + startx; - span->length = endx - startx; - j = DPSOFTRAST_ARRAY_TOTAL; - data1 = _mm_mul_ps(data[j][1], spanilength); - data0 = _mm_add_ps(data[j][0], _mm_mul_ps(data1, startxlerp)); - _mm_store_ps(span->data[0][j], data0); - _mm_store_ps(span->data[1][j], data1); - for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++) - { - //if (arraymask[j]) - { - data1 = _mm_mul_ps(data[j][1], spanilength); - data0 = _mm_add_ps(data[j][0], _mm_mul_ps(data1, startxlerp)); - _mm_store_ps(span->data[0][j], data0); - _mm_store_ps(span->data[1][j], data1); - } - } - // to keep the shader routines from needing more than a small - // buffer for pixel intermediate data, we split long spans... - while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH) - { - span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH; - if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE) - { - DPSOFTRAST_Draw_ProcessSpans(); - dpsoftrast.draw.numspans = 0; - } - oldspan = span; - span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++]; - *span = *oldspan; - startx += DPSOFTRAST_DRAW_MAXSPANLENGTH; - span->start = y * width + startx; - span->length = endx - startx; - j = DPSOFTRAST_ARRAY_TOTAL; - _mm_store_ps(span->data[0][j], _mm_add_ps(_mm_load_ps(span->data[0][j]), _mm_mul_ps(_mm_load_ps(span->data[1][j]), _mm_set1_ps(DPSOFTRAST_DRAW_MAXSPANLENGTH)))); - for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++) - { - //if (arraymask[j]) - { - _mm_store_ps(span->data[0][j], _mm_add_ps(_mm_load_ps(span->data[0][j]), _mm_mul_ps(_mm_load_ps(span->data[1][j]), _mm_set1_ps(DPSOFTRAST_DRAW_MAXSPANLENGTH)))); - } - } - } - // after all that, we have a span suitable for the pixel shader... - if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE) - { - DPSOFTRAST_Draw_ProcessSpans(); - dpsoftrast.draw.numspans = 0; - } - } + numqueued = 0; } - // draw outlines over triangle for debugging - // for (j = 0, k = numpoints-1;j < numpoints;k = j, j++) - // DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]); } - if (dpsoftrast.draw.numspans) + if (numqueued > 0) { - DPSOFTRAST_Draw_ProcessSpans(); - dpsoftrast.draw.numspans = 0; + MEMORY_BARRIER; + dpsoftrast.drawtriangle = dpsoftrast.trianglepool.freetriangle; + +#ifdef USETHREADS + SDL_LockMutex(dpsoftrast.trianglemutex); + SDL_CondBroadcast(dpsoftrast.trianglecond); + SDL_UnlockMutex(dpsoftrast.trianglemutex); +#else + DPSOFTRAST_Draw_FlushThreads(); +#endif } #endif } -void DPSOFTRAST_Draw_DebugPoints(void) +void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s) { int i; - int x; - int y; - int numvertices = dpsoftrast.draw.numvertices; - int w = dpsoftrast.fb_width; - int bounds[4]; - unsigned int *pixels = dpsoftrast.fb_colorpixels[0]; - const float *c4f; - bounds[0] = dpsoftrast.fb_viewportscissor[0]; - bounds[1] = dpsoftrast.fb_viewportscissor[1]; - bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2]; - bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3]; - for (i = 0;i < numvertices;i++) + int lastarray = DPSOFTRAST_ARRAY_POSITION; + unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL]; + memset(arraymask, false, sizeof(arraymask)); + arraymask[DPSOFTRAST_ARRAY_POSITION] = true; + for (i = 0; i < DPSOFTRAST_ARRAY_TOTAL; i++) { - // check nearclip - //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f) - // continue; - x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]); - y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]); - //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f); - //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f); - //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f); - //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f); - if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3]) - continue; - c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4; - pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]); + int arrayindex = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].arrays[i]; + if (arrayindex >= DPSOFTRAST_ARRAY_TOTAL) + break; + switch (arrayindex) + { + case DPSOFTRAST_ARRAY_POSITION: + case DPSOFTRAST_ARRAY_COLOR: + break; + default: + if (dpsoftrast.pointer_texcoordf[arrayindex-DPSOFTRAST_ARRAY_TEXCOORD0] == NULL) + continue; + break; + } + arraymask[arrayindex] = true; + if (arrayindex > lastarray) + lastarray = arrayindex; } + DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, arraymask[DPSOFTRAST_ARRAY_COLOR]); + DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Vertex(); +// DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.screencoord4f, dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices); + DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numtriangles, element3i, element3s, arraymask, lastarray+1); } -void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s) +void DPSOFTRAST_Flush(void) { - unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL]; - arraymask[0] = true; - arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode) - arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL; - arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL; - arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL; - arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL; - arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL; - arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL; - arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL; - arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL; - DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW); - DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true); - DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Vertex(); - DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices); - DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numtriangles, element3i, element3s, arraymask); + DPSOFTRAST_Draw_SyncCommands(); + DPSOFTRAST_Draw_FlushThreads(); } -void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels) +void DPSOFTRAST_Init(int width, int height, int numthreads, unsigned int *colorpixels, unsigned int *depthpixels) { + int i; union { int i; @@ -4045,49 +4750,96 @@ void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned dpsoftrast.texture_firstfree = 1; dpsoftrast.texture_end = 1; dpsoftrast.texture_max = 0; - dpsoftrast.user.colormask[0] = 1; - dpsoftrast.user.colormask[1] = 1; - dpsoftrast.user.colormask[2] = 1; - dpsoftrast.user.colormask[3] = 1; - dpsoftrast.user.blendfunc[0] = GL_ONE; - dpsoftrast.user.blendfunc[1] = GL_ZERO; - dpsoftrast.user.depthmask = true; - dpsoftrast.user.depthtest = true; - dpsoftrast.user.depthfunc = GL_LEQUAL; - dpsoftrast.user.scissortest = false; - dpsoftrast.user.cullface = GL_BACK; - dpsoftrast.user.alphatest = false; - dpsoftrast.user.alphafunc = GL_GREATER; - dpsoftrast.user.alphavalue = 0.5f; - dpsoftrast.user.scissor[0] = 0; - dpsoftrast.user.scissor[1] = 0; - dpsoftrast.user.scissor[2] = dpsoftrast.fb_width; - dpsoftrast.user.scissor[3] = dpsoftrast.fb_height; - dpsoftrast.user.viewport[0] = 0; - dpsoftrast.user.viewport[1] = 0; - dpsoftrast.user.viewport[2] = dpsoftrast.fb_width; - dpsoftrast.user.viewport[3] = dpsoftrast.fb_height; - dpsoftrast.user.depthrange[0] = 0; - dpsoftrast.user.depthrange[1] = 1; - dpsoftrast.user.polygonoffset[0] = 0; - dpsoftrast.user.polygonoffset[1] = 0; - dpsoftrast.user.color[0] = 1; - dpsoftrast.user.color[1] = 1; - dpsoftrast.user.color[2] = 1; - dpsoftrast.user.color[3] = 1; - dpsoftrast.validate = -1; - DPSOFTRAST_Validate(-1); - dpsoftrast.validate = 0; + dpsoftrast.viewport[0] = 0; + dpsoftrast.viewport[1] = 0; + dpsoftrast.viewport[2] = dpsoftrast.fb_width; + dpsoftrast.viewport[3] = dpsoftrast.fb_height; + dpsoftrast.color[0] = 1; + dpsoftrast.color[1] = 1; + dpsoftrast.color[2] = 1; + dpsoftrast.color[3] = 1; + dpsoftrast.cullface = GL_BACK; +#ifdef USETHREADS + dpsoftrast.numthreads = bound(1, numthreads, 64); + dpsoftrast.trianglemutex = SDL_CreateMutex(); + dpsoftrast.trianglecond = SDL_CreateCond(); +#else + dpsoftrast.numthreads = 1; +#endif + dpsoftrast.threads = (DPSOFTRAST_State_Thread *)MM_CALLOC(dpsoftrast.numthreads, sizeof(DPSOFTRAST_State_Thread)); + for (i = 0; i < dpsoftrast.numthreads; i++) + { + DPSOFTRAST_State_Thread *thread = &dpsoftrast.threads[i]; + thread->index = i; + thread->colormask[1] = 1; + thread->colormask[2] = 1; + thread->colormask[3] = 1; + thread->blendfunc[0] = GL_ONE; + thread->blendfunc[1] = GL_ZERO; + thread->depthmask = true; + thread->depthtest = true; + thread->depthfunc = GL_LEQUAL; + thread->scissortest = false; + thread->alphatest = false; + thread->alphafunc = GL_GREATER; + thread->alphavalue = 0.5f; + thread->scissor[0] = 0; + thread->scissor[1] = 0; + thread->scissor[2] = dpsoftrast.fb_width; + thread->scissor[3] = dpsoftrast.fb_height; + thread->depthrange[0] = 0; + thread->depthrange[1] = 1; + thread->polygonoffset[0] = 0; + thread->polygonoffset[1] = 0; + + thread->numspans = 0; + thread->triangleoffset = 0; + thread->commandoffset = 0; + thread->waiting = false; +#ifdef USETHREADS + thread->waitcond = SDL_CreateCond(); +#endif + + thread->validate = -1; + DPSOFTRAST_Validate(thread, -1); +#ifdef USETHREADS + thread->thread = SDL_CreateThread(DPSOFTRAST_Draw_Thread, thread); +#endif + } } void DPSOFTRAST_Shutdown(void) { int i; +#ifdef USETHREADS + if(dpsoftrast.numthreads > 0) + { + DPSOFTRAST_State_Thread *thread; + SDL_LockMutex(dpsoftrast.trianglemutex); + for (i = 0; i < dpsoftrast.numthreads; i++) + { + thread = &dpsoftrast.threads[i]; + thread->index = -1; + } + SDL_CondBroadcast(dpsoftrast.trianglecond); + SDL_UnlockMutex(dpsoftrast.trianglemutex); + for (i = 0; i < dpsoftrast.numthreads; i++) + { + thread = &dpsoftrast.threads[i]; + SDL_WaitThread(thread->thread, NULL); + SDL_DestroyCond(thread->waitcond); + } + SDL_DestroyMutex(dpsoftrast.trianglemutex); + SDL_DestroyCond(dpsoftrast.trianglecond); + } +#endif for (i = 0;i < dpsoftrast.texture_end;i++) if (dpsoftrast.texture[i].bytes) MM_FREE(dpsoftrast.texture[i].bytes); if (dpsoftrast.texture) free(dpsoftrast.texture); + if (dpsoftrast.threads) + MM_FREE(dpsoftrast.threads); memset(&dpsoftrast, 0, sizeof(dpsoftrast)); } diff --git a/dpsoftrast.h b/dpsoftrast.h index ea626113..f5db67b1 100644 --- a/dpsoftrast.h +++ b/dpsoftrast.h @@ -6,7 +6,7 @@ #define DPSOFTRAST_MAXMIPMAPS 16 #define DPSOFTRAST_TEXTURE_MAXSIZE (1<<(DPSOFTRAST_MAXMIPMAPS - 1)) -#define DPSOFTRAST_MAXTEXTUREUNITS 32 +#define DPSOFTRAST_MAXTEXTUREUNITS 16 #define DPSOFTRAST_MAXTEXCOORDARRAYS 8 // type of pixels in texture (some of these are converted to BGRA8 on update) @@ -31,8 +31,9 @@ typedef enum DPSOFTRAST_TEXTURE_FILTER_e } DPSOFTRAST_TEXTURE_FILTER; -void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels); +void DPSOFTRAST_Init(int width, int height, int numthreads, unsigned int *colorpixels, unsigned int *depthpixels); void DPSOFTRAST_Shutdown(void); +void DPSOFTRAST_Flush(void); int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth); void DPSOFTRAST_Texture_Free(int index); @@ -60,7 +61,7 @@ void DPSOFTRAST_DepthFunc(int comparemode); void DPSOFTRAST_DepthRange(float range0, float range1); void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview); void DPSOFTRAST_CullFace(int mode); -void DPSOFTRAST_AlphaTest(float enable); +void DPSOFTRAST_AlphaTest(int enable); void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue); void DPSOFTRAST_Color4f(float r, float g, float b, float a); void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels); @@ -290,14 +291,14 @@ typedef enum DPSOFTRAST_UNIFORM_e } DPSOFTRAST_UNIFORM; -void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation); -#define DPSOFTRAST_Uniform1fARB(index, v0) DPSOFTRAST_Uniform4fARB(index, v0, 0, 0, 0) -#define DPSOFTRAST_Uniform2fARB(index, v0, v1) DPSOFTRAST_Uniform4fARB(index, v0, v1, 0, 0) -#define DPSOFTRAST_Uniform3fARB(index, v0, v1, v2) DPSOFTRAST_Uniform4fARB(index, v0, v1, v2, 0) -void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3); -void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v); -void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v); -void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0); +void DPSOFTRAST_SetShader(int mode, int permutation); +#define DPSOFTRAST_Uniform1f(index, v0) DPSOFTRAST_Uniform4f(index, v0, 0, 0, 0) +#define DPSOFTRAST_Uniform2f(index, v0, v1) DPSOFTRAST_Uniform4f(index, v0, v1, 0, 0) +#define DPSOFTRAST_Uniform3f(index, v0, v1, v2) DPSOFTRAST_Uniform4f(index, v0, v1, v2, 0) +void DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3); +void DPSOFTRAST_Uniform4fv(DPSOFTRAST_UNIFORM index, const float *v); +void DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v); +void DPSOFTRAST_Uniform1i(DPSOFTRAST_UNIFORM index, int i0); void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s); diff --git a/gl_rmain.c b/gl_rmain.c index e3cd3bda..184740a4 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -4937,9 +4937,9 @@ void R_SetupShader_SetPermutationHLSL(unsigned int mode, unsigned int permutatio void R_SetupShader_SetPermutationSoft(unsigned int mode, unsigned int permutation) { DPSOFTRAST_SetShader(mode, permutation); - DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1, 1, false, gl_modelviewprojection16f); - DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_ModelViewMatrixM1, 1, false, gl_modelview16f); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_ClientTime, cl.time); + DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1, 1, false, gl_modelviewprojection16f); + DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelViewMatrixM1, 1, false, gl_modelview16f); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_ClientTime, cl.time); } void R_GLSL_Restart_f(void) @@ -6164,89 +6164,89 @@ void R_SetupShader_Surface(const vec3_t lightcolorbase, qboolean modellighting, RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | (rsurface.modellightmapcolor4f ? BATCHNEED_ARRAY_VERTEXCOLOR : 0) | BATCHNEED_ARRAY_TEXCOORD | (rsurface.uselightmaptexture ? BATCHNEED_ARRAY_LIGHTMAP : 0), texturenumsurfaces, texturesurfacelist); R_Mesh_PrepareVertices_Mesh_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchsvector3f, rsurface.batchtvector3f, rsurface.batchnormal3f, rsurface.batchlightmapcolor4f, rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordlightmap2f); R_SetupShader_SetPermutationSoft(mode, permutation); - {Matrix4x4_ToArrayFloatGL(&rsurface.matrix, m16f);DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_ModelToReflectCubeM1, 1, false, m16f);} + {Matrix4x4_ToArrayFloatGL(&rsurface.matrix, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelToReflectCubeM1, 1, false, m16f);} if (mode == SHADERMODE_LIGHTSOURCE) { - {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_ModelToLightM1, 1, false, m16f);} - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Ambient, colormod[0] * ambientscale, colormod[1] * ambientscale, colormod[2] * ambientscale); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Diffuse, colormod[0] * diffusescale, colormod[1] * diffusescale, colormod[2] * diffusescale); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale); + {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelToLightM1, 1, false, m16f);} + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Ambient, colormod[0] * ambientscale, colormod[1] * ambientscale, colormod[2] * ambientscale); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Diffuse, colormod[0] * diffusescale, colormod[1] * diffusescale, colormod[2] * diffusescale); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale); // additive passes are only darkened by fog, not tinted - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_FogColor, 0, 0, 0); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f)); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_FogColor, 0, 0, 0); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f)); } else { if (mode == SHADERMODE_FLATCOLOR) { - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Ambient, colormod[0], colormod[1], colormod[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Ambient, colormod[0], colormod[1], colormod[2]); } else if (mode == SHADERMODE_LIGHTDIRECTION) { - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Ambient, (r_refdef.scene.ambient + rsurface.modellight_ambient[0] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[0], (r_refdef.scene.ambient + rsurface.modellight_ambient[1] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[1], (r_refdef.scene.ambient + rsurface.modellight_ambient[2] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[2]); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Diffuse, r_refdef.lightmapintensity * colormod[0], r_refdef.lightmapintensity * colormod[1], r_refdef.lightmapintensity * colormod[2]); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_DeferredMod_Diffuse, colormod[0] * r_shadow_deferred_8bitrange.value, colormod[1] * r_shadow_deferred_8bitrange.value, colormod[2] * r_shadow_deferred_8bitrange.value); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_DeferredMod_Specular, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_LightColor, rsurface.modellight_diffuse[0] * r_refdef.scene.rtlightstylevalue[0], rsurface.modellight_diffuse[1] * r_refdef.scene.rtlightstylevalue[0], rsurface.modellight_diffuse[2] * r_refdef.scene.rtlightstylevalue[0]); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Ambient, (r_refdef.scene.ambient + rsurface.modellight_ambient[0] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[0], (r_refdef.scene.ambient + rsurface.modellight_ambient[1] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[1], (r_refdef.scene.ambient + rsurface.modellight_ambient[2] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Diffuse, r_refdef.lightmapintensity * colormod[0], r_refdef.lightmapintensity * colormod[1], r_refdef.lightmapintensity * colormod[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_DeferredMod_Diffuse, colormod[0] * r_shadow_deferred_8bitrange.value, colormod[1] * r_shadow_deferred_8bitrange.value, colormod[2] * r_shadow_deferred_8bitrange.value); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_DeferredMod_Specular, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_LightColor, rsurface.modellight_diffuse[0] * r_refdef.scene.rtlightstylevalue[0], rsurface.modellight_diffuse[1] * r_refdef.scene.rtlightstylevalue[0], rsurface.modellight_diffuse[2] * r_refdef.scene.rtlightstylevalue[0]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]); } else { - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Ambient, r_refdef.scene.ambient * colormod[0], r_refdef.scene.ambient * colormod[1], r_refdef.scene.ambient * colormod[2]); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Diffuse, rsurface.texture->lightmapcolor[0], rsurface.texture->lightmapcolor[1], rsurface.texture->lightmapcolor[2]); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_DeferredMod_Diffuse, colormod[0] * diffusescale * r_shadow_deferred_8bitrange.value, colormod[1] * diffusescale * r_shadow_deferred_8bitrange.value, colormod[2] * diffusescale * r_shadow_deferred_8bitrange.value); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_DeferredMod_Specular, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Ambient, r_refdef.scene.ambient * colormod[0], r_refdef.scene.ambient * colormod[1], r_refdef.scene.ambient * colormod[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Diffuse, rsurface.texture->lightmapcolor[0], rsurface.texture->lightmapcolor[1], rsurface.texture->lightmapcolor[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_DeferredMod_Diffuse, colormod[0] * diffusescale * r_shadow_deferred_8bitrange.value, colormod[1] * diffusescale * r_shadow_deferred_8bitrange.value, colormod[2] * diffusescale * r_shadow_deferred_8bitrange.value); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_DeferredMod_Specular, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value); } // additive passes are only darkened by fog, not tinted if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD) - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_FogColor, 0, 0, 0); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_FogColor, 0, 0, 0); else - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_DistortScaleRefractReflect, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_ScreenScaleRefractReflect, r_waterstate.screenscale[0], r_waterstate.screenscale[1], r_waterstate.screenscale[0], r_waterstate.screenscale[1]); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_ScreenCenterRefractReflect, r_waterstate.screencenter[0], r_waterstate.screencenter[1], r_waterstate.screencenter[0], r_waterstate.screencenter[1]); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_RefractColor, rsurface.texture->refractcolor4f[0], rsurface.texture->refractcolor4f[1], rsurface.texture->refractcolor4f[2], rsurface.texture->refractcolor4f[3] * rsurface.texture->lightmapcolor[3]); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_ReflectColor, rsurface.texture->reflectcolor4f[0], rsurface.texture->reflectcolor4f[1], rsurface.texture->reflectcolor4f[2], rsurface.texture->reflectcolor4f[3] * rsurface.texture->lightmapcolor[3]); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_ReflectOffset, rsurface.texture->reflectmin); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f)); - DPSOFTRAST_Uniform2fARB(DPSOFTRAST_UNIFORM_NormalmapScrollBlend, rsurface.texture->r_water_waterscroll[0], rsurface.texture->r_water_waterscroll[1]); - } - {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currenttexmatrix, m16f);DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_TexMatrixM1, 1, false, m16f);} - {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currentbackgroundtexmatrix, m16f);DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_BackgroundTexMatrixM1, 1, false, m16f);} - {Matrix4x4_ToArrayFloatGL(&r_shadow_shadowmapmatrix, m16f);DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_ShadowMapMatrixM1, 1, false, m16f);} - DPSOFTRAST_Uniform2fARB(DPSOFTRAST_UNIFORM_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]); - - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Glow, rsurface.glowmod[0], rsurface.glowmod[1], rsurface.glowmod[2]); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_Alpha, rsurface.texture->lightmapcolor[3] * ((rsurface.texture->basematerialflags & MATERIALFLAG_WATERSHADER && r_waterstate.enabled && !r_refdef.view.isoverlay) ? rsurface.texture->r_water_wateralpha : 1)); - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_DistortScaleRefractReflect, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ScreenScaleRefractReflect, r_waterstate.screenscale[0], r_waterstate.screenscale[1], r_waterstate.screenscale[0], r_waterstate.screenscale[1]); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ScreenCenterRefractReflect, r_waterstate.screencenter[0], r_waterstate.screencenter[1], r_waterstate.screencenter[0], r_waterstate.screencenter[1]); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_RefractColor, rsurface.texture->refractcolor4f[0], rsurface.texture->refractcolor4f[1], rsurface.texture->refractcolor4f[2], rsurface.texture->refractcolor4f[3] * rsurface.texture->lightmapcolor[3]); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ReflectColor, rsurface.texture->reflectcolor4f[0], rsurface.texture->reflectcolor4f[1], rsurface.texture->reflectcolor4f[2], rsurface.texture->reflectcolor4f[3] * rsurface.texture->lightmapcolor[3]); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_ReflectOffset, rsurface.texture->reflectmin); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f)); + DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_NormalmapScrollBlend, rsurface.texture->r_water_waterscroll[0], rsurface.texture->r_water_waterscroll[1]); + } + {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currenttexmatrix, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_TexMatrixM1, 1, false, m16f);} + {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currentbackgroundtexmatrix, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_BackgroundTexMatrixM1, 1, false, m16f);} + {Matrix4x4_ToArrayFloatGL(&r_shadow_shadowmapmatrix, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ShadowMapMatrixM1, 1, false, m16f);} + DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]); + + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Glow, rsurface.glowmod[0], rsurface.glowmod[1], rsurface.glowmod[2]); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_Alpha, rsurface.texture->lightmapcolor[3] * ((rsurface.texture->basematerialflags & MATERIALFLAG_WATERSHADER && r_waterstate.enabled && !r_refdef.view.isoverlay) ? rsurface.texture->r_water_wateralpha : 1)); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]); if (DPSOFTRAST_UNIFORM_Color_Pants >= 0) { if (rsurface.texture->pantstexture) - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]); else - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Pants, 0, 0, 0); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Pants, 0, 0, 0); } if (DPSOFTRAST_UNIFORM_Color_Shirt >= 0) { if (rsurface.texture->shirttexture) - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]); else - DPSOFTRAST_Uniform3fARB(DPSOFTRAST_UNIFORM_Color_Shirt, 0, 0, 0); + DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Shirt, 0, 0, 0); } - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_FogPlaneViewDist, rsurface.fogplaneviewdist); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_FogRangeRecip, rsurface.fograngerecip); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_FogHeightFade, rsurface.fogheightfade); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value*rsurface.texture->offsetscale); - DPSOFTRAST_Uniform2fARB(DPSOFTRAST_UNIFORM_ScreenToDepth, r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]); - DPSOFTRAST_Uniform2fARB(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_FogPlaneViewDist, rsurface.fogplaneviewdist); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_FogRangeRecip, rsurface.fograngerecip); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_FogHeightFade, rsurface.fogheightfade); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value*rsurface.texture->offsetscale); + DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_ScreenToDepth, r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]); + DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height); R_Mesh_TexBind(GL20TU_NORMAL , rsurface.texture->nmaptexture ); R_Mesh_TexBind(GL20TU_COLOR , rsurface.texture->basetexture ); @@ -6413,16 +6413,16 @@ void R_SetupShader_DeferredLight(const rtlight_t *rtlight) break; case RENDERPATH_SOFT: R_SetupShader_SetPermutationGLSL(mode, permutation); - DPSOFTRAST_Uniform3fARB( DPSOFTRAST_UNIFORM_LightPosition , viewlightorigin[0], viewlightorigin[1], viewlightorigin[2]); - DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_ViewToLightM1 , 1, false, viewtolight16f); - DPSOFTRAST_Uniform3fARB( DPSOFTRAST_UNIFORM_DeferredColor_Ambient , lightcolorbase[0] * ambientscale * range, lightcolorbase[1] * ambientscale * range, lightcolorbase[2] * ambientscale * range); - DPSOFTRAST_Uniform3fARB( DPSOFTRAST_UNIFORM_DeferredColor_Diffuse , lightcolorbase[0] * diffusescale * range, lightcolorbase[1] * diffusescale * range, lightcolorbase[2] * diffusescale * range); - DPSOFTRAST_Uniform3fARB( DPSOFTRAST_UNIFORM_DeferredColor_Specular , lightcolorbase[0] * specularscale * range, lightcolorbase[1] * specularscale * range, lightcolorbase[2] * specularscale * range); - DPSOFTRAST_Uniform2fARB( DPSOFTRAST_UNIFORM_ShadowMap_TextureScale , r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]); - DPSOFTRAST_Uniform4fARB( DPSOFTRAST_UNIFORM_ShadowMap_Parameters , r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]); - DPSOFTRAST_Uniform1fARB( DPSOFTRAST_UNIFORM_SpecularPower , (r_shadow_gloss.integer == 2 ? r_shadow_gloss2exponent.value : r_shadow_glossexponent.value) * (r_shadow_glossexact.integer ? 0.25f : 1.0f)); - DPSOFTRAST_Uniform2fARB( DPSOFTRAST_UNIFORM_ScreenToDepth , r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]); - DPSOFTRAST_Uniform2fARB(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height); + DPSOFTRAST_Uniform3f( DPSOFTRAST_UNIFORM_LightPosition , viewlightorigin[0], viewlightorigin[1], viewlightorigin[2]); + DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ViewToLightM1 , 1, false, viewtolight16f); + DPSOFTRAST_Uniform3f( DPSOFTRAST_UNIFORM_DeferredColor_Ambient , lightcolorbase[0] * ambientscale * range, lightcolorbase[1] * ambientscale * range, lightcolorbase[2] * ambientscale * range); + DPSOFTRAST_Uniform3f( DPSOFTRAST_UNIFORM_DeferredColor_Diffuse , lightcolorbase[0] * diffusescale * range, lightcolorbase[1] * diffusescale * range, lightcolorbase[2] * diffusescale * range); + DPSOFTRAST_Uniform3f( DPSOFTRAST_UNIFORM_DeferredColor_Specular , lightcolorbase[0] * specularscale * range, lightcolorbase[1] * specularscale * range, lightcolorbase[2] * specularscale * range); + DPSOFTRAST_Uniform2f( DPSOFTRAST_UNIFORM_ShadowMap_TextureScale , r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]); + DPSOFTRAST_Uniform4f( DPSOFTRAST_UNIFORM_ShadowMap_Parameters , r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]); + DPSOFTRAST_Uniform1f( DPSOFTRAST_UNIFORM_SpecularPower , (r_shadow_gloss.integer == 2 ? r_shadow_gloss2exponent.value : r_shadow_glossexponent.value) * (r_shadow_glossexact.integer ? 0.25f : 1.0f)); + DPSOFTRAST_Uniform2f( DPSOFTRAST_UNIFORM_ScreenToDepth , r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]); + DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height); R_Mesh_TexBind(GL20TU_ATTENUATION , r_shadow_attenuationgradienttexture ); R_Mesh_TexBind(GL20TU_SCREENDEPTH , r_shadow_prepassgeometrydepthtexture ); @@ -8591,8 +8591,8 @@ void R_EntityMatrix(const matrix4x4_t *matrix) qglLoadMatrixf(gl_modelview16f);CHECKGLERROR break; case RENDERPATH_SOFT: - DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1, 1, false, gl_modelviewprojection16f); - DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM_ModelViewMatrixM1, 1, false, gl_modelview16f); + DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1, 1, false, gl_modelviewprojection16f); + DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelViewMatrixM1, 1, false, gl_modelview16f); break; } } @@ -9578,15 +9578,15 @@ static void R_BlendView(void) R_Mesh_TexBind(GL20TU_FIRST , r_bloomstate.texture_screen); R_Mesh_TexBind(GL20TU_SECOND , r_bloomstate.texture_bloom ); R_Mesh_TexBind(GL20TU_GAMMARAMPS, r_texture_gammaramps ); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_ViewTintColor , r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]); - DPSOFTRAST_Uniform2fARB(DPSOFTRAST_UNIFORM_PixelSize , 1.0/r_bloomstate.screentexturewidth, 1.0/r_bloomstate.screentextureheight); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_UserVec1 , uservecs[0][0], uservecs[0][1], uservecs[0][2], uservecs[0][3]); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_UserVec2 , uservecs[1][0], uservecs[1][1], uservecs[1][2], uservecs[1][3]); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_UserVec3 , uservecs[2][0], uservecs[2][1], uservecs[2][2], uservecs[2][3]); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_UserVec4 , uservecs[3][0], uservecs[3][1], uservecs[3][2], uservecs[3][3]); - DPSOFTRAST_Uniform1fARB(DPSOFTRAST_UNIFORM_Saturation , r_glsl_saturation.value); - DPSOFTRAST_Uniform2fARB(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height); - DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM_BloomColorSubtract , r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 0.0f); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ViewTintColor , r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]); + DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_PixelSize , 1.0/r_bloomstate.screentexturewidth, 1.0/r_bloomstate.screentextureheight); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_UserVec1 , uservecs[0][0], uservecs[0][1], uservecs[0][2], uservecs[0][3]); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_UserVec2 , uservecs[1][0], uservecs[1][1], uservecs[1][2], uservecs[1][3]); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_UserVec3 , uservecs[2][0], uservecs[2][1], uservecs[2][2], uservecs[2][3]); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_UserVec4 , uservecs[3][0], uservecs[3][1], uservecs[3][2], uservecs[3][3]); + DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_Saturation , r_glsl_saturation.value); + DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height); + DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_BloomColorSubtract , r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 0.0f); break; default: break; diff --git a/vid_sdl.c b/vid_sdl.c index 9c682648..e0d82af4 100644 --- a/vid_sdl.c +++ b/vid_sdl.c @@ -67,6 +67,7 @@ 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", "1", "the number of threads the DarkPlaces Software Rasterizer should use"}; 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"}; @@ -692,6 +693,7 @@ void VID_Init (void) #endif #endif Cvar_RegisterVariable(&vid_soft); + Cvar_RegisterVariable(&vid_soft_threads); Cvar_RegisterVariable(&joy_detected); Cvar_RegisterVariable(&joy_enable); Cvar_RegisterVariable(&joy_index); @@ -1201,7 +1203,7 @@ qboolean VID_InitModeSoft(viddef_mode_t *mode) vid.softpixels = (unsigned int *)vid_softsurface->pixels; vid.softdepthpixels = (unsigned int *)calloc(1, mode->width * mode->height * 4); - DPSOFTRAST_Init(mode->width, mode->height, (unsigned int *)vid_softsurface->pixels, (unsigned int *)vid.softdepthpixels); + DPSOFTRAST_Init(mode->width, mode->height, vid_soft_threads.integer, (unsigned int *)vid_softsurface->pixels, (unsigned int *)vid.softdepthpixels); // set window title VID_SetCaption(); @@ -1391,6 +1393,7 @@ void VID_Finish (void) SDL_GL_SwapBuffers(); break; case RENDERPATH_SOFT: + DPSOFTRAST_Flush(); SDL_BlitSurface(vid_softsurface, NULL, screen, NULL); SDL_Flip(screen); break; -- 2.39.2