]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - dpsoftrast.c
fix overrun test for optimized texture routines
[xonotic/darkplaces.git] / dpsoftrast.c
1
2 #include <memory.h>
3 #include "dpsoftrast.h"
4 #include <stdio.h>
5 #include <math.h>
6
7 #undef true
8 #undef false
9 #ifndef __cplusplus
10 typedef enum bool {false, true} bool;
11 #endif
12
13 #if defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1400)
14 #define RESTRICT __restrict
15 #else
16 #define RESTRICT
17 #endif
18
19 #define GL_NONE                                 0
20 #define GL_FRONT_LEFT                   0x0400
21 #define GL_FRONT_RIGHT                  0x0401
22 #define GL_BACK_LEFT                    0x0402
23 #define GL_BACK_RIGHT                   0x0403
24 #define GL_FRONT                                0x0404
25 #define GL_BACK                                 0x0405
26 #define GL_LEFT                                 0x0406
27 #define GL_RIGHT                                0x0407
28 #define GL_FRONT_AND_BACK               0x0408
29 #define GL_AUX0                                 0x0409
30 #define GL_AUX1                                 0x040A
31 #define GL_AUX2                                 0x040B
32 #define GL_AUX3                                 0x040C
33
34 #define GL_NEVER                                0x0200
35 #define GL_LESS                                 0x0201
36 #define GL_EQUAL                                0x0202
37 #define GL_LEQUAL                               0x0203
38 #define GL_GREATER                              0x0204
39 #define GL_NOTEQUAL                             0x0205
40 #define GL_GEQUAL                               0x0206
41 #define GL_ALWAYS                               0x0207
42
43 #define GL_ZERO                                 0x0
44 #define GL_ONE                                  0x1
45 #define GL_SRC_COLOR                            0x0300
46 #define GL_ONE_MINUS_SRC_COLOR                  0x0301
47 #define GL_DST_COLOR                            0x0306
48 #define GL_ONE_MINUS_DST_COLOR                  0x0307
49 #define GL_SRC_ALPHA                            0x0302
50 #define GL_ONE_MINUS_SRC_ALPHA                  0x0303
51 #define GL_DST_ALPHA                            0x0304
52 #define GL_ONE_MINUS_DST_ALPHA                  0x0305
53 #define GL_SRC_ALPHA_SATURATE                   0x0308
54 #define GL_CONSTANT_COLOR                       0x8001
55 #define GL_ONE_MINUS_CONSTANT_COLOR             0x8002
56 #define GL_CONSTANT_ALPHA                       0x8003
57 #define GL_ONE_MINUS_CONSTANT_ALPHA             0x8004
58
59 typedef enum DPSOFTRAST_ARRAY_e
60 {
61         DPSOFTRAST_ARRAY_POSITION,
62         DPSOFTRAST_ARRAY_COLOR,
63         DPSOFTRAST_ARRAY_TEXCOORD0,
64         DPSOFTRAST_ARRAY_TEXCOORD1,
65         DPSOFTRAST_ARRAY_TEXCOORD2,
66         DPSOFTRAST_ARRAY_TEXCOORD3,
67         DPSOFTRAST_ARRAY_TEXCOORD4,
68         DPSOFTRAST_ARRAY_TEXCOORD5,
69         DPSOFTRAST_ARRAY_TEXCOORD6,
70         DPSOFTRAST_ARRAY_TEXCOORD7,
71         DPSOFTRAST_ARRAY_TOTAL
72 }
73 DPSOFTRAST_ARRAY;
74
75 typedef struct DPSOFTRAST_Texture_s
76 {
77         int flags;
78         int width;
79         int height;
80         int depth;
81         int sides;
82         DPSOFTRAST_TEXTURE_FILTER filter;
83         int mipmaps;
84         int size;
85         unsigned char *bytes;
86         int mipmap[DPSOFTRAST_MAXMIPMAPS][5];
87 }
88 DPSOFTRAST_Texture;
89
90 typedef struct DPSOFTRAST_State_User_s
91 {
92         int colormask[4];
93         int blendfunc[2];
94         int blendsubtract;
95         int depthmask;
96         int depthtest;
97         int depthfunc;
98         int scissortest;
99         int cullface;
100         int alphatest;
101         int alphafunc;
102         float alphavalue;
103         int scissor[4];
104         int viewport[4];
105         float depthrange[2];
106         float polygonoffset[2];
107         float color[4];
108 }
109 DPSOFTRAST_State_User;
110
111 #define DPSOFTRAST_MAXSUBSPAN 16
112
113 typedef struct DPSOFTRAST_State_Draw_Span_s
114 {
115         int start; // pixel index
116         int length; // pixel count
117         int startx; // usable range (according to pixelmask)
118         int endx; // usable range (according to pixelmask)
119         unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; // texcoord to screen space density values (for picking mipmap of textures)
120         unsigned char *pixelmask; // true for pixels that passed depth test, false for others
121         // [0][n][] is start interpolant values (projected)
122         // [1][n][] is end interpolant values (projected)
123         // [0][DPSOFTRAST_ARRAY_TOTAL][] is start screencoord4f
124         // [1][DPSOFTRAST_ARRAY_TOTAL][] is end screencoord4f
125         // NOTE: screencoord4f[3] is W (basically 1/Z), useful for depthbuffer
126         float data[2][DPSOFTRAST_ARRAY_TOTAL+1][4];
127 }
128 DPSOFTRAST_State_Draw_Span;
129
130 #define DPSOFTRAST_DRAW_MAXSPANQUEUE 1024
131
132 typedef struct DPSOFTRAST_State_Draw_s
133 {
134         int numvertices;
135         int maxvertices;
136         float *in_array4f[DPSOFTRAST_ARRAY_TOTAL];
137         float *post_array4f[DPSOFTRAST_ARRAY_TOTAL];
138         float *screencoord4f;
139
140         // spans are queued in this structure for dispatch to the pixel shader,
141         // partly to improve cache locality, partly for batching purposes, spans
142         // are flushed before DrawTriangles returns to caller
143         int numspans;
144         DPSOFTRAST_State_Draw_Span spanqueue[DPSOFTRAST_DRAW_MAXSPANQUEUE];
145 }
146 DPSOFTRAST_State_Draw;
147
148 #define DPSOFTRAST_VALIDATE_FB 1
149 #define DPSOFTRAST_VALIDATE_DEPTHFUNC 2
150 #define DPSOFTRAST_VALIDATE_BLENDFUNC 4
151 #define DPSOFTRAST_VALIDATE_DRAW (DPSOFTRAST_VALIDATE_FB | DPSOFTRAST_VALIDATE_DEPTHFUNC | DPSOFTRAST_VALIDATE_BLENDFUNC)
152
153 typedef enum DPSOFTRAST_BLENDMODE_e
154 {
155         DPSOFTRAST_BLENDMODE_OPAQUE,
156         DPSOFTRAST_BLENDMODE_ALPHA,
157         DPSOFTRAST_BLENDMODE_ADDALPHA,
158         DPSOFTRAST_BLENDMODE_ADD,
159         DPSOFTRAST_BLENDMODE_INVMOD,
160         DPSOFTRAST_BLENDMODE_MUL,
161         DPSOFTRAST_BLENDMODE_MUL2,
162         DPSOFTRAST_BLENDMODE_SUBALPHA,
163         DPSOFTRAST_BLENDMODE_PSEUDOALPHA,
164         DPSOFTRAST_BLENDMODE_TOTAL
165 }
166 DPSOFTRAST_BLENDMODE;
167
168 typedef struct DPSOFTRAST_State_s
169 {
170         // DPSOFTRAST_VALIDATE_ flags
171         int validate;
172
173         int fb_colormask;
174         int fb_width;
175         int fb_height;
176         unsigned int *fb_depthpixels;
177         unsigned int *fb_colorpixels[4];
178
179         const float *pointer_vertex3f;
180         const float *pointer_color4f;
181         const unsigned char *pointer_color4ub;
182         const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
183         int stride_vertex;
184         int stride_color;
185         int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
186         int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
187         DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
188
189         int shader_mode;
190         int shader_permutation;
191         float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4];
192         int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
193
194         // derived values (DPSOFTRAST_VALIDATE_FB)
195         int fb_clearscissor[4];
196         int fb_viewport[4];
197         int fb_viewportscissor[4];
198         float fb_viewportcenter[2];
199         float fb_viewportscale[2];
200
201         // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
202         int fb_depthfunc;
203
204         // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
205         int fb_blendmode;
206
207         int texture_max;
208         int texture_end;
209         int texture_firstfree;
210         DPSOFTRAST_Texture *texture;
211
212         int bigendian;
213
214         // error reporting
215         const char *errorstring;
216
217         DPSOFTRAST_State_User user;
218
219         DPSOFTRAST_State_Draw draw;
220 }
221 DPSOFTRAST_State;
222
223 DPSOFTRAST_State dpsoftrast;
224
225 extern int dpsoftrast_test;
226
227 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
228 #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))
229 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
230 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
231
232 void DPSOFTRAST_RecalcFB(void)
233 {
234         // calculate framebuffer scissor, viewport, viewport clipped by scissor,
235         // and viewport projection values
236         int x1, x2, x3, x4, x5, x6;
237         int y1, y2, y3, y4, y5, y6;
238         x1 = dpsoftrast.user.scissor[0];
239         x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
240         x3 = dpsoftrast.user.viewport[0];
241         x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
242         y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
243         y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
244         y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
245         y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
246         if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
247         if (x1 < 0) x1 = 0;
248         if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
249         if (x3 < 0) x1 = 0;
250         if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
251         if (y1 < 0) y1 = 0;
252         if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
253         if (y3 < 0) y1 = 0;
254         if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
255         x5 = x1;if (x5 < x3) x5 = x3;
256         x6 = x2;if (x6 > x4) x4 = x4;
257         y5 = y1;if (y5 < y3) y5 = y3;
258         y6 = y2;if (y6 > y4) y6 = y4;
259         dpsoftrast.fb_clearscissor[0] = x1;
260         dpsoftrast.fb_clearscissor[1] = y1;
261         dpsoftrast.fb_clearscissor[2] = x2 - x1;
262         dpsoftrast.fb_clearscissor[3] = y2 - y1;
263         dpsoftrast.fb_viewport[0] = x3;
264         dpsoftrast.fb_viewport[1] = y3;
265         dpsoftrast.fb_viewport[2] = x4 - x3;
266         dpsoftrast.fb_viewport[3] = y4 - y3;
267         dpsoftrast.fb_viewportscissor[0] = x5;
268         dpsoftrast.fb_viewportscissor[1] = y5;
269         dpsoftrast.fb_viewportscissor[2] = x6 - x5;
270         dpsoftrast.fb_viewportscissor[3] = y6 - y5;
271         dpsoftrast.fb_viewportcenter[0] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
272         dpsoftrast.fb_viewportcenter[1] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
273         dpsoftrast.fb_viewportscale[0] = 0.5f * dpsoftrast.user.viewport[2];
274         dpsoftrast.fb_viewportscale[1] = -0.5f * dpsoftrast.user.viewport[3];
275 }
276
277 void DPSOFTRAST_RecalcDepthFunc(void)
278 {
279         dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
280 }
281
282 int blendmodetable[][4] = 
283 {
284         {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
285         {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
286         {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
287         {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
288         {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
289         {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
290         {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
291         {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
292         {DPSOFTRAST_BLENDMODE_PSEUDOALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, false},
293         {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
294 };
295
296 void DPSOFTRAST_RecalcBlendFunc(void)
297 {
298         int i;
299         dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE;
300         for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++)
301         {
302                 if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3])
303                 {
304                         dpsoftrast.fb_blendmode = blendmodetable[i][0];
305                         break;
306                 }
307         }
308 }
309
310 #define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0)
311
312 void DPSOFTRAST_Validate(int mask)
313 {
314         mask &= dpsoftrast.validate;
315         if (!mask)
316                 return;
317         if (mask & DPSOFTRAST_VALIDATE_FB)
318         {
319                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB;
320                 DPSOFTRAST_RecalcFB();
321         }
322         if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
323         {
324                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
325                 DPSOFTRAST_RecalcDepthFunc();
326         }
327         if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
328         {
329                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
330                 DPSOFTRAST_RecalcBlendFunc();
331         }
332 }
333
334 DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
335 {
336         if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
337                 return &dpsoftrast.texture[index];
338         return NULL;
339 }
340
341 int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
342 {
343         int w;
344         int h;
345         int d;
346         int size;
347         int s;
348         int texnum;
349         int mipmaps;
350         int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
351         int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
352         DPSOFTRAST_Texture *texture;
353         if (width*height*depth < 1)
354         {
355                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
356                 return 0;
357         }
358         if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
359         {
360                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
361                 return 0;
362         }
363         switch(texformat)
364         {
365         case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
366         case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
367         case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
368                 break;
369         case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
370                 if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
371                 {
372                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
373                         return 0;
374                 }
375                 if (depth != 1)
376                 {
377                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
378                         return 0;
379                 }
380                 if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
381                 {
382                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
383                         return 0;
384                 }
385                 break;
386         }
387         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
388         {
389                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
390                 return 0;
391         }
392         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
393         {
394                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
395                 return 0;
396         }
397         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
398         {
399                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
400                 return 0;
401         }
402         if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
403         {
404                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
405                 return 0;
406         }
407         if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
408         {
409                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
410                 return 0;
411         }
412         // find first empty slot in texture array
413         for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
414                 if (!dpsoftrast.texture[texnum].bytes)
415                         break;
416         dpsoftrast.texture_firstfree = texnum + 1;
417         if (dpsoftrast.texture_max <= texnum)
418         {
419                 // expand texture array as needed
420                 if (dpsoftrast.texture_max < 1024)
421                         dpsoftrast.texture_max = 1024;
422                 else
423                         dpsoftrast.texture_max *= 2;
424                 dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
425         }
426         if (dpsoftrast.texture_end <= texnum)
427                 dpsoftrast.texture_end = texnum + 1;
428         texture = &dpsoftrast.texture[texnum];
429         memset(texture, 0, sizeof(*texture));
430         texture->flags = flags;
431         texture->width = width;
432         texture->height = height;
433         texture->depth = depth;
434         texture->sides = sides;
435         w = width;
436         h = height;
437         d = depth;
438         size = 0;
439         mipmaps = 0;
440         w = width;
441         h = height;
442         d = depth;
443         for (;;)
444         {
445                 s = w * h * d * sides * 4;
446                 texture->mipmap[mipmaps][0] = size;
447                 texture->mipmap[mipmaps][1] = s;
448                 texture->mipmap[mipmaps][2] = w;
449                 texture->mipmap[mipmaps][3] = h;
450                 texture->mipmap[mipmaps][4] = d;
451                 size += s;
452                 mipmaps++;
453                 if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
454                         break;
455                 if (w > 1) w >>= 1;
456                 if (h > 1) h >>= 1;
457                 if (d > 1) d >>= 1;
458         }
459         texture->mipmaps = mipmaps;
460         texture->size = size;
461
462         // allocate the pixels now
463         texture->bytes = (unsigned char *)calloc(1, size);
464
465         return texnum;
466 }
467 void DPSOFTRAST_Texture_Free(int index)
468 {
469         DPSOFTRAST_Texture *texture;
470         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
471         if (texture->bytes)
472                 free(texture->bytes);
473         texture->bytes = NULL;
474         memset(texture, 0, sizeof(*texture));
475         // adjust the free range and used range
476         if (dpsoftrast.texture_firstfree > index)
477                 dpsoftrast.texture_firstfree = index;
478         while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
479                 dpsoftrast.texture_end--;
480 }
481 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
482 {
483         int i, x, y, z, w, layer0, layer1, row0, row1;
484         unsigned char *o, *i0, *i1, *i2, *i3;
485         DPSOFTRAST_Texture *texture;
486         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
487         if (texture->mipmaps <= 1)
488                 return;
489         for (i = 1;i < texture->mipmaps;i++)
490         {
491                 for (z = 0;z < texture->mipmap[i][4];z++)
492                 {
493                         layer0 = z*2;
494                         layer1 = z*2+1;
495                         if (layer1 >= texture->mipmap[i-1][4])
496                                 layer1 = texture->mipmap[i-1][4]-1;
497                         for (y = 0;y < texture->mipmap[i][3];y++)
498                         {
499                                 row0 = y*2;
500                                 row1 = y*2+1;
501                                 if (row1 >= texture->mipmap[i-1][3])
502                                         row1 = texture->mipmap[i-1][3]-1;
503                                 o =  texture->bytes + texture->mipmap[i  ][0] + 4*((texture->mipmap[i  ][3] * z      + y   ) * texture->mipmap[i  ][2]);
504                                 i0 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row0) * texture->mipmap[i-1][2]);
505                                 i1 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row1) * texture->mipmap[i-1][2]);
506                                 i2 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row0) * texture->mipmap[i-1][2]);
507                                 i3 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row1) * texture->mipmap[i-1][2]);
508                                 w = texture->mipmap[i][2];
509                                 if (layer1 > layer0)
510                                 {
511                                         if (texture->mipmap[i-1][2] > 1)
512                                         {
513                                                 // average 3D texture
514                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8, i2 += 8, i3 += 8)
515                                                 {
516                                                         o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
517                                                         o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
518                                                         o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
519                                                         o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
520                                                 }
521                                         }
522                                         else
523                                         {
524                                                 // average 3D mipmap with parent width == 1
525                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
526                                                 {
527                                                         o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
528                                                         o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
529                                                         o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
530                                                         o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
531                                                 }
532                                         }
533                                 }
534                                 else
535                                 {
536                                         if (texture->mipmap[i-1][2] > 1)
537                                         {
538                                                 // average 2D texture (common case)
539                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
540                                                 {
541                                                         o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
542                                                         o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
543                                                         o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
544                                                         o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
545                                                 }
546                                         }
547                                         else
548                                         {
549                                                 // 2D texture with parent width == 1
550                                                 o[0] = (i0[0] + i1[0] + 1) >> 1;
551                                                 o[1] = (i0[1] + i1[1] + 1) >> 1;
552                                                 o[2] = (i0[2] + i1[2] + 1) >> 1;
553                                                 o[3] = (i0[3] + i1[3] + 1) >> 1;
554                                         }
555                                 }
556                         }
557                 }
558         }
559 }
560 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
561 {
562         DPSOFTRAST_Texture *texture;
563         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
564
565         // FIXME IMPLEMENT
566
567         dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
568 }
569 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
570 {
571         DPSOFTRAST_Texture *texture;
572         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
573
574         memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
575         DPSOFTRAST_Texture_CalculateMipmaps(index);
576 }
577 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
578 {
579         DPSOFTRAST_Texture *texture;
580         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
581         return texture->width;
582 }
583 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
584 {
585         DPSOFTRAST_Texture *texture;
586         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
587         return texture->height;
588 }
589 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
590 {
591         DPSOFTRAST_Texture *texture;
592         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
593         return texture->depth;
594 }
595 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
596 {
597         DPSOFTRAST_Texture *texture;
598         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
599         return texture->bytes;
600 }
601 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
602 {
603         DPSOFTRAST_Texture *texture;
604         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
605         if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
606         {
607                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
608                 return;
609         }
610         texture->filter = filter;
611 }
612
613 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
614 {
615         dpsoftrast.fb_width = width;
616         dpsoftrast.fb_height = height;
617         dpsoftrast.fb_depthpixels = depthpixels;
618         dpsoftrast.fb_colorpixels[0] = colorpixels0;
619         dpsoftrast.fb_colorpixels[1] = colorpixels1;
620         dpsoftrast.fb_colorpixels[2] = colorpixels2;
621         dpsoftrast.fb_colorpixels[3] = colorpixels3;
622 }
623 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
624 {
625         dpsoftrast.user.viewport[0] = x;
626         dpsoftrast.user.viewport[1] = y;
627         dpsoftrast.user.viewport[2] = width;
628         dpsoftrast.user.viewport[3] = height;
629         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
630 }
631 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
632 {
633         int i, x1, y1, x2, y2, w, h, x, y;
634         unsigned int *p;
635         unsigned int c;
636         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
637         x1 = dpsoftrast.fb_clearscissor[0];
638         y1 = dpsoftrast.fb_clearscissor[1];
639         x2 = dpsoftrast.fb_clearscissor[2];
640         y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
641         w = x2 - x1;
642         h = y2 - y1;
643         if (w < 1 || h < 1)
644                 return;
645         // FIXME: honor dpsoftrast.fb_colormask?
646         c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
647         for (i = 0;i < 4;i++)
648         {
649                 if (!dpsoftrast.fb_colorpixels[i])
650                         continue;
651                 for (y = y1;y < y2;y++)
652                 {
653                         p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
654                         for (x = x1;x < x2;x++)
655                                 p[x] = c;
656                 }
657         }
658 }
659 void DPSOFTRAST_ClearDepth(float d)
660 {
661         int x1, y1, x2, y2, w, h, x, y;
662         unsigned int *p;
663         unsigned int c;
664         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
665         x1 = dpsoftrast.fb_clearscissor[0];
666         y1 = dpsoftrast.fb_clearscissor[1];
667         x2 = dpsoftrast.fb_clearscissor[2];
668         y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
669         w = x2 - x1;
670         h = y2 - y1;
671         if (w < 1 || h < 1)
672                 return;
673         c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
674         for (y = y1;y < y2;y++)
675         {
676                 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
677                 for (x = x1;x < x2;x++)
678                         p[x] = c;
679         }
680 }
681 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
682 {
683         dpsoftrast.user.colormask[0] = r != 0;
684         dpsoftrast.user.colormask[1] = g != 0;
685         dpsoftrast.user.colormask[2] = b != 0;
686         dpsoftrast.user.colormask[3] = a != 0;
687         dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
688 }
689 void DPSOFTRAST_DepthTest(int enable)
690 {
691         dpsoftrast.user.depthtest = enable;
692         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
693 }
694 void DPSOFTRAST_ScissorTest(int enable)
695 {
696         dpsoftrast.user.scissortest = enable;
697         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
698 }
699 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
700 {
701         dpsoftrast.user.scissor[0] = x;
702         dpsoftrast.user.scissor[1] = y;
703         dpsoftrast.user.scissor[2] = width;
704         dpsoftrast.user.scissor[3] = height;
705         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
706 }
707
708 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
709 {
710         // FIXME: validate
711         dpsoftrast.user.blendfunc[0] = smodulate;
712         dpsoftrast.user.blendfunc[1] = dmodulate;
713         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
714 }
715 void DPSOFTRAST_BlendSubtract(int enable)
716 {
717         dpsoftrast.user.blendsubtract = enable != 0;
718         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
719 }
720 void DPSOFTRAST_DepthMask(int enable)
721 {
722         dpsoftrast.user.depthmask = enable;
723 }
724 void DPSOFTRAST_DepthFunc(int comparemode)
725 {
726         // FIXME: validate
727         dpsoftrast.user.depthfunc = comparemode;
728 }
729 void DPSOFTRAST_DepthRange(float range0, float range1)
730 {
731         dpsoftrast.user.depthrange[0] = range0;
732         dpsoftrast.user.depthrange[1] = range1;
733 }
734 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
735 {
736         dpsoftrast.user.polygonoffset[0] = alongnormal;
737         dpsoftrast.user.polygonoffset[1] = intoview;
738 }
739 void DPSOFTRAST_CullFace(int mode)
740 {
741         // FIXME: validate
742         dpsoftrast.user.cullface = mode;
743 }
744 void DPSOFTRAST_AlphaTest(float enable)
745 {
746         dpsoftrast.user.alphatest = enable;
747 }
748 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
749 {
750         // FIXME: validate
751         dpsoftrast.user.alphafunc = alphafunc;
752         dpsoftrast.user.alphavalue = alphavalue;
753 }
754 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
755 {
756         dpsoftrast.user.color[0] = r;
757         dpsoftrast.user.color[1] = g;
758         dpsoftrast.user.color[2] = b;
759         dpsoftrast.user.color[3] = a;
760 }
761 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
762 {
763         int outstride = blockwidth * 4;
764         int instride = dpsoftrast.fb_width * 4;
765         int bx1 = blockx;
766         int by1 = blocky;
767         int bx2 = blockx + blockwidth;
768         int by2 = blocky + blockheight;
769         int bw;
770         int bh;
771         int x;
772         int y;
773         unsigned char *inpixels;
774         unsigned char *b;
775         unsigned char *o;
776         if (bx1 < 0) bx1 = 0;
777         if (by1 < 0) by1 = 0;
778         if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
779         if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
780         bw = bx2 - bx1;
781         bh = by2 - by1;
782         inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
783         if (dpsoftrast.bigendian)
784         {
785                 for (y = by1;y < by2;y++)
786                 {
787                         b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
788                         o = (unsigned char *)outpixels + (y - by1) * outstride;
789                         for (x = bx1;x < bx2;x++)
790                         {
791                                 o[0] = b[3];
792                                 o[1] = b[2];
793                                 o[2] = b[1];
794                                 o[3] = b[0];
795                                 o += 4;
796                                 b += 4;
797                         }
798                 }
799         }
800         else
801         {
802                 for (y = by1;y < by2;y++)
803                 {
804                         b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
805                         o = (unsigned char *)outpixels + (y - by1) * outstride;
806                         memcpy(o, b, bw*4);
807                 }
808         }
809
810 }
811 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
812 {
813         int tx1 = tx;
814         int ty1 = ty;
815         int tx2 = tx + width;
816         int ty2 = ty + height;
817         int sx1 = sx;
818         int sy1 = sy;
819         int sx2 = sx + width;
820         int sy2 = sy + height;
821         int swidth;
822         int sheight;
823         int twidth;
824         int theight;
825         int sw;
826         int sh;
827         int tw;
828         int th;
829         int y;
830         unsigned int *spixels;
831         unsigned int *tpixels;
832         DPSOFTRAST_Texture *texture;
833         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
834         if (mip < 0 || mip >= texture->mipmaps) return;
835         spixels = dpsoftrast.fb_colorpixels[0];
836         swidth = dpsoftrast.fb_width;
837         sheight = dpsoftrast.fb_height;
838         tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
839         twidth = texture->mipmap[mip][2];
840         theight = texture->mipmap[mip][3];
841         if (tx1 < 0) tx1 = 0;
842         if (ty1 < 0) ty1 = 0;
843         if (tx2 > twidth) tx2 = twidth;
844         if (ty2 > theight) ty2 = theight;
845         if (sx1 < 0) sx1 = 0;
846         if (sy1 < 0) sy1 = 0;
847         if (sx2 > swidth) sx2 = swidth;
848         if (sy2 > sheight) sy2 = sheight;
849         tw = tx2 - tx1;
850         th = ty2 - ty1;
851         sw = sx2 - sx1;
852         sh = sy2 - sy1;
853         if (tw > sw) tw = sw;
854         if (th > sh) th = sh;
855         if (tw < 1 || th < 1)
856                 return;
857         for (y = 0;y < th;y++)
858                 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
859         if (texture->mipmaps > 1)
860                 DPSOFTRAST_Texture_CalculateMipmaps(index);
861 }
862 void DPSOFTRAST_SetTexture(int unitnum, int index)
863 {
864         DPSOFTRAST_Texture *texture;
865         if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
866         {
867                 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
868                 return;
869         }
870         texture = DPSOFTRAST_Texture_GetByIndex(index);
871         if (index && !texture)
872         {
873                 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
874                 return;
875         }
876         dpsoftrast.texbound[unitnum] = texture;
877 }
878
879 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
880 {
881         dpsoftrast.pointer_vertex3f = vertex3f;
882         dpsoftrast.stride_vertex = stride;
883 }
884 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
885 {
886         dpsoftrast.pointer_color4f = color4f;
887         dpsoftrast.pointer_color4ub = NULL;
888         dpsoftrast.stride_color = stride;
889 }
890 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
891 {
892         dpsoftrast.pointer_color4f = NULL;
893         dpsoftrast.pointer_color4ub = color4ub;
894         dpsoftrast.stride_color = stride;
895 }
896 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
897 {
898         dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
899         dpsoftrast.components_texcoord[unitnum] = numcomponents;
900         dpsoftrast.stride_texcoord[unitnum] = stride;
901 }
902
903 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
904 {
905         dpsoftrast.shader_mode = mode;
906         dpsoftrast.shader_permutation = permutation;
907 }
908 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
909 {
910         dpsoftrast.uniform4f[index*4+0] = v0;
911         dpsoftrast.uniform4f[index*4+1] = v1;
912         dpsoftrast.uniform4f[index*4+2] = v2;
913         dpsoftrast.uniform4f[index*4+3] = v3;
914 }
915 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v)
916 {
917         dpsoftrast.uniform4f[index*4+0] = v[0];
918         dpsoftrast.uniform4f[index*4+1] = v[1];
919         dpsoftrast.uniform4f[index*4+2] = v[2];
920         dpsoftrast.uniform4f[index*4+3] = v[3];
921 }
922 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v)
923 {
924         dpsoftrast.uniform4f[index*4+0] = v[0];
925         dpsoftrast.uniform4f[index*4+1] = v[1];
926         dpsoftrast.uniform4f[index*4+2] = v[2];
927         dpsoftrast.uniform4f[index*4+3] = v[3];
928         dpsoftrast.uniform4f[index*4+4] = v[4];
929         dpsoftrast.uniform4f[index*4+5] = v[5];
930         dpsoftrast.uniform4f[index*4+6] = v[6];
931         dpsoftrast.uniform4f[index*4+7] = v[7];
932         dpsoftrast.uniform4f[index*4+8] = v[8];
933         dpsoftrast.uniform4f[index*4+9] = v[9];
934         dpsoftrast.uniform4f[index*4+10] = v[10];
935         dpsoftrast.uniform4f[index*4+11] = v[11];
936         dpsoftrast.uniform4f[index*4+12] = v[12];
937         dpsoftrast.uniform4f[index*4+13] = v[13];
938         dpsoftrast.uniform4f[index*4+14] = v[14];
939         dpsoftrast.uniform4f[index*4+15] = v[15];
940 }
941 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
942 {
943         dpsoftrast.uniform1i[index] = i0;
944 }
945
946 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors)
947 {
948         int i;
949         int j;
950         int stride;
951         const float *v;
952         float *p;
953         float *data;
954         const unsigned char *b;
955         dpsoftrast.draw.numvertices = numvertices;
956         if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
957         {
958                 if (dpsoftrast.draw.maxvertices < 4096)
959                         dpsoftrast.draw.maxvertices = 4096;
960                 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
961                         dpsoftrast.draw.maxvertices *= 2;
962                 if (dpsoftrast.draw.in_array4f[0])
963                         free(dpsoftrast.draw.in_array4f[0]);
964                 data = (float *)calloc(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
965                 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
966                         dpsoftrast.draw.in_array4f[i] = data;
967                 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
968                         dpsoftrast.draw.post_array4f[i] = data;
969                 dpsoftrast.draw.screencoord4f = data;
970                 data += dpsoftrast.draw.maxvertices * 4;
971         }
972         stride = dpsoftrast.stride_vertex;
973         v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
974         p = dpsoftrast.draw.in_array4f[0];
975         for (i = 0;i < numvertices;i++)
976         {
977                 p[0] = v[0];
978                 p[1] = v[1];
979                 p[2] = v[2];
980                 p[3] = 1.0f;
981                 p += 4;
982                 v = (const float *)((const unsigned char *)v + stride);
983         }
984         if (needcolors)
985         {
986                 if (dpsoftrast.pointer_color4f)
987                 {
988                         stride = dpsoftrast.stride_color;
989                         v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
990                         p = dpsoftrast.draw.in_array4f[1];
991                         for (i = 0;i < numvertices;i++)
992                         {
993                                 p[0] = v[0];
994                                 p[1] = v[1];
995                                 p[2] = v[2];
996                                 p[3] = v[3];
997                                 p += 4;
998                                 v = (const float *)((const unsigned char *)v + stride);
999                         }
1000                 }
1001                 else if (dpsoftrast.pointer_color4ub)
1002                 {
1003                         stride = dpsoftrast.stride_color;
1004                         b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
1005                         p = dpsoftrast.draw.in_array4f[1];
1006                         for (i = 0;i < numvertices;i++)
1007                         {
1008                                 p[0] = b[0] * (1.0f / 255.0f);
1009                                 p[1] = b[1] * (1.0f / 255.0f);
1010                                 p[2] = b[2] * (1.0f / 255.0f);
1011                                 p[3] = b[3] * (1.0f / 255.0f);
1012                                 p += 4;
1013                                 b = (const unsigned char *)((const unsigned char *)b + stride);
1014                         }
1015                 }
1016                 else
1017                 {
1018                         v = dpsoftrast.user.color;
1019                         p = dpsoftrast.draw.in_array4f[1];
1020                         for (i = 0;i < numvertices;i++)
1021                         {
1022                                 p[0] = v[0];
1023                                 p[1] = v[1];
1024                                 p[2] = v[2];
1025                                 p[3] = v[3];
1026                                 p += 4;
1027                         }
1028                 }
1029         }
1030         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL-2;j++)
1031         {
1032                 if (dpsoftrast.pointer_texcoordf[j])
1033                 {
1034                         stride = dpsoftrast.stride_texcoord[j];
1035                         v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1036                         p = dpsoftrast.draw.in_array4f[j+2];
1037                         switch(dpsoftrast.components_texcoord[j])
1038                         {
1039                         case 2:
1040                                 for (i = 0;i < numvertices;i++)
1041                                 {
1042                                         p[0] = v[0];
1043                                         p[1] = v[1];
1044                                         p[2] = 0.0f;
1045                                         p[3] = 1.0f;
1046                                         p += 4;
1047                                         v = (const float *)((const unsigned char *)v + stride);
1048                                 }
1049                                 break;
1050                         case 3:
1051                                 for (i = 0;i < numvertices;i++)
1052                                 {
1053                                         p[0] = v[0];
1054                                         p[1] = v[1];
1055                                         p[2] = v[2];
1056                                         p[3] = 1.0f;
1057                                         p += 4;
1058                                         v = (const float *)((const unsigned char *)v + stride);
1059                                 }
1060                                 break;
1061                         case 4:
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] = v[3];
1068                                         p += 4;
1069                                         v = (const float *)((const unsigned char *)v + stride);
1070                                 }
1071                                 break;
1072                         }
1073                 }
1074         }
1075 }
1076
1077 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1078 {
1079         static const float identitymatrix[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
1080         // TODO: SIMD
1081         float matrix[4][4];
1082         int i;
1083         memcpy(matrix, inmatrix16f, sizeof(float[16]));
1084         if (!memcmp(identitymatrix, matrix, sizeof(float[16])))
1085         {
1086                 // fast case for identity matrix
1087                 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1088                 return;
1089         }
1090         for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1091         {
1092                 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1093                 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1094                 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1095                 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1096         }
1097 }
1098
1099 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1100 {
1101         memcpy(out4f, in4f, numitems * sizeof(float[4]));
1102 }
1103
1104 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1105 {
1106         // NOTE: this is used both as a whole mesh transform function and a
1107         // per-triangle transform function (for clipped triangles), accordingly
1108         // it should not crash on divide by 0 but the result of divide by 0 is
1109         // unimportant...
1110         // TODO: SIMD
1111         int i;
1112         float w;
1113         float viewportcenter[4];
1114         float viewportscale[4];
1115         viewportscale[0] = dpsoftrast.fb_viewportscale[0];
1116         viewportscale[1] = dpsoftrast.fb_viewportscale[1];
1117         viewportscale[2] = 0.5f;
1118         viewportscale[3] = 0.0f;
1119         viewportcenter[0] = dpsoftrast.fb_viewportcenter[0];
1120         viewportcenter[1] = dpsoftrast.fb_viewportcenter[1];
1121         viewportcenter[2] = 0.5f;
1122         viewportcenter[3] = 0.0f;
1123         for (i = 0;i < numitems;i++)
1124         {
1125                 if (!in4f[3])
1126                 {
1127                         out4f[0] = 0.0f;
1128                         out4f[1] = 0.0f;
1129                         out4f[2] = 0.0f;
1130                         out4f[3] = 0.0f;
1131                         continue;
1132                 }
1133                 w = 1.0f / in4f[3];
1134                 out4f[0] = viewportcenter[0] + viewportscale[0] * in4f[0] * w;
1135                 out4f[1] = viewportcenter[1] + viewportscale[1] * in4f[1] * w;
1136                 out4f[2] = viewportcenter[2] + viewportscale[2] * in4f[2] * w;
1137                 out4f[3] = viewportcenter[3] + viewportscale[3] * in4f[3] * w;
1138                 out4f[3] = w;
1139                 in4f += 4;
1140                 out4f += 4;
1141         }
1142 }
1143
1144 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1145 {
1146         int i;
1147         int x;
1148         int y;
1149         int w = dpsoftrast.fb_width;
1150         int bounds[4];
1151         float v0[2], v1[2];
1152         unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1153         //const float *c4f;
1154         bounds[0] = dpsoftrast.fb_viewportscissor[0];
1155         bounds[1] = dpsoftrast.fb_viewportscissor[1];
1156         bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1157         bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1158         v0[0] = screen0[0];
1159         v0[1] = screen0[1];
1160         v1[0] = screen1[0];
1161         v1[1] = screen1[1];
1162         for (i = 0;i <= 128;i++)
1163         {
1164                 // check nearclip
1165                 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1166                 //      continue;
1167                 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1168                 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1169                 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1170                         continue;
1171                 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1172                 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1173                 pixels[y*w+x] = 0xFFFFFFFF;
1174         }
1175 }
1176
1177 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *zf)
1178 {
1179         int x;
1180         int startx = span->startx;
1181         int endx = span->endx;
1182         float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1183         float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1184         for (x = startx;x < endx;)
1185         {
1186                 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1187                 float z = 1.0f / (w + wslope * x), dz;
1188                 if (endsub >= endx)
1189                 {
1190                         endsub = endx-1;
1191                         dz = endsub > x ? (1.0f / (w + wslope * endsub) - z) / (endsub - x) : 0.0f;
1192                 }
1193                 else
1194                 {
1195                         dz = (1.0f / (w + wslope * endsub) - z) * (1.0f / (DPSOFTRAST_MAXSUBSPAN-1));
1196                 }
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         int x;
1369         int startx = span->startx;
1370         int endx = span->endx;
1371         int d[4];
1372         const unsigned int * RESTRICT ini = (const unsigned int *)in4ub;
1373         int a, b;
1374         unsigned char * RESTRICT pixelmask = span->pixelmask;
1375         unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1376         unsigned int * RESTRICT pixeli = (unsigned int *)dpsoftrast.fb_colorpixels[0];
1377         if (!pixel)
1378                 return;
1379         pixel += span->start * 4;
1380         pixeli += span->start;
1381         // handle alphatest now (this affects depth writes too)
1382         if (dpsoftrast.user.alphatest)
1383                 for (x = startx;x < endx;x++)
1384                         if (in4ub[x*4+3] < 0.5f)
1385                                 pixelmask[x] = false;
1386         // FIXME: this does not handle bigendian
1387         switch(dpsoftrast.fb_blendmode)
1388         {
1389         case DPSOFTRAST_BLENDMODE_OPAQUE:
1390                 for (x = startx;x < endx;x++)
1391                         if (pixelmask[x])
1392                                 pixeli[x] = ini[x];
1393                 break;
1394         case DPSOFTRAST_BLENDMODE_ALPHA:
1395                 for (x = startx;x < endx;x++)
1396                 {
1397                         if (!pixelmask[x])
1398                                 continue;
1399                         a = in4ub[x*4+3];
1400                         b = 256 - in4ub[x*4+3];
1401                         pixel[x*4+0] = (in4ub[x*4+0]*a+pixel[x*4+0]*b) >> 8;
1402                         pixel[x*4+1] = (in4ub[x*4+1]*a+pixel[x*4+1]*b) >> 8;
1403                         pixel[x*4+2] = (in4ub[x*4+2]*a+pixel[x*4+2]*b) >> 8;
1404                         pixel[x*4+3] = (in4ub[x*4+3]*a+pixel[x*4+3]*b) >> 8;
1405                 }
1406                 break;
1407         case DPSOFTRAST_BLENDMODE_ADDALPHA:
1408                 for (x = startx;x < endx;x++)
1409                 {
1410                         if (!pixelmask[x])
1411                                 continue;
1412                         a = in4ub[x*4+3];
1413                         d[0] = (((in4ub[x*4+0]*a)>>8)+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1414                         d[1] = (((in4ub[x*4+1]*a)>>8)+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1415                         d[2] = (((in4ub[x*4+2]*a)>>8)+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1416                         d[3] = (((in4ub[x*4+3]*a)>>8)+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1417                         pixel[x*4+0] = d[0];
1418                         pixel[x*4+1] = d[1];
1419                         pixel[x*4+2] = d[2];
1420                         pixel[x*4+3] = d[3];
1421                 }
1422                 break;
1423         case DPSOFTRAST_BLENDMODE_ADD:
1424                 for (x = startx;x < endx;x++)
1425                 {
1426                         if (!pixelmask[x])
1427                                 continue;
1428                         d[0] = (in4ub[x*4+0]+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1429                         d[1] = (in4ub[x*4+1]+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1430                         d[2] = (in4ub[x*4+2]+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1431                         d[3] = (in4ub[x*4+3]+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1432                         pixel[x*4+0] = d[0];
1433                         pixel[x*4+1] = d[1];
1434                         pixel[x*4+2] = d[2];
1435                         pixel[x*4+3] = d[3];
1436                 }
1437                 break;
1438         case DPSOFTRAST_BLENDMODE_INVMOD:
1439                 for (x = startx;x < endx;x++)
1440                 {
1441                         if (!pixelmask[x])
1442                                 continue;
1443                         pixel[x*4+0] = ((255-in4ub[x*4+0])*pixel[x*4+0])>>8;
1444                         pixel[x*4+1] = ((255-in4ub[x*4+1])*pixel[x*4+1])>>8;
1445                         pixel[x*4+2] = ((255-in4ub[x*4+2])*pixel[x*4+2])>>8;
1446                         pixel[x*4+3] = ((255-in4ub[x*4+3])*pixel[x*4+3])>>8;
1447                 }
1448                 break;
1449         case DPSOFTRAST_BLENDMODE_MUL:
1450                 for (x = startx;x < endx;x++)
1451                 {
1452                         if (!pixelmask[x])
1453                                 continue;
1454                         pixel[x*4+0] = (in4ub[x*4+0]*pixel[x*4+0])>>8;
1455                         pixel[x*4+1] = (in4ub[x*4+1]*pixel[x*4+1])>>8;
1456                         pixel[x*4+2] = (in4ub[x*4+2]*pixel[x*4+2])>>8;
1457                         pixel[x*4+3] = (in4ub[x*4+3]*pixel[x*4+3])>>8;
1458                 }
1459                 break;
1460         case DPSOFTRAST_BLENDMODE_MUL2:
1461                 for (x = startx;x < endx;x++)
1462                 {
1463                         if (!pixelmask[x])
1464                                 continue;
1465                         d[0] = (in4ub[x*4+0]*pixel[x*4+0])>>7;if (d[0] > 255) d[0] = 255;
1466                         d[1] = (in4ub[x*4+1]*pixel[x*4+1])>>7;if (d[1] > 255) d[1] = 255;
1467                         d[2] = (in4ub[x*4+2]*pixel[x*4+2])>>7;if (d[2] > 255) d[2] = 255;
1468                         d[3] = (in4ub[x*4+3]*pixel[x*4+3])>>7;if (d[3] > 255) d[3] = 255;
1469                         pixel[x*4+0] = d[0];
1470                         pixel[x*4+1] = d[1];
1471                         pixel[x*4+2] = d[2];
1472                         pixel[x*4+3] = d[3];
1473                 }
1474                 break;
1475         case DPSOFTRAST_BLENDMODE_SUBALPHA:
1476                 for (x = startx;x < endx;x++)
1477                 {
1478                         if (!pixelmask[x])
1479                                 continue;
1480                         a = in4ub[x*4+3];
1481                         d[0] = pixel[x*4+0]-((in4ub[x*4+0]*a)>>8);if (d[0] < 0) d[0] = 0;
1482                         d[1] = pixel[x*4+1]-((in4ub[x*4+1]*a)>>8);if (d[1] < 0) d[1] = 0;
1483                         d[2] = pixel[x*4+2]-((in4ub[x*4+2]*a)>>8);if (d[2] < 0) d[2] = 0;
1484                         d[3] = pixel[x*4+3]-((in4ub[x*4+3]*a)>>8);if (d[3] < 0) d[3] = 0;
1485                         pixel[x*4+0] = d[0];
1486                         pixel[x*4+1] = d[1];
1487                         pixel[x*4+2] = d[2];
1488                         pixel[x*4+3] = d[3];
1489                 }
1490                 break;
1491         case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1492                 for (x = startx;x < endx;x++)
1493                 {
1494                         if (!pixelmask[x])
1495                                 continue;
1496                         b = 255 - in4ub[x*4+3];
1497                         d[0] = in4ub[x*4+0]+((pixel[x*4+0]*b)>>8);if (d[0] > 255) d[0] = 255;
1498                         d[1] = in4ub[x*4+1]+((pixel[x*4+1]*b)>>8);if (d[1] > 255) d[1] = 255;
1499                         d[2] = in4ub[x*4+2]+((pixel[x*4+2]*b)>>8);if (d[2] > 255) d[2] = 255;
1500                         d[3] = in4ub[x*4+3]+((pixel[x*4+3]*b)>>8);if (d[3] > 255) d[3] = 255;
1501                         pixel[x*4+0] = d[0];
1502                         pixel[x*4+1] = d[1];
1503                         pixel[x*4+2] = d[2];
1504                         pixel[x*4+3] = d[3];
1505                 }
1506                 break;
1507         }
1508 }
1509
1510 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float * RESTRICT out4f, int texunitindex, int arrayindex, const float * RESTRICT zf)
1511 {
1512         int x;
1513         int startx = span->startx;
1514         int endx = span->endx;
1515         int flags;
1516         float c[4];
1517         float data[4];
1518         float slope[4];
1519         float tc[2];
1520         float tcscale[2];
1521         unsigned int tci[2];
1522         unsigned int tci1[2];
1523         unsigned int tcimin[2];
1524         unsigned int tcimax[2];
1525         int tciwrapmask[2];
1526         int tciwidth;
1527         int filter;
1528         int mip;
1529         const unsigned char * RESTRICT pixelbase;
1530         const unsigned char * RESTRICT pixel[4];
1531         DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1532         // if no texture is bound, just fill it with white
1533         if (!texture)
1534         {
1535                 for (x = startx;x < endx;x++)
1536                 {
1537                         out4f[x*4+0] = 1.0f;
1538                         out4f[x*4+1] = 1.0f;
1539                         out4f[x*4+2] = 1.0f;
1540                         out4f[x*4+3] = 1.0f;
1541                 }
1542                 return;
1543         }
1544         mip = span->mip[texunitindex];
1545         // if this mipmap of the texture is 1 pixel, just fill it with that color
1546         if (texture->mipmap[mip][1] == 4)
1547         {
1548                 c[0] = texture->bytes[2] * (1.0f/255.0f);
1549                 c[1] = texture->bytes[1] * (1.0f/255.0f);
1550                 c[2] = texture->bytes[0] * (1.0f/255.0f);
1551                 c[3] = texture->bytes[3] * (1.0f/255.0f);
1552                 for (x = startx;x < endx;x++)
1553                 {
1554                         out4f[x*4+0] = c[0];
1555                         out4f[x*4+1] = c[1];
1556                         out4f[x*4+2] = c[2];
1557                         out4f[x*4+3] = c[3];
1558                 }
1559                 return;
1560         }
1561         filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1562         data[0] = span->data[0][arrayindex][0];
1563         data[1] = span->data[0][arrayindex][1];
1564         data[2] = span->data[0][arrayindex][2];
1565         data[3] = span->data[0][arrayindex][3];
1566         slope[0] = span->data[1][arrayindex][0];
1567         slope[1] = span->data[1][arrayindex][1];
1568         slope[2] = span->data[1][arrayindex][2];
1569         slope[3] = span->data[1][arrayindex][3];
1570         flags = texture->flags;
1571         pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
1572         tcscale[0] = texture->mipmap[mip][2];
1573         tcscale[1] = texture->mipmap[mip][3];
1574         tciwidth = texture->mipmap[mip][2];
1575         tcimin[0] = 0;
1576         tcimin[1] = 0;
1577         tcimax[0] = texture->mipmap[mip][2]-1;
1578         tcimax[1] = texture->mipmap[mip][3]-1;
1579         tciwrapmask[0] = texture->mipmap[mip][2]-1;
1580         tciwrapmask[1] = texture->mipmap[mip][3]-1;
1581         for (x = startx;x < endx;)
1582         {
1583                 float endtc[2];
1584                 unsigned int subtc[2];
1585                 unsigned int substep[2];
1586                 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1587                 float subscale = 4096.0f/(DPSOFTRAST_MAXSUBSPAN-1);
1588                 if (endsub >= endx)
1589                 {
1590                         endsub = endx-1;
1591                         subscale = endsub > x ? 4096.0f / (endsub - x) : 1.0f;
1592                 }
1593                 tc[0] = (data[0] + slope[0]*x) * zf[x] * tcscale[0] - 0.5f;
1594                 tc[1] = (data[1] + slope[1]*x) * zf[x] * tcscale[1] - 0.5f;
1595                 endtc[0] = (data[0] + slope[0]*endsub) * zf[endsub] * tcscale[0] - 0.5f;
1596                 endtc[1] = (data[1] + slope[1]*endsub) * zf[endsub] * tcscale[1] - 0.5f;
1597                 substep[0] = (endtc[0] - tc[0]) * subscale;
1598                 substep[1] = (endtc[1] - tc[1]) * subscale;
1599                 subtc[0] = tc[0] * (1<<12);
1600                 subtc[1] = tc[1] * (1<<12);
1601                 if (!(flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE))
1602                 {
1603                         subtc[0] &= (tciwrapmask[0]<<12)|0xFFF;
1604                         subtc[1] &= (tciwrapmask[1]<<12)|0xFFF;
1605                 }
1606                 if(filter)
1607                 {
1608                         tci[0] = (subtc[0]>>12) - tcimin[0];
1609                         tci[1] = (subtc[1]>>12) - tcimin[1];
1610                         tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12);
1611                         tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12);
1612                         if (tci[0] <= tcimax[0]-1 && tci[1] <= tcimax[1]-1 && tci1[0] <= tcimax[0]-1 && tci1[1] <= tcimax[1]-1)
1613                         {
1614                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1615                                 {
1616                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1617                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1618                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1619                                         tci[0] = subtc[0]>>12;
1620                                         tci[1] = subtc[1]>>12;
1621                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1622                                         pixel[1] = pixel[0] + 4 * tciwidth;
1623                                         c[0] = (pixel[0][2]*lerp[0]+pixel[0][4+2]*lerp[1]+pixel[1][2]*lerp[2]+pixel[1][4+2]*lerp[3]) * (1.0f / 0xFF000000);
1624                                         c[1] = (pixel[0][1]*lerp[0]+pixel[0][4+1]*lerp[1]+pixel[1][1]*lerp[2]+pixel[1][4+1]*lerp[3]) * (1.0f / 0xFF000000);
1625                                         c[2] = (pixel[0][0]*lerp[0]+pixel[0][4+0]*lerp[1]+pixel[1][0]*lerp[2]+pixel[1][4+0]*lerp[3]) * (1.0f / 0xFF000000);
1626                                         c[3] = (pixel[0][3]*lerp[0]+pixel[0][4+3]*lerp[1]+pixel[1][3]*lerp[2]+pixel[1][4+3]*lerp[3]) * (1.0f / 0xFF000000);
1627                                         out4f[x*4+0] = c[0];
1628                                         out4f[x*4+1] = c[1];
1629                                         out4f[x*4+2] = c[2];
1630                                         out4f[x*4+3] = c[3];
1631                                 }
1632                         }
1633                         else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1634                         {
1635                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1636                                 {
1637                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1638                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1639                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1640                                         tci[0] = subtc[0]>>12;
1641                                         tci[1] = subtc[1]>>12;
1642                                         tci1[0] = tci[0] + 1;
1643                                         tci1[1] = tci[1] + 1;
1644                                         tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1645                                         tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1646                                         tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1647                                         tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1648                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1649                                         pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1650                                         pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1651                                         pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1652                                         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);
1653                                         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);
1654                                         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);
1655                                         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);
1656                                         out4f[x*4+0] = c[0];
1657                                         out4f[x*4+1] = c[1];
1658                                         out4f[x*4+2] = c[2];
1659                                         out4f[x*4+3] = c[3];
1660                                 }
1661                         }
1662                         else
1663                         {
1664                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1665                                 {
1666                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1667                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1668                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1669                                         tci[0] = subtc[0]>>12;
1670                                         tci[1] = subtc[1]>>12;
1671                                         tci1[0] = tci[0] + 1;
1672                                         tci1[1] = tci[1] + 1;
1673                                         tci[0] &= tciwrapmask[0];
1674                                         tci[1] &= tciwrapmask[1];
1675                                         tci1[0] &= tciwrapmask[0];
1676                                         tci1[1] &= tciwrapmask[1];
1677                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1678                                         pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1679                                         pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1680                                         pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1681                                         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);
1682                                         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);
1683                                         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);
1684                                         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);
1685                                         out4f[x*4+0] = c[0];
1686                                         out4f[x*4+1] = c[1];
1687                                         out4f[x*4+2] = c[2];
1688                                         out4f[x*4+3] = c[3];
1689                                 }
1690                         }
1691                 }
1692                 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1693                 {
1694                         for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1695                         {
1696                                 tci[0] = subtc[0]>>12;
1697                                 tci[1] = subtc[1]>>12;
1698                                 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1699                                 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1700                                 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1701                                 c[0] = pixel[0][2] * (1.0f / 255.0f);
1702                                 c[1] = pixel[0][1] * (1.0f / 255.0f);
1703                                 c[2] = pixel[0][0] * (1.0f / 255.0f);
1704                                 c[3] = pixel[0][3] * (1.0f / 255.0f);
1705                                 out4f[x*4+0] = c[0];
1706                                 out4f[x*4+1] = c[1];
1707                                 out4f[x*4+2] = c[2];
1708                                 out4f[x*4+3] = c[3];
1709                         }
1710                 }
1711                 else
1712                 {
1713                         for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1714                         {
1715                                 tci[0] = subtc[0]>>12;
1716                                 tci[1] = subtc[1]>>12;
1717                                 tci[0] &= tciwrapmask[0];
1718                                 tci[1] &= tciwrapmask[1];
1719                                 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1720                                 c[0] = pixel[0][2] * (1.0f / 255.0f);
1721                                 c[1] = pixel[0][1] * (1.0f / 255.0f);
1722                                 c[2] = pixel[0][0] * (1.0f / 255.0f);
1723                                 c[3] = pixel[0][3] * (1.0f / 255.0f);
1724                                 out4f[x*4+0] = c[0];
1725                                 out4f[x*4+1] = c[1];
1726                                 out4f[x*4+2] = c[2];
1727                                 out4f[x*4+3] = c[3];
1728                         }
1729                 }
1730         }
1731 }
1732
1733 void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf)
1734 {
1735         int x;
1736         int startx = span->startx;
1737         int endx = span->endx;
1738         int flags;
1739         float data[4];
1740         float slope[4];
1741         float tc[2];
1742         float tcscale[2];
1743         unsigned int tci[2];
1744         unsigned int tci1[2];
1745         unsigned int tcimin[2];
1746         unsigned int tcimax[2];
1747         int tciwrapmask[2];
1748         int tciwidth;
1749         int filter;
1750         int mip;
1751         unsigned int k;
1752         unsigned int *outi = (unsigned int *)out4ub;
1753         const unsigned char * RESTRICT pixelbase;
1754         const unsigned int * RESTRICT pixelbasei;
1755         const unsigned char * RESTRICT pixel[4];
1756         DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1757         // if no texture is bound, just fill it with white
1758         if (!texture)
1759         {
1760                 memset(out4ub, 255, span->length*4);
1761                 return;
1762         }
1763         mip = span->mip[texunitindex];
1764         // if this mipmap of the texture is 1 pixel, just fill it with that color
1765         if (texture->mipmap[mip][1] == 4)
1766         {
1767                 k = *((const unsigned int *)texture->bytes);
1768                 for (x = startx;x < endx;x++)
1769                         outi[x] = k;
1770                 return;
1771         }
1772         filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1773         data[0] = span->data[0][arrayindex][0];
1774         data[1] = span->data[0][arrayindex][1];
1775         data[2] = span->data[0][arrayindex][2];
1776         data[3] = span->data[0][arrayindex][3];
1777         slope[0] = span->data[1][arrayindex][0];
1778         slope[1] = span->data[1][arrayindex][1];
1779         slope[2] = span->data[1][arrayindex][2];
1780         slope[3] = span->data[1][arrayindex][3];
1781         flags = texture->flags;
1782         pixelbase = (const unsigned char *)texture->bytes + texture->mipmap[mip][0];
1783         pixelbasei = (const unsigned int *)pixelbase;
1784         tcscale[0] = texture->mipmap[mip][2];
1785         tcscale[1] = texture->mipmap[mip][3];
1786         tciwidth = texture->mipmap[mip][2];
1787         tcimin[0] = 0;
1788         tcimin[1] = 0;
1789         tcimax[0] = texture->mipmap[mip][2]-1;
1790         tcimax[1] = texture->mipmap[mip][3]-1;
1791         tciwrapmask[0] = texture->mipmap[mip][2]-1;
1792         tciwrapmask[1] = texture->mipmap[mip][3]-1;
1793         for (x = startx;x < endx;)
1794         {
1795                 float endtc[2];
1796                 unsigned int subtc[2];
1797                 unsigned int substep[2];
1798                 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1799                 float subscale = 4096.0f/(DPSOFTRAST_MAXSUBSPAN-1);
1800                 if (endsub >= endx)
1801                 {
1802                         endsub = endx-1;
1803                         subscale = endsub > x ? 4096.0f / (endsub - x) : 1.0f;
1804                 }
1805                 tc[0] = (data[0] + slope[0]*x) * zf[x] * tcscale[0] - 0.5f;
1806                 tc[1] = (data[1] + slope[1]*x) * zf[x] * tcscale[1] - 0.5f;
1807                 endtc[0] = (data[0] + slope[0]*endsub) * zf[endsub] * tcscale[0] - 0.5f;
1808                 endtc[1] = (data[1] + slope[1]*endsub) * zf[endsub] * tcscale[1] - 0.5f;
1809                 substep[0] = (endtc[0] - tc[0]) * subscale;
1810                 substep[1] = (endtc[1] - tc[1]) * subscale;
1811                 subtc[0] = tc[0] * (1<<12);
1812                 subtc[1] = tc[1] * (1<<12);
1813                 if (!(flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE))
1814                 {
1815                         subtc[0] &= (tciwrapmask[0]<<12)|0xFFF;
1816                         subtc[1] &= (tciwrapmask[1]<<12)|0xFFF;
1817                 }
1818 #if 0
1819 // LordHavoc: an attempt at reducing number of integer multiplies, did not show any improvement in benchmarks, abandoned.
1820                 if (filter && dpsoftrast_test)
1821                 {
1822                         const unsigned int * RESTRICT pixeli[4];
1823                         tci[0] = (subtc[0]>>12) - tcimin[0];
1824                         tci[1] = (subtc[1]>>12) - tcimin[1]; 
1825                         tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12);
1826                         tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12); 
1827                         if (tci[0] <= tcimax[0]-1 && tci[1] <= tcimax[1]-1 && tci1[0] <= tcimax[0]-1 && tci1[1] <= tcimax[1]-1)
1828                         {
1829                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1830                                 {
1831                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1832                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1833                                         unsigned int lerp[4] = { (ifrac[0]*ifrac[1]) >> 16, (frac[0]*ifrac[1]) >> 16, (ifrac[0]*frac[1]) >> 16, (frac[0]*frac[1]) >> 16 };
1834                                         tci[0] = subtc[0]>>12;
1835                                         tci[1] = subtc[1]>>12;
1836                                         pixeli[0] = pixelbasei + (tci[1]*tciwidth+tci[0]);
1837                                         pixeli[1] = pixeli[0] + tciwidth;
1838                                         outi[x] = ((((pixeli[0][0] >> 8) & 0x00FF00FF) * lerp[0] + ((pixeli[0][1] >> 8) & 0x00FF00FF) * lerp[1] + ((pixeli[1][0] >> 8) & 0x00FF00FF) * lerp[2] + ((pixeli[1][1] >> 8) & 0x00FF00FF) * lerp[3])     & 0xFF00FF00)
1839                                                 | ((((pixeli[0][0]       & 0x00FF00FF) * lerp[0] + ( pixeli[0][1]       & 0x00FF00FF) * lerp[1] + ( pixeli[1][0]       & 0x00FF00FF) * lerp[2] + ( pixeli[1][1]       & 0x00FF00FF) * lerp[3])>>8) & 0x00FF00FF);
1840                                 }
1841                         }
1842                         else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1843                         {
1844                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1845                                 {
1846                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1847                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1848                                         unsigned int lerp[4] = { (ifrac[0]*ifrac[1]) >> 16, (frac[0]*ifrac[1]) >> 16, (ifrac[0]*frac[1]) >> 16, (frac[0]*frac[1]) >> 16 };
1849                                         tci[0] = subtc[0]>>12;
1850                                         tci[1] = subtc[1]>>12;
1851                                         tci1[0] = tci[0] + 1;
1852                                         tci1[1] = tci[1] + 1;
1853                                         tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1854                                         tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1855                                         tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1856                                         tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1857                                         pixeli[0] = pixelbasei + (tci[1]*tciwidth+tci[0]);
1858                                         pixeli[1] = pixelbasei + (tci[1]*tciwidth+tci1[0]);
1859                                         pixeli[2] = pixelbasei + (tci1[1]*tciwidth+tci[0]);
1860                                         pixeli[3] = pixelbasei + (tci1[1]*tciwidth+tci1[0]);
1861                                         outi[x] = ((((pixeli[0][0] >> 8) & 0x00FF00FF) * lerp[0] + ((pixeli[1][0] >> 8) & 0x00FF00FF) * lerp[1] + ((pixeli[2][0] >> 8) & 0x00FF00FF) * lerp[2] + ((pixeli[3][0] >> 8) & 0x00FF00FF) * lerp[3])     & 0xFF00FF00)
1862                                                 | ((((pixeli[0][0]       & 0x00FF00FF) * lerp[0] + ( pixeli[1][0]       & 0x00FF00FF) * lerp[1] + ( pixeli[2][0]       & 0x00FF00FF) * lerp[2] + ( pixeli[3][0]       & 0x00FF00FF) * lerp[3])>>8) & 0x00FF00FF);
1863                                 }
1864                         }
1865                         else
1866                         {
1867                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1868                                 {
1869                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1870                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1871                                         unsigned int lerp[4] = { (ifrac[0]*ifrac[1]) >> 16, (frac[0]*ifrac[1]) >> 16, (ifrac[0]*frac[1]) >> 16, (frac[0]*frac[1]) >> 16 };
1872                                         tci[0] = subtc[0]>>12;
1873                                         tci[1] = subtc[1]>>12;
1874                                         tci1[0] = tci[0] + 1;
1875                                         tci1[1] = tci[1] + 1;
1876                                         tci[0] &= tciwrapmask[0];
1877                                         tci[1] &= tciwrapmask[1];
1878                                         tci1[0] &= tciwrapmask[0];
1879                                         tci1[1] &= tciwrapmask[1];
1880                                         pixeli[0] = pixelbasei + (tci[1]*tciwidth+tci[0]);
1881                                         pixeli[1] = pixelbasei + (tci[1]*tciwidth+tci1[0]);
1882                                         pixeli[2] = pixelbasei + (tci1[1]*tciwidth+tci[0]);
1883                                         pixeli[3] = pixelbasei + (tci1[1]*tciwidth+tci1[0]);
1884                                         outi[x] = ((((pixeli[0][0] >> 8) & 0x00FF00FF) * lerp[0] + ((pixeli[1][0] >> 8) & 0x00FF00FF) * lerp[1] + ((pixeli[2][0] >> 8) & 0x00FF00FF) * lerp[2] + ((pixeli[3][0] >> 8) & 0x00FF00FF) * lerp[3])     & 0xFF00FF00)
1885                                                 | ((((pixeli[0][0]       & 0x00FF00FF) * lerp[0] + ( pixeli[1][0]       & 0x00FF00FF) * lerp[1] + ( pixeli[2][0]       & 0x00FF00FF) * lerp[2] + ( pixeli[3][0]       & 0x00FF00FF) * lerp[3])>>8) & 0x00FF00FF);
1886                                 }
1887                         }
1888                 }
1889                 else
1890 #endif
1891                 if (filter)
1892                 {
1893                         tci[0] = (subtc[0]>>12) - tcimin[0];
1894                         tci[1] = (subtc[1]>>12) - tcimin[1]; 
1895                         tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12);
1896                         tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12); 
1897                         if (tci[0] <= tcimax[0]-1 && tci[1] <= tcimax[1]-1 && tci1[0] <= tcimax[0]-1 && tci1[1] <= tcimax[1]-1)
1898                         {
1899                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1900                                 {
1901                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1902                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1903                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1904                                         tci[0] = subtc[0]>>12;
1905                                         tci[1] = subtc[1]>>12;
1906                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1907                                         pixel[1] = pixel[0] + 4 * tciwidth;
1908                                         out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[0][4+0]*lerp[1]+pixel[1][0]*lerp[2]+pixel[1][4+0]*lerp[3]) >> 24;
1909                                         out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[0][4+1]*lerp[1]+pixel[1][1]*lerp[2]+pixel[1][4+1]*lerp[3]) >> 24;
1910                                         out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[0][4+2]*lerp[1]+pixel[1][2]*lerp[2]+pixel[1][4+2]*lerp[3]) >> 24;
1911                                         out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[0][4+3]*lerp[1]+pixel[1][3]*lerp[2]+pixel[1][4+3]*lerp[3]) >> 24;
1912                                 }
1913                         }
1914                         else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1915                         {
1916                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1917                                 {
1918                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1919                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1920                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1921                                         tci[0] = subtc[0]>>12;
1922                                         tci[1] = subtc[1]>>12;
1923                                         tci1[0] = tci[0] + 1;
1924                                         tci1[1] = tci[1] + 1;
1925                                         tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1926                                         tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1927                                         tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1928                                         tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1929                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1930                                         pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1931                                         pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1932                                         pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1933                                         out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) >> 24;
1934                                         out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) >> 24;
1935                                         out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) >> 24;
1936                                         out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) >> 24;
1937                                 }
1938                         }
1939                         else
1940                         {
1941                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1942                                 {
1943                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1944                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1945                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1946                                         tci[0] = subtc[0]>>12;
1947                                         tci[1] = subtc[1]>>12;
1948                                         tci1[0] = tci[0] + 1;
1949                                         tci1[1] = tci[1] + 1;
1950                                         tci[0] &= tciwrapmask[0];
1951                                         tci[1] &= tciwrapmask[1];
1952                                         tci1[0] &= tciwrapmask[0];
1953                                         tci1[1] &= tciwrapmask[1];
1954                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1955                                         pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1956                                         pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1957                                         pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1958                                         out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) >> 24;
1959                                         out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) >> 24;
1960                                         out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) >> 24;
1961                                         out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) >> 24;
1962                                 }
1963                         }
1964                 }
1965                 else
1966                 {
1967                         tci[0] = (subtc[0]>>12) - tcimin[0];
1968                         tci[1] = (subtc[1]>>12) - tcimin[1]; 
1969                         tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12);
1970                         tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12); 
1971                         if (tci[0] <= tcimax[0]-1 && tci[1] <= tcimax[1]-1 && tci1[0] <= tcimax[0]-1 && tci1[1] <= tcimax[1]-1)
1972                         {
1973                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1974                                 {
1975                                         tci[0] = subtc[0]>>12;
1976                                         tci[1] = subtc[1]>>12;
1977                                         outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
1978                                 }
1979                         }
1980                         else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1981                         {
1982                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1983                                 {
1984                                         tci[0] = subtc[0]>>12;
1985                                         tci[1] = subtc[1]>>12;
1986                                         tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1987                                         tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1988                                         outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
1989                                 }
1990                         }
1991                         else
1992                         {
1993                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1994                                 {
1995                                         tci[0] = subtc[0]>>12;
1996                                         tci[1] = subtc[1]>>12;
1997                                         tci[0] &= tciwrapmask[0];
1998                                         tci[1] &= tciwrapmask[1];
1999                                         outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
2000                                 }
2001                         }
2002                 }
2003         }
2004 }
2005
2006 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, int arrayindex, const float *zf)
2007 {
2008         int x;
2009         int startx = span->startx;
2010         int endx = span->endx;
2011         float c[4];
2012         float data[4];
2013         float slope[4];
2014         float z;
2015         data[0] = span->data[0][arrayindex][0];
2016         data[1] = span->data[0][arrayindex][1];
2017         data[2] = span->data[0][arrayindex][2];
2018         data[3] = span->data[0][arrayindex][3];
2019         slope[0] = span->data[1][arrayindex][0];
2020         slope[1] = span->data[1][arrayindex][1];
2021         slope[2] = span->data[1][arrayindex][2];
2022         slope[3] = span->data[1][arrayindex][3];
2023         for (x = startx;x < endx;x++)
2024         {
2025                 z = zf[x];
2026                 c[0] = (data[0] + slope[0]*x) * z;
2027                 c[1] = (data[1] + slope[1]*x) * z;
2028                 c[2] = (data[2] + slope[2]*x) * z;
2029                 c[3] = (data[3] + slope[3]*x) * z;
2030                 out4f[x*4+0] = in4f[x*4+0] * c[0];
2031                 out4f[x*4+1] = in4f[x*4+1] * c[1];
2032                 out4f[x*4+2] = in4f[x*4+2] * c[2];
2033                 out4f[x*4+3] = in4f[x*4+3] * c[3];
2034         }
2035 }
2036
2037 void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, int arrayindex, const float *zf)
2038 {
2039         int x;
2040         int startx = span->startx;
2041         int endx = span->endx;
2042         float c[4];
2043         float data[4];
2044         float slope[4];
2045         float z;
2046         data[0] = span->data[0][arrayindex][0];
2047         data[1] = span->data[0][arrayindex][1];
2048         data[2] = span->data[0][arrayindex][2];
2049         data[3] = span->data[0][arrayindex][3];
2050         slope[0] = span->data[1][arrayindex][0];
2051         slope[1] = span->data[1][arrayindex][1];
2052         slope[2] = span->data[1][arrayindex][2];
2053         slope[3] = span->data[1][arrayindex][3];
2054         for (x = startx;x < endx;x++)
2055         {
2056                 z = zf[x];
2057                 c[0] = (data[0] + slope[0]*x) * z;
2058                 c[1] = (data[1] + slope[1]*x) * z;
2059                 c[2] = (data[2] + slope[2]*x) * z;
2060                 c[3] = (data[3] + slope[3]*x) * z;
2061                 out4f[x*4+0] = c[0];
2062                 out4f[x*4+1] = c[1];
2063                 out4f[x*4+2] = c[2];
2064                 out4f[x*4+3] = c[3];
2065         }
2066 }
2067
2068 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
2069 {
2070         int x, startx = span->startx, endx = span->endx;
2071         float c[4], localcolor[4];
2072         localcolor[0] = subcolor[0];
2073         localcolor[1] = subcolor[1];
2074         localcolor[2] = subcolor[2];
2075         localcolor[3] = subcolor[3];
2076         for (x = startx;x < endx;x++)
2077         {
2078                 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
2079                 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
2080                 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
2081                 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
2082                 out4f[x*4+0] = ina4f[x*4+0] + c[0];
2083                 out4f[x*4+1] = ina4f[x*4+1] + c[1];
2084                 out4f[x*4+2] = ina4f[x*4+2] + c[2];
2085                 out4f[x*4+3] = ina4f[x*4+3] + c[3];
2086         }
2087 }
2088
2089 void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2090 {
2091         int x, startx = span->startx, endx = span->endx;
2092         for (x = startx;x < endx;x++)
2093         {
2094                 out4f[x*4+0] = ina4f[x*4+0] * inb4f[x*4+0];
2095                 out4f[x*4+1] = ina4f[x*4+1] * inb4f[x*4+1];
2096                 out4f[x*4+2] = ina4f[x*4+2] * inb4f[x*4+2];
2097                 out4f[x*4+3] = ina4f[x*4+3] * inb4f[x*4+3];
2098         }
2099 }
2100
2101 void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2102 {
2103         int x, startx = span->startx, endx = span->endx;
2104         for (x = startx;x < endx;x++)
2105         {
2106                 out4f[x*4+0] = ina4f[x*4+0] + inb4f[x*4+0];
2107                 out4f[x*4+1] = ina4f[x*4+1] + inb4f[x*4+1];
2108                 out4f[x*4+2] = ina4f[x*4+2] + inb4f[x*4+2];
2109                 out4f[x*4+3] = ina4f[x*4+3] + inb4f[x*4+3];
2110         }
2111 }
2112
2113 void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2114 {
2115         int x, startx = span->startx, endx = span->endx;
2116         float a, b;
2117         for (x = startx;x < endx;x++)
2118         {
2119                 a = 1.0f - inb4f[x*4+3];
2120                 b = inb4f[x*4+3];
2121                 out4f[x*4+0] = ina4f[x*4+0] * a + inb4f[x*4+0] * b;
2122                 out4f[x*4+1] = ina4f[x*4+1] * a + inb4f[x*4+1] * b;
2123                 out4f[x*4+2] = ina4f[x*4+2] * a + inb4f[x*4+2] * b;
2124                 out4f[x*4+3] = ina4f[x*4+3] * a + inb4f[x*4+3] * b;
2125         }
2126 }
2127
2128 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, const float *color)
2129 {
2130         int x, startx = span->startx, endx = span->endx;
2131         float localcolor[4], ilerp, lerp;
2132         localcolor[0] = color[0];
2133         localcolor[1] = color[1];
2134         localcolor[2] = color[2];
2135         localcolor[3] = color[3];
2136         ilerp = 1.0f - localcolor[3];
2137         lerp = localcolor[3];
2138         for (x = startx;x < endx;x++)
2139         {
2140                 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
2141                 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
2142                 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
2143                 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
2144         }
2145 }
2146
2147
2148
2149 void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, int arrayindex, const float *zf)
2150 {
2151         int x;
2152         int startx = span->startx;
2153         int endx = span->endx;
2154         float data[4];
2155         float slope[4];
2156         float z;
2157         data[2] = span->data[0][arrayindex][0];
2158         data[1] = span->data[0][arrayindex][1];
2159         data[0] = span->data[0][arrayindex][2];
2160         data[3] = span->data[0][arrayindex][3];
2161         slope[2] = span->data[1][arrayindex][0];
2162         slope[1] = span->data[1][arrayindex][1];
2163         slope[0] = span->data[1][arrayindex][2];
2164         slope[3] = span->data[1][arrayindex][3];
2165         for (x = startx;x < endx;x++)
2166         {
2167                 z = zf[x];
2168                 out4ub[x*4+0] = (int)(in4ub[x*4+0] * (data[0] + slope[0]*x) * z);
2169                 out4ub[x*4+1] = (int)(in4ub[x*4+1] * (data[1] + slope[1]*x) * z);
2170                 out4ub[x*4+2] = (int)(in4ub[x*4+2] * (data[2] + slope[2]*x) * z);
2171                 out4ub[x*4+3] = (int)(in4ub[x*4+3] * (data[3] + slope[3]*x) * z);
2172         }
2173 }
2174
2175 void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, int arrayindex, const float *zf)
2176 {
2177         int x;
2178         int startx = span->startx;
2179         int endx = span->endx;
2180         float data[4];
2181         float slope[4];
2182         float z;
2183         data[2] = span->data[0][arrayindex][0]*255.0f;
2184         data[1] = span->data[0][arrayindex][1]*255.0f;
2185         data[0] = span->data[0][arrayindex][2]*255.0f;
2186         data[3] = span->data[0][arrayindex][3]*255.0f;
2187         slope[2] = span->data[1][arrayindex][0]*255.0f;
2188         slope[1] = span->data[1][arrayindex][1]*255.0f;
2189         slope[0] = span->data[1][arrayindex][2]*255.0f;
2190         slope[3] = span->data[1][arrayindex][3]*255.0f;
2191         for (x = startx;x < endx;x++)
2192         {
2193                 z = zf[x];
2194                 out4ub[x*4+0] = (int)((data[0] + slope[0]*x) * z);
2195                 out4ub[x*4+1] = (int)((data[1] + slope[1]*x) * z);
2196                 out4ub[x*4+2] = (int)((data[2] + slope[2]*x) * z);
2197                 out4ub[x*4+3] = (int)((data[3] + slope[3]*x) * z);
2198         }
2199 }
2200
2201 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)
2202 {
2203         int x, startx = span->startx, endx = span->endx;
2204         int c[4], localcolor[4];
2205         localcolor[2] = (int)(subcolor[0] * 255.0f);
2206         localcolor[1] = (int)(subcolor[1] * 255.0f);
2207         localcolor[0] = (int)(subcolor[2] * 255.0f);
2208         localcolor[3] = (int)(subcolor[3] * 255.0f);
2209         for (x = startx;x < endx;x++)
2210         {
2211                 c[0] = inb4ub[x*4+0] - localcolor[0];if (c[0] < 0) c[0] = 0;
2212                 c[1] = inb4ub[x*4+1] - localcolor[1];if (c[1] < 0) c[1] = 0;
2213                 c[2] = inb4ub[x*4+2] - localcolor[2];if (c[2] < 0) c[2] = 0;
2214                 c[3] = inb4ub[x*4+3] - localcolor[3];if (c[3] < 0) c[3] = 0;
2215                 c[0] += ina4ub[x*4+0];if (c[0] > 255) c[0] = 255;
2216                 c[1] += ina4ub[x*4+1];if (c[1] > 255) c[1] = 255;
2217                 c[2] += ina4ub[x*4+2];if (c[2] > 255) c[2] = 255;
2218                 c[3] += ina4ub[x*4+3];if (c[3] > 255) c[3] = 255;
2219                 out4ub[x*4+0] = c[0];
2220                 out4ub[x*4+1] = c[1];
2221                 out4ub[x*4+2] = c[2];
2222                 out4ub[x*4+3] = c[3];
2223         }
2224 }
2225
2226 void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2227 {
2228         int x, startx = span->startx, endx = span->endx;
2229         for (x = startx;x < endx;x++)
2230         {
2231                 out4ub[x*4+0] = (ina4ub[x*4+0] * inb4ub[x*4+0])>>8;
2232                 out4ub[x*4+1] = (ina4ub[x*4+1] * inb4ub[x*4+1])>>8;
2233                 out4ub[x*4+2] = (ina4ub[x*4+2] * inb4ub[x*4+2])>>8;
2234                 out4ub[x*4+3] = (ina4ub[x*4+3] * inb4ub[x*4+3])>>8;
2235         }
2236 }
2237
2238 void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2239 {
2240         int x, startx = span->startx, endx = span->endx;
2241         int d[4];
2242         for (x = startx;x < endx;x++)
2243         {
2244                 d[0] = ina4ub[x*4+0] + inb4ub[x*4+0];if (d[0] > 255) d[0] = 255;
2245                 d[1] = ina4ub[x*4+1] + inb4ub[x*4+1];if (d[1] > 255) d[1] = 255;
2246                 d[2] = ina4ub[x*4+2] + inb4ub[x*4+2];if (d[2] > 255) d[2] = 255;
2247                 d[3] = ina4ub[x*4+3] + inb4ub[x*4+3];if (d[3] > 255) d[3] = 255;
2248                 out4ub[x*4+0] = d[0];
2249                 out4ub[x*4+1] = d[1];
2250                 out4ub[x*4+2] = d[2];
2251                 out4ub[x*4+3] = d[3];
2252         }
2253 }
2254
2255 void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2256 {
2257         int x, startx = span->startx, endx = span->endx;
2258         int a, b;
2259         for (x = startx;x < endx;x++)
2260         {
2261                 a = 256 - inb4ub[x*4+3];
2262                 b = inb4ub[x*4+3];
2263                 out4ub[x*4+0] = (ina4ub[x*4+0] * a + inb4ub[x*4+0] * b)>>8;
2264                 out4ub[x*4+1] = (ina4ub[x*4+1] * a + inb4ub[x*4+1] * b)>>8;
2265                 out4ub[x*4+2] = (ina4ub[x*4+2] * a + inb4ub[x*4+2] * b)>>8;
2266                 out4ub[x*4+3] = (ina4ub[x*4+3] * a + inb4ub[x*4+3] * b)>>8;
2267         }
2268 }
2269
2270 void DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, const float *color)
2271 {
2272         int x, startx = span->startx, endx = span->endx;
2273         int localcolor[4], ilerp, lerp;
2274         localcolor[2] = (int)(color[0]*255.0f);
2275         localcolor[1] = (int)(color[1]*255.0f);
2276         localcolor[0] = (int)(color[2]*255.0f);
2277         localcolor[3] = (int)(color[3]*255.0f);
2278         ilerp = 256 - localcolor[3];
2279         lerp = localcolor[3];
2280         for (x = startx;x < endx;x++)
2281         {
2282                 out4ub[x*4+0] = (in4ub[x*4+0] * ilerp + localcolor[0] * lerp)>>8;
2283                 out4ub[x*4+1] = (in4ub[x*4+1] * ilerp + localcolor[1] * lerp)>>8;
2284                 out4ub[x*4+2] = (in4ub[x*4+2] * ilerp + localcolor[2] * lerp)>>8;
2285                 out4ub[x*4+3] = (in4ub[x*4+3] * ilerp + localcolor[3] * lerp)>>8;
2286         }
2287 }
2288
2289
2290
2291 void DPSOFTRAST_VertexShader_Generic(void)
2292 {
2293         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);
2294         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2295         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2296         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2297                 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2298 }
2299
2300 void DPSOFTRAST_PixelShader_Generic(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2301 {
2302         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2303         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2304         unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2305         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2306         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2307         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
2308         {
2309                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_FIRST, 2, buffer_z);
2310                 DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(span, buffer_FragColorbgra8, buffer_texture_colorbgra8, 1, buffer_z);
2311                 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2312                 {
2313                         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_SECOND, 2, buffer_z);
2314                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2315                         {
2316                                 // multiply
2317                                 DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2318                         }
2319                         else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2320                         {
2321                                 // add
2322                                 DPSOFTRAST_Draw_Span_AddBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2323                         }
2324                         else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND)
2325                         {
2326                                 // alphablend
2327                                 DPSOFTRAST_Draw_Span_MixBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2328                         }
2329                 }
2330         }
2331         else
2332                 DPSOFTRAST_Draw_Span_VaryingBGRA8(span, buffer_FragColorbgra8, 1, buffer_z);
2333         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2334 }
2335
2336
2337
2338 void DPSOFTRAST_VertexShader_PostProcess(void)
2339 {
2340         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);
2341         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2342         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2343 }
2344
2345 void DPSOFTRAST_PixelShader_PostProcess(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2346 {
2347         // TODO: optimize!!  at the very least there is no reason to use texture sampling on the frame texture
2348         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2349         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2350         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2351         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2352         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_FragColorbgra8, GL20TU_FIRST, 2, buffer_z);
2353         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
2354         {
2355                 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_SECOND, 3, buffer_z);
2356                 DPSOFTRAST_Draw_Span_AddBloomBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_colorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
2357         }
2358         DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
2359         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
2360         {
2361                 // TODO: implement saturation
2362         }
2363         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
2364         {
2365                 // TODO: implement gammaramps
2366         }
2367         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2368 }
2369
2370
2371
2372 void DPSOFTRAST_VertexShader_Depth_Or_Shadow(void)
2373 {
2374         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);
2375 }
2376
2377 void DPSOFTRAST_PixelShader_Depth_Or_Shadow(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2378 {
2379         // this is never called (because colormask is off when this shader is used)
2380         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2381         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2382         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2383         memset(buffer_FragColorbgra8, 0, span->length*4);
2384         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2385 }
2386
2387
2388
2389 void DPSOFTRAST_VertexShader_FlatColor(void)
2390 {
2391         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);
2392         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);
2393 }
2394
2395 void DPSOFTRAST_PixelShader_FlatColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2396 {
2397         int x, startx = span->startx, endx = span->endx;
2398         int Color_Ambienti[4];
2399         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2400         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2401         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2402         Color_Ambienti[2] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]*256.0f);
2403         Color_Ambienti[1] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]*256.0f);
2404         Color_Ambienti[0] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]*256.0f);
2405         Color_Ambienti[3] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]        *256.0f);
2406         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2407         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2408         for (x = startx;x < endx;x++)
2409         {
2410                 buffer_FragColorbgra8[x*4+0] = (buffer_texture_colorbgra8[x*4+0] * Color_Ambienti[0])>>8;
2411                 buffer_FragColorbgra8[x*4+1] = (buffer_texture_colorbgra8[x*4+1] * Color_Ambienti[1])>>8;
2412                 buffer_FragColorbgra8[x*4+2] = (buffer_texture_colorbgra8[x*4+2] * Color_Ambienti[2])>>8;
2413                 buffer_FragColorbgra8[x*4+3] = (buffer_texture_colorbgra8[x*4+3] * Color_Ambienti[3])>>8;
2414         }
2415         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2416 }
2417
2418
2419
2420 void DPSOFTRAST_VertexShader_VertexColor(void)
2421 {
2422         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);
2423         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2424         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);
2425 }
2426
2427 void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2428 {
2429         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2430         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2431         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2432         int x, startx = span->startx, endx = span->endx;
2433         float Color_Ambient[4], Color_Diffuse[4];
2434         float data[4];
2435         float slope[4];
2436         float z;
2437         int arrayindex = DPSOFTRAST_ARRAY_COLOR;
2438         data[2] = span->data[0][arrayindex][0];
2439         data[1] = span->data[0][arrayindex][1];
2440         data[0] = span->data[0][arrayindex][2];
2441         data[3] = span->data[0][arrayindex][3];
2442         slope[2] = span->data[1][arrayindex][0];
2443         slope[1] = span->data[1][arrayindex][1];
2444         slope[0] = span->data[1][arrayindex][2];
2445         slope[3] = span->data[1][arrayindex][3];
2446         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2447         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2448         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2449         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2450         Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2451         Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2452         Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2453         Color_Diffuse[3] = 0.0f;
2454         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2455         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2456         for (x = startx;x < endx;x++)
2457         {
2458                 z = buffer_z[x];
2459                 buffer_FragColorbgra8[x*4+0] = (int)(buffer_texture_colorbgra8[x*4+0] * (Color_Ambient[0] + ((data[0] + slope[0]*x) * z) * Color_Diffuse[0]));
2460                 buffer_FragColorbgra8[x*4+1] = (int)(buffer_texture_colorbgra8[x*4+1] * (Color_Ambient[1] + ((data[1] + slope[1]*x) * z) * Color_Diffuse[1]));
2461                 buffer_FragColorbgra8[x*4+2] = (int)(buffer_texture_colorbgra8[x*4+2] * (Color_Ambient[2] + ((data[2] + slope[2]*x) * z) * Color_Diffuse[2]));
2462                 buffer_FragColorbgra8[x*4+3] = (int)(buffer_texture_colorbgra8[x*4+3] * (Color_Ambient[3] + ((data[3] + slope[3]*x) * z) * Color_Diffuse[3]));
2463         }
2464         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2465 }
2466
2467
2468
2469 void DPSOFTRAST_VertexShader_Lightmap(void)
2470 {
2471         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);
2472         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);
2473         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
2474 }
2475
2476 void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2477 {
2478         int x, startx = span->startx, endx = span->endx;
2479         float Color_Ambient[4], Color_Diffuse[4];
2480         int Color_Ambienti[4];
2481         int Color_Diffusei[4];
2482         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2483         unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2484         unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2485         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2486         unsigned int d[4];
2487         //unsigned char * RESTRICT pixelmask = span->pixelmask;
2488         //unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + span->start * 4;
2489         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2490         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2491         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2492         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2493         Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2494         Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2495         Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2496         Color_Diffuse[3] = 0.0f;
2497         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2498         Color_Ambienti[2] = (int)(Color_Ambient[0] * 65536.0f);
2499         Color_Ambienti[1] = (int)(Color_Ambient[1] * 65536.0f);
2500         Color_Ambienti[0] = (int)(Color_Ambient[2] * 65536.0f);
2501         Color_Ambienti[3] = (int)(Color_Ambient[3] * 65536.0f);
2502         Color_Diffusei[2] = (int)(Color_Diffuse[0] * 256.0f);
2503         Color_Diffusei[1] = (int)(Color_Diffuse[1] * 256.0f);
2504         Color_Diffusei[0] = (int)(Color_Diffuse[2] * 256.0f);
2505         Color_Diffusei[3] = (int)(Color_Diffuse[3] * 256.0f);
2506         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2507         DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_LIGHTMAP, 6, buffer_z);
2508         for (x = startx;x < endx;x++)
2509         {
2510                 d[0] = (buffer_texture_colorbgra8[x*4+0] * (Color_Ambienti[0] + buffer_texture_lightmapbgra8[x*4+0] * Color_Diffusei[0])) >> 16;if (d[0] > 255) d[0] = 255;
2511                 d[1] = (buffer_texture_colorbgra8[x*4+1] * (Color_Ambienti[1] + buffer_texture_lightmapbgra8[x*4+1] * Color_Diffusei[1])) >> 16;if (d[1] > 255) d[1] = 255;
2512                 d[2] = (buffer_texture_colorbgra8[x*4+2] * (Color_Ambienti[2] + buffer_texture_lightmapbgra8[x*4+2] * Color_Diffusei[2])) >> 16;if (d[2] > 255) d[2] = 255;
2513                 d[3] = (buffer_texture_colorbgra8[x*4+3] * (Color_Ambienti[3] + buffer_texture_lightmapbgra8[x*4+3] * Color_Diffusei[3])) >> 16;if (d[3] > 255) d[3] = 255;
2514                 buffer_FragColorbgra8[x*4+0] = d[0];
2515                 buffer_FragColorbgra8[x*4+1] = d[1];
2516                 buffer_FragColorbgra8[x*4+2] = d[2];
2517                 buffer_FragColorbgra8[x*4+3] = d[3];
2518         }
2519         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2520 }
2521
2522
2523
2524 void DPSOFTRAST_VertexShader_FakeLight(void)
2525 {
2526         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);
2527 }
2528
2529 void DPSOFTRAST_PixelShader_FakeLight(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2530 {
2531         // TODO: IMPLEMENT
2532         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2533         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2534         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2535         memset(buffer_FragColorbgra8, 0, span->length*4);
2536         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2537 }
2538
2539
2540
2541 void DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace(void)
2542 {
2543         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);
2544 }
2545
2546 void DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2547 {
2548         // TODO: IMPLEMENT
2549         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2550         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2551         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2552         memset(buffer_FragColorbgra8, 0, span->length*4);
2553         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2554 }
2555
2556
2557
2558 void DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace(void)
2559 {
2560         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);
2561 }
2562
2563 void DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2564 {
2565         // TODO: IMPLEMENT
2566         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2567         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2568         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2569         memset(buffer_FragColorbgra8, 0, span->length*4);
2570         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2571 }
2572
2573
2574
2575 void DPSOFTRAST_VertexShader_LightDirection(void)
2576 {
2577         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);
2578         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);
2579 }
2580
2581 void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2582 {
2583         // TODO: IMPLEMENT
2584         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2585         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2586         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2587         memset(buffer_FragColorbgra8, 0, span->length*4);
2588         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2589 }
2590
2591
2592
2593 void DPSOFTRAST_VertexShader_LightSource(void)
2594 {
2595         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);
2596 }
2597
2598 void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2599 {
2600         // TODO: IMPLEMENT
2601         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2602         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2603         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2604         memset(buffer_FragColorbgra8, 0, span->length*4);
2605         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2606 }
2607
2608
2609
2610 void DPSOFTRAST_VertexShader_Refraction(void)
2611 {
2612         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);
2613 }
2614
2615 void DPSOFTRAST_PixelShader_Refraction(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2616 {
2617         // TODO: IMPLEMENT
2618         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2619         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2620         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2621         memset(buffer_FragColorbgra8, 0, span->length*4);
2622         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2623 }
2624
2625
2626
2627 void DPSOFTRAST_VertexShader_Water(void)
2628 {
2629         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);
2630 }
2631
2632
2633 void DPSOFTRAST_PixelShader_Water(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2634 {
2635         // TODO: IMPLEMENT
2636         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2637         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2638         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2639         memset(buffer_FragColorbgra8, 0, span->length*4);
2640         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2641 }
2642
2643
2644
2645 void DPSOFTRAST_VertexShader_ShowDepth(void)
2646 {
2647         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);
2648 }
2649
2650 void DPSOFTRAST_PixelShader_ShowDepth(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2651 {
2652         // TODO: IMPLEMENT
2653         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2654         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2655         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2656         memset(buffer_FragColorbgra8, 0, span->length*4);
2657         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2658 }
2659
2660
2661
2662 void DPSOFTRAST_VertexShader_DeferredGeometry(void)
2663 {
2664         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);
2665 }
2666
2667 void DPSOFTRAST_PixelShader_DeferredGeometry(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2668 {
2669         // TODO: IMPLEMENT
2670         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2671         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2672         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2673         memset(buffer_FragColorbgra8, 0, span->length*4);
2674         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2675 }
2676
2677
2678
2679 void DPSOFTRAST_VertexShader_DeferredLightSource(void)
2680 {
2681         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);
2682 }
2683
2684 void DPSOFTRAST_PixelShader_DeferredLightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2685 {
2686         // TODO: IMPLEMENT
2687         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2688         unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2689         DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2690         memset(buffer_FragColorbgra8, 0, span->length*4);
2691         DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2692 }
2693
2694
2695
2696 typedef struct DPSOFTRAST_ShaderModeInfo_s
2697 {
2698         int lodarrayindex;
2699         void (*Vertex)(void);
2700         void (*Span)(const DPSOFTRAST_State_Draw_Span * RESTRICT span);
2701 }
2702 DPSOFTRAST_ShaderModeInfo;
2703
2704 DPSOFTRAST_ShaderModeInfo DPSOFTRAST_ShaderModeTable[SHADERMODE_COUNT] =
2705 {
2706         {2, DPSOFTRAST_VertexShader_Generic,                        DPSOFTRAST_PixelShader_Generic,                      },
2707         {2, DPSOFTRAST_VertexShader_PostProcess,                    DPSOFTRAST_PixelShader_PostProcess,                  },
2708         {2, DPSOFTRAST_VertexShader_Depth_Or_Shadow,                DPSOFTRAST_PixelShader_Depth_Or_Shadow,              },
2709         {2, DPSOFTRAST_VertexShader_FlatColor,                      DPSOFTRAST_PixelShader_FlatColor,                    },
2710         {2, DPSOFTRAST_VertexShader_VertexColor,                    DPSOFTRAST_PixelShader_VertexColor,                  },
2711         {2, DPSOFTRAST_VertexShader_Lightmap,                       DPSOFTRAST_PixelShader_Lightmap,                     },
2712         {2, DPSOFTRAST_VertexShader_FakeLight,                      DPSOFTRAST_PixelShader_FakeLight,                    },
2713         {2, DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace,   DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace, },
2714         {2, DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace, DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace},
2715         {2, DPSOFTRAST_VertexShader_LightDirection,                 DPSOFTRAST_PixelShader_LightDirection,               },
2716         {2, DPSOFTRAST_VertexShader_LightSource,                    DPSOFTRAST_PixelShader_LightSource,                  },
2717         {2, DPSOFTRAST_VertexShader_Refraction,                     DPSOFTRAST_PixelShader_Refraction,                   },
2718         {2, DPSOFTRAST_VertexShader_Water,                          DPSOFTRAST_PixelShader_Water,                        },
2719         {2, DPSOFTRAST_VertexShader_ShowDepth,                      DPSOFTRAST_PixelShader_ShowDepth,                    },
2720         {2, DPSOFTRAST_VertexShader_DeferredGeometry,               DPSOFTRAST_PixelShader_DeferredGeometry,             },
2721         {2, DPSOFTRAST_VertexShader_DeferredLightSource,            DPSOFTRAST_PixelShader_DeferredLightSource,          }
2722 };
2723
2724
2725
2726 void DPSOFTRAST_Draw_ProcessSpans(void)
2727 {
2728         int i;
2729         int x;
2730         int startx;
2731         int endx;
2732         int numspans = dpsoftrast.draw.numspans;
2733 //      unsigned int c;
2734 //      unsigned int *colorpixel;
2735         unsigned int *depthpixel;
2736         float w;
2737         float wslope;
2738         int depth;
2739         int depthslope;
2740         unsigned int d;
2741         DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
2742         unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2743         for (i = 0;i < numspans;i++, span++)
2744         {
2745                 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
2746                 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
2747                 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
2748                 {
2749                         depth = (int)(w*DPSOFTRAST_DEPTHSCALE);
2750                         depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
2751                         depthpixel = dpsoftrast.fb_depthpixels + span->start;
2752                         switch(dpsoftrast.fb_depthfunc)
2753                         {
2754                         default:
2755                         case GL_ALWAYS:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
2756                         case GL_LESS:    for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
2757                         case GL_LEQUAL:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
2758                         case GL_EQUAL:   for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
2759                         case GL_GEQUAL:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
2760                         case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
2761                         case GL_NEVER:   for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
2762                         }
2763                         //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
2764                         //for (x = 0;x < span->length;x++)
2765                         //      colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
2766                         // if there is no color buffer, skip pixel shader
2767                         startx = 0;
2768                         endx = span->length;
2769                         while (startx < endx && !pixelmask[startx])
2770                                 startx++;
2771                         while (endx > startx && !pixelmask[endx-1])
2772                                 endx--;
2773                         if (startx >= endx)
2774                                 continue; // no pixels to fill
2775                         span->pixelmask = pixelmask;
2776                         span->startx = startx;
2777                         span->endx = endx;
2778                         // run pixel shader if appropriate
2779                         // do this before running depthmask code, to allow the pixelshader
2780                         // to clear pixelmask values for alpha testing
2781                         if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
2782                                 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
2783                         if (dpsoftrast.user.depthmask)
2784                                 for (x = 0, d = depth;x < span->length;x++, d += depthslope)
2785                                         if (pixelmask[x])
2786                                                 depthpixel[x] = d;
2787                 }
2788                 else
2789                 {
2790                         // no depth testing means we're just dealing with color...
2791                         // if there is no color buffer, skip pixel shader
2792                         if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
2793                         {
2794                                 memset(pixelmask, 1, span->length);
2795                                 span->pixelmask = pixelmask;
2796                                 span->startx = 0;
2797                                 span->endx = span->length;
2798                                 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
2799                         }
2800                 }
2801         }
2802 }
2803
2804 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask)
2805 {
2806         int cullface = dpsoftrast.user.cullface;
2807         int width = dpsoftrast.fb_width;
2808         int height = dpsoftrast.fb_height;
2809         int i;
2810         int j;
2811         int k;
2812         int y;
2813         int e[3];
2814         int screenx[4];
2815         int screeny[4];
2816         int screenyless[4];
2817         int numpoints;
2818         int clipflags;
2819         int edge0p;
2820         int edge0n;
2821         int edge1p;
2822         int edge1n;
2823         int extent[6];
2824         int startx;
2825         int endx;
2826         float mip_edge0tc[2];
2827         float mip_edge1tc[2];
2828         float mip_edge0xy[2];
2829         float mip_edge1xy[2];
2830         float mip_edge0xymul;
2831         float mip_edge1xymul;
2832         float mip_edge0mip;
2833         float mip_edge1mip;
2834         float mipdensity;
2835         unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS];
2836         float startxf;
2837         float endxf;
2838         float edge0ylerp;
2839         float edge0yilerp;
2840         float edge1ylerp;
2841         float edge1yilerp;
2842         float edge0xf;
2843         float edge1xf;
2844         float spanilength;
2845         float startxlerp;
2846         float yc;
2847         float w;
2848         float frac;
2849         float ifrac;
2850         float trianglearea2;
2851         float triangleedge[2][4];
2852         float trianglenormal[4];
2853         float clipdist[4];
2854         float clipped[DPSOFTRAST_ARRAY_TOTAL][4][4];
2855         float screen[4][4];
2856         float proj[DPSOFTRAST_ARRAY_TOTAL][4][4];
2857         DPSOFTRAST_Texture *texture;
2858         DPSOFTRAST_State_Draw_Span *span;
2859         DPSOFTRAST_State_Draw_Span *oldspan;
2860         for (i = 0;i < numtriangles;i++)
2861         {
2862                 // generate the 3 edges of this triangle
2863                 // generate spans for the triangle - switch based on left split or right split classification of triangle
2864                 if (element3i)
2865                 {
2866                         e[0] = element3i[i*3+0] - firstvertex;
2867                         e[1] = element3i[i*3+1] - firstvertex;
2868                         e[2] = element3i[i*3+2] - firstvertex;
2869                 }
2870                 else if (element3s)
2871                 {
2872                         e[0] = element3s[i*3+0] - firstvertex;
2873                         e[1] = element3s[i*3+1] - firstvertex;
2874                         e[2] = element3s[i*3+2] - firstvertex;
2875                 }
2876                 else
2877                 {
2878                         e[0] = i*3+0;
2879                         e[1] = i*3+1;
2880                         e[2] = i*3+2;
2881                 }
2882                 triangleedge[0][0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+0] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+0];
2883                 triangleedge[0][1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+1] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+1];
2884                 triangleedge[0][2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2];
2885                 triangleedge[1][0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+0] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+0];
2886                 triangleedge[1][1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+1] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+1];
2887                 triangleedge[1][2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2];
2888                 trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
2889                 trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
2890                 trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
2891                 trianglearea2 = trianglenormal[0] * trianglenormal[0] + trianglenormal[1] * trianglenormal[1] + trianglenormal[2] * trianglenormal[2];
2892                 // skip degenerate triangles, nothing good can come from them...
2893                 if (trianglearea2 == 0.0f)
2894                         continue;
2895                 // apply current cullface mode (this culls many triangles)
2896                 switch(cullface)
2897                 {
2898                 case GL_BACK:
2899                         if (trianglenormal[2] < 0)
2900                                 continue;
2901                         break;
2902                 case GL_FRONT:
2903                         if (trianglenormal[2] > 0)
2904                                 continue;
2905                         break;
2906                 }
2907                 // calculate distance from nearplane
2908                 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + 1.0f;
2909                 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + 1.0f;
2910                 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + 1.0f;
2911                 clipflags = 0;
2912                 if (clipdist[0] < 0.0f)
2913                         clipflags |= 1;
2914                 if (clipdist[1] < 0.0f)
2915                         clipflags |= 2;
2916                 if (clipdist[2] < 0.0f)
2917                         clipflags |= 4;
2918                 // clip triangle if necessary
2919                 switch(clipflags)
2920                 {
2921                 case 0: /*000*/
2922                         // triangle is entirely in front of nearplane
2923
2924                         // macros for clipping vertices
2925 #define CLIPPEDVERTEXLERP(k,p1,p2) \
2926                         frac = clipdist[p1] / (clipdist[p1] - clipdist[p2]);\
2927                         ifrac = 1.0f - frac;\
2928                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2929                         {\
2930                                 if (arraymask[j])\
2931                                 {\
2932                                         clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+0]*frac;\
2933                                         clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+1]*frac;\
2934                                         clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+2]*frac;\
2935                                         clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+3]*frac;\
2936                                 }\
2937                         }\
2938                         DPSOFTRAST_Draw_ProjectVertices(screen[k], clipped[DPSOFTRAST_ARRAY_POSITION][k], 1)
2939 #define CLIPPEDVERTEXCOPY(k,p1) \
2940                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2941                         {\
2942                                 if (arraymask[j])\
2943                                 {\
2944                                         clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0];\
2945                                         clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1];\
2946                                         clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2];\
2947                                         clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3];\
2948                                 }\
2949                         }\
2950                         screen[k][0] = dpsoftrast.draw.screencoord4f[e[p1]*4+0];\
2951                         screen[k][1] = dpsoftrast.draw.screencoord4f[e[p1]*4+1];\
2952                         screen[k][2] = dpsoftrast.draw.screencoord4f[e[p1]*4+2];\
2953                         screen[k][3] = dpsoftrast.draw.screencoord4f[e[p1]*4+3];
2954
2955                         CLIPPEDVERTEXCOPY(0,0);
2956                         CLIPPEDVERTEXCOPY(1,1);
2957                         CLIPPEDVERTEXCOPY(2,2);
2958                         numpoints = 3;
2959                         break;
2960                 case 1: /*100*/
2961                         CLIPPEDVERTEXLERP(0,0,1);
2962                         CLIPPEDVERTEXCOPY(1,1);
2963                         CLIPPEDVERTEXCOPY(2,2);
2964                         CLIPPEDVERTEXLERP(3,2,0);
2965                         numpoints = 4;
2966                         break;
2967                 case 2: /*010*/
2968                         CLIPPEDVERTEXCOPY(0,0);
2969                         CLIPPEDVERTEXLERP(1,0,1);
2970                         CLIPPEDVERTEXLERP(2,1,2);
2971                         CLIPPEDVERTEXCOPY(3,2);
2972                         numpoints = 4;
2973                         break;
2974                 case 3: /*110*/
2975                         CLIPPEDVERTEXLERP(0,1,2);
2976                         CLIPPEDVERTEXCOPY(1,2);
2977                         CLIPPEDVERTEXLERP(2,2,0);
2978                         numpoints = 3;
2979                         break;
2980                 case 4: /*001*/
2981                         CLIPPEDVERTEXCOPY(0,0);
2982                         CLIPPEDVERTEXCOPY(1,1);
2983                         CLIPPEDVERTEXLERP(2,1,2);
2984                         CLIPPEDVERTEXLERP(3,2,0);
2985                         numpoints = 4;
2986                         break;
2987                 case 5: /*101*/
2988                         CLIPPEDVERTEXLERP(0,0,1);
2989                         CLIPPEDVERTEXCOPY(1,1);
2990                         CLIPPEDVERTEXLERP(2,1,2);
2991                         numpoints = 3;
2992                         break;
2993                 case 6: /*011*/
2994                         CLIPPEDVERTEXCOPY(0,0);
2995                         CLIPPEDVERTEXLERP(1,0,1);
2996                         CLIPPEDVERTEXLERP(2,2,0);
2997                         numpoints = 3;
2998                         break;
2999                 case 7: /*111*/
3000                         // triangle is entirely behind nearplane
3001                         continue;
3002                 }
3003                 // calculate integer y coords for triangle points
3004                 screenx[0] = (int)(screen[0][0]);
3005                 screeny[0] = (int)(screen[0][1]);
3006                 screenx[1] = (int)(screen[1][0]);
3007                 screeny[1] = (int)(screen[1][1]);
3008                 screenx[2] = (int)(screen[2][0]);
3009                 screeny[2] = (int)(screen[2][1]);
3010                 screenx[3] = (int)(screen[3][0]);
3011                 screeny[3] = (int)(screen[3][1]);
3012                 // figure out the extents (bounding box) of the triangle
3013                 extent[0] = screenx[0];
3014                 extent[1] = screeny[0];
3015                 extent[2] = screenx[0];
3016                 extent[3] = screeny[0];
3017                 for (j = 1;j < numpoints;j++)
3018                 {
3019                         if (extent[0] > screenx[j]) extent[0] = screenx[j];
3020                         if (extent[1] > screeny[j]) extent[1] = screeny[j];
3021                         if (extent[2] < screenx[j]) extent[2] = screenx[j];
3022                         if (extent[3] < screeny[j]) extent[3] = screeny[j];
3023                 }
3024                 //extent[0]--;
3025                 //extent[1]--;
3026                 extent[2]++;
3027                 extent[3]++;
3028                 if (extent[0] < 0)
3029                         extent[0] = 0;
3030                 if (extent[1] < 0)
3031                         extent[1] = 0;
3032                 if (extent[2] > width)
3033                         extent[2] = width;
3034                 if (extent[3] > height)
3035                         extent[3] = height;
3036                 // skip offscreen triangles
3037                 if (extent[2] <= extent[0] || extent[3] <= extent[1])
3038                         continue;
3039                 // okay, this triangle is going to produce spans, we'd better project
3040                 // the interpolants now (this is what gives perspective texturing),
3041                 // this consists of simply multiplying all arrays by the W coord
3042                 // (which is basically 1/Z), which will be undone per-pixel
3043                 // (multiplying by Z again) to get the perspective-correct array
3044                 // values
3045                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3046                 {
3047                         if (arraymask[j])
3048                         {
3049                                 for (k = 0;k < numpoints;k++)
3050                                 {
3051                                         w = screen[k][3];
3052                                         proj[j][k][0] = clipped[j][k][0] * w;
3053                                         proj[j][k][1] = clipped[j][k][1] * w;
3054                                         proj[j][k][2] = clipped[j][k][2] * w;
3055                                         proj[j][k][3] = clipped[j][k][3] * w;
3056                                 }
3057                         }
3058                 }
3059                 // adjust texture LOD by texture density, in the simplest way possible...
3060                 mip_edge0xy[0] = screen[0][0] - screen[1][0];
3061                 mip_edge0xy[1] = screen[0][1] - screen[1][1];
3062                 mip_edge1xy[0] = screen[2][0] - screen[1][0];
3063                 mip_edge1xy[1] = screen[2][1] - screen[1][1];
3064                 mip_edge0xymul = 1.0f / (mip_edge0xy[0]*mip_edge0xy[0]+mip_edge0xy[1]*mip_edge0xy[1]);
3065                 mip_edge1xymul = 1.0f / (mip_edge1xy[0]*mip_edge1xy[0]+mip_edge1xy[1]*mip_edge1xy[1]);
3066                 for (j = 0;j < DPSOFTRAST_MAXTEXTUREUNITS;j++)
3067                 {
3068                         texture = dpsoftrast.texbound[j];
3069                         if (texture)
3070                         {
3071                                 if (texture->filter <= DPSOFTRAST_TEXTURE_FILTER_LINEAR)
3072                                 {
3073                                         mip[j] = 0;
3074                                         continue;
3075                                 }
3076                                 k = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].lodarrayindex;
3077                                 mip_edge0tc[0] = (clipped[k][0][0] - clipped[k][1][0]) * texture->mipmap[0][2];
3078                                 mip_edge0tc[1] = (clipped[k][0][1] - clipped[k][1][1]) * texture->mipmap[0][3];
3079                                 mip_edge1tc[0] = (clipped[k][2][0] - clipped[k][1][0]) * texture->mipmap[0][2];
3080                                 mip_edge1tc[1] = (clipped[k][2][1] - clipped[k][1][1]) * texture->mipmap[0][3];
3081                                 mip_edge0mip = (mip_edge0tc[0]*mip_edge0tc[0]+mip_edge0tc[1]*mip_edge0tc[1]) * mip_edge0xymul;
3082                                 mip_edge1mip = (mip_edge1tc[0]*mip_edge1tc[0]+mip_edge1tc[1]*mip_edge1tc[1]) * mip_edge1xymul;
3083                                 // this will be multiplied in the texturing routine by the texture resolution
3084                                 mipdensity = mip_edge0mip < mip_edge1mip ? mip_edge0mip : mip_edge1mip;
3085                                 y = (int)(log(mipdensity)/log(2.0f));
3086                                 if (y < 0)
3087                                         y = 0;
3088                                 if (y > texture->mipmaps - 1)
3089                                         y = texture->mipmaps - 1;
3090                                 mip[j] = y;
3091                         }
3092                 }
3093                 // iterate potential spans
3094                 // TODO: optimize?  if we figured out the edge order beforehand, this
3095                 //       could do loops over the edges in the proper order rather than
3096                 //       selecting them for each span
3097                 // TODO: optimize?  the edges could have data slopes calculated
3098                 // TODO: optimize?  the data slopes could be calculated as a plane
3099                 //       (2D slopes) to avoid any interpolation along edges at all
3100                 for (y = extent[1];y < extent[3];y++)
3101                 {
3102                         // get center of pixel y
3103                         yc = y;
3104                         // do the compares all at once
3105                         screenyless[0] = y <= screeny[0];
3106                         screenyless[1] = y <= screeny[1];
3107                         screenyless[2] = y <= screeny[2];
3108                         screenyless[3] = y <= screeny[3];
3109                         if (numpoints == 4)
3110                         {
3111                                 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4 + screenyless[3] * 8)
3112                                 {
3113                                 case  0: /*0000*/ continue;
3114                                 case  1: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 0;edge1n = 1;break;
3115                                 case  2: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
3116                                 case  3: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 2;break;
3117                                 case  4: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break;
3118                                 case  5: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break; // concave - nonsense
3119                                 case  6: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 3;break;
3120                                 case  7: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 3;break;
3121                                 case  8: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 3;edge1n = 0;break;
3122                                 case  9: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 1;break;
3123                                 case 10: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
3124                                 case 11: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break;
3125                                 case 12: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 0;break;
3126                                 case 13: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
3127                                 case 14: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 0;break;
3128                                 case 15: /*1111*/ continue;
3129                                 }
3130                         }
3131                         else
3132                         {
3133                                 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4)
3134                                 {
3135                                 case 0: /*000*/ continue;
3136                                 case 1: /*100*/ edge0p = 2;edge0n = 0;edge1p = 0;edge1n = 1;break;
3137                                 case 2: /*010*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
3138                                 case 3: /*110*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 2;break;
3139                                 case 4: /*001*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 0;break;
3140                                 case 5: /*101*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
3141                                 case 6: /*011*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 0;break;
3142                                 case 7: /*111*/ continue;
3143                                 }
3144                         }
3145 #if 0
3146                 {
3147                         int foundedges = 0;
3148                         int cedge0p = 0;
3149                         int cedge0n = 0;
3150                         int cedge1p = 0;
3151                         int cedge1n = 0;
3152                         for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
3153                         {
3154                                 if (screenyless[k] && !screenyless[j])
3155                                 {
3156                                         cedge1p = k;
3157                                         cedge1n = j;
3158                                         foundedges |= 1;
3159                                 }
3160                                 else if (screenyless[j] && !screenyless[k])
3161                                 {
3162                                         cedge0p = k;
3163                                         cedge0n = j;
3164                                         foundedges |= 2;
3165                                 }
3166                         }
3167                         if (foundedges != 3)
3168                                 continue;
3169                         if (cedge0p != edge0p || cedge0n != edge0n || cedge1p != edge1p || cedge1n != edge1n)
3170                         {
3171                                 if (numpoints == 4)
3172                                         printf("case %i%i%i%i is broken %i %i %i %i != %i %i %i %i\n", screenyless[0], screenyless[1], screenyless[2], screenyless[3], cedge0p, cedge0n, cedge1p, cedge1n, edge0p, edge0n, edge1p, edge1n);
3173                                 else
3174                                         printf("case %i%i%i is broken %i %i %i %i != %i %i %i %i\n", screenyless[0], screenyless[1], screenyless[2], cedge0p, cedge0n, cedge1p, cedge1n, edge0p, edge0n, edge1p, edge1n);
3175                         }
3176                 }
3177 #endif
3178                         edge0ylerp = (yc - screen[edge0p][1]) / (screen[edge0n][1] - screen[edge0p][1]);
3179                         edge1ylerp = (yc - screen[edge1p][1]) / (screen[edge1n][1] - screen[edge1p][1]);
3180                         if (edge0ylerp < 0 || edge0ylerp > 1 || edge1ylerp < 0 || edge1ylerp > 1)
3181                                 continue;
3182                         edge0yilerp = 1.0f - edge0ylerp;
3183                         edge1yilerp = 1.0f - edge1ylerp;
3184                         edge0xf = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3185                         edge1xf = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3186                         if (edge0xf < edge1xf)
3187                         {
3188                                 startxf = edge0xf;
3189                                 endxf = edge1xf;
3190                         }
3191                         else
3192                         {
3193                                 startxf = edge1xf;
3194                                 endxf = edge0xf;
3195                         }
3196                         startx = (int)ceil(startxf);
3197                         endx = (int)ceil(endxf);
3198                         if (startx < 0)
3199                                 startx = 0;
3200                         if (endx > width)
3201                                 endx = width;
3202                         if (startx >= endx)
3203                                 continue;
3204                         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); }
3205                         spanilength = 1.0f / (endxf - startxf);
3206                         startxlerp = startx - startxf;
3207                         span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3208                         memcpy(span->mip, mip, sizeof(span->mip));
3209                         span->start = y * width + startx;
3210                         span->length = endx - startx;
3211                         j = DPSOFTRAST_ARRAY_TOTAL;
3212                         if (edge0xf < edge1xf)
3213                         {
3214                                 span->data[0][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3215                                 span->data[0][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
3216                                 span->data[0][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
3217                                 span->data[0][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
3218                                 span->data[1][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3219                                 span->data[1][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
3220                                 span->data[1][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
3221                                 span->data[1][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
3222                                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3223                                 {
3224                                         if (arraymask[j])
3225                                         {
3226                                                 span->data[0][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
3227                                                 span->data[0][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
3228                                                 span->data[0][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
3229                                                 span->data[0][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
3230                                                 span->data[1][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
3231                                                 span->data[1][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
3232                                                 span->data[1][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
3233                                                 span->data[1][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
3234                                         }
3235                                 }
3236                         }
3237                         else
3238                         {
3239                                 span->data[0][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3240                                 span->data[0][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
3241                                 span->data[0][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
3242                                 span->data[0][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
3243                                 span->data[1][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3244                                 span->data[1][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
3245                                 span->data[1][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
3246                                 span->data[1][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
3247                                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3248                                 {
3249                                         if (arraymask[j])
3250                                         {
3251                                                 span->data[0][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
3252                                                 span->data[0][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
3253                                                 span->data[0][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
3254                                                 span->data[0][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
3255                                                 span->data[1][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
3256                                                 span->data[1][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
3257                                                 span->data[1][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
3258                                                 span->data[1][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
3259                                         }
3260                                 }
3261                         }
3262                         // change data[1][n][] to be a data slope
3263                         j = DPSOFTRAST_ARRAY_TOTAL;
3264                         span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
3265                         span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
3266                         span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
3267                         span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
3268                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3269                         {
3270                                 if (arraymask[j])
3271                                 {
3272                                         span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
3273                                         span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
3274                                         span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
3275                                         span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
3276                                 }
3277                         }
3278                         // adjust the data[0][n][] to be correct for the pixel centers
3279                         // this also handles horizontal clipping where a major part of the
3280                         // span may be off the left side of the screen
3281                         j = DPSOFTRAST_ARRAY_TOTAL;
3282                         span->data[0][j][0] += span->data[1][j][0] * startxlerp;
3283                         span->data[0][j][1] += span->data[1][j][1] * startxlerp;
3284                         span->data[0][j][2] += span->data[1][j][2] * startxlerp;
3285                         span->data[0][j][3] += span->data[1][j][3] * startxlerp;
3286                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3287                         {
3288                                 if (arraymask[j])
3289                                 {
3290                                         span->data[0][j][0] += span->data[1][j][0] * startxlerp;
3291                                         span->data[0][j][1] += span->data[1][j][1] * startxlerp;
3292                                         span->data[0][j][2] += span->data[1][j][2] * startxlerp;
3293                                         span->data[0][j][3] += span->data[1][j][3] * startxlerp;
3294                                 }
3295                         }
3296                         // to keep the shader routines from needing more than a small
3297                         // buffer for pixel intermediate data, we split long spans...
3298                         while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
3299                         {
3300                                 span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
3301                                 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3302                                 {
3303                                         DPSOFTRAST_Draw_ProcessSpans();
3304                                         dpsoftrast.draw.numspans = 0;
3305                                 }
3306                                 oldspan = span;
3307                                 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3308                                 *span = *oldspan;
3309                                 startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
3310                                 span->start = y * width + startx;
3311                                 span->length = endx - startx;
3312                                 j = DPSOFTRAST_ARRAY_TOTAL;
3313                                 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3314                                 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3315                                 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3316                                 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3317                                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3318                                 {
3319                                         if (arraymask[j])
3320                                         {
3321                                                 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3322                                                 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3323                                                 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3324                                                 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3325                                         }
3326                                 }
3327                         }
3328                         // after all that, we have a span suitable for the pixel shader...
3329                         if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3330                         {
3331                                 DPSOFTRAST_Draw_ProcessSpans();
3332                                 dpsoftrast.draw.numspans = 0;
3333                         }
3334                 }
3335                 // draw outlines over triangle for debugging
3336         //      for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
3337         //              DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
3338         }
3339         if (dpsoftrast.draw.numspans)
3340         {
3341                 DPSOFTRAST_Draw_ProcessSpans();
3342                 dpsoftrast.draw.numspans = 0;
3343         }
3344 }
3345
3346 void DPSOFTRAST_Draw_DebugPoints(void)
3347 {
3348         int i;
3349         int x;
3350         int y;
3351         int numvertices = dpsoftrast.draw.numvertices;
3352         int w = dpsoftrast.fb_width;
3353         int bounds[4];
3354         unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
3355         const float *c4f;
3356         bounds[0] = dpsoftrast.fb_viewportscissor[0];
3357         bounds[1] = dpsoftrast.fb_viewportscissor[1];
3358         bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
3359         bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
3360         for (i = 0;i < numvertices;i++)
3361         {
3362                 // check nearclip
3363                 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
3364                 //      continue;
3365                 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
3366                 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
3367                 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
3368                 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
3369                 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
3370                 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
3371                 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
3372                         continue;
3373                 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
3374                 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
3375         }
3376 }
3377
3378 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
3379 {
3380         unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL];
3381         arraymask[0] = true;
3382         arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode)
3383         arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL;
3384         arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL;
3385         arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL;
3386         arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL;
3387         arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL;
3388         arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL;
3389         arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL;
3390         arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL;
3391         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
3392         DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true);
3393         DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Vertex();
3394         DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
3395         DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numvertices, numtriangles, element3i, element3s, arraymask);
3396 }
3397
3398 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
3399 {
3400         union
3401         {
3402                 int i;
3403                 unsigned char b[4];
3404         }
3405         u;
3406         u.i = 1;
3407         memset(&dpsoftrast, 0, sizeof(dpsoftrast));
3408         dpsoftrast.bigendian = u.b[3];
3409         dpsoftrast.fb_width = width;
3410         dpsoftrast.fb_height = height;
3411         dpsoftrast.fb_depthpixels = depthpixels;
3412         dpsoftrast.fb_colorpixels[0] = colorpixels;
3413         dpsoftrast.fb_colorpixels[1] = NULL;
3414         dpsoftrast.fb_colorpixels[1] = NULL;
3415         dpsoftrast.fb_colorpixels[1] = NULL;
3416         dpsoftrast.texture_firstfree = 1;
3417         dpsoftrast.texture_end = 1;
3418         dpsoftrast.texture_max = 0;
3419         dpsoftrast.user.colormask[0] = 1;
3420         dpsoftrast.user.colormask[1] = 1;
3421         dpsoftrast.user.colormask[2] = 1;
3422         dpsoftrast.user.colormask[3] = 1;
3423         dpsoftrast.user.blendfunc[0] = GL_ONE;
3424         dpsoftrast.user.blendfunc[1] = GL_ZERO;
3425         dpsoftrast.user.depthmask = true;
3426         dpsoftrast.user.depthtest = true;
3427         dpsoftrast.user.depthfunc = GL_LEQUAL;
3428         dpsoftrast.user.scissortest = false;
3429         dpsoftrast.user.cullface = GL_BACK;
3430         dpsoftrast.user.alphatest = false;
3431         dpsoftrast.user.alphafunc = GL_GREATER;
3432         dpsoftrast.user.alphavalue = 0.5f;
3433         dpsoftrast.user.scissor[0] = 0;
3434         dpsoftrast.user.scissor[1] = 0;
3435         dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
3436         dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
3437         dpsoftrast.user.viewport[0] = 0;
3438         dpsoftrast.user.viewport[1] = 0;
3439         dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
3440         dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
3441         dpsoftrast.user.depthrange[0] = 0;
3442         dpsoftrast.user.depthrange[1] = 1;
3443         dpsoftrast.user.polygonoffset[0] = 0;
3444         dpsoftrast.user.polygonoffset[1] = 0;
3445         dpsoftrast.user.color[0] = 1;
3446         dpsoftrast.user.color[1] = 1;
3447         dpsoftrast.user.color[2] = 1;
3448         dpsoftrast.user.color[3] = 1;
3449         dpsoftrast.validate = -1;
3450         DPSOFTRAST_Validate(-1);
3451         dpsoftrast.validate = 0;
3452 }
3453
3454 void DPSOFTRAST_Shutdown(void)
3455 {
3456         int i;
3457         for (i = 0;i < dpsoftrast.texture_end;i++)
3458                 if (dpsoftrast.texture[i].bytes)
3459                         free(dpsoftrast.texture[i].bytes);
3460         if (dpsoftrast.texture)
3461                 free(dpsoftrast.texture);
3462         memset(&dpsoftrast, 0, sizeof(dpsoftrast));
3463 }