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