3 #include "dpsoftrast.h"
10 typedef enum bool {false, true} bool;
14 #define GL_FRONT_LEFT 0x0400
15 #define GL_FRONT_RIGHT 0x0401
16 #define GL_BACK_LEFT 0x0402
17 #define GL_BACK_RIGHT 0x0403
18 #define GL_FRONT 0x0404
19 #define GL_BACK 0x0405
20 #define GL_LEFT 0x0406
21 #define GL_RIGHT 0x0407
22 #define GL_FRONT_AND_BACK 0x0408
23 #define GL_AUX0 0x0409
24 #define GL_AUX1 0x040A
25 #define GL_AUX2 0x040B
26 #define GL_AUX3 0x040C
28 #define GL_NEVER 0x0200
29 #define GL_LESS 0x0201
30 #define GL_EQUAL 0x0202
31 #define GL_LEQUAL 0x0203
32 #define GL_GREATER 0x0204
33 #define GL_NOTEQUAL 0x0205
34 #define GL_GEQUAL 0x0206
35 #define GL_ALWAYS 0x0207
39 #define GL_SRC_COLOR 0x0300
40 #define GL_ONE_MINUS_SRC_COLOR 0x0301
41 #define GL_DST_COLOR 0x0306
42 #define GL_ONE_MINUS_DST_COLOR 0x0307
43 #define GL_SRC_ALPHA 0x0302
44 #define GL_ONE_MINUS_SRC_ALPHA 0x0303
45 #define GL_DST_ALPHA 0x0304
46 #define GL_ONE_MINUS_DST_ALPHA 0x0305
47 #define GL_SRC_ALPHA_SATURATE 0x0308
48 #define GL_CONSTANT_COLOR 0x8001
49 #define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
50 #define GL_CONSTANT_ALPHA 0x8003
51 #define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
53 typedef enum DPSOFTRAST_ARRAY_e
55 DPSOFTRAST_ARRAY_POSITION,
56 DPSOFTRAST_ARRAY_COLOR,
57 DPSOFTRAST_ARRAY_TEXCOORD0,
58 DPSOFTRAST_ARRAY_TEXCOORD1,
59 DPSOFTRAST_ARRAY_TEXCOORD2,
60 DPSOFTRAST_ARRAY_TEXCOORD3,
61 DPSOFTRAST_ARRAY_TEXCOORD4,
62 DPSOFTRAST_ARRAY_TEXCOORD5,
63 DPSOFTRAST_ARRAY_TEXCOORD6,
64 DPSOFTRAST_ARRAY_TEXCOORD7,
65 DPSOFTRAST_ARRAY_TOTAL
69 typedef struct DPSOFTRAST_Texture_s
76 DPSOFTRAST_TEXTURE_FILTER filter;
80 int mipmap[DPSOFTRAST_MAXMIPMAPS][5];
84 typedef struct DPSOFTRAST_State_User_s
100 float polygonoffset[2];
103 DPSOFTRAST_State_User;
105 typedef struct DPSOFTRAST_State_Draw_Span_s
107 int start; // pixel index
108 int length; // pixel count
109 int startx; // usable range (according to pixelmask)
110 int endx; // usable range (according to pixelmask)
111 int mip; // which mipmap of the texture(s) to use on this
112 unsigned char *pixelmask; // true for pixels that passed depth test, false for others
113 // [0][n][] is start interpolant values (projected)
114 // [1][n][] is end interpolant values (projected)
115 // [0][DPSOFTRAST_ARRAY_TOTAL][] is start screencoord4f
116 // [1][DPSOFTRAST_ARRAY_TOTAL][] is end screencoord4f
117 // NOTE: screencoord4f[3] is W (basically 1/Z), useful for depthbuffer
118 float data[2][DPSOFTRAST_ARRAY_TOTAL+1][4];
120 DPSOFTRAST_State_Draw_Span;
122 #define DPSOFTRAST_DRAW_MAXSPANQUEUE 1024
124 typedef struct DPSOFTRAST_State_Draw_s
128 float *in_array4f[DPSOFTRAST_ARRAY_TOTAL];
129 float *post_array4f[DPSOFTRAST_ARRAY_TOTAL];
130 float *screencoord4f;
132 // spans are queued in this structure for dispatch to the pixel shader,
133 // partly to improve cache locality, partly for batching purposes, spans
134 // are flushed before DrawTriangles returns to caller
136 DPSOFTRAST_State_Draw_Span spanqueue[DPSOFTRAST_DRAW_MAXSPANQUEUE];
138 DPSOFTRAST_State_Draw;
140 #define DPSOFTRAST_VALIDATE_FB 1
141 #define DPSOFTRAST_VALIDATE_DEPTHFUNC 2
142 #define DPSOFTRAST_VALIDATE_BLENDFUNC 4
143 #define DPSOFTRAST_VALIDATE_DRAW (DPSOFTRAST_VALIDATE_FB | DPSOFTRAST_VALIDATE_DEPTHFUNC | DPSOFTRAST_VALIDATE_BLENDFUNC)
145 typedef enum DPSOFTRAST_BLENDMODE_e
147 DPSOFTRAST_BLENDMODE_OPAQUE,
148 DPSOFTRAST_BLENDMODE_ALPHA,
149 DPSOFTRAST_BLENDMODE_ADDALPHA,
150 DPSOFTRAST_BLENDMODE_ADD,
151 DPSOFTRAST_BLENDMODE_INVMOD,
152 DPSOFTRAST_BLENDMODE_MUL,
153 DPSOFTRAST_BLENDMODE_MUL2,
154 DPSOFTRAST_BLENDMODE_SUBALPHA,
155 DPSOFTRAST_BLENDMODE_PSEUDOALPHA,
156 DPSOFTRAST_BLENDMODE_TOTAL
158 DPSOFTRAST_BLENDMODE;
160 typedef struct DPSOFTRAST_State_s
162 // DPSOFTRAST_VALIDATE_ flags
168 unsigned int *fb_depthpixels;
169 unsigned int *fb_colorpixels[4];
171 const float *pointer_vertex3f;
172 const float *pointer_color4f;
173 const unsigned char *pointer_color4ub;
174 const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
177 int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
178 int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
179 DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
182 int shader_permutation;
183 float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4];
184 int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
186 // derived values (DPSOFTRAST_VALIDATE_FB)
187 int fb_clearscissor[4];
189 int fb_viewportscissor[4];
190 float fb_viewportcenter[2];
191 float fb_viewportscale[2];
193 // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
196 // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
201 int texture_firstfree;
202 DPSOFTRAST_Texture *texture;
207 const char *errorstring;
209 DPSOFTRAST_State_User user;
211 DPSOFTRAST_State_Draw draw;
215 DPSOFTRAST_State dpsoftrast;
217 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
218 #define DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a) (((int)(r * 255.0f + 0.5f) << 16) | ((int)(g * 255.0f + 0.5f) << 8) | (int)(b * 255.0f + 0.5f) | ((int)(a * 255.0f + 0.5f) << 24))
219 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
220 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
222 void DPSOFTRAST_RecalcFB(void)
224 // calculate framebuffer scissor, viewport, viewport clipped by scissor,
225 // and viewport projection values
226 int x1, x2, x3, x4, x5, x6;
227 int y1, y2, y3, y4, y5, y6;
228 x1 = dpsoftrast.user.scissor[0];
229 x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
230 x3 = dpsoftrast.user.viewport[0];
231 x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
232 y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
233 y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
234 y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
235 y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
236 if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
238 if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
240 if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
242 if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
244 if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
245 x5 = x1;if (x5 < x3) x5 = x3;
246 x6 = x2;if (x6 > x4) x4 = x4;
247 y5 = y1;if (y5 < y3) y5 = y3;
248 y6 = y2;if (y6 > y4) y6 = y4;
249 dpsoftrast.fb_clearscissor[0] = x1;
250 dpsoftrast.fb_clearscissor[1] = y1;
251 dpsoftrast.fb_clearscissor[2] = x2 - x1;
252 dpsoftrast.fb_clearscissor[3] = y2 - y1;
253 dpsoftrast.fb_viewport[0] = x3;
254 dpsoftrast.fb_viewport[1] = y3;
255 dpsoftrast.fb_viewport[2] = x4 - x3;
256 dpsoftrast.fb_viewport[3] = y4 - y3;
257 dpsoftrast.fb_viewportscissor[0] = x5;
258 dpsoftrast.fb_viewportscissor[1] = y5;
259 dpsoftrast.fb_viewportscissor[2] = x6 - x5;
260 dpsoftrast.fb_viewportscissor[3] = y6 - y5;
261 dpsoftrast.fb_viewportcenter[0] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
262 dpsoftrast.fb_viewportcenter[1] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
263 dpsoftrast.fb_viewportscale[0] = 0.5f * dpsoftrast.user.viewport[2];
264 dpsoftrast.fb_viewportscale[1] = -0.5f * dpsoftrast.user.viewport[3];
267 void DPSOFTRAST_RecalcDepthFunc(void)
269 dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
272 int blendmodetable[][4] =
274 {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
275 {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
276 {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
277 {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
278 {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
279 {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
280 {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
281 {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
282 {DPSOFTRAST_BLENDMODE_PSEUDOALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, false},
283 {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
286 void DPSOFTRAST_RecalcBlendFunc(void)
289 dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE;
290 for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++)
292 if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3])
294 dpsoftrast.fb_blendmode = blendmodetable[i][0];
300 #define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0)
302 void DPSOFTRAST_Validate(int mask)
304 mask &= dpsoftrast.validate;
307 if (mask & DPSOFTRAST_VALIDATE_FB)
309 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB;
310 DPSOFTRAST_RecalcFB();
312 if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
314 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
315 DPSOFTRAST_RecalcDepthFunc();
317 if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
319 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
320 DPSOFTRAST_RecalcBlendFunc();
324 DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
326 if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
327 return &dpsoftrast.texture[index];
331 int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
340 int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
341 int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
342 DPSOFTRAST_Texture *texture;
343 if (width*height*depth < 1)
345 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
348 if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
350 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
355 case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
356 case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
357 case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
359 case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
360 if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
362 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
367 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
370 if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
372 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
377 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
379 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
382 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
384 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
387 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
389 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
392 if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
394 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
397 if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
399 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
402 // find first empty slot in texture array
403 for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
404 if (!dpsoftrast.texture[texnum].bytes)
406 dpsoftrast.texture_firstfree = texnum + 1;
407 if (dpsoftrast.texture_max <= texnum)
409 // expand texture array as needed
410 if (dpsoftrast.texture_max < 1024)
411 dpsoftrast.texture_max = 1024;
413 dpsoftrast.texture_max *= 2;
414 dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
416 if (dpsoftrast.texture_end <= texnum)
417 dpsoftrast.texture_end = texnum + 1;
418 texture = &dpsoftrast.texture[texnum];
419 memset(texture, 0, sizeof(*texture));
420 texture->flags = flags;
421 texture->width = width;
422 texture->height = height;
423 texture->depth = depth;
424 texture->sides = sides;
435 s = w * h * d * sides * 4;
436 texture->mipmap[mipmaps][0] = size;
437 texture->mipmap[mipmaps][1] = s;
438 texture->mipmap[mipmaps][2] = w;
439 texture->mipmap[mipmaps][3] = h;
440 texture->mipmap[mipmaps][4] = d;
443 if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
449 texture->mipmaps = mipmaps;
450 texture->size = size;
452 // allocate the pixels now
453 texture->bytes = calloc(1, size);
457 void DPSOFTRAST_Texture_Free(int index)
459 DPSOFTRAST_Texture *texture;
460 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
462 free(texture->bytes);
463 texture->bytes = NULL;
464 memset(texture, 0, sizeof(*texture));
465 // adjust the free range and used range
466 if (dpsoftrast.texture_firstfree > index)
467 dpsoftrast.texture_firstfree = index;
468 while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
469 dpsoftrast.texture_end--;
471 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
473 int i, x, y, z, w, layer0, layer1, row0, row1;
474 unsigned char *o, *i0, *i1, *i2, *i3;
475 DPSOFTRAST_Texture *texture;
476 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
477 if (texture->mipmaps <= 1)
479 for (i = 1;i < texture->mipmaps;i++)
481 for (z = 0;z < texture->mipmap[i][4];z++)
485 if (layer1 >= texture->mipmap[i-1][4])
486 layer1 = texture->mipmap[i-1][4]-1;
487 for (y = 0;y < texture->mipmap[i][3];y++)
491 if (row1 >= texture->mipmap[i-1][3])
492 row1 = texture->mipmap[i-1][3]-1;
493 o = texture->bytes + texture->mipmap[i ][0] + 4*(texture->mipmap[i ][3] * texture->mipmap[i ][2] * z + texture->mipmap[i ][2] * y );
494 i0 = texture->bytes + texture->mipmap[i-1][0] + 4*(texture->mipmap[i-1][3] * texture->mipmap[i-1][2] * layer0 + texture->mipmap[i-1][2] * row0);
495 i1 = texture->bytes + texture->mipmap[i-1][0] + 4*(texture->mipmap[i-1][3] * texture->mipmap[i-1][2] * layer0 + texture->mipmap[i-1][2] * row1);
496 i2 = texture->bytes + texture->mipmap[i-1][0] + 4*(texture->mipmap[i-1][3] * texture->mipmap[i-1][2] * layer1 + texture->mipmap[i-1][2] * row0);
497 i3 = texture->bytes + texture->mipmap[i-1][0] + 4*(texture->mipmap[i-1][3] * texture->mipmap[i-1][2] * layer1 + texture->mipmap[i-1][2] * row1);
498 w = texture->mipmap[i][2];
501 if (texture->mipmap[i-1][2] > 1)
503 // average 3D texture
504 for (x = 0;x < w;x++)
506 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
507 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
508 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
509 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
514 // average 3D mipmap with parent width == 1
515 for (x = 0;x < w;x++)
517 o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
518 o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
519 o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
520 o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
526 if (texture->mipmap[i-1][2] > 1)
528 // average 2D texture (common case)
529 for (x = 0;x < w;x++)
531 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
532 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
533 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
534 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
539 // 2D texture with parent width == 1
540 for (x = 0;x < w;x++)
542 o[0] = (i0[0] + i1[0] + 1) >> 1;
543 o[1] = (i0[1] + i1[1] + 1) >> 1;
544 o[2] = (i0[2] + i1[2] + 1) >> 1;
545 o[3] = (i0[3] + i1[3] + 1) >> 1;
553 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
555 DPSOFTRAST_Texture *texture;
556 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
560 dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
562 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
564 DPSOFTRAST_Texture *texture;
565 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
567 memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
568 DPSOFTRAST_Texture_CalculateMipmaps(index);
570 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
572 DPSOFTRAST_Texture *texture;
573 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
574 return texture->width;
576 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
578 DPSOFTRAST_Texture *texture;
579 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
580 return texture->height;
582 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
584 DPSOFTRAST_Texture *texture;
585 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
586 return texture->depth;
588 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
590 DPSOFTRAST_Texture *texture;
591 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
592 return texture->bytes;
594 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
596 DPSOFTRAST_Texture *texture;
597 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
598 if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
600 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
603 texture->filter = filter;
606 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
608 dpsoftrast.fb_width = width;
609 dpsoftrast.fb_height = height;
610 dpsoftrast.fb_depthpixels = depthpixels;
611 dpsoftrast.fb_colorpixels[0] = colorpixels0;
612 dpsoftrast.fb_colorpixels[1] = colorpixels1;
613 dpsoftrast.fb_colorpixels[2] = colorpixels2;
614 dpsoftrast.fb_colorpixels[3] = colorpixels3;
616 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
618 dpsoftrast.user.viewport[0] = x;
619 dpsoftrast.user.viewport[1] = y;
620 dpsoftrast.user.viewport[2] = width;
621 dpsoftrast.user.viewport[3] = height;
622 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
624 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
626 int i, x1, y1, x2, y2, w, h, x, y;
629 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
630 x1 = dpsoftrast.fb_clearscissor[0];
631 y1 = dpsoftrast.fb_clearscissor[1];
632 x2 = dpsoftrast.fb_clearscissor[2];
633 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
638 // FIXME: honor dpsoftrast.fb_colormask?
639 c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
640 for (i = 0;i < 4;i++)
642 if (!dpsoftrast.fb_colorpixels[i])
644 for (y = y1;y < y2;y++)
646 p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
647 for (x = x1;x < x2;x++)
652 void DPSOFTRAST_ClearDepth(float d)
654 int x1, y1, x2, y2, w, h, x, y;
657 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
658 x1 = dpsoftrast.fb_clearscissor[0];
659 y1 = dpsoftrast.fb_clearscissor[1];
660 x2 = dpsoftrast.fb_clearscissor[2];
661 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
666 c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
667 for (y = y1;y < y2;y++)
669 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
670 for (x = x1;x < x2;x++)
674 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
676 dpsoftrast.user.colormask[0] = r != 0;
677 dpsoftrast.user.colormask[1] = g != 0;
678 dpsoftrast.user.colormask[2] = b != 0;
679 dpsoftrast.user.colormask[3] = a != 0;
680 dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
682 void DPSOFTRAST_DepthTest(int enable)
684 dpsoftrast.user.depthtest = enable;
685 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
687 void DPSOFTRAST_ScissorTest(int enable)
689 dpsoftrast.user.scissortest = enable;
690 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
692 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
694 dpsoftrast.user.scissor[0] = x;
695 dpsoftrast.user.scissor[1] = y;
696 dpsoftrast.user.scissor[2] = width;
697 dpsoftrast.user.scissor[3] = height;
698 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
701 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
704 dpsoftrast.user.blendfunc[0] = smodulate;
705 dpsoftrast.user.blendfunc[1] = dmodulate;
706 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
708 void DPSOFTRAST_BlendSubtract(int enable)
710 dpsoftrast.user.blendsubtract = enable != 0;
711 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
713 void DPSOFTRAST_DepthMask(int enable)
715 dpsoftrast.user.depthmask = enable;
717 void DPSOFTRAST_DepthFunc(int comparemode)
720 dpsoftrast.user.depthfunc = comparemode;
722 void DPSOFTRAST_DepthRange(float range0, float range1)
724 dpsoftrast.user.depthrange[0] = range0;
725 dpsoftrast.user.depthrange[1] = range1;
727 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
729 dpsoftrast.user.polygonoffset[0] = alongnormal;
730 dpsoftrast.user.polygonoffset[1] = intoview;
732 void DPSOFTRAST_CullFace(int mode)
735 dpsoftrast.user.cullface = mode;
737 void DPSOFTRAST_AlphaTest(float enable)
739 dpsoftrast.user.alphatest = enable;
741 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
744 dpsoftrast.user.alphafunc = alphafunc;
745 dpsoftrast.user.alphavalue = alphavalue;
747 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
749 dpsoftrast.user.color[0] = r;
750 dpsoftrast.user.color[1] = g;
751 dpsoftrast.user.color[2] = b;
752 dpsoftrast.user.color[3] = a;
754 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
756 int outstride = blockwidth * 4;
757 int instride = dpsoftrast.fb_width * 4;
760 int bx2 = blockx + blockwidth;
761 int by2 = blocky + blockheight;
766 unsigned char *inpixels;
769 if (bx1 < 0) bx1 = 0;
770 if (by1 < 0) by1 = 0;
771 if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
772 if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
775 inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
776 if (dpsoftrast.bigendian)
778 for (y = by1;y < by2;y++)
780 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
781 o = (unsigned char *)outpixels + (y - by1) * outstride;
782 for (x = bx1;x < bx2;x++)
795 for (y = by1;y < by2;y++)
797 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
798 o = (unsigned char *)outpixels + (y - by1) * outstride;
804 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
808 int tx2 = tx + width;
809 int ty2 = ty + height;
812 int sx2 = sx + width;
813 int sy2 = sy + height;
823 unsigned int *spixels;
824 unsigned int *tpixels;
825 DPSOFTRAST_Texture *texture;
826 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
827 if (mip < 0 || mip >= texture->mipmaps) return;
828 spixels = dpsoftrast.fb_colorpixels[0];
829 swidth = dpsoftrast.fb_width;
830 sheight = dpsoftrast.fb_height;
831 tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
832 twidth = texture->mipmap[mip][2];
833 theight = texture->mipmap[mip][3];
834 if (tx1 < 0) tx1 = 0;
835 if (ty1 < 0) ty1 = 0;
836 if (tx2 > twidth) tx2 = twidth;
837 if (ty2 > theight) ty2 = theight;
838 if (sx1 < 0) sx1 = 0;
839 if (sy1 < 0) sy1 = 0;
840 if (sx2 > swidth) sx2 = swidth;
841 if (sy2 > sheight) sy2 = sheight;
846 if (tw > sw) tw = sw;
847 if (th > sh) th = sh;
848 if (tw < 1 || th < 1)
850 for (y = 0;y < th;y++)
851 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
852 if (texture->mipmaps > 1)
853 DPSOFTRAST_Texture_CalculateMipmaps(index);
855 void DPSOFTRAST_SetTexture(int unitnum, int index)
857 DPSOFTRAST_Texture *texture;
858 if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
860 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
863 texture = DPSOFTRAST_Texture_GetByIndex(index);
864 if (index && !texture)
866 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
869 dpsoftrast.texbound[unitnum] = texture;
872 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
874 dpsoftrast.pointer_vertex3f = vertex3f;
875 dpsoftrast.stride_vertex = stride;
877 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
879 dpsoftrast.pointer_color4f = color4f;
880 dpsoftrast.pointer_color4ub = NULL;
881 dpsoftrast.stride_color = stride;
883 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
885 dpsoftrast.pointer_color4f = NULL;
886 dpsoftrast.pointer_color4ub = color4ub;
887 dpsoftrast.stride_color = stride;
889 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
891 dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
892 dpsoftrast.components_texcoord[unitnum] = numcomponents;
893 dpsoftrast.stride_texcoord[unitnum] = stride;
896 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
898 dpsoftrast.shader_mode = mode;
899 dpsoftrast.shader_permutation = permutation;
901 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
903 dpsoftrast.uniform4f[index*4+0] = v0;
904 dpsoftrast.uniform4f[index*4+1] = v1;
905 dpsoftrast.uniform4f[index*4+2] = v2;
906 dpsoftrast.uniform4f[index*4+3] = v3;
908 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v)
910 dpsoftrast.uniform4f[index*4+0] = v[0];
911 dpsoftrast.uniform4f[index*4+1] = v[1];
912 dpsoftrast.uniform4f[index*4+2] = v[2];
913 dpsoftrast.uniform4f[index*4+3] = v[3];
915 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, 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];
921 dpsoftrast.uniform4f[index*4+4] = v[4];
922 dpsoftrast.uniform4f[index*4+5] = v[5];
923 dpsoftrast.uniform4f[index*4+6] = v[6];
924 dpsoftrast.uniform4f[index*4+7] = v[7];
925 dpsoftrast.uniform4f[index*4+8] = v[8];
926 dpsoftrast.uniform4f[index*4+9] = v[9];
927 dpsoftrast.uniform4f[index*4+10] = v[10];
928 dpsoftrast.uniform4f[index*4+11] = v[11];
929 dpsoftrast.uniform4f[index*4+12] = v[12];
930 dpsoftrast.uniform4f[index*4+13] = v[13];
931 dpsoftrast.uniform4f[index*4+14] = v[14];
932 dpsoftrast.uniform4f[index*4+15] = v[15];
934 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
936 dpsoftrast.uniform1i[index] = i0;
939 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors)
947 const unsigned char *b;
948 dpsoftrast.draw.numvertices = numvertices;
949 if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
951 if (dpsoftrast.draw.maxvertices < 4096)
952 dpsoftrast.draw.maxvertices = 4096;
953 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
954 dpsoftrast.draw.maxvertices *= 2;
955 if (dpsoftrast.draw.in_array4f[0])
956 free(dpsoftrast.draw.in_array4f[0]);
957 data = calloc(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
958 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
959 dpsoftrast.draw.in_array4f[i] = data;
960 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
961 dpsoftrast.draw.post_array4f[i] = data;
962 dpsoftrast.draw.screencoord4f = data;
963 data += dpsoftrast.draw.maxvertices * 4;
965 stride = dpsoftrast.stride_vertex;
966 v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
967 p = dpsoftrast.draw.in_array4f[0];
968 for (i = 0;i < numvertices;i++)
975 v = (const float *)((const unsigned char *)v + stride);
979 if (dpsoftrast.pointer_color4f)
981 stride = dpsoftrast.stride_color;
982 v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
983 p = dpsoftrast.draw.in_array4f[1];
984 for (i = 0;i < numvertices;i++)
991 v = (const float *)((const unsigned char *)v + stride);
994 else if (dpsoftrast.pointer_color4ub)
996 stride = dpsoftrast.stride_color;
997 b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
998 p = dpsoftrast.draw.in_array4f[1];
999 for (i = 0;i < numvertices;i++)
1001 p[0] = b[0] * (1.0f / 255.0f);
1002 p[1] = b[1] * (1.0f / 255.0f);
1003 p[2] = b[2] * (1.0f / 255.0f);
1004 p[3] = b[3] * (1.0f / 255.0f);
1006 b = (const unsigned char *)((const unsigned char *)b + stride);
1011 v = dpsoftrast.user.color;
1012 p = dpsoftrast.draw.in_array4f[1];
1013 for (i = 0;i < numvertices;i++)
1023 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL-2;j++)
1025 if (dpsoftrast.pointer_texcoordf[j])
1027 stride = dpsoftrast.stride_texcoord[j];
1028 v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1029 p = dpsoftrast.draw.in_array4f[j+2];
1030 switch(dpsoftrast.components_texcoord[j])
1033 for (i = 0;i < numvertices;i++)
1040 v = (const float *)((const unsigned char *)v + stride);
1044 for (i = 0;i < numvertices;i++)
1051 v = (const float *)((const unsigned char *)v + stride);
1055 for (i = 0;i < numvertices;i++)
1062 v = (const float *)((const unsigned char *)v + stride);
1070 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1075 memcpy(matrix, inmatrix16f, sizeof(float[16]));
1076 for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1078 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1079 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1080 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1081 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1085 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1087 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1090 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1092 // NOTE: this is used both as a whole mesh transform function and a
1093 // per-triangle transform function (for clipped triangles), accordingly
1094 // it should not crash on divide by 0 but the result of divide by 0 is
1099 float viewportcenter[4];
1100 float viewportscale[4];
1101 viewportscale[0] = dpsoftrast.fb_viewportscale[0];
1102 viewportscale[1] = dpsoftrast.fb_viewportscale[1];
1103 viewportscale[2] = 0.5f;
1104 viewportscale[3] = 0.0f;
1105 viewportcenter[0] = dpsoftrast.fb_viewportcenter[0];
1106 viewportcenter[1] = dpsoftrast.fb_viewportcenter[1];
1107 viewportcenter[2] = 0.5f;
1108 viewportcenter[3] = 0.0f;
1109 for (i = 0;i < numitems;i++)
1120 out4f[0] = viewportcenter[0] + viewportscale[0] * in4f[0] * w;
1121 out4f[1] = viewportcenter[1] + viewportscale[1] * in4f[1] * w;
1122 out4f[2] = viewportcenter[2] + viewportscale[2] * in4f[2] * w;
1123 out4f[3] = viewportcenter[3] + viewportscale[3] * in4f[3] * w;
1130 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1135 int w = dpsoftrast.fb_width;
1138 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1140 bounds[0] = dpsoftrast.fb_viewportscissor[0];
1141 bounds[1] = dpsoftrast.fb_viewportscissor[1];
1142 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1143 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1148 for (i = 0;i <= 128;i++)
1151 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1153 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1154 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1155 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1157 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1158 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1159 pixels[y*w+x] = 0xFFFFFFFF;
1163 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span *span, float *zf)
1166 int startx = span->startx;
1167 int endx = span->endx;
1168 float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1169 float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1170 // TODO: optimize by approximating every 8 pixels?
1171 for (x = startx;x < endx;x++)
1172 zf[x] = 1.0f / (w + wslope * x);
1175 void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span *span, const float *in4f)
1178 int startx = span->startx;
1179 int endx = span->endx;
1182 unsigned char *pixelmask = span->pixelmask;
1183 unsigned char *pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1186 pixel += span->start * 4;
1187 // handle alphatest now (this affects depth writes too)
1188 if (dpsoftrast.user.alphatest)
1189 for (x = startx;x < endx;x++)
1190 if (in4f[x*4+3] < 0.5f)
1191 pixelmask[x] = false;
1192 // FIXME: this does not handle bigendian
1193 switch(dpsoftrast.fb_blendmode)
1195 case DPSOFTRAST_BLENDMODE_OPAQUE:
1196 for (x = startx;x < endx;x++)
1200 d[0] = (int)(in4f[x*4+2]*255.0f);if (d[0] > 255) d[0] = 255;
1201 d[1] = (int)(in4f[x*4+1]*255.0f);if (d[1] > 255) d[1] = 255;
1202 d[2] = (int)(in4f[x*4+0]*255.0f);if (d[2] > 255) d[2] = 255;
1203 d[3] = (int)(in4f[x*4+3]*255.0f);if (d[3] > 255) d[3] = 255;
1204 pixel[x*4+0] = d[0];
1205 pixel[x*4+1] = d[1];
1206 pixel[x*4+2] = d[2];
1207 pixel[x*4+3] = d[3];
1210 case DPSOFTRAST_BLENDMODE_ALPHA:
1211 for (x = startx;x < endx;x++)
1215 a = in4f[x*4+3] * 255.0f;
1216 b = 1.0f - in4f[x*4+3];
1217 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1218 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1219 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1220 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1221 pixel[x*4+0] = d[0];
1222 pixel[x*4+1] = d[1];
1223 pixel[x*4+2] = d[2];
1224 pixel[x*4+3] = d[3];
1227 case DPSOFTRAST_BLENDMODE_ADDALPHA:
1228 for (x = startx;x < endx;x++)
1232 a = in4f[x*4+3] * 255.0f;
1233 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1234 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1235 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1236 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1237 pixel[x*4+0] = d[0];
1238 pixel[x*4+1] = d[1];
1239 pixel[x*4+2] = d[2];
1240 pixel[x*4+3] = d[3];
1243 case DPSOFTRAST_BLENDMODE_ADD:
1244 for (x = startx;x < endx;x++)
1248 d[0] = (int)(in4f[x*4+2]*255.0f+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1249 d[1] = (int)(in4f[x*4+1]*255.0f+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1250 d[2] = (int)(in4f[x*4+0]*255.0f+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1251 d[3] = (int)(in4f[x*4+3]*255.0f+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1252 pixel[x*4+0] = d[0];
1253 pixel[x*4+1] = d[1];
1254 pixel[x*4+2] = d[2];
1255 pixel[x*4+3] = d[3];
1258 case DPSOFTRAST_BLENDMODE_INVMOD:
1259 for (x = startx;x < endx;x++)
1263 d[0] = (int)((1.0f-in4f[x*4+2])*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1264 d[1] = (int)((1.0f-in4f[x*4+1])*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1265 d[2] = (int)((1.0f-in4f[x*4+0])*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1266 d[3] = (int)((1.0f-in4f[x*4+3])*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1267 pixel[x*4+0] = d[0];
1268 pixel[x*4+1] = d[1];
1269 pixel[x*4+2] = d[2];
1270 pixel[x*4+3] = d[3];
1273 case DPSOFTRAST_BLENDMODE_MUL:
1274 for (x = startx;x < endx;x++)
1278 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1279 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1280 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1281 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1282 pixel[x*4+0] = d[0];
1283 pixel[x*4+1] = d[1];
1284 pixel[x*4+2] = d[2];
1285 pixel[x*4+3] = d[3];
1288 case DPSOFTRAST_BLENDMODE_MUL2:
1289 for (x = startx;x < endx;x++)
1293 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]*2.0f);if (d[0] > 255) d[0] = 255;
1294 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]*2.0f);if (d[1] > 255) d[1] = 255;
1295 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]*2.0f);if (d[2] > 255) d[2] = 255;
1296 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]*2.0f);if (d[3] > 255) d[3] = 255;
1297 pixel[x*4+0] = d[0];
1298 pixel[x*4+1] = d[1];
1299 pixel[x*4+2] = d[2];
1300 pixel[x*4+3] = d[3];
1303 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1304 for (x = startx;x < endx;x++)
1308 a = in4f[x*4+3] * -255.0f;
1309 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;if (d[0] < 0) d[0] = 0;
1310 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;if (d[1] < 0) d[1] = 0;
1311 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;if (d[2] < 0) d[2] = 0;
1312 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;if (d[3] < 0) d[3] = 0;
1313 pixel[x*4+0] = d[0];
1314 pixel[x*4+1] = d[1];
1315 pixel[x*4+2] = d[2];
1316 pixel[x*4+3] = d[3];
1319 case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1320 for (x = startx;x < endx;x++)
1325 b = 1.0f - in4f[x*4+3];
1326 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1327 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1328 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1329 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1330 pixel[x*4+0] = d[0];
1331 pixel[x*4+1] = d[1];
1332 pixel[x*4+2] = d[2];
1333 pixel[x*4+3] = d[3];
1339 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span *span, float *out4f, int texunitindex, int arrayindex, const float *zf)
1342 int startx = span->startx;
1343 int endx = span->endx;
1362 unsigned char *pixelbase;
1363 unsigned char *pixel[4];
1364 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1365 // if no texture is bound, just fill it with white
1368 for (x = startx;x < endx;x++)
1370 out4f[x*4+0] = 1.0f;
1371 out4f[x*4+1] = 1.0f;
1372 out4f[x*4+2] = 1.0f;
1373 out4f[x*4+3] = 1.0f;
1377 // if this mipmap of the texture is 1 pixel, just fill it with that color
1379 if (mip >= texture->mipmaps)
1380 mip = texture->mipmaps - 1;
1381 if (texture->mipmap[mip][1] == 4)
1383 c[0] = texture->bytes[2] * (1.0f/255.0f);
1384 c[1] = texture->bytes[1] * (1.0f/255.0f);
1385 c[2] = texture->bytes[0] * (1.0f/255.0f);
1386 c[3] = texture->bytes[3] * (1.0f/255.0f);
1387 for (x = startx;x < endx;x++)
1389 out4f[x*4+0] = c[0];
1390 out4f[x*4+1] = c[1];
1391 out4f[x*4+2] = c[2];
1392 out4f[x*4+3] = c[3];
1396 filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1397 data[0] = span->data[0][arrayindex][0];
1398 data[1] = span->data[0][arrayindex][1];
1399 data[2] = span->data[0][arrayindex][2];
1400 data[3] = span->data[0][arrayindex][3];
1401 slope[0] = span->data[1][arrayindex][0];
1402 slope[1] = span->data[1][arrayindex][1];
1403 slope[2] = span->data[1][arrayindex][2];
1404 slope[3] = span->data[1][arrayindex][3];
1405 flags = texture->flags;
1406 pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
1407 tcscale[0] = texture->mipmap[mip][2];
1408 tcscale[1] = texture->mipmap[mip][3];
1409 tciwidth = texture->mipmap[mip][2];
1412 tcimax[0] = texture->mipmap[mip][2]-1;
1413 tcimax[1] = texture->mipmap[mip][3]-1;
1414 tciwrapmask[0] = texture->mipmap[mip][2]-1;
1415 tciwrapmask[1] = texture->mipmap[mip][3]-1;
1418 if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1420 for (x = startx;x < endx;x++)
1423 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0] - 0.5f;
1424 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1] - 0.5f;
1425 tci[0] = (int)floor(tc[0]);
1426 tci[1] = (int)floor(tc[1]);
1427 tci1[0] = tci[0] + 1;
1428 tci1[1] = tci[1] + 1;
1429 frac[0] = tc[0] - tci[0];ifrac[0] = 1.0f - frac[0];
1430 frac[1] = tc[1] - tci[1];ifrac[1] = 1.0f - frac[1];
1431 lerp[0] = ifrac[0]*ifrac[1];
1432 lerp[1] = frac[0]*ifrac[1];
1433 lerp[2] = ifrac[0]* frac[1];
1434 lerp[3] = frac[0]* frac[1];
1435 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1436 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1437 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1438 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1439 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1440 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1441 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1442 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1443 c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 255.0f);
1444 c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 255.0f);
1445 c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 255.0f);
1446 c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 255.0f);
1447 out4f[x*4+0] = c[0];
1448 out4f[x*4+1] = c[1];
1449 out4f[x*4+2] = c[2];
1450 out4f[x*4+3] = c[3];
1455 for (x = startx;x < endx;x++)
1458 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0] - 0.5f;
1459 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1] - 0.5f;
1460 tci[0] = (int)floor(tc[0]);
1461 tci[1] = (int)floor(tc[1]);
1462 tci1[0] = tci[0] + 1;
1463 tci1[1] = tci[1] + 1;
1464 frac[0] = tc[0] - tci[0];ifrac[0] = 1.0f - frac[0];
1465 frac[1] = tc[1] - tci[1];ifrac[1] = 1.0f - frac[1];
1466 lerp[0] = ifrac[0]*ifrac[1];
1467 lerp[1] = frac[0]*ifrac[1];
1468 lerp[2] = ifrac[0]* frac[1];
1469 lerp[3] = frac[0]* frac[1];
1470 tci[0] &= tciwrapmask[0];
1471 tci[1] &= tciwrapmask[1];
1472 tci1[0] &= tciwrapmask[0];
1473 tci1[1] &= tciwrapmask[1];
1474 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1475 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1476 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1477 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1478 c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 255.0f);
1479 c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 255.0f);
1480 c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 255.0f);
1481 c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 255.0f);
1482 out4f[x*4+0] = c[0];
1483 out4f[x*4+1] = c[1];
1484 out4f[x*4+2] = c[2];
1485 out4f[x*4+3] = c[3];
1491 if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1493 for (x = startx;x < endx;x++)
1496 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0] - 0.5f;
1497 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1] - 0.5f;
1498 tci[0] = (int)floor(tc[0]);
1499 tci[1] = (int)floor(tc[1]);
1500 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1501 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1502 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1503 c[0] = pixel[0][2] * (1.0f / 255.0f);
1504 c[1] = pixel[0][1] * (1.0f / 255.0f);
1505 c[2] = pixel[0][0] * (1.0f / 255.0f);
1506 c[3] = pixel[0][3] * (1.0f / 255.0f);
1507 out4f[x*4+0] = c[0];
1508 out4f[x*4+1] = c[1];
1509 out4f[x*4+2] = c[2];
1510 out4f[x*4+3] = c[3];
1515 for (x = startx;x < endx;x++)
1518 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0] - 0.5f;
1519 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1] - 0.5f;
1520 tci[0] = (int)floor(tc[0]);
1521 tci[1] = (int)floor(tc[1]);
1522 tci[0] &= tciwrapmask[0];
1523 tci[1] &= tciwrapmask[1];
1524 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1525 c[0] = pixel[0][2] * (1.0f / 255.0f);
1526 c[1] = pixel[0][1] * (1.0f / 255.0f);
1527 c[2] = pixel[0][0] * (1.0f / 255.0f);
1528 c[3] = pixel[0][3] * (1.0f / 255.0f);
1529 out4f[x*4+0] = c[0];
1530 out4f[x*4+1] = c[1];
1531 out4f[x*4+2] = c[2];
1532 out4f[x*4+3] = c[3];
1538 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *in4f, int arrayindex, const float *zf)
1541 int startx = span->startx;
1542 int endx = span->endx;
1547 data[0] = span->data[0][arrayindex][0];
1548 data[1] = span->data[0][arrayindex][1];
1549 data[2] = span->data[0][arrayindex][2];
1550 data[3] = span->data[0][arrayindex][3];
1551 slope[0] = span->data[1][arrayindex][0];
1552 slope[1] = span->data[1][arrayindex][1];
1553 slope[2] = span->data[1][arrayindex][2];
1554 slope[3] = span->data[1][arrayindex][3];
1555 for (x = startx;x < endx;x++)
1558 c[0] = (data[0] + slope[0]*x) * z;
1559 c[1] = (data[1] + slope[1]*x) * z;
1560 c[2] = (data[2] + slope[2]*x) * z;
1561 c[3] = (data[3] + slope[3]*x) * z;
1562 out4f[x*4+0] = in4f[x*4+0] * c[0];
1563 out4f[x*4+1] = in4f[x*4+1] * c[1];
1564 out4f[x*4+2] = in4f[x*4+2] * c[2];
1565 out4f[x*4+3] = in4f[x*4+3] * c[3];
1569 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
1571 int x, startx = span->startx, endx = span->endx;
1572 float c[4], localcolor[4];
1573 localcolor[0] = subcolor[0];
1574 localcolor[1] = subcolor[1];
1575 localcolor[2] = subcolor[2];
1576 localcolor[3] = subcolor[3];
1577 for (x = startx;x < endx;x++)
1579 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
1580 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
1581 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
1582 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
1583 out4f[x*4+0] = ina4f[x*4+0] + c[0];
1584 out4f[x*4+1] = ina4f[x*4+1] + c[1];
1585 out4f[x*4+2] = ina4f[x*4+2] + c[2];
1586 out4f[x*4+3] = ina4f[x*4+3] + c[3];
1590 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *in4f, const float *color)
1592 int x, startx = span->startx, endx = span->endx;
1593 float localcolor[4], ilerp, lerp;
1594 localcolor[0] = color[0];
1595 localcolor[1] = color[1];
1596 localcolor[2] = color[2];
1597 localcolor[3] = color[3];
1598 ilerp = 1.0f - localcolor[3];
1599 lerp = localcolor[3];
1600 for (x = startx;x < endx;x++)
1602 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
1603 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
1604 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
1605 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
1609 void DPSOFTRAST_Draw_Span_Lightmap(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *lightmap)
1611 int x, startx = span->startx, endx = span->endx;
1612 float Color_Ambient[4], Color_Diffuse[4];
1613 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1614 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1615 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1616 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1617 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
1618 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
1619 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
1620 Color_Diffuse[3] = 0.0f;
1621 for (x = startx;x < endx;x++)
1623 out4f[x*4+0] = diffuse[x*4+0] * (Color_Ambient[0] + lightmap[x*4+0] * Color_Diffuse[0]);
1624 out4f[x*4+1] = diffuse[x*4+1] * (Color_Ambient[1] + lightmap[x*4+1] * Color_Diffuse[1]);
1625 out4f[x*4+2] = diffuse[x*4+2] * (Color_Ambient[2] + lightmap[x*4+2] * Color_Diffuse[2]);
1626 out4f[x*4+3] = diffuse[x*4+3] * (Color_Ambient[3] + lightmap[x*4+3] * Color_Diffuse[3]);
1630 void DPSOFTRAST_Draw_Span_VertexColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1632 int x, startx = span->startx, endx = span->endx;
1633 float Color_Ambient[4], Color_Diffuse[4];
1638 int arrayindex = DPSOFTRAST_ARRAY_COLOR;
1639 data[0] = span->data[0][arrayindex][0];
1640 data[1] = span->data[0][arrayindex][1];
1641 data[2] = span->data[0][arrayindex][2];
1642 data[3] = span->data[0][arrayindex][3];
1643 slope[0] = span->data[1][arrayindex][0];
1644 slope[1] = span->data[1][arrayindex][1];
1645 slope[2] = span->data[1][arrayindex][2];
1646 slope[3] = span->data[1][arrayindex][3];
1647 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1648 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1649 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1650 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1651 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
1652 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
1653 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
1654 Color_Diffuse[3] = 0.0f;
1655 for (x = startx;x < endx;x++)
1658 c[0] = (data[0] + slope[0]*x) * z;
1659 c[1] = (data[1] + slope[1]*x) * z;
1660 c[2] = (data[2] + slope[2]*x) * z;
1661 c[3] = (data[3] + slope[3]*x) * z;
1662 out4f[x*4+0] = diffuse[x*4+0] * (Color_Ambient[0] + c[0] * Color_Diffuse[0]);
1663 out4f[x*4+1] = diffuse[x*4+1] * (Color_Ambient[1] + c[1] * Color_Diffuse[1]);
1664 out4f[x*4+2] = diffuse[x*4+2] * (Color_Ambient[2] + c[2] * Color_Diffuse[2]);
1665 out4f[x*4+3] = diffuse[x*4+3] * (Color_Ambient[3] + c[3] * Color_Diffuse[3]);
1669 void DPSOFTRAST_Draw_Span_FlatColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse)
1671 int x, startx = span->startx, endx = span->endx;
1672 float Color_Ambient[4];
1673 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1674 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1675 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1676 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1677 for (x = startx;x < endx;x++)
1679 out4f[x*4+0] = diffuse[x*4+0] * Color_Ambient[0];
1680 out4f[x*4+1] = diffuse[x*4+1] * Color_Ambient[1];
1681 out4f[x*4+2] = diffuse[x*4+2] * Color_Ambient[2];
1682 out4f[x*4+3] = diffuse[x*4+3] * Color_Ambient[3];
1686 void DPSOFTRAST_Draw_VertexShader(void)
1688 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1);
1689 switch(dpsoftrast.shader_mode)
1691 case SHADERMODE_GENERIC: ///< (particles/HUD/etc) vertex color: optionally multiplied by one texture
1692 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
1693 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1695 case SHADERMODE_POSTPROCESS: ///< postprocessing shader (r_glsl_postprocess)
1696 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1697 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
1699 case SHADERMODE_DEPTH_OR_SHADOW: ///< (depthfirst/shadows) vertex shader only
1701 case SHADERMODE_FLATCOLOR: ///< (lightmap) modulate texture by uniform color (q1bsp: q3bsp)
1702 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1704 case SHADERMODE_VERTEXCOLOR: ///< (lightmap) modulate texture by vertex colors (q3bsp)
1705 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
1706 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1708 case SHADERMODE_LIGHTMAP: ///< (lightmap) modulate texture by lightmap texture (q1bsp: q3bsp)
1709 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1710 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
1712 case SHADERMODE_FAKELIGHT: ///< (fakelight) modulate texture by "fake" lighting (no lightmaps: no nothing)
1714 case SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE: ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1716 case SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE: ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1718 case SHADERMODE_LIGHTDIRECTION: ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1720 case SHADERMODE_LIGHTSOURCE: ///< (lightsource) use directional pixel shading from light source (rtlight)
1722 case SHADERMODE_REFRACTION: ///< refract background (the material is rendered normally after this pass)
1724 case SHADERMODE_WATER: ///< refract background and reflection (the material is rendered normally after this pass)
1726 case SHADERMODE_SHOWDEPTH: ///< (debugging) renders depth as color
1728 case SHADERMODE_DEFERREDGEOMETRY: ///< (deferred) render material properties to screenspace geometry buffers
1730 case SHADERMODE_DEFERREDLIGHTSOURCE: ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
1732 case SHADERMODE_COUNT:
1737 void DPSOFTRAST_Draw_PixelShaderSpan(const DPSOFTRAST_State_Draw_Span *span)
1739 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
1740 float buffer_texture_color[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1741 float buffer_texture_lightmap[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1742 float buffer_FragColor[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1743 switch(dpsoftrast.shader_mode)
1745 case SHADERMODE_GENERIC: ///< (particles/HUD/etc) vertex color: optionally multiplied by one texture
1746 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1747 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_FIRST, 2, buffer_z);
1748 DPSOFTRAST_Draw_Span_MultiplyVarying(span, buffer_FragColor, buffer_texture_color, 1, buffer_z);
1749 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1751 case SHADERMODE_POSTPROCESS: ///< postprocessing shader (r_glsl_postprocess)
1752 // TODO: optimize!! at the very least there is no reason to use texture sampling on the frame texture
1753 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1754 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_FragColor, GL20TU_FIRST, 2, buffer_z);
1755 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
1757 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_SECOND, 3, buffer_z);
1758 DPSOFTRAST_Draw_Span_AddBloom(span, buffer_FragColor, buffer_FragColor, buffer_texture_color, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
1760 DPSOFTRAST_Draw_Span_MixUniformColor(span, buffer_FragColor, buffer_FragColor, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
1761 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
1763 // TODO: implement saturation
1765 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
1767 // TODO: implement gammaramps
1769 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1771 case SHADERMODE_DEPTH_OR_SHADOW: ///< (depthfirst/shadows) vertex shader only
1773 case SHADERMODE_FLATCOLOR: ///< (lightmap) modulate texture by uniform color (q1bsp: q3bsp)
1774 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1775 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1776 DPSOFTRAST_Draw_Span_FlatColor(span, buffer_FragColor, buffer_texture_color);
1777 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1779 case SHADERMODE_VERTEXCOLOR: ///< (lightmap) modulate texture by vertex colors (q3bsp)
1780 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1781 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1782 DPSOFTRAST_Draw_Span_VertexColor(span, buffer_FragColor, buffer_texture_color, buffer_z);
1783 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1785 case SHADERMODE_LIGHTMAP: ///< (lightmap) modulate texture by lightmap texture (q1bsp: q3bsp)
1786 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1787 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1788 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_lightmap, GL20TU_LIGHTMAP, 6, buffer_z);
1789 DPSOFTRAST_Draw_Span_Lightmap(span, buffer_FragColor, buffer_texture_color, buffer_texture_lightmap);
1790 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1792 case SHADERMODE_FAKELIGHT: ///< (fakelight) modulate texture by "fake" lighting (no lightmaps: no nothing)
1794 case SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE: ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1796 case SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE: ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1798 case SHADERMODE_LIGHTDIRECTION: ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1800 case SHADERMODE_LIGHTSOURCE: ///< (lightsource) use directional pixel shading from light source (rtlight)
1802 case SHADERMODE_REFRACTION: ///< refract background (the material is rendered normally after this pass)
1804 case SHADERMODE_WATER: ///< refract background and reflection (the material is rendered normally after this pass)
1806 case SHADERMODE_SHOWDEPTH: ///< (debugging) renders depth as color
1808 case SHADERMODE_DEFERREDGEOMETRY: ///< (deferred) render material properties to screenspace geometry buffers
1810 case SHADERMODE_DEFERREDLIGHTSOURCE: ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
1812 case SHADERMODE_COUNT:
1817 void DPSOFTRAST_Draw_ProcessSpans(void)
1823 int numspans = dpsoftrast.draw.numspans;
1825 // unsigned int *colorpixel;
1826 unsigned int *depthpixel;
1832 DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
1833 unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
1834 for (i = 0;i < numspans;i++, span++)
1836 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1837 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1838 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
1840 depth = (int)(w*DPSOFTRAST_DEPTHSCALE);
1841 depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
1842 depthpixel = dpsoftrast.fb_depthpixels + span->start;
1843 switch(dpsoftrast.fb_depthfunc)
1846 case GL_ALWAYS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
1847 case GL_LESS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
1848 case GL_LEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
1849 case GL_EQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
1850 case GL_GEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
1851 case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
1852 case GL_NEVER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
1854 //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
1855 //for (x = 0;x < span->length;x++)
1856 // colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
1857 // if there is no color buffer, skip pixel shader
1859 endx = span->length;
1860 while (startx < endx && !pixelmask[startx])
1862 while (endx > startx && !pixelmask[endx-1])
1865 continue; // no pixels to fill
1866 span->pixelmask = pixelmask;
1867 span->startx = startx;
1869 // run pixel shader if appropriate
1870 // do this before running depthmask code, to allow the pixelshader
1871 // to clear pixelmask values for alpha testing
1872 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
1873 DPSOFTRAST_Draw_PixelShaderSpan(span);
1874 if (dpsoftrast.user.depthmask)
1875 for (x = 0, d = depth;x < span->length;x++, d += depthslope)
1881 // no depth testing means we're just dealing with color...
1882 // if there is no color buffer, skip pixel shader
1883 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
1885 memset(pixelmask, 1, span->length);
1886 span->pixelmask = pixelmask;
1888 span->endx = span->length;
1889 DPSOFTRAST_Draw_PixelShaderSpan(span);
1895 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask)
1897 int cullface = dpsoftrast.user.cullface;
1898 int width = dpsoftrast.fb_width;
1899 int height = dpsoftrast.fb_height;
1932 float trianglearea2;
1933 float triangleedge[2][4];
1934 float trianglenormal[4];
1936 float clipped[DPSOFTRAST_ARRAY_TOTAL][4][4];
1938 float proj[DPSOFTRAST_ARRAY_TOTAL][4][4];
1939 DPSOFTRAST_State_Draw_Span *span;
1940 DPSOFTRAST_State_Draw_Span *oldspan;
1941 for (i = 0;i < numtriangles;i++)
1943 // generate the 3 edges of this triangle
1944 // generate spans for the triangle - switch based on left split or right split classification of triangle
1947 e[0] = element3i[i*3+0] - firstvertex;
1948 e[1] = element3i[i*3+1] - firstvertex;
1949 e[2] = element3i[i*3+2] - firstvertex;
1953 e[0] = element3s[i*3+0] - firstvertex;
1954 e[1] = element3s[i*3+1] - firstvertex;
1955 e[2] = element3s[i*3+2] - firstvertex;
1963 triangleedge[0][0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+0] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+0];
1964 triangleedge[0][1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+1] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+1];
1965 triangleedge[0][2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2];
1966 triangleedge[1][0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+0] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+0];
1967 triangleedge[1][1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+1] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+1];
1968 triangleedge[1][2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2];
1969 trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
1970 trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
1971 trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
1972 trianglearea2 = trianglenormal[0] * trianglenormal[0] + trianglenormal[1] * trianglenormal[1] + trianglenormal[2] * trianglenormal[2];
1973 // skip degenerate triangles, nothing good can come from them...
1974 if (trianglearea2 == 0.0f)
1976 // apply current cullface mode (this culls many triangles)
1980 if (trianglenormal[2] < 0)
1984 if (trianglenormal[2] > 0)
1988 // calculate distance from nearplane
1989 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + 1.0f;
1990 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + 1.0f;
1991 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + 1.0f;
1993 if (clipdist[0] < 0.0f)
1995 if (clipdist[1] < 0.0f)
1997 if (clipdist[2] < 0.0f)
1999 // clip triangle if necessary
2003 // triangle is entirely in front of nearplane
2005 // macros for clipping vertices
2006 #define CLIPPEDVERTEXLERP(k,p1,p2) \
2007 frac = clipdist[p1] / (clipdist[p1] - clipdist[p2]);\
2008 ifrac = 1.0f - frac;\
2009 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2013 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+0]*frac;\
2014 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+1]*frac;\
2015 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+2]*frac;\
2016 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+3]*frac;\
2019 DPSOFTRAST_Draw_ProjectVertices(screen[k], clipped[DPSOFTRAST_ARRAY_POSITION][k], 1)
2020 #define CLIPPEDVERTEXCOPY(k,p1) \
2021 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2025 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0];\
2026 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1];\
2027 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2];\
2028 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3];\
2031 screen[k][0] = dpsoftrast.draw.screencoord4f[e[p1]*4+0];\
2032 screen[k][1] = dpsoftrast.draw.screencoord4f[e[p1]*4+1];\
2033 screen[k][2] = dpsoftrast.draw.screencoord4f[e[p1]*4+2];\
2034 screen[k][3] = dpsoftrast.draw.screencoord4f[e[p1]*4+3];
2036 CLIPPEDVERTEXCOPY(0,0);
2037 CLIPPEDVERTEXCOPY(1,1);
2038 CLIPPEDVERTEXCOPY(2,2);
2042 CLIPPEDVERTEXLERP(0,0,1);
2043 CLIPPEDVERTEXCOPY(1,1);
2044 CLIPPEDVERTEXCOPY(2,2);
2045 CLIPPEDVERTEXLERP(3,2,0);
2049 CLIPPEDVERTEXCOPY(0,0);
2050 CLIPPEDVERTEXLERP(1,0,1);
2051 CLIPPEDVERTEXLERP(2,1,2);
2052 CLIPPEDVERTEXCOPY(3,2);
2056 CLIPPEDVERTEXLERP(0,1,2);
2057 CLIPPEDVERTEXCOPY(1,2);
2058 CLIPPEDVERTEXLERP(2,2,0);
2062 CLIPPEDVERTEXCOPY(0,0);
2063 CLIPPEDVERTEXCOPY(1,1);
2064 CLIPPEDVERTEXLERP(2,1,2);
2065 CLIPPEDVERTEXLERP(3,2,0);
2069 CLIPPEDVERTEXLERP(0,0,1);
2070 CLIPPEDVERTEXCOPY(1,1);
2071 CLIPPEDVERTEXLERP(2,1,2);
2075 CLIPPEDVERTEXCOPY(0,0);
2076 CLIPPEDVERTEXLERP(1,0,1);
2077 CLIPPEDVERTEXLERP(2,2,0);
2081 // triangle is entirely behind nearplane
2084 // calculate integer y coords for triangle points
2085 screenx[0] = (int)(screen[0][0]);
2086 screeny[0] = (int)(screen[0][1]);
2087 screenx[1] = (int)(screen[1][0]);
2088 screeny[1] = (int)(screen[1][1]);
2089 screenx[2] = (int)(screen[2][0]);
2090 screeny[2] = (int)(screen[2][1]);
2091 screenx[3] = (int)(screen[3][0]);
2092 screeny[3] = (int)(screen[3][1]);
2093 // figure out the extents (bounding box) of the triangle
2094 extent[0] = screenx[0];
2095 extent[1] = screeny[0];
2096 extent[2] = screenx[0];
2097 extent[3] = screeny[0];
2098 for (j = 1;j < numpoints;j++)
2100 if (extent[0] > screenx[j]) extent[0] = screenx[j];
2101 if (extent[1] > screeny[j]) extent[1] = screeny[j];
2102 if (extent[2] < screenx[j]) extent[2] = screenx[j];
2103 if (extent[3] < screeny[j]) extent[3] = screeny[j];
2113 if (extent[2] > width)
2115 if (extent[3] > height)
2117 // skip offscreen triangles
2118 if (extent[2] <= extent[0] || extent[3] <= extent[1])
2120 // TODO: adjust texture LOD by texture density
2122 // okay, this triangle is going to produce spans, we'd better project
2123 // the interpolants now (this is what gives perspective texturing),
2124 // this consists of simply multiplying all arrays by the W coord
2125 // (which is basically 1/Z), which will be undone per-pixel
2126 // (multiplying by Z again) to get the perspective-correct array
2128 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2132 for (k = 0;k < numpoints;k++)
2135 proj[j][k][0] = clipped[j][k][0] * w;
2136 proj[j][k][1] = clipped[j][k][1] * w;
2137 proj[j][k][2] = clipped[j][k][2] * w;
2138 proj[j][k][3] = clipped[j][k][3] * w;
2142 // iterate potential spans
2143 // TODO: optimize? if we figured out the edge order beforehand, this
2144 // could do loops over the edges in the proper order rather than
2145 // selecting them for each span
2146 // TODO: optimize? the edges could have data slopes calculated
2147 // TODO: optimize? the data slopes could be calculated as a plane
2148 // (2D slopes) to avoid any interpolation along edges at all
2149 for (y = extent[1];y < extent[3];y++)
2151 // get center of pixel y
2153 // do the compares all at once
2154 screenyless[0] = y <= screeny[0];
2155 screenyless[1] = y <= screeny[1];
2156 screenyless[2] = y <= screeny[2];
2157 screenyless[3] = y <= screeny[3];
2160 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4 + screenyless[3] * 8)
2162 case 0: /*0000*/ continue;
2163 case 1: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 0;edge1n = 1;break;
2164 case 2: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
2165 case 3: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 2;break;
2166 case 4: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break;
2167 case 5: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break; // concave - nonsense
2168 case 6: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 3;break;
2169 case 7: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 3;break;
2170 case 8: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 3;edge1n = 0;break;
2171 case 9: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 1;break;
2172 case 10: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
2173 case 11: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break;
2174 case 12: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 0;break;
2175 case 13: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
2176 case 14: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 0;break;
2177 case 15: /*1111*/ continue;
2182 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4)
2184 case 0: /*000*/ continue;
2185 case 1: /*100*/ edge0p = 2;edge0n = 0;edge1p = 0;edge1n = 1;break;
2186 case 2: /*010*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
2187 case 3: /*110*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 2;break;
2188 case 4: /*001*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 0;break;
2189 case 5: /*101*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
2190 case 6: /*011*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 0;break;
2191 case 7: /*111*/ continue;
2201 for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
2203 if (screenyless[k] && !screenyless[j])
2209 else if (screenyless[j] && !screenyless[k])
2216 if (foundedges != 3)
2218 if (cedge0p != edge0p || cedge0n != edge0n || cedge1p != edge1p || cedge1n != edge1n)
2221 printf("case %i%i%i%i is broken %i %i %i %i != %i %i %i %i\n", screenyless[0], screenyless[1], screenyless[2], screenyless[3], cedge0p, cedge0n, cedge1p, cedge1n, edge0p, edge0n, edge1p, edge1n);
2223 printf("case %i%i%i is broken %i %i %i %i != %i %i %i %i\n", screenyless[0], screenyless[1], screenyless[2], cedge0p, cedge0n, cedge1p, cedge1n, edge0p, edge0n, edge1p, edge1n);
2227 edge0ylerp = (yc - screen[edge0p][1]) / (screen[edge0n][1] - screen[edge0p][1]);
2228 edge1ylerp = (yc - screen[edge1p][1]) / (screen[edge1n][1] - screen[edge1p][1]);
2229 if (edge0ylerp < 0 || edge0ylerp > 1 || edge1ylerp < 0 || edge1ylerp > 1)
2231 edge0yilerp = 1.0f - edge0ylerp;
2232 edge1yilerp = 1.0f - edge1ylerp;
2233 edge0xf = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2234 edge1xf = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2235 if (edge0xf < edge1xf)
2245 startx = (int)ceil(startxf);
2246 endx = (int)ceil(endxf);
2253 if (startxf > startx || endxf < endx-1) { printf("%s:%i X wrong (%i to %i is outside %f to %f)\n", __FILE__, __LINE__, startx, endx, startxf, endxf); }
2254 spanilength = 1.0f / (endxf - startxf);
2255 startxlerp = startx - startxf;
2256 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
2258 span->start = y * width + startx;
2259 span->length = endx - startx;
2260 j = DPSOFTRAST_ARRAY_TOTAL;
2261 if (edge0xf < edge1xf)
2263 span->data[0][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2264 span->data[0][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
2265 span->data[0][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
2266 span->data[0][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
2267 span->data[1][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2268 span->data[1][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
2269 span->data[1][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
2270 span->data[1][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
2271 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2275 span->data[0][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
2276 span->data[0][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
2277 span->data[0][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
2278 span->data[0][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
2279 span->data[1][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
2280 span->data[1][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
2281 span->data[1][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
2282 span->data[1][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
2288 span->data[0][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2289 span->data[0][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
2290 span->data[0][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
2291 span->data[0][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
2292 span->data[1][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2293 span->data[1][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
2294 span->data[1][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
2295 span->data[1][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
2296 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2300 span->data[0][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
2301 span->data[0][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
2302 span->data[0][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
2303 span->data[0][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
2304 span->data[1][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
2305 span->data[1][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
2306 span->data[1][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
2307 span->data[1][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
2311 // change data[1][n][] to be a data slope
2312 j = DPSOFTRAST_ARRAY_TOTAL;
2313 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
2314 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
2315 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
2316 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
2317 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2321 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
2322 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
2323 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
2324 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
2327 // adjust the data[0][n][] to be correct for the pixel centers
2328 // this also handles horizontal clipping where a major part of the
2329 // span may be off the left side of the screen
2330 j = DPSOFTRAST_ARRAY_TOTAL;
2331 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
2332 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
2333 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
2334 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
2335 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2339 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
2340 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
2341 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
2342 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
2345 // to keep the shader routines from needing more than a small
2346 // buffer for pixel intermediate data, we split long spans...
2347 while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
2349 span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
2350 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
2352 DPSOFTRAST_Draw_ProcessSpans();
2353 dpsoftrast.draw.numspans = 0;
2356 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
2358 startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
2359 span->start = y * width + startx;
2360 span->length = endx - startx;
2361 j = DPSOFTRAST_ARRAY_TOTAL;
2362 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2363 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2364 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2365 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2366 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2370 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2371 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2372 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2373 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2377 // after all that, we have a span suitable for the pixel shader...
2378 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
2380 DPSOFTRAST_Draw_ProcessSpans();
2381 dpsoftrast.draw.numspans = 0;
2384 // draw outlines over triangle for debugging
2385 // for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
2386 // DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
2388 if (dpsoftrast.draw.numspans)
2390 DPSOFTRAST_Draw_ProcessSpans();
2391 dpsoftrast.draw.numspans = 0;
2395 void DPSOFTRAST_Draw_DebugPoints(void)
2400 int numvertices = dpsoftrast.draw.numvertices;
2401 int w = dpsoftrast.fb_width;
2403 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
2405 bounds[0] = dpsoftrast.fb_viewportscissor[0];
2406 bounds[1] = dpsoftrast.fb_viewportscissor[1];
2407 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
2408 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
2409 for (i = 0;i < numvertices;i++)
2412 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
2414 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
2415 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
2416 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
2417 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
2418 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
2419 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
2420 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
2422 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
2423 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
2427 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
2429 unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL];
2430 arraymask[0] = true;
2431 arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode)
2432 arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL;
2433 arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL;
2434 arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL;
2435 arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL;
2436 arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL;
2437 arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL;
2438 arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL;
2439 arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL;
2440 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
2441 DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true);
2442 DPSOFTRAST_Draw_VertexShader();
2443 DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
2444 DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numvertices, numtriangles, element3i, element3s, arraymask);
2447 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
2456 memset(&dpsoftrast, 0, sizeof(dpsoftrast));
2457 dpsoftrast.bigendian = u.b[3];
2458 dpsoftrast.fb_width = width;
2459 dpsoftrast.fb_height = height;
2460 dpsoftrast.fb_depthpixels = depthpixels;
2461 dpsoftrast.fb_colorpixels[0] = colorpixels;
2462 dpsoftrast.fb_colorpixels[1] = NULL;
2463 dpsoftrast.fb_colorpixels[1] = NULL;
2464 dpsoftrast.fb_colorpixels[1] = NULL;
2465 dpsoftrast.texture_firstfree = 1;
2466 dpsoftrast.texture_end = 1;
2467 dpsoftrast.texture_max = 0;
2468 dpsoftrast.user.colormask[0] = 1;
2469 dpsoftrast.user.colormask[1] = 1;
2470 dpsoftrast.user.colormask[2] = 1;
2471 dpsoftrast.user.colormask[3] = 1;
2472 dpsoftrast.user.blendfunc[0] = GL_ONE;
2473 dpsoftrast.user.blendfunc[1] = GL_ZERO;
2474 dpsoftrast.user.depthmask = true;
2475 dpsoftrast.user.depthtest = true;
2476 dpsoftrast.user.depthfunc = GL_LEQUAL;
2477 dpsoftrast.user.scissortest = false;
2478 dpsoftrast.user.cullface = GL_BACK;
2479 dpsoftrast.user.alphatest = false;
2480 dpsoftrast.user.alphafunc = GL_GREATER;
2481 dpsoftrast.user.alphavalue = 0.5f;
2482 dpsoftrast.user.scissor[0] = 0;
2483 dpsoftrast.user.scissor[1] = 0;
2484 dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
2485 dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
2486 dpsoftrast.user.viewport[0] = 0;
2487 dpsoftrast.user.viewport[1] = 0;
2488 dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
2489 dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
2490 dpsoftrast.user.depthrange[0] = 0;
2491 dpsoftrast.user.depthrange[1] = 1;
2492 dpsoftrast.user.polygonoffset[0] = 0;
2493 dpsoftrast.user.polygonoffset[1] = 0;
2494 dpsoftrast.user.color[0] = 1;
2495 dpsoftrast.user.color[1] = 1;
2496 dpsoftrast.user.color[2] = 1;
2497 dpsoftrast.user.color[3] = 1;
2498 dpsoftrast.validate = -1;
2499 DPSOFTRAST_Validate(-1);
2500 dpsoftrast.validate = 0;
2503 void DPSOFTRAST_Shutdown(void)
2506 for (i = 0;i < dpsoftrast.texture_end;i++)
2507 if (dpsoftrast.texture[i].bytes)
2508 free(dpsoftrast.texture[i].bytes);
2509 if (dpsoftrast.texture)
2510 free(dpsoftrast.texture);
2511 memset(&dpsoftrast, 0, sizeof(dpsoftrast));