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