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_TOTAL
157 DPSOFTRAST_BLENDMODE;
159 typedef struct DPSOFTRAST_State_s
161 // DPSOFTRAST_VALIDATE_ flags
167 unsigned int *fb_depthpixels;
168 unsigned int *fb_colorpixels[4];
170 const float *pointer_vertex3f;
171 const float *pointer_color4f;
172 const unsigned char *pointer_color4ub;
173 const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
176 int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
177 int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
178 DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
181 int shader_permutation;
182 float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4];
183 int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
185 // derived values (DPSOFTRAST_VALIDATE_FB)
186 int fb_clearscissor[4];
188 int fb_viewportscissor[4];
189 float fb_viewportcenter[2];
190 float fb_viewportscale[2];
192 // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
195 // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
200 int texture_firstfree;
201 DPSOFTRAST_Texture *texture;
206 const char *errorstring;
208 DPSOFTRAST_State_User user;
210 DPSOFTRAST_State_Draw draw;
214 DPSOFTRAST_State dpsoftrast;
216 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
217 #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))
218 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
219 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
221 void DPSOFTRAST_RecalcFB(void)
223 // calculate framebuffer scissor, viewport, viewport clipped by scissor,
224 // and viewport projection values
225 int x1, x2, x3, x4, x5, x6;
226 int y1, y2, y3, y4, y5, y6;
227 x1 = dpsoftrast.user.scissor[0];
228 x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
229 x3 = dpsoftrast.user.viewport[0];
230 x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
231 y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
232 y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
233 y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
234 y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
235 if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
237 if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
239 if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
241 if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
243 if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
244 x5 = x1;if (x5 < x3) x5 = x3;
245 x6 = x2;if (x6 > x4) x4 = x4;
246 y5 = y1;if (y5 < y3) y5 = y3;
247 y6 = y2;if (y6 > y4) y6 = y4;
248 dpsoftrast.fb_clearscissor[0] = x1;
249 dpsoftrast.fb_clearscissor[1] = y1;
250 dpsoftrast.fb_clearscissor[2] = x2 - x1;
251 dpsoftrast.fb_clearscissor[3] = y2 - y1;
252 dpsoftrast.fb_viewport[0] = x3;
253 dpsoftrast.fb_viewport[1] = y3;
254 dpsoftrast.fb_viewport[2] = x4 - x3;
255 dpsoftrast.fb_viewport[3] = y4 - y3;
256 dpsoftrast.fb_viewportscissor[0] = x5;
257 dpsoftrast.fb_viewportscissor[1] = y5;
258 dpsoftrast.fb_viewportscissor[2] = x6 - x5;
259 dpsoftrast.fb_viewportscissor[3] = y6 - y5;
260 dpsoftrast.fb_viewportcenter[0] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
261 dpsoftrast.fb_viewportcenter[1] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
262 dpsoftrast.fb_viewportscale[0] = 0.5f * dpsoftrast.user.viewport[2];
263 dpsoftrast.fb_viewportscale[1] = -0.5f * dpsoftrast.user.viewport[3];
266 void DPSOFTRAST_RecalcDepthFunc(void)
268 dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
271 int blendmodetable[][4] =
273 {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
274 {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
275 {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
276 {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
277 {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
278 {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
279 {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
280 {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
281 {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
284 void DPSOFTRAST_RecalcBlendFunc(void)
287 dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE;
288 for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++)
290 if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3])
292 dpsoftrast.fb_blendmode = blendmodetable[i][0];
298 #define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0)
300 void DPSOFTRAST_Validate(int mask)
302 mask &= dpsoftrast.validate;
305 if (mask & DPSOFTRAST_VALIDATE_FB)
307 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB;
308 DPSOFTRAST_RecalcFB();
310 if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
312 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
313 DPSOFTRAST_RecalcDepthFunc();
315 if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
317 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
318 DPSOFTRAST_RecalcBlendFunc();
322 DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
324 if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
325 return &dpsoftrast.texture[index];
329 int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
338 int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
339 int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
340 DPSOFTRAST_Texture *texture;
341 if (width*height*depth < 1)
343 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
346 if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
348 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
353 case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
354 case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
355 case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
357 case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
358 if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
360 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
365 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
368 if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
370 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
375 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
377 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
380 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
382 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
385 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
387 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
390 if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
392 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
395 if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
397 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
400 // find first empty slot in texture array
401 for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
402 if (!dpsoftrast.texture[texnum].bytes)
404 dpsoftrast.texture_firstfree = texnum + 1;
405 if (dpsoftrast.texture_max <= texnum)
407 // expand texture array as needed
408 if (dpsoftrast.texture_max < 1024)
409 dpsoftrast.texture_max = 1024;
411 dpsoftrast.texture_max *= 2;
412 dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
414 if (dpsoftrast.texture_end <= texnum)
415 dpsoftrast.texture_end = texnum + 1;
416 texture = &dpsoftrast.texture[texnum];
417 memset(texture, 0, sizeof(*texture));
418 texture->flags = flags;
419 texture->width = width;
420 texture->height = height;
421 texture->depth = depth;
422 texture->sides = sides;
433 s = w * h * d * sides * 4;
434 texture->mipmap[mipmaps][0] = size;
435 texture->mipmap[mipmaps][1] = s;
436 texture->mipmap[mipmaps][2] = w;
437 texture->mipmap[mipmaps][3] = h;
438 texture->mipmap[mipmaps][4] = d;
441 if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
447 texture->mipmaps = mipmaps;
448 texture->size = size;
450 // allocate the pixels now
451 texture->bytes = calloc(1, size);
455 void DPSOFTRAST_Texture_Free(int index)
457 DPSOFTRAST_Texture *texture;
458 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
460 free(texture->bytes);
461 texture->bytes = NULL;
462 memset(texture, 0, sizeof(*texture));
463 // adjust the free range and used range
464 if (dpsoftrast.texture_firstfree > index)
465 dpsoftrast.texture_firstfree = index;
466 while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
467 dpsoftrast.texture_end--;
469 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
471 int i, x, y, z, w, layer0, layer1, row0, row1;
472 unsigned char *o, *i0, *i1, *i2, *i3;
473 DPSOFTRAST_Texture *texture;
474 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
475 if (texture->mipmaps <= 1)
477 for (i = 1;i < texture->mipmaps;i++)
479 for (z = 0;z < texture->mipmap[i][4];z++)
483 if (layer1 >= texture->mipmap[i-1][4])
484 layer1 = texture->mipmap[i-1][4]-1;
485 for (y = 0;y < texture->mipmap[i][3];y++)
489 if (row1 >= texture->mipmap[i-1][3])
490 row1 = texture->mipmap[i-1][3]-1;
491 o = texture->bytes + texture->mipmap[i ][0] + 4*(texture->mipmap[i ][3] * texture->mipmap[i ][2] * z + texture->mipmap[i ][2] * y );
492 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);
493 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);
494 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);
495 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);
496 w = texture->mipmap[i][2];
499 if (texture->mipmap[i-1][2] > 1)
501 // average 3D texture
502 for (x = 0;x < w;x++)
504 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
505 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
506 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
507 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
512 // average 3D mipmap with parent width == 1
513 for (x = 0;x < w;x++)
515 o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
516 o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
517 o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
518 o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
524 if (texture->mipmap[i-1][2] > 1)
526 // average 2D texture (common case)
527 for (x = 0;x < w;x++)
529 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
530 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
531 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
532 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
537 // 2D texture with parent width == 1
538 for (x = 0;x < w;x++)
540 o[0] = (i0[0] + i1[0] + 1) >> 1;
541 o[1] = (i0[1] + i1[1] + 1) >> 1;
542 o[2] = (i0[2] + i1[2] + 1) >> 1;
543 o[3] = (i0[3] + i1[3] + 1) >> 1;
551 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
553 DPSOFTRAST_Texture *texture;
554 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
558 dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
560 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
562 DPSOFTRAST_Texture *texture;
563 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
565 memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
566 DPSOFTRAST_Texture_CalculateMipmaps(index);
568 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
570 DPSOFTRAST_Texture *texture;
571 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
572 return texture->width;
574 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
576 DPSOFTRAST_Texture *texture;
577 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
578 return texture->height;
580 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
582 DPSOFTRAST_Texture *texture;
583 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
584 return texture->depth;
586 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
588 DPSOFTRAST_Texture *texture;
589 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
590 return texture->bytes;
592 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
594 DPSOFTRAST_Texture *texture;
595 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
596 if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
598 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
601 texture->filter = filter;
604 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
606 dpsoftrast.fb_width = width;
607 dpsoftrast.fb_height = height;
608 dpsoftrast.fb_depthpixels = depthpixels;
609 dpsoftrast.fb_colorpixels[0] = colorpixels0;
610 dpsoftrast.fb_colorpixels[1] = colorpixels1;
611 dpsoftrast.fb_colorpixels[2] = colorpixels2;
612 dpsoftrast.fb_colorpixels[3] = colorpixels3;
614 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
616 dpsoftrast.user.viewport[0] = x;
617 dpsoftrast.user.viewport[1] = y;
618 dpsoftrast.user.viewport[2] = width;
619 dpsoftrast.user.viewport[3] = height;
620 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
622 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
624 int i, x1, y1, x2, y2, w, h, x, y;
627 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
628 x1 = dpsoftrast.fb_clearscissor[0];
629 y1 = dpsoftrast.fb_clearscissor[1];
630 x2 = dpsoftrast.fb_clearscissor[2];
631 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
636 // FIXME: honor dpsoftrast.fb_colormask?
637 c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
638 for (i = 0;i < 4;i++)
640 if (!dpsoftrast.fb_colorpixels[i])
642 for (y = y1;y < y2;y++)
644 p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
645 for (x = x1;x < x2;x++)
650 void DPSOFTRAST_ClearDepth(float d)
652 int x1, y1, x2, y2, w, h, x, y;
655 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
656 x1 = dpsoftrast.fb_clearscissor[0];
657 y1 = dpsoftrast.fb_clearscissor[1];
658 x2 = dpsoftrast.fb_clearscissor[2];
659 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
664 c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
665 for (y = y1;y < y2;y++)
667 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
668 for (x = x1;x < x2;x++)
672 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
674 dpsoftrast.user.colormask[0] = r != 0;
675 dpsoftrast.user.colormask[1] = g != 0;
676 dpsoftrast.user.colormask[2] = b != 0;
677 dpsoftrast.user.colormask[3] = a != 0;
678 dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
680 void DPSOFTRAST_DepthTest(int enable)
682 dpsoftrast.user.depthtest = enable;
683 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
685 void DPSOFTRAST_ScissorTest(int enable)
687 dpsoftrast.user.scissortest = enable;
688 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
690 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
692 dpsoftrast.user.scissor[0] = x;
693 dpsoftrast.user.scissor[1] = y;
694 dpsoftrast.user.scissor[2] = width;
695 dpsoftrast.user.scissor[3] = height;
696 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
699 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
702 dpsoftrast.user.blendfunc[0] = smodulate;
703 dpsoftrast.user.blendfunc[1] = dmodulate;
704 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
706 void DPSOFTRAST_BlendSubtract(int enable)
708 dpsoftrast.user.blendsubtract = enable != 0;
709 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
711 void DPSOFTRAST_DepthMask(int enable)
713 dpsoftrast.user.depthmask = enable;
715 void DPSOFTRAST_DepthFunc(int comparemode)
718 dpsoftrast.user.depthfunc = comparemode;
720 void DPSOFTRAST_DepthRange(float range0, float range1)
722 dpsoftrast.user.depthrange[0] = range0;
723 dpsoftrast.user.depthrange[1] = range1;
725 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
727 dpsoftrast.user.polygonoffset[0] = alongnormal;
728 dpsoftrast.user.polygonoffset[1] = intoview;
730 void DPSOFTRAST_CullFace(int mode)
733 dpsoftrast.user.cullface = mode;
735 void DPSOFTRAST_AlphaTest(float enable)
737 dpsoftrast.user.alphatest = enable;
739 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
742 dpsoftrast.user.alphafunc = alphafunc;
743 dpsoftrast.user.alphavalue = alphavalue;
745 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
747 dpsoftrast.user.color[0] = r;
748 dpsoftrast.user.color[1] = g;
749 dpsoftrast.user.color[2] = b;
750 dpsoftrast.user.color[3] = a;
752 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
754 int outstride = blockwidth * 4;
755 int instride = dpsoftrast.fb_width * 4;
758 int bx2 = blockx + blockwidth;
759 int by2 = blocky + blockheight;
764 unsigned char *inpixels;
767 if (bx1 < 0) bx1 = 0;
768 if (by1 < 0) by1 = 0;
769 if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
770 if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
773 inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
774 if (dpsoftrast.bigendian)
776 for (y = by1;y < by2;y++)
778 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
779 o = (unsigned char *)outpixels + (y - by1) * outstride;
780 for (x = bx1;x < bx2;x++)
793 for (y = by1;y < by2;y++)
795 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
796 o = (unsigned char *)outpixels + (y - by1) * outstride;
802 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
806 int tx2 = tx + width;
807 int ty2 = ty + height;
810 int sx2 = sx + width;
811 int sy2 = sy + height;
821 unsigned int *spixels;
822 unsigned int *tpixels;
823 DPSOFTRAST_Texture *texture;
824 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
825 if (mip < 0 || mip >= texture->mipmaps) return;
826 spixels = dpsoftrast.fb_colorpixels[0];
827 swidth = dpsoftrast.fb_width;
828 sheight = dpsoftrast.fb_height;
829 tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
830 twidth = texture->mipmap[mip][2];
831 theight = texture->mipmap[mip][3];
832 if (tx1 < 0) tx1 = 0;
833 if (ty1 < 0) ty1 = 0;
834 if (tx2 > twidth) tx2 = twidth;
835 if (ty2 > theight) ty2 = theight;
836 if (sx1 < 0) sx1 = 0;
837 if (sy1 < 0) sy1 = 0;
838 if (sx2 > swidth) sx2 = swidth;
839 if (sy2 > sheight) sy2 = sheight;
844 if (tw > sw) tw = sw;
845 if (th > sh) th = sh;
846 if (tw < 1 || th < 1)
848 for (y = 0;y < th;y++)
849 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
850 if (texture->mipmaps > 1)
851 DPSOFTRAST_Texture_CalculateMipmaps(index);
853 void DPSOFTRAST_SetTexture(int unitnum, int index)
855 DPSOFTRAST_Texture *texture;
856 if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
858 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
861 texture = DPSOFTRAST_Texture_GetByIndex(index);
862 if (index && !texture)
864 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
867 dpsoftrast.texbound[unitnum] = texture;
870 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
872 dpsoftrast.pointer_vertex3f = vertex3f;
873 dpsoftrast.stride_vertex = stride;
875 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
877 dpsoftrast.pointer_color4f = color4f;
878 dpsoftrast.pointer_color4ub = NULL;
879 dpsoftrast.stride_color = stride;
881 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
883 dpsoftrast.pointer_color4f = NULL;
884 dpsoftrast.pointer_color4ub = color4ub;
885 dpsoftrast.stride_color = stride;
887 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
889 dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
890 dpsoftrast.components_texcoord[unitnum] = numcomponents;
891 dpsoftrast.stride_texcoord[unitnum] = stride;
894 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
896 dpsoftrast.shader_mode = mode;
897 dpsoftrast.shader_permutation = permutation;
899 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
901 dpsoftrast.uniform4f[index*4+0] = v0;
902 dpsoftrast.uniform4f[index*4+1] = v1;
903 dpsoftrast.uniform4f[index*4+2] = v2;
904 dpsoftrast.uniform4f[index*4+3] = v3;
906 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v)
908 dpsoftrast.uniform4f[index*4+0] = v[0];
909 dpsoftrast.uniform4f[index*4+1] = v[1];
910 dpsoftrast.uniform4f[index*4+2] = v[2];
911 dpsoftrast.uniform4f[index*4+3] = v[3];
913 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v)
915 dpsoftrast.uniform4f[index*4+0] = v[0];
916 dpsoftrast.uniform4f[index*4+1] = v[1];
917 dpsoftrast.uniform4f[index*4+2] = v[2];
918 dpsoftrast.uniform4f[index*4+3] = v[3];
919 dpsoftrast.uniform4f[index*4+4] = v[4];
920 dpsoftrast.uniform4f[index*4+5] = v[5];
921 dpsoftrast.uniform4f[index*4+6] = v[6];
922 dpsoftrast.uniform4f[index*4+7] = v[7];
923 dpsoftrast.uniform4f[index*4+8] = v[8];
924 dpsoftrast.uniform4f[index*4+9] = v[9];
925 dpsoftrast.uniform4f[index*4+10] = v[10];
926 dpsoftrast.uniform4f[index*4+11] = v[11];
927 dpsoftrast.uniform4f[index*4+12] = v[12];
928 dpsoftrast.uniform4f[index*4+13] = v[13];
929 dpsoftrast.uniform4f[index*4+14] = v[14];
930 dpsoftrast.uniform4f[index*4+15] = v[15];
932 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
934 dpsoftrast.uniform1i[index] = i0;
937 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors, int numtexcoords)
945 const unsigned char *b;
946 dpsoftrast.draw.numvertices = numvertices;
947 if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
949 if (dpsoftrast.draw.maxvertices < 4096)
950 dpsoftrast.draw.maxvertices = 4096;
951 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
952 dpsoftrast.draw.maxvertices *= 2;
953 if (dpsoftrast.draw.in_array4f[0])
954 free(dpsoftrast.draw.in_array4f[0]);
955 data = calloc(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
956 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
957 dpsoftrast.draw.in_array4f[i] = data;
958 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
959 dpsoftrast.draw.post_array4f[i] = data;
960 dpsoftrast.draw.screencoord4f = data;
961 data += dpsoftrast.draw.maxvertices * 4;
963 stride = dpsoftrast.stride_vertex;
964 v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
965 p = dpsoftrast.draw.in_array4f[0];
966 for (i = 0;i < numvertices;i++)
973 v = (const float *)((const unsigned char *)v + stride);
977 if (dpsoftrast.pointer_color4f)
979 stride = dpsoftrast.stride_color;
980 v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
981 p = dpsoftrast.draw.in_array4f[1];
982 for (i = 0;i < numvertices;i++)
989 v = (const float *)((const unsigned char *)v + stride);
992 else if (dpsoftrast.pointer_color4ub)
994 stride = dpsoftrast.stride_color;
995 b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
996 p = dpsoftrast.draw.in_array4f[1];
997 for (i = 0;i < numvertices;i++)
999 p[0] = b[0] * (1.0f / 255.0f);
1000 p[1] = b[1] * (1.0f / 255.0f);
1001 p[2] = b[2] * (1.0f / 255.0f);
1002 p[3] = b[3] * (1.0f / 255.0f);
1004 b = (const unsigned char *)((const unsigned char *)b + stride);
1009 v = dpsoftrast.user.color;
1010 p = dpsoftrast.draw.in_array4f[1];
1011 for (i = 0;i < numvertices;i++)
1021 for (j = 0;j < numtexcoords;j++)
1023 if (dpsoftrast.pointer_texcoordf[j])
1025 stride = dpsoftrast.stride_texcoord[j];
1026 v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1027 p = dpsoftrast.draw.in_array4f[j+2];
1028 switch(dpsoftrast.components_texcoord[j])
1031 for (i = 0;i < numvertices;i++)
1038 v = (const float *)((const unsigned char *)v + stride);
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);
1068 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1073 memcpy(matrix, inmatrix16f, sizeof(float[16]));
1074 for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1076 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1077 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1078 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1079 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1083 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1085 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1088 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1090 // NOTE: this is used both as a whole mesh transform function and a
1091 // per-triangle transform function (for clipped triangles), accordingly
1092 // it should not crash on divide by 0 but the result of divide by 0 is
1097 float viewportcenter[4];
1098 float viewportscale[4];
1099 viewportscale[0] = dpsoftrast.fb_viewportscale[0];
1100 viewportscale[1] = dpsoftrast.fb_viewportscale[1];
1101 viewportscale[2] = 0.5f;
1102 viewportscale[3] = 0.0f;
1103 viewportcenter[0] = dpsoftrast.fb_viewportcenter[0];
1104 viewportcenter[1] = dpsoftrast.fb_viewportcenter[1];
1105 viewportcenter[2] = 0.5f;
1106 viewportcenter[3] = 0.0f;
1107 for (i = 0;i < numitems;i++)
1118 out4f[0] = viewportcenter[0] + viewportscale[0] * in4f[0] * w;
1119 out4f[1] = viewportcenter[1] + viewportscale[1] * in4f[1] * w;
1120 out4f[2] = viewportcenter[2] + viewportscale[2] * in4f[2] * w;
1121 out4f[3] = viewportcenter[3] + viewportscale[3] * in4f[3] * w;
1128 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1133 int w = dpsoftrast.fb_width;
1136 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1138 bounds[0] = dpsoftrast.fb_viewportscissor[0];
1139 bounds[1] = dpsoftrast.fb_viewportscissor[1];
1140 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1141 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1146 for (i = 0;i <= 128;i++)
1149 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1151 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1152 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1153 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1155 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1156 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1157 pixels[y*w+x] = 0xFFFFFFFF;
1161 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span *span, float *zf)
1164 int startx = span->startx;
1165 int endx = span->endx;
1166 float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1167 float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1168 // TODO: optimize by approximating every 8 pixels?
1169 for (x = startx;x < endx;x++)
1170 zf[x] = 1.0f / (w + wslope * x);
1173 void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span *span, const float *in4f)
1176 int startx = span->startx;
1177 int endx = span->endx;
1180 unsigned char *pixelmask = span->pixelmask;
1181 unsigned char *pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1184 pixel += span->start * 4;
1185 // handle alphatest now (this affects depth writes too)
1186 if (dpsoftrast.user.alphatest)
1187 for (x = startx;x < endx;x++)
1188 if (in4f[x*4+3] < 0.5f)
1189 pixelmask[x] = false;
1190 // FIXME: this does not handle bigendian
1191 switch(dpsoftrast.fb_blendmode)
1193 case DPSOFTRAST_BLENDMODE_OPAQUE:
1194 for (x = startx;x < endx;x++)
1198 d[0] = (int)(in4f[x*4+2]*255.0f);if (d[0] > 255) d[0] = 255;
1199 d[1] = (int)(in4f[x*4+1]*255.0f);if (d[1] > 255) d[1] = 255;
1200 d[2] = (int)(in4f[x*4+0]*255.0f);if (d[2] > 255) d[2] = 255;
1201 d[3] = (int)(in4f[x*4+3]*255.0f);if (d[3] > 255) d[3] = 255;
1202 pixel[x*4+0] = d[0];
1203 pixel[x*4+1] = d[1];
1204 pixel[x*4+2] = d[2];
1205 pixel[x*4+3] = d[3];
1208 case DPSOFTRAST_BLENDMODE_ALPHA:
1209 for (x = startx;x < endx;x++)
1213 a = in4f[x*4+3] * 255.0f;
1214 b = 1.0f - in4f[x*4+3];
1215 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1216 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1217 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1218 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1219 pixel[x*4+0] = d[0];
1220 pixel[x*4+1] = d[1];
1221 pixel[x*4+2] = d[2];
1222 pixel[x*4+3] = d[3];
1225 case DPSOFTRAST_BLENDMODE_ADDALPHA:
1226 for (x = startx;x < endx;x++)
1230 a = in4f[x*4+3] * 255.0f;
1231 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1232 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1233 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1234 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1235 pixel[x*4+0] = d[0];
1236 pixel[x*4+1] = d[1];
1237 pixel[x*4+2] = d[2];
1238 pixel[x*4+3] = d[3];
1241 case DPSOFTRAST_BLENDMODE_ADD:
1242 for (x = startx;x < endx;x++)
1246 d[0] = (int)(in4f[x*4+2]*255.0f+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1247 d[1] = (int)(in4f[x*4+1]*255.0f+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1248 d[2] = (int)(in4f[x*4+0]*255.0f+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1249 d[3] = (int)(in4f[x*4+3]*255.0f+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1250 pixel[x*4+0] = d[0];
1251 pixel[x*4+1] = d[1];
1252 pixel[x*4+2] = d[2];
1253 pixel[x*4+3] = d[3];
1256 case DPSOFTRAST_BLENDMODE_INVMOD:
1257 for (x = startx;x < endx;x++)
1261 d[0] = (int)((1.0f-in4f[x*4+2])*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1262 d[1] = (int)((1.0f-in4f[x*4+1])*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1263 d[2] = (int)((1.0f-in4f[x*4+0])*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1264 d[3] = (int)((1.0f-in4f[x*4+3])*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1265 pixel[x*4+0] = d[0];
1266 pixel[x*4+1] = d[1];
1267 pixel[x*4+2] = d[2];
1268 pixel[x*4+3] = d[3];
1271 case DPSOFTRAST_BLENDMODE_MUL:
1272 for (x = startx;x < endx;x++)
1276 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1277 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1278 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1279 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1280 pixel[x*4+0] = d[0];
1281 pixel[x*4+1] = d[1];
1282 pixel[x*4+2] = d[2];
1283 pixel[x*4+3] = d[3];
1286 case DPSOFTRAST_BLENDMODE_MUL2:
1287 for (x = startx;x < endx;x++)
1291 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]*2.0f);if (d[0] > 255) d[0] = 255;
1292 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]*2.0f);if (d[1] > 255) d[1] = 255;
1293 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]*2.0f);if (d[2] > 255) d[2] = 255;
1294 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]*2.0f);if (d[3] > 255) d[3] = 255;
1295 pixel[x*4+0] = d[0];
1296 pixel[x*4+1] = d[1];
1297 pixel[x*4+2] = d[2];
1298 pixel[x*4+3] = d[3];
1301 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1302 for (x = startx;x < endx;x++)
1306 a = in4f[x*4+3] * -255.0f;
1307 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;
1308 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;
1309 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;
1310 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;
1311 pixel[x*4+0] = d[0];
1312 pixel[x*4+1] = d[1];
1313 pixel[x*4+2] = d[2];
1314 pixel[x*4+3] = d[3];
1320 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span *span, float *out4f, int texunitindex, int arrayindex, const float *zf)
1323 int startx = span->startx;
1324 int endx = span->endx;
1343 unsigned char *pixelbase;
1344 unsigned char *pixel[4];
1345 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1346 // if no texture is bound, just fill it with white
1349 for (x = startx;x < endx;x++)
1351 out4f[x*4+0] = 1.0f;
1352 out4f[x*4+1] = 1.0f;
1353 out4f[x*4+2] = 1.0f;
1354 out4f[x*4+3] = 1.0f;
1358 // if this mipmap of the texture is 1 pixel, just fill it with that color
1360 if (mip >= texture->mipmaps)
1361 mip = texture->mipmaps - 1;
1362 if (texture->mipmap[mip][1] == 4)
1364 c[0] = texture->bytes[2] * (1.0f/255.0f);
1365 c[1] = texture->bytes[1] * (1.0f/255.0f);
1366 c[2] = texture->bytes[0] * (1.0f/255.0f);
1367 c[3] = texture->bytes[3] * (1.0f/255.0f);
1368 for (x = startx;x < endx;x++)
1370 out4f[x*4+0] = c[0];
1371 out4f[x*4+1] = c[1];
1372 out4f[x*4+2] = c[2];
1373 out4f[x*4+3] = c[3];
1377 filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1378 data[0] = span->data[0][arrayindex][0];
1379 data[1] = span->data[0][arrayindex][1];
1380 data[2] = span->data[0][arrayindex][2];
1381 data[3] = span->data[0][arrayindex][3];
1382 slope[0] = span->data[1][arrayindex][0];
1383 slope[1] = span->data[1][arrayindex][1];
1384 slope[2] = span->data[1][arrayindex][2];
1385 slope[3] = span->data[1][arrayindex][3];
1386 flags = texture->flags;
1387 pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
1388 tcscale[0] = texture->mipmap[mip][2];
1389 tcscale[1] = texture->mipmap[mip][3];
1390 tciwidth = texture->mipmap[mip][2];
1393 tcimax[0] = texture->mipmap[mip][2]-1;
1394 tcimax[1] = texture->mipmap[mip][3]-1;
1395 tciwrapmask[0] = texture->mipmap[mip][2]-1;
1396 tciwrapmask[1] = texture->mipmap[mip][3]-1;
1399 if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1401 for (x = startx;x < endx;x++)
1404 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0];
1405 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1];
1406 tci[0] = (int)floor(tc[0]);
1407 tci[1] = (int)floor(tc[1]);
1408 tci1[0] = tci[0] + 1;
1409 tci1[1] = tci[1] + 1;
1410 frac[0] = tc[0] - tci[0];ifrac[0] = 1.0f - frac[0];
1411 frac[1] = tc[1] - tci[1];ifrac[1] = 1.0f - frac[1];
1412 lerp[0] = ifrac[0]*ifrac[1];
1413 lerp[1] = frac[0]*ifrac[1];
1414 lerp[2] = ifrac[0]* frac[1];
1415 lerp[3] = frac[0]* frac[1];
1416 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1417 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1418 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1419 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1420 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1421 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1422 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1423 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1424 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);
1425 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);
1426 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);
1427 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);
1428 out4f[x*4+0] = c[0];
1429 out4f[x*4+1] = c[1];
1430 out4f[x*4+2] = c[2];
1431 out4f[x*4+3] = c[3];
1436 for (x = startx;x < endx;x++)
1439 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0];
1440 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1];
1441 tci[0] = (int)floor(tc[0]);
1442 tci[1] = (int)floor(tc[1]);
1443 tci1[0] = tci[0] + 1;
1444 tci1[1] = tci[1] + 1;
1445 frac[0] = tc[0] - tci[0];ifrac[0] = 1.0f - frac[0];
1446 frac[1] = tc[1] - tci[1];ifrac[1] = 1.0f - frac[1];
1447 lerp[0] = ifrac[0]*ifrac[1];
1448 lerp[1] = frac[0]*ifrac[1];
1449 lerp[2] = ifrac[0]* frac[1];
1450 lerp[3] = frac[0]* frac[1];
1451 tci[0] &= tciwrapmask[0];
1452 tci[1] &= tciwrapmask[1];
1453 tci1[0] &= tciwrapmask[0];
1454 tci1[1] &= tciwrapmask[1];
1455 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1456 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1457 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1458 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1459 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);
1460 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);
1461 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);
1462 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);
1463 out4f[x*4+0] = c[0];
1464 out4f[x*4+1] = c[1];
1465 out4f[x*4+2] = c[2];
1466 out4f[x*4+3] = c[3];
1472 if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1474 for (x = startx;x < endx;x++)
1477 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0];
1478 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1];
1479 tci[0] = (int)floor(tc[0]);
1480 tci[1] = (int)floor(tc[1]);
1481 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1482 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1483 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1484 c[0] = pixel[0][2] * (1.0f / 255.0f);
1485 c[1] = pixel[0][1] * (1.0f / 255.0f);
1486 c[2] = pixel[0][0] * (1.0f / 255.0f);
1487 c[3] = pixel[0][3] * (1.0f / 255.0f);
1488 out4f[x*4+0] = c[0];
1489 out4f[x*4+1] = c[1];
1490 out4f[x*4+2] = c[2];
1491 out4f[x*4+3] = c[3];
1496 for (x = startx;x < endx;x++)
1499 tc[0] = (data[0] + slope[0]*x) * z * tcscale[0];
1500 tc[1] = (data[1] + slope[1]*x) * z * tcscale[1];
1501 tci[0] = (int)floor(tc[0]);
1502 tci[1] = (int)floor(tc[1]);
1503 tci[0] &= tciwrapmask[0];
1504 tci[1] &= tciwrapmask[1];
1505 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1506 c[0] = pixel[0][2] * (1.0f / 255.0f);
1507 c[1] = pixel[0][1] * (1.0f / 255.0f);
1508 c[2] = pixel[0][0] * (1.0f / 255.0f);
1509 c[3] = pixel[0][3] * (1.0f / 255.0f);
1510 out4f[x*4+0] = c[0];
1511 out4f[x*4+1] = c[1];
1512 out4f[x*4+2] = c[2];
1513 out4f[x*4+3] = c[3];
1519 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *in4f, int arrayindex, const float *zf)
1522 int startx = span->startx;
1523 int endx = span->endx;
1528 data[0] = span->data[0][arrayindex][0];
1529 data[1] = span->data[0][arrayindex][1];
1530 data[2] = span->data[0][arrayindex][2];
1531 data[3] = span->data[0][arrayindex][3];
1532 slope[0] = span->data[1][arrayindex][0];
1533 slope[1] = span->data[1][arrayindex][1];
1534 slope[2] = span->data[1][arrayindex][2];
1535 slope[3] = span->data[1][arrayindex][3];
1536 for (x = startx;x < endx;x++)
1539 c[0] = (data[0] + slope[0]*x) * z;
1540 c[1] = (data[1] + slope[1]*x) * z;
1541 c[2] = (data[2] + slope[2]*x) * z;
1542 c[3] = (data[3] + slope[3]*x) * z;
1543 out4f[x*4+0] = in4f[x*4+0] * c[0];
1544 out4f[x*4+1] = in4f[x*4+1] * c[1];
1545 out4f[x*4+2] = in4f[x*4+2] * c[2];
1546 out4f[x*4+3] = in4f[x*4+3] * c[3];
1550 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
1552 int x, startx = span->startx, endx = span->endx;
1553 float c[4], localcolor[4];
1554 localcolor[0] = subcolor[0];
1555 localcolor[1] = subcolor[1];
1556 localcolor[2] = subcolor[2];
1557 localcolor[3] = subcolor[3];
1558 for (x = startx;x < endx;x++)
1560 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
1561 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
1562 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
1563 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
1564 out4f[x*4+0] = ina4f[x*4+0] + c[0];
1565 out4f[x*4+1] = ina4f[x*4+1] + c[1];
1566 out4f[x*4+2] = ina4f[x*4+2] + c[2];
1567 out4f[x*4+3] = ina4f[x*4+3] + c[3];
1571 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *in4f, const float *color)
1573 int x, startx = span->startx, endx = span->endx;
1574 float localcolor[4], ilerp, lerp;
1575 localcolor[0] = color[0];
1576 localcolor[1] = color[1];
1577 localcolor[2] = color[2];
1578 localcolor[3] = color[3];
1579 ilerp = 1.0f - localcolor[3];
1580 lerp = localcolor[3];
1581 for (x = startx;x < endx;x++)
1583 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
1584 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
1585 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
1586 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
1590 void DPSOFTRAST_Draw_Span_Lightmap(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *lightmap)
1592 int x, startx = span->startx, endx = span->endx;
1593 float Color_Ambient[4], Color_Diffuse[4];
1594 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1595 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1596 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1597 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1598 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
1599 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
1600 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
1601 Color_Diffuse[3] = 0.0f;
1602 for (x = startx;x < endx;x++)
1604 out4f[x*4+0] = diffuse[x*4+0] * (Color_Ambient[0] + lightmap[x*4+0] * Color_Diffuse[0]);
1605 out4f[x*4+1] = diffuse[x*4+1] * (Color_Ambient[1] + lightmap[x*4+1] * Color_Diffuse[1]);
1606 out4f[x*4+2] = diffuse[x*4+2] * (Color_Ambient[2] + lightmap[x*4+2] * Color_Diffuse[2]);
1607 out4f[x*4+3] = diffuse[x*4+3] * (Color_Ambient[3] + lightmap[x*4+3] * Color_Diffuse[3]);
1611 void DPSOFTRAST_Draw_Span_VertexColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse, const float *zf)
1613 int x, startx = span->startx, endx = span->endx;
1614 float Color_Ambient[4], Color_Diffuse[4];
1619 int arrayindex = DPSOFTRAST_ARRAY_COLOR;
1620 data[0] = span->data[0][arrayindex][0];
1621 data[1] = span->data[0][arrayindex][1];
1622 data[2] = span->data[0][arrayindex][2];
1623 data[3] = span->data[0][arrayindex][3];
1624 slope[0] = span->data[1][arrayindex][0];
1625 slope[1] = span->data[1][arrayindex][1];
1626 slope[2] = span->data[1][arrayindex][2];
1627 slope[3] = span->data[1][arrayindex][3];
1628 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1629 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1630 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1631 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1632 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
1633 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
1634 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
1635 Color_Diffuse[3] = 0.0f;
1636 for (x = startx;x < endx;x++)
1639 c[0] = (data[0] + slope[0]*x) * z;
1640 c[1] = (data[1] + slope[1]*x) * z;
1641 c[2] = (data[2] + slope[2]*x) * z;
1642 c[3] = (data[3] + slope[3]*x) * z;
1643 out4f[x*4+0] = diffuse[x*4+0] * (Color_Ambient[0] + c[0] * Color_Diffuse[0]);
1644 out4f[x*4+1] = diffuse[x*4+1] * (Color_Ambient[1] + c[1] * Color_Diffuse[1]);
1645 out4f[x*4+2] = diffuse[x*4+2] * (Color_Ambient[2] + c[2] * Color_Diffuse[2]);
1646 out4f[x*4+3] = diffuse[x*4+3] * (Color_Ambient[3] + c[3] * Color_Diffuse[3]);
1650 void DPSOFTRAST_Draw_Span_FlatColor(const DPSOFTRAST_State_Draw_Span *span, float *out4f, const float *diffuse)
1652 int x, startx = span->startx, endx = span->endx;
1653 float Color_Ambient[4];
1654 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
1655 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
1656 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
1657 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
1658 for (x = startx;x < endx;x++)
1660 out4f[x*4+0] = diffuse[x*4+0] * Color_Ambient[0];
1661 out4f[x*4+1] = diffuse[x*4+1] * Color_Ambient[1];
1662 out4f[x*4+2] = diffuse[x*4+2] * Color_Ambient[2];
1663 out4f[x*4+3] = diffuse[x*4+3] * Color_Ambient[3];
1667 void DPSOFTRAST_Draw_VertexShader(void)
1669 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);
1670 switch(dpsoftrast.shader_mode)
1672 case SHADERMODE_GENERIC: ///< (particles/HUD/etc) vertex color: optionally multiplied by one texture
1673 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
1674 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1676 case SHADERMODE_POSTPROCESS: ///< postprocessing shader (r_glsl_postprocess)
1677 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1678 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
1680 case SHADERMODE_DEPTH_OR_SHADOW: ///< (depthfirst/shadows) vertex shader only
1682 case SHADERMODE_FLATCOLOR: ///< (lightmap) modulate texture by uniform color (q1bsp: q3bsp)
1683 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1685 case SHADERMODE_VERTEXCOLOR: ///< (lightmap) modulate texture by vertex colors (q3bsp)
1686 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
1687 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1689 case SHADERMODE_LIGHTMAP: ///< (lightmap) modulate texture by lightmap texture (q1bsp: q3bsp)
1690 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
1691 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
1693 case SHADERMODE_FAKELIGHT: ///< (fakelight) modulate texture by "fake" lighting (no lightmaps: no nothing)
1695 case SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE: ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1697 case SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE: ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1699 case SHADERMODE_LIGHTDIRECTION: ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1701 case SHADERMODE_LIGHTSOURCE: ///< (lightsource) use directional pixel shading from light source (rtlight)
1703 case SHADERMODE_REFRACTION: ///< refract background (the material is rendered normally after this pass)
1705 case SHADERMODE_WATER: ///< refract background and reflection (the material is rendered normally after this pass)
1707 case SHADERMODE_SHOWDEPTH: ///< (debugging) renders depth as color
1709 case SHADERMODE_DEFERREDGEOMETRY: ///< (deferred) render material properties to screenspace geometry buffers
1711 case SHADERMODE_DEFERREDLIGHTSOURCE: ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
1713 case SHADERMODE_COUNT:
1718 void DPSOFTRAST_Draw_PixelShaderSpan(const DPSOFTRAST_State_Draw_Span *span)
1720 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
1721 float buffer_texture_color[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1722 float buffer_texture_lightmap[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1723 float buffer_FragColor[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
1724 switch(dpsoftrast.shader_mode)
1726 case SHADERMODE_GENERIC: ///< (particles/HUD/etc) vertex color: optionally multiplied by one texture
1727 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1728 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_FIRST, 2, buffer_z);
1729 DPSOFTRAST_Draw_Span_MultiplyVarying(span, buffer_FragColor, buffer_texture_color, 1, buffer_z);
1730 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1732 case SHADERMODE_POSTPROCESS: ///< postprocessing shader (r_glsl_postprocess)
1733 // TODO: optimize!! at the very least there is no reason to use texture sampling on the frame texture
1734 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1735 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_FragColor, GL20TU_FIRST, 2, buffer_z);
1736 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
1738 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_SECOND, 3, buffer_z);
1739 DPSOFTRAST_Draw_Span_AddBloom(span, buffer_FragColor, buffer_FragColor, buffer_texture_color, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
1741 DPSOFTRAST_Draw_Span_MixUniformColor(span, buffer_FragColor, buffer_FragColor, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
1742 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
1744 // TODO: implement saturation
1746 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
1748 // TODO: implement gammaramps
1750 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1752 case SHADERMODE_DEPTH_OR_SHADOW: ///< (depthfirst/shadows) vertex shader only
1754 case SHADERMODE_FLATCOLOR: ///< (lightmap) modulate texture by uniform color (q1bsp: q3bsp)
1755 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1756 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1757 DPSOFTRAST_Draw_Span_FlatColor(span, buffer_FragColor, buffer_texture_color);
1758 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1760 case SHADERMODE_VERTEXCOLOR: ///< (lightmap) modulate texture by vertex colors (q3bsp)
1761 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1762 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1763 DPSOFTRAST_Draw_Span_VertexColor(span, buffer_FragColor, buffer_texture_color, buffer_z);
1764 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1766 case SHADERMODE_LIGHTMAP: ///< (lightmap) modulate texture by lightmap texture (q1bsp: q3bsp)
1767 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
1768 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_color, GL20TU_COLOR, 2, buffer_z);
1769 DPSOFTRAST_Draw_Span_Texture2DVarying(span, buffer_texture_lightmap, GL20TU_LIGHTMAP, 6, buffer_z);
1770 DPSOFTRAST_Draw_Span_Lightmap(span, buffer_FragColor, buffer_texture_color, buffer_texture_lightmap);
1771 DPSOFTRAST_Draw_Span_Finish(span, buffer_FragColor);
1773 case SHADERMODE_FAKELIGHT: ///< (fakelight) modulate texture by "fake" lighting (no lightmaps: no nothing)
1775 case SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE: ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1777 case SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE: ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1779 case SHADERMODE_LIGHTDIRECTION: ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1781 case SHADERMODE_LIGHTSOURCE: ///< (lightsource) use directional pixel shading from light source (rtlight)
1783 case SHADERMODE_REFRACTION: ///< refract background (the material is rendered normally after this pass)
1785 case SHADERMODE_WATER: ///< refract background and reflection (the material is rendered normally after this pass)
1787 case SHADERMODE_SHOWDEPTH: ///< (debugging) renders depth as color
1789 case SHADERMODE_DEFERREDGEOMETRY: ///< (deferred) render material properties to screenspace geometry buffers
1791 case SHADERMODE_DEFERREDLIGHTSOURCE: ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
1793 case SHADERMODE_COUNT:
1798 void DPSOFTRAST_Draw_ProcessSpans(void)
1804 int numspans = dpsoftrast.draw.numspans;
1806 // unsigned int *colorpixel;
1807 unsigned int *depthpixel;
1813 DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
1814 unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
1815 for (i = 0;i < numspans;i++, span++)
1817 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1818 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1819 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
1821 depth = (int)(w*DPSOFTRAST_DEPTHSCALE);
1822 depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
1823 depthpixel = dpsoftrast.fb_depthpixels + span->start;
1824 switch(dpsoftrast.fb_depthfunc)
1827 case GL_ALWAYS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
1828 case GL_LESS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
1829 case GL_LEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
1830 case GL_EQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
1831 case GL_GEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
1832 case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
1833 case GL_NEVER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
1835 //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
1836 //for (x = 0;x < span->length;x++)
1837 // colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
1838 // if there is no color buffer, skip pixel shader
1840 endx = span->length;
1841 while (startx < endx && !pixelmask[startx])
1843 while (endx > startx && !pixelmask[endx-1])
1846 continue; // no pixels to fill
1847 span->pixelmask = pixelmask;
1848 span->startx = startx;
1850 // run pixel shader if appropriate
1851 // do this before running depthmask code, to allow the pixelshader
1852 // to clear pixelmask values for alpha testing
1853 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
1854 DPSOFTRAST_Draw_PixelShaderSpan(span);
1855 if (dpsoftrast.user.depthmask)
1856 for (x = 0, d = depth;x < span->length;x++, d += depthslope)
1862 // no depth testing means we're just dealing with color...
1863 // if there is no color buffer, skip pixel shader
1864 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
1866 memset(pixelmask, 1, span->length);
1867 span->pixelmask = pixelmask;
1869 span->endx = span->length;
1870 DPSOFTRAST_Draw_PixelShaderSpan(span);
1876 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask)
1878 int cullface = dpsoftrast.user.cullface;
1879 int width = dpsoftrast.fb_width;
1880 int height = dpsoftrast.fb_height;
1913 float trianglearea2;
1914 float triangleedge[2][4];
1915 float trianglenormal[4];
1917 float clipped[DPSOFTRAST_ARRAY_TOTAL][4][4];
1919 float proj[DPSOFTRAST_ARRAY_TOTAL][4][4];
1920 DPSOFTRAST_State_Draw_Span *span;
1921 DPSOFTRAST_State_Draw_Span *oldspan;
1922 for (i = 0;i < numtriangles;i++)
1924 // generate the 3 edges of this triangle
1925 // generate spans for the triangle - switch based on left split or right split classification of triangle
1928 e[0] = element3i[i*3+0] - firstvertex;
1929 e[1] = element3i[i*3+1] - firstvertex;
1930 e[2] = element3i[i*3+2] - firstvertex;
1934 e[0] = element3s[i*3+0] - firstvertex;
1935 e[1] = element3s[i*3+1] - firstvertex;
1936 e[2] = element3s[i*3+2] - firstvertex;
1944 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];
1945 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];
1946 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];
1947 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];
1948 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];
1949 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];
1950 trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
1951 trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
1952 trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
1953 trianglearea2 = trianglenormal[0] * trianglenormal[0] + trianglenormal[1] * trianglenormal[1] + trianglenormal[2] * trianglenormal[2];
1954 // skip degenerate triangles, nothing good can come from them...
1955 if (trianglearea2 == 0.0f)
1957 // apply current cullface mode (this culls many triangles)
1961 if (trianglenormal[2] < 0)
1965 if (trianglenormal[2] > 0)
1969 // calculate distance from nearplane
1970 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + 1.0f;
1971 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + 1.0f;
1972 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + 1.0f;
1974 if (clipdist[0] < 0.0f)
1976 if (clipdist[1] < 0.0f)
1978 if (clipdist[2] < 0.0f)
1980 // clip triangle if necessary
1984 // triangle is entirely in front of nearplane
1986 // macros for clipping vertices
1987 #define CLIPPEDVERTEXLERP(k,p1,p2) \
1988 frac = clipdist[p1] / (clipdist[p1] - clipdist[p2]);\
1989 ifrac = 1.0f - frac;\
1990 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
1994 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+0]*frac;\
1995 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+1]*frac;\
1996 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+2]*frac;\
1997 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+3]*frac;\
2000 DPSOFTRAST_Draw_ProjectVertices(screen[k], clipped[DPSOFTRAST_ARRAY_POSITION][k], 1)
2001 #define CLIPPEDVERTEXCOPY(k,p1) \
2002 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2006 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0];\
2007 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1];\
2008 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2];\
2009 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 // TODO: adjust texture LOD by texture density
2103 // okay, this triangle is going to produce spans, we'd better project
2104 // the interpolants now (this is what gives perspective texturing),
2105 // this consists of simply multiplying all arrays by the W coord
2106 // (which is basically 1/Z), which will be undone per-pixel
2107 // (multiplying by Z again) to get the perspective-correct array
2109 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2113 for (k = 0;k < numpoints;k++)
2116 proj[j][k][0] = clipped[j][k][0] * w;
2117 proj[j][k][1] = clipped[j][k][1] * w;
2118 proj[j][k][2] = clipped[j][k][2] * w;
2119 proj[j][k][3] = clipped[j][k][3] * w;
2123 // iterate potential spans
2124 // TODO: optimize? if we figured out the edge order beforehand, this
2125 // could do loops over the edges in the proper order rather than
2126 // selecting them for each span
2127 // TODO: optimize? the edges could have data slopes calculated
2128 // TODO: optimize? the data slopes could be calculated as a plane
2129 // (2D slopes) to avoid any interpolation along edges at all
2130 for (y = extent[1];y < extent[3];y++)
2132 // get center of pixel y
2134 // do the compares all at once
2135 screenyless[0] = y <= screeny[0];
2136 screenyless[1] = y <= screeny[1];
2137 screenyless[2] = y <= screeny[2];
2138 screenyless[3] = y <= screeny[3];
2141 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4 + screenyless[3] * 8)
2143 case 0: /*0000*/ continue;
2144 case 1: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 0;edge1n = 1;break;
2145 case 2: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
2146 case 3: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 2;break;
2147 case 4: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break;
2148 case 5: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break; // concave - nonsense
2149 case 6: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 3;break;
2150 case 7: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 3;break;
2151 case 8: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 3;edge1n = 0;break;
2152 case 9: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 1;break;
2153 case 10: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
2154 case 11: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break;
2155 case 12: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 0;break;
2156 case 13: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
2157 case 14: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 0;break;
2158 case 15: /*1111*/ continue;
2163 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4)
2165 case 0: /*000*/ continue;
2166 case 1: /*100*/ edge0p = 2;edge0n = 0;edge1p = 0;edge1n = 1;break;
2167 case 2: /*010*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
2168 case 3: /*110*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 2;break;
2169 case 4: /*001*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 0;break;
2170 case 5: /*101*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
2171 case 6: /*011*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 0;break;
2172 case 7: /*111*/ continue;
2182 for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
2184 if (screenyless[k] && !screenyless[j])
2190 else if (screenyless[j] && !screenyless[k])
2197 if (foundedges != 3)
2199 if (cedge0p != edge0p || cedge0n != edge0n || cedge1p != edge1p || cedge1n != edge1n)
2202 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);
2204 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);
2208 edge0ylerp = (yc - screen[edge0p][1]) / (screen[edge0n][1] - screen[edge0p][1]);
2209 edge1ylerp = (yc - screen[edge1p][1]) / (screen[edge1n][1] - screen[edge1p][1]);
2210 if (edge0ylerp < 0 || edge0ylerp > 1 || edge1ylerp < 0 || edge1ylerp > 1)
2212 edge0yilerp = 1.0f - edge0ylerp;
2213 edge1yilerp = 1.0f - edge1ylerp;
2214 edge0xf = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2215 edge1xf = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2216 if (edge0xf < edge1xf)
2226 startx = (int)ceil(startxf);
2227 endx = (int)ceil(endxf);
2234 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); }
2235 spanilength = 1.0f / (endxf - startxf);
2236 startxlerp = startx - startxf;
2237 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
2239 span->start = y * width + startx;
2240 span->length = endx - startx;
2241 j = DPSOFTRAST_ARRAY_TOTAL;
2242 if (edge0xf < edge1xf)
2244 span->data[0][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2245 span->data[0][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
2246 span->data[0][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
2247 span->data[0][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
2248 span->data[1][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2249 span->data[1][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
2250 span->data[1][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
2251 span->data[1][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
2252 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2256 span->data[0][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
2257 span->data[0][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
2258 span->data[0][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
2259 span->data[0][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
2260 span->data[1][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
2261 span->data[1][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
2262 span->data[1][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
2263 span->data[1][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
2269 span->data[0][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
2270 span->data[0][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
2271 span->data[0][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
2272 span->data[0][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
2273 span->data[1][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
2274 span->data[1][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
2275 span->data[1][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
2276 span->data[1][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
2277 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2281 span->data[0][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
2282 span->data[0][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
2283 span->data[0][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
2284 span->data[0][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
2285 span->data[1][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
2286 span->data[1][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
2287 span->data[1][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
2288 span->data[1][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
2292 // change data[1][n][] to be a data slope
2293 j = DPSOFTRAST_ARRAY_TOTAL;
2294 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
2295 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
2296 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
2297 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
2298 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2302 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
2303 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
2304 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
2305 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
2308 // adjust the data[0][n][] to be correct for the pixel centers
2309 // this also handles horizontal clipping where a major part of the
2310 // span may be off the left side of the screen
2311 j = DPSOFTRAST_ARRAY_TOTAL;
2312 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
2313 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
2314 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
2315 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
2316 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2320 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
2321 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
2322 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
2323 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
2326 // to keep the shader routines from needing more than a small
2327 // buffer for pixel intermediate data, we split long spans...
2328 while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
2330 span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
2331 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
2333 DPSOFTRAST_Draw_ProcessSpans();
2334 dpsoftrast.draw.numspans = 0;
2337 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
2339 startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
2340 span->start = y * width + startx;
2341 span->length = endx - startx;
2342 j = DPSOFTRAST_ARRAY_TOTAL;
2343 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2344 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2345 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2346 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2347 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2351 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2352 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2353 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2354 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
2358 // after all that, we have a span suitable for the pixel shader...
2359 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
2361 DPSOFTRAST_Draw_ProcessSpans();
2362 dpsoftrast.draw.numspans = 0;
2365 // draw outlines over triangle for debugging
2366 // for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
2367 // DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
2369 if (dpsoftrast.draw.numspans)
2371 DPSOFTRAST_Draw_ProcessSpans();
2372 dpsoftrast.draw.numspans = 0;
2376 void DPSOFTRAST_Draw_DebugPoints(void)
2381 int numvertices = dpsoftrast.draw.numvertices;
2382 int w = dpsoftrast.fb_width;
2384 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
2386 bounds[0] = dpsoftrast.fb_viewportscissor[0];
2387 bounds[1] = dpsoftrast.fb_viewportscissor[1];
2388 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
2389 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
2390 for (i = 0;i < numvertices;i++)
2393 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
2395 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
2396 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
2397 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
2398 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
2399 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
2400 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
2401 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
2403 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
2404 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
2408 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
2410 unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL];
2411 arraymask[0] = true;
2412 arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode)
2413 arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL;
2414 arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL;
2415 arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL;
2416 arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL;
2417 arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL;
2418 arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL;
2419 arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL;
2420 arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL;
2421 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
2422 DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true, 1);
2423 DPSOFTRAST_Draw_VertexShader();
2424 DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
2425 DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numvertices, numtriangles, element3i, element3s, arraymask);
2428 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
2437 memset(&dpsoftrast, 0, sizeof(dpsoftrast));
2438 dpsoftrast.bigendian = u.b[3];
2439 dpsoftrast.fb_width = width;
2440 dpsoftrast.fb_height = height;
2441 dpsoftrast.fb_depthpixels = depthpixels;
2442 dpsoftrast.fb_colorpixels[0] = colorpixels;
2443 dpsoftrast.fb_colorpixels[1] = NULL;
2444 dpsoftrast.fb_colorpixels[1] = NULL;
2445 dpsoftrast.fb_colorpixels[1] = NULL;
2446 dpsoftrast.texture_firstfree = 1;
2447 dpsoftrast.texture_end = 1;
2448 dpsoftrast.texture_max = 0;
2449 dpsoftrast.user.colormask[0] = 1;
2450 dpsoftrast.user.colormask[1] = 1;
2451 dpsoftrast.user.colormask[2] = 1;
2452 dpsoftrast.user.colormask[3] = 1;
2453 dpsoftrast.user.blendfunc[0] = GL_ONE;
2454 dpsoftrast.user.blendfunc[1] = GL_ZERO;
2455 dpsoftrast.user.depthmask = true;
2456 dpsoftrast.user.depthtest = true;
2457 dpsoftrast.user.depthfunc = GL_LEQUAL;
2458 dpsoftrast.user.scissortest = false;
2459 dpsoftrast.user.cullface = GL_BACK;
2460 dpsoftrast.user.alphatest = false;
2461 dpsoftrast.user.alphafunc = GL_GREATER;
2462 dpsoftrast.user.alphavalue = 0.5f;
2463 dpsoftrast.user.scissor[0] = 0;
2464 dpsoftrast.user.scissor[1] = 0;
2465 dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
2466 dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
2467 dpsoftrast.user.viewport[0] = 0;
2468 dpsoftrast.user.viewport[1] = 0;
2469 dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
2470 dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
2471 dpsoftrast.user.depthrange[0] = 0;
2472 dpsoftrast.user.depthrange[1] = 1;
2473 dpsoftrast.user.polygonoffset[0] = 0;
2474 dpsoftrast.user.polygonoffset[1] = 0;
2475 dpsoftrast.user.color[0] = 1;
2476 dpsoftrast.user.color[1] = 1;
2477 dpsoftrast.user.color[2] = 1;
2478 dpsoftrast.user.color[3] = 1;
2479 dpsoftrast.validate = -1;
2480 DPSOFTRAST_Validate(-1);
2481 dpsoftrast.validate = 0;
2484 void DPSOFTRAST_Shutdown(void)
2487 for (i = 0;i < dpsoftrast.texture_end;i++)
2488 if (dpsoftrast.texture[i].bytes)
2489 free(dpsoftrast.texture[i].bytes);
2490 if (dpsoftrast.texture)
2491 free(dpsoftrast.texture);
2492 memset(&dpsoftrast, 0, sizeof(dpsoftrast));