3 #include "dpsoftrast.h"
10 typedef enum bool {false, true} bool;
13 #if defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1400)
14 #define RESTRICT __restrict
20 #define GL_FRONT_LEFT 0x0400
21 #define GL_FRONT_RIGHT 0x0401
22 #define GL_BACK_LEFT 0x0402
23 #define GL_BACK_RIGHT 0x0403
24 #define GL_FRONT 0x0404
25 #define GL_BACK 0x0405
26 #define GL_LEFT 0x0406
27 #define GL_RIGHT 0x0407
28 #define GL_FRONT_AND_BACK 0x0408
29 #define GL_AUX0 0x0409
30 #define GL_AUX1 0x040A
31 #define GL_AUX2 0x040B
32 #define GL_AUX3 0x040C
34 #define GL_NEVER 0x0200
35 #define GL_LESS 0x0201
36 #define GL_EQUAL 0x0202
37 #define GL_LEQUAL 0x0203
38 #define GL_GREATER 0x0204
39 #define GL_NOTEQUAL 0x0205
40 #define GL_GEQUAL 0x0206
41 #define GL_ALWAYS 0x0207
45 #define GL_SRC_COLOR 0x0300
46 #define GL_ONE_MINUS_SRC_COLOR 0x0301
47 #define GL_DST_COLOR 0x0306
48 #define GL_ONE_MINUS_DST_COLOR 0x0307
49 #define GL_SRC_ALPHA 0x0302
50 #define GL_ONE_MINUS_SRC_ALPHA 0x0303
51 #define GL_DST_ALPHA 0x0304
52 #define GL_ONE_MINUS_DST_ALPHA 0x0305
53 #define GL_SRC_ALPHA_SATURATE 0x0308
54 #define GL_CONSTANT_COLOR 0x8001
55 #define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
56 #define GL_CONSTANT_ALPHA 0x8003
57 #define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
59 typedef enum DPSOFTRAST_ARRAY_e
61 DPSOFTRAST_ARRAY_POSITION,
62 DPSOFTRAST_ARRAY_COLOR,
63 DPSOFTRAST_ARRAY_TEXCOORD0,
64 DPSOFTRAST_ARRAY_TEXCOORD1,
65 DPSOFTRAST_ARRAY_TEXCOORD2,
66 DPSOFTRAST_ARRAY_TEXCOORD3,
67 DPSOFTRAST_ARRAY_TEXCOORD4,
68 DPSOFTRAST_ARRAY_TEXCOORD5,
69 DPSOFTRAST_ARRAY_TEXCOORD6,
70 DPSOFTRAST_ARRAY_TEXCOORD7,
71 DPSOFTRAST_ARRAY_TOTAL
75 typedef struct DPSOFTRAST_Texture_s
82 DPSOFTRAST_TEXTURE_FILTER filter;
86 int mipmap[DPSOFTRAST_MAXMIPMAPS][5];
90 typedef struct DPSOFTRAST_State_User_s
106 float polygonoffset[2];
109 DPSOFTRAST_State_User;
111 #define DPSOFTRAST_MAXSUBSPAN 16
113 typedef struct DPSOFTRAST_State_Draw_Span_s
115 int start; // pixel index
116 int length; // pixel count
117 int startx; // usable range (according to pixelmask)
118 int endx; // usable range (according to pixelmask)
119 unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; // texcoord to screen space density values (for picking mipmap of textures)
120 unsigned char *pixelmask; // true for pixels that passed depth test, false for others
121 // [0][n][] is start interpolant values (projected)
122 // [1][n][] is end interpolant values (projected)
123 // [0][DPSOFTRAST_ARRAY_TOTAL][] is start screencoord4f
124 // [1][DPSOFTRAST_ARRAY_TOTAL][] is end screencoord4f
125 // NOTE: screencoord4f[3] is W (basically 1/Z), useful for depthbuffer
126 float data[2][DPSOFTRAST_ARRAY_TOTAL+1][4];
128 DPSOFTRAST_State_Draw_Span;
130 #define DPSOFTRAST_DRAW_MAXSPANQUEUE 1024
132 typedef struct DPSOFTRAST_State_Draw_s
136 float *in_array4f[DPSOFTRAST_ARRAY_TOTAL];
137 float *post_array4f[DPSOFTRAST_ARRAY_TOTAL];
138 float *screencoord4f;
140 // spans are queued in this structure for dispatch to the pixel shader,
141 // partly to improve cache locality, partly for batching purposes, spans
142 // are flushed before DrawTriangles returns to caller
144 DPSOFTRAST_State_Draw_Span spanqueue[DPSOFTRAST_DRAW_MAXSPANQUEUE];
146 DPSOFTRAST_State_Draw;
148 #define DPSOFTRAST_VALIDATE_FB 1
149 #define DPSOFTRAST_VALIDATE_DEPTHFUNC 2
150 #define DPSOFTRAST_VALIDATE_BLENDFUNC 4
151 #define DPSOFTRAST_VALIDATE_DRAW (DPSOFTRAST_VALIDATE_FB | DPSOFTRAST_VALIDATE_DEPTHFUNC | DPSOFTRAST_VALIDATE_BLENDFUNC)
153 typedef enum DPSOFTRAST_BLENDMODE_e
155 DPSOFTRAST_BLENDMODE_OPAQUE,
156 DPSOFTRAST_BLENDMODE_ALPHA,
157 DPSOFTRAST_BLENDMODE_ADDALPHA,
158 DPSOFTRAST_BLENDMODE_ADD,
159 DPSOFTRAST_BLENDMODE_INVMOD,
160 DPSOFTRAST_BLENDMODE_MUL,
161 DPSOFTRAST_BLENDMODE_MUL2,
162 DPSOFTRAST_BLENDMODE_SUBALPHA,
163 DPSOFTRAST_BLENDMODE_PSEUDOALPHA,
164 DPSOFTRAST_BLENDMODE_TOTAL
166 DPSOFTRAST_BLENDMODE;
168 typedef struct DPSOFTRAST_State_s
170 // DPSOFTRAST_VALIDATE_ flags
176 unsigned int *fb_depthpixels;
177 unsigned int *fb_colorpixels[4];
179 const float *pointer_vertex3f;
180 const float *pointer_color4f;
181 const unsigned char *pointer_color4ub;
182 const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
185 int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
186 int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
187 DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
190 int shader_permutation;
191 float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4];
192 int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
194 // derived values (DPSOFTRAST_VALIDATE_FB)
195 int fb_clearscissor[4];
197 int fb_viewportscissor[4];
198 float fb_viewportcenter[2];
199 float fb_viewportscale[2];
201 // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
204 // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
209 int texture_firstfree;
210 DPSOFTRAST_Texture *texture;
215 const char *errorstring;
217 DPSOFTRAST_State_User user;
219 DPSOFTRAST_State_Draw draw;
223 DPSOFTRAST_State dpsoftrast;
225 extern int dpsoftrast_test;
227 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
228 #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))
229 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
230 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
232 void DPSOFTRAST_RecalcFB(void)
234 // calculate framebuffer scissor, viewport, viewport clipped by scissor,
235 // and viewport projection values
236 int x1, x2, x3, x4, x5, x6;
237 int y1, y2, y3, y4, y5, y6;
238 x1 = dpsoftrast.user.scissor[0];
239 x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
240 x3 = dpsoftrast.user.viewport[0];
241 x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
242 y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
243 y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
244 y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
245 y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
246 if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
248 if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
250 if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
252 if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
254 if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
255 x5 = x1;if (x5 < x3) x5 = x3;
256 x6 = x2;if (x6 > x4) x4 = x4;
257 y5 = y1;if (y5 < y3) y5 = y3;
258 y6 = y2;if (y6 > y4) y6 = y4;
259 dpsoftrast.fb_clearscissor[0] = x1;
260 dpsoftrast.fb_clearscissor[1] = y1;
261 dpsoftrast.fb_clearscissor[2] = x2 - x1;
262 dpsoftrast.fb_clearscissor[3] = y2 - y1;
263 dpsoftrast.fb_viewport[0] = x3;
264 dpsoftrast.fb_viewport[1] = y3;
265 dpsoftrast.fb_viewport[2] = x4 - x3;
266 dpsoftrast.fb_viewport[3] = y4 - y3;
267 dpsoftrast.fb_viewportscissor[0] = x5;
268 dpsoftrast.fb_viewportscissor[1] = y5;
269 dpsoftrast.fb_viewportscissor[2] = x6 - x5;
270 dpsoftrast.fb_viewportscissor[3] = y6 - y5;
271 dpsoftrast.fb_viewportcenter[0] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
272 dpsoftrast.fb_viewportcenter[1] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
273 dpsoftrast.fb_viewportscale[0] = 0.5f * dpsoftrast.user.viewport[2];
274 dpsoftrast.fb_viewportscale[1] = -0.5f * dpsoftrast.user.viewport[3];
277 void DPSOFTRAST_RecalcDepthFunc(void)
279 dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
282 int blendmodetable[][4] =
284 {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
285 {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
286 {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
287 {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
288 {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
289 {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
290 {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
291 {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
292 {DPSOFTRAST_BLENDMODE_PSEUDOALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, false},
293 {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
296 void DPSOFTRAST_RecalcBlendFunc(void)
299 dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE;
300 for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++)
302 if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3])
304 dpsoftrast.fb_blendmode = blendmodetable[i][0];
310 #define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0)
312 void DPSOFTRAST_Validate(int mask)
314 mask &= dpsoftrast.validate;
317 if (mask & DPSOFTRAST_VALIDATE_FB)
319 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB;
320 DPSOFTRAST_RecalcFB();
322 if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
324 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
325 DPSOFTRAST_RecalcDepthFunc();
327 if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
329 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
330 DPSOFTRAST_RecalcBlendFunc();
334 DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
336 if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
337 return &dpsoftrast.texture[index];
341 int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
350 int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
351 int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
352 DPSOFTRAST_Texture *texture;
353 if (width*height*depth < 1)
355 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
358 if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
360 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
365 case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
366 case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
367 case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
369 case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
370 if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
372 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
377 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
380 if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
382 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
387 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
389 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
392 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
394 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
397 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
399 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
402 if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
404 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
407 if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
409 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
412 // find first empty slot in texture array
413 for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
414 if (!dpsoftrast.texture[texnum].bytes)
416 dpsoftrast.texture_firstfree = texnum + 1;
417 if (dpsoftrast.texture_max <= texnum)
419 // expand texture array as needed
420 if (dpsoftrast.texture_max < 1024)
421 dpsoftrast.texture_max = 1024;
423 dpsoftrast.texture_max *= 2;
424 dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
426 if (dpsoftrast.texture_end <= texnum)
427 dpsoftrast.texture_end = texnum + 1;
428 texture = &dpsoftrast.texture[texnum];
429 memset(texture, 0, sizeof(*texture));
430 texture->flags = flags;
431 texture->width = width;
432 texture->height = height;
433 texture->depth = depth;
434 texture->sides = sides;
445 s = w * h * d * sides * 4;
446 texture->mipmap[mipmaps][0] = size;
447 texture->mipmap[mipmaps][1] = s;
448 texture->mipmap[mipmaps][2] = w;
449 texture->mipmap[mipmaps][3] = h;
450 texture->mipmap[mipmaps][4] = d;
453 if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
459 texture->mipmaps = mipmaps;
460 texture->size = size;
462 // allocate the pixels now
463 texture->bytes = (unsigned char *)calloc(1, size);
467 void DPSOFTRAST_Texture_Free(int index)
469 DPSOFTRAST_Texture *texture;
470 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
472 free(texture->bytes);
473 texture->bytes = NULL;
474 memset(texture, 0, sizeof(*texture));
475 // adjust the free range and used range
476 if (dpsoftrast.texture_firstfree > index)
477 dpsoftrast.texture_firstfree = index;
478 while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
479 dpsoftrast.texture_end--;
481 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
483 int i, x, y, z, w, layer0, layer1, row0, row1;
484 unsigned char *o, *i0, *i1, *i2, *i3;
485 DPSOFTRAST_Texture *texture;
486 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
487 if (texture->mipmaps <= 1)
489 for (i = 1;i < texture->mipmaps;i++)
491 for (z = 0;z < texture->mipmap[i][4];z++)
495 if (layer1 >= texture->mipmap[i-1][4])
496 layer1 = texture->mipmap[i-1][4]-1;
497 for (y = 0;y < texture->mipmap[i][3];y++)
501 if (row1 >= texture->mipmap[i-1][3])
502 row1 = texture->mipmap[i-1][3]-1;
503 o = texture->bytes + texture->mipmap[i ][0] + 4*((texture->mipmap[i ][3] * z + y ) * texture->mipmap[i ][2]);
504 i0 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row0) * texture->mipmap[i-1][2]);
505 i1 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row1) * texture->mipmap[i-1][2]);
506 i2 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row0) * texture->mipmap[i-1][2]);
507 i3 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row1) * texture->mipmap[i-1][2]);
508 w = texture->mipmap[i][2];
511 if (texture->mipmap[i-1][2] > 1)
513 // average 3D texture
514 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8, i2 += 8, i3 += 8)
516 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
517 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
518 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
519 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
524 // average 3D mipmap with parent width == 1
525 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
527 o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
528 o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
529 o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
530 o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
536 if (texture->mipmap[i-1][2] > 1)
538 // average 2D texture (common case)
539 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
541 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
542 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
543 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
544 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
549 // 2D texture with parent width == 1
550 o[0] = (i0[0] + i1[0] + 1) >> 1;
551 o[1] = (i0[1] + i1[1] + 1) >> 1;
552 o[2] = (i0[2] + i1[2] + 1) >> 1;
553 o[3] = (i0[3] + i1[3] + 1) >> 1;
560 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
562 DPSOFTRAST_Texture *texture;
563 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
567 dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
569 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
571 DPSOFTRAST_Texture *texture;
572 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
574 memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
575 DPSOFTRAST_Texture_CalculateMipmaps(index);
577 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
579 DPSOFTRAST_Texture *texture;
580 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
581 return texture->width;
583 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
585 DPSOFTRAST_Texture *texture;
586 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
587 return texture->height;
589 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
591 DPSOFTRAST_Texture *texture;
592 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
593 return texture->depth;
595 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
597 DPSOFTRAST_Texture *texture;
598 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
599 return texture->bytes;
601 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
603 DPSOFTRAST_Texture *texture;
604 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
605 if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
607 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
610 texture->filter = filter;
613 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
615 dpsoftrast.fb_width = width;
616 dpsoftrast.fb_height = height;
617 dpsoftrast.fb_depthpixels = depthpixels;
618 dpsoftrast.fb_colorpixels[0] = colorpixels0;
619 dpsoftrast.fb_colorpixels[1] = colorpixels1;
620 dpsoftrast.fb_colorpixels[2] = colorpixels2;
621 dpsoftrast.fb_colorpixels[3] = colorpixels3;
623 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
625 dpsoftrast.user.viewport[0] = x;
626 dpsoftrast.user.viewport[1] = y;
627 dpsoftrast.user.viewport[2] = width;
628 dpsoftrast.user.viewport[3] = height;
629 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
631 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
633 int i, x1, y1, x2, y2, w, h, x, y;
636 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
637 x1 = dpsoftrast.fb_clearscissor[0];
638 y1 = dpsoftrast.fb_clearscissor[1];
639 x2 = dpsoftrast.fb_clearscissor[2];
640 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
645 // FIXME: honor dpsoftrast.fb_colormask?
646 c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
647 for (i = 0;i < 4;i++)
649 if (!dpsoftrast.fb_colorpixels[i])
651 for (y = y1;y < y2;y++)
653 p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
654 for (x = x1;x < x2;x++)
659 void DPSOFTRAST_ClearDepth(float d)
661 int x1, y1, x2, y2, w, h, x, y;
664 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
665 x1 = dpsoftrast.fb_clearscissor[0];
666 y1 = dpsoftrast.fb_clearscissor[1];
667 x2 = dpsoftrast.fb_clearscissor[2];
668 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
673 c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
674 for (y = y1;y < y2;y++)
676 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
677 for (x = x1;x < x2;x++)
681 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
683 dpsoftrast.user.colormask[0] = r != 0;
684 dpsoftrast.user.colormask[1] = g != 0;
685 dpsoftrast.user.colormask[2] = b != 0;
686 dpsoftrast.user.colormask[3] = a != 0;
687 dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
689 void DPSOFTRAST_DepthTest(int enable)
691 dpsoftrast.user.depthtest = enable;
692 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
694 void DPSOFTRAST_ScissorTest(int enable)
696 dpsoftrast.user.scissortest = enable;
697 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
699 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
701 dpsoftrast.user.scissor[0] = x;
702 dpsoftrast.user.scissor[1] = y;
703 dpsoftrast.user.scissor[2] = width;
704 dpsoftrast.user.scissor[3] = height;
705 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
708 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
711 dpsoftrast.user.blendfunc[0] = smodulate;
712 dpsoftrast.user.blendfunc[1] = dmodulate;
713 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
715 void DPSOFTRAST_BlendSubtract(int enable)
717 dpsoftrast.user.blendsubtract = enable != 0;
718 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
720 void DPSOFTRAST_DepthMask(int enable)
722 dpsoftrast.user.depthmask = enable;
724 void DPSOFTRAST_DepthFunc(int comparemode)
727 dpsoftrast.user.depthfunc = comparemode;
729 void DPSOFTRAST_DepthRange(float range0, float range1)
731 dpsoftrast.user.depthrange[0] = range0;
732 dpsoftrast.user.depthrange[1] = range1;
734 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
736 dpsoftrast.user.polygonoffset[0] = alongnormal;
737 dpsoftrast.user.polygonoffset[1] = intoview;
739 void DPSOFTRAST_CullFace(int mode)
742 dpsoftrast.user.cullface = mode;
744 void DPSOFTRAST_AlphaTest(float enable)
746 dpsoftrast.user.alphatest = enable;
748 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
751 dpsoftrast.user.alphafunc = alphafunc;
752 dpsoftrast.user.alphavalue = alphavalue;
754 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
756 dpsoftrast.user.color[0] = r;
757 dpsoftrast.user.color[1] = g;
758 dpsoftrast.user.color[2] = b;
759 dpsoftrast.user.color[3] = a;
761 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
763 int outstride = blockwidth * 4;
764 int instride = dpsoftrast.fb_width * 4;
767 int bx2 = blockx + blockwidth;
768 int by2 = blocky + blockheight;
773 unsigned char *inpixels;
776 if (bx1 < 0) bx1 = 0;
777 if (by1 < 0) by1 = 0;
778 if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
779 if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
782 inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
783 if (dpsoftrast.bigendian)
785 for (y = by1;y < by2;y++)
787 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
788 o = (unsigned char *)outpixels + (y - by1) * outstride;
789 for (x = bx1;x < bx2;x++)
802 for (y = by1;y < by2;y++)
804 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
805 o = (unsigned char *)outpixels + (y - by1) * outstride;
811 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
815 int tx2 = tx + width;
816 int ty2 = ty + height;
819 int sx2 = sx + width;
820 int sy2 = sy + height;
830 unsigned int *spixels;
831 unsigned int *tpixels;
832 DPSOFTRAST_Texture *texture;
833 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
834 if (mip < 0 || mip >= texture->mipmaps) return;
835 spixels = dpsoftrast.fb_colorpixels[0];
836 swidth = dpsoftrast.fb_width;
837 sheight = dpsoftrast.fb_height;
838 tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
839 twidth = texture->mipmap[mip][2];
840 theight = texture->mipmap[mip][3];
841 if (tx1 < 0) tx1 = 0;
842 if (ty1 < 0) ty1 = 0;
843 if (tx2 > twidth) tx2 = twidth;
844 if (ty2 > theight) ty2 = theight;
845 if (sx1 < 0) sx1 = 0;
846 if (sy1 < 0) sy1 = 0;
847 if (sx2 > swidth) sx2 = swidth;
848 if (sy2 > sheight) sy2 = sheight;
853 if (tw > sw) tw = sw;
854 if (th > sh) th = sh;
855 if (tw < 1 || th < 1)
857 for (y = 0;y < th;y++)
858 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
859 if (texture->mipmaps > 1)
860 DPSOFTRAST_Texture_CalculateMipmaps(index);
862 void DPSOFTRAST_SetTexture(int unitnum, int index)
864 DPSOFTRAST_Texture *texture;
865 if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
867 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
870 texture = DPSOFTRAST_Texture_GetByIndex(index);
871 if (index && !texture)
873 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
876 dpsoftrast.texbound[unitnum] = texture;
879 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
881 dpsoftrast.pointer_vertex3f = vertex3f;
882 dpsoftrast.stride_vertex = stride;
884 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
886 dpsoftrast.pointer_color4f = color4f;
887 dpsoftrast.pointer_color4ub = NULL;
888 dpsoftrast.stride_color = stride;
890 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
892 dpsoftrast.pointer_color4f = NULL;
893 dpsoftrast.pointer_color4ub = color4ub;
894 dpsoftrast.stride_color = stride;
896 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
898 dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
899 dpsoftrast.components_texcoord[unitnum] = numcomponents;
900 dpsoftrast.stride_texcoord[unitnum] = stride;
903 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
905 dpsoftrast.shader_mode = mode;
906 dpsoftrast.shader_permutation = permutation;
908 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
910 dpsoftrast.uniform4f[index*4+0] = v0;
911 dpsoftrast.uniform4f[index*4+1] = v1;
912 dpsoftrast.uniform4f[index*4+2] = v2;
913 dpsoftrast.uniform4f[index*4+3] = v3;
915 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v)
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];
922 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v)
924 dpsoftrast.uniform4f[index*4+0] = v[0];
925 dpsoftrast.uniform4f[index*4+1] = v[1];
926 dpsoftrast.uniform4f[index*4+2] = v[2];
927 dpsoftrast.uniform4f[index*4+3] = v[3];
928 dpsoftrast.uniform4f[index*4+4] = v[4];
929 dpsoftrast.uniform4f[index*4+5] = v[5];
930 dpsoftrast.uniform4f[index*4+6] = v[6];
931 dpsoftrast.uniform4f[index*4+7] = v[7];
932 dpsoftrast.uniform4f[index*4+8] = v[8];
933 dpsoftrast.uniform4f[index*4+9] = v[9];
934 dpsoftrast.uniform4f[index*4+10] = v[10];
935 dpsoftrast.uniform4f[index*4+11] = v[11];
936 dpsoftrast.uniform4f[index*4+12] = v[12];
937 dpsoftrast.uniform4f[index*4+13] = v[13];
938 dpsoftrast.uniform4f[index*4+14] = v[14];
939 dpsoftrast.uniform4f[index*4+15] = v[15];
941 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
943 dpsoftrast.uniform1i[index] = i0;
946 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors)
954 const unsigned char *b;
955 dpsoftrast.draw.numvertices = numvertices;
956 if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
958 if (dpsoftrast.draw.maxvertices < 4096)
959 dpsoftrast.draw.maxvertices = 4096;
960 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
961 dpsoftrast.draw.maxvertices *= 2;
962 if (dpsoftrast.draw.in_array4f[0])
963 free(dpsoftrast.draw.in_array4f[0]);
964 data = (float *)calloc(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
965 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
966 dpsoftrast.draw.in_array4f[i] = data;
967 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
968 dpsoftrast.draw.post_array4f[i] = data;
969 dpsoftrast.draw.screencoord4f = data;
970 data += dpsoftrast.draw.maxvertices * 4;
972 stride = dpsoftrast.stride_vertex;
973 v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
974 p = dpsoftrast.draw.in_array4f[0];
975 for (i = 0;i < numvertices;i++)
982 v = (const float *)((const unsigned char *)v + stride);
986 if (dpsoftrast.pointer_color4f)
988 stride = dpsoftrast.stride_color;
989 v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
990 p = dpsoftrast.draw.in_array4f[1];
991 for (i = 0;i < numvertices;i++)
998 v = (const float *)((const unsigned char *)v + stride);
1001 else if (dpsoftrast.pointer_color4ub)
1003 stride = dpsoftrast.stride_color;
1004 b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
1005 p = dpsoftrast.draw.in_array4f[1];
1006 for (i = 0;i < numvertices;i++)
1008 p[0] = b[0] * (1.0f / 255.0f);
1009 p[1] = b[1] * (1.0f / 255.0f);
1010 p[2] = b[2] * (1.0f / 255.0f);
1011 p[3] = b[3] * (1.0f / 255.0f);
1013 b = (const unsigned char *)((const unsigned char *)b + stride);
1018 v = dpsoftrast.user.color;
1019 p = dpsoftrast.draw.in_array4f[1];
1020 for (i = 0;i < numvertices;i++)
1030 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL-2;j++)
1032 if (dpsoftrast.pointer_texcoordf[j])
1034 stride = dpsoftrast.stride_texcoord[j];
1035 v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1036 p = dpsoftrast.draw.in_array4f[j+2];
1037 switch(dpsoftrast.components_texcoord[j])
1040 for (i = 0;i < numvertices;i++)
1047 v = (const float *)((const unsigned char *)v + stride);
1051 for (i = 0;i < numvertices;i++)
1058 v = (const float *)((const unsigned char *)v + stride);
1062 for (i = 0;i < numvertices;i++)
1069 v = (const float *)((const unsigned char *)v + stride);
1077 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1079 static const float identitymatrix[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
1083 memcpy(matrix, inmatrix16f, sizeof(float[16]));
1084 if (!memcmp(identitymatrix, matrix, sizeof(float[16])))
1086 // fast case for identity matrix
1087 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1090 for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1092 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1093 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1094 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1095 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1099 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1101 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1104 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1106 // NOTE: this is used both as a whole mesh transform function and a
1107 // per-triangle transform function (for clipped triangles), accordingly
1108 // it should not crash on divide by 0 but the result of divide by 0 is
1113 float viewportcenter[4];
1114 float viewportscale[4];
1115 viewportscale[0] = dpsoftrast.fb_viewportscale[0];
1116 viewportscale[1] = dpsoftrast.fb_viewportscale[1];
1117 viewportscale[2] = 0.5f;
1118 viewportscale[3] = 0.0f;
1119 viewportcenter[0] = dpsoftrast.fb_viewportcenter[0];
1120 viewportcenter[1] = dpsoftrast.fb_viewportcenter[1];
1121 viewportcenter[2] = 0.5f;
1122 viewportcenter[3] = 0.0f;
1123 for (i = 0;i < numitems;i++)
1134 out4f[0] = viewportcenter[0] + viewportscale[0] * in4f[0] * w;
1135 out4f[1] = viewportcenter[1] + viewportscale[1] * in4f[1] * w;
1136 out4f[2] = viewportcenter[2] + viewportscale[2] * in4f[2] * w;
1137 out4f[3] = viewportcenter[3] + viewportscale[3] * in4f[3] * w;
1144 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1149 int w = dpsoftrast.fb_width;
1152 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1154 bounds[0] = dpsoftrast.fb_viewportscissor[0];
1155 bounds[1] = dpsoftrast.fb_viewportscissor[1];
1156 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1157 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1162 for (i = 0;i <= 128;i++)
1165 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1167 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1168 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1169 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1171 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1172 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1173 pixels[y*w+x] = 0xFFFFFFFF;
1177 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *zf)
1180 int startx = span->startx;
1181 int endx = span->endx;
1182 float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1183 float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1184 for (x = startx;x < endx;)
1186 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1187 float z = 1.0f / (w + wslope * x), dz;
1191 dz = endsub > x ? (1.0f / (w + wslope * endsub) - z) / (endsub - x) : 0.0f;
1195 dz = (1.0f / (w + wslope * endsub) - z) * (1.0f / (DPSOFTRAST_MAXSUBSPAN-1));
1197 for (; x <= endsub; x++, z += dz)
1202 void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const float * RESTRICT in4f)
1205 int startx = span->startx;
1206 int endx = span->endx;
1209 unsigned char * RESTRICT pixelmask = span->pixelmask;
1210 unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1213 pixel += span->start * 4;
1214 // handle alphatest now (this affects depth writes too)
1215 if (dpsoftrast.user.alphatest)
1216 for (x = startx;x < endx;x++)
1217 if (in4f[x*4+3] < 0.5f)
1218 pixelmask[x] = false;
1219 // FIXME: this does not handle bigendian
1220 switch(dpsoftrast.fb_blendmode)
1222 case DPSOFTRAST_BLENDMODE_OPAQUE:
1223 for (x = startx;x < endx;x++)
1227 d[0] = (int)(in4f[x*4+2]*255.0f);if (d[0] > 255) d[0] = 255;
1228 d[1] = (int)(in4f[x*4+1]*255.0f);if (d[1] > 255) d[1] = 255;
1229 d[2] = (int)(in4f[x*4+0]*255.0f);if (d[2] > 255) d[2] = 255;
1230 d[3] = (int)(in4f[x*4+3]*255.0f);if (d[3] > 255) d[3] = 255;
1231 pixel[x*4+0] = d[0];
1232 pixel[x*4+1] = d[1];
1233 pixel[x*4+2] = d[2];
1234 pixel[x*4+3] = d[3];
1237 case DPSOFTRAST_BLENDMODE_ALPHA:
1238 for (x = startx;x < endx;x++)
1242 a = in4f[x*4+3] * 255.0f;
1243 b = 1.0f - in4f[x*4+3];
1244 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1245 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1246 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1247 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1248 pixel[x*4+0] = d[0];
1249 pixel[x*4+1] = d[1];
1250 pixel[x*4+2] = d[2];
1251 pixel[x*4+3] = d[3];
1254 case DPSOFTRAST_BLENDMODE_ADDALPHA:
1255 for (x = startx;x < endx;x++)
1259 a = in4f[x*4+3] * 255.0f;
1260 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1261 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1262 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1263 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1264 pixel[x*4+0] = d[0];
1265 pixel[x*4+1] = d[1];
1266 pixel[x*4+2] = d[2];
1267 pixel[x*4+3] = d[3];
1270 case DPSOFTRAST_BLENDMODE_ADD:
1271 for (x = startx;x < endx;x++)
1275 d[0] = (int)(in4f[x*4+2]*255.0f+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1276 d[1] = (int)(in4f[x*4+1]*255.0f+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1277 d[2] = (int)(in4f[x*4+0]*255.0f+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1278 d[3] = (int)(in4f[x*4+3]*255.0f+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1279 pixel[x*4+0] = d[0];
1280 pixel[x*4+1] = d[1];
1281 pixel[x*4+2] = d[2];
1282 pixel[x*4+3] = d[3];
1285 case DPSOFTRAST_BLENDMODE_INVMOD:
1286 for (x = startx;x < endx;x++)
1290 d[0] = (int)((1.0f-in4f[x*4+2])*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1291 d[1] = (int)((1.0f-in4f[x*4+1])*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1292 d[2] = (int)((1.0f-in4f[x*4+0])*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1293 d[3] = (int)((1.0f-in4f[x*4+3])*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1294 pixel[x*4+0] = d[0];
1295 pixel[x*4+1] = d[1];
1296 pixel[x*4+2] = d[2];
1297 pixel[x*4+3] = d[3];
1300 case DPSOFTRAST_BLENDMODE_MUL:
1301 for (x = startx;x < endx;x++)
1305 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1306 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1307 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1308 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1309 pixel[x*4+0] = d[0];
1310 pixel[x*4+1] = d[1];
1311 pixel[x*4+2] = d[2];
1312 pixel[x*4+3] = d[3];
1315 case DPSOFTRAST_BLENDMODE_MUL2:
1316 for (x = startx;x < endx;x++)
1320 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]*2.0f);if (d[0] > 255) d[0] = 255;
1321 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]*2.0f);if (d[1] > 255) d[1] = 255;
1322 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]*2.0f);if (d[2] > 255) d[2] = 255;
1323 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]*2.0f);if (d[3] > 255) d[3] = 255;
1324 pixel[x*4+0] = d[0];
1325 pixel[x*4+1] = d[1];
1326 pixel[x*4+2] = d[2];
1327 pixel[x*4+3] = d[3];
1330 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1331 for (x = startx;x < endx;x++)
1335 a = in4f[x*4+3] * -255.0f;
1336 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;
1337 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;
1338 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;
1339 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;
1340 pixel[x*4+0] = d[0];
1341 pixel[x*4+1] = d[1];
1342 pixel[x*4+2] = d[2];
1343 pixel[x*4+3] = d[3];
1346 case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1347 for (x = startx;x < endx;x++)
1352 b = 1.0f - in4f[x*4+3];
1353 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1354 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1355 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1356 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1357 pixel[x*4+0] = d[0];
1358 pixel[x*4+1] = d[1];
1359 pixel[x*4+2] = d[2];
1360 pixel[x*4+3] = d[3];
1366 void DPSOFTRAST_Draw_Span_FinishBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const unsigned char* RESTRICT in4ub)
1369 int startx = span->startx;
1370 int endx = span->endx;
1372 const unsigned int * RESTRICT ini = (const unsigned int *)in4ub;
1374 unsigned char * RESTRICT pixelmask = span->pixelmask;
1375 unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1376 unsigned int * RESTRICT pixeli = (unsigned int *)dpsoftrast.fb_colorpixels[0];
1379 pixel += span->start * 4;
1380 pixeli += span->start;
1381 // handle alphatest now (this affects depth writes too)
1382 if (dpsoftrast.user.alphatest)
1383 for (x = startx;x < endx;x++)
1384 if (in4ub[x*4+3] < 0.5f)
1385 pixelmask[x] = false;
1386 // FIXME: this does not handle bigendian
1387 switch(dpsoftrast.fb_blendmode)
1389 case DPSOFTRAST_BLENDMODE_OPAQUE:
1390 for (x = startx;x < endx;x++)
1394 case DPSOFTRAST_BLENDMODE_ALPHA:
1395 for (x = startx;x < endx;x++)
1400 b = 256 - in4ub[x*4+3];
1401 pixel[x*4+0] = (in4ub[x*4+0]*a+pixel[x*4+0]*b) >> 8;
1402 pixel[x*4+1] = (in4ub[x*4+1]*a+pixel[x*4+1]*b) >> 8;
1403 pixel[x*4+2] = (in4ub[x*4+2]*a+pixel[x*4+2]*b) >> 8;
1404 pixel[x*4+3] = (in4ub[x*4+3]*a+pixel[x*4+3]*b) >> 8;
1407 case DPSOFTRAST_BLENDMODE_ADDALPHA:
1408 for (x = startx;x < endx;x++)
1413 d[0] = (((in4ub[x*4+0]*a)>>8)+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1414 d[1] = (((in4ub[x*4+1]*a)>>8)+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1415 d[2] = (((in4ub[x*4+2]*a)>>8)+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1416 d[3] = (((in4ub[x*4+3]*a)>>8)+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1417 pixel[x*4+0] = d[0];
1418 pixel[x*4+1] = d[1];
1419 pixel[x*4+2] = d[2];
1420 pixel[x*4+3] = d[3];
1423 case DPSOFTRAST_BLENDMODE_ADD:
1424 for (x = startx;x < endx;x++)
1428 d[0] = (in4ub[x*4+0]+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1429 d[1] = (in4ub[x*4+1]+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1430 d[2] = (in4ub[x*4+2]+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1431 d[3] = (in4ub[x*4+3]+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1432 pixel[x*4+0] = d[0];
1433 pixel[x*4+1] = d[1];
1434 pixel[x*4+2] = d[2];
1435 pixel[x*4+3] = d[3];
1438 case DPSOFTRAST_BLENDMODE_INVMOD:
1439 for (x = startx;x < endx;x++)
1443 pixel[x*4+0] = ((255-in4ub[x*4+0])*pixel[x*4+0])>>8;
1444 pixel[x*4+1] = ((255-in4ub[x*4+1])*pixel[x*4+1])>>8;
1445 pixel[x*4+2] = ((255-in4ub[x*4+2])*pixel[x*4+2])>>8;
1446 pixel[x*4+3] = ((255-in4ub[x*4+3])*pixel[x*4+3])>>8;
1449 case DPSOFTRAST_BLENDMODE_MUL:
1450 for (x = startx;x < endx;x++)
1454 pixel[x*4+0] = (in4ub[x*4+0]*pixel[x*4+0])>>8;
1455 pixel[x*4+1] = (in4ub[x*4+1]*pixel[x*4+1])>>8;
1456 pixel[x*4+2] = (in4ub[x*4+2]*pixel[x*4+2])>>8;
1457 pixel[x*4+3] = (in4ub[x*4+3]*pixel[x*4+3])>>8;
1460 case DPSOFTRAST_BLENDMODE_MUL2:
1461 for (x = startx;x < endx;x++)
1465 d[0] = (in4ub[x*4+0]*pixel[x*4+0])>>7;if (d[0] > 255) d[0] = 255;
1466 d[1] = (in4ub[x*4+1]*pixel[x*4+1])>>7;if (d[1] > 255) d[1] = 255;
1467 d[2] = (in4ub[x*4+2]*pixel[x*4+2])>>7;if (d[2] > 255) d[2] = 255;
1468 d[3] = (in4ub[x*4+3]*pixel[x*4+3])>>7;if (d[3] > 255) d[3] = 255;
1469 pixel[x*4+0] = d[0];
1470 pixel[x*4+1] = d[1];
1471 pixel[x*4+2] = d[2];
1472 pixel[x*4+3] = d[3];
1475 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1476 for (x = startx;x < endx;x++)
1481 d[0] = pixel[x*4+0]-((in4ub[x*4+0]*a)>>8);if (d[0] < 0) d[0] = 0;
1482 d[1] = pixel[x*4+1]-((in4ub[x*4+1]*a)>>8);if (d[1] < 0) d[1] = 0;
1483 d[2] = pixel[x*4+2]-((in4ub[x*4+2]*a)>>8);if (d[2] < 0) d[2] = 0;
1484 d[3] = pixel[x*4+3]-((in4ub[x*4+3]*a)>>8);if (d[3] < 0) d[3] = 0;
1485 pixel[x*4+0] = d[0];
1486 pixel[x*4+1] = d[1];
1487 pixel[x*4+2] = d[2];
1488 pixel[x*4+3] = d[3];
1491 case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1492 for (x = startx;x < endx;x++)
1496 b = 255 - in4ub[x*4+3];
1497 d[0] = in4ub[x*4+0]+((pixel[x*4+0]*b)>>8);if (d[0] > 255) d[0] = 255;
1498 d[1] = in4ub[x*4+1]+((pixel[x*4+1]*b)>>8);if (d[1] > 255) d[1] = 255;
1499 d[2] = in4ub[x*4+2]+((pixel[x*4+2]*b)>>8);if (d[2] > 255) d[2] = 255;
1500 d[3] = in4ub[x*4+3]+((pixel[x*4+3]*b)>>8);if (d[3] > 255) d[3] = 255;
1501 pixel[x*4+0] = d[0];
1502 pixel[x*4+1] = d[1];
1503 pixel[x*4+2] = d[2];
1504 pixel[x*4+3] = d[3];
1510 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float * RESTRICT out4f, int texunitindex, int arrayindex, const float * RESTRICT zf)
1513 int startx = span->startx;
1514 int endx = span->endx;
1521 unsigned int tci[2];
1522 unsigned int tci1[2];
1523 unsigned int tcimin[2];
1524 unsigned int tcimax[2];
1529 const unsigned char * RESTRICT pixelbase;
1530 const unsigned char * RESTRICT pixel[4];
1531 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1532 // if no texture is bound, just fill it with white
1535 for (x = startx;x < endx;x++)
1537 out4f[x*4+0] = 1.0f;
1538 out4f[x*4+1] = 1.0f;
1539 out4f[x*4+2] = 1.0f;
1540 out4f[x*4+3] = 1.0f;
1544 mip = span->mip[texunitindex];
1545 // if this mipmap of the texture is 1 pixel, just fill it with that color
1546 if (texture->mipmap[mip][1] == 4)
1548 c[0] = texture->bytes[2] * (1.0f/255.0f);
1549 c[1] = texture->bytes[1] * (1.0f/255.0f);
1550 c[2] = texture->bytes[0] * (1.0f/255.0f);
1551 c[3] = texture->bytes[3] * (1.0f/255.0f);
1552 for (x = startx;x < endx;x++)
1554 out4f[x*4+0] = c[0];
1555 out4f[x*4+1] = c[1];
1556 out4f[x*4+2] = c[2];
1557 out4f[x*4+3] = c[3];
1561 filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1562 data[0] = span->data[0][arrayindex][0];
1563 data[1] = span->data[0][arrayindex][1];
1564 data[2] = span->data[0][arrayindex][2];
1565 data[3] = span->data[0][arrayindex][3];
1566 slope[0] = span->data[1][arrayindex][0];
1567 slope[1] = span->data[1][arrayindex][1];
1568 slope[2] = span->data[1][arrayindex][2];
1569 slope[3] = span->data[1][arrayindex][3];
1570 flags = texture->flags;
1571 pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
1572 tcscale[0] = texture->mipmap[mip][2];
1573 tcscale[1] = texture->mipmap[mip][3];
1574 tciwidth = texture->mipmap[mip][2];
1577 tcimax[0] = texture->mipmap[mip][2]-1;
1578 tcimax[1] = texture->mipmap[mip][3]-1;
1579 tciwrapmask[0] = texture->mipmap[mip][2]-1;
1580 tciwrapmask[1] = texture->mipmap[mip][3]-1;
1581 for (x = startx;x < endx;)
1584 unsigned int subtc[2];
1585 unsigned int substep[2];
1586 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1587 float subscale = 4096.0f/(DPSOFTRAST_MAXSUBSPAN-1);
1591 subscale = endsub > x ? 4096.0f / (endsub - x) : 1.0f;
1593 tc[0] = (data[0] + slope[0]*x) * zf[x] * tcscale[0] - 0.5f;
1594 tc[1] = (data[1] + slope[1]*x) * zf[x] * tcscale[1] - 0.5f;
1595 endtc[0] = (data[0] + slope[0]*endsub) * zf[endsub] * tcscale[0] - 0.5f;
1596 endtc[1] = (data[1] + slope[1]*endsub) * zf[endsub] * tcscale[1] - 0.5f;
1597 substep[0] = (endtc[0] - tc[0]) * subscale;
1598 substep[1] = (endtc[1] - tc[1]) * subscale;
1599 subtc[0] = tc[0] * (1<<12);
1600 subtc[1] = tc[1] * (1<<12);
1601 if (!(flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE))
1603 subtc[0] &= (tciwrapmask[0]<<12)|0xFFF;
1604 subtc[1] &= (tciwrapmask[1]<<12)|0xFFF;
1608 tci[0] = (subtc[0]>>12) - tcimin[0];
1609 tci[1] = (subtc[1]>>12) - tcimin[0];
1610 tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12) + 1;
1611 tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12) + 1;
1612 if (tci[0] <= tcimax[0] && tci[1] <= tcimax[1] && tci1[0] <= tcimax[0] && tci1[1] <= tcimax[1])
1614 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1616 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1617 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1618 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1619 tci[0] = subtc[0]>>12;
1620 tci[1] = subtc[1]>>12;
1621 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1622 pixel[1] = pixel[0] + 4 * tciwidth;
1623 c[0] = (pixel[0][2]*lerp[0]+pixel[0][4+2]*lerp[1]+pixel[1][2]*lerp[2]+pixel[1][4+2]*lerp[3]) * (1.0f / 0xFF000000);
1624 c[1] = (pixel[0][1]*lerp[0]+pixel[0][4+1]*lerp[1]+pixel[1][1]*lerp[2]+pixel[1][4+1]*lerp[3]) * (1.0f / 0xFF000000);
1625 c[2] = (pixel[0][0]*lerp[0]+pixel[0][4+0]*lerp[1]+pixel[1][0]*lerp[2]+pixel[1][4+0]*lerp[3]) * (1.0f / 0xFF000000);
1626 c[3] = (pixel[0][3]*lerp[0]+pixel[0][4+3]*lerp[1]+pixel[1][3]*lerp[2]+pixel[1][4+3]*lerp[3]) * (1.0f / 0xFF000000);
1627 out4f[x*4+0] = c[0];
1628 out4f[x*4+1] = c[1];
1629 out4f[x*4+2] = c[2];
1630 out4f[x*4+3] = c[3];
1633 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1635 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1637 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1638 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1639 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1640 tci[0] = subtc[0]>>12;
1641 tci[1] = subtc[1]>>12;
1642 tci1[0] = tci[0] + 1;
1643 tci1[1] = tci[1] + 1;
1644 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1645 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1646 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1647 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1648 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1649 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1650 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1651 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1652 c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF000000);
1653 c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF000000);
1654 c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF000000);
1655 c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF000000);
1656 out4f[x*4+0] = c[0];
1657 out4f[x*4+1] = c[1];
1658 out4f[x*4+2] = c[2];
1659 out4f[x*4+3] = c[3];
1664 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1666 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1667 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1668 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1669 tci[0] = subtc[0]>>12;
1670 tci[1] = subtc[1]>>12;
1671 tci1[0] = tci[0] + 1;
1672 tci1[1] = tci[1] + 1;
1673 tci[0] &= tciwrapmask[0];
1674 tci[1] &= tciwrapmask[1];
1675 tci1[0] &= tciwrapmask[0];
1676 tci1[1] &= tciwrapmask[1];
1677 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1678 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1679 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1680 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1681 c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF000000);
1682 c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF000000);
1683 c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF000000);
1684 c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF000000);
1685 out4f[x*4+0] = c[0];
1686 out4f[x*4+1] = c[1];
1687 out4f[x*4+2] = c[2];
1688 out4f[x*4+3] = c[3];
1692 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1694 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1696 tci[0] = subtc[0]>>12;
1697 tci[1] = subtc[1]>>12;
1698 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1699 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1700 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1701 c[0] = pixel[0][2] * (1.0f / 255.0f);
1702 c[1] = pixel[0][1] * (1.0f / 255.0f);
1703 c[2] = pixel[0][0] * (1.0f / 255.0f);
1704 c[3] = pixel[0][3] * (1.0f / 255.0f);
1705 out4f[x*4+0] = c[0];
1706 out4f[x*4+1] = c[1];
1707 out4f[x*4+2] = c[2];
1708 out4f[x*4+3] = c[3];
1713 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1715 tci[0] = subtc[0]>>12;
1716 tci[1] = subtc[1]>>12;
1717 tci[0] &= tciwrapmask[0];
1718 tci[1] &= tciwrapmask[1];
1719 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1720 c[0] = pixel[0][2] * (1.0f / 255.0f);
1721 c[1] = pixel[0][1] * (1.0f / 255.0f);
1722 c[2] = pixel[0][0] * (1.0f / 255.0f);
1723 c[3] = pixel[0][3] * (1.0f / 255.0f);
1724 out4f[x*4+0] = c[0];
1725 out4f[x*4+1] = c[1];
1726 out4f[x*4+2] = c[2];
1727 out4f[x*4+3] = c[3];
1733 void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf)
1736 int startx = span->startx;
1737 int endx = span->endx;
1743 unsigned int tci[2];
1744 unsigned int tci1[2];
1745 unsigned int tcimin[2];
1746 unsigned int tcimax[2];
1752 unsigned int *outi = (unsigned int *)out4ub;
1753 const unsigned char * RESTRICT pixelbase;
1754 const unsigned int * RESTRICT pixelbasei;
1755 const unsigned char * RESTRICT pixel[4];
1756 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1757 // if no texture is bound, just fill it with white
1760 memset(out4ub, 255, span->length*4);
1763 mip = span->mip[texunitindex];
1764 // if this mipmap of the texture is 1 pixel, just fill it with that color
1765 if (texture->mipmap[mip][1] == 4)
1767 k = *((const unsigned int *)texture->bytes);
1768 for (x = startx;x < endx;x++)
1772 filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1773 data[0] = span->data[0][arrayindex][0];
1774 data[1] = span->data[0][arrayindex][1];
1775 data[2] = span->data[0][arrayindex][2];
1776 data[3] = span->data[0][arrayindex][3];
1777 slope[0] = span->data[1][arrayindex][0];
1778 slope[1] = span->data[1][arrayindex][1];
1779 slope[2] = span->data[1][arrayindex][2];
1780 slope[3] = span->data[1][arrayindex][3];
1781 flags = texture->flags;
1782 pixelbase = (const unsigned char *)texture->bytes + texture->mipmap[mip][0];
1783 pixelbasei = (const unsigned int *)pixelbase;
1784 tcscale[0] = texture->mipmap[mip][2];
1785 tcscale[1] = texture->mipmap[mip][3];
1786 tciwidth = texture->mipmap[mip][2];
1789 tcimax[0] = texture->mipmap[mip][2]-1;
1790 tcimax[1] = texture->mipmap[mip][3]-1;
1791 tciwrapmask[0] = texture->mipmap[mip][2]-1;
1792 tciwrapmask[1] = texture->mipmap[mip][3]-1;
1793 for (x = startx;x < endx;)
1796 unsigned int subtc[2];
1797 unsigned int substep[2];
1798 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1799 float subscale = 4096.0f/(DPSOFTRAST_MAXSUBSPAN-1);
1803 subscale = endsub > x ? 4096.0f / (endsub - x) : 1.0f;
1805 tc[0] = (data[0] + slope[0]*x) * zf[x] * tcscale[0] - 0.5f;
1806 tc[1] = (data[1] + slope[1]*x) * zf[x] * tcscale[1] - 0.5f;
1807 endtc[0] = (data[0] + slope[0]*endsub) * zf[endsub] * tcscale[0] - 0.5f;
1808 endtc[1] = (data[1] + slope[1]*endsub) * zf[endsub] * tcscale[1] - 0.5f;
1809 substep[0] = (endtc[0] - tc[0]) * subscale;
1810 substep[1] = (endtc[1] - tc[1]) * subscale;
1811 subtc[0] = tc[0] * (1<<12);
1812 subtc[1] = tc[1] * (1<<12);
1813 if (!(flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE))
1815 subtc[0] &= (tciwrapmask[0]<<12)|0xFFF;
1816 subtc[1] &= (tciwrapmask[1]<<12)|0xFFF;
1819 // LordHavoc: an attempt at reducing number of integer multiplies, did not show any improvement in benchmarks, abandoned.
1820 if (filter && dpsoftrast_test)
1822 const unsigned int * RESTRICT pixeli[4];
1823 tci[0] = (subtc[0]>>12) - tcimin[0];
1824 tci[1] = (subtc[1]>>12) - tcimin[0];
1825 tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12) + 1;
1826 tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12) + 1;
1827 if (tci[0] <= tcimax[0] && tci[1] <= tcimax[1] && tci1[0] <= tcimax[0] && tci1[1] <= tcimax[1])
1829 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1831 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1832 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1833 unsigned int lerp[4] = { (ifrac[0]*ifrac[1]) >> 16, (frac[0]*ifrac[1]) >> 16, (ifrac[0]*frac[1]) >> 16, (frac[0]*frac[1]) >> 16 };
1834 tci[0] = subtc[0]>>12;
1835 tci[1] = subtc[1]>>12;
1836 pixeli[0] = pixelbasei + (tci[1]*tciwidth+tci[0]);
1837 pixeli[1] = pixeli[0] + tciwidth;
1838 outi[x] = ((((pixeli[0][0] >> 8) & 0x00FF00FF) * lerp[0] + ((pixeli[0][1] >> 8) & 0x00FF00FF) * lerp[1] + ((pixeli[1][0] >> 8) & 0x00FF00FF) * lerp[2] + ((pixeli[1][1] >> 8) & 0x00FF00FF) * lerp[3]) & 0xFF00FF00)
1839 | ((((pixeli[0][0] & 0x00FF00FF) * lerp[0] + ( pixeli[0][1] & 0x00FF00FF) * lerp[1] + ( pixeli[1][0] & 0x00FF00FF) * lerp[2] + ( pixeli[1][1] & 0x00FF00FF) * lerp[3])>>8) & 0x00FF00FF);
1842 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1844 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1846 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1847 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1848 unsigned int lerp[4] = { (ifrac[0]*ifrac[1]) >> 16, (frac[0]*ifrac[1]) >> 16, (ifrac[0]*frac[1]) >> 16, (frac[0]*frac[1]) >> 16 };
1849 tci[0] = subtc[0]>>12;
1850 tci[1] = subtc[1]>>12;
1851 tci1[0] = tci[0] + 1;
1852 tci1[1] = tci[1] + 1;
1853 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1854 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1855 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1856 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1857 pixeli[0] = pixelbasei + (tci[1]*tciwidth+tci[0]);
1858 pixeli[1] = pixelbasei + (tci[1]*tciwidth+tci1[0]);
1859 pixeli[2] = pixelbasei + (tci1[1]*tciwidth+tci[0]);
1860 pixeli[3] = pixelbasei + (tci1[1]*tciwidth+tci1[0]);
1861 outi[x] = ((((pixeli[0][0] >> 8) & 0x00FF00FF) * lerp[0] + ((pixeli[1][0] >> 8) & 0x00FF00FF) * lerp[1] + ((pixeli[2][0] >> 8) & 0x00FF00FF) * lerp[2] + ((pixeli[3][0] >> 8) & 0x00FF00FF) * lerp[3]) & 0xFF00FF00)
1862 | ((((pixeli[0][0] & 0x00FF00FF) * lerp[0] + ( pixeli[1][0] & 0x00FF00FF) * lerp[1] + ( pixeli[2][0] & 0x00FF00FF) * lerp[2] + ( pixeli[3][0] & 0x00FF00FF) * lerp[3])>>8) & 0x00FF00FF);
1867 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1869 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1870 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1871 unsigned int lerp[4] = { (ifrac[0]*ifrac[1]) >> 16, (frac[0]*ifrac[1]) >> 16, (ifrac[0]*frac[1]) >> 16, (frac[0]*frac[1]) >> 16 };
1872 tci[0] = subtc[0]>>12;
1873 tci[1] = subtc[1]>>12;
1874 tci1[0] = tci[0] + 1;
1875 tci1[1] = tci[1] + 1;
1876 tci[0] &= tciwrapmask[0];
1877 tci[1] &= tciwrapmask[1];
1878 tci1[0] &= tciwrapmask[0];
1879 tci1[1] &= tciwrapmask[1];
1880 pixeli[0] = pixelbasei + (tci[1]*tciwidth+tci[0]);
1881 pixeli[1] = pixelbasei + (tci[1]*tciwidth+tci1[0]);
1882 pixeli[2] = pixelbasei + (tci1[1]*tciwidth+tci[0]);
1883 pixeli[3] = pixelbasei + (tci1[1]*tciwidth+tci1[0]);
1884 outi[x] = ((((pixeli[0][0] >> 8) & 0x00FF00FF) * lerp[0] + ((pixeli[1][0] >> 8) & 0x00FF00FF) * lerp[1] + ((pixeli[2][0] >> 8) & 0x00FF00FF) * lerp[2] + ((pixeli[3][0] >> 8) & 0x00FF00FF) * lerp[3]) & 0xFF00FF00)
1885 | ((((pixeli[0][0] & 0x00FF00FF) * lerp[0] + ( pixeli[1][0] & 0x00FF00FF) * lerp[1] + ( pixeli[2][0] & 0x00FF00FF) * lerp[2] + ( pixeli[3][0] & 0x00FF00FF) * lerp[3])>>8) & 0x00FF00FF);
1893 tci[0] = (subtc[0]>>12) - tcimin[0];
1894 tci[1] = (subtc[1]>>12) - tcimin[0];
1895 tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12) + 1;
1896 tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12) + 1;
1897 if (tci[0] <= tcimax[0] && tci[1] <= tcimax[1] && tci1[0] <= tcimax[0] && tci1[1] <= tcimax[1])
1899 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1901 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1902 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1903 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1904 tci[0] = subtc[0]>>12;
1905 tci[1] = subtc[1]>>12;
1906 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1907 pixel[1] = pixel[0] + 4 * tciwidth;
1908 out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[0][4+0]*lerp[1]+pixel[1][0]*lerp[2]+pixel[1][4+0]*lerp[3]) >> 24;
1909 out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[0][4+1]*lerp[1]+pixel[1][1]*lerp[2]+pixel[1][4+1]*lerp[3]) >> 24;
1910 out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[0][4+2]*lerp[1]+pixel[1][2]*lerp[2]+pixel[1][4+2]*lerp[3]) >> 24;
1911 out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[0][4+3]*lerp[1]+pixel[1][3]*lerp[2]+pixel[1][4+3]*lerp[3]) >> 24;
1914 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1916 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1918 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1919 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1920 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1921 tci[0] = subtc[0]>>12;
1922 tci[1] = subtc[1]>>12;
1923 tci1[0] = tci[0] + 1;
1924 tci1[1] = tci[1] + 1;
1925 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1926 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1927 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1928 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1929 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1930 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1931 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1932 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1933 out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) >> 24;
1934 out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) >> 24;
1935 out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) >> 24;
1936 out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) >> 24;
1941 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1943 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1944 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1945 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1946 tci[0] = subtc[0]>>12;
1947 tci[1] = subtc[1]>>12;
1948 tci1[0] = tci[0] + 1;
1949 tci1[1] = tci[1] + 1;
1950 tci[0] &= tciwrapmask[0];
1951 tci[1] &= tciwrapmask[1];
1952 tci1[0] &= tciwrapmask[0];
1953 tci1[1] &= tciwrapmask[1];
1954 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1955 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1956 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1957 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1958 out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) >> 24;
1959 out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) >> 24;
1960 out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) >> 24;
1961 out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) >> 24;
1967 tci[0] = (subtc[0]>>12) - tcimin[0];
1968 tci[1] = (subtc[1]>>12) - tcimin[0];
1969 tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12);
1970 tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12);
1971 if (tci[0] <= tcimax[0] && tci[1] <= tcimax[1] && tci1[0] <= tcimax[0] && tci1[1] <= tcimax[1])
1973 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1975 tci[0] = subtc[0]>>12;
1976 tci[1] = subtc[1]>>12;
1977 outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
1980 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1982 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1984 tci[0] = subtc[0]>>12;
1985 tci[1] = subtc[1]>>12;
1986 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1987 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1988 outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
1993 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1995 tci[0] = subtc[0]>>12;
1996 tci[1] = subtc[1]>>12;
1997 tci[0] &= tciwrapmask[0];
1998 tci[1] &= tciwrapmask[1];
1999 outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
2006 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, int arrayindex, const float *zf)
2009 int startx = span->startx;
2010 int endx = span->endx;
2015 data[0] = span->data[0][arrayindex][0];
2016 data[1] = span->data[0][arrayindex][1];
2017 data[2] = span->data[0][arrayindex][2];
2018 data[3] = span->data[0][arrayindex][3];
2019 slope[0] = span->data[1][arrayindex][0];
2020 slope[1] = span->data[1][arrayindex][1];
2021 slope[2] = span->data[1][arrayindex][2];
2022 slope[3] = span->data[1][arrayindex][3];
2023 for (x = startx;x < endx;x++)
2026 c[0] = (data[0] + slope[0]*x) * z;
2027 c[1] = (data[1] + slope[1]*x) * z;
2028 c[2] = (data[2] + slope[2]*x) * z;
2029 c[3] = (data[3] + slope[3]*x) * z;
2030 out4f[x*4+0] = in4f[x*4+0] * c[0];
2031 out4f[x*4+1] = in4f[x*4+1] * c[1];
2032 out4f[x*4+2] = in4f[x*4+2] * c[2];
2033 out4f[x*4+3] = in4f[x*4+3] * c[3];
2037 void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, int arrayindex, const float *zf)
2040 int startx = span->startx;
2041 int endx = span->endx;
2046 data[0] = span->data[0][arrayindex][0];
2047 data[1] = span->data[0][arrayindex][1];
2048 data[2] = span->data[0][arrayindex][2];
2049 data[3] = span->data[0][arrayindex][3];
2050 slope[0] = span->data[1][arrayindex][0];
2051 slope[1] = span->data[1][arrayindex][1];
2052 slope[2] = span->data[1][arrayindex][2];
2053 slope[3] = span->data[1][arrayindex][3];
2054 for (x = startx;x < endx;x++)
2057 c[0] = (data[0] + slope[0]*x) * z;
2058 c[1] = (data[1] + slope[1]*x) * z;
2059 c[2] = (data[2] + slope[2]*x) * z;
2060 c[3] = (data[3] + slope[3]*x) * z;
2061 out4f[x*4+0] = c[0];
2062 out4f[x*4+1] = c[1];
2063 out4f[x*4+2] = c[2];
2064 out4f[x*4+3] = c[3];
2068 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
2070 int x, startx = span->startx, endx = span->endx;
2071 float c[4], localcolor[4];
2072 localcolor[0] = subcolor[0];
2073 localcolor[1] = subcolor[1];
2074 localcolor[2] = subcolor[2];
2075 localcolor[3] = subcolor[3];
2076 for (x = startx;x < endx;x++)
2078 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
2079 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
2080 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
2081 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
2082 out4f[x*4+0] = ina4f[x*4+0] + c[0];
2083 out4f[x*4+1] = ina4f[x*4+1] + c[1];
2084 out4f[x*4+2] = ina4f[x*4+2] + c[2];
2085 out4f[x*4+3] = ina4f[x*4+3] + c[3];
2089 void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2091 int x, startx = span->startx, endx = span->endx;
2092 for (x = startx;x < endx;x++)
2094 out4f[x*4+0] = ina4f[x*4+0] * inb4f[x*4+0];
2095 out4f[x*4+1] = ina4f[x*4+1] * inb4f[x*4+1];
2096 out4f[x*4+2] = ina4f[x*4+2] * inb4f[x*4+2];
2097 out4f[x*4+3] = ina4f[x*4+3] * inb4f[x*4+3];
2101 void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2103 int x, startx = span->startx, endx = span->endx;
2104 for (x = startx;x < endx;x++)
2106 out4f[x*4+0] = ina4f[x*4+0] + inb4f[x*4+0];
2107 out4f[x*4+1] = ina4f[x*4+1] + inb4f[x*4+1];
2108 out4f[x*4+2] = ina4f[x*4+2] + inb4f[x*4+2];
2109 out4f[x*4+3] = ina4f[x*4+3] + inb4f[x*4+3];
2113 void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2115 int x, startx = span->startx, endx = span->endx;
2117 for (x = startx;x < endx;x++)
2119 a = 1.0f - inb4f[x*4+3];
2121 out4f[x*4+0] = ina4f[x*4+0] * a + inb4f[x*4+0] * b;
2122 out4f[x*4+1] = ina4f[x*4+1] * a + inb4f[x*4+1] * b;
2123 out4f[x*4+2] = ina4f[x*4+2] * a + inb4f[x*4+2] * b;
2124 out4f[x*4+3] = ina4f[x*4+3] * a + inb4f[x*4+3] * b;
2128 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, const float *color)
2130 int x, startx = span->startx, endx = span->endx;
2131 float localcolor[4], ilerp, lerp;
2132 localcolor[0] = color[0];
2133 localcolor[1] = color[1];
2134 localcolor[2] = color[2];
2135 localcolor[3] = color[3];
2136 ilerp = 1.0f - localcolor[3];
2137 lerp = localcolor[3];
2138 for (x = startx;x < endx;x++)
2140 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
2141 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
2142 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
2143 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
2149 void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, int arrayindex, const float *zf)
2152 int startx = span->startx;
2153 int endx = span->endx;
2157 data[2] = span->data[0][arrayindex][0];
2158 data[1] = span->data[0][arrayindex][1];
2159 data[0] = span->data[0][arrayindex][2];
2160 data[3] = span->data[0][arrayindex][3];
2161 slope[2] = span->data[1][arrayindex][0];
2162 slope[1] = span->data[1][arrayindex][1];
2163 slope[0] = span->data[1][arrayindex][2];
2164 slope[3] = span->data[1][arrayindex][3];
2165 for (x = startx;x < endx;x++)
2168 out4ub[x*4+0] = (int)(in4ub[x*4+0] * (data[0] + slope[0]*x) * z);
2169 out4ub[x*4+1] = (int)(in4ub[x*4+1] * (data[1] + slope[1]*x) * z);
2170 out4ub[x*4+2] = (int)(in4ub[x*4+2] * (data[2] + slope[2]*x) * z);
2171 out4ub[x*4+3] = (int)(in4ub[x*4+3] * (data[3] + slope[3]*x) * z);
2175 void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, int arrayindex, const float *zf)
2178 int startx = span->startx;
2179 int endx = span->endx;
2183 data[2] = span->data[0][arrayindex][0]*255.0f;
2184 data[1] = span->data[0][arrayindex][1]*255.0f;
2185 data[0] = span->data[0][arrayindex][2]*255.0f;
2186 data[3] = span->data[0][arrayindex][3]*255.0f;
2187 slope[2] = span->data[1][arrayindex][0]*255.0f;
2188 slope[1] = span->data[1][arrayindex][1]*255.0f;
2189 slope[0] = span->data[1][arrayindex][2]*255.0f;
2190 slope[3] = span->data[1][arrayindex][3]*255.0f;
2191 for (x = startx;x < endx;x++)
2194 out4ub[x*4+0] = (int)((data[0] + slope[0]*x) * z);
2195 out4ub[x*4+1] = (int)((data[1] + slope[1]*x) * z);
2196 out4ub[x*4+2] = (int)((data[2] + slope[2]*x) * z);
2197 out4ub[x*4+3] = (int)((data[3] + slope[3]*x) * z);
2201 void DPSOFTRAST_Draw_Span_AddBloomBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub, const float *subcolor)
2203 int x, startx = span->startx, endx = span->endx;
2204 int c[4], localcolor[4];
2205 localcolor[2] = (int)(subcolor[0] * 255.0f);
2206 localcolor[1] = (int)(subcolor[1] * 255.0f);
2207 localcolor[0] = (int)(subcolor[2] * 255.0f);
2208 localcolor[3] = (int)(subcolor[3] * 255.0f);
2209 for (x = startx;x < endx;x++)
2211 c[0] = inb4ub[x*4+0] - localcolor[0];if (c[0] < 0) c[0] = 0;
2212 c[1] = inb4ub[x*4+1] - localcolor[1];if (c[1] < 0) c[1] = 0;
2213 c[2] = inb4ub[x*4+2] - localcolor[2];if (c[2] < 0) c[2] = 0;
2214 c[3] = inb4ub[x*4+3] - localcolor[3];if (c[3] < 0) c[3] = 0;
2215 c[0] += ina4ub[x*4+0];if (c[0] > 255) c[0] = 255;
2216 c[1] += ina4ub[x*4+1];if (c[1] > 255) c[1] = 255;
2217 c[2] += ina4ub[x*4+2];if (c[2] > 255) c[2] = 255;
2218 c[3] += ina4ub[x*4+3];if (c[3] > 255) c[3] = 255;
2219 out4ub[x*4+0] = c[0];
2220 out4ub[x*4+1] = c[1];
2221 out4ub[x*4+2] = c[2];
2222 out4ub[x*4+3] = c[3];
2226 void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2228 int x, startx = span->startx, endx = span->endx;
2229 for (x = startx;x < endx;x++)
2231 out4ub[x*4+0] = (ina4ub[x*4+0] * inb4ub[x*4+0])>>8;
2232 out4ub[x*4+1] = (ina4ub[x*4+1] * inb4ub[x*4+1])>>8;
2233 out4ub[x*4+2] = (ina4ub[x*4+2] * inb4ub[x*4+2])>>8;
2234 out4ub[x*4+3] = (ina4ub[x*4+3] * inb4ub[x*4+3])>>8;
2238 void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2240 int x, startx = span->startx, endx = span->endx;
2242 for (x = startx;x < endx;x++)
2244 d[0] = ina4ub[x*4+0] + inb4ub[x*4+0];if (d[0] > 255) d[0] = 255;
2245 d[1] = ina4ub[x*4+1] + inb4ub[x*4+1];if (d[1] > 255) d[1] = 255;
2246 d[2] = ina4ub[x*4+2] + inb4ub[x*4+2];if (d[2] > 255) d[2] = 255;
2247 d[3] = ina4ub[x*4+3] + inb4ub[x*4+3];if (d[3] > 255) d[3] = 255;
2248 out4ub[x*4+0] = d[0];
2249 out4ub[x*4+1] = d[1];
2250 out4ub[x*4+2] = d[2];
2251 out4ub[x*4+3] = d[3];
2255 void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2257 int x, startx = span->startx, endx = span->endx;
2259 for (x = startx;x < endx;x++)
2261 a = 256 - inb4ub[x*4+3];
2263 out4ub[x*4+0] = (ina4ub[x*4+0] * a + inb4ub[x*4+0] * b)>>8;
2264 out4ub[x*4+1] = (ina4ub[x*4+1] * a + inb4ub[x*4+1] * b)>>8;
2265 out4ub[x*4+2] = (ina4ub[x*4+2] * a + inb4ub[x*4+2] * b)>>8;
2266 out4ub[x*4+3] = (ina4ub[x*4+3] * a + inb4ub[x*4+3] * b)>>8;
2270 void DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, const float *color)
2272 int x, startx = span->startx, endx = span->endx;
2273 int localcolor[4], ilerp, lerp;
2274 localcolor[2] = (int)(color[0]*255.0f);
2275 localcolor[1] = (int)(color[1]*255.0f);
2276 localcolor[0] = (int)(color[2]*255.0f);
2277 localcolor[3] = (int)(color[3]*255.0f);
2278 ilerp = 256 - localcolor[3];
2279 lerp = localcolor[3];
2280 for (x = startx;x < endx;x++)
2282 out4ub[x*4+0] = (in4ub[x*4+0] * ilerp + localcolor[0] * lerp)>>8;
2283 out4ub[x*4+1] = (in4ub[x*4+1] * ilerp + localcolor[1] * lerp)>>8;
2284 out4ub[x*4+2] = (in4ub[x*4+2] * ilerp + localcolor[2] * lerp)>>8;
2285 out4ub[x*4+3] = (in4ub[x*4+3] * ilerp + localcolor[3] * lerp)>>8;
2291 void DPSOFTRAST_VertexShader_Generic(void)
2293 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);
2294 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2295 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2296 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2297 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2300 void DPSOFTRAST_PixelShader_Generic(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2302 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2303 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2304 unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2305 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2306 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2307 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
2309 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_FIRST, 2, buffer_z);
2310 DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(span, buffer_FragColorbgra8, buffer_texture_colorbgra8, 1, buffer_z);
2311 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2313 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_SECOND, 2, buffer_z);
2314 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2317 DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2319 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2322 DPSOFTRAST_Draw_Span_AddBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2324 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND)
2327 DPSOFTRAST_Draw_Span_MixBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2332 DPSOFTRAST_Draw_Span_VaryingBGRA8(span, buffer_FragColorbgra8, 1, buffer_z);
2333 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2338 void DPSOFTRAST_VertexShader_PostProcess(void)
2340 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);
2341 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2342 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2345 void DPSOFTRAST_PixelShader_PostProcess(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2347 // TODO: optimize!! at the very least there is no reason to use texture sampling on the frame texture
2348 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2349 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2350 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2351 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2352 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_FragColorbgra8, GL20TU_FIRST, 2, buffer_z);
2353 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
2355 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_SECOND, 3, buffer_z);
2356 DPSOFTRAST_Draw_Span_AddBloomBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_colorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
2358 DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
2359 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
2361 // TODO: implement saturation
2363 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
2365 // TODO: implement gammaramps
2367 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2372 void DPSOFTRAST_VertexShader_Depth_Or_Shadow(void)
2374 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);
2377 void DPSOFTRAST_PixelShader_Depth_Or_Shadow(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2379 // this is never called (because colormask is off when this shader is used)
2380 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2381 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2382 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2383 memset(buffer_FragColorbgra8, 0, span->length*4);
2384 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2389 void DPSOFTRAST_VertexShader_FlatColor(void)
2391 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);
2392 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1);
2395 void DPSOFTRAST_PixelShader_FlatColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2397 int x, startx = span->startx, endx = span->endx;
2398 int Color_Ambienti[4];
2399 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2400 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2401 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2402 Color_Ambienti[2] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]*256.0f);
2403 Color_Ambienti[1] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]*256.0f);
2404 Color_Ambienti[0] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]*256.0f);
2405 Color_Ambienti[3] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0] *256.0f);
2406 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2407 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2408 for (x = startx;x < endx;x++)
2410 buffer_FragColorbgra8[x*4+0] = (buffer_texture_colorbgra8[x*4+0] * Color_Ambienti[0])>>8;
2411 buffer_FragColorbgra8[x*4+1] = (buffer_texture_colorbgra8[x*4+1] * Color_Ambienti[1])>>8;
2412 buffer_FragColorbgra8[x*4+2] = (buffer_texture_colorbgra8[x*4+2] * Color_Ambienti[2])>>8;
2413 buffer_FragColorbgra8[x*4+3] = (buffer_texture_colorbgra8[x*4+3] * Color_Ambienti[3])>>8;
2415 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2420 void DPSOFTRAST_VertexShader_VertexColor(void)
2422 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);
2423 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2424 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1);
2427 void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2429 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2430 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2431 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2432 int x, startx = span->startx, endx = span->endx;
2433 float Color_Ambient[4], Color_Diffuse[4];
2437 int arrayindex = DPSOFTRAST_ARRAY_COLOR;
2438 data[2] = span->data[0][arrayindex][0];
2439 data[1] = span->data[0][arrayindex][1];
2440 data[0] = span->data[0][arrayindex][2];
2441 data[3] = span->data[0][arrayindex][3];
2442 slope[2] = span->data[1][arrayindex][0];
2443 slope[1] = span->data[1][arrayindex][1];
2444 slope[0] = span->data[1][arrayindex][2];
2445 slope[3] = span->data[1][arrayindex][3];
2446 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2447 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2448 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2449 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2450 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2451 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2452 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2453 Color_Diffuse[3] = 0.0f;
2454 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2455 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2456 for (x = startx;x < endx;x++)
2459 buffer_FragColorbgra8[x*4+0] = (int)(buffer_texture_colorbgra8[x*4+0] * (Color_Ambient[0] + ((data[0] + slope[0]*x) * z) * Color_Diffuse[0]));
2460 buffer_FragColorbgra8[x*4+1] = (int)(buffer_texture_colorbgra8[x*4+1] * (Color_Ambient[1] + ((data[1] + slope[1]*x) * z) * Color_Diffuse[1]));
2461 buffer_FragColorbgra8[x*4+2] = (int)(buffer_texture_colorbgra8[x*4+2] * (Color_Ambient[2] + ((data[2] + slope[2]*x) * z) * Color_Diffuse[2]));
2462 buffer_FragColorbgra8[x*4+3] = (int)(buffer_texture_colorbgra8[x*4+3] * (Color_Ambient[3] + ((data[3] + slope[3]*x) * z) * Color_Diffuse[3]));
2464 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2469 void DPSOFTRAST_VertexShader_Lightmap(void)
2471 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);
2472 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1);
2473 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
2476 void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2478 int x, startx = span->startx, endx = span->endx;
2479 float Color_Ambient[4], Color_Diffuse[4];
2480 int Color_Ambienti[4];
2481 int Color_Diffusei[4];
2482 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2483 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2484 unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2485 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2487 //unsigned char * RESTRICT pixelmask = span->pixelmask;
2488 //unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + span->start * 4;
2489 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2490 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2491 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2492 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2493 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2494 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2495 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2496 Color_Diffuse[3] = 0.0f;
2497 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2498 Color_Ambienti[2] = (int)(Color_Ambient[0] * 65536.0f);
2499 Color_Ambienti[1] = (int)(Color_Ambient[1] * 65536.0f);
2500 Color_Ambienti[0] = (int)(Color_Ambient[2] * 65536.0f);
2501 Color_Ambienti[3] = (int)(Color_Ambient[3] * 65536.0f);
2502 Color_Diffusei[2] = (int)(Color_Diffuse[0] * 256.0f);
2503 Color_Diffusei[1] = (int)(Color_Diffuse[1] * 256.0f);
2504 Color_Diffusei[0] = (int)(Color_Diffuse[2] * 256.0f);
2505 Color_Diffusei[3] = (int)(Color_Diffuse[3] * 256.0f);
2506 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2507 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_LIGHTMAP, 6, buffer_z);
2508 for (x = startx;x < endx;x++)
2510 d[0] = (buffer_texture_colorbgra8[x*4+0] * (Color_Ambienti[0] + buffer_texture_lightmapbgra8[x*4+0] * Color_Diffusei[0])) >> 16;if (d[0] > 255) d[0] = 255;
2511 d[1] = (buffer_texture_colorbgra8[x*4+1] * (Color_Ambienti[1] + buffer_texture_lightmapbgra8[x*4+1] * Color_Diffusei[1])) >> 16;if (d[1] > 255) d[1] = 255;
2512 d[2] = (buffer_texture_colorbgra8[x*4+2] * (Color_Ambienti[2] + buffer_texture_lightmapbgra8[x*4+2] * Color_Diffusei[2])) >> 16;if (d[2] > 255) d[2] = 255;
2513 d[3] = (buffer_texture_colorbgra8[x*4+3] * (Color_Ambienti[3] + buffer_texture_lightmapbgra8[x*4+3] * Color_Diffusei[3])) >> 16;if (d[3] > 255) d[3] = 255;
2514 buffer_FragColorbgra8[x*4+0] = d[0];
2515 buffer_FragColorbgra8[x*4+1] = d[1];
2516 buffer_FragColorbgra8[x*4+2] = d[2];
2517 buffer_FragColorbgra8[x*4+3] = d[3];
2519 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2524 void DPSOFTRAST_VertexShader_FakeLight(void)
2526 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);
2529 void DPSOFTRAST_PixelShader_FakeLight(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2532 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2533 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2534 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2535 memset(buffer_FragColorbgra8, 0, span->length*4);
2536 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2541 void DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace(void)
2543 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);
2546 void DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2549 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2550 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2551 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2552 memset(buffer_FragColorbgra8, 0, span->length*4);
2553 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2558 void DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace(void)
2560 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);
2563 void DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2566 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2567 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2568 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2569 memset(buffer_FragColorbgra8, 0, span->length*4);
2570 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2575 void DPSOFTRAST_VertexShader_LightDirection(void)
2577 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);
2578 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1);
2581 void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2584 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2585 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2586 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2587 memset(buffer_FragColorbgra8, 0, span->length*4);
2588 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2593 void DPSOFTRAST_VertexShader_LightSource(void)
2595 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);
2598 void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2601 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2602 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2603 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2604 memset(buffer_FragColorbgra8, 0, span->length*4);
2605 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2610 void DPSOFTRAST_VertexShader_Refraction(void)
2612 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);
2615 void DPSOFTRAST_PixelShader_Refraction(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2618 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2619 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2620 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2621 memset(buffer_FragColorbgra8, 0, span->length*4);
2622 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2627 void DPSOFTRAST_VertexShader_Water(void)
2629 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);
2633 void DPSOFTRAST_PixelShader_Water(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2636 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2637 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2638 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2639 memset(buffer_FragColorbgra8, 0, span->length*4);
2640 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2645 void DPSOFTRAST_VertexShader_ShowDepth(void)
2647 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);
2650 void DPSOFTRAST_PixelShader_ShowDepth(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2653 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2654 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2655 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2656 memset(buffer_FragColorbgra8, 0, span->length*4);
2657 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2662 void DPSOFTRAST_VertexShader_DeferredGeometry(void)
2664 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);
2667 void DPSOFTRAST_PixelShader_DeferredGeometry(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2670 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2671 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2672 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2673 memset(buffer_FragColorbgra8, 0, span->length*4);
2674 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2679 void DPSOFTRAST_VertexShader_DeferredLightSource(void)
2681 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);
2684 void DPSOFTRAST_PixelShader_DeferredLightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2687 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2688 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2689 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2690 memset(buffer_FragColorbgra8, 0, span->length*4);
2691 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2696 typedef struct DPSOFTRAST_ShaderModeInfo_s
2699 void (*Vertex)(void);
2700 void (*Span)(const DPSOFTRAST_State_Draw_Span * RESTRICT span);
2702 DPSOFTRAST_ShaderModeInfo;
2704 DPSOFTRAST_ShaderModeInfo DPSOFTRAST_ShaderModeTable[SHADERMODE_COUNT] =
2706 {2, DPSOFTRAST_VertexShader_Generic, DPSOFTRAST_PixelShader_Generic, },
2707 {2, DPSOFTRAST_VertexShader_PostProcess, DPSOFTRAST_PixelShader_PostProcess, },
2708 {2, DPSOFTRAST_VertexShader_Depth_Or_Shadow, DPSOFTRAST_PixelShader_Depth_Or_Shadow, },
2709 {2, DPSOFTRAST_VertexShader_FlatColor, DPSOFTRAST_PixelShader_FlatColor, },
2710 {2, DPSOFTRAST_VertexShader_VertexColor, DPSOFTRAST_PixelShader_VertexColor, },
2711 {2, DPSOFTRAST_VertexShader_Lightmap, DPSOFTRAST_PixelShader_Lightmap, },
2712 {2, DPSOFTRAST_VertexShader_FakeLight, DPSOFTRAST_PixelShader_FakeLight, },
2713 {2, DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace, DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace, },
2714 {2, DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace, DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace},
2715 {2, DPSOFTRAST_VertexShader_LightDirection, DPSOFTRAST_PixelShader_LightDirection, },
2716 {2, DPSOFTRAST_VertexShader_LightSource, DPSOFTRAST_PixelShader_LightSource, },
2717 {2, DPSOFTRAST_VertexShader_Refraction, DPSOFTRAST_PixelShader_Refraction, },
2718 {2, DPSOFTRAST_VertexShader_Water, DPSOFTRAST_PixelShader_Water, },
2719 {2, DPSOFTRAST_VertexShader_ShowDepth, DPSOFTRAST_PixelShader_ShowDepth, },
2720 {2, DPSOFTRAST_VertexShader_DeferredGeometry, DPSOFTRAST_PixelShader_DeferredGeometry, },
2721 {2, DPSOFTRAST_VertexShader_DeferredLightSource, DPSOFTRAST_PixelShader_DeferredLightSource, }
2726 void DPSOFTRAST_Draw_ProcessSpans(void)
2732 int numspans = dpsoftrast.draw.numspans;
2734 // unsigned int *colorpixel;
2735 unsigned int *depthpixel;
2741 DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
2742 unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2743 for (i = 0;i < numspans;i++, span++)
2745 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
2746 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
2747 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
2749 depth = (int)(w*DPSOFTRAST_DEPTHSCALE);
2750 depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
2751 depthpixel = dpsoftrast.fb_depthpixels + span->start;
2752 switch(dpsoftrast.fb_depthfunc)
2755 case GL_ALWAYS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
2756 case GL_LESS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
2757 case GL_LEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
2758 case GL_EQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
2759 case GL_GEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
2760 case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
2761 case GL_NEVER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
2763 //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
2764 //for (x = 0;x < span->length;x++)
2765 // colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
2766 // if there is no color buffer, skip pixel shader
2768 endx = span->length;
2769 while (startx < endx && !pixelmask[startx])
2771 while (endx > startx && !pixelmask[endx-1])
2774 continue; // no pixels to fill
2775 span->pixelmask = pixelmask;
2776 span->startx = startx;
2778 // run pixel shader if appropriate
2779 // do this before running depthmask code, to allow the pixelshader
2780 // to clear pixelmask values for alpha testing
2781 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
2782 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
2783 if (dpsoftrast.user.depthmask)
2784 for (x = 0, d = depth;x < span->length;x++, d += depthslope)
2790 // no depth testing means we're just dealing with color...
2791 // if there is no color buffer, skip pixel shader
2792 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
2794 memset(pixelmask, 1, span->length);
2795 span->pixelmask = pixelmask;
2797 span->endx = span->length;
2798 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
2804 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask)
2806 int cullface = dpsoftrast.user.cullface;
2807 int width = dpsoftrast.fb_width;
2808 int height = dpsoftrast.fb_height;
2826 float mip_edge0tc[2];
2827 float mip_edge1tc[2];
2828 float mip_edge0xy[2];
2829 float mip_edge1xy[2];
2830 float mip_edge0xymul;
2831 float mip_edge1xymul;
2835 unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS];
2850 float trianglearea2;
2851 float triangleedge[2][4];
2852 float trianglenormal[4];
2854 float clipped[DPSOFTRAST_ARRAY_TOTAL][4][4];
2856 float proj[DPSOFTRAST_ARRAY_TOTAL][4][4];
2857 DPSOFTRAST_Texture *texture;
2858 DPSOFTRAST_State_Draw_Span *span;
2859 DPSOFTRAST_State_Draw_Span *oldspan;
2860 for (i = 0;i < numtriangles;i++)
2862 // generate the 3 edges of this triangle
2863 // generate spans for the triangle - switch based on left split or right split classification of triangle
2866 e[0] = element3i[i*3+0] - firstvertex;
2867 e[1] = element3i[i*3+1] - firstvertex;
2868 e[2] = element3i[i*3+2] - firstvertex;
2872 e[0] = element3s[i*3+0] - firstvertex;
2873 e[1] = element3s[i*3+1] - firstvertex;
2874 e[2] = element3s[i*3+2] - firstvertex;
2882 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];
2883 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];
2884 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];
2885 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];
2886 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];
2887 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];
2888 trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
2889 trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
2890 trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
2891 trianglearea2 = trianglenormal[0] * trianglenormal[0] + trianglenormal[1] * trianglenormal[1] + trianglenormal[2] * trianglenormal[2];
2892 // skip degenerate triangles, nothing good can come from them...
2893 if (trianglearea2 == 0.0f)
2895 // apply current cullface mode (this culls many triangles)
2899 if (trianglenormal[2] < 0)
2903 if (trianglenormal[2] > 0)
2907 // calculate distance from nearplane
2908 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + 1.0f;
2909 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + 1.0f;
2910 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + 1.0f;
2912 if (clipdist[0] < 0.0f)
2914 if (clipdist[1] < 0.0f)
2916 if (clipdist[2] < 0.0f)
2918 // clip triangle if necessary
2922 // triangle is entirely in front of nearplane
2924 // macros for clipping vertices
2925 #define CLIPPEDVERTEXLERP(k,p1,p2) \
2926 frac = clipdist[p1] / (clipdist[p1] - clipdist[p2]);\
2927 ifrac = 1.0f - frac;\
2928 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2932 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+0]*frac;\
2933 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+1]*frac;\
2934 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+2]*frac;\
2935 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+3]*frac;\
2938 DPSOFTRAST_Draw_ProjectVertices(screen[k], clipped[DPSOFTRAST_ARRAY_POSITION][k], 1)
2939 #define CLIPPEDVERTEXCOPY(k,p1) \
2940 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2944 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0];\
2945 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1];\
2946 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2];\
2947 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3];\
2950 screen[k][0] = dpsoftrast.draw.screencoord4f[e[p1]*4+0];\
2951 screen[k][1] = dpsoftrast.draw.screencoord4f[e[p1]*4+1];\
2952 screen[k][2] = dpsoftrast.draw.screencoord4f[e[p1]*4+2];\
2953 screen[k][3] = dpsoftrast.draw.screencoord4f[e[p1]*4+3];
2955 CLIPPEDVERTEXCOPY(0,0);
2956 CLIPPEDVERTEXCOPY(1,1);
2957 CLIPPEDVERTEXCOPY(2,2);
2961 CLIPPEDVERTEXLERP(0,0,1);
2962 CLIPPEDVERTEXCOPY(1,1);
2963 CLIPPEDVERTEXCOPY(2,2);
2964 CLIPPEDVERTEXLERP(3,2,0);
2968 CLIPPEDVERTEXCOPY(0,0);
2969 CLIPPEDVERTEXLERP(1,0,1);
2970 CLIPPEDVERTEXLERP(2,1,2);
2971 CLIPPEDVERTEXCOPY(3,2);
2975 CLIPPEDVERTEXLERP(0,1,2);
2976 CLIPPEDVERTEXCOPY(1,2);
2977 CLIPPEDVERTEXLERP(2,2,0);
2981 CLIPPEDVERTEXCOPY(0,0);
2982 CLIPPEDVERTEXCOPY(1,1);
2983 CLIPPEDVERTEXLERP(2,1,2);
2984 CLIPPEDVERTEXLERP(3,2,0);
2988 CLIPPEDVERTEXLERP(0,0,1);
2989 CLIPPEDVERTEXCOPY(1,1);
2990 CLIPPEDVERTEXLERP(2,1,2);
2994 CLIPPEDVERTEXCOPY(0,0);
2995 CLIPPEDVERTEXLERP(1,0,1);
2996 CLIPPEDVERTEXLERP(2,2,0);
3000 // triangle is entirely behind nearplane
3003 // calculate integer y coords for triangle points
3004 screenx[0] = (int)(screen[0][0]);
3005 screeny[0] = (int)(screen[0][1]);
3006 screenx[1] = (int)(screen[1][0]);
3007 screeny[1] = (int)(screen[1][1]);
3008 screenx[2] = (int)(screen[2][0]);
3009 screeny[2] = (int)(screen[2][1]);
3010 screenx[3] = (int)(screen[3][0]);
3011 screeny[3] = (int)(screen[3][1]);
3012 // figure out the extents (bounding box) of the triangle
3013 extent[0] = screenx[0];
3014 extent[1] = screeny[0];
3015 extent[2] = screenx[0];
3016 extent[3] = screeny[0];
3017 for (j = 1;j < numpoints;j++)
3019 if (extent[0] > screenx[j]) extent[0] = screenx[j];
3020 if (extent[1] > screeny[j]) extent[1] = screeny[j];
3021 if (extent[2] < screenx[j]) extent[2] = screenx[j];
3022 if (extent[3] < screeny[j]) extent[3] = screeny[j];
3032 if (extent[2] > width)
3034 if (extent[3] > height)
3036 // skip offscreen triangles
3037 if (extent[2] <= extent[0] || extent[3] <= extent[1])
3039 // okay, this triangle is going to produce spans, we'd better project
3040 // the interpolants now (this is what gives perspective texturing),
3041 // this consists of simply multiplying all arrays by the W coord
3042 // (which is basically 1/Z), which will be undone per-pixel
3043 // (multiplying by Z again) to get the perspective-correct array
3045 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3049 for (k = 0;k < numpoints;k++)
3052 proj[j][k][0] = clipped[j][k][0] * w;
3053 proj[j][k][1] = clipped[j][k][1] * w;
3054 proj[j][k][2] = clipped[j][k][2] * w;
3055 proj[j][k][3] = clipped[j][k][3] * w;
3059 // adjust texture LOD by texture density, in the simplest way possible...
3060 mip_edge0xy[0] = screen[0][0] - screen[1][0];
3061 mip_edge0xy[1] = screen[0][1] - screen[1][1];
3062 mip_edge1xy[0] = screen[2][0] - screen[1][0];
3063 mip_edge1xy[1] = screen[2][1] - screen[1][1];
3064 mip_edge0xymul = 1.0f / (mip_edge0xy[0]*mip_edge0xy[0]+mip_edge0xy[1]*mip_edge0xy[1]);
3065 mip_edge1xymul = 1.0f / (mip_edge1xy[0]*mip_edge1xy[0]+mip_edge1xy[1]*mip_edge1xy[1]);
3066 for (j = 0;j < DPSOFTRAST_MAXTEXTUREUNITS;j++)
3068 texture = dpsoftrast.texbound[j];
3071 if (texture->filter <= DPSOFTRAST_TEXTURE_FILTER_LINEAR)
3076 k = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].lodarrayindex;
3077 mip_edge0tc[0] = (clipped[k][0][0] - clipped[k][1][0]) * texture->mipmap[0][2];
3078 mip_edge0tc[1] = (clipped[k][0][1] - clipped[k][1][1]) * texture->mipmap[0][3];
3079 mip_edge1tc[0] = (clipped[k][2][0] - clipped[k][1][0]) * texture->mipmap[0][2];
3080 mip_edge1tc[1] = (clipped[k][2][1] - clipped[k][1][1]) * texture->mipmap[0][3];
3081 mip_edge0mip = (mip_edge0tc[0]*mip_edge0tc[0]+mip_edge0tc[1]*mip_edge0tc[1]) * mip_edge0xymul;
3082 mip_edge1mip = (mip_edge1tc[0]*mip_edge1tc[0]+mip_edge1tc[1]*mip_edge1tc[1]) * mip_edge1xymul;
3083 // this will be multiplied in the texturing routine by the texture resolution
3084 mipdensity = mip_edge0mip < mip_edge1mip ? mip_edge0mip : mip_edge1mip;
3085 y = (int)(log(mipdensity)/log(2.0f));
3088 if (y > texture->mipmaps - 1)
3089 y = texture->mipmaps - 1;
3093 // iterate potential spans
3094 // TODO: optimize? if we figured out the edge order beforehand, this
3095 // could do loops over the edges in the proper order rather than
3096 // selecting them for each span
3097 // TODO: optimize? the edges could have data slopes calculated
3098 // TODO: optimize? the data slopes could be calculated as a plane
3099 // (2D slopes) to avoid any interpolation along edges at all
3100 for (y = extent[1];y < extent[3];y++)
3102 // get center of pixel y
3104 // do the compares all at once
3105 screenyless[0] = y <= screeny[0];
3106 screenyless[1] = y <= screeny[1];
3107 screenyless[2] = y <= screeny[2];
3108 screenyless[3] = y <= screeny[3];
3111 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4 + screenyless[3] * 8)
3113 case 0: /*0000*/ continue;
3114 case 1: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 0;edge1n = 1;break;
3115 case 2: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
3116 case 3: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 2;break;
3117 case 4: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break;
3118 case 5: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break; // concave - nonsense
3119 case 6: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 3;break;
3120 case 7: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 3;break;
3121 case 8: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 3;edge1n = 0;break;
3122 case 9: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 1;break;
3123 case 10: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
3124 case 11: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break;
3125 case 12: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 0;break;
3126 case 13: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
3127 case 14: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 0;break;
3128 case 15: /*1111*/ continue;
3133 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4)
3135 case 0: /*000*/ continue;
3136 case 1: /*100*/ edge0p = 2;edge0n = 0;edge1p = 0;edge1n = 1;break;
3137 case 2: /*010*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
3138 case 3: /*110*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 2;break;
3139 case 4: /*001*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 0;break;
3140 case 5: /*101*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
3141 case 6: /*011*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 0;break;
3142 case 7: /*111*/ continue;
3152 for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
3154 if (screenyless[k] && !screenyless[j])
3160 else if (screenyless[j] && !screenyless[k])
3167 if (foundedges != 3)
3169 if (cedge0p != edge0p || cedge0n != edge0n || cedge1p != edge1p || cedge1n != edge1n)
3172 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);
3174 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);
3178 edge0ylerp = (yc - screen[edge0p][1]) / (screen[edge0n][1] - screen[edge0p][1]);
3179 edge1ylerp = (yc - screen[edge1p][1]) / (screen[edge1n][1] - screen[edge1p][1]);
3180 if (edge0ylerp < 0 || edge0ylerp > 1 || edge1ylerp < 0 || edge1ylerp > 1)
3182 edge0yilerp = 1.0f - edge0ylerp;
3183 edge1yilerp = 1.0f - edge1ylerp;
3184 edge0xf = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3185 edge1xf = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3186 if (edge0xf < edge1xf)
3196 startx = (int)ceil(startxf);
3197 endx = (int)ceil(endxf);
3204 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); }
3205 spanilength = 1.0f / (endxf - startxf);
3206 startxlerp = startx - startxf;
3207 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3208 memcpy(span->mip, mip, sizeof(span->mip));
3209 span->start = y * width + startx;
3210 span->length = endx - startx;
3211 j = DPSOFTRAST_ARRAY_TOTAL;
3212 if (edge0xf < edge1xf)
3214 span->data[0][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3215 span->data[0][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
3216 span->data[0][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
3217 span->data[0][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
3218 span->data[1][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3219 span->data[1][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
3220 span->data[1][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
3221 span->data[1][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
3222 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3226 span->data[0][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
3227 span->data[0][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
3228 span->data[0][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
3229 span->data[0][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
3230 span->data[1][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
3231 span->data[1][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
3232 span->data[1][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
3233 span->data[1][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
3239 span->data[0][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3240 span->data[0][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
3241 span->data[0][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
3242 span->data[0][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
3243 span->data[1][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3244 span->data[1][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
3245 span->data[1][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
3246 span->data[1][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
3247 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3251 span->data[0][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
3252 span->data[0][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
3253 span->data[0][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
3254 span->data[0][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
3255 span->data[1][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
3256 span->data[1][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
3257 span->data[1][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
3258 span->data[1][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
3262 // change data[1][n][] to be a data slope
3263 j = DPSOFTRAST_ARRAY_TOTAL;
3264 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
3265 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
3266 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
3267 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
3268 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3272 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
3273 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
3274 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
3275 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
3278 // adjust the data[0][n][] to be correct for the pixel centers
3279 // this also handles horizontal clipping where a major part of the
3280 // span may be off the left side of the screen
3281 j = DPSOFTRAST_ARRAY_TOTAL;
3282 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
3283 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
3284 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
3285 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
3286 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3290 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
3291 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
3292 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
3293 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
3296 // to keep the shader routines from needing more than a small
3297 // buffer for pixel intermediate data, we split long spans...
3298 while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
3300 span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
3301 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3303 DPSOFTRAST_Draw_ProcessSpans();
3304 dpsoftrast.draw.numspans = 0;
3307 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3309 startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
3310 span->start = y * width + startx;
3311 span->length = endx - startx;
3312 j = DPSOFTRAST_ARRAY_TOTAL;
3313 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3314 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3315 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3316 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3317 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3321 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3322 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3323 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3324 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3328 // after all that, we have a span suitable for the pixel shader...
3329 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3331 DPSOFTRAST_Draw_ProcessSpans();
3332 dpsoftrast.draw.numspans = 0;
3335 // draw outlines over triangle for debugging
3336 // for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
3337 // DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
3339 if (dpsoftrast.draw.numspans)
3341 DPSOFTRAST_Draw_ProcessSpans();
3342 dpsoftrast.draw.numspans = 0;
3346 void DPSOFTRAST_Draw_DebugPoints(void)
3351 int numvertices = dpsoftrast.draw.numvertices;
3352 int w = dpsoftrast.fb_width;
3354 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
3356 bounds[0] = dpsoftrast.fb_viewportscissor[0];
3357 bounds[1] = dpsoftrast.fb_viewportscissor[1];
3358 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
3359 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
3360 for (i = 0;i < numvertices;i++)
3363 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
3365 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
3366 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
3367 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
3368 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
3369 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
3370 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
3371 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
3373 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
3374 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
3378 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
3380 unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL];
3381 arraymask[0] = true;
3382 arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode)
3383 arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL;
3384 arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL;
3385 arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL;
3386 arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL;
3387 arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL;
3388 arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL;
3389 arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL;
3390 arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL;
3391 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
3392 DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true);
3393 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Vertex();
3394 DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
3395 DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numvertices, numtriangles, element3i, element3s, arraymask);
3398 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
3407 memset(&dpsoftrast, 0, sizeof(dpsoftrast));
3408 dpsoftrast.bigendian = u.b[3];
3409 dpsoftrast.fb_width = width;
3410 dpsoftrast.fb_height = height;
3411 dpsoftrast.fb_depthpixels = depthpixels;
3412 dpsoftrast.fb_colorpixels[0] = colorpixels;
3413 dpsoftrast.fb_colorpixels[1] = NULL;
3414 dpsoftrast.fb_colorpixels[1] = NULL;
3415 dpsoftrast.fb_colorpixels[1] = NULL;
3416 dpsoftrast.texture_firstfree = 1;
3417 dpsoftrast.texture_end = 1;
3418 dpsoftrast.texture_max = 0;
3419 dpsoftrast.user.colormask[0] = 1;
3420 dpsoftrast.user.colormask[1] = 1;
3421 dpsoftrast.user.colormask[2] = 1;
3422 dpsoftrast.user.colormask[3] = 1;
3423 dpsoftrast.user.blendfunc[0] = GL_ONE;
3424 dpsoftrast.user.blendfunc[1] = GL_ZERO;
3425 dpsoftrast.user.depthmask = true;
3426 dpsoftrast.user.depthtest = true;
3427 dpsoftrast.user.depthfunc = GL_LEQUAL;
3428 dpsoftrast.user.scissortest = false;
3429 dpsoftrast.user.cullface = GL_BACK;
3430 dpsoftrast.user.alphatest = false;
3431 dpsoftrast.user.alphafunc = GL_GREATER;
3432 dpsoftrast.user.alphavalue = 0.5f;
3433 dpsoftrast.user.scissor[0] = 0;
3434 dpsoftrast.user.scissor[1] = 0;
3435 dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
3436 dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
3437 dpsoftrast.user.viewport[0] = 0;
3438 dpsoftrast.user.viewport[1] = 0;
3439 dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
3440 dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
3441 dpsoftrast.user.depthrange[0] = 0;
3442 dpsoftrast.user.depthrange[1] = 1;
3443 dpsoftrast.user.polygonoffset[0] = 0;
3444 dpsoftrast.user.polygonoffset[1] = 0;
3445 dpsoftrast.user.color[0] = 1;
3446 dpsoftrast.user.color[1] = 1;
3447 dpsoftrast.user.color[2] = 1;
3448 dpsoftrast.user.color[3] = 1;
3449 dpsoftrast.validate = -1;
3450 DPSOFTRAST_Validate(-1);
3451 dpsoftrast.validate = 0;
3454 void DPSOFTRAST_Shutdown(void)
3457 for (i = 0;i < dpsoftrast.texture_end;i++)
3458 if (dpsoftrast.texture[i].bytes)
3459 free(dpsoftrast.texture[i].bytes);
3460 if (dpsoftrast.texture)
3461 free(dpsoftrast.texture);
3462 memset(&dpsoftrast, 0, sizeof(dpsoftrast));