]> git.xonotic.org Git - xonotic/darkplaces.git/blob - dpsoftrast.c
draw things as black if no shader code exists yet
[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 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
226 #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))
227 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
228 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
229
230 void DPSOFTRAST_RecalcFB(void)
231 {
232         // calculate framebuffer scissor, viewport, viewport clipped by scissor,
233         // and viewport projection values
234         int x1, x2, x3, x4, x5, x6;
235         int y1, y2, y3, y4, y5, y6;
236         x1 = dpsoftrast.user.scissor[0];
237         x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
238         x3 = dpsoftrast.user.viewport[0];
239         x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
240         y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
241         y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
242         y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
243         y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
244         if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
245         if (x1 < 0) x1 = 0;
246         if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
247         if (x3 < 0) x1 = 0;
248         if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
249         if (y1 < 0) y1 = 0;
250         if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
251         if (y3 < 0) y1 = 0;
252         if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
253         x5 = x1;if (x5 < x3) x5 = x3;
254         x6 = x2;if (x6 > x4) x4 = x4;
255         y5 = y1;if (y5 < y3) y5 = y3;
256         y6 = y2;if (y6 > y4) y6 = y4;
257         dpsoftrast.fb_clearscissor[0] = x1;
258         dpsoftrast.fb_clearscissor[1] = y1;
259         dpsoftrast.fb_clearscissor[2] = x2 - x1;
260         dpsoftrast.fb_clearscissor[3] = y2 - y1;
261         dpsoftrast.fb_viewport[0] = x3;
262         dpsoftrast.fb_viewport[1] = y3;
263         dpsoftrast.fb_viewport[2] = x4 - x3;
264         dpsoftrast.fb_viewport[3] = y4 - y3;
265         dpsoftrast.fb_viewportscissor[0] = x5;
266         dpsoftrast.fb_viewportscissor[1] = y5;
267         dpsoftrast.fb_viewportscissor[2] = x6 - x5;
268         dpsoftrast.fb_viewportscissor[3] = y6 - y5;
269         dpsoftrast.fb_viewportcenter[0] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
270         dpsoftrast.fb_viewportcenter[1] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
271         dpsoftrast.fb_viewportscale[0] = 0.5f * dpsoftrast.user.viewport[2];
272         dpsoftrast.fb_viewportscale[1] = -0.5f * dpsoftrast.user.viewport[3];
273 }
274
275 void DPSOFTRAST_RecalcDepthFunc(void)
276 {
277         dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
278 }
279
280 int blendmodetable[][4] = 
281 {
282         {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
283         {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
284         {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
285         {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
286         {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
287         {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
288         {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
289         {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
290         {DPSOFTRAST_BLENDMODE_PSEUDOALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, false},
291         {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
292 };
293
294 void DPSOFTRAST_RecalcBlendFunc(void)
295 {
296         int i;
297         dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE;
298         for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++)
299         {
300                 if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3])
301                 {
302                         dpsoftrast.fb_blendmode = blendmodetable[i][0];
303                         break;
304                 }
305         }
306 }
307
308 #define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0)
309
310 void DPSOFTRAST_Validate(int mask)
311 {
312         mask &= dpsoftrast.validate;
313         if (!mask)
314                 return;
315         if (mask & DPSOFTRAST_VALIDATE_FB)
316         {
317                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB;
318                 DPSOFTRAST_RecalcFB();
319         }
320         if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
321         {
322                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
323                 DPSOFTRAST_RecalcDepthFunc();
324         }
325         if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
326         {
327                 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
328                 DPSOFTRAST_RecalcBlendFunc();
329         }
330 }
331
332 DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
333 {
334         if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
335                 return &dpsoftrast.texture[index];
336         return NULL;
337 }
338
339 int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
340 {
341         int w;
342         int h;
343         int d;
344         int size;
345         int s;
346         int texnum;
347         int mipmaps;
348         int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
349         int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
350         DPSOFTRAST_Texture *texture;
351         if (width*height*depth < 1)
352         {
353                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
354                 return 0;
355         }
356         if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
357         {
358                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
359                 return 0;
360         }
361         switch(texformat)
362         {
363         case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
364         case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
365         case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
366                 break;
367         case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
368                 if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
369                 {
370                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
371                         return 0;
372                 }
373                 if (depth != 1)
374                 {
375                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
376                         return 0;
377                 }
378                 if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
379                 {
380                         dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
381                         return 0;
382                 }
383                 break;
384         }
385         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
386         {
387                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
388                 return 0;
389         }
390         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
391         {
392                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
393                 return 0;
394         }
395         if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
396         {
397                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
398                 return 0;
399         }
400         if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
401         {
402                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
403                 return 0;
404         }
405         if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
406         {
407                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
408                 return 0;
409         }
410         // find first empty slot in texture array
411         for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
412                 if (!dpsoftrast.texture[texnum].bytes)
413                         break;
414         dpsoftrast.texture_firstfree = texnum + 1;
415         if (dpsoftrast.texture_max <= texnum)
416         {
417                 // expand texture array as needed
418                 if (dpsoftrast.texture_max < 1024)
419                         dpsoftrast.texture_max = 1024;
420                 else
421                         dpsoftrast.texture_max *= 2;
422                 dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
423         }
424         if (dpsoftrast.texture_end <= texnum)
425                 dpsoftrast.texture_end = texnum + 1;
426         texture = &dpsoftrast.texture[texnum];
427         memset(texture, 0, sizeof(*texture));
428         texture->flags = flags;
429         texture->width = width;
430         texture->height = height;
431         texture->depth = depth;
432         texture->sides = sides;
433         w = width;
434         h = height;
435         d = depth;
436         size = 0;
437         mipmaps = 0;
438         w = width;
439         h = height;
440         d = depth;
441         for (;;)
442         {
443                 s = w * h * d * sides * 4;
444                 texture->mipmap[mipmaps][0] = size;
445                 texture->mipmap[mipmaps][1] = s;
446                 texture->mipmap[mipmaps][2] = w;
447                 texture->mipmap[mipmaps][3] = h;
448                 texture->mipmap[mipmaps][4] = d;
449                 size += s;
450                 mipmaps++;
451                 if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
452                         break;
453                 if (w > 1) w >>= 1;
454                 if (h > 1) h >>= 1;
455                 if (d > 1) d >>= 1;
456         }
457         texture->mipmaps = mipmaps;
458         texture->size = size;
459
460         // allocate the pixels now
461         texture->bytes = (unsigned char *)calloc(1, size);
462
463         return texnum;
464 }
465 void DPSOFTRAST_Texture_Free(int index)
466 {
467         DPSOFTRAST_Texture *texture;
468         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
469         if (texture->bytes)
470                 free(texture->bytes);
471         texture->bytes = NULL;
472         memset(texture, 0, sizeof(*texture));
473         // adjust the free range and used range
474         if (dpsoftrast.texture_firstfree > index)
475                 dpsoftrast.texture_firstfree = index;
476         while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
477                 dpsoftrast.texture_end--;
478 }
479 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
480 {
481         int i, x, y, z, w, layer0, layer1, row0, row1;
482         unsigned char *o, *i0, *i1, *i2, *i3;
483         DPSOFTRAST_Texture *texture;
484         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
485         if (texture->mipmaps <= 1)
486                 return;
487         for (i = 1;i < texture->mipmaps;i++)
488         {
489                 for (z = 0;z < texture->mipmap[i][4];z++)
490                 {
491                         layer0 = z*2;
492                         layer1 = z*2+1;
493                         if (layer1 >= texture->mipmap[i-1][4])
494                                 layer1 = texture->mipmap[i-1][4]-1;
495                         for (y = 0;y < texture->mipmap[i][3];y++)
496                         {
497                                 row0 = y*2;
498                                 row1 = y*2+1;
499                                 if (row1 >= texture->mipmap[i-1][3])
500                                         row1 = texture->mipmap[i-1][3]-1;
501                                 o =  texture->bytes + texture->mipmap[i  ][0] + 4*((texture->mipmap[i  ][3] * z      + y   ) * texture->mipmap[i  ][2]);
502                                 i0 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row0) * texture->mipmap[i-1][2]);
503                                 i1 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row1) * texture->mipmap[i-1][2]);
504                                 i2 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row0) * texture->mipmap[i-1][2]);
505                                 i3 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row1) * texture->mipmap[i-1][2]);
506                                 w = texture->mipmap[i][2];
507                                 if (layer1 > layer0)
508                                 {
509                                         if (texture->mipmap[i-1][2] > 1)
510                                         {
511                                                 // average 3D texture
512                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8, i2 += 8, i3 += 8)
513                                                 {
514                                                         o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
515                                                         o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
516                                                         o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
517                                                         o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
518                                                 }
519                                         }
520                                         else
521                                         {
522                                                 // average 3D mipmap with parent width == 1
523                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
524                                                 {
525                                                         o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
526                                                         o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
527                                                         o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
528                                                         o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
529                                                 }
530                                         }
531                                 }
532                                 else
533                                 {
534                                         if (texture->mipmap[i-1][2] > 1)
535                                         {
536                                                 // average 2D texture (common case)
537                                                 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
538                                                 {
539                                                         o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
540                                                         o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
541                                                         o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
542                                                         o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
543                                                 }
544                                         }
545                                         else
546                                         {
547                                                 // 2D texture with parent width == 1
548                                                 o[0] = (i0[0] + i1[0] + 1) >> 1;
549                                                 o[1] = (i0[1] + i1[1] + 1) >> 1;
550                                                 o[2] = (i0[2] + i1[2] + 1) >> 1;
551                                                 o[3] = (i0[3] + i1[3] + 1) >> 1;
552                                         }
553                                 }
554                         }
555                 }
556         }
557 }
558 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
559 {
560         DPSOFTRAST_Texture *texture;
561         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
562
563         // FIXME IMPLEMENT
564
565         dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
566 }
567 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
568 {
569         DPSOFTRAST_Texture *texture;
570         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
571
572         memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
573         DPSOFTRAST_Texture_CalculateMipmaps(index);
574 }
575 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
576 {
577         DPSOFTRAST_Texture *texture;
578         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
579         return texture->width;
580 }
581 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
582 {
583         DPSOFTRAST_Texture *texture;
584         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
585         return texture->height;
586 }
587 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
588 {
589         DPSOFTRAST_Texture *texture;
590         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
591         return texture->depth;
592 }
593 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
594 {
595         DPSOFTRAST_Texture *texture;
596         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
597         return texture->bytes;
598 }
599 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
600 {
601         DPSOFTRAST_Texture *texture;
602         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
603         if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
604         {
605                 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
606                 return;
607         }
608         texture->filter = filter;
609 }
610
611 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
612 {
613         dpsoftrast.fb_width = width;
614         dpsoftrast.fb_height = height;
615         dpsoftrast.fb_depthpixels = depthpixels;
616         dpsoftrast.fb_colorpixels[0] = colorpixels0;
617         dpsoftrast.fb_colorpixels[1] = colorpixels1;
618         dpsoftrast.fb_colorpixels[2] = colorpixels2;
619         dpsoftrast.fb_colorpixels[3] = colorpixels3;
620 }
621 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
622 {
623         dpsoftrast.user.viewport[0] = x;
624         dpsoftrast.user.viewport[1] = y;
625         dpsoftrast.user.viewport[2] = width;
626         dpsoftrast.user.viewport[3] = height;
627         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
628 }
629 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
630 {
631         int i, x1, y1, x2, y2, w, h, x, y;
632         unsigned int *p;
633         unsigned int c;
634         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
635         x1 = dpsoftrast.fb_clearscissor[0];
636         y1 = dpsoftrast.fb_clearscissor[1];
637         x2 = dpsoftrast.fb_clearscissor[2];
638         y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
639         w = x2 - x1;
640         h = y2 - y1;
641         if (w < 1 || h < 1)
642                 return;
643         // FIXME: honor dpsoftrast.fb_colormask?
644         c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
645         for (i = 0;i < 4;i++)
646         {
647                 if (!dpsoftrast.fb_colorpixels[i])
648                         continue;
649                 for (y = y1;y < y2;y++)
650                 {
651                         p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
652                         for (x = x1;x < x2;x++)
653                                 p[x] = c;
654                 }
655         }
656 }
657 void DPSOFTRAST_ClearDepth(float d)
658 {
659         int x1, y1, x2, y2, w, h, x, y;
660         unsigned int *p;
661         unsigned int c;
662         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
663         x1 = dpsoftrast.fb_clearscissor[0];
664         y1 = dpsoftrast.fb_clearscissor[1];
665         x2 = dpsoftrast.fb_clearscissor[2];
666         y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
667         w = x2 - x1;
668         h = y2 - y1;
669         if (w < 1 || h < 1)
670                 return;
671         c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
672         for (y = y1;y < y2;y++)
673         {
674                 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
675                 for (x = x1;x < x2;x++)
676                         p[x] = c;
677         }
678 }
679 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
680 {
681         dpsoftrast.user.colormask[0] = r != 0;
682         dpsoftrast.user.colormask[1] = g != 0;
683         dpsoftrast.user.colormask[2] = b != 0;
684         dpsoftrast.user.colormask[3] = a != 0;
685         dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
686 }
687 void DPSOFTRAST_DepthTest(int enable)
688 {
689         dpsoftrast.user.depthtest = enable;
690         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
691 }
692 void DPSOFTRAST_ScissorTest(int enable)
693 {
694         dpsoftrast.user.scissortest = enable;
695         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
696 }
697 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
698 {
699         dpsoftrast.user.scissor[0] = x;
700         dpsoftrast.user.scissor[1] = y;
701         dpsoftrast.user.scissor[2] = width;
702         dpsoftrast.user.scissor[3] = height;
703         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
704 }
705
706 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
707 {
708         // FIXME: validate
709         dpsoftrast.user.blendfunc[0] = smodulate;
710         dpsoftrast.user.blendfunc[1] = dmodulate;
711         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
712 }
713 void DPSOFTRAST_BlendSubtract(int enable)
714 {
715         dpsoftrast.user.blendsubtract = enable != 0;
716         dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
717 }
718 void DPSOFTRAST_DepthMask(int enable)
719 {
720         dpsoftrast.user.depthmask = enable;
721 }
722 void DPSOFTRAST_DepthFunc(int comparemode)
723 {
724         // FIXME: validate
725         dpsoftrast.user.depthfunc = comparemode;
726 }
727 void DPSOFTRAST_DepthRange(float range0, float range1)
728 {
729         dpsoftrast.user.depthrange[0] = range0;
730         dpsoftrast.user.depthrange[1] = range1;
731 }
732 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
733 {
734         dpsoftrast.user.polygonoffset[0] = alongnormal;
735         dpsoftrast.user.polygonoffset[1] = intoview;
736 }
737 void DPSOFTRAST_CullFace(int mode)
738 {
739         // FIXME: validate
740         dpsoftrast.user.cullface = mode;
741 }
742 void DPSOFTRAST_AlphaTest(float enable)
743 {
744         dpsoftrast.user.alphatest = enable;
745 }
746 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
747 {
748         // FIXME: validate
749         dpsoftrast.user.alphafunc = alphafunc;
750         dpsoftrast.user.alphavalue = alphavalue;
751 }
752 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
753 {
754         dpsoftrast.user.color[0] = r;
755         dpsoftrast.user.color[1] = g;
756         dpsoftrast.user.color[2] = b;
757         dpsoftrast.user.color[3] = a;
758 }
759 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
760 {
761         int outstride = blockwidth * 4;
762         int instride = dpsoftrast.fb_width * 4;
763         int bx1 = blockx;
764         int by1 = blocky;
765         int bx2 = blockx + blockwidth;
766         int by2 = blocky + blockheight;
767         int bw;
768         int bh;
769         int x;
770         int y;
771         unsigned char *inpixels;
772         unsigned char *b;
773         unsigned char *o;
774         if (bx1 < 0) bx1 = 0;
775         if (by1 < 0) by1 = 0;
776         if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
777         if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
778         bw = bx2 - bx1;
779         bh = by2 - by1;
780         inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
781         if (dpsoftrast.bigendian)
782         {
783                 for (y = by1;y < by2;y++)
784                 {
785                         b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
786                         o = (unsigned char *)outpixels + (y - by1) * outstride;
787                         for (x = bx1;x < bx2;x++)
788                         {
789                                 o[0] = b[3];
790                                 o[1] = b[2];
791                                 o[2] = b[1];
792                                 o[3] = b[0];
793                                 o += 4;
794                                 b += 4;
795                         }
796                 }
797         }
798         else
799         {
800                 for (y = by1;y < by2;y++)
801                 {
802                         b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
803                         o = (unsigned char *)outpixels + (y - by1) * outstride;
804                         memcpy(o, b, bw*4);
805                 }
806         }
807
808 }
809 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
810 {
811         int tx1 = tx;
812         int ty1 = ty;
813         int tx2 = tx + width;
814         int ty2 = ty + height;
815         int sx1 = sx;
816         int sy1 = sy;
817         int sx2 = sx + width;
818         int sy2 = sy + height;
819         int swidth;
820         int sheight;
821         int twidth;
822         int theight;
823         int sw;
824         int sh;
825         int tw;
826         int th;
827         int y;
828         unsigned int *spixels;
829         unsigned int *tpixels;
830         DPSOFTRAST_Texture *texture;
831         texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
832         if (mip < 0 || mip >= texture->mipmaps) return;
833         spixels = dpsoftrast.fb_colorpixels[0];
834         swidth = dpsoftrast.fb_width;
835         sheight = dpsoftrast.fb_height;
836         tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
837         twidth = texture->mipmap[mip][2];
838         theight = texture->mipmap[mip][3];
839         if (tx1 < 0) tx1 = 0;
840         if (ty1 < 0) ty1 = 0;
841         if (tx2 > twidth) tx2 = twidth;
842         if (ty2 > theight) ty2 = theight;
843         if (sx1 < 0) sx1 = 0;
844         if (sy1 < 0) sy1 = 0;
845         if (sx2 > swidth) sx2 = swidth;
846         if (sy2 > sheight) sy2 = sheight;
847         tw = tx2 - tx1;
848         th = ty2 - ty1;
849         sw = sx2 - sx1;
850         sh = sy2 - sy1;
851         if (tw > sw) tw = sw;
852         if (th > sh) th = sh;
853         if (tw < 1 || th < 1)
854                 return;
855         for (y = 0;y < th;y++)
856                 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
857         if (texture->mipmaps > 1)
858                 DPSOFTRAST_Texture_CalculateMipmaps(index);
859 }
860 void DPSOFTRAST_SetTexture(int unitnum, int index)
861 {
862         DPSOFTRAST_Texture *texture;
863         if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
864         {
865                 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
866                 return;
867         }
868         texture = DPSOFTRAST_Texture_GetByIndex(index);
869         if (index && !texture)
870         {
871                 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
872                 return;
873         }
874         dpsoftrast.texbound[unitnum] = texture;
875 }
876
877 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
878 {
879         dpsoftrast.pointer_vertex3f = vertex3f;
880         dpsoftrast.stride_vertex = stride;
881 }
882 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
883 {
884         dpsoftrast.pointer_color4f = color4f;
885         dpsoftrast.pointer_color4ub = NULL;
886         dpsoftrast.stride_color = stride;
887 }
888 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
889 {
890         dpsoftrast.pointer_color4f = NULL;
891         dpsoftrast.pointer_color4ub = color4ub;
892         dpsoftrast.stride_color = stride;
893 }
894 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
895 {
896         dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
897         dpsoftrast.components_texcoord[unitnum] = numcomponents;
898         dpsoftrast.stride_texcoord[unitnum] = stride;
899 }
900
901 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
902 {
903         dpsoftrast.shader_mode = mode;
904         dpsoftrast.shader_permutation = permutation;
905 }
906 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
907 {
908         dpsoftrast.uniform4f[index*4+0] = v0;
909         dpsoftrast.uniform4f[index*4+1] = v1;
910         dpsoftrast.uniform4f[index*4+2] = v2;
911         dpsoftrast.uniform4f[index*4+3] = v3;
912 }
913 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v)
914 {
915         dpsoftrast.uniform4f[index*4+0] = v[0];
916         dpsoftrast.uniform4f[index*4+1] = v[1];
917         dpsoftrast.uniform4f[index*4+2] = v[2];
918         dpsoftrast.uniform4f[index*4+3] = v[3];
919 }
920 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v)
921 {
922         dpsoftrast.uniform4f[index*4+0] = v[0];
923         dpsoftrast.uniform4f[index*4+1] = v[1];
924         dpsoftrast.uniform4f[index*4+2] = v[2];
925         dpsoftrast.uniform4f[index*4+3] = v[3];
926         dpsoftrast.uniform4f[index*4+4] = v[4];
927         dpsoftrast.uniform4f[index*4+5] = v[5];
928         dpsoftrast.uniform4f[index*4+6] = v[6];
929         dpsoftrast.uniform4f[index*4+7] = v[7];
930         dpsoftrast.uniform4f[index*4+8] = v[8];
931         dpsoftrast.uniform4f[index*4+9] = v[9];
932         dpsoftrast.uniform4f[index*4+10] = v[10];
933         dpsoftrast.uniform4f[index*4+11] = v[11];
934         dpsoftrast.uniform4f[index*4+12] = v[12];
935         dpsoftrast.uniform4f[index*4+13] = v[13];
936         dpsoftrast.uniform4f[index*4+14] = v[14];
937         dpsoftrast.uniform4f[index*4+15] = v[15];
938 }
939 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
940 {
941         dpsoftrast.uniform1i[index] = i0;
942 }
943
944 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors)
945 {
946         int i;
947         int j;
948         int stride;
949         const float *v;
950         float *p;
951         float *data;
952         const unsigned char *b;
953         dpsoftrast.draw.numvertices = numvertices;
954         if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
955         {
956                 if (dpsoftrast.draw.maxvertices < 4096)
957                         dpsoftrast.draw.maxvertices = 4096;
958                 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
959                         dpsoftrast.draw.maxvertices *= 2;
960                 if (dpsoftrast.draw.in_array4f[0])
961                         free(dpsoftrast.draw.in_array4f[0]);
962                 data = (float *)calloc(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
963                 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
964                         dpsoftrast.draw.in_array4f[i] = data;
965                 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
966                         dpsoftrast.draw.post_array4f[i] = data;
967                 dpsoftrast.draw.screencoord4f = data;
968                 data += dpsoftrast.draw.maxvertices * 4;
969         }
970         stride = dpsoftrast.stride_vertex;
971         v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
972         p = dpsoftrast.draw.in_array4f[0];
973         for (i = 0;i < numvertices;i++)
974         {
975                 p[0] = v[0];
976                 p[1] = v[1];
977                 p[2] = v[2];
978                 p[3] = 1.0f;
979                 p += 4;
980                 v = (const float *)((const unsigned char *)v + stride);
981         }
982         if (needcolors)
983         {
984                 if (dpsoftrast.pointer_color4f)
985                 {
986                         stride = dpsoftrast.stride_color;
987                         v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
988                         p = dpsoftrast.draw.in_array4f[1];
989                         for (i = 0;i < numvertices;i++)
990                         {
991                                 p[0] = v[0];
992                                 p[1] = v[1];
993                                 p[2] = v[2];
994                                 p[3] = v[3];
995                                 p += 4;
996                                 v = (const float *)((const unsigned char *)v + stride);
997                         }
998                 }
999                 else if (dpsoftrast.pointer_color4ub)
1000                 {
1001                         stride = dpsoftrast.stride_color;
1002                         b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
1003                         p = dpsoftrast.draw.in_array4f[1];
1004                         for (i = 0;i < numvertices;i++)
1005                         {
1006                                 p[0] = b[0] * (1.0f / 255.0f);
1007                                 p[1] = b[1] * (1.0f / 255.0f);
1008                                 p[2] = b[2] * (1.0f / 255.0f);
1009                                 p[3] = b[3] * (1.0f / 255.0f);
1010                                 p += 4;
1011                                 b = (const unsigned char *)((const unsigned char *)b + stride);
1012                         }
1013                 }
1014                 else
1015                 {
1016                         v = dpsoftrast.user.color;
1017                         p = dpsoftrast.draw.in_array4f[1];
1018                         for (i = 0;i < numvertices;i++)
1019                         {
1020                                 p[0] = v[0];
1021                                 p[1] = v[1];
1022                                 p[2] = v[2];
1023                                 p[3] = v[3];
1024                                 p += 4;
1025                         }
1026                 }
1027         }
1028         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL-2;j++)
1029         {
1030                 if (dpsoftrast.pointer_texcoordf[j])
1031                 {
1032                         stride = dpsoftrast.stride_texcoord[j];
1033                         v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1034                         p = dpsoftrast.draw.in_array4f[j+2];
1035                         switch(dpsoftrast.components_texcoord[j])
1036                         {
1037                         case 2:
1038                                 for (i = 0;i < numvertices;i++)
1039                                 {
1040                                         p[0] = v[0];
1041                                         p[1] = v[1];
1042                                         p[2] = 0.0f;
1043                                         p[3] = 1.0f;
1044                                         p += 4;
1045                                         v = (const float *)((const unsigned char *)v + stride);
1046                                 }
1047                                 break;
1048                         case 3:
1049                                 for (i = 0;i < numvertices;i++)
1050                                 {
1051                                         p[0] = v[0];
1052                                         p[1] = v[1];
1053                                         p[2] = v[2];
1054                                         p[3] = 1.0f;
1055                                         p += 4;
1056                                         v = (const float *)((const unsigned char *)v + stride);
1057                                 }
1058                                 break;
1059                         case 4:
1060                                 for (i = 0;i < numvertices;i++)
1061                                 {
1062                                         p[0] = v[0];
1063                                         p[1] = v[1];
1064                                         p[2] = v[2];
1065                                         p[3] = v[3];
1066                                         p += 4;
1067                                         v = (const float *)((const unsigned char *)v + stride);
1068                                 }
1069                                 break;
1070                         }
1071                 }
1072         }
1073 }
1074
1075 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1076 {
1077         static const float identitymatrix[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
1078         // TODO: SIMD
1079         float matrix[4][4];
1080         int i;
1081         memcpy(matrix, inmatrix16f, sizeof(float[16]));
1082         if (!memcmp(identitymatrix, matrix, sizeof(float[16])))
1083         {
1084                 // fast case for identity matrix
1085                 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1086                 return;
1087         }
1088         for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1089         {
1090                 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1091                 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1092                 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1093                 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1094         }
1095 }
1096
1097 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1098 {
1099         memcpy(out4f, in4f, numitems * sizeof(float[4]));
1100 }
1101
1102 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1103 {
1104         // NOTE: this is used both as a whole mesh transform function and a
1105         // per-triangle transform function (for clipped triangles), accordingly
1106         // it should not crash on divide by 0 but the result of divide by 0 is
1107         // unimportant...
1108         // TODO: SIMD
1109         int i;
1110         float w;
1111         float viewportcenter[4];
1112         float viewportscale[4];
1113         viewportscale[0] = dpsoftrast.fb_viewportscale[0];
1114         viewportscale[1] = dpsoftrast.fb_viewportscale[1];
1115         viewportscale[2] = 0.5f;
1116         viewportscale[3] = 0.0f;
1117         viewportcenter[0] = dpsoftrast.fb_viewportcenter[0];
1118         viewportcenter[1] = dpsoftrast.fb_viewportcenter[1];
1119         viewportcenter[2] = 0.5f;
1120         viewportcenter[3] = 0.0f;
1121         for (i = 0;i < numitems;i++)
1122         {
1123                 if (!in4f[3])
1124                 {
1125                         out4f[0] = 0.0f;
1126                         out4f[1] = 0.0f;
1127                         out4f[2] = 0.0f;
1128                         out4f[3] = 0.0f;
1129                         continue;
1130                 }
1131                 w = 1.0f / in4f[3];
1132                 out4f[0] = viewportcenter[0] + viewportscale[0] * in4f[0] * w;
1133                 out4f[1] = viewportcenter[1] + viewportscale[1] * in4f[1] * w;
1134                 out4f[2] = viewportcenter[2] + viewportscale[2] * in4f[2] * w;
1135                 out4f[3] = viewportcenter[3] + viewportscale[3] * in4f[3] * w;
1136                 out4f[3] = w;
1137                 in4f += 4;
1138                 out4f += 4;
1139         }
1140 }
1141
1142 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1143 {
1144         int i;
1145         int x;
1146         int y;
1147         int w = dpsoftrast.fb_width;
1148         int bounds[4];
1149         float v0[2], v1[2];
1150         unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1151         //const float *c4f;
1152         bounds[0] = dpsoftrast.fb_viewportscissor[0];
1153         bounds[1] = dpsoftrast.fb_viewportscissor[1];
1154         bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1155         bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1156         v0[0] = screen0[0];
1157         v0[1] = screen0[1];
1158         v1[0] = screen1[0];
1159         v1[1] = screen1[1];
1160         for (i = 0;i <= 128;i++)
1161         {
1162                 // check nearclip
1163                 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1164                 //      continue;
1165                 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1166                 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1167                 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1168                         continue;
1169                 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1170                 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1171                 pixels[y*w+x] = 0xFFFFFFFF;
1172         }
1173 }
1174
1175 void DPSOFTRAST_Draw_VertexShaderLightDirection(void)
1176 {
1177 }
1178
1179 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span *span, float *zf)
1180 {
1181         int x;
1182         int startx = span->startx;
1183         int endx = span->endx;
1184         float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1185         float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1186         for (x = startx;x < endx;)
1187         {
1188                 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1189                 float z = 1.0f / (w + wslope * x), dz;
1190                 if (endsub >= endx)
1191                 {
1192                         endsub = endx-1;
1193                         dz = endsub > x ? (1.0f / (w + wslope * endsub) - z) / (endsub - x) : 0.0f;
1194                 }
1195                 else
1196                 {
1197                         dz = (1.0f / (w + wslope * endsub) - z) * (1.0f / (DPSOFTRAST_MAXSUBSPAN-1));
1198                 }
1199                 for (; x <= endsub; x++, z += dz)
1200                         zf[x] = z;
1201         }
1202 }
1203
1204 void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span *span, const float * RESTRICT in4f)
1205 {
1206         int x;
1207         int startx = span->startx;
1208         int endx = span->endx;
1209         int d[4];
1210         float a, b;
1211         unsigned char * RESTRICT pixelmask = span->pixelmask;
1212         unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1213         if (!pixel)
1214                 return;
1215         pixel += span->start * 4;
1216         // handle alphatest now (this affects depth writes too)
1217         if (dpsoftrast.user.alphatest)
1218                 for (x = startx;x < endx;x++)
1219                         if (in4f[x*4+3] < 0.5f)
1220                                 pixelmask[x] = false;
1221         // FIXME: this does not handle bigendian
1222         switch(dpsoftrast.fb_blendmode)
1223         {
1224         case DPSOFTRAST_BLENDMODE_OPAQUE:
1225                 for (x = startx;x < endx;x++)
1226                 {
1227                         if (!pixelmask[x])
1228                                 continue;
1229                         d[0] = (int)(in4f[x*4+2]*255.0f);if (d[0] > 255) d[0] = 255;
1230                         d[1] = (int)(in4f[x*4+1]*255.0f);if (d[1] > 255) d[1] = 255;
1231                         d[2] = (int)(in4f[x*4+0]*255.0f);if (d[2] > 255) d[2] = 255;
1232                         d[3] = (int)(in4f[x*4+3]*255.0f);if (d[3] > 255) d[3] = 255;
1233                         pixel[x*4+0] = d[0];
1234                         pixel[x*4+1] = d[1];
1235                         pixel[x*4+2] = d[2];
1236                         pixel[x*4+3] = d[3];
1237                 }
1238                 break;
1239         case DPSOFTRAST_BLENDMODE_ALPHA:
1240                 for (x = startx;x < endx;x++)
1241                 {
1242                         if (!pixelmask[x])
1243                                 continue;
1244                         a = in4f[x*4+3] * 255.0f;
1245                         b = 1.0f - in4f[x*4+3];
1246                         d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1247                         d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1248                         d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1249                         d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1250                         pixel[x*4+0] = d[0];
1251                         pixel[x*4+1] = d[1];
1252                         pixel[x*4+2] = d[2];
1253                         pixel[x*4+3] = d[3];
1254                 }
1255                 break;
1256         case DPSOFTRAST_BLENDMODE_ADDALPHA:
1257                 for (x = startx;x < endx;x++)
1258                 {
1259                         if (!pixelmask[x])
1260                                 continue;
1261                         a = in4f[x*4+3] * 255.0f;
1262                         d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1263                         d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1264                         d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1265                         d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1266                         pixel[x*4+0] = d[0];
1267                         pixel[x*4+1] = d[1];
1268                         pixel[x*4+2] = d[2];
1269                         pixel[x*4+3] = d[3];
1270                 }
1271                 break;
1272         case DPSOFTRAST_BLENDMODE_ADD:
1273                 for (x = startx;x < endx;x++)
1274                 {
1275                         if (!pixelmask[x])
1276                                 continue;
1277                         d[0] = (int)(in4f[x*4+2]*255.0f+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1278                         d[1] = (int)(in4f[x*4+1]*255.0f+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1279                         d[2] = (int)(in4f[x*4+0]*255.0f+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1280                         d[3] = (int)(in4f[x*4+3]*255.0f+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1281                         pixel[x*4+0] = d[0];
1282                         pixel[x*4+1] = d[1];
1283                         pixel[x*4+2] = d[2];
1284                         pixel[x*4+3] = d[3];
1285                 }
1286                 break;
1287         case DPSOFTRAST_BLENDMODE_INVMOD:
1288                 for (x = startx;x < endx;x++)
1289                 {
1290                         if (!pixelmask[x])
1291                                 continue;
1292                         d[0] = (int)((1.0f-in4f[x*4+2])*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1293                         d[1] = (int)((1.0f-in4f[x*4+1])*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1294                         d[2] = (int)((1.0f-in4f[x*4+0])*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1295                         d[3] = (int)((1.0f-in4f[x*4+3])*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1296                         pixel[x*4+0] = d[0];
1297                         pixel[x*4+1] = d[1];
1298                         pixel[x*4+2] = d[2];
1299                         pixel[x*4+3] = d[3];
1300                 }
1301                 break;
1302         case DPSOFTRAST_BLENDMODE_MUL:
1303                 for (x = startx;x < endx;x++)
1304                 {
1305                         if (!pixelmask[x])
1306                                 continue;
1307                         d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1308                         d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1309                         d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1310                         d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1311                         pixel[x*4+0] = d[0];
1312                         pixel[x*4+1] = d[1];
1313                         pixel[x*4+2] = d[2];
1314                         pixel[x*4+3] = d[3];
1315                 }
1316                 break;
1317         case DPSOFTRAST_BLENDMODE_MUL2:
1318                 for (x = startx;x < endx;x++)
1319                 {
1320                         if (!pixelmask[x])
1321                                 continue;
1322                         d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]*2.0f);if (d[0] > 255) d[0] = 255;
1323                         d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]*2.0f);if (d[1] > 255) d[1] = 255;
1324                         d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]*2.0f);if (d[2] > 255) d[2] = 255;
1325                         d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]*2.0f);if (d[3] > 255) d[3] = 255;
1326                         pixel[x*4+0] = d[0];
1327                         pixel[x*4+1] = d[1];
1328                         pixel[x*4+2] = d[2];
1329                         pixel[x*4+3] = d[3];
1330                 }
1331                 break;
1332         case DPSOFTRAST_BLENDMODE_SUBALPHA:
1333                 for (x = startx;x < endx;x++)
1334                 {
1335                         if (!pixelmask[x])
1336                                 continue;
1337                         a = in4f[x*4+3] * -255.0f;
1338                         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;
1339                         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;
1340                         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;
1341                         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;
1342                         pixel[x*4+0] = d[0];
1343                         pixel[x*4+1] = d[1];
1344                         pixel[x*4+2] = d[2];
1345                         pixel[x*4+3] = d[3];
1346                 }
1347                 break;
1348         case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1349                 for (x = startx;x < endx;x++)
1350                 {
1351                         if (!pixelmask[x])
1352                                 continue;
1353                         a = 255.0f;
1354                         b = 1.0f - in4f[x*4+3];
1355                         d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1356                         d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1357                         d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1358                         d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1359                         pixel[x*4+0] = d[0];
1360                         pixel[x*4+1] = d[1];
1361                         pixel[x*4+2] = d[2];
1362                         pixel[x*4+3] = d[3];
1363                 }
1364                 break;
1365         }
1366 }
1367
1368 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span *span, float * RESTRICT out4f, int texunitindex, int arrayindex, const float * RESTRICT zf)
1369 {
1370         int x;
1371         int startx = span->startx;
1372         int endx = span->endx;
1373         int flags;
1374         float c[4];
1375         float data[4];
1376         float slope[4];
1377         float tc[2];
1378         float tcscale[2];
1379         unsigned int tci[2];
1380         unsigned int tci1[2];
1381         unsigned int tcimin[2];
1382         unsigned int tcimax[2];
1383         int tciwrapmask[2];
1384         int tciwidth;
1385         int filter;
1386         int mip;
1387         const unsigned char * RESTRICT pixelbase;
1388         const unsigned char * RESTRICT pixel[4];
1389         DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1390         // if no texture is bound, just fill it with white
1391         if (!texture)
1392         {
1393                 for (x = startx;x < endx;x++)
1394                 {
1395                         out4f[x*4+0] = 1.0f;
1396                         out4f[x*4+1] = 1.0f;
1397                         out4f[x*4+2] = 1.0f;
1398                         out4f[x*4+3] = 1.0f;
1399                 }
1400                 return;
1401         }
1402         mip = span->mip[texunitindex];
1403         // if this mipmap of the texture is 1 pixel, just fill it with that color
1404         if (texture->mipmap[mip][1] == 4)
1405         {
1406                 c[0] = texture->bytes[2] * (1.0f/255.0f);
1407                 c[1] = texture->bytes[1] * (1.0f/255.0f);
1408                 c[2] = texture->bytes[0] * (1.0f/255.0f);
1409                 c[3] = texture->bytes[3] * (1.0f/255.0f);
1410                 for (x = startx;x < endx;x++)
1411                 {
1412                         out4f[x*4+0] = c[0];
1413                         out4f[x*4+1] = c[1];
1414                         out4f[x*4+2] = c[2];
1415                         out4f[x*4+3] = c[3];
1416                 }
1417                 return;
1418         }
1419         filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1420         data[0] = span->data[0][arrayindex][0];
1421         data[1] = span->data[0][arrayindex][1];
1422         data[2] = span->data[0][arrayindex][2];
1423         data[3] = span->data[0][arrayindex][3];
1424         slope[0] = span->data[1][arrayindex][0];
1425         slope[1] = span->data[1][arrayindex][1];
1426         slope[2] = span->data[1][arrayindex][2];
1427         slope[3] = span->data[1][arrayindex][3];
1428         flags = texture->flags;
1429         pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
1430         tcscale[0] = texture->mipmap[mip][2];
1431         tcscale[1] = texture->mipmap[mip][3];
1432         tciwidth = texture->mipmap[mip][2];
1433         tcimin[0] = 0;
1434         tcimin[1] = 0;
1435         tcimax[0] = texture->mipmap[mip][2]-1;
1436         tcimax[1] = texture->mipmap[mip][3]-1;
1437         tciwrapmask[0] = texture->mipmap[mip][2]-1;
1438         tciwrapmask[1] = texture->mipmap[mip][3]-1;
1439         for (x = startx;x < endx;)
1440         {
1441                 float endtc[2];
1442                 unsigned int subtc[2];
1443                 unsigned int substep[2];
1444                 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1445                 float subscale = 4096.0f/(DPSOFTRAST_MAXSUBSPAN-1);
1446                 if (endsub >= endx)
1447                 {
1448                         endsub = endx-1;
1449                         subscale = endsub > x ? 4096.0f / (endsub - x) : 1.0f;
1450                 }
1451                 tc[0] = (data[0] + slope[0]*x) * zf[x] * tcscale[0] - 0.5f;
1452                 tc[1] = (data[1] + slope[1]*x) * zf[x] * tcscale[1] - 0.5f;
1453                 endtc[0] = (data[0] + slope[0]*endsub) * zf[endsub] * tcscale[0] - 0.5f;
1454                 endtc[1] = (data[1] + slope[1]*endsub) * zf[endsub] * tcscale[1] - 0.5f;
1455                 substep[0] = (endtc[0] - tc[0]) * subscale;
1456                 substep[1] = (endtc[1] - tc[1]) * subscale;
1457                 subtc[0] = tc[0] * (1<<12);
1458                 subtc[1] = tc[1] * (1<<12);
1459                 if (!(flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE))
1460                 {
1461                         subtc[0] &= (tciwrapmask[0]<<12)|0xFFF;
1462                         subtc[1] &= (tciwrapmask[1]<<12)|0xFFF;
1463                 }
1464                 if(filter)
1465                 {
1466                         tci[0] = (subtc[0]>>12) - tcimin[0];
1467                         tci[1] = (subtc[1]>>12) - tcimin[0];
1468                         tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12) + 1;
1469                         tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12) + 1;
1470                         if (tci[0] <= tcimax[0] && tci[1] <= tcimax[1] && tci1[0] <= tcimax[0] && tci1[1] <= tcimax[1])
1471                         {
1472                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1473                                 {
1474                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1475                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1476                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1477                                         tci[0] = subtc[0]>>12;
1478                                         tci[1] = subtc[1]>>12;
1479                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1480                                         pixel[1] = pixel[0] + 4 * tciwidth;
1481                                         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);
1482                                         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);
1483                                         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);
1484                                         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);
1485                                         out4f[x*4+0] = c[0];
1486                                         out4f[x*4+1] = c[1];
1487                                         out4f[x*4+2] = c[2];
1488                                         out4f[x*4+3] = c[3];
1489                                 }
1490                         }
1491                         else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1492                         {
1493                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1494                                 {
1495                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1496                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1497                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1498                                         tci[0] = subtc[0]>>12;
1499                                         tci[1] = subtc[1]>>12;
1500                                         tci1[0] = tci[0] + 1;
1501                                         tci1[1] = tci[1] + 1;
1502                                         tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1503                                         tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1504                                         tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1505                                         tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1506                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1507                                         pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1508                                         pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1509                                         pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1510                                         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);
1511                                         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);
1512                                         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);
1513                                         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);
1514                                         out4f[x*4+0] = c[0];
1515                                         out4f[x*4+1] = c[1];
1516                                         out4f[x*4+2] = c[2];
1517                                         out4f[x*4+3] = c[3];
1518                                 }
1519                         }
1520                         else
1521                         {
1522                                 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1523                                 {
1524                                         unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1525                                         unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1526                                         unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1527                                         tci[0] = subtc[0]>>12;
1528                                         tci[1] = subtc[1]>>12;
1529                                         tci1[0] = tci[0] + 1;
1530                                         tci1[1] = tci[1] + 1;
1531                                         tci[0] &= tciwrapmask[0];
1532                                         tci[1] &= tciwrapmask[1];
1533                                         tci1[0] &= tciwrapmask[0];
1534                                         tci1[1] &= tciwrapmask[1];
1535                                         pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1536                                         pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1537                                         pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1538                                         pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1539                                         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);
1540                                         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);
1541                                         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);
1542                                         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);
1543                                         out4f[x*4+0] = c[0];
1544                                         out4f[x*4+1] = c[1];
1545                                         out4f[x*4+2] = c[2];
1546                                         out4f[x*4+3] = c[3];
1547                                 }
1548                         }
1549                 }
1550                 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1551                 {
1552                         for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1553                         {
1554                                 tci[0] = subtc[0]>>12;
1555                                 tci[1] = subtc[1]>>12;
1556                                 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1557                                 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1558                                 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1559                                 c[0] = pixel[0][2] * (1.0f / 255.0f);
1560                                 c[1] = pixel[0][1] * (1.0f / 255.0f);
1561                                 c[2] = pixel[0][0] * (1.0f / 255.0f);
1562                                 c[3] = pixel[0][3] * (1.0f / 255.0f);
1563                                 out4f[x*4+0] = c[0];
1564                                 out4f[x*4+1] = c[1];
1565                                 out4f[x*4+2] = c[2];
1566                                 out4f[x*4+3] = c[3];
1567                         }
1568                 }
1569                 else
1570                 {
1571                         for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1572                         {
1573                                 tci[0] = subtc[0]>>12;
1574                                 tci[1] = subtc[1]>>12;
1575                                 tci[0] &= tciwrapmask[0];
1576                                 tci[1] &= tciwrapmask[1];
1577                                 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1578                                 c[0] = pixel[0][2] * (1.0f / 255.0f);
1579                                 c[1] = pixel[0][1] * (1.0f / 255.0f);
1580                                 c[2] = pixel[0][0] * (1.0f / 255.0f);
1581                                 c[3] = pixel[0][3] * (1.0f / 255.0f);
1582                                 out4f[x*4+0] = c[0];
1583                                 out4f[x*4+1] = c[1];
1584                                 out4f[x*4+2] = c[2];
1585                                 out4f[x*4+3] = c[3];
1586                         }
1587                 }
1588         }
1589 }
1590
1591 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *in4f, int arrayindex, const float *zf)
1592 {
1593         int x;
1594         int startx = span->startx;
1595         int endx = span->endx;
1596         float c[4];
1597         float data[4];
1598         float slope[4];
1599         float z;
1600         data[0] = span->data[0][arrayindex][0];
1601         data[1] = span->data[0][arrayindex][1];
1602         data[2] = span->data[0][arrayindex][2];
1603         data[3] = span->data[0][arrayindex][3];
1604         slope[0] = span->data[1][arrayindex][0];
1605         slope[1] = span->data[1][arrayindex][1];
1606         slope[2] = span->data[1][arrayindex][2];
1607         slope[3] = span->data[1][arrayindex][3];
1608         for (x = startx;x < endx;x++)
1609         {
1610                 z = zf[x];
1611                 c[0] = (data[0] + slope[0]*x) * z;
1612                 c[1] = (data[1] + slope[1]*x) * z;
1613                 c[2] = (data[2] + slope[2]*x) * z;
1614                 c[3] = (data[3] + slope[3]*x) * z;
1615                 out4f[x*4+0] = in4f[x*4+0] * c[0];
1616                 out4f[x*4+1] = in4f[x*4+1] * c[1];
1617                 out4f[x*4+2] = in4f[x*4+2] * c[2];
1618                 out4f[x*4+3] = in4f[x*4+3] * c[3];
1619         }
1620 }
1621
1622 void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span *span, float *out4f, int arrayindex, const float *zf)
1623 {
1624         int x;
1625         int startx = span->startx;
1626         int endx = span->endx;
1627         float c[4];
1628         float data[4];
1629         float slope[4];
1630         float z;
1631         data[0] = span->data[0][arrayindex][0];
1632         data[1] = span->data[0][arrayindex][1];
1633         data[2] = span->data[0][arrayindex][2];
1634         data[3] = span->data[0][arrayindex][3];
1635         slope[0] = span->data[1][arrayindex][0];
1636         slope[1] = span->data[1][arrayindex][1];
1637         slope[2] = span->data[1][arrayindex][2];
1638         slope[3] = span->data[1][arrayindex][3];
1639         for (x = startx;x < endx;x++)
1640         {
1641                 z = zf[x];
1642                 c[0] = (data[0] + slope[0]*x) * z;
1643                 c[1] = (data[1] + slope[1]*x) * z;
1644                 c[2] = (data[2] + slope[2]*x) * z;
1645                 c[3] = (data[3] + slope[3]*x) * z;
1646                 out4f[x*4+0] = c[0];
1647                 out4f[x*4+1] = c[1];
1648                 out4f[x*4+2] = c[2];
1649                 out4f[x*4+3] = c[3];
1650         }
1651 }
1652
1653 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
1654 {
1655         int x, startx = span->startx, endx = span->endx;
1656         float c[4], localcolor[4];
1657         localcolor[0] = subcolor[0];
1658         localcolor[1] = subcolor[1];
1659         localcolor[2] = subcolor[2];
1660         localcolor[3] = subcolor[3];
1661         for (x = startx;x < endx;x++)
1662         {
1663                 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
1664                 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
1665                 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
1666                 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
1667                 out4f[x*4+0] = ina4f[x*4+0] + c[0];
1668                 out4f[x*4+1] = ina4f[x*4+1] + c[1];
1669                 out4f[x*4+2] = ina4f[x*4+2] + c[2];
1670                 out4f[x*4+3] = ina4f[x*4+3] + c[3];
1671         }
1672 }
1673
1674 void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *ina4f, const float *inb4f)
1675 {
1676         int x, startx = span->startx, endx = span->endx;
1677         for (x = startx;x < endx;x++)
1678         {
1679                 out4f[x*4+0] = ina4f[x*4+0] * inb4f[x*4+0];
1680                 out4f[x*4+1] = ina4f[x*4+1] * inb4f[x*4+1];
1681                 out4f[x*4+2] = ina4f[x*4+2] * inb4f[x*4+2];
1682                 out4f[x*4+3] = ina4f[x*4+3] * inb4f[x*4+3];
1683         }
1684 }
1685
1686 void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *ina4f, const float *inb4f)
1687 {
1688         int x, startx = span->startx, endx = span->endx;
1689         for (x = startx;x < endx;x++)
1690         {
1691                 out4f[x*4+0] = ina4f[x*4+0] + inb4f[x*4+0];
1692                 out4f[x*4+1] = ina4f[x*4+1] + inb4f[x*4+1];
1693                 out4f[x*4+2] = ina4f[x*4+2] + inb4f[x*4+2];
1694                 out4f[x*4+3] = ina4f[x*4+3] + inb4f[x*4+3];
1695         }
1696 }
1697
1698 void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *ina4f, const float *inb4f)
1699 {
1700         int x, startx = span->startx, endx = span->endx;
1701         float a, b;
1702         for (x = startx;x < endx;x++)
1703         {
1704                 a = 1.0f - inb4f[x*4+3];
1705                 b = inb4f[x*4+3];
1706                 out4f[x*4+0] = ina4f[x*4+0] * a + inb4f[x*4+0] * b;
1707                 out4f[x*4+1] = ina4f[x*4+1] * a + inb4f[x*4+1] * b;
1708                 out4f[x*4+2] = ina4f[x*4+2] * a + inb4f[x*4+2] * b;
1709                 out4f[x*4+3] = ina4f[x*4+3] * a + inb4f[x*4+3] * b;
1710         }
1711 }
1712
1713 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *in4f, const float *color)
1714 {
1715         int x, startx = span->startx, endx = span->endx;
1716         float localcolor[4], ilerp, lerp;
1717         localcolor[0] = color[0];
1718         localcolor[1] = color[1];
1719         localcolor[2] = color[2];
1720         localcolor[3] = color[3];
1721         ilerp = 1.0f - localcolor[3];
1722         lerp = localcolor[3];
1723         for (x = startx;x < endx;x++)
1724         {
1725                 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
1726                 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
1727                 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
1728                 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
1729         }
1730 }
1731
1732 void DPSOFTRAST_Draw_Span_Lightmap(const DPSOFTRAST_State_Draw_Span *span, float * RESTRICT out4f, const float * RESTRICT diffuse, const float * RESTRICT lightmap)
1733 {
1734         int x, startx = span->startx, endx = span->endx;
1735         float Color_Ambient[4], Color_Diffuse[4];
1736         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1737         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1738         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1739         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1740         Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
1741         Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
1742         Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
1743         Color_Diffuse[3] = 0.0f;
1744         for (x = startx;x < endx;x++)
1745         {
1746                 out4f[x*4+0] = diffuse[x*4+0] * (Color_Ambient[0] + lightmap[x*4+0] * Color_Diffuse[0]);
1747                 out4f[x*4+1] = diffuse[x*4+1] * (Color_Ambient[1] + lightmap[x*4+1] * Color_Diffuse[1]);
1748                 out4f[x*4+2] = diffuse[x*4+2] * (Color_Ambient[2] + lightmap[x*4+2] * Color_Diffuse[2]);
1749                 out4f[x*4+3] = diffuse[x*4+3] * (Color_Ambient[3] + lightmap[x*4+3] * Color_Diffuse[3]);
1750         }
1751 }
1752
1753 void DPSOFTRAST_Draw_Span_Lightmap_Finish(const DPSOFTRAST_State_Draw_Span *span, const float * RESTRICT diffuse, const float * RESTRICT lightmap)
1754 {
1755         int x, startx = span->startx, endx = span->endx;
1756         int d[4];
1757         float Color_Ambient[4], Color_Diffuse[4];
1758         unsigned char * RESTRICT pixelmask = span->pixelmask;
1759         unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1760         if (!pixel)
1761                 return;
1762         pixel += span->start * 4;
1763         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]*255.0f;
1764         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]*255.0f;
1765         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]*255.0f;
1766         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]*255.0f;
1767         Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0]*255.0f;
1768         Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1]*255.0f;
1769         Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2]*255.0f;
1770         Color_Diffuse[3] = 0.0f;
1771         for (x = startx;x < endx;x++)
1772         {
1773                 if (!pixelmask[x])
1774                         continue;
1775                 d[0] = diffuse[x*4+0] * (Color_Ambient[0] + lightmap[x*4+0] * Color_Diffuse[0]);if (d[0] > 255) d[0] = 255;
1776                 d[1] = diffuse[x*4+1] * (Color_Ambient[1] + lightmap[x*4+1] * Color_Diffuse[1]);if (d[1] > 255) d[1] = 255;
1777                 d[2] = diffuse[x*4+2] * (Color_Ambient[2] + lightmap[x*4+2] * Color_Diffuse[2]);if (d[2] > 255) d[2] = 255;
1778                 d[3] = diffuse[x*4+3] * (Color_Ambient[3] + lightmap[x*4+3] * Color_Diffuse[3]);if (d[3] > 255) d[3] = 255;
1779                 pixel[x*4+0] = d[2];
1780                 pixel[x*4+1] = d[1];
1781                 pixel[x*4+2] = d[0];
1782                 pixel[x*4+3] = d[3];
1783         }
1784 }
1785
1786 void DPSOFTRAST_Draw_Span_VertexColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1787 {
1788         int x, startx = span->startx, endx = span->endx;
1789         float Color_Ambient[4], Color_Diffuse[4];
1790         float c[4];
1791         float data[4];
1792         float slope[4];
1793         float z;
1794         int arrayindex = DPSOFTRAST_ARRAY_COLOR;
1795         data[0] = span->data[0][arrayindex][0];
1796         data[1] = span->data[0][arrayindex][1];
1797         data[2] = span->data[0][arrayindex][2];
1798         data[3] = span->data[0][arrayindex][3];
1799         slope[0] = span->data[1][arrayindex][0];
1800         slope[1] = span->data[1][arrayindex][1];
1801         slope[2] = span->data[1][arrayindex][2];
1802         slope[3] = span->data[1][arrayindex][3];
1803         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1804         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1805         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1806         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1807         Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
1808         Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
1809         Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
1810         Color_Diffuse[3] = 0.0f;
1811         for (x = startx;x < endx;x++)
1812         {
1813                 z = zf[x];
1814                 c[0] = (data[0] + slope[0]*x) * z;
1815                 c[1] = (data[1] + slope[1]*x) * z;
1816                 c[2] = (data[2] + slope[2]*x) * z;
1817                 c[3] = (data[3] + slope[3]*x) * z;
1818                 out4f[x*4+0] = diffuse[x*4+0] * (Color_Ambient[0] + c[0] * Color_Diffuse[0]);
1819                 out4f[x*4+1] = diffuse[x*4+1] * (Color_Ambient[1] + c[1] * Color_Diffuse[1]);
1820                 out4f[x*4+2] = diffuse[x*4+2] * (Color_Ambient[2] + c[2] * Color_Diffuse[2]);
1821                 out4f[x*4+3] = diffuse[x*4+3] * (Color_Ambient[3] + c[3] * Color_Diffuse[3]);
1822         }
1823 }
1824
1825 void DPSOFTRAST_Draw_Span_FlatColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse)
1826 {
1827         int x, startx = span->startx, endx = span->endx;
1828         float Color_Ambient[4];
1829         Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1830         Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1831         Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1832         Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1833         for (x = startx;x < endx;x++)
1834         {
1835                 out4f[x*4+0] = diffuse[x*4+0] * Color_Ambient[0];
1836                 out4f[x*4+1] = diffuse[x*4+1] * Color_Ambient[1];
1837                 out4f[x*4+2] = diffuse[x*4+2] * Color_Ambient[2];
1838                 out4f[x*4+3] = diffuse[x*4+3] * Color_Ambient[3];
1839         }
1840 }
1841
1842 void DPSOFTRAST_Draw_Span_FakeLight(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1843 {
1844         memset(out4f, 0, span->length*sizeof(float[4]));
1845 }
1846
1847 void DPSOFTRAST_Draw_Span_LightDirectionMap_ModelSpace(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1848 {
1849         memset(out4f, 0, span->length*sizeof(float[4]));
1850 }
1851
1852 void DPSOFTRAST_Draw_Span_LightDirectionMap_TangentSpace(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1853 {
1854         memset(out4f, 0, span->length*sizeof(float[4]));
1855 }
1856
1857 void DPSOFTRAST_Draw_Span_LightDirection(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1858 {
1859         memset(out4f, 0, span->length*sizeof(float[4]));
1860 }
1861
1862 void DPSOFTRAST_Draw_Span_LightSource(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1863 {
1864         memset(out4f, 0, span->length*sizeof(float[4]));
1865 }
1866
1867 void DPSOFTRAST_Draw_Span_Refraction(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1868 {
1869         memset(out4f, 0, span->length*sizeof(float[4]));
1870 }
1871
1872 void DPSOFTRAST_Draw_Span_Water(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1873 {
1874         memset(out4f, 0, span->length*sizeof(float[4]));
1875 }
1876
1877 void DPSOFTRAST_Draw_Span_DeferredGeometry(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1878 {
1879         memset(out4f, 0, span->length*sizeof(float[4]));
1880 }
1881
1882 void DPSOFTRAST_Draw_Span_DeferredLightSource(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *zf)
1883 {
1884         memset(out4f, 0, span->length*sizeof(float[4]));
1885 }
1886
1887 void DPSOFTRAST_Draw_VertexShader(void)
1888 {
1889         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);
1890         switch(dpsoftrast.shader_mode)
1891         {
1892         case SHADERMODE_GENERIC: ///< (particles/HUD/etc) vertex color: optionally multiplied by one texture
1893                 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
1894                 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1895                 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
1896                         DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
1897                 break;
1898         case SHADERMODE_POSTPROCESS: ///< postprocessing shader (r_glsl_postprocess)
1899                 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1900                 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
1901                 break;
1902         case SHADERMODE_DEPTH_OR_SHADOW: ///< (depthfirst/shadows) vertex shader only
1903                 break;
1904         case SHADERMODE_FLATCOLOR: ///< (lightmap) modulate texture by uniform color (q1bsp: q3bsp)
1905                 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);
1906                 break;
1907         case SHADERMODE_VERTEXCOLOR: ///< (lightmap) modulate texture by vertex colors (q3bsp)
1908                 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
1909                 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);
1910                 break;
1911         case SHADERMODE_LIGHTMAP: ///< (lightmap) modulate texture by lightmap texture (q1bsp: q3bsp)
1912                 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);
1913                 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
1914                 break;
1915         case SHADERMODE_FAKELIGHT: ///< (fakelight) modulate texture by "fake" lighting (no lightmaps: no nothing)
1916                 break;
1917         case SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE: ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1918                 break;
1919         case SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE: ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1920                 break;
1921         case SHADERMODE_LIGHTDIRECTION: ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1922                 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);
1923                 DPSOFTRAST_Draw_VertexShaderLightDirection();
1924                 break;
1925         case SHADERMODE_LIGHTSOURCE: ///< (lightsource) use directional pixel shading from light source (rtlight)
1926                 break;
1927         case SHADERMODE_REFRACTION: ///< refract background (the material is rendered normally after this pass)
1928                 break;
1929         case SHADERMODE_WATER: ///< refract background and reflection (the material is rendered normally after this pass)
1930                 break;
1931         case SHADERMODE_SHOWDEPTH: ///< (debugging) renders depth as color
1932                 break;
1933         case SHADERMODE_DEFERREDGEOMETRY: ///< (deferred) render material properties to screenspace geometry buffers
1934                 break;
1935         case SHADERMODE_DEFERREDLIGHTSOURCE: ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
1936                 break;
1937         case SHADERMODE_COUNT:
1938                 break;
1939         }
1940 }
1941
1942 void DPSOFTRAST_Draw_PixelShaderSpan(const DPSOFTRAST_State_Draw_Span *span)
1943 {
1944         float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
1945         float buffer_texture_color[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1946         float buffer_texture_lightmap[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1947         float buffer_FragColor[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1948         switch(dpsoftrast.shader_mode)
1949         {
1950         case SHADERMODE_GENERIC: ///< (particles/HUD/etc) vertex color: optionally multiplied by one texture
1951                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1952                 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
1953                 {
1954                         DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_FIRST, 2, buffer_z);
1955                         DPSOFTRAST_Draw_Span_MultiplyVarying(span, buffer_FragColor, buffer_texture_color, 1, buffer_z);
1956                         if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
1957                         {
1958                                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_lightmap, GL20TU_SECOND, 2, buffer_z);
1959                                 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
1960                                 {
1961                                         // multiply
1962                                         DPSOFTRAST_Draw_Span_MultiplyBuffers(span, buffer_FragColor, buffer_FragColor, buffer_texture_lightmap);
1963                                 }
1964                                 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
1965                                 {
1966                                         // add
1967                                         DPSOFTRAST_Draw_Span_AddBuffers(span, buffer_FragColor, buffer_FragColor, buffer_texture_lightmap);
1968                                 }
1969                                 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND)
1970                                 {
1971                                         // alphablend
1972                                         DPSOFTRAST_Draw_Span_MixBuffers(span, buffer_FragColor, buffer_FragColor, buffer_texture_lightmap);
1973                                 }
1974                         }
1975                 }
1976                 else
1977                         DPSOFTRAST_Draw_Span_Varying(span, buffer_FragColor, 1, buffer_z);
1978                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1979                 break;
1980         case SHADERMODE_POSTPROCESS: ///< postprocessing shader (r_glsl_postprocess)
1981                 // TODO: optimize!!  at the very least there is no reason to use texture sampling on the frame texture
1982                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1983                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_FragColor, GL20TU_FIRST, 2, buffer_z);
1984                 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
1985                 {
1986                         DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_SECOND, 3, buffer_z);
1987                         DPSOFTRAST_Draw_Span_AddBloom(span, buffer_FragColor, buffer_FragColor, buffer_texture_color, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
1988                 }
1989                 DPSOFTRAST_Draw_Span_MixUniformColor(span, buffer_FragColor, buffer_FragColor, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
1990                 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
1991                 {
1992                         // TODO: implement saturation
1993                 }
1994                 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
1995                 {
1996                         // TODO: implement gammaramps
1997                 }
1998                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1999                 break;
2000         case SHADERMODE_DEPTH_OR_SHADOW: ///< (depthfirst/shadows) vertex shader only
2001                 break;
2002         case SHADERMODE_FLATCOLOR: ///< (lightmap) modulate texture by uniform color (q1bsp: q3bsp)
2003                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2004                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2005                 DPSOFTRAST_Draw_Span_FlatColor(span, buffer_FragColor, buffer_texture_color);
2006                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2007                 break;
2008         case SHADERMODE_VERTEXCOLOR: ///< (lightmap) modulate texture by vertex colors (q3bsp)
2009                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2010                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2011                 DPSOFTRAST_Draw_Span_VertexColor(span, buffer_FragColor, buffer_texture_color, buffer_z);
2012                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2013                 break;
2014         case SHADERMODE_LIGHTMAP: ///< (lightmap) modulate texture by lightmap texture (q1bsp: q3bsp)
2015                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2016                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2017                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_lightmap, GL20TU_LIGHTMAP, 6, buffer_z);
2018                 if(!dpsoftrast.user.alphatest && dpsoftrast.fb_blendmode == DPSOFTRAST_BLENDMODE_OPAQUE)
2019                 {
2020                         DPSOFTRAST_Draw_Span_Lightmap_Finish(span, buffer_texture_color, buffer_texture_lightmap);
2021                 }
2022                 else
2023                 {
2024                         DPSOFTRAST_Draw_Span_Lightmap(span, buffer_FragColor, buffer_texture_color, buffer_texture_lightmap);
2025                         DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2026                 }
2027                 break;
2028         case SHADERMODE_FAKELIGHT: ///< (fakelight) modulate texture by "fake" lighting (no lightmaps: no nothing)
2029                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2030                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2031                 DPSOFTRAST_Draw_Span_FakeLight(span, buffer_FragColor, buffer_texture_color, buffer_z);
2032                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2033                 break;
2034         case SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE: ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
2035                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2036                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2037                 DPSOFTRAST_Draw_Span_LightDirectionMap_ModelSpace(span, buffer_FragColor, buffer_texture_color, buffer_z);
2038                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2039                 break;
2040         case SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE: ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
2041                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2042                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2043                 DPSOFTRAST_Draw_Span_LightDirectionMap_TangentSpace(span, buffer_FragColor, buffer_texture_color, buffer_z);
2044                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2045                 break;
2046         case SHADERMODE_LIGHTDIRECTION: ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
2047                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2048                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2049                 DPSOFTRAST_Draw_Span_LightDirection(span, buffer_FragColor, buffer_texture_color, buffer_z);
2050                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2051                 break;
2052         case SHADERMODE_LIGHTSOURCE: ///< (lightsource) use directional pixel shading from light source (rtlight)
2053                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2054                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2055                 DPSOFTRAST_Draw_Span_LightSource(span, buffer_FragColor, buffer_texture_color, buffer_z);
2056                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2057                 break;
2058         case SHADERMODE_REFRACTION: ///< refract background (the material is rendered normally after this pass)
2059                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2060                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2061                 DPSOFTRAST_Draw_Span_Refraction(span, buffer_FragColor, buffer_texture_color, buffer_z);
2062                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2063                 break;
2064         case SHADERMODE_WATER: ///< refract background and reflection (the material is rendered normally after this pass)
2065                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2066                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2067                 DPSOFTRAST_Draw_Span_Water(span, buffer_FragColor, buffer_texture_color, buffer_z);
2068                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2069                 break;
2070         case SHADERMODE_SHOWDEPTH: ///< (debugging) renders depth as color
2071                 break;
2072         case SHADERMODE_DEFERREDGEOMETRY: ///< (deferred) render material properties to screenspace geometry buffers
2073                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2074                 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
2075                 DPSOFTRAST_Draw_Span_DeferredGeometry(span, buffer_FragColor, buffer_texture_color, buffer_z);
2076                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2077                 break;
2078         case SHADERMODE_DEFERREDLIGHTSOURCE: ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
2079                 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2080                 DPSOFTRAST_Draw_Span_DeferredLightSource(span, buffer_FragColor, buffer_z);
2081                 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
2082                 break;
2083         case SHADERMODE_COUNT:
2084                 break;
2085         }
2086 }
2087
2088 void DPSOFTRAST_Draw_ProcessSpans(void)
2089 {
2090         int i;
2091         int x;
2092         int startx;
2093         int endx;
2094         int numspans = dpsoftrast.draw.numspans;
2095 //      unsigned int c;
2096 //      unsigned int *colorpixel;
2097         unsigned int *depthpixel;
2098         float w;
2099         float wslope;
2100         int depth;
2101         int depthslope;
2102         unsigned int d;
2103         DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
2104         unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2105         for (i = 0;i < numspans;i++, span++)
2106         {
2107                 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
2108                 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
2109                 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
2110                 {
2111                         depth = (int)(w*DPSOFTRAST_DEPTHSCALE);
2112                         depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
2113                         depthpixel = dpsoftrast.fb_depthpixels + span->start;
2114                         switch(dpsoftrast.fb_depthfunc)
2115                         {
2116                         default:
2117                         case GL_ALWAYS:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
2118                         case GL_LESS:    for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
2119                         case GL_LEQUAL:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
2120                         case GL_EQUAL:   for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
2121                         case GL_GEQUAL:  for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
2122                         case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
2123                         case GL_NEVER:   for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
2124                         }
2125                         //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
2126                         //for (x = 0;x < span->length;x++)
2127                         //      colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
2128                         // if there is no color buffer, skip pixel shader
2129                         startx = 0;
2130                         endx = span->length;
2131                         while (startx < endx && !pixelmask[startx])
2132                                 startx++;
2133                         while (endx > startx && !pixelmask[endx-1])
2134                                 endx--;
2135                         if (startx >= endx)
2136                                 continue; // no pixels to fill
2137                         span->pixelmask = pixelmask;
2138                         span->startx = startx;
2139                         span->endx = endx;
2140                         // run pixel shader if appropriate
2141                         // do this before running depthmask code, to allow the pixelshader
2142                         // to clear pixelmask values for alpha testing
2143                         if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
2144                                 DPSOFTRAST_Draw_PixelShaderSpan(span);
2145                         if (dpsoftrast.user.depthmask)
2146                                 for (x = 0, d = depth;x < span->length;x++, d += depthslope)
2147                                         if (pixelmask[x])
2148                                                 depthpixel[x] = d;
2149                 }
2150                 else
2151                 {
2152                         // no depth testing means we're just dealing with color...
2153                         // if there is no color buffer, skip pixel shader
2154                         if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
2155                         {
2156                                 memset(pixelmask, 1, span->length);
2157                                 span->pixelmask = pixelmask;
2158                                 span->startx = 0;
2159                                 span->endx = span->length;
2160                                 DPSOFTRAST_Draw_PixelShaderSpan(span);
2161                         }
2162                 }
2163         }
2164 }
2165
2166 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask)
2167 {
2168         int cullface = dpsoftrast.user.cullface;
2169         int width = dpsoftrast.fb_width;
2170         int height = dpsoftrast.fb_height;
2171         int i;
2172         int j;
2173         int k;
2174         int y;
2175         int e[3];
2176         int screenx[4];
2177         int screeny[4];
2178         int screenyless[4];
2179         int numpoints;
2180         int clipflags;
2181         int edge0p;
2182         int edge0n;
2183         int edge1p;
2184         int edge1n;
2185         int extent[6];
2186         int startx;
2187         int endx;
2188         float mip_edge0tc[2];
2189         float mip_edge1tc[2];
2190         float mip_edge0xy[2];
2191         float mip_edge1xy[2];
2192         float mip_edge0xymul;
2193         float mip_edge1xymul;
2194         float mip_edge0mip;
2195         float mip_edge1mip;
2196         float mipdensity;
2197         unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS];
2198         float startxf;
2199         float endxf;
2200         float edge0ylerp;
2201         float edge0yilerp;
2202         float edge1ylerp;
2203         float edge1yilerp;
2204         float edge0xf;
2205         float edge1xf;
2206         float spanilength;
2207         float startxlerp;
2208         float yc;
2209         float w;
2210         float frac;
2211         float ifrac;
2212         float trianglearea2;
2213         float triangleedge[2][4];
2214         float trianglenormal[4];
2215         float clipdist[4];
2216         float clipped[DPSOFTRAST_ARRAY_TOTAL][4][4];
2217         float screen[4][4];
2218         float proj[DPSOFTRAST_ARRAY_TOTAL][4][4];
2219         DPSOFTRAST_Texture *texture;
2220         DPSOFTRAST_State_Draw_Span *span;
2221         DPSOFTRAST_State_Draw_Span *oldspan;
2222         for (i = 0;i < numtriangles;i++)
2223         {
2224                 // generate the 3 edges of this triangle
2225                 // generate spans for the triangle - switch based on left split or right split classification of triangle
2226                 if (element3i)
2227                 {
2228                         e[0] = element3i[i*3+0] - firstvertex;
2229                         e[1] = element3i[i*3+1] - firstvertex;
2230                         e[2] = element3i[i*3+2] - firstvertex;
2231                 }
2232                 else if (element3s)
2233                 {
2234                         e[0] = element3s[i*3+0] - firstvertex;
2235                         e[1] = element3s[i*3+1] - firstvertex;
2236                         e[2] = element3s[i*3+2] - firstvertex;
2237                 }
2238                 else
2239                 {
2240                         e[0] = i*3+0;
2241                         e[1] = i*3+1;
2242                         e[2] = i*3+2;
2243                 }
2244                 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];
2245                 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];
2246                 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];
2247                 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];
2248                 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];
2249                 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];
2250                 trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
2251                 trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
2252                 trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
2253                 trianglearea2 = trianglenormal[0] * trianglenormal[0] + trianglenormal[1] * trianglenormal[1] + trianglenormal[2] * trianglenormal[2];
2254                 // skip degenerate triangles, nothing good can come from them...
2255                 if (trianglearea2 == 0.0f)
2256                         continue;
2257                 // apply current cullface mode (this culls many triangles)
2258                 switch(cullface)
2259                 {
2260                 case GL_BACK:
2261                         if (trianglenormal[2] < 0)
2262                                 continue;
2263                         break;
2264                 case GL_FRONT:
2265                         if (trianglenormal[2] > 0)
2266                                 continue;
2267                         break;
2268                 }
2269                 // calculate distance from nearplane
2270                 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + 1.0f;
2271                 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + 1.0f;
2272                 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + 1.0f;
2273                 clipflags = 0;
2274                 if (clipdist[0] < 0.0f)
2275                         clipflags |= 1;
2276                 if (clipdist[1] < 0.0f)
2277                         clipflags |= 2;
2278                 if (clipdist[2] < 0.0f)
2279                         clipflags |= 4;
2280                 // clip triangle if necessary
2281                 switch(clipflags)
2282                 {
2283                 case 0: /*000*/
2284                         // triangle is entirely in front of nearplane
2285
2286                         // macros for clipping vertices
2287 #define CLIPPEDVERTEXLERP(k,p1,p2) \
2288                         frac = clipdist[p1] / (clipdist[p1] - clipdist[p2]);\
2289                         ifrac = 1.0f - frac;\
2290                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2291                         {\
2292                                 if (arraymask[j])\
2293                                 {\
2294                                         clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+0]*frac;\
2295                                         clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+1]*frac;\
2296                                         clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+2]*frac;\
2297                                         clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+3]*frac;\
2298                                 }\
2299                         }\
2300                         DPSOFTRAST_Draw_ProjectVertices(screen[k], clipped[DPSOFTRAST_ARRAY_POSITION][k], 1)
2301 #define CLIPPEDVERTEXCOPY(k,p1) \
2302                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2303                         {\
2304                                 if (arraymask[j])\
2305                                 {\
2306                                         clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0];\
2307                                         clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1];\
2308                                         clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2];\
2309                                         clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3];\
2310                                 }\
2311                         }\
2312                         screen[k][0] = dpsoftrast.draw.screencoord4f[e[p1]*4+0];\
2313                         screen[k][1] = dpsoftrast.draw.screencoord4f[e[p1]*4+1];\
2314                         screen[k][2] = dpsoftrast.draw.screencoord4f[e[p1]*4+2];\
2315                         screen[k][3] = dpsoftrast.draw.screencoord4f[e[p1]*4+3];
2316
2317                         CLIPPEDVERTEXCOPY(0,0);
2318                         CLIPPEDVERTEXCOPY(1,1);
2319                         CLIPPEDVERTEXCOPY(2,2);
2320                         numpoints = 3;
2321                         break;
2322                 case 1: /*100*/
2323                         CLIPPEDVERTEXLERP(0,0,1);
2324                         CLIPPEDVERTEXCOPY(1,1);
2325                         CLIPPEDVERTEXCOPY(2,2);
2326                         CLIPPEDVERTEXLERP(3,2,0);
2327                         numpoints = 4;
2328                         break;
2329                 case 2: /*010*/
2330                         CLIPPEDVERTEXCOPY(0,0);
2331                         CLIPPEDVERTEXLERP(1,0,1);
2332                         CLIPPEDVERTEXLERP(2,1,2);
2333                         CLIPPEDVERTEXCOPY(3,2);
2334                         numpoints = 4;
2335                         break;
2336                 case 3: /*110*/
2337                         CLIPPEDVERTEXLERP(0,1,2);
2338                         CLIPPEDVERTEXCOPY(1,2);
2339                         CLIPPEDVERTEXLERP(2,2,0);
2340                         numpoints = 3;
2341                         break;
2342                 case 4: /*001*/
2343                         CLIPPEDVERTEXCOPY(0,0);
2344                         CLIPPEDVERTEXCOPY(1,1);
2345                         CLIPPEDVERTEXLERP(2,1,2);
2346                         CLIPPEDVERTEXLERP(3,2,0);
2347                         numpoints = 4;
2348                         break;
2349                 case 5: /*101*/
2350                         CLIPPEDVERTEXLERP(0,0,1);
2351                         CLIPPEDVERTEXCOPY(1,1);
2352                         CLIPPEDVERTEXLERP(2,1,2);
2353                         numpoints = 3;
2354                         break;
2355                 case 6: /*011*/
2356                         CLIPPEDVERTEXCOPY(0,0);
2357                         CLIPPEDVERTEXLERP(1,0,1);
2358                         CLIPPEDVERTEXLERP(2,2,0);
2359                         numpoints = 3;
2360                         break;
2361                 case 7: /*111*/
2362                         // triangle is entirely behind nearplane
2363                         continue;
2364                 }
2365                 // calculate integer y coords for triangle points
2366                 screenx[0] = (int)(screen[0][0]);
2367                 screeny[0] = (int)(screen[0][1]);
2368                 screenx[1] = (int)(screen[1][0]);
2369                 screeny[1] = (int)(screen[1][1]);
2370                 screenx[2] = (int)(screen[2][0]);
2371                 screeny[2] = (int)(screen[2][1]);
2372                 screenx[3] = (int)(screen[3][0]);
2373                 screeny[3] = (int)(screen[3][1]);
2374                 // figure out the extents (bounding box) of the triangle
2375                 extent[0] = screenx[0];
2376                 extent[1] = screeny[0];
2377                 extent[2] = screenx[0];
2378                 extent[3] = screeny[0];
2379                 for (j = 1;j < numpoints;j++)
2380                 {
2381                         if (extent[0] > screenx[j]) extent[0] = screenx[j];
2382                         if (extent[1] > screeny[j]) extent[1] = screeny[j];
2383                         if (extent[2] < screenx[j]) extent[2] = screenx[j];
2384                         if (extent[3] < screeny[j]) extent[3] = screeny[j];
2385                 }
2386                 //extent[0]--;
2387                 //extent[1]--;
2388                 extent[2]++;
2389                 extent[3]++;
2390                 if (extent[0] < 0)
2391                         extent[0] = 0;
2392                 if (extent[1] < 0)
2393                         extent[1] = 0;
2394                 if (extent[2] > width)
2395                         extent[2] = width;
2396                 if (extent[3] > height)
2397                         extent[3] = height;
2398                 // skip offscreen triangles
2399                 if (extent[2] <= extent[0] || extent[3] <= extent[1])
2400                         continue;
2401                 // okay, this triangle is going to produce spans, we'd better project
2402                 // the interpolants now (this is what gives perspective texturing),
2403                 // this consists of simply multiplying all arrays by the W coord
2404                 // (which is basically 1/Z), which will be undone per-pixel
2405                 // (multiplying by Z again) to get the perspective-correct array
2406                 // values
2407                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2408                 {
2409                         if (arraymask[j])
2410                         {
2411                                 for (k = 0;k < numpoints;k++)
2412                                 {
2413                                         w = screen[k][3];
2414                                         proj[j][k][0] = clipped[j][k][0] * w;
2415                                         proj[j][k][1] = clipped[j][k][1] * w;
2416                                         proj[j][k][2] = clipped[j][k][2] * w;
2417                                         proj[j][k][3] = clipped[j][k][3] * w;
2418                                 }
2419                         }
2420                 }
2421                 // adjust texture LOD by texture density, in the simplest way possible...
2422                 mip_edge0xy[0] = screen[0][0] - screen[1][0];
2423                 mip_edge0xy[1] = screen[0][1] - screen[1][1];
2424                 mip_edge1xy[0] = screen[2][0] - screen[1][0];
2425                 mip_edge1xy[1] = screen[2][1] - screen[1][1];
2426                 mip_edge0xymul = 1.0f / (mip_edge0xy[0]*mip_edge0xy[0]+mip_edge0xy[1]*mip_edge0xy[1]);
2427                 mip_edge1xymul = 1.0f / (mip_edge1xy[0]*mip_edge1xy[0]+mip_edge1xy[1]*mip_edge1xy[1]);
2428                 for (j = 0;j < DPSOFTRAST_MAXTEXTUREUNITS;j++)
2429                 {
2430                         texture = dpsoftrast.texbound[j];
2431                         if (texture)
2432                         {
2433                                 if (texture->filter <= DPSOFTRAST_TEXTURE_FILTER_LINEAR)
2434                                 {
2435                                         mip[j] = 0;
2436                                         continue;
2437                                 }
2438                                 // FIXME: use appropriate array for this texture!
2439                                 mip_edge0tc[0] = (clipped[DPSOFTRAST_ARRAY_TEXCOORD0][0][0] - clipped[DPSOFTRAST_ARRAY_TEXCOORD0][1][0]) * texture->mipmap[0][2];
2440                                 mip_edge0tc[1] = (clipped[DPSOFTRAST_ARRAY_TEXCOORD0][0][1] - clipped[DPSOFTRAST_ARRAY_TEXCOORD0][1][1]) * texture->mipmap[0][3];
2441                                 mip_edge1tc[0] = (clipped[DPSOFTRAST_ARRAY_TEXCOORD0][2][0] - clipped[DPSOFTRAST_ARRAY_TEXCOORD0][1][0]) * texture->mipmap[0][2];
2442                                 mip_edge1tc[1] = (clipped[DPSOFTRAST_ARRAY_TEXCOORD0][2][1] - clipped[DPSOFTRAST_ARRAY_TEXCOORD0][1][1]) * texture->mipmap[0][3];
2443                                 mip_edge0mip = (mip_edge0tc[0]*mip_edge0tc[0]+mip_edge0tc[1]*mip_edge0tc[1]) * mip_edge0xymul;
2444                                 mip_edge1mip = (mip_edge1tc[0]*mip_edge1tc[0]+mip_edge1tc[1]*mip_edge1tc[1]) * mip_edge1xymul;
2445                                 // this will be multiplied in the texturing routine by the texture resolution
2446                                 mipdensity = mip_edge0mip < mip_edge1mip ? mip_edge0mip : mip_edge1mip;
2447                                 y = (int)(log(mipdensity)/log(2.0f) + 0.5f);
2448                                 if (y < 0)
2449                                         y = 0;
2450                                 if (y > texture->mipmaps - 1)
2451                                         y = texture->mipmaps - 1;
2452                                 mip[j] = y;
2453                         }
2454                 }
2455                 // iterate potential spans
2456                 // TODO: optimize?  if we figured out the edge order beforehand, this
2457                 //       could do loops over the edges in the proper order rather than
2458                 //       selecting them for each span
2459                 // TODO: optimize?  the edges could have data slopes calculated
2460                 // TODO: optimize?  the data slopes could be calculated as a plane
2461                 //       (2D slopes) to avoid any interpolation along edges at all
2462                 for (y = extent[1];y < extent[3];y++)
2463                 {
2464                         // get center of pixel y
2465                         yc = y;
2466                         // do the compares all at once
2467                         screenyless[0] = y <= screeny[0];
2468                         screenyless[1] = y <= screeny[1];
2469                         screenyless[2] = y <= screeny[2];
2470                         screenyless[3] = y <= screeny[3];
2471                         if (numpoints == 4)
2472                         {
2473                                 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4 + screenyless[3] * 8)
2474                                 {
2475                                 case  0: /*0000*/ continue;
2476                                 case  1: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 0;edge1n = 1;break;
2477                                 case  2: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
2478                                 case  3: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 2;break;
2479                                 case  4: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break;
2480                                 case  5: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break; // concave - nonsense
2481                                 case  6: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 3;break;
2482                                 case  7: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 3;break;
2483                                 case  8: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 3;edge1n = 0;break;
2484                                 case  9: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 1;break;
2485                                 case 10: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
2486                                 case 11: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break;
2487                                 case 12: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 0;break;
2488                                 case 13: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
2489                                 case 14: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 0;break;
2490                                 case 15: /*1111*/ continue;
2491                                 }
2492                         }
2493                         else
2494                         {
2495                                 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4)
2496                                 {
2497                                 case 0: /*000*/ continue;
2498                                 case 1: /*100*/ edge0p = 2;edge0n = 0;edge1p = 0;edge1n = 1;break;
2499                                 case 2: /*010*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
2500                                 case 3: /*110*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 2;break;
2501                                 case 4: /*001*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 0;break;
2502                                 case 5: /*101*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
2503                                 case 6: /*011*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 0;break;
2504                                 case 7: /*111*/ continue;
2505                                 }
2506                         }
2507 #if 0
2508                 {
2509                         int foundedges = 0;
2510                         int cedge0p = 0;
2511                         int cedge0n = 0;
2512                         int cedge1p = 0;
2513                         int cedge1n = 0;
2514                         for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
2515                         {
2516                                 if (screenyless[k] && !screenyless[j])
2517                                 {
2518                                         cedge1p = k;
2519                                         cedge1n = j;
2520                                         foundedges |= 1;
2521                                 }
2522                                 else if (screenyless[j] && !screenyless[k])
2523                                 {
2524                                         cedge0p = k;
2525                                         cedge0n = j;
2526                                         foundedges |= 2;
2527                                 }
2528                         }
2529                         if (foundedges != 3)
2530                                 continue;
2531                         if (cedge0p != edge0p || cedge0n != edge0n || cedge1p != edge1p || cedge1n != edge1n)
2532                         {
2533                                 if (numpoints == 4)
2534                                         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);
2535                                 else
2536                                         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);
2537                         }
2538                 }
2539 #endif
2540                         edge0ylerp = (yc - screen[edge0p][1]) / (screen[edge0n][1] - screen[edge0p][1]);
2541                         edge1ylerp = (yc - screen[edge1p][1]) / (screen[edge1n][1] - screen[edge1p][1]);
2542                         if (edge0ylerp < 0 || edge0ylerp > 1 || edge1ylerp < 0 || edge1ylerp > 1)
2543                                 continue;
2544                         edge0yilerp = 1.0f - edge0ylerp;
2545                         edge1yilerp = 1.0f - edge1ylerp;
2546                         edge0xf = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2547                         edge1xf = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2548                         if (edge0xf < edge1xf)
2549                         {
2550                                 startxf = edge0xf;
2551                                 endxf = edge1xf;
2552                         }
2553                         else
2554                         {
2555                                 startxf = edge1xf;
2556                                 endxf = edge0xf;
2557                         }
2558                         startx = (int)ceil(startxf);
2559                         endx = (int)ceil(endxf);
2560                         if (startx < 0)
2561                                 startx = 0;
2562                         if (endx > width)
2563                                 endx = width;
2564                         if (startx >= endx)
2565                                 continue;
2566                         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); }
2567                         spanilength = 1.0f / (endxf - startxf);
2568                         startxlerp = startx - startxf;
2569                         span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
2570                         memcpy(span->mip, mip, sizeof(span->mip));
2571                         span->start = y * width + startx;
2572                         span->length = endx - startx;
2573                         j = DPSOFTRAST_ARRAY_TOTAL;
2574                         if (edge0xf < edge1xf)
2575                         {
2576                                 span->data[0][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2577                                 span->data[0][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
2578                                 span->data[0][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
2579                                 span->data[0][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
2580                                 span->data[1][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2581                                 span->data[1][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
2582                                 span->data[1][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
2583                                 span->data[1][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
2584                                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2585                                 {
2586                                         if (arraymask[j])
2587                                         {
2588                                                 span->data[0][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
2589                                                 span->data[0][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
2590                                                 span->data[0][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
2591                                                 span->data[0][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
2592                                                 span->data[1][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
2593                                                 span->data[1][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
2594                                                 span->data[1][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
2595                                                 span->data[1][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
2596                                         }
2597                                 }
2598                         }
2599                         else
2600                         {
2601                                 span->data[0][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2602                                 span->data[0][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
2603                                 span->data[0][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
2604                                 span->data[0][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
2605                                 span->data[1][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2606                                 span->data[1][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
2607                                 span->data[1][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
2608                                 span->data[1][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
2609                                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2610                                 {
2611                                         if (arraymask[j])
2612                                         {
2613                                                 span->data[0][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
2614                                                 span->data[0][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
2615                                                 span->data[0][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
2616                                                 span->data[0][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
2617                                                 span->data[1][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
2618                                                 span->data[1][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
2619                                                 span->data[1][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
2620                                                 span->data[1][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
2621                                         }
2622                                 }
2623                         }
2624                         // change data[1][n][] to be a data slope
2625                         j = DPSOFTRAST_ARRAY_TOTAL;
2626                         span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
2627                         span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
2628                         span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
2629                         span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
2630                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2631                         {
2632                                 if (arraymask[j])
2633                                 {
2634                                         span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
2635                                         span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
2636                                         span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
2637                                         span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
2638                                 }
2639                         }
2640                         // adjust the data[0][n][] to be correct for the pixel centers
2641                         // this also handles horizontal clipping where a major part of the
2642                         // span may be off the left side of the screen
2643                         j = DPSOFTRAST_ARRAY_TOTAL;
2644                         span->data[0][j][0] += span->data[1][j][0] * startxlerp;
2645                         span->data[0][j][1] += span->data[1][j][1] * startxlerp;
2646                         span->data[0][j][2] += span->data[1][j][2] * startxlerp;
2647                         span->data[0][j][3] += span->data[1][j][3] * startxlerp;
2648                         for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2649                         {
2650                                 if (arraymask[j])
2651                                 {
2652                                         span->data[0][j][0] += span->data[1][j][0] * startxlerp;
2653                                         span->data[0][j][1] += span->data[1][j][1] * startxlerp;
2654                                         span->data[0][j][2] += span->data[1][j][2] * startxlerp;
2655                                         span->data[0][j][3] += span->data[1][j][3] * startxlerp;
2656                                 }
2657                         }
2658                         // to keep the shader routines from needing more than a small
2659                         // buffer for pixel intermediate data, we split long spans...
2660                         while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
2661                         {
2662                                 span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
2663                                 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
2664                                 {
2665                                         DPSOFTRAST_Draw_ProcessSpans();
2666                                         dpsoftrast.draw.numspans = 0;
2667                                 }
2668                                 oldspan = span;
2669                                 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
2670                                 *span = *oldspan;
2671                                 startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
2672                                 span->start = y * width + startx;
2673                                 span->length = endx - startx;
2674                                 j = DPSOFTRAST_ARRAY_TOTAL;
2675                                 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2676                                 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2677                                 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2678                                 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2679                                 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2680                                 {
2681                                         if (arraymask[j])
2682                                         {
2683                                                 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2684                                                 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2685                                                 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2686                                                 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2687                                         }
2688                                 }
2689                         }
2690                         // after all that, we have a span suitable for the pixel shader...
2691                         if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
2692                         {
2693                                 DPSOFTRAST_Draw_ProcessSpans();
2694                                 dpsoftrast.draw.numspans = 0;
2695                         }
2696                 }
2697                 // draw outlines over triangle for debugging
2698         //      for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
2699         //              DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
2700         }
2701         if (dpsoftrast.draw.numspans)
2702         {
2703                 DPSOFTRAST_Draw_ProcessSpans();
2704                 dpsoftrast.draw.numspans = 0;
2705         }
2706 }
2707
2708 void DPSOFTRAST_Draw_DebugPoints(void)
2709 {
2710         int i;
2711         int x;
2712         int y;
2713         int numvertices = dpsoftrast.draw.numvertices;
2714         int w = dpsoftrast.fb_width;
2715         int bounds[4];
2716         unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
2717         const float *c4f;
2718         bounds[0] = dpsoftrast.fb_viewportscissor[0];
2719         bounds[1] = dpsoftrast.fb_viewportscissor[1];
2720         bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
2721         bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
2722         for (i = 0;i < numvertices;i++)
2723         {
2724                 // check nearclip
2725                 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
2726                 //      continue;
2727                 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
2728                 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
2729                 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
2730                 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
2731                 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
2732                 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
2733                 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
2734                         continue;
2735                 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
2736                 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
2737         }
2738 }
2739
2740 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
2741 {
2742         unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL];
2743         arraymask[0] = true;
2744         arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode)
2745         arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL;
2746         arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL;
2747         arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL;
2748         arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL;
2749         arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL;
2750         arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL;
2751         arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL;
2752         arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL;
2753         DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
2754         DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true);
2755         DPSOFTRAST_Draw_VertexShader();
2756         DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
2757         DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numvertices, numtriangles, element3i, element3s, arraymask);
2758 }
2759
2760 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
2761 {
2762         union
2763         {
2764                 int i;
2765                 unsigned char b[4];
2766         }
2767         u;
2768         u.i = 1;
2769         memset(&dpsoftrast, 0, sizeof(dpsoftrast));
2770         dpsoftrast.bigendian = u.b[3];
2771         dpsoftrast.fb_width = width;
2772         dpsoftrast.fb_height = height;
2773         dpsoftrast.fb_depthpixels = depthpixels;
2774         dpsoftrast.fb_colorpixels[0] = colorpixels;
2775         dpsoftrast.fb_colorpixels[1] = NULL;
2776         dpsoftrast.fb_colorpixels[1] = NULL;
2777         dpsoftrast.fb_colorpixels[1] = NULL;
2778         dpsoftrast.texture_firstfree = 1;
2779         dpsoftrast.texture_end = 1;
2780         dpsoftrast.texture_max = 0;
2781         dpsoftrast.user.colormask[0] = 1;
2782         dpsoftrast.user.colormask[1] = 1;
2783         dpsoftrast.user.colormask[2] = 1;
2784         dpsoftrast.user.colormask[3] = 1;
2785         dpsoftrast.user.blendfunc[0] = GL_ONE;
2786         dpsoftrast.user.blendfunc[1] = GL_ZERO;
2787         dpsoftrast.user.depthmask = true;
2788         dpsoftrast.user.depthtest = true;
2789         dpsoftrast.user.depthfunc = GL_LEQUAL;
2790         dpsoftrast.user.scissortest = false;
2791         dpsoftrast.user.cullface = GL_BACK;
2792         dpsoftrast.user.alphatest = false;
2793         dpsoftrast.user.alphafunc = GL_GREATER;
2794         dpsoftrast.user.alphavalue = 0.5f;
2795         dpsoftrast.user.scissor[0] = 0;
2796         dpsoftrast.user.scissor[1] = 0;
2797         dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
2798         dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
2799         dpsoftrast.user.viewport[0] = 0;
2800         dpsoftrast.user.viewport[1] = 0;
2801         dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
2802         dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
2803         dpsoftrast.user.depthrange[0] = 0;
2804         dpsoftrast.user.depthrange[1] = 1;
2805         dpsoftrast.user.polygonoffset[0] = 0;
2806         dpsoftrast.user.polygonoffset[1] = 0;
2807         dpsoftrast.user.color[0] = 1;
2808         dpsoftrast.user.color[1] = 1;
2809         dpsoftrast.user.color[2] = 1;
2810         dpsoftrast.user.color[3] = 1;
2811         dpsoftrast.validate = -1;
2812         DPSOFTRAST_Validate(-1);
2813         dpsoftrast.validate = 0;
2814 }
2815
2816 void DPSOFTRAST_Shutdown(void)
2817 {
2818         int i;
2819         for (i = 0;i < dpsoftrast.texture_end;i++)
2820                 if (dpsoftrast.texture[i].bytes)
2821                         free(dpsoftrast.texture[i].bytes);
2822         if (dpsoftrast.texture)
2823                 free(dpsoftrast.texture);
2824         memset(&dpsoftrast, 0, sizeof(dpsoftrast));
2825 }