X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=dpsoftrast.c;h=86d4f7b5cbc7d8a5da85e155a23124358cff7e9e;hb=bd172244c077be2f4f13caf8cabd022fcad4bd33;hp=483378baadaae55a605e260b3d9eca877d7951cf;hpb=2105e04af182c4271a02168b05ac08bfcf8a51e2;p=xonotic%2Fdarkplaces.git diff --git a/dpsoftrast.c b/dpsoftrast.c index 483378ba..86d4f7b5 100644 --- a/dpsoftrast.c +++ b/dpsoftrast.c @@ -27,6 +27,20 @@ typedef qboolean bool; #define ATOMIC_INCREMENT(counter) (OSAtomicIncrement32Barrier(&(counter))) #define ATOMIC_DECREMENT(counter) (OSAtomicDecrement32Barrier(&(counter))) #define ATOMIC_ADD(counter, val) ((void)OSAtomicAdd32Barrier((val), &(counter))) + #elif defined(__GNUC__) && defined(WIN32) + #define ALIGN(var) var __attribute__((__aligned__(16))) + #define ATOMIC(var) var __attribute__((__aligned__(32))) + #define MEMORY_BARRIER (_mm_sfence()) + //(__sync_synchronize()) + #define ATOMIC_COUNTER volatile LONG + // this LONG * cast serves to fix an issue with broken mingw + // packages on Ubuntu; these only declare the function to take + // a LONG *, causing a compile error here. This seems to be + // error- and warn-free on platforms that DO declare + // InterlockedIncrement correctly, like mingw on Windows. + #define ATOMIC_INCREMENT(counter) (InterlockedIncrement((LONG *) &(counter))) + #define ATOMIC_DECREMENT(counter) (InterlockedDecrement((LONG *) &(counter))) + #define ATOMIC_ADD(counter, val) ((void)InterlockedExchangeAdd((LONG *) &(counter), (val))) #elif defined(__GNUC__) #define ALIGN(var) var __attribute__((__aligned__(16))) #define ATOMIC(var) var __attribute__((__aligned__(32))) @@ -73,7 +87,7 @@ typedef qboolean bool; #ifdef SSE_POSSIBLE #include -#if defined(__GNUC__) && (__GNUC < 4 || __GNUC_MINOR__ < 6) +#if defined(__GNUC__) && (__GNUC < 4 || __GNUC_MINOR__ < 6) && !defined(__clang__) #define _mm_cvtss_f32(val) (__builtin_ia32_vec_ext_v4sf ((__v4sf)(val), 0)) #endif @@ -2297,166 +2311,63 @@ void DPSOFTRAST_Draw_Span_FinishBGRA8(DPSOFTRAST_State_Thread *thread, const DPS #endif } -static void DPSOFTRAST_Texture2D(DPSOFTRAST_Texture *texture, int mip, float x, float y, float c[4]) - // warning: this is SLOW, only use if the optimized per-span functions won't do - // FIXME does this function need flipping of the color order? -{ - const unsigned char * RESTRICT pixelbase; - const unsigned char * RESTRICT pixel[4]; - int tciwrapmask[2]; - tciwrapmask[0] = texture->mipmap[mip][2]-1; - tciwrapmask[1] = texture->mipmap[mip][3]-1; - pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0]; - if(texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR) - { - if (texture->flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE) - { - unsigned int tc[2] = { x * (texture->mipmap[mip][2]<<12) - 2048, y * (texture->mipmap[mip][3]<<12) - 2048}; - unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF }; - unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] }; - unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] }; - int tci[2] = { tc[0]>>12, tc[1]>>12 }; - int tci1[2] = { tci[0] + 1, tci[1] + 1 }; - tci[0] = tci[0] >= 0 ? (tci[0] <= texture->mipmap[mip][2]-1 ? tci[0] : texture->mipmap[mip][2]-1) : 0; - tci[1] = tci[1] >= 0 ? (tci[1] <= texture->mipmap[mip][3]-1 ? tci[1] : texture->mipmap[mip][3]-1) : 0; - tci1[0] = tci1[0] >= 0 ? (tci1[0] <= texture->mipmap[mip][2]-1 ? tci1[0] : texture->mipmap[mip][2]-1) : 0; - tci1[1] = tci1[1] >= 0 ? (tci1[1] <= texture->mipmap[mip][3]-1 ? tci1[1] : texture->mipmap[mip][3]-1) : 0; - pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]); - pixel[1] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci1[0]); - pixel[2] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci[0]); - pixel[3] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci1[0]); - c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF00000); - c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF00000); - c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF00000); - c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF00000); - } - else - { - unsigned int tc[2] = { x * (texture->mipmap[mip][2]<<12) - 2048, y * (texture->mipmap[mip][3]<<12) - 2048}; - unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF }; - unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] }; - unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] }; - int tci[2] = { tc[0]>>12, tc[1]>>12 }; - int tci1[2] = { tci[0] + 1, tci[1] + 1 }; - tci[0] &= tciwrapmask[0]; - tci[1] &= tciwrapmask[1]; - tci1[0] &= tciwrapmask[0]; - tci1[1] &= tciwrapmask[1]; - pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]); - pixel[1] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci1[0]); - pixel[2] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci[0]); - pixel[3] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci1[0]); - c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF00000); - c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF00000); - c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF00000); - c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF00000); - } - } - else - { - if (texture->flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE) - { - int tci[2] = { x * texture->mipmap[mip][2], y * texture->mipmap[mip][3] }; - tci[0] = tci[0] >= 0 ? (tci[0] <= texture->mipmap[mip][2]-1 ? tci[0] : texture->mipmap[mip][2]-1) : 0; - tci[1] = tci[1] >= 0 ? (tci[1] <= texture->mipmap[mip][3]-1 ? tci[1] : texture->mipmap[mip][3]-1) : 0; - pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]); - c[0] = pixel[0][0] * (1.0f / 255.0f); - c[1] = pixel[0][1] * (1.0f / 255.0f); - c[2] = pixel[0][2] * (1.0f / 255.0f); - c[3] = pixel[0][3] * (1.0f / 255.0f); - } - else - { - int tci[2] = { x * texture->mipmap[mip][2], y * texture->mipmap[mip][3] }; - tci[0] &= tciwrapmask[0]; - tci[1] &= tciwrapmask[1]; - pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]); - c[0] = pixel[0][0] * (1.0f / 255.0f); - c[1] = pixel[0][1] * (1.0f / 255.0f); - c[2] = pixel[0][2] * (1.0f / 255.0f); - c[3] = pixel[0][3] * (1.0f / 255.0f); - } - } -} - static void DPSOFTRAST_Texture2DBGRA8(DPSOFTRAST_Texture *texture, int mip, float x, float y, unsigned char c[4]) // warning: this is SLOW, only use if the optimized per-span functions won't do { const unsigned char * RESTRICT pixelbase; const unsigned char * RESTRICT pixel[4]; - int tciwrapmask[2]; - tciwrapmask[0] = texture->mipmap[mip][2]-1; - tciwrapmask[1] = texture->mipmap[mip][3]-1; + int width = texture->mipmap[mip][2], height = texture->mipmap[mip][3]; + int wrapmask[2] = { width-1, height-1 }; pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0]; if(texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR) { + unsigned int tc[2] = { x * (width<<12) - 2048, y * (height<<12) - 2048}; + unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF }; + unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] }; + unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] }; + int tci[2] = { tc[0]>>12, tc[1]>>12 }; + int tci1[2] = { tci[0] + 1, tci[1] + 1 }; if (texture->flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE) { - unsigned int tc[2] = { x * (texture->mipmap[mip][2]<<12) - 2048, y * (texture->mipmap[mip][3]<<12) - 2048}; - unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF }; - unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] }; - unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] }; - int tci[2] = { tc[0]>>12, tc[1]>>12 }; - int tci1[2] = { tci[0] + 1, tci[1] + 1 }; - tci[0] = tci[0] >= 0 ? (tci[0] <= texture->mipmap[mip][2]-1 ? tci[0] : texture->mipmap[mip][2]-1) : 0; - tci[1] = tci[1] >= 0 ? (tci[1] <= texture->mipmap[mip][3]-1 ? tci[1] : texture->mipmap[mip][3]-1) : 0; - tci1[0] = tci1[0] >= 0 ? (tci1[0] <= texture->mipmap[mip][2]-1 ? tci1[0] : texture->mipmap[mip][2]-1) : 0; - tci1[1] = tci1[1] >= 0 ? (tci1[1] <= texture->mipmap[mip][3]-1 ? tci1[1] : texture->mipmap[mip][3]-1) : 0; - pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]); - pixel[1] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci1[0]); - pixel[2] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci[0]); - pixel[3] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci1[0]); - c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3])>>24; - c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3])>>24; - c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3])>>24; - c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3])>>24; + tci[0] = tci[0] >= 0 ? (tci[0] <= wrapmask[0] ? tci[0] : wrapmask[0]) : 0; + tci[1] = tci[1] >= 0 ? (tci[1] <= wrapmask[1] ? tci[1] : wrapmask[1]) : 0; + tci1[0] = tci1[0] >= 0 ? (tci1[0] <= wrapmask[0] ? tci1[0] : wrapmask[0]) : 0; + tci1[1] = tci1[1] >= 0 ? (tci1[1] <= wrapmask[1] ? tci1[1] : wrapmask[1]) : 0; } else { - unsigned int tc[2] = { x * (texture->mipmap[mip][2]<<12) - 2048, y * (texture->mipmap[mip][3]<<12) - 2048}; - unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF }; - unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] }; - unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] }; - int tci[2] = { tc[0]>>12, tc[1]>>12 }; - int tci1[2] = { tci[0] + 1, tci[1] + 1 }; - tci[0] &= tciwrapmask[0]; - tci[1] &= tciwrapmask[1]; - tci1[0] &= tciwrapmask[0]; - tci1[1] &= tciwrapmask[1]; - pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]); - pixel[1] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci1[0]); - pixel[2] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci[0]); - pixel[3] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci1[0]); - c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3])>>24; - c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3])>>24; - c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3])>>24; - c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3])>>24; + tci[0] &= wrapmask[0]; + tci[1] &= wrapmask[1]; + tci1[0] &= wrapmask[0]; + tci1[1] &= wrapmask[1]; } + pixel[0] = pixelbase + 4 * (tci[1]*width+tci[0]); + pixel[1] = pixelbase + 4 * (tci[1]*width+tci1[0]); + pixel[2] = pixelbase + 4 * (tci1[1]*width+tci[0]); + pixel[3] = pixelbase + 4 * (tci1[1]*width+tci1[0]); + c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3])>>24; + c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3])>>24; + c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3])>>24; + c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3])>>24; } else { + int tci[2] = { x * width, y * height }; if (texture->flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE) { - int tci[2] = { x * texture->mipmap[mip][2], y * texture->mipmap[mip][3] }; - tci[0] = tci[0] >= 0 ? (tci[0] <= texture->mipmap[mip][2]-1 ? tci[0] : texture->mipmap[mip][2]-1) : 0; - tci[1] = tci[1] >= 0 ? (tci[1] <= texture->mipmap[mip][3]-1 ? tci[1] : texture->mipmap[mip][3]-1) : 0; - pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]); - c[0] = pixel[0][0]; - c[1] = pixel[0][1]; - c[2] = pixel[0][2]; - c[3] = pixel[0][3]; + tci[0] = tci[0] >= 0 ? (tci[0] <= wrapmask[0] ? tci[0] : wrapmask[0]) : 0; + tci[1] = tci[1] >= 0 ? (tci[1] <= wrapmask[1] ? tci[1] : wrapmask[1]) : 0; } else { - int tci[2] = { x * texture->mipmap[mip][2], y * texture->mipmap[mip][3] }; - tci[0] &= tciwrapmask[0]; - tci[1] &= tciwrapmask[1]; - pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]); - c[0] = pixel[0][0]; - c[1] = pixel[0][1]; - c[2] = pixel[0][2]; - c[3] = pixel[0][3]; + tci[0] &= wrapmask[0]; + tci[1] &= wrapmask[1]; } + pixel[0] = pixelbase + 4 * (tci[1]*width+tci[0]); + c[0] = pixel[0][0]; + c[1] = pixel[0][1]; + c[2] = pixel[0][2]; + c[3] = pixel[0][3]; } } @@ -4561,7 +4472,7 @@ void DPSOFTRAST_PixelShader_Refraction(DPSOFTRAST_State_Thread *thread, const DP DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); // read varyings - DPSOFTRAST_CALCATTRIB4F(triangle, span, ModelViewProjectionPositiondata, ModelViewProjectionPositionslope, DPSOFTRAST_ARRAY_TEXCOORD4); // or POSITION? + DPSOFTRAST_CALCATTRIB4F(triangle, span, ModelViewProjectionPositiondata, ModelViewProjectionPositionslope, DPSOFTRAST_ARRAY_TEXCOORD4); // read uniforms ScreenScaleRefractReflect[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenScaleRefractReflect*4+0]; @@ -4617,17 +4528,167 @@ void DPSOFTRAST_PixelShader_Refraction(DPSOFTRAST_State_Thread *thread, const DP void DPSOFTRAST_VertexShader_Water(void) { - DPSOFTRAST_Array_TransformProject(DPSOFTRAST_ARRAY_POSITION, DPSOFTRAST_ARRAY_POSITION, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + int i; + int numvertices = dpsoftrast.numvertices; + float EyePosition[4]; + float EyeVectorModelSpace[4]; + float EyeVector[4]; + float position[4]; + float svector[4]; + float tvector[4]; + float normal[4]; + EyePosition[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+0]; + 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_Load(DPSOFTRAST_ARRAY_POSITION, DPSOFTRAST_ARRAY_POSITION); + DPSOFTRAST_Array_Load(DPSOFTRAST_ARRAY_TEXCOORD1, DPSOFTRAST_ARRAY_TEXCOORD1); + DPSOFTRAST_Array_Load(DPSOFTRAST_ARRAY_TEXCOORD2, DPSOFTRAST_ARRAY_TEXCOORD2); + DPSOFTRAST_Array_Load(DPSOFTRAST_ARRAY_TEXCOORD3, DPSOFTRAST_ARRAY_TEXCOORD3); + for (i = 0;i < numvertices;i++) + { + position[0] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0]; + position[1] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1]; + position[2] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2]; + svector[0] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0]; + svector[1] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1]; + svector[2] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2]; + tvector[0] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0]; + tvector[1] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1]; + tvector[2] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2]; + normal[0] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0]; + normal[1] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1]; + normal[2] = dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2]; + 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.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD6][i*4+0] = EyeVector[0]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD6][i*4+1] = EyeVector[1]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD6][i*4+2] = EyeVector[2]; + dpsoftrast.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD6][i*4+3] = 0.0f; + } + DPSOFTRAST_Array_TransformProject(DPSOFTRAST_ARRAY_POSITION, -1, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Transform(DPSOFTRAST_ARRAY_TEXCOORD4, DPSOFTRAST_ARRAY_POSITION, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1); + DPSOFTRAST_Array_Transform(DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD0, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1); } 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]; + float z; + int x, startx = span->startx, endx = span->endx; + + // texture reads + unsigned char buffer_texture_normalbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4]; + + // varyings + float ModelViewProjectionPositiondata[4]; + float ModelViewProjectionPositionslope[4]; + float EyeVectordata[4]; + float EyeVectorslope[4]; + + // uniforms + float ScreenScaleRefractReflect[2]; + float ScreenCenterRefractReflect[2]; + float DistortScaleRefractReflect[2]; + float RefractColor[4]; + float ReflectColor[4]; + float ReflectFactor; + float ReflectOffset; + + DPSOFTRAST_Texture *texture_refraction = thread->texbound[GL20TU_REFRACTION]; + DPSOFTRAST_Texture *texture_reflection = thread->texbound[GL20TU_REFLECTION]; + if(!texture_refraction || !texture_reflection) return; + + // read textures DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z); - memset(buffer_FragColorbgra8 + span->startx*4, 0, (span->endx - span->startx)*4); + DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z); + + // read varyings + DPSOFTRAST_CALCATTRIB4F(triangle, span, ModelViewProjectionPositiondata, ModelViewProjectionPositionslope, DPSOFTRAST_ARRAY_TEXCOORD4); + DPSOFTRAST_CALCATTRIB4F(triangle, span, EyeVectordata, EyeVectorslope, DPSOFTRAST_ARRAY_TEXCOORD6); + + // read uniforms + ScreenScaleRefractReflect[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenScaleRefractReflect*4+0]; + ScreenScaleRefractReflect[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenScaleRefractReflect*4+1]; + ScreenScaleRefractReflect[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenScaleRefractReflect*4+2]; + ScreenScaleRefractReflect[3] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenScaleRefractReflect*4+3]; + ScreenCenterRefractReflect[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenCenterRefractReflect*4+0]; + ScreenCenterRefractReflect[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenCenterRefractReflect*4+1]; + ScreenCenterRefractReflect[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenCenterRefractReflect*4+2]; + ScreenCenterRefractReflect[3] = thread->uniform4f[DPSOFTRAST_UNIFORM_ScreenCenterRefractReflect*4+3]; + DistortScaleRefractReflect[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_DistortScaleRefractReflect*4+0]; + DistortScaleRefractReflect[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_DistortScaleRefractReflect*4+1]; + DistortScaleRefractReflect[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_DistortScaleRefractReflect*4+2]; + DistortScaleRefractReflect[3] = thread->uniform4f[DPSOFTRAST_UNIFORM_DistortScaleRefractReflect*4+3]; + RefractColor[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_RefractColor*4+2]; + RefractColor[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_RefractColor*4+1]; + RefractColor[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_RefractColor*4+0]; + RefractColor[3] = thread->uniform4f[DPSOFTRAST_UNIFORM_RefractColor*4+3]; + ReflectColor[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_ReflectColor*4+2]; + ReflectColor[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_ReflectColor*4+1]; + ReflectColor[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_ReflectColor*4+0]; + ReflectColor[3] = thread->uniform4f[DPSOFTRAST_UNIFORM_ReflectColor*4+3]; + ReflectFactor = thread->uniform4f[DPSOFTRAST_UNIFORM_ReflectFactor*4+0]; + ReflectOffset = thread->uniform4f[DPSOFTRAST_UNIFORM_ReflectOffset*4+0]; + + // do stuff + for (x = startx;x < endx;x++) + { + float SafeScreenTexCoord[4]; + float ScreenTexCoord[4]; + float v[3]; + float iw; + unsigned char c1[4]; + unsigned char c2[4]; + float Fresnel; + + z = buffer_z[x]; + + // " vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n" + iw = 1.0f / (ModelViewProjectionPositiondata[3] + ModelViewProjectionPositionslope[3]*x); // / z + + // " vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n" + SafeScreenTexCoord[0] = (ModelViewProjectionPositiondata[0] + ModelViewProjectionPositionslope[0]*x) * iw * ScreenScaleRefractReflect[0] + ScreenCenterRefractReflect[0]; // * z (disappears) + SafeScreenTexCoord[1] = (ModelViewProjectionPositiondata[1] + ModelViewProjectionPositionslope[1]*x) * iw * ScreenScaleRefractReflect[1] + ScreenCenterRefractReflect[1]; // * z (disappears) + SafeScreenTexCoord[2] = (ModelViewProjectionPositiondata[0] + ModelViewProjectionPositionslope[0]*x) * iw * ScreenScaleRefractReflect[2] + ScreenCenterRefractReflect[2]; // * z (disappears) + SafeScreenTexCoord[3] = (ModelViewProjectionPositiondata[1] + ModelViewProjectionPositionslope[1]*x) * iw * ScreenScaleRefractReflect[3] + ScreenCenterRefractReflect[3]; // * z (disappears) + + // " vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * DistortScaleRefractReflect;\n" + v[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f; + v[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f; + v[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f; + DPSOFTRAST_Vector3Normalize(v); + ScreenTexCoord[0] = SafeScreenTexCoord[0] + v[0] * DistortScaleRefractReflect[0]; + ScreenTexCoord[1] = SafeScreenTexCoord[1] + v[1] * DistortScaleRefractReflect[1]; + ScreenTexCoord[2] = SafeScreenTexCoord[2] + v[0] * DistortScaleRefractReflect[2]; + ScreenTexCoord[3] = SafeScreenTexCoord[3] + v[1] * DistortScaleRefractReflect[3]; + + // " float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n" + v[0] = (EyeVectordata[0] + EyeVectorslope[0] * x); // * z (disappears) + v[1] = (EyeVectordata[1] + EyeVectorslope[1] * x); // * z (disappears) + v[2] = (EyeVectordata[2] + EyeVectorslope[2] * x); // * z (disappears) + DPSOFTRAST_Vector3Normalize(v); + Fresnel = 1.0f - v[2]; + Fresnel = min(1.0f, Fresnel); + Fresnel = Fresnel * Fresnel * ReflectFactor + ReflectOffset; + + // " dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * RefractColor;\n" + // " dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * RefractColor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n" + DPSOFTRAST_Texture2DBGRA8(texture_refraction, 0, ScreenTexCoord[0], ScreenTexCoord[1], c1); + DPSOFTRAST_Texture2DBGRA8(texture_reflection, 0, ScreenTexCoord[2], ScreenTexCoord[3], c2); + + buffer_FragColorbgra8[x*4+0] = (c1[0] * RefractColor[0]) * (1.0f - Fresnel) + (c2[0] * ReflectColor[0]) * Fresnel; + buffer_FragColorbgra8[x*4+1] = (c1[1] * RefractColor[1]) * (1.0f - Fresnel) + (c2[1] * ReflectColor[1]) * Fresnel; + buffer_FragColorbgra8[x*4+2] = (c1[2] * RefractColor[2]) * (1.0f - Fresnel) + (c2[2] * ReflectColor[2]) * Fresnel; + buffer_FragColorbgra8[x*4+3] = min(( RefractColor[3] * (1.0f - Fresnel) + ReflectColor[3] * Fresnel) * 256, 255); + } + DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8); } @@ -4708,7 +4769,7 @@ static const DPSOFTRAST_ShaderModeInfo DPSOFTRAST_ShaderModeTable[SHADERMODE_COU {2, DPSOFTRAST_VertexShader_LightDirection, DPSOFTRAST_PixelShader_LightDirection, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD1, DPSOFTRAST_ARRAY_TEXCOORD2, DPSOFTRAST_ARRAY_TEXCOORD3, DPSOFTRAST_ARRAY_TEXCOORD5, DPSOFTRAST_ARRAY_TEXCOORD6, ~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, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD4, ~0}, {GL20TU_NORMAL, GL20TU_REFRACTION, ~0}}, - {2, DPSOFTRAST_VertexShader_Water, DPSOFTRAST_PixelShader_Water, {~0}}, + {2, DPSOFTRAST_VertexShader_Water, DPSOFTRAST_PixelShader_Water, {DPSOFTRAST_ARRAY_TEXCOORD0, DPSOFTRAST_ARRAY_TEXCOORD1, DPSOFTRAST_ARRAY_TEXCOORD2, DPSOFTRAST_ARRAY_TEXCOORD3, DPSOFTRAST_ARRAY_TEXCOORD4, DPSOFTRAST_ARRAY_TEXCOORD6, ~0}, {GL20TU_NORMAL, GL20TU_REFLECTION, GL20TU_REFRACTION, ~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}},