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