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