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];
87 typedef struct DPSOFTRAST_State_User_s
103 float polygonoffset[2];
106 DPSOFTRAST_State_User;
108 typedef struct DPSOFTRAST_State_Draw_Span_s
110 int start; // pixel index
111 int length; // pixel count
112 int startx; // usable range (according to pixelmask)
113 int endx; // usable range (according to pixelmask)
114 unsigned char *pixelmask; // true for pixels that passed depth test, false for others
115 // [0][n][] is start interpolant values (projected)
116 // [1][n][] is end interpolant values (projected)
117 // [0][DPSOFTRAST_ARRAY_TOTAL][] is start screencoord4f
118 // [1][DPSOFTRAST_ARRAY_TOTAL][] is end screencoord4f
119 // NOTE: screencoord4f[3] is W (basically 1/Z), useful for depthbuffer
120 float data[2][DPSOFTRAST_ARRAY_TOTAL+1][4];
122 DPSOFTRAST_State_Draw_Span;
124 #define DPSOFTRAST_DRAW_MAXSPANQUEUE 1024
126 typedef struct DPSOFTRAST_State_Draw_s
130 float *in_array4f[DPSOFTRAST_ARRAY_TOTAL];
131 float *post_array4f[DPSOFTRAST_ARRAY_TOTAL];
132 float *screencoord4f;
134 // spans are queued in this structure for dispatch to the pixel shader,
135 // partly to improve cache locality, partly for batching purposes, spans
136 // are flushed before DrawTriangles returns to caller
138 DPSOFTRAST_State_Draw_Span spanqueue[DPSOFTRAST_DRAW_MAXSPANQUEUE];
140 DPSOFTRAST_State_Draw;
142 #define DPSOFTRAST_VALIDATE_FB 1
143 #define DPSOFTRAST_VALIDATE_DEPTHFUNC 2
144 #define DPSOFTRAST_VALIDATE_BLENDFUNC 4
145 #define DPSOFTRAST_VALIDATE_DRAW (DPSOFTRAST_VALIDATE_FB | DPSOFTRAST_VALIDATE_DEPTHFUNC | DPSOFTRAST_VALIDATE_BLENDFUNC)
147 typedef enum DPSOFTRAST_BLENDMODE_e
149 DPSOFTRAST_BLENDMODE_OPAQUE,
150 DPSOFTRAST_BLENDMODE_ALPHA,
151 DPSOFTRAST_BLENDMODE_ADDALPHA,
152 DPSOFTRAST_BLENDMODE_ADD,
153 DPSOFTRAST_BLENDMODE_INVMOD,
154 DPSOFTRAST_BLENDMODE_MUL,
155 DPSOFTRAST_BLENDMODE_MUL2,
156 DPSOFTRAST_BLENDMODE_SUBALPHA,
157 DPSOFTRAST_BLENDMODE_TOTAL
159 DPSOFTRAST_BLENDMODE;
161 typedef struct DPSOFTRAST_State_s
163 // DPSOFTRAST_VALIDATE_ flags
169 unsigned int *fb_depthpixels;
170 unsigned int *fb_colorpixels[4];
172 const float *pointer_vertex3f;
173 const float *pointer_color4f;
174 const unsigned char *pointer_color4ub;
175 const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
178 int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
179 int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
180 DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
183 int shader_permutation;
184 float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4];
185 int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
187 // derived values (DPSOFTRAST_VALIDATE_FB)
188 int fb_clearscissor[4];
190 int fb_viewportscissor[4];
191 float fb_viewportcenter[2];
192 float fb_viewportscale[2];
194 // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
197 // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
202 int texture_firstfree;
203 DPSOFTRAST_Texture *texture;
208 const char *errorstring;
210 DPSOFTRAST_State_User user;
212 DPSOFTRAST_State_Draw draw;
216 DPSOFTRAST_State dpsoftrast;
218 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
219 #define DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a) (((int)(r * 255.0f + 0.5f) << 16) | ((int)(g * 255.0f + 0.5f) << 8) | (int)(b * 255.0f + 0.5f) | ((int)(a * 255.0f + 0.5f) << 24))
220 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
221 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
223 void DPSOFTRAST_RecalcFB(void)
225 // calculate framebuffer scissor, viewport, viewport clipped by scissor,
226 // and viewport projection values
227 int x1, x2, x3, x4, x5, x6;
228 int y1, y2, y3, y4, y5, y6;
229 x1 = dpsoftrast.user.scissor[0];
230 x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
231 x3 = dpsoftrast.user.viewport[0];
232 x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
233 y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
234 y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
235 y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
236 y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
237 if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
239 if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
241 if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
243 if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
245 if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
246 x5 = x1;if (x5 < x3) x5 = x3;
247 x6 = x2;if (x6 > x4) x4 = x4;
248 y5 = y1;if (y5 < y3) y5 = y3;
249 y6 = y2;if (y6 > y4) y6 = y4;
250 dpsoftrast.fb_clearscissor[0] = x1;
251 dpsoftrast.fb_clearscissor[1] = y1;
252 dpsoftrast.fb_clearscissor[2] = x2 - x1;
253 dpsoftrast.fb_clearscissor[3] = y2 - y1;
254 dpsoftrast.fb_viewport[0] = x3;
255 dpsoftrast.fb_viewport[1] = y3;
256 dpsoftrast.fb_viewport[2] = x4 - x3;
257 dpsoftrast.fb_viewport[3] = y4 - y3;
258 dpsoftrast.fb_viewportscissor[0] = x5;
259 dpsoftrast.fb_viewportscissor[1] = y5;
260 dpsoftrast.fb_viewportscissor[2] = x6 - x5;
261 dpsoftrast.fb_viewportscissor[3] = y6 - y5;
262 dpsoftrast.fb_viewportcenter[0] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
263 dpsoftrast.fb_viewportcenter[1] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
264 dpsoftrast.fb_viewportscale[0] = 0.5f * dpsoftrast.user.viewport[2];
265 dpsoftrast.fb_viewportscale[1] = -0.5f * dpsoftrast.user.viewport[3];
268 void DPSOFTRAST_RecalcDepthFunc(void)
270 dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
273 int blendmodetable[][4] =
275 {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
276 {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
277 {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
278 {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
279 {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
280 {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
281 {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
282 {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
283 {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
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);
454 texture->clampmin[0] = 0;
455 texture->clampmin[1] = 0;
456 texture->clampmin[2] = 0;
457 texture->clampmax[0] = texture->width-1;
458 texture->clampmax[1] = texture->height-1;
459 texture->clampmax[2] = texture->depth-1;
460 texture->wrapmask[0] = texture->width-1;
461 texture->wrapmask[1] = texture->height-1;
462 texture->wrapmask[2] = texture->depth-1;
466 void DPSOFTRAST_Texture_Free(int index)
468 DPSOFTRAST_Texture *texture;
469 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
471 free(texture->bytes);
472 texture->bytes = NULL;
473 memset(texture, 0, sizeof(*texture));
474 // adjust the free range and used range
475 if (dpsoftrast.texture_firstfree > index)
476 dpsoftrast.texture_firstfree = index;
477 while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
478 dpsoftrast.texture_end--;
480 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
482 int i, x, y, z, w, layer0, layer1, row0, row1;
483 unsigned char *o, *i0, *i1, *i2, *i3;
484 DPSOFTRAST_Texture *texture;
485 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
486 if (texture->mipmaps <= 1)
488 for (i = 1;i < texture->mipmaps;i++)
490 for (z = 0;z < texture->mipmap[i][4];z++)
494 if (layer1 >= texture->mipmap[i-1][4])
495 layer1 = texture->mipmap[i-1][4]-1;
496 for (y = 0;y < texture->mipmap[i][3];y++)
500 if (row1 >= texture->mipmap[i-1][3])
501 row1 = texture->mipmap[i-1][3]-1;
502 o = texture->bytes + texture->mipmap[i ][0] + 4*(texture->mipmap[i ][3] * texture->mipmap[i ][2] * z + texture->mipmap[i ][2] * y );
503 i0 = texture->bytes + texture->mipmap[i-1][0] + 4*(texture->mipmap[i-1][3] * texture->mipmap[i-1][2] * layer0 + texture->mipmap[i-1][2] * row0);
504 i1 = texture->bytes + texture->mipmap[i-1][0] + 4*(texture->mipmap[i-1][3] * texture->mipmap[i-1][2] * layer0 + texture->mipmap[i-1][2] * row1);
505 i2 = texture->bytes + texture->mipmap[i-1][0] + 4*(texture->mipmap[i-1][3] * texture->mipmap[i-1][2] * layer1 + texture->mipmap[i-1][2] * row0);
506 i3 = texture->bytes + texture->mipmap[i-1][0] + 4*(texture->mipmap[i-1][3] * texture->mipmap[i-1][2] * layer1 + texture->mipmap[i-1][2] * row1);
507 w = texture->mipmap[i][2];
510 if (texture->mipmap[i-1][2] > 1)
512 // average 3D texture
513 for (x = 0;x < w;x++)
515 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
516 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
517 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
518 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
523 // average 3D mipmap with parent width == 1
524 for (x = 0;x < w;x++)
526 o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
527 o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
528 o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
529 o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
535 if (texture->mipmap[i-1][2] > 1)
537 // average 2D texture (common case)
538 for (x = 0;x < w;x++)
540 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
541 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
542 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
543 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
548 // 2D texture with parent width == 1
549 for (x = 0;x < w;x++)
551 o[0] = (i0[0] + i1[0] + 1) >> 1;
552 o[1] = (i0[1] + i1[1] + 1) >> 1;
553 o[2] = (i0[2] + i1[2] + 1) >> 1;
554 o[3] = (i0[3] + i1[3] + 1) >> 1;
562 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
564 DPSOFTRAST_Texture *texture;
565 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
569 dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
571 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
573 DPSOFTRAST_Texture *texture;
574 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
576 memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
577 DPSOFTRAST_Texture_CalculateMipmaps(index);
579 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
581 DPSOFTRAST_Texture *texture;
582 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
583 return texture->width;
585 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
587 DPSOFTRAST_Texture *texture;
588 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
589 return texture->height;
591 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
593 DPSOFTRAST_Texture *texture;
594 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
595 return texture->depth;
597 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
599 DPSOFTRAST_Texture *texture;
600 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
601 return texture->bytes;
603 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
605 DPSOFTRAST_Texture *texture;
606 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
607 if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
609 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
612 texture->filter = filter;
615 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
617 dpsoftrast.fb_width = width;
618 dpsoftrast.fb_height = height;
619 dpsoftrast.fb_depthpixels = depthpixels;
620 dpsoftrast.fb_colorpixels[0] = colorpixels0;
621 dpsoftrast.fb_colorpixels[1] = colorpixels1;
622 dpsoftrast.fb_colorpixels[2] = colorpixels2;
623 dpsoftrast.fb_colorpixels[3] = colorpixels3;
625 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
627 dpsoftrast.user.viewport[0] = x;
628 dpsoftrast.user.viewport[1] = y;
629 dpsoftrast.user.viewport[2] = width;
630 dpsoftrast.user.viewport[3] = height;
631 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
633 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
635 int i, x1, y1, x2, y2, w, h, x, y;
638 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
639 x1 = dpsoftrast.fb_clearscissor[0];
640 y1 = dpsoftrast.fb_clearscissor[1];
641 x2 = dpsoftrast.fb_clearscissor[2];
642 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
647 // FIXME: honor dpsoftrast.fb_colormask?
648 c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
649 for (i = 0;i < 4;i++)
651 if (!dpsoftrast.fb_colorpixels[i])
653 for (y = y1;y < y2;y++)
655 p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
656 for (x = x1;x < x2;x++)
661 void DPSOFTRAST_ClearDepth(float d)
663 int x1, y1, x2, y2, w, h, x, y;
666 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
667 x1 = dpsoftrast.fb_clearscissor[0];
668 y1 = dpsoftrast.fb_clearscissor[1];
669 x2 = dpsoftrast.fb_clearscissor[2];
670 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
675 c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
676 for (y = y1;y < y2;y++)
678 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
679 for (x = x1;x < x2;x++)
683 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
685 dpsoftrast.user.colormask[0] = r != 0;
686 dpsoftrast.user.colormask[1] = g != 0;
687 dpsoftrast.user.colormask[2] = b != 0;
688 dpsoftrast.user.colormask[3] = a != 0;
689 dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
691 void DPSOFTRAST_DepthTest(int enable)
693 dpsoftrast.user.depthtest = enable;
694 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
696 void DPSOFTRAST_ScissorTest(int enable)
698 dpsoftrast.user.scissortest = enable;
699 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
701 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
703 dpsoftrast.user.scissor[0] = x;
704 dpsoftrast.user.scissor[1] = y;
705 dpsoftrast.user.scissor[2] = width;
706 dpsoftrast.user.scissor[3] = height;
707 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
710 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
713 dpsoftrast.user.blendfunc[0] = smodulate;
714 dpsoftrast.user.blendfunc[1] = dmodulate;
715 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
717 void DPSOFTRAST_BlendSubtract(int enable)
719 dpsoftrast.user.blendsubtract = enable != 0;
720 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
722 void DPSOFTRAST_DepthMask(int enable)
724 dpsoftrast.user.depthmask = enable;
726 void DPSOFTRAST_DepthFunc(int comparemode)
729 dpsoftrast.user.depthfunc = comparemode;
731 void DPSOFTRAST_DepthRange(float range0, float range1)
733 dpsoftrast.user.depthrange[0] = range0;
734 dpsoftrast.user.depthrange[1] = range1;
736 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
738 dpsoftrast.user.polygonoffset[0] = alongnormal;
739 dpsoftrast.user.polygonoffset[1] = intoview;
741 void DPSOFTRAST_CullFace(int mode)
744 dpsoftrast.user.cullface = mode;
746 void DPSOFTRAST_AlphaTest(float enable)
748 dpsoftrast.user.alphatest = enable;
750 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
753 dpsoftrast.user.alphafunc = alphafunc;
754 dpsoftrast.user.alphavalue = alphavalue;
756 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
758 dpsoftrast.user.color[0] = r;
759 dpsoftrast.user.color[1] = g;
760 dpsoftrast.user.color[2] = b;
761 dpsoftrast.user.color[3] = a;
763 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
765 int outstride = blockwidth * 4;
766 int instride = dpsoftrast.fb_width * 4;
769 int bx2 = blockx + blockwidth;
770 int by2 = blocky + blockheight;
775 unsigned char *inpixels;
778 if (bx1 < 0) bx1 = 0;
779 if (by1 < 0) by1 = 0;
780 if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
781 if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
784 inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
785 if (dpsoftrast.bigendian)
787 for (y = by1;y < by2;y++)
789 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
790 o = (unsigned char *)outpixels + (y - by1) * outstride;
791 for (x = bx1;x < bx2;x++)
804 for (y = by1;y < by2;y++)
806 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
807 o = (unsigned char *)outpixels + (y - by1) * outstride;
813 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
817 int tx2 = tx + width;
818 int ty2 = ty + height;
821 int sx2 = sx + width;
822 int sy2 = sy + height;
832 unsigned int *spixels;
833 unsigned int *tpixels;
834 DPSOFTRAST_Texture *texture;
835 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
836 if (mip < 0 || mip >= texture->mipmaps) return;
837 spixels = dpsoftrast.fb_colorpixels[0];
838 swidth = dpsoftrast.fb_width;
839 sheight = dpsoftrast.fb_height;
840 tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
841 twidth = texture->mipmap[mip][2];
842 theight = texture->mipmap[mip][3];
843 if (tx1 < 0) tx1 = 0;
844 if (ty1 < 0) ty1 = 0;
845 if (tx2 > twidth) tx2 = twidth;
846 if (ty2 > theight) ty2 = theight;
847 if (sx1 < 0) sx1 = 0;
848 if (sy1 < 0) sy1 = 0;
849 if (sx2 > swidth) sx2 = swidth;
850 if (sy2 > sheight) sy2 = sheight;
855 if (tw > sw) tw = sw;
856 if (th > sh) th = sh;
857 if (tw < 1 || th < 1)
859 for (y = 0;y < th;y++)
860 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
861 if (texture->mipmaps > 1)
862 DPSOFTRAST_Texture_CalculateMipmaps(index);
864 void DPSOFTRAST_SetTexture(int unitnum, int index)
866 DPSOFTRAST_Texture *texture;
867 if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
869 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
872 texture = DPSOFTRAST_Texture_GetByIndex(index);
873 if (index && !texture)
875 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
878 dpsoftrast.texbound[unitnum] = texture;
881 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
883 dpsoftrast.pointer_vertex3f = vertex3f;
884 dpsoftrast.stride_vertex = stride;
886 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
888 dpsoftrast.pointer_color4f = color4f;
889 dpsoftrast.pointer_color4ub = NULL;
890 dpsoftrast.stride_color = stride;
892 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
894 dpsoftrast.pointer_color4f = NULL;
895 dpsoftrast.pointer_color4ub = color4ub;
896 dpsoftrast.stride_color = stride;
898 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
900 dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
901 dpsoftrast.components_texcoord[unitnum] = numcomponents;
902 dpsoftrast.stride_texcoord[unitnum] = stride;
905 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
907 dpsoftrast.shader_mode = mode;
908 dpsoftrast.shader_permutation = permutation;
910 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
912 dpsoftrast.uniform4f[index*4+0] = v0;
913 dpsoftrast.uniform4f[index*4+1] = v1;
914 dpsoftrast.uniform4f[index*4+2] = v2;
915 dpsoftrast.uniform4f[index*4+3] = v3;
917 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v)
919 dpsoftrast.uniform4f[index*4+0] = v[0];
920 dpsoftrast.uniform4f[index*4+1] = v[1];
921 dpsoftrast.uniform4f[index*4+2] = v[2];
922 dpsoftrast.uniform4f[index*4+3] = v[3];
924 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v)
926 dpsoftrast.uniform4f[index*4+0] = v[0];
927 dpsoftrast.uniform4f[index*4+1] = v[1];
928 dpsoftrast.uniform4f[index*4+2] = v[2];
929 dpsoftrast.uniform4f[index*4+3] = v[3];
930 dpsoftrast.uniform4f[index*4+4] = v[4];
931 dpsoftrast.uniform4f[index*4+5] = v[5];
932 dpsoftrast.uniform4f[index*4+6] = v[6];
933 dpsoftrast.uniform4f[index*4+7] = v[7];
934 dpsoftrast.uniform4f[index*4+8] = v[8];
935 dpsoftrast.uniform4f[index*4+9] = v[9];
936 dpsoftrast.uniform4f[index*4+10] = v[10];
937 dpsoftrast.uniform4f[index*4+11] = v[11];
938 dpsoftrast.uniform4f[index*4+12] = v[12];
939 dpsoftrast.uniform4f[index*4+13] = v[13];
940 dpsoftrast.uniform4f[index*4+14] = v[14];
941 dpsoftrast.uniform4f[index*4+15] = v[15];
943 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
945 dpsoftrast.uniform1i[index] = i0;
948 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors, int numtexcoords)
956 const unsigned char *b;
957 dpsoftrast.draw.numvertices = numvertices;
958 if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
960 if (dpsoftrast.draw.maxvertices < 4096)
961 dpsoftrast.draw.maxvertices = 4096;
962 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
963 dpsoftrast.draw.maxvertices *= 2;
964 if (dpsoftrast.draw.in_array4f[0])
965 free(dpsoftrast.draw.in_array4f[0]);
966 data = calloc(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
967 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
968 dpsoftrast.draw.in_array4f[i] = data;
969 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
970 dpsoftrast.draw.post_array4f[i] = data;
971 dpsoftrast.draw.screencoord4f = data;
972 data += dpsoftrast.draw.maxvertices * 4;
974 stride = dpsoftrast.stride_vertex;
975 v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
976 p = dpsoftrast.draw.in_array4f[0];
977 for (i = 0;i < numvertices;i++)
984 v = (const float *)((const unsigned char *)v + stride);
988 if (dpsoftrast.pointer_color4f)
990 stride = dpsoftrast.stride_color;
991 v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
992 p = dpsoftrast.draw.in_array4f[1];
993 for (i = 0;i < numvertices;i++)
1000 v = (const float *)((const unsigned char *)v + stride);
1003 else if (dpsoftrast.pointer_color4ub)
1005 stride = dpsoftrast.stride_color;
1006 b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
1007 p = dpsoftrast.draw.in_array4f[1];
1008 for (i = 0;i < numvertices;i++)
1010 p[0] = b[0] * (1.0f / 255.0f);
1011 p[1] = b[1] * (1.0f / 255.0f);
1012 p[2] = b[2] * (1.0f / 255.0f);
1013 p[3] = b[3] * (1.0f / 255.0f);
1015 b = (const unsigned char *)((const unsigned char *)b + stride);
1020 v = dpsoftrast.user.color;
1021 p = dpsoftrast.draw.in_array4f[1];
1022 for (i = 0;i < numvertices;i++)
1032 for (j = 0;j < numtexcoords;j++)
1034 if (dpsoftrast.pointer_texcoordf[j])
1036 stride = dpsoftrast.stride_texcoord[j];
1037 v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1038 p = dpsoftrast.draw.in_array4f[j+2];
1039 switch(dpsoftrast.components_texcoord[j])
1042 for (i = 0;i < numvertices;i++)
1049 v = (const float *)((const unsigned char *)v + stride);
1053 for (i = 0;i < numvertices;i++)
1060 v = (const float *)((const unsigned char *)v + stride);
1064 for (i = 0;i < numvertices;i++)
1071 v = (const float *)((const unsigned char *)v + stride);
1079 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1084 memcpy(matrix, inmatrix16f, sizeof(float[16]));
1085 for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1087 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1088 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1089 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1090 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1094 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1096 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1099 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1101 // NOTE: this is used both as a whole mesh transform function and a
1102 // per-triangle transform function (for clipped triangles), accordingly
1103 // it should not crash on divide by 0 but the result of divide by 0 is
1108 float viewportcenter[4];
1109 float viewportscale[4];
1110 viewportscale[0] = dpsoftrast.fb_viewportscale[0];
1111 viewportscale[1] = dpsoftrast.fb_viewportscale[1];
1112 viewportscale[2] = 0.5f;
1113 viewportscale[3] = 0.0f;
1114 viewportcenter[0] = dpsoftrast.fb_viewportcenter[0];
1115 viewportcenter[1] = dpsoftrast.fb_viewportcenter[1];
1116 viewportcenter[2] = 0.5f;
1117 viewportcenter[3] = 0.0f;
1118 for (i = 0;i < numitems;i++)
1129 out4f[0] = viewportcenter[0] + viewportscale[0] * in4f[0] * w;
1130 out4f[1] = viewportcenter[1] + viewportscale[1] * in4f[1] * w;
1131 out4f[2] = viewportcenter[2] + viewportscale[2] * in4f[2] * w;
1132 out4f[3] = viewportcenter[3] + viewportscale[3] * in4f[3] * w;
1139 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1144 int w = dpsoftrast.fb_width;
1147 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1149 bounds[0] = dpsoftrast.fb_viewportscissor[0];
1150 bounds[1] = dpsoftrast.fb_viewportscissor[1];
1151 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1152 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1157 for (i = 0;i <= 128;i++)
1160 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1162 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1163 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1164 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1166 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1167 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1168 pixels[y*w+x] = 0xFFFFFFFF;
1172 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span *span, float *zf)
1175 int startx = span->startx;
1176 int endx = span->endx;
1177 float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1178 float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1179 // TODO: optimize by approximating every 8 pixels?
1180 for (x = startx;x < endx;x++)
1181 zf[x] = 1.0f / (w + wslope * x);
1184 void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span *span, const float *in4f)
1187 int startx = span->startx;
1188 int endx = span->endx;
1191 unsigned char *pixelmask = span->pixelmask;
1192 unsigned char *pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1195 pixel += span->start * 4;
1196 // handle alphatest now (this affects depth writes too)
1197 if (dpsoftrast.user.alphatest)
1198 for (x = startx;x < endx;x++)
1199 if (in4f[x*4+3] < 0.5f)
1200 pixelmask[x] = false;
1201 // FIXME: this does not handle bigendian
1202 switch(dpsoftrast.fb_blendmode)
1204 case DPSOFTRAST_BLENDMODE_OPAQUE:
1205 for (x = startx;x < endx;x++)
1209 d[0] = (int)(in4f[x*4+2]*255.0f);if (d[0] > 255) d[0] = 255;
1210 d[1] = (int)(in4f[x*4+1]*255.0f);if (d[1] > 255) d[1] = 255;
1211 d[2] = (int)(in4f[x*4+0]*255.0f);if (d[2] > 255) d[2] = 255;
1212 d[3] = (int)(in4f[x*4+3]*255.0f);if (d[3] > 255) d[3] = 255;
1213 pixel[x*4+0] = d[0];
1214 pixel[x*4+1] = d[1];
1215 pixel[x*4+2] = d[2];
1216 pixel[x*4+3] = d[3];
1219 case DPSOFTRAST_BLENDMODE_ALPHA:
1220 for (x = startx;x < endx;x++)
1224 a = in4f[x*4+3] * 255.0f;
1225 b = 1.0f - in4f[x*4+3];
1226 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1227 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1228 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1229 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1230 pixel[x*4+0] = d[0];
1231 pixel[x*4+1] = d[1];
1232 pixel[x*4+2] = d[2];
1233 pixel[x*4+3] = d[3];
1236 case DPSOFTRAST_BLENDMODE_ADDALPHA:
1237 for (x = startx;x < endx;x++)
1241 a = in4f[x*4+3] * 255.0f;
1242 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1243 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1244 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1245 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1246 pixel[x*4+0] = d[0];
1247 pixel[x*4+1] = d[1];
1248 pixel[x*4+2] = d[2];
1249 pixel[x*4+3] = d[3];
1252 case DPSOFTRAST_BLENDMODE_ADD:
1253 for (x = startx;x < endx;x++)
1257 d[0] = (int)(in4f[x*4+2]*255.0f+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1258 d[1] = (int)(in4f[x*4+1]*255.0f+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1259 d[2] = (int)(in4f[x*4+0]*255.0f+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1260 d[3] = (int)(in4f[x*4+3]*255.0f+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1261 pixel[x*4+0] = d[0];
1262 pixel[x*4+1] = d[1];
1263 pixel[x*4+2] = d[2];
1264 pixel[x*4+3] = d[3];
1267 case DPSOFTRAST_BLENDMODE_INVMOD:
1268 for (x = startx;x < endx;x++)
1272 d[0] = (int)((1.0f-in4f[x*4+2])*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1273 d[1] = (int)((1.0f-in4f[x*4+1])*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1274 d[2] = (int)((1.0f-in4f[x*4+0])*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1275 d[3] = (int)((1.0f-in4f[x*4+3])*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1276 pixel[x*4+0] = d[0];
1277 pixel[x*4+1] = d[1];
1278 pixel[x*4+2] = d[2];
1279 pixel[x*4+3] = d[3];
1282 case DPSOFTRAST_BLENDMODE_MUL:
1283 for (x = startx;x < endx;x++)
1287 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1288 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1289 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1290 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1291 pixel[x*4+0] = d[0];
1292 pixel[x*4+1] = d[1];
1293 pixel[x*4+2] = d[2];
1294 pixel[x*4+3] = d[3];
1297 case DPSOFTRAST_BLENDMODE_MUL2:
1298 for (x = startx;x < endx;x++)
1302 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]*2.0f);if (d[0] > 255) d[0] = 255;
1303 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]*2.0f);if (d[1] > 255) d[1] = 255;
1304 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]*2.0f);if (d[2] > 255) d[2] = 255;
1305 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]*2.0f);if (d[3] > 255) d[3] = 255;
1306 pixel[x*4+0] = d[0];
1307 pixel[x*4+1] = d[1];
1308 pixel[x*4+2] = d[2];
1309 pixel[x*4+3] = d[3];
1312 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1313 for (x = startx;x < endx;x++)
1317 a = in4f[x*4+3] * -255.0f;
1318 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;if (d[0] < 0) d[0] = 0;
1319 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;if (d[1] < 0) d[1] = 0;
1320 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;if (d[2] < 0) d[2] = 0;
1321 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;if (d[3] < 0) d[3] = 0;
1322 pixel[x*4+0] = d[0];
1323 pixel[x*4+1] = d[1];
1324 pixel[x*4+2] = d[2];
1325 pixel[x*4+3] = d[3];
1331 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span *span, float *out4f, int texunitindex, int arrayindex, const float *zf)
1334 int startx = span->startx;
1335 int endx = span->endx;
1353 unsigned char *pixelbase;
1354 unsigned char *pixel[4];
1355 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1356 // if no texture is bound, just fill it with white
1359 for (x = startx;x < endx;x++)
1361 out4f[x*4+0] = 1.0f;
1362 out4f[x*4+1] = 1.0f;
1363 out4f[x*4+2] = 1.0f;
1364 out4f[x*4+3] = 1.0f;
1368 // if texture is 1 pixel, just fill it with that color
1369 if (texture->width * texture->height * texture->depth * texture->sides == 1)
1371 c[0] = texture->bytes[2] * (1.0f/255.0f);
1372 c[1] = texture->bytes[1] * (1.0f/255.0f);
1373 c[2] = texture->bytes[0] * (1.0f/255.0f);
1374 c[3] = texture->bytes[3] * (1.0f/255.0f);
1375 for (x = startx;x < endx;x++)
1377 out4f[x*4+0] = c[0];
1378 out4f[x*4+1] = c[1];
1379 out4f[x*4+2] = c[2];
1380 out4f[x*4+3] = c[3];
1384 filter = dpsoftrast.texbound[texunitindex]->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1385 data[0] = span->data[0][arrayindex][0];
1386 data[1] = span->data[0][arrayindex][1];
1387 data[2] = span->data[0][arrayindex][2];
1388 data[3] = span->data[0][arrayindex][3];
1389 slope[0] = span->data[1][arrayindex][0];
1390 slope[1] = span->data[1][arrayindex][1];
1391 slope[2] = span->data[1][arrayindex][2];
1392 slope[3] = span->data[1][arrayindex][3];
1393 flags = dpsoftrast.texbound[texunitindex]->flags;
1394 pixelbase = (unsigned char *)dpsoftrast.texbound[texunitindex]->bytes;
1395 tcscale[0] = dpsoftrast.texbound[texunitindex]->width;
1396 tcscale[1] = dpsoftrast.texbound[texunitindex]->height;
1397 tciwidth = dpsoftrast.texbound[texunitindex]->width;
1398 tcimin[0] = dpsoftrast.texbound[texunitindex]->clampmin[0];
1399 tcimin[1] = dpsoftrast.texbound[texunitindex]->clampmin[1];
1400 tcimax[0] = dpsoftrast.texbound[texunitindex]->clampmax[0];
1401 tcimax[1] = dpsoftrast.texbound[texunitindex]->clampmax[1];
1402 tciwrapmask[0] = dpsoftrast.texbound[texunitindex]->wrapmask[0];
1403 tciwrapmask[1] = dpsoftrast.texbound[texunitindex]->wrapmask[1];
1406 if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1408 for (x = startx;x < endx;x++)
1411 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0];
1412 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1];
1413 tci[0] = (int)floor(tc[0]);
1414 tci[1] = (int)floor(tc[1]);
1415 tci1[0] = tci[0] + 1;
1416 tci1[1] = tci[1] + 1;
1417 frac[0] = tc[0] - tci[0];ifrac[0] = 1.0f - frac[0];
1418 frac[1] = tc[1] - tci[1];ifrac[1] = 1.0f - frac[1];
1419 lerp[0] = ifrac[0]*ifrac[1];
1420 lerp[1] = frac[0]*ifrac[1];
1421 lerp[2] = ifrac[0]* frac[1];
1422 lerp[3] = frac[0]* frac[1];
1423 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1424 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1425 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1426 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1427 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1428 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1429 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1430 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1431 c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 255.0f);
1432 c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 255.0f);
1433 c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 255.0f);
1434 c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 255.0f);
1435 out4f[x*4+0] = c[0];
1436 out4f[x*4+1] = c[1];
1437 out4f[x*4+2] = c[2];
1438 out4f[x*4+3] = c[3];
1443 for (x = startx;x < endx;x++)
1446 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0];
1447 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1];
1448 tci[0] = (int)floor(tc[0]);
1449 tci[1] = (int)floor(tc[1]);
1450 tci1[0] = tci[0] + 1;
1451 tci1[1] = tci[1] + 1;
1452 frac[0] = tc[0] - tci[0];ifrac[0] = 1.0f - frac[0];
1453 frac[1] = tc[1] - tci[1];ifrac[1] = 1.0f - frac[1];
1454 lerp[0] = ifrac[0]*ifrac[1];
1455 lerp[1] = frac[0]*ifrac[1];
1456 lerp[2] = ifrac[0]* frac[1];
1457 lerp[3] = frac[0]* frac[1];
1458 tci[0] &= tciwrapmask[0];
1459 tci[1] &= tciwrapmask[1];
1460 tci1[0] &= tciwrapmask[0];
1461 tci1[1] &= tciwrapmask[1];
1462 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1463 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1464 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1465 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1466 c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 255.0f);
1467 c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 255.0f);
1468 c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 255.0f);
1469 c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 255.0f);
1470 out4f[x*4+0] = c[0];
1471 out4f[x*4+1] = c[1];
1472 out4f[x*4+2] = c[2];
1473 out4f[x*4+3] = c[3];
1479 if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1481 for (x = startx;x < endx;x++)
1484 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0];
1485 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1];
1486 tci[0] = (int)floor(tc[0]);
1487 tci[1] = (int)floor(tc[1]);
1488 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1489 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1490 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1491 c[0] = pixel[0][2] * (1.0f / 255.0f);
1492 c[1] = pixel[0][1] * (1.0f / 255.0f);
1493 c[2] = pixel[0][0] * (1.0f / 255.0f);
1494 c[3] = pixel[0][3] * (1.0f / 255.0f);
1495 out4f[x*4+0] = c[0];
1496 out4f[x*4+1] = c[1];
1497 out4f[x*4+2] = c[2];
1498 out4f[x*4+3] = c[3];
1503 for (x = startx;x < endx;x++)
1506 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0];
1507 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1];
1508 tci[0] = (int)floor(tc[0]);
1509 tci[1] = (int)floor(tc[1]);
1510 tci[0] &= tciwrapmask[0];
1511 tci[1] &= tciwrapmask[1];
1512 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1513 c[0] = pixel[0][2] * (1.0f / 255.0f);
1514 c[1] = pixel[0][1] * (1.0f / 255.0f);
1515 c[2] = pixel[0][0] * (1.0f / 255.0f);
1516 c[3] = pixel[0][3] * (1.0f / 255.0f);
1517 out4f[x*4+0] = c[0];
1518 out4f[x*4+1] = c[1];
1519 out4f[x*4+2] = c[2];
1520 out4f[x*4+3] = c[3];
1526 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *in4f, int arrayindex, const float *zf)
1529 int startx = span->startx;
1530 int endx = span->endx;
1535 data[0] = span->data[0][arrayindex][0];
1536 data[1] = span->data[0][arrayindex][1];
1537 data[2] = span->data[0][arrayindex][2];
1538 data[3] = span->data[0][arrayindex][3];
1539 slope[0] = span->data[1][arrayindex][0];
1540 slope[1] = span->data[1][arrayindex][1];
1541 slope[2] = span->data[1][arrayindex][2];
1542 slope[3] = span->data[1][arrayindex][3];
1543 for (x = startx;x < endx;x++)
1546 c[0] = (data[0] + slope[0]*x) * z;
1547 c[1] = (data[1] + slope[1]*x) * z;
1548 c[2] = (data[2] + slope[2]*x) * z;
1549 c[3] = (data[3] + slope[3]*x) * z;
1550 out4f[x*4+0] = in4f[x*4+0] * c[0];
1551 out4f[x*4+1] = in4f[x*4+1] * c[1];
1552 out4f[x*4+2] = in4f[x*4+2] * c[2];
1553 out4f[x*4+3] = in4f[x*4+3] * c[3];
1557 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
1559 int x, startx = span->startx, endx = span->endx;
1560 float c[4], localcolor[4];
1561 localcolor[0] = subcolor[0];
1562 localcolor[1] = subcolor[1];
1563 localcolor[2] = subcolor[2];
1564 localcolor[3] = subcolor[3];
1565 for (x = startx;x < endx;x++)
1567 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
1568 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
1569 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
1570 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
1571 out4f[x*4+0] = ina4f[x*4+0] + c[0];
1572 out4f[x*4+1] = ina4f[x*4+1] + c[1];
1573 out4f[x*4+2] = ina4f[x*4+2] + c[2];
1574 out4f[x*4+3] = ina4f[x*4+3] + c[3];
1578 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *in4f, const float *color)
1580 int x, startx = span->startx, endx = span->endx;
1581 float localcolor[4], ilerp, lerp;
1582 localcolor[0] = color[0];
1583 localcolor[1] = color[1];
1584 localcolor[2] = color[2];
1585 localcolor[3] = color[3];
1586 ilerp = 1.0f - localcolor[3];
1587 lerp = localcolor[3];
1588 for (x = startx;x < endx;x++)
1590 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
1591 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
1592 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
1593 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
1597 void DPSOFTRAST_Draw_Span_Lightmap(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *lightmap)
1599 int x, startx = span->startx, endx = span->endx;
1600 float Color_Ambient[4], Color_Diffuse[4];
1601 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1602 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1603 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1604 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1605 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
1606 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
1607 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
1608 Color_Diffuse[3] = 0.0f;
1609 for (x = startx;x < endx;x++)
1611 out4f[x*4+0] = diffuse[x*4+0] * (Color_Ambient[0] + lightmap[x*4+0] * Color_Diffuse[0]);
1612 out4f[x*4+1] = diffuse[x*4+1] * (Color_Ambient[1] + lightmap[x*4+1] * Color_Diffuse[1]);
1613 out4f[x*4+2] = diffuse[x*4+2] * (Color_Ambient[2] + lightmap[x*4+2] * Color_Diffuse[2]);
1614 out4f[x*4+3] = diffuse[x*4+3] * (Color_Ambient[3] + lightmap[x*4+3] * Color_Diffuse[3]);
1618 void DPSOFTRAST_Draw_Span_VertexColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1620 int x, startx = span->startx, endx = span->endx;
1621 float Color_Ambient[4], Color_Diffuse[4];
1626 int arrayindex = DPSOFTRAST_ARRAY_COLOR;
1627 data[0] = span->data[0][arrayindex][0];
1628 data[1] = span->data[0][arrayindex][1];
1629 data[2] = span->data[0][arrayindex][2];
1630 data[3] = span->data[0][arrayindex][3];
1631 slope[0] = span->data[1][arrayindex][0];
1632 slope[1] = span->data[1][arrayindex][1];
1633 slope[2] = span->data[1][arrayindex][2];
1634 slope[3] = span->data[1][arrayindex][3];
1635 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1636 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1637 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1638 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1639 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
1640 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
1641 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
1642 Color_Diffuse[3] = 0.0f;
1643 for (x = startx;x < endx;x++)
1646 c[0] = (data[0] + slope[0]*x) * z;
1647 c[1] = (data[1] + slope[1]*x) * z;
1648 c[2] = (data[2] + slope[2]*x) * z;
1649 c[3] = (data[3] + slope[3]*x) * z;
1650 out4f[x*4+0] = diffuse[x*4+0] * (Color_Ambient[0] + c[0] * Color_Diffuse[0]);
1651 out4f[x*4+1] = diffuse[x*4+1] * (Color_Ambient[1] + c[1] * Color_Diffuse[1]);
1652 out4f[x*4+2] = diffuse[x*4+2] * (Color_Ambient[2] + c[2] * Color_Diffuse[2]);
1653 out4f[x*4+3] = diffuse[x*4+3] * (Color_Ambient[3] + c[3] * Color_Diffuse[3]);
1657 void DPSOFTRAST_Draw_Span_FlatColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse)
1659 int x, startx = span->startx, endx = span->endx;
1660 float Color_Ambient[4];
1661 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1662 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1663 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1664 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1665 for (x = startx;x < endx;x++)
1667 out4f[x*4+0] = diffuse[x*4+0] * Color_Ambient[0];
1668 out4f[x*4+1] = diffuse[x*4+1] * Color_Ambient[1];
1669 out4f[x*4+2] = diffuse[x*4+2] * Color_Ambient[2];
1670 out4f[x*4+3] = diffuse[x*4+3] * Color_Ambient[3];
1674 void DPSOFTRAST_Draw_VertexShader(void)
1676 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1);
1677 switch(dpsoftrast.shader_mode)
1679 case SHADERMODE_GENERIC: ///< (particles/HUD/etc) vertex color: optionally multiplied by one texture
1680 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
1681 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1683 case SHADERMODE_POSTPROCESS: ///< postprocessing shader (r_glsl_postprocess)
1684 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1685 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
1687 case SHADERMODE_DEPTH_OR_SHADOW: ///< (depthfirst/shadows) vertex shader only
1689 case SHADERMODE_FLATCOLOR: ///< (lightmap) modulate texture by uniform color (q1bsp: q3bsp)
1690 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1692 case SHADERMODE_VERTEXCOLOR: ///< (lightmap) modulate texture by vertex colors (q3bsp)
1693 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
1694 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1696 case SHADERMODE_LIGHTMAP: ///< (lightmap) modulate texture by lightmap texture (q1bsp: q3bsp)
1697 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1698 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
1700 case SHADERMODE_FAKELIGHT: ///< (fakelight) modulate texture by "fake" lighting (no lightmaps: no nothing)
1702 case SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE: ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1704 case SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE: ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1706 case SHADERMODE_LIGHTDIRECTION: ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1708 case SHADERMODE_LIGHTSOURCE: ///< (lightsource) use directional pixel shading from light source (rtlight)
1710 case SHADERMODE_REFRACTION: ///< refract background (the material is rendered normally after this pass)
1712 case SHADERMODE_WATER: ///< refract background and reflection (the material is rendered normally after this pass)
1714 case SHADERMODE_SHOWDEPTH: ///< (debugging) renders depth as color
1716 case SHADERMODE_DEFERREDGEOMETRY: ///< (deferred) render material properties to screenspace geometry buffers
1718 case SHADERMODE_DEFERREDLIGHTSOURCE: ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
1720 case SHADERMODE_COUNT:
1725 void DPSOFTRAST_Draw_PixelShaderSpan(const DPSOFTRAST_State_Draw_Span *span)
1727 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
1728 float buffer_texture_color[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1729 float buffer_texture_lightmap[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1730 float buffer_FragColor[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1731 switch(dpsoftrast.shader_mode)
1733 case SHADERMODE_GENERIC: ///< (particles/HUD/etc) vertex color: optionally multiplied by one texture
1734 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1735 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_FIRST, 2, buffer_z);
1736 DPSOFTRAST_Draw_Span_MultiplyVarying(span, buffer_FragColor, buffer_texture_color, 1, buffer_z);
1737 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1739 case SHADERMODE_POSTPROCESS: ///< postprocessing shader (r_glsl_postprocess)
1740 // TODO: optimize!! at the very least there is no reason to use texture sampling on the frame texture
1741 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1742 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_FragColor, GL20TU_FIRST, 2, buffer_z);
1743 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
1745 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_SECOND, 3, buffer_z);
1746 DPSOFTRAST_Draw_Span_AddBloom(span, buffer_FragColor, buffer_FragColor, buffer_texture_color, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
1748 DPSOFTRAST_Draw_Span_MixUniformColor(span, buffer_FragColor, buffer_FragColor, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
1749 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
1751 // TODO: implement saturation
1753 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
1755 // TODO: implement gammaramps
1757 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1759 case SHADERMODE_DEPTH_OR_SHADOW: ///< (depthfirst/shadows) vertex shader only
1761 case SHADERMODE_FLATCOLOR: ///< (lightmap) modulate texture by uniform color (q1bsp: q3bsp)
1762 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1763 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1764 DPSOFTRAST_Draw_Span_FlatColor(span, buffer_FragColor, buffer_texture_color);
1765 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1767 case SHADERMODE_VERTEXCOLOR: ///< (lightmap) modulate texture by vertex colors (q3bsp)
1768 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1769 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1770 DPSOFTRAST_Draw_Span_VertexColor(span, buffer_FragColor, buffer_texture_color, buffer_z);
1771 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1773 case SHADERMODE_LIGHTMAP: ///< (lightmap) modulate texture by lightmap texture (q1bsp: q3bsp)
1774 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1775 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1776 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_lightmap, GL20TU_LIGHTMAP, 6, buffer_z);
1777 DPSOFTRAST_Draw_Span_Lightmap(span, buffer_FragColor, buffer_texture_color, buffer_texture_lightmap);
1778 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1780 case SHADERMODE_FAKELIGHT: ///< (fakelight) modulate texture by "fake" lighting (no lightmaps: no nothing)
1782 case SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE: ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1784 case SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE: ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1786 case SHADERMODE_LIGHTDIRECTION: ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1788 case SHADERMODE_LIGHTSOURCE: ///< (lightsource) use directional pixel shading from light source (rtlight)
1790 case SHADERMODE_REFRACTION: ///< refract background (the material is rendered normally after this pass)
1792 case SHADERMODE_WATER: ///< refract background and reflection (the material is rendered normally after this pass)
1794 case SHADERMODE_SHOWDEPTH: ///< (debugging) renders depth as color
1796 case SHADERMODE_DEFERREDGEOMETRY: ///< (deferred) render material properties to screenspace geometry buffers
1798 case SHADERMODE_DEFERREDLIGHTSOURCE: ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
1800 case SHADERMODE_COUNT:
1805 void DPSOFTRAST_Draw_ProcessSpans(void)
1811 int numspans = dpsoftrast.draw.numspans;
1813 // unsigned int *colorpixel;
1814 unsigned int *depthpixel;
1820 DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
1821 unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
1822 for (i = 0;i < numspans;i++, span++)
1824 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1825 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1826 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
1828 depth = (int)(w*DPSOFTRAST_DEPTHSCALE);
1829 depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
1830 depthpixel = dpsoftrast.fb_depthpixels + span->start;
1831 switch(dpsoftrast.fb_depthfunc)
1834 case GL_ALWAYS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
1835 case GL_LESS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
1836 case GL_LEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
1837 case GL_EQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
1838 case GL_GEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
1839 case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
1840 case GL_NEVER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
1842 //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
1843 //for (x = 0;x < span->length;x++)
1844 // colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
1845 // if there is no color buffer, skip pixel shader
1847 endx = span->length;
1848 while (startx < endx && !pixelmask[startx])
1850 while (endx > startx && !pixelmask[endx-1])
1853 continue; // no pixels to fill
1854 span->pixelmask = pixelmask;
1855 span->startx = startx;
1857 // run pixel shader if appropriate
1858 // do this before running depthmask code, to allow the pixelshader
1859 // to clear pixelmask values for alpha testing
1860 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
1861 DPSOFTRAST_Draw_PixelShaderSpan(span);
1862 if (dpsoftrast.user.depthmask)
1863 for (x = 0, d = depth;x < span->length;x++, d += depthslope)
1869 // no depth testing means we're just dealing with color...
1870 // if there is no color buffer, skip pixel shader
1871 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
1873 memset(pixelmask, 1, span->length);
1874 span->pixelmask = pixelmask;
1876 span->endx = span->length;
1877 DPSOFTRAST_Draw_PixelShaderSpan(span);
1883 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s, int numarrays)
1885 int cullface = dpsoftrast.user.cullface;
1886 int width = dpsoftrast.fb_width;
1887 int height = dpsoftrast.fb_height;
1919 float trianglearea2;
1920 float triangleedge[2][4];
1921 float trianglenormal[4];
1923 float clipped[DPSOFTRAST_ARRAY_TOTAL][4][4];
1925 float proj[DPSOFTRAST_ARRAY_TOTAL][4][4];
1926 DPSOFTRAST_State_Draw_Span *span;
1927 DPSOFTRAST_State_Draw_Span *oldspan;
1928 for (i = 0;i < numtriangles;i++)
1930 // generate the 3 edges of this triangle
1931 // generate spans for the triangle - switch based on left split or right split classification of triangle
1934 e[0] = element3i[i*3+0] - firstvertex;
1935 e[1] = element3i[i*3+1] - firstvertex;
1936 e[2] = element3i[i*3+2] - firstvertex;
1940 e[0] = element3s[i*3+0] - firstvertex;
1941 e[1] = element3s[i*3+1] - firstvertex;
1942 e[2] = element3s[i*3+2] - firstvertex;
1950 triangleedge[0][0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+0] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+0];
1951 triangleedge[0][1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+1] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+1];
1952 triangleedge[0][2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2];
1953 triangleedge[1][0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+0] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+0];
1954 triangleedge[1][1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+1] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+1];
1955 triangleedge[1][2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] - dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2];
1956 trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
1957 trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
1958 trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
1959 trianglearea2 = trianglenormal[0] * trianglenormal[0] + trianglenormal[1] * trianglenormal[1] + trianglenormal[2] * trianglenormal[2];
1960 // skip degenerate triangles, nothing good can come from them...
1961 if (trianglearea2 == 0.0f)
1963 // apply current cullface mode (this culls many triangles)
1967 if (trianglenormal[2] < 0)
1971 if (trianglenormal[2] > 0)
1975 // calculate distance from nearplane
1976 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + 1.0f;
1977 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + 1.0f;
1978 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + 1.0f;
1980 if (clipdist[0] < 0.0f)
1982 if (clipdist[1] < 0.0f)
1984 if (clipdist[2] < 0.0f)
1986 // clip triangle if necessary
1990 // triangle is entirely in front of nearplane
1992 // macros for clipping vertices
1993 #define CLIPPEDVERTEXLERP(k,p1,p2) \
1994 frac = clipdist[p1] / (clipdist[p1] - clipdist[p2]);\
1995 ifrac = 1.0f - frac;\
1996 for (j = 0;j < numarrays;j++)\
1998 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+0]*frac;\
1999 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+1]*frac;\
2000 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+2]*frac;\
2001 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+3]*frac;\
2003 DPSOFTRAST_Draw_ProjectVertices(screen[k], clipped[DPSOFTRAST_ARRAY_POSITION][k], 1)
2004 #define CLIPPEDVERTEXCOPY(k,p1) \
2005 for (j = 0;j < numarrays;j++)\
2007 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0];\
2008 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1];\
2009 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2];\
2010 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3];\
2012 screen[k][0] = dpsoftrast.draw.screencoord4f[e[p1]*4+0];\
2013 screen[k][1] = dpsoftrast.draw.screencoord4f[e[p1]*4+1];\
2014 screen[k][2] = dpsoftrast.draw.screencoord4f[e[p1]*4+2];\
2015 screen[k][3] = dpsoftrast.draw.screencoord4f[e[p1]*4+3];
2017 CLIPPEDVERTEXCOPY(0,0);
2018 CLIPPEDVERTEXCOPY(1,1);
2019 CLIPPEDVERTEXCOPY(2,2);
2023 CLIPPEDVERTEXLERP(0,0,1);
2024 CLIPPEDVERTEXCOPY(1,1);
2025 CLIPPEDVERTEXCOPY(2,2);
2026 CLIPPEDVERTEXLERP(3,2,0);
2030 CLIPPEDVERTEXCOPY(0,0);
2031 CLIPPEDVERTEXLERP(1,0,1);
2032 CLIPPEDVERTEXLERP(2,1,2);
2033 CLIPPEDVERTEXCOPY(3,2);
2037 CLIPPEDVERTEXLERP(0,1,2);
2038 CLIPPEDVERTEXCOPY(1,2);
2039 CLIPPEDVERTEXLERP(2,2,0);
2043 CLIPPEDVERTEXCOPY(0,0);
2044 CLIPPEDVERTEXCOPY(1,1);
2045 CLIPPEDVERTEXLERP(2,1,2);
2046 CLIPPEDVERTEXLERP(3,2,0);
2050 CLIPPEDVERTEXLERP(0,0,1);
2051 CLIPPEDVERTEXCOPY(1,1);
2052 CLIPPEDVERTEXLERP(2,1,2);
2056 CLIPPEDVERTEXCOPY(0,0);
2057 CLIPPEDVERTEXLERP(1,0,1);
2058 CLIPPEDVERTEXLERP(2,2,0);
2062 // triangle is entirely behind nearplane
2065 // calculate integer y coords for triangle points
2066 screenx[0] = (int)(screen[0][0]);
2067 screeny[0] = (int)(screen[0][1]);
2068 screenx[1] = (int)(screen[1][0]);
2069 screeny[1] = (int)(screen[1][1]);
2070 screenx[2] = (int)(screen[2][0]);
2071 screeny[2] = (int)(screen[2][1]);
2072 screenx[3] = (int)(screen[3][0]);
2073 screeny[3] = (int)(screen[3][1]);
2074 // figure out the extents (bounding box) of the triangle
2075 extent[0] = screenx[0];
2076 extent[1] = screeny[0];
2077 extent[2] = screenx[0];
2078 extent[3] = screeny[0];
2079 for (j = 1;j < numpoints;j++)
2081 if (extent[0] > screenx[j]) extent[0] = screenx[j];
2082 if (extent[1] > screeny[j]) extent[1] = screeny[j];
2083 if (extent[2] < screenx[j]) extent[2] = screenx[j];
2084 if (extent[3] < screeny[j]) extent[3] = screeny[j];
2094 if (extent[2] > width)
2096 if (extent[3] > height)
2098 // skip offscreen triangles
2099 if (extent[2] <= extent[0] || extent[3] <= extent[1])
2101 // okay, this triangle is going to produce spans, we'd better project
2102 // the interpolants now (this is what gives perspective texturing),
2103 // this consists of simply multiplying all arrays by the W coord
2104 // (which is basically 1/Z), which will be undone per-pixel
2105 // (multiplying by Z again) to get the perspective-correct array
2107 for (j = 0;j < numarrays;j++)
2109 for (k = 0;k < numpoints;k++)
2112 proj[j][k][0] = clipped[j][k][0] * w;
2113 proj[j][k][1] = clipped[j][k][1] * w;
2114 proj[j][k][2] = clipped[j][k][2] * w;
2115 proj[j][k][3] = clipped[j][k][3] * w;
2118 // iterate potential spans
2119 // TODO: optimize? if we figured out the edge order beforehand, this
2120 // could do loops over the edges in the proper order rather than
2121 // selecting them for each span
2122 // TODO: optimize? the edges could have data slopes calculated
2123 // TODO: optimize? the data slopes could be calculated as a plane
2124 // (2D slopes) to avoid any interpolation along edges at all
2125 for (y = extent[1];y < extent[3];y++)
2127 // get center of pixel y
2129 // do the compares all at once
2130 screenyless[0] = y <= screeny[0];
2131 screenyless[1] = y <= screeny[1];
2132 screenyless[2] = y <= screeny[2];
2133 screenyless[3] = y <= screeny[3];
2136 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4 + screenyless[3] * 8)
2138 case 0: /*0000*/ continue;
2139 case 1: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 0;edge1n = 1;break;
2140 case 2: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
2141 case 3: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 2;break;
2142 case 4: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break;
2143 case 5: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break; // concave - nonsense
2144 case 6: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 3;break;
2145 case 7: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 3;break;
2146 case 8: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 3;edge1n = 0;break;
2147 case 9: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 1;break;
2148 case 10: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
2149 case 11: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break;
2150 case 12: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 0;break;
2151 case 13: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
2152 case 14: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 0;break;
2153 case 15: /*1111*/ continue;
2158 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4)
2160 case 0: /*000*/ continue;
2161 case 1: /*100*/ edge0p = 2;edge0n = 0;edge1p = 0;edge1n = 1;break;
2162 case 2: /*010*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
2163 case 3: /*110*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 2;break;
2164 case 4: /*001*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 0;break;
2165 case 5: /*101*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
2166 case 6: /*011*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 0;break;
2167 case 7: /*111*/ continue;
2177 for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
2179 if (screenyless[k] && !screenyless[j])
2185 else if (screenyless[j] && !screenyless[k])
2192 if (foundedges != 3)
2194 if (cedge0p != edge0p || cedge0n != edge0n || cedge1p != edge1p || cedge1n != edge1n)
2197 printf("case %i%i%i%i is broken %i %i %i %i != %i %i %i %i\n", screenyless[0], screenyless[1], screenyless[2], screenyless[3], cedge0p, cedge0n, cedge1p, cedge1n, edge0p, edge0n, edge1p, edge1n);
2199 printf("case %i%i%i is broken %i %i %i %i != %i %i %i %i\n", screenyless[0], screenyless[1], screenyless[2], cedge0p, cedge0n, cedge1p, cedge1n, edge0p, edge0n, edge1p, edge1n);
2203 edge0ylerp = (yc - screen[edge0p][1]) / (screen[edge0n][1] - screen[edge0p][1]);
2204 edge1ylerp = (yc - screen[edge1p][1]) / (screen[edge1n][1] - screen[edge1p][1]);
2205 if (edge0ylerp < 0 || edge0ylerp > 1 || edge1ylerp < 0 || edge1ylerp > 1)
2207 edge0yilerp = 1.0f - edge0ylerp;
2208 edge1yilerp = 1.0f - edge1ylerp;
2209 edge0xf = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2210 edge1xf = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2211 if (edge0xf < edge1xf)
2221 startx = (int)ceil(startxf);
2222 endx = (int)ceil(endxf);
2229 if (startxf > startx || endxf < endx-1) { printf("%s:%i X wrong (%i to %i is outside %f to %f)\n", __FILE__, __LINE__, startx, endx, startxf, endxf); }
2230 spanilength = 1.0f / (endxf - startxf);
2231 startxlerp = startx - startxf;
2232 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
2233 span->start = y * width + startx;
2234 span->length = endx - startx;
2235 j = DPSOFTRAST_ARRAY_TOTAL;
2236 if (edge0xf < edge1xf)
2238 span->data[0][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2239 span->data[0][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
2240 span->data[0][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
2241 span->data[0][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
2242 span->data[1][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2243 span->data[1][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
2244 span->data[1][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
2245 span->data[1][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
2246 for (j = 0;j < numarrays;j++)
2248 span->data[0][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
2249 span->data[0][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
2250 span->data[0][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
2251 span->data[0][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
2252 span->data[1][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
2253 span->data[1][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
2254 span->data[1][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
2255 span->data[1][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
2260 span->data[0][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2261 span->data[0][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
2262 span->data[0][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
2263 span->data[0][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
2264 span->data[1][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2265 span->data[1][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
2266 span->data[1][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
2267 span->data[1][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
2268 for (j = 0;j < numarrays;j++)
2270 span->data[0][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
2271 span->data[0][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
2272 span->data[0][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
2273 span->data[0][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
2274 span->data[1][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
2275 span->data[1][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
2276 span->data[1][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
2277 span->data[1][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
2280 // change data[1][n][] to be a data slope
2281 j = DPSOFTRAST_ARRAY_TOTAL;
2282 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
2283 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
2284 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
2285 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
2286 for (j = 0;j < numarrays;j++)
2288 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
2289 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
2290 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
2291 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
2293 // adjust the data[0][n][] to be correct for the pixel centers
2294 // this also handles horizontal clipping where a major part of the
2295 // span may be off the left side of the screen
2296 j = DPSOFTRAST_ARRAY_TOTAL;
2297 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
2298 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
2299 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
2300 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
2301 for (j = 0;j < numarrays;j++)
2303 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
2304 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
2305 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
2306 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
2308 // to keep the shader routines from needing more than a small
2309 // buffer for pixel intermediate data, we split long spans...
2310 while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
2312 span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
2313 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
2315 DPSOFTRAST_Draw_ProcessSpans();
2316 dpsoftrast.draw.numspans = 0;
2319 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
2321 startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
2322 span->start = y * width + startx;
2323 span->length = endx - startx;
2324 j = DPSOFTRAST_ARRAY_TOTAL;
2325 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2326 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2327 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2328 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2329 for (j = 0;j < numarrays;j++)
2331 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2332 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2333 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2334 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2337 // after all that, we have a span suitable for the pixel shader...
2338 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
2340 DPSOFTRAST_Draw_ProcessSpans();
2341 dpsoftrast.draw.numspans = 0;
2344 // draw outlines over triangle for debugging
2345 // for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
2346 // DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
2348 if (dpsoftrast.draw.numspans)
2350 DPSOFTRAST_Draw_ProcessSpans();
2351 dpsoftrast.draw.numspans = 0;
2355 void DPSOFTRAST_Draw_DebugPoints(void)
2360 int numvertices = dpsoftrast.draw.numvertices;
2361 int w = dpsoftrast.fb_width;
2363 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
2365 bounds[0] = dpsoftrast.fb_viewportscissor[0];
2366 bounds[1] = dpsoftrast.fb_viewportscissor[1];
2367 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
2368 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
2369 for (i = 0;i < numvertices;i++)
2372 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
2374 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
2375 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
2376 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
2377 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
2378 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
2379 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
2380 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
2382 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
2383 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
2387 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
2389 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
2390 DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true, 1);
2391 DPSOFTRAST_Draw_VertexShader();
2392 DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
2393 DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numvertices, numtriangles, element3i, element3s, 3);
2396 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
2405 memset(&dpsoftrast, 0, sizeof(dpsoftrast));
2406 dpsoftrast.bigendian = u.b[3];
2407 dpsoftrast.fb_width = width;
2408 dpsoftrast.fb_height = height;
2409 dpsoftrast.fb_depthpixels = depthpixels;
2410 dpsoftrast.fb_colorpixels[0] = colorpixels;
2411 dpsoftrast.fb_colorpixels[1] = NULL;
2412 dpsoftrast.fb_colorpixels[1] = NULL;
2413 dpsoftrast.fb_colorpixels[1] = NULL;
2414 dpsoftrast.texture_firstfree = 1;
2415 dpsoftrast.texture_end = 1;
2416 dpsoftrast.texture_max = 0;
2417 dpsoftrast.user.colormask[0] = 1;
2418 dpsoftrast.user.colormask[1] = 1;
2419 dpsoftrast.user.colormask[2] = 1;
2420 dpsoftrast.user.colormask[3] = 1;
2421 dpsoftrast.user.blendfunc[0] = GL_ONE;
2422 dpsoftrast.user.blendfunc[1] = GL_ZERO;
2423 dpsoftrast.user.depthmask = true;
2424 dpsoftrast.user.depthtest = true;
2425 dpsoftrast.user.depthfunc = GL_LEQUAL;
2426 dpsoftrast.user.scissortest = false;
2427 dpsoftrast.user.cullface = GL_BACK;
2428 dpsoftrast.user.alphatest = false;
2429 dpsoftrast.user.alphafunc = GL_GREATER;
2430 dpsoftrast.user.alphavalue = 0.5f;
2431 dpsoftrast.user.scissor[0] = 0;
2432 dpsoftrast.user.scissor[1] = 0;
2433 dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
2434 dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
2435 dpsoftrast.user.viewport[0] = 0;
2436 dpsoftrast.user.viewport[1] = 0;
2437 dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
2438 dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
2439 dpsoftrast.user.depthrange[0] = 0;
2440 dpsoftrast.user.depthrange[1] = 1;
2441 dpsoftrast.user.polygonoffset[0] = 0;
2442 dpsoftrast.user.polygonoffset[1] = 0;
2443 dpsoftrast.user.color[0] = 1;
2444 dpsoftrast.user.color[1] = 1;
2445 dpsoftrast.user.color[2] = 1;
2446 dpsoftrast.user.color[3] = 1;
2447 dpsoftrast.validate = -1;
2448 DPSOFTRAST_Validate(-1);
2449 dpsoftrast.validate = 0;
2452 void DPSOFTRAST_Shutdown(void)
2455 for (i = 0;i < dpsoftrast.texture_end;i++)
2456 if (dpsoftrast.texture[i].bytes)
2457 free(dpsoftrast.texture[i].bytes);
2458 if (dpsoftrast.texture)
2459 free(dpsoftrast.texture);
2460 memset(&dpsoftrast, 0, sizeof(dpsoftrast));