]> git.xonotic.org Git - xonotic/darkplaces.git/blob - dpsoftrast.c
1365fc94fbf81550c6b2b9aae532f3266433bb61
[xonotic/darkplaces.git] / dpsoftrast.c
1
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5 #include "quakedef.h"
6 #include "dpsoftrast.h"
7
8 #ifndef __cplusplus
9 typedef qboolean bool;
10 #endif
11
12 #if defined(__GNUC__)
13 #define ALIGN(var) var __attribute__((__aligned__(16)))
14 #elif defined(_MSC_VER)
15 #define ALIGN(var) __declspec(align(16)) var
16 #else
17 #define ALIGN(var) var
18 #endif
19
20 #ifdef SSE2_PRESENT
21 #include <emmintrin.h>
22
23 #define MM_MALLOC(size) _mm_malloc(size, 16)
24
25 static void *MM_CALLOC(size_t nmemb, size_t size)
26 {
27         void *ptr = _mm_malloc(nmemb*size, 16);
28         if(ptr != NULL) memset(ptr, 0, nmemb*size);
29         return ptr;
30 }
31
32 #define MM_FREE _mm_free
33 #else
34 #define MM_MALLOC(size) malloc(size)
35 #define MM_CALLOC(nmemb, size) calloc(nmemb, size)
36 #define MM_FREE free
37 #endif
38
39 typedef enum DPSOFTRAST_ARRAY_e
40 {
41         DPSOFTRAST_ARRAY_POSITION,
42         DPSOFTRAST_ARRAY_COLOR,
43         DPSOFTRAST_ARRAY_TEXCOORD0,
44         DPSOFTRAST_ARRAY_TEXCOORD1,
45         DPSOFTRAST_ARRAY_TEXCOORD2,
46         DPSOFTRAST_ARRAY_TEXCOORD3,
47         DPSOFTRAST_ARRAY_TEXCOORD4,
48         DPSOFTRAST_ARRAY_TEXCOORD5,
49         DPSOFTRAST_ARRAY_TEXCOORD6,
50         DPSOFTRAST_ARRAY_TEXCOORD7,
51         DPSOFTRAST_ARRAY_TOTAL
52 }
53 DPSOFTRAST_ARRAY;
54
55 typedef struct DPSOFTRAST_Texture_s
56 {
57         int flags;
58         int width;
59         int height;
60         int depth;
61         int sides;
62         DPSOFTRAST_TEXTURE_FILTER filter;
63         int mipmaps;
64         int size;
65         unsigned char *bytes;
66         int mipmap[DPSOFTRAST_MAXMIPMAPS][5];
67 }
68 DPSOFTRAST_Texture;
69
70 typedef struct DPSOFTRAST_State_User_s
71 {
72         int colormask[4];
73         int blendfunc[2];
74         int blendsubtract;
75         int depthmask;
76         int depthtest;
77         int depthfunc;
78         int scissortest;
79         int cullface;
80         int alphatest;
81         int alphafunc;
82         float alphavalue;
83         int scissor[4];
84         int viewport[4];
85         float depthrange[2];
86         float polygonoffset[2];
87         float color[4];
88 }
89 DPSOFTRAST_State_User;
90
91 #define DPSOFTRAST_MAXSUBSPAN 16
92
93 typedef ALIGN(struct DPSOFTRAST_State_Draw_Span_s
94 {
95         int start; // pixel index
96         int length; // pixel count
97         int startx; // usable range (according to pixelmask)
98         int endx; // usable range (according to pixelmask)
99         unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; // texcoord to screen space density values (for picking mipmap of textures)
100         unsigned char *pixelmask; // true for pixels that passed depth test, false for others
101         // [0][n][] is start interpolant values (projected)
102         // [1][n][] is end interpolant values (projected)
103         // [0][DPSOFTRAST_ARRAY_TOTAL][] is start screencoord4f
104         // [1][DPSOFTRAST_ARRAY_TOTAL][] is end screencoord4f
105         // NOTE: screencoord4f[3] is W (basically 1/Z), useful for depthbuffer
106         ALIGN(float data[2][DPSOFTRAST_ARRAY_TOTAL+1][4]);
107 }
108 DPSOFTRAST_State_Draw_Span);
109
110 #define DPSOFTRAST_DRAW_MAXSPANQUEUE 1024
111
112 typedef struct DPSOFTRAST_State_Draw_s
113 {
114         int numvertices;
115         int maxvertices;
116         float *in_array4f[DPSOFTRAST_ARRAY_TOTAL];
117         float *post_array4f[DPSOFTRAST_ARRAY_TOTAL];
118         float *screencoord4f;
119
120         // spans are queued in this structure for dispatch to the pixel shader,
121         // partly to improve cache locality, partly for batching purposes, spans
122         // are flushed before DrawTriangles returns to caller
123         int numspans;
124         DPSOFTRAST_State_Draw_Span spanqueue[DPSOFTRAST_DRAW_MAXSPANQUEUE];
125 }
126 DPSOFTRAST_State_Draw;
127
128 #define DPSOFTRAST_VALIDATE_FB 1
129 #define DPSOFTRAST_VALIDATE_DEPTHFUNC 2
130 #define DPSOFTRAST_VALIDATE_BLENDFUNC 4
131 #define DPSOFTRAST_VALIDATE_DRAW (DPSOFTRAST_VALIDATE_FB | DPSOFTRAST_VALIDATE_DEPTHFUNC | DPSOFTRAST_VALIDATE_BLENDFUNC)
132
133 typedef enum DPSOFTRAST_BLENDMODE_e
134 {
135         DPSOFTRAST_BLENDMODE_OPAQUE,
136         DPSOFTRAST_BLENDMODE_ALPHA,
137         DPSOFTRAST_BLENDMODE_ADDALPHA,
138         DPSOFTRAST_BLENDMODE_ADD,
139         DPSOFTRAST_BLENDMODE_INVMOD,
140         DPSOFTRAST_BLENDMODE_MUL,
141         DPSOFTRAST_BLENDMODE_MUL2,
142         DPSOFTRAST_BLENDMODE_SUBALPHA,
143         DPSOFTRAST_BLENDMODE_PSEUDOALPHA,
144         DPSOFTRAST_BLENDMODE_TOTAL
145 }
146 DPSOFTRAST_BLENDMODE;
147
148 typedef ALIGN(struct DPSOFTRAST_State_s
149 {
150         // DPSOFTRAST_VALIDATE_ flags
151         int validate;
152
153         int fb_colormask;
154         int fb_width;
155         int fb_height;
156         unsigned int *fb_depthpixels;
157         unsigned int *fb_colorpixels[4];
158
159         const float *pointer_vertex3f;
160         const float *pointer_color4f;
161         const unsigned char *pointer_color4ub;
162         const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
163         int stride_vertex;
164         int stride_color;
165         int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
166         int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
167         DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
168
169         int shader_mode;
170         int shader_permutation;
171         ALIGN(float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4]);
172         int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
173
174         // derived values (DPSOFTRAST_VALIDATE_FB)
175         int fb_clearscissor[4];
176         int fb_viewport[4];
177         int fb_viewportscissor[4];
178         ALIGN(float fb_viewportcenter[4]);
179         ALIGN(float fb_viewportscale[4]);
180
181         // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
182         int fb_depthfunc;
183
184         // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
185         int fb_blendmode;
186
187         int texture_max;
188         int texture_end;
189         int texture_firstfree;
190         DPSOFTRAST_Texture *texture;
191
192         int bigendian;
193
194         // error reporting
195         const char *errorstring;
196
197         DPSOFTRAST_State_User user;
198
199         DPSOFTRAST_State_Draw draw;
200 }
201 DPSOFTRAST_State);
202
203 DPSOFTRAST_State dpsoftrast;
204
205 extern int dpsoftrast_test;
206
207 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
208 #define DPSOFTRAST_DEPTHOFFSET (128.0f)
209 #define DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a) (((int)(r * 255.0f + 0.5f) << 16) | ((int)(g * 255.0f + 0.5f) << 8) | (int)(b * 255.0f + 0.5f) | ((int)(a * 255.0f + 0.5f) << 24))
210 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
211 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
212
213 void DPSOFTRAST_RecalcFB(void)
214 {
215         // calculate framebuffer scissor, viewport, viewport clipped by scissor,
216         // and viewport projection values
217         int x1, x2, x3, x4, x5, x6;
218         int y1, y2, y3, y4, y5, y6;
219         x1 = dpsoftrast.user.scissor[0];
220         x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
221         x3 = dpsoftrast.user.viewport[0];
222         x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
223         y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
224         y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
225         y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
226         y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
227         if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
228         if (x1 < 0) x1 = 0;
229         if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
230         if (x3 < 0) x1 = 0;
231         if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
232         if (y1 < 0) y1 = 0;
233         if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
234         if (y3 < 0) y1 = 0;
235         if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
236         x5 = x1;if (x5 < x3) x5 = x3;
237         x6 = x2;if (x6 > x4) x4 = x4;
238         y5 = y1;if (y5 < y3) y5 = y3;
239         y6 = y2;if (y6 > y4) y6 = y4;
240         dpsoftrast.fb_clearscissor[0] = x1;
241         dpsoftrast.fb_clearscissor[1] = y1;
242         dpsoftrast.fb_clearscissor[2] = x2 - x1;
243         dpsoftrast.fb_clearscissor[3] = y2 - y1;
244         dpsoftrast.fb_viewport[0] = x3;
245         dpsoftrast.fb_viewport[1] = y3;
246         dpsoftrast.fb_viewport[2] = x4 - x3;
247         dpsoftrast.fb_viewport[3] = y4 - y3;
248         dpsoftrast.fb_viewportscissor[0] = x5;
249         dpsoftrast.fb_viewportscissor[1] = y5;
250         dpsoftrast.fb_viewportscissor[2] = x6 - x5;
251         dpsoftrast.fb_viewportscissor[3] = y6 - y5;
252         dpsoftrast.fb_viewportcenter[1] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
253         dpsoftrast.fb_viewportcenter[2] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
254         dpsoftrast.fb_viewportcenter[3] = 0.5f;
255         dpsoftrast.fb_viewportcenter[0] = 0.0f;
256         dpsoftrast.fb_viewportscale[1] = 0.5f * dpsoftrast.user.viewport[2];
257         dpsoftrast.fb_viewportscale[2] = -0.5f * dpsoftrast.user.viewport[3];
258         dpsoftrast.fb_viewportscale[3] = 0.5f;
259         dpsoftrast.fb_viewportscale[0] = 1.0f;
260 }
261
262 void DPSOFTRAST_RecalcDepthFunc(void)
263 {
264         dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
265 }
266
267 int blendmodetable[][4] = 
268 {
269         {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
270         {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
271         {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
272         {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
273         {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
274         {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
275         {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
276         {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
277         {DPSOFTRAST_BLENDMODE_PSEUDOALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, false},
278         {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
279 };
280
281 void DPSOFTRAST_RecalcBlendFunc(void)
282 {
283         int i;
284         dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE;
285         for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++)
286         {
287                 if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3])
288                 {
289                         dpsoftrast.fb_blendmode = blendmodetable[i][0];
290                         break;
291                 }
292         }
293 }
294
295 #define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0)
296
297 void DPSOFTRAST_Validate(int mask)
298 {
299         mask &= dpsoftrast.validate;
300         if (!mask)
301                 return;
302         if (mask & DPSOFTRAST_VALIDATE_FB)
303         {
304                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB;
305                 DPSOFTRAST_RecalcFB();
306         }
307         if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
308         {
309                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
310                 DPSOFTRAST_RecalcDepthFunc();
311         }
312         if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
313         {
314                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
315                 DPSOFTRAST_RecalcBlendFunc();
316         }
317 }
318
319 DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
320 {
321         if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
322                 return &dpsoftrast.texture[index];
323         return NULL;
324 }
325
326 int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
327 {
328         int w;
329         int h;
330         int d;
331         int size;
332         int s;
333         int texnum;
334         int mipmaps;
335         int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
336         int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
337         DPSOFTRAST_Texture *texture;
338         if (width*height*depth < 1)
339         {
340                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
341                 return 0;
342         }
343         if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
344         {
345                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
346                 return 0;
347         }
348         switch(texformat)
349         {
350         case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
351         case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
352         case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
353                 break;
354         case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
355                 if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
356                 {
357                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
358                         return 0;
359                 }
360                 if (depth != 1)
361                 {
362                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
363                         return 0;
364                 }
365                 if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
366                 {
367                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
368                         return 0;
369                 }
370                 break;
371         }
372         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
373         {
374                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
375                 return 0;
376         }
377         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
378         {
379                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
380                 return 0;
381         }
382         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
383         {
384                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
385                 return 0;
386         }
387         if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
388         {
389                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
390                 return 0;
391         }
392         if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
393         {
394                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
395                 return 0;
396         }
397         // find first empty slot in texture array
398         for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
399                 if (!dpsoftrast.texture[texnum].bytes)
400                         break;
401         dpsoftrast.texture_firstfree = texnum + 1;
402         if (dpsoftrast.texture_max <= texnum)
403         {
404                 // expand texture array as needed
405                 if (dpsoftrast.texture_max < 1024)
406                         dpsoftrast.texture_max = 1024;
407                 else
408                         dpsoftrast.texture_max *= 2;
409                 dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
410         }
411         if (dpsoftrast.texture_end <= texnum)
412                 dpsoftrast.texture_end = texnum + 1;
413         texture = &dpsoftrast.texture[texnum];
414         memset(texture, 0, sizeof(*texture));
415         texture->flags = flags;
416         texture->width = width;
417         texture->height = height;
418         texture->depth = depth;
419         texture->sides = sides;
420         w = width;
421         h = height;
422         d = depth;
423         size = 0;
424         mipmaps = 0;
425         w = width;
426         h = height;
427         d = depth;
428         for (;;)
429         {
430                 s = w * h * d * sides * 4;
431                 texture->mipmap[mipmaps][0] = size;
432                 texture->mipmap[mipmaps][1] = s;
433                 texture->mipmap[mipmaps][2] = w;
434                 texture->mipmap[mipmaps][3] = h;
435                 texture->mipmap[mipmaps][4] = d;
436                 size += s;
437                 mipmaps++;
438                 if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
439                         break;
440                 if (w > 1) w >>= 1;
441                 if (h > 1) h >>= 1;
442                 if (d > 1) d >>= 1;
443         }
444         texture->mipmaps = mipmaps;
445         texture->size = size;
446
447         // allocate the pixels now
448         texture->bytes = (unsigned char *)MM_CALLOC(1, size);
449
450         return texnum;
451 }
452 void DPSOFTRAST_Texture_Free(int index)
453 {
454         DPSOFTRAST_Texture *texture;
455         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
456         if (texture->bytes)
457                 MM_FREE(texture->bytes);
458         texture->bytes = NULL;
459         memset(texture, 0, sizeof(*texture));
460         // adjust the free range and used range
461         if (dpsoftrast.texture_firstfree > index)
462                 dpsoftrast.texture_firstfree = index;
463         while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
464                 dpsoftrast.texture_end--;
465 }
466 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
467 {
468         int i, x, y, z, w, layer0, layer1, row0, row1;
469         unsigned char *o, *i0, *i1, *i2, *i3;
470         DPSOFTRAST_Texture *texture;
471         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
472         if (texture->mipmaps <= 1)
473                 return;
474         for (i = 1;i < texture->mipmaps;i++)
475         {
476                 for (z = 0;z < texture->mipmap[i][4];z++)
477                 {
478                         layer0 = z*2;
479                         layer1 = z*2+1;
480                         if (layer1 >= texture->mipmap[i-1][4])
481                                 layer1 = texture->mipmap[i-1][4]-1;
482                         for (y = 0;y < texture->mipmap[i][3];y++)
483                         {
484                                 row0 = y*2;
485                                 row1 = y*2+1;
486                                 if (row1 >= texture->mipmap[i-1][3])
487                                         row1 = texture->mipmap[i-1][3]-1;
488                                 o =  texture->bytes + texture->mipmap[i  ][0] + 4*((texture->mipmap[i  ][3] * z      + y   ) * texture->mipmap[i  ][2]);
489                                 i0 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row0) * texture->mipmap[i-1][2]);
490                                 i1 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row1) * texture->mipmap[i-1][2]);
491                                 i2 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row0) * texture->mipmap[i-1][2]);
492                                 i3 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row1) * texture->mipmap[i-1][2]);
493                                 w = texture->mipmap[i][2];
494                                 if (layer1 > layer0)
495                                 {
496                                         if (texture->mipmap[i-1][2] > 1)
497                                         {
498                                                 // average 3D texture
499                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8, i2 += 8, i3 += 8)
500                                                 {
501                                                         o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
502                                                         o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
503                                                         o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
504                                                         o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
505                                                 }
506                                         }
507                                         else
508                                         {
509                                                 // average 3D mipmap with parent width == 1
510                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
511                                                 {
512                                                         o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
513                                                         o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
514                                                         o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
515                                                         o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
516                                                 }
517                                         }
518                                 }
519                                 else
520                                 {
521                                         if (texture->mipmap[i-1][2] > 1)
522                                         {
523                                                 // average 2D texture (common case)
524                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
525                                                 {
526                                                         o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
527                                                         o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
528                                                         o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
529                                                         o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
530                                                 }
531                                         }
532                                         else
533                                         {
534                                                 // 2D texture with parent width == 1
535                                                 o[0] = (i0[0] + i1[0] + 1) >> 1;
536                                                 o[1] = (i0[1] + i1[1] + 1) >> 1;
537                                                 o[2] = (i0[2] + i1[2] + 1) >> 1;
538                                                 o[3] = (i0[3] + i1[3] + 1) >> 1;
539                                         }
540                                 }
541                         }
542                 }
543         }
544 }
545 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
546 {
547         DPSOFTRAST_Texture *texture;
548         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
549
550         // FIXME IMPLEMENT
551
552         dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
553 }
554 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
555 {
556         DPSOFTRAST_Texture *texture;
557         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
558
559         memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
560         DPSOFTRAST_Texture_CalculateMipmaps(index);
561 }
562 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
563 {
564         DPSOFTRAST_Texture *texture;
565         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
566         return texture->mipmap[mip][2];
567 }
568 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
569 {
570         DPSOFTRAST_Texture *texture;
571         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
572         return texture->mipmap[mip][3];
573 }
574 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
575 {
576         DPSOFTRAST_Texture *texture;
577         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
578         return texture->mipmap[mip][4];
579 }
580 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
581 {
582         DPSOFTRAST_Texture *texture;
583         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
584         return texture->bytes + texture->mipmap[mip][0];
585 }
586 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
587 {
588         DPSOFTRAST_Texture *texture;
589         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
590         if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
591         {
592                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
593                 return;
594         }
595         texture->filter = filter;
596 }
597
598 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
599 {
600         dpsoftrast.fb_width = width;
601         dpsoftrast.fb_height = height;
602         dpsoftrast.fb_depthpixels = depthpixels;
603         dpsoftrast.fb_colorpixels[0] = colorpixels0;
604         dpsoftrast.fb_colorpixels[1] = colorpixels1;
605         dpsoftrast.fb_colorpixels[2] = colorpixels2;
606         dpsoftrast.fb_colorpixels[3] = colorpixels3;
607 }
608 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
609 {
610         dpsoftrast.user.viewport[0] = x;
611         dpsoftrast.user.viewport[1] = y;
612         dpsoftrast.user.viewport[2] = width;
613         dpsoftrast.user.viewport[3] = height;
614         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
615 }
616 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
617 {
618         int i, x1, y1, x2, y2, w, h, x, y;
619         unsigned int *p;
620         unsigned int c;
621         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
622         x1 = dpsoftrast.fb_clearscissor[0];
623         y1 = dpsoftrast.fb_clearscissor[1];
624         x2 = dpsoftrast.fb_clearscissor[2];
625         y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
626         w = x2 - x1;
627         h = y2 - y1;
628         if (w < 1 || h < 1)
629                 return;
630         // FIXME: honor dpsoftrast.fb_colormask?
631         c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
632         for (i = 0;i < 4;i++)
633         {
634                 if (!dpsoftrast.fb_colorpixels[i])
635                         continue;
636                 for (y = y1;y < y2;y++)
637                 {
638                         p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
639                         for (x = x1;x < x2;x++)
640                                 p[x] = c;
641                 }
642         }
643 }
644 void DPSOFTRAST_ClearDepth(float d)
645 {
646         int x1, y1, x2, y2, w, h, x, y;
647         unsigned int *p;
648         unsigned int c;
649         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
650         x1 = dpsoftrast.fb_clearscissor[0];
651         y1 = dpsoftrast.fb_clearscissor[1];
652         x2 = dpsoftrast.fb_clearscissor[2];
653         y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
654         w = x2 - x1;
655         h = y2 - y1;
656         if (w < 1 || h < 1)
657                 return;
658         c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
659         for (y = y1;y < y2;y++)
660         {
661                 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
662                 for (x = x1;x < x2;x++)
663                         p[x] = c;
664         }
665 }
666 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
667 {
668         dpsoftrast.user.colormask[0] = r != 0;
669         dpsoftrast.user.colormask[1] = g != 0;
670         dpsoftrast.user.colormask[2] = b != 0;
671         dpsoftrast.user.colormask[3] = a != 0;
672         dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
673 }
674 void DPSOFTRAST_DepthTest(int enable)
675 {
676         dpsoftrast.user.depthtest = enable;
677         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
678 }
679 void DPSOFTRAST_ScissorTest(int enable)
680 {
681         dpsoftrast.user.scissortest = enable;
682         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
683 }
684 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
685 {
686         dpsoftrast.user.scissor[0] = x;
687         dpsoftrast.user.scissor[1] = y;
688         dpsoftrast.user.scissor[2] = width;
689         dpsoftrast.user.scissor[3] = height;
690         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
691 }
692
693 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
694 {
695         // FIXME: validate
696         dpsoftrast.user.blendfunc[0] = smodulate;
697         dpsoftrast.user.blendfunc[1] = dmodulate;
698         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
699 }
700 void DPSOFTRAST_BlendSubtract(int enable)
701 {
702         dpsoftrast.user.blendsubtract = enable != 0;
703         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
704 }
705 void DPSOFTRAST_DepthMask(int enable)
706 {
707         dpsoftrast.user.depthmask = enable;
708 }
709 void DPSOFTRAST_DepthFunc(int comparemode)
710 {
711         // FIXME: validate
712         dpsoftrast.user.depthfunc = comparemode;
713 }
714 void DPSOFTRAST_DepthRange(float range0, float range1)
715 {
716         dpsoftrast.user.depthrange[0] = range0;
717         dpsoftrast.user.depthrange[1] = range1;
718 }
719 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
720 {
721         dpsoftrast.user.polygonoffset[0] = alongnormal;
722         dpsoftrast.user.polygonoffset[1] = intoview;
723 }
724 void DPSOFTRAST_CullFace(int mode)
725 {
726         // FIXME: validate
727         dpsoftrast.user.cullface = mode;
728 }
729 void DPSOFTRAST_AlphaTest(float enable)
730 {
731         dpsoftrast.user.alphatest = enable;
732 }
733 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
734 {
735         // FIXME: validate
736         dpsoftrast.user.alphafunc = alphafunc;
737         dpsoftrast.user.alphavalue = alphavalue;
738 }
739 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
740 {
741         dpsoftrast.user.color[0] = r;
742         dpsoftrast.user.color[1] = g;
743         dpsoftrast.user.color[2] = b;
744         dpsoftrast.user.color[3] = a;
745 }
746 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
747 {
748         int outstride = blockwidth * 4;
749         int instride = dpsoftrast.fb_width * 4;
750         int bx1 = blockx;
751         int by1 = blocky;
752         int bx2 = blockx + blockwidth;
753         int by2 = blocky + blockheight;
754         int bw;
755         int bh;
756         int x;
757         int y;
758         unsigned char *inpixels;
759         unsigned char *b;
760         unsigned char *o;
761         if (bx1 < 0) bx1 = 0;
762         if (by1 < 0) by1 = 0;
763         if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
764         if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
765         bw = bx2 - bx1;
766         bh = by2 - by1;
767         inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
768         if (dpsoftrast.bigendian)
769         {
770                 for (y = by1;y < by2;y++)
771                 {
772                         b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
773                         o = (unsigned char *)outpixels + (y - by1) * outstride;
774                         for (x = bx1;x < bx2;x++)
775                         {
776                                 o[0] = b[3];
777                                 o[1] = b[2];
778                                 o[2] = b[1];
779                                 o[3] = b[0];
780                                 o += 4;
781                                 b += 4;
782                         }
783                 }
784         }
785         else
786         {
787                 for (y = by1;y < by2;y++)
788                 {
789                         b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
790                         o = (unsigned char *)outpixels + (y - by1) * outstride;
791                         memcpy(o, b, bw*4);
792                 }
793         }
794
795 }
796 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
797 {
798         int tx1 = tx;
799         int ty1 = ty;
800         int tx2 = tx + width;
801         int ty2 = ty + height;
802         int sx1 = sx;
803         int sy1 = sy;
804         int sx2 = sx + width;
805         int sy2 = sy + height;
806         int swidth;
807         int sheight;
808         int twidth;
809         int theight;
810         int sw;
811         int sh;
812         int tw;
813         int th;
814         int y;
815         unsigned int *spixels;
816         unsigned int *tpixels;
817         DPSOFTRAST_Texture *texture;
818         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
819         if (mip < 0 || mip >= texture->mipmaps) return;
820         spixels = dpsoftrast.fb_colorpixels[0];
821         swidth = dpsoftrast.fb_width;
822         sheight = dpsoftrast.fb_height;
823         tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
824         twidth = texture->mipmap[mip][2];
825         theight = texture->mipmap[mip][3];
826         if (tx1 < 0) tx1 = 0;
827         if (ty1 < 0) ty1 = 0;
828         if (tx2 > twidth) tx2 = twidth;
829         if (ty2 > theight) ty2 = theight;
830         if (sx1 < 0) sx1 = 0;
831         if (sy1 < 0) sy1 = 0;
832         if (sx2 > swidth) sx2 = swidth;
833         if (sy2 > sheight) sy2 = sheight;
834         tw = tx2 - tx1;
835         th = ty2 - ty1;
836         sw = sx2 - sx1;
837         sh = sy2 - sy1;
838         if (tw > sw) tw = sw;
839         if (th > sh) th = sh;
840         if (tw < 1 || th < 1)
841                 return;
842         for (y = 0;y < th;y++)
843                 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
844         if (texture->mipmaps > 1)
845                 DPSOFTRAST_Texture_CalculateMipmaps(index);
846 }
847 void DPSOFTRAST_SetTexture(int unitnum, int index)
848 {
849         DPSOFTRAST_Texture *texture;
850         if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
851         {
852                 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
853                 return;
854         }
855         texture = DPSOFTRAST_Texture_GetByIndex(index);
856         if (index && !texture)
857         {
858                 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
859                 return;
860         }
861         dpsoftrast.texbound[unitnum] = texture;
862 }
863
864 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
865 {
866         dpsoftrast.pointer_vertex3f = vertex3f;
867         dpsoftrast.stride_vertex = stride;
868 }
869 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
870 {
871         dpsoftrast.pointer_color4f = color4f;
872         dpsoftrast.pointer_color4ub = NULL;
873         dpsoftrast.stride_color = stride;
874 }
875 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
876 {
877         dpsoftrast.pointer_color4f = NULL;
878         dpsoftrast.pointer_color4ub = color4ub;
879         dpsoftrast.stride_color = stride;
880 }
881 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
882 {
883         dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
884         dpsoftrast.components_texcoord[unitnum] = numcomponents;
885         dpsoftrast.stride_texcoord[unitnum] = stride;
886 }
887
888 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
889 {
890         dpsoftrast.shader_mode = mode;
891         dpsoftrast.shader_permutation = permutation;
892 }
893 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
894 {
895         dpsoftrast.uniform4f[index*4+0] = v0;
896         dpsoftrast.uniform4f[index*4+1] = v1;
897         dpsoftrast.uniform4f[index*4+2] = v2;
898         dpsoftrast.uniform4f[index*4+3] = v3;
899 }
900 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v)
901 {
902         dpsoftrast.uniform4f[index*4+0] = v[0];
903         dpsoftrast.uniform4f[index*4+1] = v[1];
904         dpsoftrast.uniform4f[index*4+2] = v[2];
905         dpsoftrast.uniform4f[index*4+3] = v[3];
906 }
907 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v)
908 {
909         int i;
910         for (i = 0;i < arraysize;i++, index += 4, v += 16)
911         {
912                 if (transpose)
913                 {
914                         dpsoftrast.uniform4f[index*4+0] = v[0];
915                         dpsoftrast.uniform4f[index*4+1] = v[4];
916                         dpsoftrast.uniform4f[index*4+2] = v[8];
917                         dpsoftrast.uniform4f[index*4+3] = v[12];
918                         dpsoftrast.uniform4f[index*4+4] = v[1];
919                         dpsoftrast.uniform4f[index*4+5] = v[5];
920                         dpsoftrast.uniform4f[index*4+6] = v[9];
921                         dpsoftrast.uniform4f[index*4+7] = v[13];
922                         dpsoftrast.uniform4f[index*4+8] = v[2];
923                         dpsoftrast.uniform4f[index*4+9] = v[6];
924                         dpsoftrast.uniform4f[index*4+10] = v[10];
925                         dpsoftrast.uniform4f[index*4+11] = v[14];
926                         dpsoftrast.uniform4f[index*4+12] = v[3];
927                         dpsoftrast.uniform4f[index*4+13] = v[7];
928                         dpsoftrast.uniform4f[index*4+14] = v[11];
929                         dpsoftrast.uniform4f[index*4+15] = v[15];
930                 }
931                 else
932                 {
933                         dpsoftrast.uniform4f[index*4+0] = v[0];
934                         dpsoftrast.uniform4f[index*4+1] = v[1];
935                         dpsoftrast.uniform4f[index*4+2] = v[2];
936                         dpsoftrast.uniform4f[index*4+3] = v[3];
937                         dpsoftrast.uniform4f[index*4+4] = v[4];
938                         dpsoftrast.uniform4f[index*4+5] = v[5];
939                         dpsoftrast.uniform4f[index*4+6] = v[6];
940                         dpsoftrast.uniform4f[index*4+7] = v[7];
941                         dpsoftrast.uniform4f[index*4+8] = v[8];
942                         dpsoftrast.uniform4f[index*4+9] = v[9];
943                         dpsoftrast.uniform4f[index*4+10] = v[10];
944                         dpsoftrast.uniform4f[index*4+11] = v[11];
945                         dpsoftrast.uniform4f[index*4+12] = v[12];
946                         dpsoftrast.uniform4f[index*4+13] = v[13];
947                         dpsoftrast.uniform4f[index*4+14] = v[14];
948                         dpsoftrast.uniform4f[index*4+15] = v[15];
949                 }
950         }
951 }
952 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
953 {
954         dpsoftrast.uniform1i[index] = i0;
955 }
956
957 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors)
958 {
959         int i;
960         int j;
961         int stride;
962         const float *v;
963         float *p;
964         float *data;
965         const unsigned char *b;
966         dpsoftrast.draw.numvertices = numvertices;
967         if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
968         {
969                 if (dpsoftrast.draw.maxvertices < 4096)
970                         dpsoftrast.draw.maxvertices = 4096;
971                 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
972                         dpsoftrast.draw.maxvertices *= 2;
973                 if (dpsoftrast.draw.in_array4f[0])
974                         MM_FREE(dpsoftrast.draw.in_array4f[0]);
975                 data = (float *)MM_CALLOC(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
976                 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
977                         dpsoftrast.draw.in_array4f[i] = data;
978                 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
979                         dpsoftrast.draw.post_array4f[i] = data;
980                 dpsoftrast.draw.screencoord4f = data;
981                 data += dpsoftrast.draw.maxvertices * 4;
982         }
983         stride = dpsoftrast.stride_vertex;
984         v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
985         p = dpsoftrast.draw.in_array4f[0];
986         for (i = 0;i < numvertices;i++)
987         {
988                 p[0] = v[0];
989                 p[1] = v[1];
990                 p[2] = v[2];
991                 p[3] = 1.0f;
992                 p += 4;
993                 v = (const float *)((const unsigned char *)v + stride);
994         }
995         if (needcolors)
996         {
997                 if (dpsoftrast.pointer_color4f)
998                 {
999                         stride = dpsoftrast.stride_color;
1000                         v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
1001                         p = dpsoftrast.draw.in_array4f[1];
1002                         for (i = 0;i < numvertices;i++)
1003                         {
1004                                 p[0] = v[0];
1005                                 p[1] = v[1];
1006                                 p[2] = v[2];
1007                                 p[3] = v[3];
1008                                 p += 4;
1009                                 v = (const float *)((const unsigned char *)v + stride);
1010                         }
1011                 }
1012                 else if (dpsoftrast.pointer_color4ub)
1013                 {
1014                         stride = dpsoftrast.stride_color;
1015                         b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
1016                         p = dpsoftrast.draw.in_array4f[1];
1017                         for (i = 0;i < numvertices;i++)
1018                         {
1019                                 p[0] = b[0] * (1.0f / 255.0f);
1020                                 p[1] = b[1] * (1.0f / 255.0f);
1021                                 p[2] = b[2] * (1.0f / 255.0f);
1022                                 p[3] = b[3] * (1.0f / 255.0f);
1023                                 p += 4;
1024                                 b = (const unsigned char *)((const unsigned char *)b + stride);
1025                         }
1026                 }
1027                 else
1028                 {
1029                         v = dpsoftrast.user.color;
1030                         p = dpsoftrast.draw.in_array4f[1];
1031                         for (i = 0;i < numvertices;i++)
1032                         {
1033                                 p[0] = v[0];
1034                                 p[1] = v[1];
1035                                 p[2] = v[2];
1036                                 p[3] = v[3];
1037                                 p += 4;
1038                         }
1039                 }
1040         }
1041         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL-2;j++)
1042         {
1043                 if (dpsoftrast.pointer_texcoordf[j])
1044                 {
1045                         stride = dpsoftrast.stride_texcoord[j];
1046                         v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1047                         p = dpsoftrast.draw.in_array4f[j+2];
1048                         switch(dpsoftrast.components_texcoord[j])
1049                         {
1050                         case 2:
1051                                 for (i = 0;i < numvertices;i++)
1052                                 {
1053                                         p[0] = v[0];
1054                                         p[1] = v[1];
1055                                         p[2] = 0.0f;
1056                                         p[3] = 1.0f;
1057                                         p += 4;
1058                                         v = (const float *)((const unsigned char *)v + stride);
1059                                 }
1060                                 break;
1061                         case 3:
1062                                 for (i = 0;i < numvertices;i++)
1063                                 {
1064                                         p[0] = v[0];
1065                                         p[1] = v[1];
1066                                         p[2] = v[2];
1067                                         p[3] = 1.0f;
1068                                         p += 4;
1069                                         v = (const float *)((const unsigned char *)v + stride);
1070                                 }
1071                                 break;
1072                         case 4:
1073                                 for (i = 0;i < numvertices;i++)
1074                                 {
1075                                         p[0] = v[0];
1076                                         p[1] = v[1];
1077                                         p[2] = v[2];
1078                                         p[3] = v[3];
1079                                         p += 4;
1080                                         v = (const float *)((const unsigned char *)v + stride);
1081                                 }
1082                                 break;
1083                         }
1084                 }
1085         }
1086 }
1087
1088 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1089 {
1090         static const float identitymatrix[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
1091         // TODO: SIMD
1092         float matrix[4][4];
1093         int i;
1094         memcpy(matrix, inmatrix16f, sizeof(float[16]));
1095         if (!memcmp(identitymatrix, matrix, sizeof(float[16])))
1096         {
1097                 // fast case for identity matrix
1098                 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1099                 return;
1100         }
1101         for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1102         {
1103                 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1104                 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1105                 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1106                 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1107         }
1108 }
1109
1110 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1111 {
1112         memcpy(out4f, in4f, numitems * sizeof(float[4]));
1113 }
1114
1115 #ifdef SSE2_PRESENT
1116 static __m128 DPSOFTRAST_Draw_ProjectVertex(__m128 v)
1117 {
1118         __m128 viewportcenter = _mm_load_ps(dpsoftrast.fb_viewportcenter), viewportscale = _mm_load_ps(dpsoftrast.fb_viewportscale);
1119         __m128 w = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3));
1120         v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set1_ps(1.0f));
1121         v = _mm_add_ps(viewportcenter, _mm_div_ps(_mm_mul_ps(viewportscale, v), w));
1122         v = _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1));
1123         return v;
1124 }
1125 #endif
1126
1127 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1128 {
1129 #ifdef SSE2_PRESENT
1130         // NOTE: this is used both as a whole mesh transform function and a
1131         // per-triangle transform function (for clipped triangles), accordingly
1132         // it should not crash on divide by 0 but the result of divide by 0 is
1133         // unimportant...
1134         // TODO: SIMD
1135         int i;
1136         __m128 viewportcenter = _mm_load_ps(dpsoftrast.fb_viewportcenter), viewportscale = _mm_load_ps(dpsoftrast.fb_viewportscale);
1137         for (i = 0;i < numitems;i++)
1138         {
1139                 __m128 v = _mm_load_ps(in4f), w = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3));
1140                 v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set1_ps(1.0f));
1141                 v = _mm_add_ps(viewportcenter, _mm_div_ps(_mm_mul_ps(viewportscale, v), w));
1142                 _mm_store_ps(out4f, _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1)));
1143                 in4f += 4;
1144                 out4f += 4;
1145         }
1146 #endif
1147 }
1148
1149 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1150 {
1151         int i;
1152         int x;
1153         int y;
1154         int w = dpsoftrast.fb_width;
1155         int bounds[4];
1156         float v0[2], v1[2];
1157         unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1158         //const float *c4f;
1159         bounds[0] = dpsoftrast.fb_viewportscissor[0];
1160         bounds[1] = dpsoftrast.fb_viewportscissor[1];
1161         bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1162         bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1163         v0[0] = screen0[0];
1164         v0[1] = screen0[1];
1165         v1[0] = screen1[0];
1166         v1[1] = screen1[1];
1167         for (i = 0;i <= 128;i++)
1168         {
1169                 // check nearclip
1170                 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1171                 //      continue;
1172                 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1173                 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1174                 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1175                         continue;
1176                 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1177                 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1178                 pixels[y*w+x] = 0xFFFFFFFF;
1179         }
1180 }
1181
1182 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *zf)
1183 {
1184         int x;
1185         int startx = span->startx;
1186         int endx = span->endx;
1187         float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1188         float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1189         float endz = 1.0f / (w + wslope * startx);
1190         for (x = startx;x < endx;)
1191         {
1192                 int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1;
1193                 float z = endz, dz;
1194                 if(nextsub >= endx) nextsub = endsub = endx-1;
1195                 endz = 1.0f / (w + wslope * nextsub);
1196                 dz = x < nextsub ? (endz - z) / (nextsub - x) : 0.0f;
1197                 for (; x <= endsub; x++, z += dz)
1198                         zf[x] = z;
1199         }
1200 }
1201
1202 void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const float * RESTRICT in4f)
1203 {
1204         int x;
1205         int startx = span->startx;
1206         int endx = span->endx;
1207         int d[4];
1208         float a, b;
1209         unsigned char * RESTRICT pixelmask = span->pixelmask;
1210         unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1211         if (!pixel)
1212                 return;
1213         pixel += span->start * 4;
1214         // handle alphatest now (this affects depth writes too)
1215         if (dpsoftrast.user.alphatest)
1216                 for (x = startx;x < endx;x++)
1217                         if (in4f[x*4+3] < 0.5f)
1218                                 pixelmask[x] = false;
1219         // FIXME: this does not handle bigendian
1220         switch(dpsoftrast.fb_blendmode)
1221         {
1222         case DPSOFTRAST_BLENDMODE_OPAQUE:
1223                 for (x = startx;x < endx;x++)
1224                 {
1225                         if (!pixelmask[x])
1226                                 continue;
1227                         d[0] = (int)(in4f[x*4+2]*255.0f);if (d[0] > 255) d[0] = 255;
1228                         d[1] = (int)(in4f[x*4+1]*255.0f);if (d[1] > 255) d[1] = 255;
1229                         d[2] = (int)(in4f[x*4+0]*255.0f);if (d[2] > 255) d[2] = 255;
1230                         d[3] = (int)(in4f[x*4+3]*255.0f);if (d[3] > 255) d[3] = 255;
1231                         pixel[x*4+0] = d[0];
1232                         pixel[x*4+1] = d[1];
1233                         pixel[x*4+2] = d[2];
1234                         pixel[x*4+3] = d[3];
1235                 }
1236                 break;
1237         case DPSOFTRAST_BLENDMODE_ALPHA:
1238                 for (x = startx;x < endx;x++)
1239                 {
1240                         if (!pixelmask[x])
1241                                 continue;
1242                         a = in4f[x*4+3] * 255.0f;
1243                         b = 1.0f - in4f[x*4+3];
1244                         d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1245                         d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1246                         d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1247                         d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1248                         pixel[x*4+0] = d[0];
1249                         pixel[x*4+1] = d[1];
1250                         pixel[x*4+2] = d[2];
1251                         pixel[x*4+3] = d[3];
1252                 }
1253                 break;
1254         case DPSOFTRAST_BLENDMODE_ADDALPHA:
1255                 for (x = startx;x < endx;x++)
1256                 {
1257                         if (!pixelmask[x])
1258                                 continue;
1259                         a = in4f[x*4+3] * 255.0f;
1260                         d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1261                         d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1262                         d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1263                         d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1264                         pixel[x*4+0] = d[0];
1265                         pixel[x*4+1] = d[1];
1266                         pixel[x*4+2] = d[2];
1267                         pixel[x*4+3] = d[3];
1268                 }
1269                 break;
1270         case DPSOFTRAST_BLENDMODE_ADD:
1271                 for (x = startx;x < endx;x++)
1272                 {
1273                         if (!pixelmask[x])
1274                                 continue;
1275                         d[0] = (int)(in4f[x*4+2]*255.0f+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1276                         d[1] = (int)(in4f[x*4+1]*255.0f+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1277                         d[2] = (int)(in4f[x*4+0]*255.0f+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1278                         d[3] = (int)(in4f[x*4+3]*255.0f+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1279                         pixel[x*4+0] = d[0];
1280                         pixel[x*4+1] = d[1];
1281                         pixel[x*4+2] = d[2];
1282                         pixel[x*4+3] = d[3];
1283                 }
1284                 break;
1285         case DPSOFTRAST_BLENDMODE_INVMOD:
1286                 for (x = startx;x < endx;x++)
1287                 {
1288                         if (!pixelmask[x])
1289                                 continue;
1290                         d[0] = (int)((1.0f-in4f[x*4+2])*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1291                         d[1] = (int)((1.0f-in4f[x*4+1])*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1292                         d[2] = (int)((1.0f-in4f[x*4+0])*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1293                         d[3] = (int)((1.0f-in4f[x*4+3])*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1294                         pixel[x*4+0] = d[0];
1295                         pixel[x*4+1] = d[1];
1296                         pixel[x*4+2] = d[2];
1297                         pixel[x*4+3] = d[3];
1298                 }
1299                 break;
1300         case DPSOFTRAST_BLENDMODE_MUL:
1301                 for (x = startx;x < endx;x++)
1302                 {
1303                         if (!pixelmask[x])
1304                                 continue;
1305                         d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1306                         d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1307                         d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1308                         d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1309                         pixel[x*4+0] = d[0];
1310                         pixel[x*4+1] = d[1];
1311                         pixel[x*4+2] = d[2];
1312                         pixel[x*4+3] = d[3];
1313                 }
1314                 break;
1315         case DPSOFTRAST_BLENDMODE_MUL2:
1316                 for (x = startx;x < endx;x++)
1317                 {
1318                         if (!pixelmask[x])
1319                                 continue;
1320                         d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]*2.0f);if (d[0] > 255) d[0] = 255;
1321                         d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]*2.0f);if (d[1] > 255) d[1] = 255;
1322                         d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]*2.0f);if (d[2] > 255) d[2] = 255;
1323                         d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]*2.0f);if (d[3] > 255) d[3] = 255;
1324                         pixel[x*4+0] = d[0];
1325                         pixel[x*4+1] = d[1];
1326                         pixel[x*4+2] = d[2];
1327                         pixel[x*4+3] = d[3];
1328                 }
1329                 break;
1330         case DPSOFTRAST_BLENDMODE_SUBALPHA:
1331                 for (x = startx;x < endx;x++)
1332                 {
1333                         if (!pixelmask[x])
1334                                 continue;
1335                         a = in4f[x*4+3] * -255.0f;
1336                         d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;if (d[0] < 0) d[0] = 0;
1337                         d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;if (d[1] < 0) d[1] = 0;
1338                         d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;if (d[2] < 0) d[2] = 0;
1339                         d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;if (d[3] < 0) d[3] = 0;
1340                         pixel[x*4+0] = d[0];
1341                         pixel[x*4+1] = d[1];
1342                         pixel[x*4+2] = d[2];
1343                         pixel[x*4+3] = d[3];
1344                 }
1345                 break;
1346         case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1347                 for (x = startx;x < endx;x++)
1348                 {
1349                         if (!pixelmask[x])
1350                                 continue;
1351                         a = 255.0f;
1352                         b = 1.0f - in4f[x*4+3];
1353                         d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1354                         d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1355                         d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1356                         d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1357                         pixel[x*4+0] = d[0];
1358                         pixel[x*4+1] = d[1];
1359                         pixel[x*4+2] = d[2];
1360                         pixel[x*4+3] = d[3];
1361                 }
1362                 break;
1363         }
1364 }
1365
1366 void DPSOFTRAST_Draw_Span_FinishBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const unsigned char* RESTRICT in4ub)
1367 {
1368 #ifdef SSE2_PRESENT
1369         int x;
1370         int startx = span->startx;
1371         int endx = span->endx;
1372         const unsigned int * RESTRICT ini = (const unsigned int *)in4ub;
1373         unsigned char * RESTRICT pixelmask = span->pixelmask;
1374         unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1375         unsigned int * RESTRICT pixeli = (unsigned int *)dpsoftrast.fb_colorpixels[0];
1376         if (!pixel)
1377                 return;
1378         pixel += span->start * 4;
1379         pixeli += span->start;
1380         // handle alphatest now (this affects depth writes too)
1381         if (dpsoftrast.user.alphatest)
1382                 for (x = startx;x < endx;x++)
1383                         if (in4ub[x*4+3] < 0.5f)
1384                                 pixelmask[x] = false;
1385         // FIXME: this does not handle bigendian
1386         switch(dpsoftrast.fb_blendmode)
1387         {
1388         case DPSOFTRAST_BLENDMODE_OPAQUE:
1389                 for (x = startx;x + 4 <= endx;)
1390                 {
1391                         if (*(const unsigned int *)&pixelmask[x] == 0x01010101)
1392                         {
1393                                 _mm_storeu_si128((__m128i *)&pixeli[x], _mm_loadu_si128((const __m128i *)&ini[x]));
1394                                 x += 4;
1395                         }
1396                         else
1397                         {
1398                                 if (pixelmask[x])
1399                                         pixeli[x] = ini[x];
1400                                 x++;
1401                         }
1402                 }
1403                 for (;x < endx;x++)
1404                         if (pixelmask[x])
1405                                 pixeli[x] = ini[x];
1406                 break;
1407         case DPSOFTRAST_BLENDMODE_ALPHA:
1408         #define FINISHBLEND(blend2, blend1) \
1409                 for (x = startx;x + 2 <= endx;x += 2) \
1410                 { \
1411                         __m128i src, dst; \
1412                         switch (*(const unsigned short*)&pixelmask[x]) \
1413                         { \
1414                         case 0x0101: \
1415                                 src = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ini[x]), _mm_setzero_si128()); \
1416                                 dst = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixeli[x]), _mm_setzero_si128()); \
1417                                 blend2; \
1418                                 _mm_storel_epi64((__m128i *)&pixeli[x], _mm_packus_epi16(dst, dst)); \
1419                                 continue; \
1420                         case 0x0100: \
1421                                 src = _mm_unpacklo_epi8(_mm_cvtsi32_si128(ini[x+1]), _mm_setzero_si128()); \
1422                                 dst = _mm_unpacklo_epi8(_mm_cvtsi32_si128(pixeli[x+1]), _mm_setzero_si128()); \
1423                                 blend1; \
1424                                 pixeli[x+1] = _mm_cvtsi128_si32(_mm_packus_epi16(dst, dst));  \
1425                                 continue; \
1426                         case 0x0001: \
1427                                 src = _mm_unpacklo_epi8(_mm_cvtsi32_si128(ini[x]), _mm_setzero_si128()); \
1428                                 dst = _mm_unpacklo_epi8(_mm_cvtsi32_si128(pixeli[x]), _mm_setzero_si128()); \
1429                                 blend1; \
1430                                 pixeli[x] = _mm_cvtsi128_si32(_mm_packus_epi16(dst, dst)); \
1431                                 continue; \
1432                         } \
1433                         break; \
1434                 } \
1435                 for(;x < endx; x++) \
1436                 { \
1437                         __m128i src, dst; \
1438                         if (!pixelmask[x]) \
1439                                 continue; \
1440                         src = _mm_unpacklo_epi8(_mm_cvtsi32_si128(ini[x]), _mm_setzero_si128()); \
1441                         dst = _mm_unpacklo_epi8(_mm_cvtsi32_si128(pixeli[x]), _mm_setzero_si128()); \
1442                         blend1; \
1443                         pixeli[x] = _mm_cvtsi128_si32(_mm_packus_epi16(dst, dst)); \
1444                 }
1445
1446                 FINISHBLEND({
1447                         __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
1448                         dst = _mm_add_epi16(dst, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(src, dst), 4), _mm_slli_epi16(blend, 4)));
1449                 }, {
1450                         __m128i blend = _mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3));
1451                         dst = _mm_add_epi16(dst, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(src, dst), 4), _mm_slli_epi16(blend, 4)));
1452                 });
1453                 break;
1454         case DPSOFTRAST_BLENDMODE_ADDALPHA:
1455                 FINISHBLEND({
1456                         __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
1457                         dst = _mm_add_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(src, blend), 8));
1458                 }, {
1459                         __m128i blend = _mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3));
1460                         dst = _mm_add_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(src, blend), 8));
1461                 });
1462                 break;
1463         case DPSOFTRAST_BLENDMODE_ADD:
1464                 FINISHBLEND({ dst = _mm_add_epi16(src, dst); }, { dst = _mm_add_epi16(src, dst); });
1465                 break;
1466         case DPSOFTRAST_BLENDMODE_INVMOD:
1467                 FINISHBLEND({
1468                         dst = _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(dst, src), 8));
1469                 }, {
1470                         dst = _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(dst, src), 8));
1471                 });
1472                 break;
1473         case DPSOFTRAST_BLENDMODE_MUL:
1474                 FINISHBLEND({ dst = _mm_srli_epi16(_mm_mullo_epi16(src, dst), 8); }, { dst = _mm_srli_epi16(_mm_mullo_epi16(src, dst), 8); });
1475                 break;
1476         case DPSOFTRAST_BLENDMODE_MUL2:
1477                 FINISHBLEND({ dst = _mm_srli_epi16(_mm_mullo_epi16(src, dst), 7); }, { dst = _mm_srli_epi16(_mm_mullo_epi16(src, dst), 7); });
1478                 break;
1479         case DPSOFTRAST_BLENDMODE_SUBALPHA:
1480                 FINISHBLEND({
1481                         __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
1482                         dst = _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(src, blend), 8));
1483                 }, {
1484                         __m128i blend = _mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3));
1485                         dst = _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(src, blend), 8));
1486                 });
1487                 break;
1488         case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1489                 FINISHBLEND({
1490                         __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
1491                         dst = _mm_add_epi16(src, _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(dst, blend), 8)));
1492                 }, {
1493                         __m128i blend = _mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3));
1494                         dst = _mm_add_epi16(src, _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(dst, blend), 8)));
1495                 });
1496                 break;
1497         }
1498 #endif
1499 }
1500
1501 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float * RESTRICT out4f, int texunitindex, int arrayindex, const float * RESTRICT zf)
1502 {
1503         int x;
1504         int startx = span->startx;
1505         int endx = span->endx;
1506         int flags;
1507         float c[4];
1508         float data[4];
1509         float slope[4];
1510         float tc[2], endtc[2];
1511         float tcscale[2];
1512         unsigned int tci[2];
1513         unsigned int tci1[2];
1514         unsigned int tcimin[2];
1515         unsigned int tcimax[2];
1516         int tciwrapmask[2];
1517         int tciwidth;
1518         int filter;
1519         int mip;
1520         const unsigned char * RESTRICT pixelbase;
1521         const unsigned char * RESTRICT pixel[4];
1522         DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1523         // if no texture is bound, just fill it with white
1524         if (!texture)
1525         {
1526                 for (x = startx;x < endx;x++)
1527                 {
1528                         out4f[x*4+0] = 1.0f;
1529                         out4f[x*4+1] = 1.0f;
1530                         out4f[x*4+2] = 1.0f;
1531                         out4f[x*4+3] = 1.0f;
1532                 }
1533                 return;
1534         }
1535         mip = span->mip[texunitindex];
1536         pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
1537         // if this mipmap of the texture is 1 pixel, just fill it with that color
1538         if (texture->mipmap[mip][1] == 4)
1539         {
1540                 c[0] = texture->bytes[2] * (1.0f/255.0f);
1541                 c[1] = texture->bytes[1] * (1.0f/255.0f);
1542                 c[2] = texture->bytes[0] * (1.0f/255.0f);
1543                 c[3] = texture->bytes[3] * (1.0f/255.0f);
1544                 for (x = startx;x < endx;x++)
1545                 {
1546                         out4f[x*4+0] = c[0];
1547                         out4f[x*4+1] = c[1];
1548                         out4f[x*4+2] = c[2];
1549                         out4f[x*4+3] = c[3];
1550                 }
1551                 return;
1552         }
1553         filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1554         data[0] = span->data[0][arrayindex][0];
1555         data[1] = span->data[0][arrayindex][1];
1556         data[2] = span->data[0][arrayindex][2];
1557         data[3] = span->data[0][arrayindex][3];
1558         slope[0] = span->data[1][arrayindex][0];
1559         slope[1] = span->data[1][arrayindex][1];
1560         slope[2] = span->data[1][arrayindex][2];
1561         slope[3] = span->data[1][arrayindex][3];
1562         flags = texture->flags;
1563         tcscale[0] = texture->mipmap[mip][2];
1564         tcscale[1] = texture->mipmap[mip][3];
1565         tciwidth = texture->mipmap[mip][2];
1566         tcimin[0] = 0;
1567         tcimin[1] = 0;
1568         tcimax[0] = texture->mipmap[mip][2]-1;
1569         tcimax[1] = texture->mipmap[mip][3]-1;
1570         tciwrapmask[0] = texture->mipmap[mip][2]-1;
1571         tciwrapmask[1] = texture->mipmap[mip][3]-1;
1572         endtc[0] = (data[0] + slope[0]*startx) * zf[startx] * tcscale[0] - 0.5f;
1573         endtc[1] = (data[1] + slope[1]*startx) * zf[startx] * tcscale[1] - 0.5f;
1574         for (x = startx;x < endx;)
1575         {
1576                 unsigned int subtc[2];
1577                 unsigned int substep[2];
1578                 float subscale = 65536.0f/DPSOFTRAST_MAXSUBSPAN;
1579                 int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1;
1580                 if(nextsub >= endx)
1581                 {
1582                         nextsub = endsub = endx-1;      
1583                         if(x < nextsub) subscale = 65536.0f / (nextsub - x);
1584                 }
1585                 tc[0] = endtc[0];
1586                 tc[1] = endtc[1];
1587                 endtc[0] = (data[0] + slope[0]*nextsub) * zf[nextsub] * tcscale[0] - 0.5f;
1588                 endtc[1] = (data[1] + slope[1]*nextsub) * zf[nextsub] * tcscale[1] - 0.5f;
1589                 substep[0] = (endtc[0] - tc[0]) * subscale;
1590                 substep[1] = (endtc[1] - tc[1]) * subscale;
1591                 subtc[0] = tc[0] * (1<<16);
1592                 subtc[1] = tc[1] * (1<<16);
1593                 if(filter)
1594                 {
1595                         if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1596                         {
1597                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1598                                 {
1599                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1600                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1601                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1602                                         tci[0] = subtc[0]>>16;
1603                                         tci[1] = subtc[1]>>16;
1604                                         tci1[0] = tci[0] + 1;
1605                                         tci1[1] = tci[1] + 1;
1606                                         tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1607                                         tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1608                                         tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1609                                         tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1610                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1611                                         pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1612                                         pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1613                                         pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1614                                         c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF000000);
1615                                         c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF000000);
1616                                         c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF000000);
1617                                         c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF000000);
1618                                         out4f[x*4+0] = c[0];
1619                                         out4f[x*4+1] = c[1];
1620                                         out4f[x*4+2] = c[2];
1621                                         out4f[x*4+3] = c[3];
1622                                 }
1623                         }
1624                         else
1625                         {
1626                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1627                                 {
1628                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1629                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1630                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1631                                         tci[0] = subtc[0]>>16;
1632                                         tci[1] = subtc[1]>>16;
1633                                         tci1[0] = tci[0] + 1;
1634                                         tci1[1] = tci[1] + 1;
1635                                         tci[0] &= tciwrapmask[0];
1636                                         tci[1] &= tciwrapmask[1];
1637                                         tci1[0] &= tciwrapmask[0];
1638                                         tci1[1] &= tciwrapmask[1];
1639                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1640                                         pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1641                                         pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1642                                         pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1643                                         c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF000000);
1644                                         c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF000000);
1645                                         c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF000000);
1646                                         c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF000000);
1647                                         out4f[x*4+0] = c[0];
1648                                         out4f[x*4+1] = c[1];
1649                                         out4f[x*4+2] = c[2];
1650                                         out4f[x*4+3] = c[3];
1651                                 }
1652                         }
1653                 }
1654                 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1655                 {
1656                         for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1657                         {
1658                                 tci[0] = subtc[0]>>16;
1659                                 tci[1] = subtc[1]>>16;
1660                                 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1661                                 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1662                                 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1663                                 c[0] = pixel[0][2] * (1.0f / 255.0f);
1664                                 c[1] = pixel[0][1] * (1.0f / 255.0f);
1665                                 c[2] = pixel[0][0] * (1.0f / 255.0f);
1666                                 c[3] = pixel[0][3] * (1.0f / 255.0f);
1667                                 out4f[x*4+0] = c[0];
1668                                 out4f[x*4+1] = c[1];
1669                                 out4f[x*4+2] = c[2];
1670                                 out4f[x*4+3] = c[3];
1671                         }
1672                 }
1673                 else
1674                 {
1675                         for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1676                         {
1677                                 tci[0] = subtc[0]>>16;
1678                                 tci[1] = subtc[1]>>16;
1679                                 tci[0] &= tciwrapmask[0];
1680                                 tci[1] &= tciwrapmask[1];
1681                                 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1682                                 c[0] = pixel[0][2] * (1.0f / 255.0f);
1683                                 c[1] = pixel[0][1] * (1.0f / 255.0f);
1684                                 c[2] = pixel[0][0] * (1.0f / 255.0f);
1685                                 c[3] = pixel[0][3] * (1.0f / 255.0f);
1686                                 out4f[x*4+0] = c[0];
1687                                 out4f[x*4+1] = c[1];
1688                                 out4f[x*4+2] = c[2];
1689                                 out4f[x*4+3] = c[3];
1690                         }
1691                 }
1692         }
1693 }
1694
1695 void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf)
1696 {
1697 #ifdef SSE2_PRESENT
1698         int x;
1699         int startx = span->startx;
1700         int endx = span->endx;
1701         int flags;
1702         __m128 data, slope, tcscale;
1703         __m128i tcsize, tcmask, tcoffset, tcmax;
1704         __m128 tc, endtc;
1705         __m128i subtc, substep, endsubtc;
1706         int filter;
1707         int mip;
1708         unsigned int *outi = (unsigned int *)out4ub;
1709         const unsigned char * RESTRICT pixelbase;
1710         DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1711         // if no texture is bound, just fill it with white
1712         if (!texture)
1713         {
1714                 memset(out4ub + startx*4, 255, span->length*4);
1715                 return;
1716         }
1717         mip = span->mip[texunitindex];
1718         pixelbase = (const unsigned char *)texture->bytes + texture->mipmap[mip][0];
1719         // if this mipmap of the texture is 1 pixel, just fill it with that color
1720         if (texture->mipmap[mip][1] == 4)
1721         {
1722                 unsigned int k = *((const unsigned int *)pixelbase);
1723                 for (x = startx;x < endx;x++)
1724                         outi[x] = k;
1725                 return;
1726         }
1727         filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1728         data = _mm_load_ps(span->data[0][arrayindex]);
1729         slope = _mm_load_ps(span->data[1][arrayindex]);
1730         flags = texture->flags;
1731         tcsize = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i *)&texture->mipmap[mip][0]), _MM_SHUFFLE(3, 2, 3, 2));
1732         tcmask = _mm_sub_epi32(tcsize, _mm_set1_epi32(1));
1733         tcscale = _mm_cvtepi32_ps(tcsize);
1734         data = _mm_mul_ps(_mm_shuffle_ps(data, data, _MM_SHUFFLE(1, 0, 1, 0)), tcscale);
1735         slope = _mm_mul_ps(_mm_shuffle_ps(slope, slope, _MM_SHUFFLE(1, 0, 1, 0)), tcscale);
1736         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));
1737         endsubtc = _mm_cvtps_epi32(_mm_mul_ps(endtc, _mm_set1_ps(65536.0f)));
1738         tcoffset = _mm_add_epi32(_mm_slli_epi32(_mm_shuffle_epi32(tcsize, _MM_SHUFFLE(0, 0, 0, 0)), 18), _mm_set1_epi32(4));
1739         tcmax = filter ? _mm_packs_epi32(tcmask, tcmask) : _mm_slli_epi32(tcmask, 16);  
1740         for (x = startx;x < endx;)
1741         {
1742                 int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1;
1743                 __m128 subscale = _mm_set1_ps(65536.0f/DPSOFTRAST_MAXSUBSPAN);
1744                 if(nextsub >= endx)
1745                 {
1746                         nextsub = endsub = endx-1;
1747                         if(x < nextsub) subscale = _mm_set1_ps(65536.0f / (nextsub - x));
1748                 }       
1749                 tc = endtc;
1750                 subtc = endsubtc;
1751                 endtc = _mm_sub_ps(_mm_mul_ps(_mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(nextsub))), _mm_load1_ps(&zf[nextsub])), _mm_set1_ps(0.5f));
1752                 substep = _mm_cvtps_epi32(_mm_mul_ps(_mm_sub_ps(endtc, tc), subscale));
1753                 endsubtc = _mm_cvtps_epi32(_mm_mul_ps(endtc, _mm_set1_ps(65536.0f)));
1754                 subtc = _mm_unpacklo_epi64(subtc, _mm_add_epi32(subtc, substep));
1755                 substep = _mm_slli_epi32(substep, 1);
1756                 if (filter)
1757                 {
1758                         __m128i tcrange = _mm_srai_epi32(_mm_unpacklo_epi64(subtc, _mm_add_epi32(endsubtc, substep)), 16);
1759                         if (_mm_movemask_epi8(_mm_andnot_si128(_mm_cmplt_epi32(tcrange, _mm_setzero_si128()), _mm_cmplt_epi32(tcrange, tcmask))) == 0xFFFF)
1760                         {
1761                                 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1762                                 {
1763                                         __m128i tci = _mm_shufflehi_epi16(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 1, 3, 1)), pix1, pix2, pix3, pix4, fracm;
1764                                         tci = _mm_madd_epi16(_mm_add_epi16(tci, _mm_setr_epi32(0, 0x10000, 0, 0x10000)), tcoffset);
1765                                         pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(tci)]), _mm_setzero_si128());
1766                                         pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))]), _mm_setzero_si128());
1767                                         pix3 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]), _mm_setzero_si128());
1768                                         pix4 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))]), _mm_setzero_si128());
1769                                         fracm = _mm_srli_epi16(subtc, 1);
1770                                         pix1 = _mm_add_epi16(pix1,
1771                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1772                                                                                                                  _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1773                                         pix3 = _mm_add_epi16(pix3,
1774                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix3), 1),
1775                                                                                                                  _mm_shuffle_epi32(_mm_shufflehi_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(3, 2, 3, 2))));
1776                                         pix2 = _mm_unpacklo_epi64(pix1, pix3);
1777                                         pix4 = _mm_unpackhi_epi64(pix1, pix3);
1778                                         pix2 = _mm_add_epi16(pix2,
1779                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix2), 1),
1780                                                                                                                  _mm_shufflehi_epi16(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0)), _MM_SHUFFLE(0, 0, 0, 0))));
1781                                         _mm_storel_epi64((__m128i *)&outi[x], _mm_packus_epi16(pix2, _mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 2, 3, 2))));
1782                                 }
1783                                 if (x <= endsub)
1784                                 {
1785                                         __m128i tci = _mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), pix1, pix2, fracm;
1786                                         tci = _mm_madd_epi16(_mm_add_epi16(tci, _mm_setr_epi32(0, 0x10000, 0, 0)), tcoffset);
1787                                         pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(tci)]), _mm_setzero_si128());
1788                                         pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))]), _mm_setzero_si128());
1789                                         fracm = _mm_srli_epi16(subtc, 1);
1790                                         pix1 = _mm_add_epi16(pix1,
1791                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1792                                                                                                                  _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1793                                         pix2 = _mm_shuffle_epi32(pix1, _MM_SHUFFLE(3, 2, 3, 2));
1794                                         pix1 = _mm_add_epi16(pix1,
1795                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1796                                                                                                                  _mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0))));
1797                                         outi[x] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
1798                                         x++;
1799                                 }
1800                         }
1801                         else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1802                         {
1803                                 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1804                                 {
1805                                         __m128i tci = _mm_shuffle_epi32(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(1, 0, 1, 0)), pix1, pix2, pix3, pix4, fracm;
1806                                         tci = _mm_min_epi16(_mm_max_epi16(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), _mm_setzero_si128()), tcmax);
1807                                         tci = _mm_madd_epi16(tci, tcoffset);
1808                                         pix1 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1809                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1810                                                                                         _mm_setzero_si128());
1811                                         pix2 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1812                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1813                                                                                         _mm_setzero_si128());
1814                                         tci = _mm_shuffle_epi32(_mm_shufflehi_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 2, 3, 2));
1815                                         tci = _mm_and_si128(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), tcmax);
1816                                         tci = _mm_madd_epi16(tci, tcoffset);
1817                                         pix3 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1818                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1819                                                                                         _mm_setzero_si128());
1820                                         pix4 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1821                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1822                                                                                         _mm_setzero_si128());
1823                                         fracm = _mm_srli_epi16(subtc, 1);
1824                                         pix1 = _mm_add_epi16(pix1,
1825                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1826                                                                                                                  _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1827                                         pix3 = _mm_add_epi16(pix3,
1828                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix3), 1),
1829                                                                                                                  _mm_shuffle_epi32(_mm_shufflehi_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(3, 2, 3, 2))));
1830                                         pix2 = _mm_unpacklo_epi64(pix1, pix3);
1831                                         pix4 = _mm_unpackhi_epi64(pix1, pix3);
1832                                         pix2 = _mm_add_epi16(pix2,
1833                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix2), 1),
1834                                                                                                                  _mm_shufflehi_epi16(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0)), _MM_SHUFFLE(0, 0, 0, 0))));
1835                                         _mm_storel_epi64((__m128i *)&outi[x], _mm_packus_epi16(pix2, _mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 2, 3, 2))));
1836                                 }
1837                                 if (x <= endsub)
1838                                 {
1839                                         __m128i tci = _mm_shuffle_epi32(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(1, 0, 1, 0)), pix1, pix2, fracm;
1840                                         tci = _mm_min_epi16(_mm_max_epi16(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), _mm_setzero_si128()), tcmax);
1841                                         tci = _mm_madd_epi16(tci, tcoffset);
1842                                         pix1 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]), 
1843                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])), 
1844                                                                                         _mm_setzero_si128());
1845                                         pix2 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]), 
1846                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])), 
1847                                                                                         _mm_setzero_si128());
1848                                         fracm = _mm_srli_epi16(subtc, 1);
1849                                         pix1 = _mm_add_epi16(pix1,
1850                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1851                                                                                                                  _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1852                                         pix2 = _mm_shuffle_epi32(pix1, _MM_SHUFFLE(3, 2, 3, 2));
1853                                         pix1 = _mm_add_epi16(pix1,
1854                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1855                                                                                                                  _mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0))));
1856                                         outi[x] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
1857                                         x++;
1858                                 }
1859                         }
1860                         else
1861                         {
1862                                 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1863                                 {
1864                                         __m128i tci = _mm_shuffle_epi32(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(1, 0, 1, 0)), pix1, pix2, pix3, pix4, fracm;
1865                                         tci = _mm_and_si128(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), tcmax);
1866                                         tci = _mm_madd_epi16(tci, tcoffset);
1867                                         pix1 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1868                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1869                                                                                         _mm_setzero_si128());
1870                                         pix2 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1871                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1872                                                                                         _mm_setzero_si128());
1873                                         tci = _mm_shuffle_epi32(_mm_shufflehi_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 2, 3, 2));
1874                                         tci = _mm_and_si128(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), tcmax);
1875                                         tci = _mm_madd_epi16(tci, tcoffset);
1876                                         pix3 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1877                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1878                                                                                         _mm_setzero_si128());
1879                                         pix4 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1880                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1881                                                                                         _mm_setzero_si128());
1882                                         fracm = _mm_srli_epi16(subtc, 1);
1883                                         pix1 = _mm_add_epi16(pix1,
1884                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1885                                                                                                                  _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1886                                         pix3 = _mm_add_epi16(pix3,
1887                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix3), 1),
1888                                                                                                                  _mm_shuffle_epi32(_mm_shufflehi_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(3, 2, 3, 2))));
1889                                         pix2 = _mm_unpacklo_epi64(pix1, pix3);
1890                                         pix4 = _mm_unpackhi_epi64(pix1, pix3);
1891                                         pix2 = _mm_add_epi16(pix2,
1892                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix2), 1),
1893                                                                                                                  _mm_shufflehi_epi16(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0)), _MM_SHUFFLE(0, 0, 0, 0))));
1894                                         _mm_storel_epi64((__m128i *)&outi[x], _mm_packus_epi16(pix2, _mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 2, 3, 2))));
1895                                 }
1896                                 if (x <= endsub)
1897                                 {
1898                                         __m128i tci = _mm_shuffle_epi32(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(1, 0, 1, 0)), pix1, pix2, fracm;
1899                                         tci = _mm_and_si128(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), tcmax);
1900                                         tci = _mm_madd_epi16(tci, tcoffset);
1901                                         pix1 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),                                                                                        
1902                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1903                                                                                         _mm_setzero_si128());
1904                                         pix2 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1905                                                                                                                                 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1906                                                                                         _mm_setzero_si128());
1907                                         fracm = _mm_srli_epi16(subtc, 1);
1908                                         pix1 = _mm_add_epi16(pix1,
1909                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1910                                                                                                                  _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1911                                         pix2 = _mm_shuffle_epi32(pix1, _MM_SHUFFLE(3, 2, 3, 2));
1912                                         pix1 = _mm_add_epi16(pix1,
1913                                                                                  _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1914                                                                                                                  _mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0))));
1915                                         outi[x] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
1916                                         x++;
1917                                 }
1918                         }
1919                 }
1920                 else
1921                 {
1922                         if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1923                         {
1924                                 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1925                                 {
1926                                         __m128i tci = _mm_min_epi16(_mm_max_epi16(subtc, _mm_setzero_si128()), tcmax); 
1927                                         tci = _mm_shufflehi_epi16(_mm_shufflelo_epi16(tci, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 1, 3, 1));
1928                                         tci = _mm_madd_epi16(tci, tcoffset);
1929                                         outi[x] = *(const int *)&pixelbase[_mm_cvtsi128_si32(tci)];
1930                                         outi[x+1] = *(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))];
1931                                 }
1932                                 if (x <= endsub)
1933                                 {
1934                                         __m128i tci = _mm_min_epi16(_mm_max_epi16(subtc, _mm_setzero_si128()), tcmax);
1935                                         tci = _mm_shufflelo_epi16(tci, _MM_SHUFFLE(3, 1, 3, 1));
1936                                         tci = _mm_madd_epi16(tci, tcoffset);
1937                                         outi[x] = *(const int *)&pixelbase[_mm_cvtsi128_si32(tci)];
1938                                         x++;
1939                                 }
1940                         }
1941                         else
1942                         {
1943                                 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1944                                 {
1945                                         __m128i tci = _mm_and_si128(subtc, tcmax); 
1946                                         tci = _mm_shufflehi_epi16(_mm_shufflelo_epi16(tci, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 1, 3, 1));
1947                                         tci = _mm_madd_epi16(tci, tcoffset);
1948                                         outi[x] = *(const int *)&pixelbase[_mm_cvtsi128_si32(tci)];
1949                                         outi[x+1] = *(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))];
1950                                 }
1951                                 if (x <= endsub)
1952                                 {
1953                                         __m128i tci = _mm_and_si128(subtc, tcmax); 
1954                                         tci = _mm_shufflelo_epi16(tci, _MM_SHUFFLE(3, 1, 3, 1));
1955                                         tci = _mm_madd_epi16(tci, tcoffset);
1956                                         outi[x] = *(const int *)&pixelbase[_mm_cvtsi128_si32(tci)];
1957                                         x++;
1958                                 }
1959                         }
1960                 }
1961         }
1962 #endif
1963 }
1964
1965 void DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf)
1966 {
1967         // TODO: IMPLEMENT
1968         memset(out4ub, 255, span->length*4);
1969 }
1970
1971 float DPSOFTRAST_SampleShadowmap(const float *vector)
1972 {
1973         // TODO: IMPLEMENT
1974         return 1.0f;
1975 }
1976
1977 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, int arrayindex, const float *zf)
1978 {
1979         int x;
1980         int startx = span->startx;
1981         int endx = span->endx;
1982         float c[4];
1983         float data[4];
1984         float slope[4];
1985         float z;
1986         data[0] = span->data[0][arrayindex][0];
1987         data[1] = span->data[0][arrayindex][1];
1988         data[2] = span->data[0][arrayindex][2];
1989         data[3] = span->data[0][arrayindex][3];
1990         slope[0] = span->data[1][arrayindex][0];
1991         slope[1] = span->data[1][arrayindex][1];
1992         slope[2] = span->data[1][arrayindex][2];
1993         slope[3] = span->data[1][arrayindex][3];
1994         for (x = startx;x < endx;x++)
1995         {
1996                 z = zf[x];
1997                 c[0] = (data[0] + slope[0]*x) * z;
1998                 c[1] = (data[1] + slope[1]*x) * z;
1999                 c[2] = (data[2] + slope[2]*x) * z;
2000                 c[3] = (data[3] + slope[3]*x) * z;
2001                 out4f[x*4+0] = in4f[x*4+0] * c[0];
2002                 out4f[x*4+1] = in4f[x*4+1] * c[1];
2003                 out4f[x*4+2] = in4f[x*4+2] * c[2];
2004                 out4f[x*4+3] = in4f[x*4+3] * c[3];
2005         }
2006 }
2007
2008 void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, int arrayindex, const float *zf)
2009 {
2010         int x;
2011         int startx = span->startx;
2012         int endx = span->endx;
2013         float c[4];
2014         float data[4];
2015         float slope[4];
2016         float z;
2017         data[0] = span->data[0][arrayindex][0];
2018         data[1] = span->data[0][arrayindex][1];
2019         data[2] = span->data[0][arrayindex][2];
2020         data[3] = span->data[0][arrayindex][3];
2021         slope[0] = span->data[1][arrayindex][0];
2022         slope[1] = span->data[1][arrayindex][1];
2023         slope[2] = span->data[1][arrayindex][2];
2024         slope[3] = span->data[1][arrayindex][3];
2025         for (x = startx;x < endx;x++)
2026         {
2027                 z = zf[x];
2028                 c[0] = (data[0] + slope[0]*x) * z;
2029                 c[1] = (data[1] + slope[1]*x) * z;
2030                 c[2] = (data[2] + slope[2]*x) * z;
2031                 c[3] = (data[3] + slope[3]*x) * z;
2032                 out4f[x*4+0] = c[0];
2033                 out4f[x*4+1] = c[1];
2034                 out4f[x*4+2] = c[2];
2035                 out4f[x*4+3] = c[3];
2036         }
2037 }
2038
2039 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
2040 {
2041         int x, startx = span->startx, endx = span->endx;
2042         float c[4], localcolor[4];
2043         localcolor[0] = subcolor[0];
2044         localcolor[1] = subcolor[1];
2045         localcolor[2] = subcolor[2];
2046         localcolor[3] = subcolor[3];
2047         for (x = startx;x < endx;x++)
2048         {
2049                 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
2050                 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
2051                 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
2052                 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
2053                 out4f[x*4+0] = ina4f[x*4+0] + c[0];
2054                 out4f[x*4+1] = ina4f[x*4+1] + c[1];
2055                 out4f[x*4+2] = ina4f[x*4+2] + c[2];
2056                 out4f[x*4+3] = ina4f[x*4+3] + c[3];
2057         }
2058 }
2059
2060 void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2061 {
2062         int x, startx = span->startx, endx = span->endx;
2063         for (x = startx;x < endx;x++)
2064         {
2065                 out4f[x*4+0] = ina4f[x*4+0] * inb4f[x*4+0];
2066                 out4f[x*4+1] = ina4f[x*4+1] * inb4f[x*4+1];
2067                 out4f[x*4+2] = ina4f[x*4+2] * inb4f[x*4+2];
2068                 out4f[x*4+3] = ina4f[x*4+3] * inb4f[x*4+3];
2069         }
2070 }
2071
2072 void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2073 {
2074         int x, startx = span->startx, endx = span->endx;
2075         for (x = startx;x < endx;x++)
2076         {
2077                 out4f[x*4+0] = ina4f[x*4+0] + inb4f[x*4+0];
2078                 out4f[x*4+1] = ina4f[x*4+1] + inb4f[x*4+1];
2079                 out4f[x*4+2] = ina4f[x*4+2] + inb4f[x*4+2];
2080                 out4f[x*4+3] = ina4f[x*4+3] + inb4f[x*4+3];
2081         }
2082 }
2083
2084 void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2085 {
2086         int x, startx = span->startx, endx = span->endx;
2087         float a, b;
2088         for (x = startx;x < endx;x++)
2089         {
2090                 a = 1.0f - inb4f[x*4+3];
2091                 b = inb4f[x*4+3];
2092                 out4f[x*4+0] = ina4f[x*4+0] * a + inb4f[x*4+0] * b;
2093                 out4f[x*4+1] = ina4f[x*4+1] * a + inb4f[x*4+1] * b;
2094                 out4f[x*4+2] = ina4f[x*4+2] * a + inb4f[x*4+2] * b;
2095                 out4f[x*4+3] = ina4f[x*4+3] * a + inb4f[x*4+3] * b;
2096         }
2097 }
2098
2099 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, const float *color)
2100 {
2101         int x, startx = span->startx, endx = span->endx;
2102         float localcolor[4], ilerp, lerp;
2103         localcolor[0] = color[0];
2104         localcolor[1] = color[1];
2105         localcolor[2] = color[2];
2106         localcolor[3] = color[3];
2107         ilerp = 1.0f - localcolor[3];
2108         lerp = localcolor[3];
2109         for (x = startx;x < endx;x++)
2110         {
2111                 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
2112                 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
2113                 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
2114                 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
2115         }
2116 }
2117
2118
2119
2120 void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, int arrayindex, const float *zf)
2121 {
2122 #ifdef SSE2_PRESENT
2123         int x;
2124         int startx = span->startx;
2125         int endx = span->endx;
2126         __m128 data = _mm_load_ps(span->data[0][arrayindex]), slope = _mm_load_ps(span->data[1][arrayindex]);
2127         data = _mm_shuffle_ps(data, data, _MM_SHUFFLE(3, 0, 1, 2));
2128         slope = _mm_shuffle_ps(slope, slope, _MM_SHUFFLE(3, 0, 1, 2));
2129         data = _mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx)));
2130         data = _mm_mul_ps(data, _mm_set1_ps(256.0f));
2131         slope = _mm_mul_ps(slope, _mm_set1_ps(256.0f));
2132         for (x = startx;x+2 <= endx;x += 2, data = _mm_add_ps(data, slope))
2133         {
2134                 __m128i pix = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_loadl_epi64((const __m128i *)&in4ub[x*4]));
2135                 __m128i mod = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x]))), mod2;
2136                 data = _mm_add_ps(data, slope);
2137                 mod2 = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x+1])));
2138                 mod = _mm_unpacklo_epi64(_mm_packs_epi32(mod, mod), _mm_packs_epi32(mod2, mod2));
2139                 pix = _mm_mulhi_epu16(pix, mod);
2140                 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix, pix));
2141         }
2142         for (;x < endx;x++, data = _mm_add_ps(data, slope))
2143         {
2144                 __m128i pix = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&in4ub[x*4]));
2145                 __m128i mod = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x])));
2146                 mod = _mm_packs_epi32(mod, mod);
2147                 pix = _mm_mulhi_epu16(pix, mod);
2148                 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2149         }
2150 #endif
2151 }
2152
2153 void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, int arrayindex, const float *zf)
2154 {
2155 #ifdef SSE2_PRESENT
2156         int x;
2157         int startx = span->startx;
2158         int endx = span->endx;
2159         __m128 data = _mm_load_ps(span->data[0][arrayindex]), slope = _mm_load_ps(span->data[1][arrayindex]);
2160         data = _mm_shuffle_ps(data, data, _MM_SHUFFLE(3, 0, 1, 2));
2161         slope = _mm_shuffle_ps(slope, slope, _MM_SHUFFLE(3, 0, 1, 2));
2162         data = _mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx)));
2163         data = _mm_mul_ps(data, _mm_set1_ps(255.0f));
2164         slope = _mm_mul_ps(slope, _mm_set1_ps(255.0f));
2165         for (x = startx;x+2 <= endx;x += 2, data = _mm_add_ps(data, slope))
2166         {
2167                 __m128i pix = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x]))), pix2;
2168                 data = _mm_add_ps(data, slope);
2169                 pix2 = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x+1])));
2170                 pix = _mm_unpacklo_epi64(_mm_packs_epi32(pix, pix), _mm_packs_epi32(pix2, pix2));
2171                 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix, pix));
2172         }
2173         for (;x < endx;x++, data = _mm_add_ps(data, slope))
2174         {
2175                 __m128i pix = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x])));
2176                 pix = _mm_packs_epi32(pix, pix);
2177                 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2178         }
2179 #endif
2180 }
2181
2182 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)
2183 {
2184 #ifdef SSE2_PRESENT
2185         int x, startx = span->startx, endx = span->endx;
2186         __m128i localcolor = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(subcolor), _mm_set1_ps(255.0f))), _MM_SHUFFLE(3, 0, 1, 2));
2187         localcolor = _mm_shuffle_epi32(_mm_packs_epi32(localcolor, localcolor), _MM_SHUFFLE(1, 0, 1, 0));
2188         for (x = startx;x+2 <= endx;x+=2)
2189         {
2190                 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2191                 __m128i pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&inb4ub[x*4]), _mm_setzero_si128());
2192                 pix1 = _mm_add_epi16(pix1, _mm_sub_epi16(pix2, localcolor));
2193                 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2194         }
2195         if(x < endx)
2196         {
2197                 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2198                 __m128i pix2 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]), _mm_setzero_si128());
2199                 pix1 = _mm_add_epi16(pix1, _mm_sub_epi16(pix2, localcolor));
2200                 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2201         }
2202 #endif
2203 }
2204
2205 void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2206 {
2207 #ifdef SSE2_PRESENT
2208         int x, startx = span->startx, endx = span->endx;
2209         for (x = startx;x+2 <= endx;x+=2)
2210         {
2211                 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2212                 __m128i pix2 = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_loadl_epi64((const __m128i *)&inb4ub[x*4]));
2213                 pix1 = _mm_mulhi_epu16(pix1, pix2);
2214                 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2215         }
2216         if(x < endx)
2217         {
2218                 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2219                 __m128i pix2 = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]));
2220                 pix1 = _mm_mulhi_epu16(pix1, pix2);
2221                 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2222         }
2223 #endif
2224 }
2225
2226 void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2227 {
2228 #ifdef SSE2_PRESENT
2229         int x, startx = span->startx, endx = span->endx;
2230         for (x = startx;x+2 <= endx;x+=2)
2231         {
2232                 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2233                 __m128i pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&inb4ub[x*4]), _mm_setzero_si128());
2234                 pix1 = _mm_add_epi16(pix1, pix2);
2235                 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2236         }
2237         if(x < endx)
2238         {
2239                 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2240                 __m128i pix2 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]), _mm_setzero_si128());
2241                 pix1 = _mm_add_epi16(pix1, pix2);
2242                 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2243         }
2244 #endif
2245 }
2246
2247 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)
2248 {
2249 #ifdef SSE2_PRESENT
2250         int x, startx = span->startx, endx = span->endx;
2251         __m128i tint = _mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(inbtintbgra), _mm_set1_ps(256.0f)));
2252         tint = _mm_shuffle_epi32(_mm_packs_epi32(tint, tint), _MM_SHUFFLE(1, 0, 1, 0));
2253         for (x = startx;x+2 <= endx;x+=2)
2254         {
2255                 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2256                 __m128i pix2 = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_loadl_epi64((const __m128i *)&inb4ub[x*4]));
2257                 pix1 = _mm_add_epi16(pix1, _mm_mulhi_epu16(tint, pix2));
2258                 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2259         }
2260         if(x < endx)
2261         {
2262                 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2263                 __m128i pix2 = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]));
2264                 pix1 = _mm_add_epi16(pix1, _mm_mulhi_epu16(tint, pix2));
2265                 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2266         }
2267 #endif
2268 }
2269
2270 void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2271 {
2272 #ifdef SSE2_PRESENT
2273         int x, startx = span->startx, endx = span->endx;
2274         for (x = startx;x+2 <= endx;x+=2)
2275         {
2276                 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2277                 __m128i pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&inb4ub[x*4]), _mm_setzero_si128());
2278                 __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
2279                 pix1 = _mm_add_epi16(pix1, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 4), _mm_slli_epi16(blend, 4)));
2280                 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2281         }
2282         if(x < endx)
2283         {
2284                 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2285                 __m128i pix2 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]), _mm_setzero_si128());
2286                 __m128i blend = _mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 3, 3, 3));
2287                 pix1 = _mm_add_epi16(pix1, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 4), _mm_slli_epi16(blend, 4)));
2288                 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2289         }
2290 #endif
2291 }
2292
2293 void DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, const float *color)
2294 {
2295 #ifdef SSE2_PRESENT
2296         int x, startx = span->startx, endx = span->endx;
2297         __m128i localcolor = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(color), _mm_set1_ps(255.0f))), _MM_SHUFFLE(3, 0, 1, 2)), blend;
2298         localcolor = _mm_shuffle_epi32(_mm_packs_epi32(localcolor, localcolor), _MM_SHUFFLE(1, 0, 1, 0));
2299         blend = _mm_slli_epi16(_mm_shufflehi_epi16(_mm_shufflelo_epi16(localcolor, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3)), 4);
2300         for (x = startx;x+2 <= endx;x+=2)
2301         {
2302                 __m128i pix = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&in4ub[x*4]), _mm_setzero_si128());
2303                 pix = _mm_add_epi16(pix, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(localcolor, pix), 4), blend));
2304                 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix, pix));
2305         }
2306         if(x < endx)
2307         {
2308                 __m128i pix = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&in4ub[x*4]), _mm_setzero_si128());
2309                 pix = _mm_add_epi16(pix, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(localcolor, pix), 4), blend));
2310                 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2311         }
2312 #endif
2313 }
2314
2315
2316
2317 void DPSOFTRAST_VertexShader_Generic(void)
2318 {
2319         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);
2320         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2321         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2322         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2323                 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2324 }
2325
2326 void DPSOFTRAST_PixelShader_Generic(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2327 {
2328         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2329         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2330         unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2331         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2332         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2333         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
2334         {
2335                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_FIRST, 2, buffer_z);
2336                 DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(span, buffer_FragColorbgra8, buffer_texture_colorbgra8, 1, buffer_z);
2337                 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2338                 {
2339                         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_SECOND, 2, buffer_z);
2340                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2341                         {
2342                                 // multiply
2343                                 DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2344                         }
2345                         else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2346                         {
2347                                 // add
2348                                 DPSOFTRAST_Draw_Span_AddBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2349                         }
2350                         else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND)
2351                         {
2352                                 // alphablend
2353                                 DPSOFTRAST_Draw_Span_MixBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2354                         }
2355                 }
2356         }
2357         else
2358                 DPSOFTRAST_Draw_Span_VaryingBGRA8(span, buffer_FragColorbgra8, 1, buffer_z);
2359         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2360 }
2361
2362
2363
2364 void DPSOFTRAST_VertexShader_PostProcess(void)
2365 {
2366         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);
2367         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2368         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2369 }
2370
2371 void DPSOFTRAST_PixelShader_PostProcess(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2372 {
2373         // TODO: optimize!!  at the very least there is no reason to use texture sampling on the frame texture
2374         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2375         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2376         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2377         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2378         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_FragColorbgra8, GL20TU_FIRST, 2, buffer_z);
2379         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
2380         {
2381                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_SECOND, 3, buffer_z);
2382                 DPSOFTRAST_Draw_Span_AddBloomBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_colorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
2383         }
2384         DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
2385         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
2386         {
2387                 // TODO: implement saturation
2388         }
2389         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
2390         {
2391                 // TODO: implement gammaramps
2392         }
2393         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2394 }
2395
2396
2397
2398 void DPSOFTRAST_VertexShader_Depth_Or_Shadow(void)
2399 {
2400         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);
2401 }
2402
2403 void DPSOFTRAST_PixelShader_Depth_Or_Shadow(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2404 {
2405         // this is never called (because colormask is off when this shader is used)
2406         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2407         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2408         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2409         memset(buffer_FragColorbgra8, 0, span->length*4);
2410         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2411 }
2412
2413
2414
2415 void DPSOFTRAST_VertexShader_FlatColor(void)
2416 {
2417         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);
2418         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);
2419 }
2420
2421 void DPSOFTRAST_PixelShader_FlatColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2422 {
2423         int x, startx = span->startx, endx = span->endx;
2424         int Color_Ambienti[4];
2425         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2426         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2427         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2428         Color_Ambienti[2] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]*256.0f);
2429         Color_Ambienti[1] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]*256.0f);
2430         Color_Ambienti[0] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]*256.0f);
2431         Color_Ambienti[3] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]        *256.0f);
2432         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2433         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2434         for (x = startx;x < endx;x++)
2435         {
2436                 buffer_FragColorbgra8[x*4+0] = (buffer_texture_colorbgra8[x*4+0] * Color_Ambienti[0])>>8;
2437                 buffer_FragColorbgra8[x*4+1] = (buffer_texture_colorbgra8[x*4+1] * Color_Ambienti[1])>>8;
2438                 buffer_FragColorbgra8[x*4+2] = (buffer_texture_colorbgra8[x*4+2] * Color_Ambienti[2])>>8;
2439                 buffer_FragColorbgra8[x*4+3] = (buffer_texture_colorbgra8[x*4+3] * Color_Ambienti[3])>>8;
2440         }
2441         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2442 }
2443
2444
2445
2446 void DPSOFTRAST_VertexShader_VertexColor(void)
2447 {
2448         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);
2449         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2450         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);
2451 }
2452
2453 void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2454 {
2455         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2456         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2457         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2458         int x, startx = span->startx, endx = span->endx;
2459         float Color_Ambient[4], Color_Diffuse[4];
2460         float data[4];
2461         float slope[4];
2462         float z;
2463         int arrayindex = DPSOFTRAST_ARRAY_COLOR;
2464         data[2] = span->data[0][arrayindex][0];
2465         data[1] = span->data[0][arrayindex][1];
2466         data[0] = span->data[0][arrayindex][2];
2467         data[3] = span->data[0][arrayindex][3];
2468         slope[2] = span->data[1][arrayindex][0];
2469         slope[1] = span->data[1][arrayindex][1];
2470         slope[0] = span->data[1][arrayindex][2];
2471         slope[3] = span->data[1][arrayindex][3];
2472         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2473         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2474         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2475         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2476         Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2477         Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2478         Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2479         Color_Diffuse[3] = 0.0f;
2480         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2481         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2482         for (x = startx;x < endx;x++)
2483         {
2484                 z = buffer_z[x];
2485                 buffer_FragColorbgra8[x*4+0] = (int)(buffer_texture_colorbgra8[x*4+0] * (Color_Ambient[0] + ((data[0] + slope[0]*x) * z) * Color_Diffuse[0]));
2486                 buffer_FragColorbgra8[x*4+1] = (int)(buffer_texture_colorbgra8[x*4+1] * (Color_Ambient[1] + ((data[1] + slope[1]*x) * z) * Color_Diffuse[1]));
2487                 buffer_FragColorbgra8[x*4+2] = (int)(buffer_texture_colorbgra8[x*4+2] * (Color_Ambient[2] + ((data[2] + slope[2]*x) * z) * Color_Diffuse[2]));
2488                 buffer_FragColorbgra8[x*4+3] = (int)(buffer_texture_colorbgra8[x*4+3] * (Color_Ambient[3] + ((data[3] + slope[3]*x) * z) * Color_Diffuse[3]));
2489         }
2490         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2491 }
2492
2493
2494
2495 void DPSOFTRAST_VertexShader_Lightmap(void)
2496 {
2497         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);
2498         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);
2499         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
2500 }
2501
2502 void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2503 {
2504 #ifdef SSE2_PRESENT
2505         unsigned char * RESTRICT pixelmask = span->pixelmask;
2506         unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + span->start * 4;
2507         int x, startx = span->startx, endx = span->endx;
2508         __m128i Color_Ambientm, Color_Diffusem, Color_Glowm, Color_AmbientGlowm;
2509         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2510         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2511         unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2512         unsigned char buffer_texture_glowbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2513         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2514         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2515         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2516         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_LIGHTMAP, DPSOFTRAST_ARRAY_TEXCOORD4, buffer_z);
2517         if (dpsoftrast.user.alphatest || dpsoftrast.fb_blendmode != DPSOFTRAST_BLENDMODE_OPAQUE)
2518                 pixel = buffer_FragColorbgra8;
2519         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));
2520         Color_Ambientm = _mm_and_si128(Color_Ambientm, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0));
2521         Color_Ambientm = _mm_or_si128(Color_Ambientm, _mm_setr_epi32(0, 0, 0, (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]*255.0f)));
2522         Color_Ambientm = _mm_packs_epi32(Color_Ambientm, Color_Ambientm);
2523         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));
2524         Color_Diffusem = _mm_and_si128(Color_Diffusem, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0));
2525         Color_Diffusem = _mm_packs_epi32(Color_Diffusem, Color_Diffusem);
2526         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2527         {
2528                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glowbgra8, GL20TU_GLOW, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2529                 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));
2530                 Color_Glowm = _mm_and_si128(Color_Glowm, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0));
2531                 Color_Glowm = _mm_packs_epi32(Color_Glowm, Color_Glowm);
2532                 Color_AmbientGlowm = _mm_unpacklo_epi64(Color_Ambientm, Color_Glowm);
2533                 for (x = startx;x < endx;x++)
2534                 {
2535                         __m128i color, lightmap, glow, pix;
2536                         if (x + 4 <= endx && *(const unsigned int *)&pixelmask[x] == 0x01010101)
2537                         {
2538                                 __m128i pix2;
2539                                 color = _mm_loadu_si128((const __m128i *)&buffer_texture_colorbgra8[x*4]);
2540                                 lightmap = _mm_loadu_si128((const __m128i *)&buffer_texture_lightmapbgra8[x*4]);
2541                                 glow = _mm_loadu_si128((const __m128i *)&buffer_texture_glowbgra8[x*4]);
2542                                 pix = _mm_add_epi16(_mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, _mm_unpacklo_epi8(_mm_setzero_si128(), lightmap)), Color_Ambientm), 
2543                                                                                                         _mm_unpacklo_epi8(_mm_setzero_si128(), color)),
2544                                                                         _mm_mulhi_epu16(Color_Glowm, _mm_unpacklo_epi8(_mm_setzero_si128(), glow)));
2545                                 pix2 = _mm_add_epi16(_mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, _mm_unpackhi_epi8(_mm_setzero_si128(), lightmap)), Color_Ambientm), 
2546                                                                                                         _mm_unpackhi_epi8(_mm_setzero_si128(), color)),
2547                                                                         _mm_mulhi_epu16(Color_Glowm, _mm_unpackhi_epi8(_mm_setzero_si128(), glow)));
2548                                 _mm_storeu_si128((__m128i *)&pixel[x*4], _mm_packus_epi16(pix, pix2));
2549                                 x += 3;
2550                                 continue;
2551                         }
2552                         if(!pixelmask[x])
2553                                 continue;
2554                         color = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_colorbgra8[x*4]));
2555                         lightmap = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_lightmapbgra8[x*4]));
2556                         glow = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_glowbgra8[x*4]));
2557                         pix = _mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, lightmap), Color_AmbientGlowm), _mm_unpacklo_epi64(color, glow));
2558                         pix = _mm_add_epi16(pix, _mm_shuffle_epi32(pix, _MM_SHUFFLE(3, 2, 3, 2)));
2559                         *(int *)&pixel[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2560                 }
2561         }
2562         else
2563         {
2564                 for (x = startx;x < endx;x++)
2565                 {
2566                         __m128i color, lightmap, pix;
2567                         if (x + 4 <= endx && *(const unsigned int *)&pixelmask[x] == 0x01010101)
2568                         {
2569                                 __m128i pix2;
2570                                 color = _mm_loadu_si128((const __m128i *)&buffer_texture_colorbgra8[x*4]);
2571                                 lightmap = _mm_loadu_si128((const __m128i *)&buffer_texture_lightmapbgra8[x*4]);
2572                                 pix = _mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, _mm_unpacklo_epi8(_mm_setzero_si128(), lightmap)), Color_Ambientm), 
2573                                                                           _mm_unpacklo_epi8(_mm_setzero_si128(), color));
2574                                 pix2 = _mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, _mm_unpackhi_epi8(_mm_setzero_si128(), lightmap)), Color_Ambientm),
2575                                                                            _mm_unpackhi_epi8(_mm_setzero_si128(), color));
2576                                 _mm_storeu_si128((__m128i *)&pixel[x*4], _mm_packus_epi16(pix, pix2));
2577                                 x += 3;
2578                                 continue;
2579                         }
2580                         if(!pixelmask[x]) 
2581                                 continue;
2582                         color = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_colorbgra8[x*4]));
2583                         lightmap = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_lightmapbgra8[x*4]));
2584                         pix = _mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(lightmap, Color_Diffusem), Color_Ambientm), color);
2585                         *(int *)&pixel[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2586                 }
2587         }
2588         if(pixel == buffer_FragColorbgra8)
2589                 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2590 #endif
2591 }
2592
2593
2594
2595 void DPSOFTRAST_VertexShader_FakeLight(void)
2596 {
2597         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);
2598 }
2599
2600 void DPSOFTRAST_PixelShader_FakeLight(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2601 {
2602         // TODO: IMPLEMENT
2603         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2604         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2605         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2606         memset(buffer_FragColorbgra8, 0, span->length*4);
2607         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2608 }
2609
2610
2611
2612 void DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace(void)
2613 {
2614         DPSOFTRAST_VertexShader_Lightmap();
2615 }
2616
2617 void DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2618 {
2619         DPSOFTRAST_PixelShader_Lightmap(span);
2620         // TODO: IMPLEMENT
2621 }
2622
2623
2624
2625 void DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace(void)
2626 {
2627         DPSOFTRAST_VertexShader_Lightmap();
2628 }
2629
2630 void DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2631 {
2632         DPSOFTRAST_PixelShader_Lightmap(span);
2633         // TODO: IMPLEMENT
2634 }
2635
2636
2637
2638 void DPSOFTRAST_VertexShader_LightDirection(void)
2639 {
2640         int i;
2641         int numvertices = dpsoftrast.draw.numvertices;
2642         float LightDir[4];
2643         float LightVector[4];
2644         float EyePosition[4];
2645         float EyeVectorModelSpace[4];
2646         float EyeVector[4];
2647         float position[4];
2648         float svector[4];
2649         float tvector[4];
2650         float normal[4];
2651         LightDir[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightDir*4+0];
2652         LightDir[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightDir*4+1];
2653         LightDir[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightDir*4+2];
2654         LightDir[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightDir*4+3];
2655         EyePosition[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+0];
2656         EyePosition[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+1];
2657         EyePosition[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+2];
2658         EyePosition[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+3];
2659         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);
2660         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);
2661         for (i = 0;i < numvertices;i++)
2662         {
2663                 position[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0];
2664                 position[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1];
2665                 position[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2];
2666                 svector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0];
2667                 svector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1];
2668                 svector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2];
2669                 tvector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0];
2670                 tvector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1];
2671                 tvector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2];
2672                 normal[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0];
2673                 normal[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1];
2674                 normal[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2];
2675                 LightVector[0] = svector[0] * LightDir[0] + svector[1] * LightDir[1] + svector[2] * LightDir[2];
2676                 LightVector[1] = tvector[0] * LightDir[0] + tvector[1] * LightDir[1] + tvector[2] * LightDir[2];
2677                 LightVector[2] = normal[0] * LightDir[0] + normal[1] * LightDir[1] + normal[2] * LightDir[2];
2678                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0] = LightVector[0];
2679                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1] = LightVector[1];
2680                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2] = LightVector[2];
2681                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+3] = 0.0f;
2682                 EyeVectorModelSpace[0] = EyePosition[0] - position[0];
2683                 EyeVectorModelSpace[1] = EyePosition[1] - position[1];
2684                 EyeVectorModelSpace[2] = EyePosition[2] - position[2];
2685                 EyeVector[0] = svector[0] * EyeVectorModelSpace[0] + svector[1] * EyeVectorModelSpace[1] + svector[2] * EyeVectorModelSpace[2];
2686                 EyeVector[1] = tvector[0] * EyeVectorModelSpace[0] + tvector[1] * EyeVectorModelSpace[1] + tvector[2] * EyeVectorModelSpace[2];
2687                 EyeVector[2] = normal[0]  * EyeVectorModelSpace[0] + normal[1]  * EyeVectorModelSpace[1] + normal[2]  * EyeVectorModelSpace[2];
2688                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0] = EyeVector[0];
2689                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1] = EyeVector[1];
2690                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2] = EyeVector[2];
2691                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+3] = 0.0f;
2692         }
2693 }
2694
2695 #define DPSOFTRAST_Min(a,b) ((a) < (b) ? (a) : (b))
2696 #define DPSOFTRAST_Max(a,b) ((a) > (b) ? (a) : (b))
2697 #define DPSOFTRAST_Vector3Dot(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
2698 #define DPSOFTRAST_Vector3LengthSquared(v) (DPSOFTRAST_Vector3Dot((v),(v)))
2699 #define DPSOFTRAST_Vector3Length(v) (sqrt(DPSOFTRAST_Vector3LengthSquared(v)))
2700 #define DPSOFTRAST_Vector3Normalize(v)\
2701 do\
2702 {\
2703         float len = sqrt(DPSOFTRAST_Vector3Dot(v,v));\
2704         if (len)\
2705         {\
2706                 len = 1.0f / len;\
2707                 v[0] *= len;\
2708                 v[1] *= len;\
2709                 v[2] *= len;\
2710         }\
2711 }\
2712 while(0)
2713
2714 void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2715 {
2716         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2717         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2718         unsigned char buffer_texture_normalbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2719         unsigned char buffer_texture_glossbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2720         unsigned char buffer_texture_glowbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2721         unsigned char buffer_texture_pantsbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2722         unsigned char buffer_texture_shirtbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2723         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2724         int x, startx = span->startx, endx = span->endx;
2725         float Color_Ambient[4], Color_Diffuse[4], Color_Specular[4], Color_Glow[4], Color_Pants[4], Color_Shirt[4], LightColor[4];
2726         float LightVectordata[4];
2727         float LightVectorslope[4];
2728         float EyeVectordata[4];
2729         float EyeVectorslope[4];
2730         float z;
2731         float diffusetex[4];
2732         float glosstex[4];
2733         float surfacenormal[4];
2734         float lightnormal[4];
2735         float eyenormal[4];
2736         float specularnormal[4];
2737         float diffuse;
2738         float specular;
2739         float SpecularPower;
2740         int d[4];
2741         Color_Glow[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0];
2742         Color_Glow[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1];
2743         Color_Glow[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2];
2744         Color_Glow[3] = 0.0f;
2745         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2746         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2747         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2748         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2749         Color_Pants[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0];
2750         Color_Pants[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1];
2751         Color_Pants[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2];
2752         Color_Pants[3] = 0.0f;
2753         Color_Shirt[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0];
2754         Color_Shirt[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1];
2755         Color_Shirt[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2];
2756         Color_Shirt[3] = 0.0f;
2757         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2758         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2759         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2760         {
2761                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2762                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2763         }
2764         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2765         {
2766                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glowbgra8, GL20TU_GLOW, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2767         }
2768         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2769         {
2770                 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2771                 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2772                 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2773                 Color_Diffuse[3] = 0.0f;
2774                 LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0];
2775                 LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1];
2776                 LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2];
2777                 LightColor[3] = 0.0f;
2778                 LightVectordata[0]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0];
2779                 LightVectordata[1]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1];
2780                 LightVectordata[2]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2];
2781                 LightVectordata[3]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3];
2782                 LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0];
2783                 LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1];
2784                 LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2];
2785                 LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3];
2786                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2787                 Color_Specular[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0];
2788                 Color_Specular[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1];
2789                 Color_Specular[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2];
2790                 Color_Specular[3] = 0.0f;
2791                 SpecularPower = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f);
2792                 EyeVectordata[0]    = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][0];
2793                 EyeVectordata[1]    = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][1];
2794                 EyeVectordata[2]    = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][2];
2795                 EyeVectordata[3]    = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][3];
2796                 EyeVectorslope[0]   = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][0];
2797                 EyeVectorslope[1]   = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][1];
2798                 EyeVectorslope[2]   = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][2];
2799                 EyeVectorslope[3]   = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][3];
2800                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2801                 for (x = startx;x < endx;x++)
2802                 {
2803                         z = buffer_z[x];
2804                         diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
2805                         diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
2806                         diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
2807                         diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
2808                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2809                         {
2810                                 diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
2811                                 diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
2812                                 diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
2813                                 diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
2814                         }
2815                         glosstex[0] = buffer_texture_glossbgra8[x*4+0];
2816                         glosstex[1] = buffer_texture_glossbgra8[x*4+1];
2817                         glosstex[2] = buffer_texture_glossbgra8[x*4+2];
2818                         glosstex[3] = buffer_texture_glossbgra8[x*4+3];
2819                         surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
2820                         surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
2821                         surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
2822                         DPSOFTRAST_Vector3Normalize(surfacenormal);
2823
2824                         lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
2825                         lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
2826                         lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
2827                         DPSOFTRAST_Vector3Normalize(lightnormal);
2828
2829                         eyenormal[0] = (EyeVectordata[0] + EyeVectorslope[0]*x) * z;
2830                         eyenormal[1] = (EyeVectordata[1] + EyeVectorslope[1]*x) * z;
2831                         eyenormal[2] = (EyeVectordata[2] + EyeVectorslope[2]*x) * z;
2832                         DPSOFTRAST_Vector3Normalize(eyenormal);
2833
2834                         specularnormal[0] = lightnormal[0] + eyenormal[0];
2835                         specularnormal[1] = lightnormal[1] + eyenormal[1];
2836                         specularnormal[2] = lightnormal[2] + eyenormal[2];
2837                         DPSOFTRAST_Vector3Normalize(specularnormal);
2838
2839                         diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
2840                         specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f;
2841                         specular = pow(specular, SpecularPower * glosstex[3]);
2842                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2843                         {
2844                                 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;
2845                                 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;
2846                                 d[2] = (int)(buffer_texture_glowbgra8[x*4+2] * Color_Glow[2] + diffusetex[2] * Color_Ambient[2] + (diffusetex[2] * Color_Diffuse[2] * diffuse + glosstex[2] * Color_Specular[2] * specular) * LightColor[2]);if (d[2] > 255) d[2] = 255;
2847                                 d[3] = (int)(                                                  diffusetex[3] * Color_Ambient[3]);if (d[3] > 255) d[3] = 255;
2848                         }
2849                         else
2850                         {
2851                                 d[0] = (int)(                                                  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;
2852                                 d[1] = (int)(                                                  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;
2853                                 d[2] = (int)(                                                  diffusetex[2] * Color_Ambient[2] + (diffusetex[2] * Color_Diffuse[2] * diffuse + glosstex[2] * Color_Specular[2] * specular) * LightColor[2]);if (d[2] > 255) d[2] = 255;
2854                                 d[3] = (int)(                                                  diffusetex[3] * Color_Ambient[3]);if (d[3] > 255) d[3] = 255;
2855                         }
2856                         buffer_FragColorbgra8[x*4+0] = d[0];
2857                         buffer_FragColorbgra8[x*4+1] = d[1];
2858                         buffer_FragColorbgra8[x*4+2] = d[2];
2859                         buffer_FragColorbgra8[x*4+3] = d[3];
2860                 }
2861         }
2862         else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
2863         {
2864                 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2865                 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2866                 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2867                 Color_Diffuse[3] = 0.0f;
2868                 LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0];
2869                 LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1];
2870                 LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2];
2871                 LightColor[3] = 0.0f;
2872                 LightVectordata[0]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0];
2873                 LightVectordata[1]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1];
2874                 LightVectordata[2]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2];
2875                 LightVectordata[3]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3];
2876                 LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0];
2877                 LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1];
2878                 LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2];
2879                 LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3];
2880                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2881                 for (x = startx;x < endx;x++)
2882                 {
2883                         z = buffer_z[x];
2884                         diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
2885                         diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
2886                         diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
2887                         diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
2888                         surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
2889                         surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
2890                         surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
2891                         DPSOFTRAST_Vector3Normalize(surfacenormal);
2892
2893                         lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
2894                         lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
2895                         lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
2896                         DPSOFTRAST_Vector3Normalize(lightnormal);
2897
2898                         diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
2899                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2900                         {
2901                                 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;
2902                                 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;
2903                                 d[2] = (int)(buffer_texture_glowbgra8[x*4+2] * Color_Glow[2] + diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse * LightColor[2]));if (d[2] > 255) d[2] = 255;
2904                                 d[3] = (int)(                                                  diffusetex[3] * (Color_Ambient[3]                                             ));if (d[3] > 255) d[3] = 255;
2905                         }
2906                         else
2907                         {
2908                                 d[0] = (int)(                                                + diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse * LightColor[0]));if (d[0] > 255) d[0] = 255;
2909                                 d[1] = (int)(                                                + diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse * LightColor[1]));if (d[1] > 255) d[1] = 255;
2910                                 d[2] = (int)(                                                + diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse * LightColor[2]));if (d[2] > 255) d[2] = 255;
2911                                 d[3] = (int)(                                                  diffusetex[3] * (Color_Ambient[3]                                             ));if (d[3] > 255) d[3] = 255;
2912                         }
2913                         buffer_FragColorbgra8[x*4+0] = d[0];
2914                         buffer_FragColorbgra8[x*4+1] = d[1];
2915                         buffer_FragColorbgra8[x*4+2] = d[2];
2916                         buffer_FragColorbgra8[x*4+3] = d[3];
2917                 }
2918         }
2919         else
2920         {
2921                 for (x = startx;x < endx;x++)
2922                 {
2923                         z = buffer_z[x];
2924                         diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
2925                         diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
2926                         diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
2927                         diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
2928
2929                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2930                         {
2931                                 d[0] = (int)(buffer_texture_glowbgra8[x*4+0] * Color_Glow[0] + diffusetex[0] * Color_Ambient[0]);if (d[0] > 255) d[0] = 255;
2932                                 d[1] = (int)(buffer_texture_glowbgra8[x*4+1] * Color_Glow[1] + diffusetex[1] * Color_Ambient[1]);if (d[1] > 255) d[1] = 255;
2933                                 d[2] = (int)(buffer_texture_glowbgra8[x*4+2] * Color_Glow[2] + diffusetex[2] * Color_Ambient[2]);if (d[2] > 255) d[2] = 255;
2934                                 d[3] = (int)(                                                  diffusetex[3] * Color_Ambient[3]);if (d[3] > 255) d[3] = 255;
2935                         }
2936                         else
2937                         {
2938                                 d[0] = (int)(                                                  diffusetex[0] * Color_Ambient[0]);if (d[0] > 255) d[0] = 255;
2939                                 d[1] = (int)(                                                  diffusetex[1] * Color_Ambient[1]);if (d[1] > 255) d[1] = 255;
2940                                 d[2] = (int)(                                                  diffusetex[2] * Color_Ambient[2]);if (d[2] > 255) d[2] = 255;
2941                                 d[3] = (int)(                                                  diffusetex[3] * Color_Ambient[3]);if (d[3] > 255) d[3] = 255;
2942                         }
2943                         buffer_FragColorbgra8[x*4+0] = d[0];
2944                         buffer_FragColorbgra8[x*4+1] = d[1];
2945                         buffer_FragColorbgra8[x*4+2] = d[2];
2946                         buffer_FragColorbgra8[x*4+3] = d[3];
2947                 }
2948         }
2949         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2950 }
2951
2952
2953
2954 void DPSOFTRAST_VertexShader_LightSource(void)
2955 {
2956         int i;
2957         int numvertices = dpsoftrast.draw.numvertices;
2958         float LightPosition[4];
2959         float LightVector[4];
2960         float LightVectorModelSpace[4];
2961         float EyePosition[4];
2962         float EyeVectorModelSpace[4];
2963         float EyeVector[4];
2964         float position[4];
2965         float svector[4];
2966         float tvector[4];
2967         float normal[4];
2968         LightPosition[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightPosition*4+0];
2969         LightPosition[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightPosition*4+1];
2970         LightPosition[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightPosition*4+2];
2971         LightPosition[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightPosition*4+3];
2972         EyePosition[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+0];
2973         EyePosition[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+1];
2974         EyePosition[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+2];
2975         EyePosition[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+3];
2976         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);
2977         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);
2978         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);
2979         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
2980         for (i = 0;i < numvertices;i++)
2981         {
2982                 position[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0];
2983                 position[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1];
2984                 position[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2];
2985                 svector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0];
2986                 svector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1];
2987                 svector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2];
2988                 tvector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0];
2989                 tvector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1];
2990                 tvector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2];
2991                 normal[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0];
2992                 normal[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1];
2993                 normal[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2];
2994                 LightVectorModelSpace[0] = LightPosition[0] - position[0];
2995                 LightVectorModelSpace[1] = LightPosition[1] - position[1];
2996                 LightVectorModelSpace[2] = LightPosition[2] - position[2];
2997                 LightVector[0] = svector[0] * LightVectorModelSpace[0] + svector[1] * LightVectorModelSpace[1] + svector[2] * LightVectorModelSpace[2];
2998                 LightVector[1] = tvector[0] * LightVectorModelSpace[0] + tvector[1] * LightVectorModelSpace[1] + tvector[2] * LightVectorModelSpace[2];
2999                 LightVector[2] = normal[0]  * LightVectorModelSpace[0] + normal[1]  * LightVectorModelSpace[1] + normal[2]  * LightVectorModelSpace[2];
3000                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0] = LightVector[0];
3001                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1] = LightVector[1];
3002                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2] = LightVector[2];
3003                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+3] = 0.0f;
3004                 EyeVectorModelSpace[0] = EyePosition[0] - position[0];
3005                 EyeVectorModelSpace[1] = EyePosition[1] - position[1];
3006                 EyeVectorModelSpace[2] = EyePosition[2] - position[2];
3007                 EyeVector[0] = svector[0] * EyeVectorModelSpace[0] + svector[1] * EyeVectorModelSpace[1] + svector[2] * EyeVectorModelSpace[2];
3008                 EyeVector[1] = tvector[0] * EyeVectorModelSpace[0] + tvector[1] * EyeVectorModelSpace[1] + tvector[2] * EyeVectorModelSpace[2];
3009                 EyeVector[2] = normal[0]  * EyeVectorModelSpace[0] + normal[1]  * EyeVectorModelSpace[1] + normal[2]  * EyeVectorModelSpace[2];
3010                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0] = EyeVector[0];
3011                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1] = EyeVector[1];
3012                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2] = EyeVector[2];
3013                 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+3] = 0.0f;
3014         }
3015 }
3016
3017 void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3018 {
3019         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3020         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3021         unsigned char buffer_texture_normalbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3022         unsigned char buffer_texture_glossbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3023         unsigned char buffer_texture_cubebgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3024         unsigned char buffer_texture_pantsbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3025         unsigned char buffer_texture_shirtbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3026         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3027         int x, startx = span->startx, endx = span->endx;
3028         float Color_Ambient[4], Color_Diffuse[4], Color_Specular[4], Color_Glow[4], Color_Pants[4], Color_Shirt[4], LightColor[4];
3029         float CubeVectordata[4];
3030         float CubeVectorslope[4];
3031         float LightVectordata[4];
3032         float LightVectorslope[4];
3033         float EyeVectordata[4];
3034         float EyeVectorslope[4];
3035         float z;
3036         float diffusetex[4];
3037         float glosstex[4];
3038         float surfacenormal[4];
3039         float lightnormal[4];
3040         float eyenormal[4];
3041         float specularnormal[4];
3042         float diffuse;
3043         float specular;
3044         float SpecularPower;
3045         float CubeVector[4];
3046         float attenuation;
3047         int d[4];
3048         Color_Glow[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0];
3049         Color_Glow[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1];
3050         Color_Glow[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2];
3051         Color_Glow[3] = 0.0f;
3052         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
3053         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
3054         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
3055         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
3056         Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
3057         Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
3058         Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
3059         Color_Diffuse[3] = 0.0f;
3060         Color_Specular[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0];
3061         Color_Specular[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1];
3062         Color_Specular[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2];
3063         Color_Specular[3] = 0.0f;
3064         Color_Pants[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0];
3065         Color_Pants[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1];
3066         Color_Pants[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2];
3067         Color_Pants[3] = 0.0f;
3068         Color_Shirt[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0];
3069         Color_Shirt[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1];
3070         Color_Shirt[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2];
3071         Color_Shirt[3] = 0.0f;
3072         LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0];
3073         LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1];
3074         LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2];
3075         LightColor[3] = 0.0f;
3076         SpecularPower = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f);
3077         EyeVectordata[0]    = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][0];
3078         EyeVectordata[1]    = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][1];
3079         EyeVectordata[2]    = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][2];
3080         EyeVectordata[3]    = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][3];
3081         EyeVectorslope[0]   = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][0];
3082         EyeVectorslope[1]   = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][1];
3083         EyeVectorslope[2]   = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][2];
3084         EyeVectorslope[3]   = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][3];
3085         LightVectordata[0]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0];
3086         LightVectordata[1]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1];
3087         LightVectordata[2]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2];
3088         LightVectordata[3]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3];
3089         LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0];
3090         LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1];
3091         LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2];
3092         LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3];
3093         CubeVectordata[0]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][0];
3094         CubeVectordata[1]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][1];
3095         CubeVectordata[2]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][2];
3096         CubeVectordata[3]  = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][3];
3097         CubeVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][0];
3098         CubeVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][1];
3099         CubeVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][2];
3100         CubeVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][3];
3101         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3102         memset(buffer_FragColorbgra8 + startx*4, 0, (endx-startx)*4); // clear first, because we skip writing black pixels, and there are a LOT of them...
3103         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3104         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
3105         {
3106                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3107                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3108         }
3109         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER)
3110                 DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(span, buffer_texture_cubebgra8, GL20TU_CUBE, DPSOFTRAST_ARRAY_TEXCOORD3, buffer_z);
3111         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
3112         {
3113                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3114                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3115                 for (x = startx;x < endx;x++)
3116                 {
3117                         z = buffer_z[x];
3118                         CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
3119                         CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
3120                         CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
3121                         attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
3122                         if (attenuation < 0.01f)
3123                                 continue;
3124                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
3125                         {
3126                                 attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
3127                                 if (attenuation < 0.01f)
3128                                         continue;
3129                         }
3130
3131                         diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
3132                         diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
3133                         diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
3134                         diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
3135                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
3136                         {
3137                                 diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
3138                                 diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
3139                                 diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
3140                                 diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
3141                         }
3142                         glosstex[0] = buffer_texture_glossbgra8[x*4+0];
3143                         glosstex[1] = buffer_texture_glossbgra8[x*4+1];
3144                         glosstex[2] = buffer_texture_glossbgra8[x*4+2];
3145                         glosstex[3] = buffer_texture_glossbgra8[x*4+3];
3146                         surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
3147                         surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
3148                         surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
3149                         DPSOFTRAST_Vector3Normalize(surfacenormal);
3150
3151                         lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
3152                         lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
3153                         lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
3154                         DPSOFTRAST_Vector3Normalize(lightnormal);
3155
3156                         eyenormal[0] = (EyeVectordata[0] + EyeVectorslope[0]*x) * z;
3157                         eyenormal[1] = (EyeVectordata[1] + EyeVectorslope[1]*x) * z;
3158                         eyenormal[2] = (EyeVectordata[2] + EyeVectorslope[2]*x) * z;
3159                         DPSOFTRAST_Vector3Normalize(eyenormal);
3160
3161                         specularnormal[0] = lightnormal[0] + eyenormal[0];
3162                         specularnormal[1] = lightnormal[1] + eyenormal[1];
3163                         specularnormal[2] = lightnormal[2] + eyenormal[2];
3164                         DPSOFTRAST_Vector3Normalize(specularnormal);
3165
3166                         diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
3167                         specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f;
3168                         specular = pow(specular, SpecularPower * glosstex[3]);
3169                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER)
3170                         {
3171                                 // scale down the attenuation to account for the cubefilter multiplying everything by 255
3172                                 attenuation *= (1.0f / 255.0f);
3173                                 d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse) + glosstex[0] * Color_Specular[0] * specular) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
3174                                 d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse) + glosstex[1] * Color_Specular[1] * specular) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
3175                                 d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse) + glosstex[2] * Color_Specular[2] * specular) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
3176                                 d[3] = (int)( diffusetex[3]                                                                                                                                                                );if (d[3] > 255) d[3] = 255;
3177                         }
3178                         else
3179                         {
3180                                 d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse) + glosstex[0] * Color_Specular[0] * specular) * LightColor[0]                                   * attenuation);if (d[0] > 255) d[0] = 255;
3181                                 d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse) + glosstex[1] * Color_Specular[1] * specular) * LightColor[1]                                   * attenuation);if (d[1] > 255) d[1] = 255;
3182                                 d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse) + glosstex[2] * Color_Specular[2] * specular) * LightColor[2]                                   * attenuation);if (d[2] > 255) d[2] = 255;
3183                                 d[3] = (int)( diffusetex[3]                                                                                                                                                                );if (d[3] > 255) d[3] = 255;
3184                         }
3185                         buffer_FragColorbgra8[x*4+0] = d[0];
3186                         buffer_FragColorbgra8[x*4+1] = d[1];
3187                         buffer_FragColorbgra8[x*4+2] = d[2];
3188                         buffer_FragColorbgra8[x*4+3] = d[3];
3189                 }
3190         }
3191         else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
3192         {
3193                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3194                 for (x = startx;x < endx;x++)
3195                 {
3196                         z = buffer_z[x];
3197                         CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
3198                         CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
3199                         CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
3200                         attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
3201                         if (attenuation < 0.01f)
3202                                 continue;
3203                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
3204                         {
3205                                 attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
3206                                 if (attenuation < 0.01f)
3207                                         continue;
3208                         }
3209
3210                         diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
3211                         diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
3212                         diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
3213                         diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
3214                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
3215                         {
3216                                 diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
3217                                 diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
3218                                 diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
3219                                 diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
3220                         }
3221                         surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
3222                         surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
3223                         surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
3224                         DPSOFTRAST_Vector3Normalize(surfacenormal);
3225
3226                         lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
3227                         lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
3228                         lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
3229                         DPSOFTRAST_Vector3Normalize(lightnormal);
3230
3231                         diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
3232                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER)
3233                         {
3234                                 // scale down the attenuation to account for the cubefilter multiplying everything by 255
3235                                 attenuation *= (1.0f / 255.0f);
3236                                 d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse)) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
3237                                 d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse)) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
3238                                 d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse)) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
3239                                 d[3] = (int)( diffusetex[3]                                                                                                                   );if (d[3] > 255) d[3] = 255;
3240                         }
3241                         else
3242                         {
3243                                 d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse)) * LightColor[0]                                   * attenuation);if (d[0] > 255) d[0] = 255;
3244                                 d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse)) * LightColor[1]                                   * attenuation);if (d[1] > 255) d[1] = 255;
3245                                 d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse)) * LightColor[2]                                   * attenuation);if (d[2] > 255) d[2] = 255;
3246                                 d[3] = (int)( diffusetex[3]                                                                                                                                                                );if (d[3] > 255) d[3] = 255;
3247                         }
3248                         buffer_FragColorbgra8[x*4+0] = d[0];
3249                         buffer_FragColorbgra8[x*4+1] = d[1];
3250                         buffer_FragColorbgra8[x*4+2] = d[2];
3251                         buffer_FragColorbgra8[x*4+3] = d[3];
3252                 }
3253         }
3254         else
3255         {
3256                 for (x = startx;x < endx;x++)
3257                 {
3258                         z = buffer_z[x];
3259                         CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
3260                         CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
3261                         CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
3262                         attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
3263                         if (attenuation < 0.01f)
3264                                 continue;
3265                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
3266                         {
3267                                 attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
3268                                 if (attenuation < 0.01f)
3269                                         continue;
3270                         }
3271
3272                         diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
3273                         diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
3274                         diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
3275                         diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
3276                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
3277                         {
3278                                 diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
3279                                 diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
3280                                 diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
3281                                 diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
3282                         }
3283                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER)
3284                         {
3285                                 // scale down the attenuation to account for the cubefilter multiplying everything by 255
3286                                 attenuation *= (1.0f / 255.0f);
3287                                 d[0] = (int)((diffusetex[0] * (Color_Ambient[0])) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
3288                                 d[1] = (int)((diffusetex[1] * (Color_Ambient[1])) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
3289                                 d[2] = (int)((diffusetex[2] * (Color_Ambient[2])) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
3290                                 d[3] = (int)( diffusetex[3]                                                                                      );if (d[3] > 255) d[3] = 255;
3291                         }
3292                         else
3293                         {
3294                                 d[0] = (int)((diffusetex[0] * (Color_Ambient[0])) * LightColor[0]                                   * attenuation);if (d[0] > 255) d[0] = 255;
3295                                 d[1] = (int)((diffusetex[1] * (Color_Ambient[1])) * LightColor[1]                                   * attenuation);if (d[1] > 255) d[1] = 255;
3296                                 d[2] = (int)((diffusetex[2] * (Color_Ambient[2])) * LightColor[2]                                   * attenuation);if (d[2] > 255) d[2] = 255;
3297                                 d[3] = (int)( diffusetex[3]                                                                                                                                                                );if (d[3] > 255) d[3] = 255;
3298                         }
3299                         buffer_FragColorbgra8[x*4+0] = d[0];
3300                         buffer_FragColorbgra8[x*4+1] = d[1];
3301                         buffer_FragColorbgra8[x*4+2] = d[2];
3302                         buffer_FragColorbgra8[x*4+3] = d[3];
3303                 }
3304         }
3305         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3306 }
3307
3308
3309
3310 void DPSOFTRAST_VertexShader_Refraction(void)
3311 {
3312         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);
3313 }
3314
3315 void DPSOFTRAST_PixelShader_Refraction(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3316 {
3317         // TODO: IMPLEMENT
3318         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3319         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3320         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3321         memset(buffer_FragColorbgra8, 0, span->length*4);
3322         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3323 }
3324
3325
3326
3327 void DPSOFTRAST_VertexShader_Water(void)
3328 {
3329         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);
3330 }
3331
3332
3333 void DPSOFTRAST_PixelShader_Water(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3334 {
3335         // TODO: IMPLEMENT
3336         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3337         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3338         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3339         memset(buffer_FragColorbgra8, 0, span->length*4);
3340         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3341 }
3342
3343
3344
3345 void DPSOFTRAST_VertexShader_ShowDepth(void)
3346 {
3347         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);
3348 }
3349
3350 void DPSOFTRAST_PixelShader_ShowDepth(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3351 {
3352         // TODO: IMPLEMENT
3353         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3354         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3355         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3356         memset(buffer_FragColorbgra8, 0, span->length*4);
3357         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3358 }
3359
3360
3361
3362 void DPSOFTRAST_VertexShader_DeferredGeometry(void)
3363 {
3364         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);
3365 }
3366
3367 void DPSOFTRAST_PixelShader_DeferredGeometry(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3368 {
3369         // TODO: IMPLEMENT
3370         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3371         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3372         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3373         memset(buffer_FragColorbgra8, 0, span->length*4);
3374         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3375 }
3376
3377
3378
3379 void DPSOFTRAST_VertexShader_DeferredLightSource(void)
3380 {
3381         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);
3382 }
3383
3384 void DPSOFTRAST_PixelShader_DeferredLightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3385 {
3386         // TODO: IMPLEMENT
3387         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3388         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3389         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3390         memset(buffer_FragColorbgra8, 0, span->length*4);
3391         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3392 }
3393
3394
3395
3396 typedef struct DPSOFTRAST_ShaderModeInfo_s
3397 {
3398         int lodarrayindex;
3399         void (*Vertex)(void);
3400         void (*Span)(const DPSOFTRAST_State_Draw_Span * RESTRICT span);
3401 }
3402 DPSOFTRAST_ShaderModeInfo;
3403
3404 DPSOFTRAST_ShaderModeInfo DPSOFTRAST_ShaderModeTable[SHADERMODE_COUNT] =
3405 {
3406         {2, DPSOFTRAST_VertexShader_Generic,                        DPSOFTRAST_PixelShader_Generic,                      },
3407         {2, DPSOFTRAST_VertexShader_PostProcess,                    DPSOFTRAST_PixelShader_PostProcess,                  },
3408         {2, DPSOFTRAST_VertexShader_Depth_Or_Shadow,                DPSOFTRAST_PixelShader_Depth_Or_Shadow,              },
3409         {2, DPSOFTRAST_VertexShader_FlatColor,                      DPSOFTRAST_PixelShader_FlatColor,                    },
3410         {2, DPSOFTRAST_VertexShader_VertexColor,                    DPSOFTRAST_PixelShader_VertexColor,                  },
3411         {2, DPSOFTRAST_VertexShader_Lightmap,                       DPSOFTRAST_PixelShader_Lightmap,                     },
3412         {2, DPSOFTRAST_VertexShader_FakeLight,                      DPSOFTRAST_PixelShader_FakeLight,                    },
3413         {2, DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace,   DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace, },
3414         {2, DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace, DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace},
3415         {2, DPSOFTRAST_VertexShader_LightDirection,                 DPSOFTRAST_PixelShader_LightDirection,               },
3416         {2, DPSOFTRAST_VertexShader_LightSource,                    DPSOFTRAST_PixelShader_LightSource,                  },
3417         {2, DPSOFTRAST_VertexShader_Refraction,                     DPSOFTRAST_PixelShader_Refraction,                   },
3418         {2, DPSOFTRAST_VertexShader_Water,                          DPSOFTRAST_PixelShader_Water,                        },
3419         {2, DPSOFTRAST_VertexShader_ShowDepth,                      DPSOFTRAST_PixelShader_ShowDepth,                    },
3420         {2, DPSOFTRAST_VertexShader_DeferredGeometry,               DPSOFTRAST_PixelShader_DeferredGeometry,             },
3421         {2, DPSOFTRAST_VertexShader_DeferredLightSource,            DPSOFTRAST_PixelShader_DeferredLightSource,          }
3422 };
3423
3424
3425
3426 void DPSOFTRAST_Draw_ProcessSpans(void)
3427 {
3428         int i;
3429         int x;
3430         int startx;
3431         int endx;
3432         int numspans = dpsoftrast.draw.numspans;
3433 //      unsigned int c;
3434 //      unsigned int *colorpixel;
3435         unsigned int *depthpixel;
3436         float w;
3437         float wslope;
3438         int depth;
3439         int depthslope;
3440         unsigned int d;
3441         DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
3442         unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3443         for (i = 0;i < numspans;i++, span++)
3444         {
3445                 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
3446                 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
3447                 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
3448                 {
3449                         depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
3450                         depth = (int)(w*DPSOFTRAST_DEPTHSCALE - DPSOFTRAST_DEPTHOFFSET*(dpsoftrast.user.polygonoffset[1] + fabs(wslope)*dpsoftrast.user.polygonoffset[0]));
3451                         depthpixel = dpsoftrast.fb_depthpixels + span->start;
3452                         switch(dpsoftrast.fb_depthfunc)
3453                         {
3454                         default:
3455                         case GL_ALWAYS:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
3456                         case GL_LESS:    for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
3457                         case GL_LEQUAL:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
3458                         case GL_EQUAL:   for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
3459                         case GL_GEQUAL:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
3460                         case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
3461                         case GL_NEVER:   for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
3462                         }
3463                         //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
3464                         //for (x = 0;x < span->length;x++)
3465                         //      colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
3466                         // if there is no color buffer, skip pixel shader
3467                         startx = 0;
3468                         endx = span->length;
3469                         while (startx < endx && !pixelmask[startx])
3470                                 startx++;
3471                         while (endx > startx && !pixelmask[endx-1])
3472                                 endx--;
3473                         if (startx >= endx)
3474                                 continue; // no pixels to fill
3475                         span->pixelmask = pixelmask;
3476                         span->startx = startx;
3477                         span->endx = endx;
3478                         // run pixel shader if appropriate
3479                         // do this before running depthmask code, to allow the pixelshader
3480                         // to clear pixelmask values for alpha testing
3481                         if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
3482                                 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
3483                         if (dpsoftrast.user.depthmask)
3484                                 for (x = startx, d = depth + depthslope*startx;x < endx;x++, d += depthslope)
3485                                         if (pixelmask[x])
3486                                                 depthpixel[x] = d;
3487                 }
3488                 else
3489                 {
3490                         // no depth testing means we're just dealing with color...
3491                         // if there is no color buffer, skip pixel shader
3492                         if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
3493                         {
3494                                 memset(pixelmask, 1, span->length);
3495                                 span->pixelmask = pixelmask;
3496                                 span->startx = 0;
3497                                 span->endx = span->length;
3498                                 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
3499                         }
3500                 }
3501         }
3502 }
3503
3504 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask)
3505 {
3506 #ifdef SSE2_PRESENT
3507         int cullface = dpsoftrast.user.cullface;
3508         int width = dpsoftrast.fb_width;
3509         int height = dpsoftrast.fb_height;
3510         __m128i fbmax = _mm_sub_epi16(_mm_setr_epi16(width, height, width, height, width, height, width, height), _mm_set1_epi16(1));
3511         int i;
3512         int j;
3513         int k;
3514         int y;
3515         int e[3];
3516         __m128i screeny;
3517         int starty, endy;
3518         int numpoints;
3519         int edge0p;
3520         int edge0n;
3521         int edge1p;
3522         int edge1n;
3523         int startx;
3524         int endx;
3525         unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS];
3526         __m128 mipedgescale;
3527         float clipdist[4];
3528         __m128 clipfrac[4];
3529         __m128 clipped[DPSOFTRAST_ARRAY_TOTAL][4];
3530         __m128 screen[4];
3531         __m128 proj[DPSOFTRAST_ARRAY_TOTAL][4];
3532         DPSOFTRAST_Texture *texture;
3533         DPSOFTRAST_State_Draw_Span *span;
3534         DPSOFTRAST_State_Draw_Span *oldspan;
3535         for (i = 0;i < numtriangles;i++)
3536         {
3537                 // generate the 3 edges of this triangle
3538                 // generate spans for the triangle - switch based on left split or right split classification of triangle
3539                 if (element3i)
3540                 {
3541                         e[0] = element3i[i*3+0] - firstvertex;
3542                         e[1] = element3i[i*3+1] - firstvertex;
3543                         e[2] = element3i[i*3+2] - firstvertex;
3544                 }
3545                 else if (element3s)
3546                 {
3547                         e[0] = element3s[i*3+0] - firstvertex;
3548                         e[1] = element3s[i*3+1] - firstvertex;
3549                         e[2] = element3s[i*3+2] - firstvertex;
3550                 }
3551                 else
3552                 {
3553                         e[0] = i*3+0;
3554                         e[1] = i*3+1;
3555                         e[2] = i*3+2;
3556                 }
3557
3558 #define SKIPBACKFACE \
3559                 if(cullface != GL_NONE) \
3560                 { \
3561                         __m128 triangleedge[2] = { _mm_sub_ps(screen[0], screen[1]), _mm_sub_ps(screen[2], screen[1]) }; \
3562                         /* store normal in 2, 0, 1 order instead of 0, 1, 2 as it requires fewer shuffles and leaves z component accessible as scalar */ \
3563                         __m128 trianglenormal = _mm_sub_ss(_mm_mul_ss(triangleedge[0], _mm_shuffle_ps(triangleedge[1], triangleedge[1], _MM_SHUFFLE(3, 0, 2, 1))), \
3564                                                                                         _mm_mul_ss(_mm_shuffle_ps(triangleedge[0], triangleedge[0], _MM_SHUFFLE(3, 0, 2, 1)), triangleedge[1])); \
3565                         /* apply current cullface mode (this culls many triangles) */ \
3566                         switch(cullface) \
3567                         { \
3568                         case GL_BACK: \
3569                                 if (_mm_ucomilt_ss(trianglenormal, _mm_setzero_ps())) \
3570                                         continue; \
3571                                 break; \
3572                         case GL_FRONT: \
3573                                 if (_mm_ucomigt_ss(trianglenormal, _mm_setzero_ps())) \
3574                                         continue; \
3575                                 break; \
3576                         } \
3577                 }
3578                         //trianglenormal = _mm_sub_ps(_mm_mul_ps(triangleedge[0], _mm_shuffle_ps(triangleedge[1], triangleedge[1], _MM_SHUFFLE(3, 0, 2, 1))),
3579                         //                                                _mm_mul_ps(_mm_shuffle_ps(triangleedge[0], triangleedge[0], _MM_SHUFFLE(3, 0, 2, 1)), triangleedge[1]));
3580                         //trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
3581                         //trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
3582                         //trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
3583
3584                         // macros for clipping vertices
3585 #define CLIPPEDVERTEXLERP(k,p1, p2) \
3586                         clipfrac[k] = _mm_set1_ps(clipdist[p1] / (clipdist[p1] - clipdist[p2])); \
3587                         { \
3588                                 __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]); \
3589                                 clipped[DPSOFTRAST_ARRAY_POSITION][k] = _mm_add_ps(v1, _mm_mul_ps(_mm_sub_ps(v2, v1), clipfrac[k])); \
3590                         } \
3591                         screen[k] = DPSOFTRAST_Draw_ProjectVertex(clipped[DPSOFTRAST_ARRAY_POSITION][k]);
3592 #define CLIPPEDATTRIBSLERP(k,p1,p2) \
3593                         for (j = DPSOFTRAST_ARRAY_POSITION+1;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
3594                         {\
3595                                 /*if (arraymask[j])*/\
3596                                 {\
3597                                         __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]); \
3598                                         clipped[j][k] = _mm_add_ps(v1, _mm_mul_ps(_mm_sub_ps(v2, v1), clipfrac[k])); \
3599                                 }\
3600                         }
3601 #define CLIPPEDVERTEXCOPY(k,p1) \
3602                         screen[k] = _mm_load_ps(&dpsoftrast.draw.screencoord4f[e[p1]*4]);
3603 #define CLIPPEDATTRIBSCOPY(k,p1) \
3604                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
3605                         {\
3606                                 /*if (arraymask[j])*/\
3607                                 {\
3608                                         clipped[j][k] = _mm_load_ps(&dpsoftrast.draw.post_array4f[j][e[p1]*4]); \
3609                                 }\
3610                         }
3611
3612                 // calculate distance from nearplane
3613                 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+3];
3614                 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+3];
3615                 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+3];
3616                 if (clipdist[0] >= 0.0f)
3617                 {
3618                         if (clipdist[1] >= 0.0f)
3619                         {
3620                                 if (clipdist[2] >= 0.0f)
3621                                 {
3622                                         // triangle is entirely in front of nearplane
3623                                         CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXCOPY(2,2);
3624                                         numpoints = 3;
3625                                         SKIPBACKFACE;
3626                                         CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSCOPY(2,2);
3627                                 }
3628                                 else
3629                                 {
3630                                         CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXLERP(2,1,2); CLIPPEDVERTEXLERP(3,2,0);
3631                                         numpoints = 4;
3632                                         SKIPBACKFACE;
3633                                         CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSLERP(2,1,2); CLIPPEDATTRIBSLERP(3,2,0);
3634                                 }
3635                         }
3636                         else 
3637                         {
3638                                 if (clipdist[2] >= 0.0f)
3639                                 {
3640                                         CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXLERP(1,0,1); CLIPPEDVERTEXLERP(2,1,2);     CLIPPEDVERTEXCOPY(3,2);
3641                                         numpoints = 4;
3642                                         SKIPBACKFACE;
3643                                         CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSLERP(1,0,1); CLIPPEDATTRIBSLERP(2,1,2); CLIPPEDATTRIBSCOPY(3,2);
3644                                 }
3645                                 else
3646                                 {
3647                                         CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXLERP(1,0,1); CLIPPEDVERTEXLERP(2,2,0);
3648                                         numpoints = 3;
3649                                         SKIPBACKFACE;
3650                                         CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSLERP(1,0,1); CLIPPEDATTRIBSLERP(2,2,0);
3651                                 }
3652                         }
3653                 }                       
3654                 else if (clipdist[1] >= 0.0f)
3655                 {
3656                         if (clipdist[2] >= 0.0f)
3657                         {
3658                                 CLIPPEDVERTEXLERP(0,0,1); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXCOPY(2,2); CLIPPEDVERTEXLERP(3,2,0);
3659                                 numpoints = 4;
3660                                 SKIPBACKFACE;
3661                                 CLIPPEDATTRIBSLERP(0,0,1); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSCOPY(2,2); CLIPPEDATTRIBSLERP(3,2,0);
3662                         }
3663                         else
3664                         {
3665                                 CLIPPEDVERTEXLERP(0,0,1); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXLERP(2,1,2);
3666                                 numpoints = 3;
3667                                 SKIPBACKFACE;
3668                                 CLIPPEDATTRIBSLERP(0,0,1); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSLERP(2,1,2);
3669                         }
3670                 }
3671                 else if (clipdist[2] >= 0.0f)
3672                 {
3673                         CLIPPEDVERTEXLERP(0,1,2); CLIPPEDVERTEXCOPY(1,2); CLIPPEDVERTEXLERP(2,2,0);
3674                         numpoints = 3;
3675                         SKIPBACKFACE;
3676                         CLIPPEDATTRIBSLERP(0,1,2); CLIPPEDATTRIBSCOPY(1,2); CLIPPEDATTRIBSLERP(2,2,0);
3677                 }
3678                 else continue; // triangle is entirely behind nearplane
3679
3680                 {
3681                         // calculate integer y coords for triangle points
3682                         __m128i screeni = _mm_packs_epi32(_mm_cvttps_epi32(_mm_shuffle_ps(screen[0], screen[1], _MM_SHUFFLE(1, 0, 1, 0))),
3683                                                                                   _mm_cvttps_epi32(_mm_shuffle_ps(screen[2], numpoints <= 3 ? screen[2] : screen[3], _MM_SHUFFLE(1, 0, 1, 0)))),
3684                                         screenir = _mm_shuffle_epi32(screeni, _MM_SHUFFLE(1, 0, 3, 2)), 
3685                                         screenmin = _mm_min_epi16(screeni, screenir), 
3686                                         screenmax = _mm_max_epi16(screeni, screenir);
3687                         screenmin = _mm_min_epi16(screenmin, _mm_shufflelo_epi16(screenmin, _MM_SHUFFLE(1, 0, 3, 2)));
3688                         screenmax = _mm_max_epi16(screenmax, _mm_shufflelo_epi16(screenmax, _MM_SHUFFLE(1, 0, 3, 2)));
3689                         screenmin = _mm_max_epi16(screenmin, _mm_setzero_si128());
3690                         screenmax = _mm_min_epi16(screenmax, fbmax);
3691                         // skip offscreen triangles
3692                         if (_mm_cvtsi128_si32(_mm_cmplt_epi16(screenmax, screenmin)))
3693                                 continue;
3694                         starty = _mm_extract_epi16(screenmin, 1);
3695                         endy = _mm_extract_epi16(screenmax, 1)+1;
3696                         screeny = _mm_srai_epi32(screeni, 16);
3697                 }
3698
3699                 // okay, this triangle is going to produce spans, we'd better project
3700                 // the interpolants now (this is what gives perspective texturing),
3701                 // this consists of simply multiplying all arrays by the W coord
3702                 // (which is basically 1/Z), which will be undone per-pixel
3703                 // (multiplying by Z again) to get the perspective-correct array
3704                 // values
3705                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3706                 {
3707                         //if (arraymask[j])
3708                         {
3709                                 for (k = 0;k < numpoints;k++)
3710                                 {
3711                                         proj[j][k] = _mm_mul_ps(clipped[j][k], _mm_shuffle_ps(screen[k], screen[k], _MM_SHUFFLE(3, 3, 3, 3)));
3712                                 }
3713                         }
3714                 }
3715                 // adjust texture LOD by texture density, in the simplest way possible...
3716                 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)));
3717                 mipedgescale = _mm_mul_ps(mipedgescale, mipedgescale);
3718                 mipedgescale = _mm_div_ps(_mm_set1_ps(1.0f), _mm_add_ps(mipedgescale, _mm_shuffle_ps(mipedgescale, mipedgescale, _MM_SHUFFLE(2, 3, 0, 1))));
3719                 for (j = 0;j < DPSOFTRAST_MAXTEXTUREUNITS;j++)
3720                 {
3721                         texture = dpsoftrast.texbound[j];
3722                         if (texture)
3723                         {
3724                                 __m128 mipedgetc;
3725                                 if (texture->filter <= DPSOFTRAST_TEXTURE_FILTER_LINEAR)
3726                                 {
3727                                         mip[j] = 0;
3728                                         continue;
3729                                 }
3730                                 k = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].lodarrayindex;
3731                                 mipedgetc = _mm_sub_ps(_mm_shuffle_ps(clipped[k][0], clipped[k][2], _MM_SHUFFLE(1, 0, 1, 0)),
3732                                                                                 _mm_shuffle_ps(clipped[k][1], clipped[k][1], _MM_SHUFFLE(1, 0, 1, 0)));
3733                                 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))));
3734                                 mipedgetc = _mm_mul_ps(mipedgetc, mipedgetc);
3735                                 mipedgetc = _mm_add_ps(mipedgetc, _mm_shuffle_ps(mipedgetc, mipedgetc, _MM_SHUFFLE(2, 3, 0, 1)));
3736                                 mipedgetc = _mm_mul_ps(mipedgetc, mipedgescale);
3737                                 mipedgetc = _mm_min_ss(mipedgetc, _mm_shuffle_ps(mipedgetc, mipedgetc, _MM_SHUFFLE(2, 2, 2, 2)));       
3738                                 // this will be multiplied in the texturing routine by the texture resolution
3739                                 y = _mm_cvtss_si32(mipedgetc);
3740                                 if (y > 0) 
3741                                 {
3742                                         y = (int)(log(y)/M_LN2);
3743                                         if (y > texture->mipmaps - 1)
3744                                                 y = texture->mipmaps - 1;
3745                                 }
3746                                 else y = 0;
3747                                 mip[j] = y;
3748                         }
3749                 }
3750                 // iterate potential spans
3751                 // TODO: optimize?  if we figured out the edge order beforehand, this
3752                 //         could do loops over the edges in the proper order rather than
3753                 //         selecting them for each span
3754                 // TODO: optimize?  the edges could have data slopes calculated
3755                 // TODO: optimize?  the data slopes could be calculated as a plane
3756                 //         (2D slopes) to avoid any interpolation along edges at all
3757                 for (y = starty+1;y < endy;)
3758                 {
3759                         int nexty = -1;
3760                         __m128 edge0offset, edge1offset, edge0scale, edge1scale, data[DPSOFTRAST_ARRAY_TOTAL+1][2], slope[DPSOFTRAST_ARRAY_TOTAL+1][2];
3761                         __m128i screenycc = _mm_cmpgt_epi32(_mm_set1_epi32(y), screeny);
3762                         int screenymask = _mm_movemask_epi8(screenycc);
3763                         if (numpoints == 4)
3764                         {
3765                                 switch(screenymask)
3766                                 {
3767                                 default:
3768                                 case 0xFFFF: /*0000*/ y++; continue;
3769                                 case 0xFFF0: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 0;break;
3770                                 case 0xFF0F: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 1;break;
3771                                 case 0xFF00: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 1;break;
3772                                 case 0xF0FF: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 2;break;
3773                                 case 0xF0F0: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 2;break; // concave - nonsense
3774                                 case 0xF00F: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 2;break;
3775                                 case 0xF000: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 3;edge1n = 2;break;
3776                                 case 0x0FFF: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 3;break;
3777                                 case 0x0FF0: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 0;break;
3778                                 case 0x0F0F: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
3779                                 case 0x0F00: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 2;edge1n = 1;break;
3780                                 case 0x00FF: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 3;break;
3781                                 case 0x00F0: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 1;edge1n = 0;break;
3782                                 case 0x000F: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 0;edge1n = 3;break;
3783                                 case 0x0000: /*1111*/ y++; continue;
3784                                 }
3785                         }
3786                         else
3787                         {
3788                                 switch(screenymask)
3789                                 {
3790                                 default:
3791                                 case 0xFFFF: /*000*/ y++; continue;
3792                                 case 0xFFF0: /*100*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 0;break;
3793                                 case 0xFF0F: /*010*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 1;break;
3794                                 case 0xFF00: /*110*/ edge0p = 2;edge0n = 0;edge1p = 2;edge1n = 1;break;
3795                                 case 0x00FF: /*001*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 2;break;
3796                                 case 0x00F0: /*101*/ edge0p = 1;edge0n = 2;edge1p = 1;edge1n = 0;break;
3797                                 case 0x000F: /*011*/ edge0p = 0;edge0n = 1;edge1p = 0;edge1n = 2;break;
3798                                 case 0x0000: /*111*/ y++; continue;
3799                                 }
3800                         }
3801                         screenycc = _mm_max_epi16(_mm_srli_epi16(screenycc, 1), screeny);
3802                         screenycc = _mm_min_epi16(screenycc, _mm_shuffle_epi32(screenycc, _MM_SHUFFLE(1, 0, 3, 2)));  
3803                         screenycc = _mm_min_epi16(screenycc, _mm_shuffle_epi32(screenycc, _MM_SHUFFLE(2, 3, 0, 1)));
3804                         nexty = _mm_extract_epi16(screenycc, 0);        
3805                         if(nexty >= endy) nexty = endy-1;
3806                         if (_mm_ucomigt_ss(_mm_max_ss(screen[edge0n], screen[edge0p]), _mm_min_ss(screen[edge1n], screen[edge1p])))
3807                         {
3808                                 int tmp = edge0n;
3809                                 edge0n = edge1n;
3810                                 edge1n = tmp;
3811                                 tmp = edge0p;
3812                                 edge0p = edge1p;
3813                                 edge1p = tmp;
3814                         }       
3815                         edge0offset = _mm_shuffle_ps(screen[edge0p], screen[edge0p], _MM_SHUFFLE(1, 1, 1, 1));
3816                         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));
3817                         edge0scale = _mm_shuffle_ps(edge0scale, edge0scale, _MM_SHUFFLE(0, 0, 0, 0));
3818                         edge0offset = _mm_sub_ps(_mm_set1_ps(y), edge0offset);
3819                         edge1offset = _mm_shuffle_ps(screen[edge1p], screen[edge1p], _MM_SHUFFLE(1, 1, 1, 1));
3820                         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));
3821                         edge1offset = _mm_sub_ps(_mm_set1_ps(y), edge1offset);
3822                         edge1scale = _mm_shuffle_ps(edge1scale, edge1scale, _MM_SHUFFLE(0, 0, 0, 0));
3823                         j = DPSOFTRAST_ARRAY_TOTAL;
3824                         slope[j][0] = _mm_mul_ps(_mm_sub_ps(screen[edge0n], screen[edge0p]), edge0scale);
3825                         slope[j][1] = _mm_mul_ps(_mm_sub_ps(screen[edge1n], screen[edge1p]), edge1scale);
3826                         data[j][0] = _mm_add_ps(_mm_mul_ps(slope[j][0], edge0offset), screen[edge0p]);
3827                         data[j][1] = _mm_add_ps(_mm_mul_ps(slope[j][1], edge1offset), screen[edge1p]);
3828                         data[j][1] = _mm_sub_ps(data[j][1], data[j][0]);
3829                         slope[j][1] = _mm_sub_ps(slope[j][1], slope[j][0]);
3830                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3831                         {
3832                                 //if (arraymask[j])
3833                                 {
3834                                         slope[j][0] = _mm_mul_ps(_mm_sub_ps(proj[j][edge0n], proj[j][edge0p]), edge0scale);
3835                                         slope[j][1] = _mm_mul_ps(_mm_sub_ps(proj[j][edge1n], proj[j][edge1p]), edge1scale);
3836                                         data[j][0] = _mm_add_ps(_mm_mul_ps(slope[j][0], edge0offset), proj[j][edge0p]);
3837                                         data[j][1] = _mm_add_ps(_mm_mul_ps(slope[j][1], edge1offset), proj[j][edge1p]);
3838                                         data[j][1] = _mm_sub_ps(data[j][1], data[j][0]);
3839                                         slope[j][1] = _mm_sub_ps(slope[j][1], slope[j][0]);
3840                                 }
3841                         }
3842                         goto firstspan;
3843                         for(; y <= nexty; y++)
3844                         {
3845                                 __m128 data0, data1, spanilength, startxlerp;
3846                                 j = DPSOFTRAST_ARRAY_TOTAL;
3847                                 data[j][0] = _mm_add_ps(data[j][0], slope[j][0]);
3848                                 data[j][1] = _mm_add_ps(data[j][1], slope[j][1]);
3849                                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3850                                 {
3851                                         //if (arraymask[j])
3852                                         {
3853                                                 data[j][0] = _mm_add_ps(data[j][0], slope[j][0]);
3854                                                 data[j][1] = _mm_add_ps(data[j][1], slope[j][1]);
3855                                         }
3856                                 }
3857
3858                         firstspan:
3859                                 startx = _mm_cvtss_si32(_mm_add_ss(data[DPSOFTRAST_ARRAY_TOTAL][0], _mm_set1_ps(0.5f)));
3860                                 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)));
3861                                 if (startx < 0) startx = 0;
3862                                 if (endx > width) endx = width;
3863                                 if (startx >= endx) continue;
3864 #if 0
3865                                 _mm_store_ss(&startxf, data0);
3866                                 _mm_store_ss(&endxf, data1);
3867                                 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); }
3868 #endif
3869                                 spanilength = _mm_div_ss(_mm_set1_ps(1.0f), data[DPSOFTRAST_ARRAY_TOTAL][1]);
3870                                 spanilength = _mm_shuffle_ps(spanilength, spanilength, _MM_SHUFFLE(0, 0, 0, 0));
3871                                 startxlerp = _mm_sub_ss(_mm_cvtsi32_ss(_mm_setzero_ps(), startx), data[DPSOFTRAST_ARRAY_TOTAL][0]);
3872                                 startxlerp = _mm_shuffle_ps(startxlerp, startxlerp, _MM_SHUFFLE(0, 0, 0, 0));
3873                                 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3874                                 memcpy(span->mip, mip, sizeof(span->mip));
3875                                 span->start = y * width + startx;
3876                                 span->length = endx - startx;
3877                                 j = DPSOFTRAST_ARRAY_TOTAL;
3878                                 data1 = _mm_mul_ps(data[j][1], spanilength);
3879                                 data0 = _mm_add_ps(data[j][0], _mm_mul_ps(data1, startxlerp));
3880                                 _mm_store_ps(span->data[0][j], data0);
3881                                 _mm_store_ps(span->data[1][j], data1);
3882                                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3883                                 {
3884                                         //if (arraymask[j])
3885                                         {
3886                                                 data1 = _mm_mul_ps(data[j][1], spanilength);
3887                                                 data0 = _mm_add_ps(data[j][0], _mm_mul_ps(data1, startxlerp));
3888                                                 _mm_store_ps(span->data[0][j], data0);
3889                                                 _mm_store_ps(span->data[1][j], data1);
3890                                         }
3891                                 }
3892                                 // to keep the shader routines from needing more than a small
3893                                 // buffer for pixel intermediate data, we split long spans...
3894                                 while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
3895                                 {
3896                                         span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
3897                                         if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3898                                         {
3899                                                 DPSOFTRAST_Draw_ProcessSpans();
3900                                                 dpsoftrast.draw.numspans = 0;
3901                                         }
3902                                         oldspan = span;
3903                                         span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3904                                         *span = *oldspan;
3905                                         startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
3906                                         span->start = y * width + startx;
3907                                         span->length = endx - startx;
3908                                         j = DPSOFTRAST_ARRAY_TOTAL;
3909                                         _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))));
3910                                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3911                                         {
3912                                                 //if (arraymask[j])
3913                                                 {
3914                                                          _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))));
3915                                                 }
3916                                         }
3917                                 }
3918                                 // after all that, we have a span suitable for the pixel shader...
3919                                 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3920                                 {
3921                                         DPSOFTRAST_Draw_ProcessSpans();
3922                                         dpsoftrast.draw.numspans = 0;
3923                                 }
3924                         }
3925                 }
3926                 // draw outlines over triangle for debugging
3927         //      for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
3928         //              DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
3929         }
3930         if (dpsoftrast.draw.numspans)
3931         {
3932                 DPSOFTRAST_Draw_ProcessSpans();
3933                 dpsoftrast.draw.numspans = 0;
3934         }
3935 #endif
3936 }
3937
3938 void DPSOFTRAST_Draw_DebugPoints(void)
3939 {
3940         int i;
3941         int x;
3942         int y;
3943         int numvertices = dpsoftrast.draw.numvertices;
3944         int w = dpsoftrast.fb_width;
3945         int bounds[4];
3946         unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
3947         const float *c4f;
3948         bounds[0] = dpsoftrast.fb_viewportscissor[0];
3949         bounds[1] = dpsoftrast.fb_viewportscissor[1];
3950         bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
3951         bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
3952         for (i = 0;i < numvertices;i++)
3953         {
3954                 // check nearclip
3955                 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
3956                 //      continue;
3957                 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
3958                 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
3959                 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
3960                 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
3961                 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
3962                 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
3963                 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
3964                         continue;
3965                 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
3966                 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
3967         }
3968 }
3969
3970 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
3971 {
3972         unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL];
3973         arraymask[0] = true;
3974         arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode)
3975         arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL;
3976         arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL;
3977         arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL;
3978         arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL;
3979         arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL;
3980         arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL;
3981         arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL;
3982         arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL;
3983         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
3984         DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true);
3985         DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Vertex();
3986         DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
3987         DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numtriangles, element3i, element3s, arraymask);
3988 }
3989
3990 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
3991 {
3992         union
3993         {
3994                 int i;
3995                 unsigned char b[4];
3996         }
3997         u;
3998         u.i = 1;
3999         memset(&dpsoftrast, 0, sizeof(dpsoftrast));
4000         dpsoftrast.bigendian = u.b[3];
4001         dpsoftrast.fb_width = width;
4002         dpsoftrast.fb_height = height;
4003         dpsoftrast.fb_depthpixels = depthpixels;
4004         dpsoftrast.fb_colorpixels[0] = colorpixels;
4005         dpsoftrast.fb_colorpixels[1] = NULL;
4006         dpsoftrast.fb_colorpixels[1] = NULL;
4007         dpsoftrast.fb_colorpixels[1] = NULL;
4008         dpsoftrast.texture_firstfree = 1;
4009         dpsoftrast.texture_end = 1;
4010         dpsoftrast.texture_max = 0;
4011         dpsoftrast.user.colormask[0] = 1;
4012         dpsoftrast.user.colormask[1] = 1;
4013         dpsoftrast.user.colormask[2] = 1;
4014         dpsoftrast.user.colormask[3] = 1;
4015         dpsoftrast.user.blendfunc[0] = GL_ONE;
4016         dpsoftrast.user.blendfunc[1] = GL_ZERO;
4017         dpsoftrast.user.depthmask = true;
4018         dpsoftrast.user.depthtest = true;
4019         dpsoftrast.user.depthfunc = GL_LEQUAL;
4020         dpsoftrast.user.scissortest = false;
4021         dpsoftrast.user.cullface = GL_BACK;
4022         dpsoftrast.user.alphatest = false;
4023         dpsoftrast.user.alphafunc = GL_GREATER;
4024         dpsoftrast.user.alphavalue = 0.5f;
4025         dpsoftrast.user.scissor[0] = 0;
4026         dpsoftrast.user.scissor[1] = 0;
4027         dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
4028         dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
4029         dpsoftrast.user.viewport[0] = 0;
4030         dpsoftrast.user.viewport[1] = 0;
4031         dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
4032         dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
4033         dpsoftrast.user.depthrange[0] = 0;
4034         dpsoftrast.user.depthrange[1] = 1;
4035         dpsoftrast.user.polygonoffset[0] = 0;
4036         dpsoftrast.user.polygonoffset[1] = 0;
4037         dpsoftrast.user.color[0] = 1;
4038         dpsoftrast.user.color[1] = 1;
4039         dpsoftrast.user.color[2] = 1;
4040         dpsoftrast.user.color[3] = 1;
4041         dpsoftrast.validate = -1;
4042         DPSOFTRAST_Validate(-1);
4043         dpsoftrast.validate = 0;
4044 }
4045
4046 void DPSOFTRAST_Shutdown(void)
4047 {
4048         int i;
4049         for (i = 0;i < dpsoftrast.texture_end;i++)
4050                 if (dpsoftrast.texture[i].bytes)
4051                         MM_FREE(dpsoftrast.texture[i].bytes);
4052         if (dpsoftrast.texture)
4053                 free(dpsoftrast.texture);
4054         memset(&dpsoftrast, 0, sizeof(dpsoftrast));
4055 }
4056