3 #include "dpsoftrast.h"
10 typedef enum bool {false, true} bool;
13 #if defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1400)
14 #define RESTRICT __restrict
20 #define GL_FRONT_LEFT 0x0400
21 #define GL_FRONT_RIGHT 0x0401
22 #define GL_BACK_LEFT 0x0402
23 #define GL_BACK_RIGHT 0x0403
24 #define GL_FRONT 0x0404
25 #define GL_BACK 0x0405
26 #define GL_LEFT 0x0406
27 #define GL_RIGHT 0x0407
28 #define GL_FRONT_AND_BACK 0x0408
29 #define GL_AUX0 0x0409
30 #define GL_AUX1 0x040A
31 #define GL_AUX2 0x040B
32 #define GL_AUX3 0x040C
34 #define GL_NEVER 0x0200
35 #define GL_LESS 0x0201
36 #define GL_EQUAL 0x0202
37 #define GL_LEQUAL 0x0203
38 #define GL_GREATER 0x0204
39 #define GL_NOTEQUAL 0x0205
40 #define GL_GEQUAL 0x0206
41 #define GL_ALWAYS 0x0207
45 #define GL_SRC_COLOR 0x0300
46 #define GL_ONE_MINUS_SRC_COLOR 0x0301
47 #define GL_DST_COLOR 0x0306
48 #define GL_ONE_MINUS_DST_COLOR 0x0307
49 #define GL_SRC_ALPHA 0x0302
50 #define GL_ONE_MINUS_SRC_ALPHA 0x0303
51 #define GL_DST_ALPHA 0x0304
52 #define GL_ONE_MINUS_DST_ALPHA 0x0305
53 #define GL_SRC_ALPHA_SATURATE 0x0308
54 #define GL_CONSTANT_COLOR 0x8001
55 #define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
56 #define GL_CONSTANT_ALPHA 0x8003
57 #define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
59 typedef enum DPSOFTRAST_ARRAY_e
61 DPSOFTRAST_ARRAY_POSITION,
62 DPSOFTRAST_ARRAY_COLOR,
63 DPSOFTRAST_ARRAY_TEXCOORD0,
64 DPSOFTRAST_ARRAY_TEXCOORD1,
65 DPSOFTRAST_ARRAY_TEXCOORD2,
66 DPSOFTRAST_ARRAY_TEXCOORD3,
67 DPSOFTRAST_ARRAY_TEXCOORD4,
68 DPSOFTRAST_ARRAY_TEXCOORD5,
69 DPSOFTRAST_ARRAY_TEXCOORD6,
70 DPSOFTRAST_ARRAY_TEXCOORD7,
71 DPSOFTRAST_ARRAY_TOTAL
75 typedef struct DPSOFTRAST_Texture_s
82 DPSOFTRAST_TEXTURE_FILTER filter;
86 int mipmap[DPSOFTRAST_MAXMIPMAPS][5];
90 typedef struct DPSOFTRAST_State_User_s
106 float polygonoffset[2];
109 DPSOFTRAST_State_User;
111 #define DPSOFTRAST_MAXSUBSPAN 16
113 typedef struct DPSOFTRAST_State_Draw_Span_s
115 int start; // pixel index
116 int length; // pixel count
117 int startx; // usable range (according to pixelmask)
118 int endx; // usable range (according to pixelmask)
119 unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; // texcoord to screen space density values (for picking mipmap of textures)
120 unsigned char *pixelmask; // true for pixels that passed depth test, false for others
121 // [0][n][] is start interpolant values (projected)
122 // [1][n][] is end interpolant values (projected)
123 // [0][DPSOFTRAST_ARRAY_TOTAL][] is start screencoord4f
124 // [1][DPSOFTRAST_ARRAY_TOTAL][] is end screencoord4f
125 // NOTE: screencoord4f[3] is W (basically 1/Z), useful for depthbuffer
126 float data[2][DPSOFTRAST_ARRAY_TOTAL+1][4];
128 DPSOFTRAST_State_Draw_Span;
130 #define DPSOFTRAST_DRAW_MAXSPANQUEUE 1024
132 typedef struct DPSOFTRAST_State_Draw_s
136 float *in_array4f[DPSOFTRAST_ARRAY_TOTAL];
137 float *post_array4f[DPSOFTRAST_ARRAY_TOTAL];
138 float *screencoord4f;
140 // spans are queued in this structure for dispatch to the pixel shader,
141 // partly to improve cache locality, partly for batching purposes, spans
142 // are flushed before DrawTriangles returns to caller
144 DPSOFTRAST_State_Draw_Span spanqueue[DPSOFTRAST_DRAW_MAXSPANQUEUE];
146 DPSOFTRAST_State_Draw;
148 #define DPSOFTRAST_VALIDATE_FB 1
149 #define DPSOFTRAST_VALIDATE_DEPTHFUNC 2
150 #define DPSOFTRAST_VALIDATE_BLENDFUNC 4
151 #define DPSOFTRAST_VALIDATE_DRAW (DPSOFTRAST_VALIDATE_FB | DPSOFTRAST_VALIDATE_DEPTHFUNC | DPSOFTRAST_VALIDATE_BLENDFUNC)
153 typedef enum DPSOFTRAST_BLENDMODE_e
155 DPSOFTRAST_BLENDMODE_OPAQUE,
156 DPSOFTRAST_BLENDMODE_ALPHA,
157 DPSOFTRAST_BLENDMODE_ADDALPHA,
158 DPSOFTRAST_BLENDMODE_ADD,
159 DPSOFTRAST_BLENDMODE_INVMOD,
160 DPSOFTRAST_BLENDMODE_MUL,
161 DPSOFTRAST_BLENDMODE_MUL2,
162 DPSOFTRAST_BLENDMODE_SUBALPHA,
163 DPSOFTRAST_BLENDMODE_PSEUDOALPHA,
164 DPSOFTRAST_BLENDMODE_TOTAL
166 DPSOFTRAST_BLENDMODE;
168 typedef struct DPSOFTRAST_State_s
170 // DPSOFTRAST_VALIDATE_ flags
176 unsigned int *fb_depthpixels;
177 unsigned int *fb_colorpixels[4];
179 const float *pointer_vertex3f;
180 const float *pointer_color4f;
181 const unsigned char *pointer_color4ub;
182 const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
185 int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
186 int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
187 DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
190 int shader_permutation;
191 float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4];
192 int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
194 // derived values (DPSOFTRAST_VALIDATE_FB)
195 int fb_clearscissor[4];
197 int fb_viewportscissor[4];
198 float fb_viewportcenter[2];
199 float fb_viewportscale[2];
201 // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
204 // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
209 int texture_firstfree;
210 DPSOFTRAST_Texture *texture;
215 const char *errorstring;
217 DPSOFTRAST_State_User user;
219 DPSOFTRAST_State_Draw draw;
223 DPSOFTRAST_State dpsoftrast;
225 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
226 #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))
227 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
228 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
230 void DPSOFTRAST_RecalcFB(void)
232 // calculate framebuffer scissor, viewport, viewport clipped by scissor,
233 // and viewport projection values
234 int x1, x2, x3, x4, x5, x6;
235 int y1, y2, y3, y4, y5, y6;
236 x1 = dpsoftrast.user.scissor[0];
237 x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
238 x3 = dpsoftrast.user.viewport[0];
239 x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
240 y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
241 y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
242 y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
243 y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
244 if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
246 if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
248 if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
250 if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
252 if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
253 x5 = x1;if (x5 < x3) x5 = x3;
254 x6 = x2;if (x6 > x4) x4 = x4;
255 y5 = y1;if (y5 < y3) y5 = y3;
256 y6 = y2;if (y6 > y4) y6 = y4;
257 dpsoftrast.fb_clearscissor[0] = x1;
258 dpsoftrast.fb_clearscissor[1] = y1;
259 dpsoftrast.fb_clearscissor[2] = x2 - x1;
260 dpsoftrast.fb_clearscissor[3] = y2 - y1;
261 dpsoftrast.fb_viewport[0] = x3;
262 dpsoftrast.fb_viewport[1] = y3;
263 dpsoftrast.fb_viewport[2] = x4 - x3;
264 dpsoftrast.fb_viewport[3] = y4 - y3;
265 dpsoftrast.fb_viewportscissor[0] = x5;
266 dpsoftrast.fb_viewportscissor[1] = y5;
267 dpsoftrast.fb_viewportscissor[2] = x6 - x5;
268 dpsoftrast.fb_viewportscissor[3] = y6 - y5;
269 dpsoftrast.fb_viewportcenter[0] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
270 dpsoftrast.fb_viewportcenter[1] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
271 dpsoftrast.fb_viewportscale[0] = 0.5f * dpsoftrast.user.viewport[2];
272 dpsoftrast.fb_viewportscale[1] = -0.5f * dpsoftrast.user.viewport[3];
275 void DPSOFTRAST_RecalcDepthFunc(void)
277 dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
280 int blendmodetable[][4] =
282 {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
283 {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
284 {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
285 {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
286 {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
287 {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
288 {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
289 {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
290 {DPSOFTRAST_BLENDMODE_PSEUDOALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, false},
291 {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
294 void DPSOFTRAST_RecalcBlendFunc(void)
297 dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE;
298 for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++)
300 if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3])
302 dpsoftrast.fb_blendmode = blendmodetable[i][0];
308 #define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0)
310 void DPSOFTRAST_Validate(int mask)
312 mask &= dpsoftrast.validate;
315 if (mask & DPSOFTRAST_VALIDATE_FB)
317 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB;
318 DPSOFTRAST_RecalcFB();
320 if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
322 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
323 DPSOFTRAST_RecalcDepthFunc();
325 if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
327 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
328 DPSOFTRAST_RecalcBlendFunc();
332 DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
334 if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
335 return &dpsoftrast.texture[index];
339 int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
348 int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
349 int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
350 DPSOFTRAST_Texture *texture;
351 if (width*height*depth < 1)
353 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
356 if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
358 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
363 case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
364 case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
365 case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
367 case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
368 if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
370 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
375 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
378 if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
380 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
385 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
387 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
390 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
392 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
395 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
397 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
400 if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
402 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
405 if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
407 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
410 // find first empty slot in texture array
411 for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
412 if (!dpsoftrast.texture[texnum].bytes)
414 dpsoftrast.texture_firstfree = texnum + 1;
415 if (dpsoftrast.texture_max <= texnum)
417 // expand texture array as needed
418 if (dpsoftrast.texture_max < 1024)
419 dpsoftrast.texture_max = 1024;
421 dpsoftrast.texture_max *= 2;
422 dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
424 if (dpsoftrast.texture_end <= texnum)
425 dpsoftrast.texture_end = texnum + 1;
426 texture = &dpsoftrast.texture[texnum];
427 memset(texture, 0, sizeof(*texture));
428 texture->flags = flags;
429 texture->width = width;
430 texture->height = height;
431 texture->depth = depth;
432 texture->sides = sides;
443 s = w * h * d * sides * 4;
444 texture->mipmap[mipmaps][0] = size;
445 texture->mipmap[mipmaps][1] = s;
446 texture->mipmap[mipmaps][2] = w;
447 texture->mipmap[mipmaps][3] = h;
448 texture->mipmap[mipmaps][4] = d;
451 if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
457 texture->mipmaps = mipmaps;
458 texture->size = size;
460 // allocate the pixels now
461 texture->bytes = (unsigned char *)calloc(1, size);
465 void DPSOFTRAST_Texture_Free(int index)
467 DPSOFTRAST_Texture *texture;
468 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
470 free(texture->bytes);
471 texture->bytes = NULL;
472 memset(texture, 0, sizeof(*texture));
473 // adjust the free range and used range
474 if (dpsoftrast.texture_firstfree > index)
475 dpsoftrast.texture_firstfree = index;
476 while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
477 dpsoftrast.texture_end--;
479 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
481 int i, x, y, z, w, layer0, layer1, row0, row1;
482 unsigned char *o, *i0, *i1, *i2, *i3;
483 DPSOFTRAST_Texture *texture;
484 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
485 if (texture->mipmaps <= 1)
487 for (i = 1;i < texture->mipmaps;i++)
489 for (z = 0;z < texture->mipmap[i][4];z++)
493 if (layer1 >= texture->mipmap[i-1][4])
494 layer1 = texture->mipmap[i-1][4]-1;
495 for (y = 0;y < texture->mipmap[i][3];y++)
499 if (row1 >= texture->mipmap[i-1][3])
500 row1 = texture->mipmap[i-1][3]-1;
501 o = texture->bytes + texture->mipmap[i ][0] + 4*((texture->mipmap[i ][3] * z + y ) * texture->mipmap[i ][2]);
502 i0 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row0) * texture->mipmap[i-1][2]);
503 i1 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row1) * texture->mipmap[i-1][2]);
504 i2 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row0) * texture->mipmap[i-1][2]);
505 i3 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row1) * texture->mipmap[i-1][2]);
506 w = texture->mipmap[i][2];
509 if (texture->mipmap[i-1][2] > 1)
511 // average 3D texture
512 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8, i2 += 8, i3 += 8)
514 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
515 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
516 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
517 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
522 // average 3D mipmap with parent width == 1
523 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
525 o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
526 o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
527 o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
528 o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
534 if (texture->mipmap[i-1][2] > 1)
536 // average 2D texture (common case)
537 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
539 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
540 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
541 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
542 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
547 // 2D texture with parent width == 1
548 o[0] = (i0[0] + i1[0] + 1) >> 1;
549 o[1] = (i0[1] + i1[1] + 1) >> 1;
550 o[2] = (i0[2] + i1[2] + 1) >> 1;
551 o[3] = (i0[3] + i1[3] + 1) >> 1;
558 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
560 DPSOFTRAST_Texture *texture;
561 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
565 dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
567 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
569 DPSOFTRAST_Texture *texture;
570 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
572 memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
573 DPSOFTRAST_Texture_CalculateMipmaps(index);
575 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
577 DPSOFTRAST_Texture *texture;
578 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
579 return texture->width;
581 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
583 DPSOFTRAST_Texture *texture;
584 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
585 return texture->height;
587 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
589 DPSOFTRAST_Texture *texture;
590 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
591 return texture->depth;
593 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
595 DPSOFTRAST_Texture *texture;
596 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
597 return texture->bytes;
599 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
601 DPSOFTRAST_Texture *texture;
602 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
603 if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
605 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
608 texture->filter = filter;
611 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
613 dpsoftrast.fb_width = width;
614 dpsoftrast.fb_height = height;
615 dpsoftrast.fb_depthpixels = depthpixels;
616 dpsoftrast.fb_colorpixels[0] = colorpixels0;
617 dpsoftrast.fb_colorpixels[1] = colorpixels1;
618 dpsoftrast.fb_colorpixels[2] = colorpixels2;
619 dpsoftrast.fb_colorpixels[3] = colorpixels3;
621 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
623 dpsoftrast.user.viewport[0] = x;
624 dpsoftrast.user.viewport[1] = y;
625 dpsoftrast.user.viewport[2] = width;
626 dpsoftrast.user.viewport[3] = height;
627 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
629 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
631 int i, x1, y1, x2, y2, w, h, x, y;
634 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
635 x1 = dpsoftrast.fb_clearscissor[0];
636 y1 = dpsoftrast.fb_clearscissor[1];
637 x2 = dpsoftrast.fb_clearscissor[2];
638 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
643 // FIXME: honor dpsoftrast.fb_colormask?
644 c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
645 for (i = 0;i < 4;i++)
647 if (!dpsoftrast.fb_colorpixels[i])
649 for (y = y1;y < y2;y++)
651 p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
652 for (x = x1;x < x2;x++)
657 void DPSOFTRAST_ClearDepth(float d)
659 int x1, y1, x2, y2, w, h, x, y;
662 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
663 x1 = dpsoftrast.fb_clearscissor[0];
664 y1 = dpsoftrast.fb_clearscissor[1];
665 x2 = dpsoftrast.fb_clearscissor[2];
666 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
671 c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
672 for (y = y1;y < y2;y++)
674 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
675 for (x = x1;x < x2;x++)
679 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
681 dpsoftrast.user.colormask[0] = r != 0;
682 dpsoftrast.user.colormask[1] = g != 0;
683 dpsoftrast.user.colormask[2] = b != 0;
684 dpsoftrast.user.colormask[3] = a != 0;
685 dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
687 void DPSOFTRAST_DepthTest(int enable)
689 dpsoftrast.user.depthtest = enable;
690 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
692 void DPSOFTRAST_ScissorTest(int enable)
694 dpsoftrast.user.scissortest = enable;
695 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
697 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
699 dpsoftrast.user.scissor[0] = x;
700 dpsoftrast.user.scissor[1] = y;
701 dpsoftrast.user.scissor[2] = width;
702 dpsoftrast.user.scissor[3] = height;
703 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
706 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
709 dpsoftrast.user.blendfunc[0] = smodulate;
710 dpsoftrast.user.blendfunc[1] = dmodulate;
711 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
713 void DPSOFTRAST_BlendSubtract(int enable)
715 dpsoftrast.user.blendsubtract = enable != 0;
716 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
718 void DPSOFTRAST_DepthMask(int enable)
720 dpsoftrast.user.depthmask = enable;
722 void DPSOFTRAST_DepthFunc(int comparemode)
725 dpsoftrast.user.depthfunc = comparemode;
727 void DPSOFTRAST_DepthRange(float range0, float range1)
729 dpsoftrast.user.depthrange[0] = range0;
730 dpsoftrast.user.depthrange[1] = range1;
732 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
734 dpsoftrast.user.polygonoffset[0] = alongnormal;
735 dpsoftrast.user.polygonoffset[1] = intoview;
737 void DPSOFTRAST_CullFace(int mode)
740 dpsoftrast.user.cullface = mode;
742 void DPSOFTRAST_AlphaTest(float enable)
744 dpsoftrast.user.alphatest = enable;
746 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
749 dpsoftrast.user.alphafunc = alphafunc;
750 dpsoftrast.user.alphavalue = alphavalue;
752 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
754 dpsoftrast.user.color[0] = r;
755 dpsoftrast.user.color[1] = g;
756 dpsoftrast.user.color[2] = b;
757 dpsoftrast.user.color[3] = a;
759 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
761 int outstride = blockwidth * 4;
762 int instride = dpsoftrast.fb_width * 4;
765 int bx2 = blockx + blockwidth;
766 int by2 = blocky + blockheight;
771 unsigned char *inpixels;
774 if (bx1 < 0) bx1 = 0;
775 if (by1 < 0) by1 = 0;
776 if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
777 if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
780 inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
781 if (dpsoftrast.bigendian)
783 for (y = by1;y < by2;y++)
785 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
786 o = (unsigned char *)outpixels + (y - by1) * outstride;
787 for (x = bx1;x < bx2;x++)
800 for (y = by1;y < by2;y++)
802 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
803 o = (unsigned char *)outpixels + (y - by1) * outstride;
809 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
813 int tx2 = tx + width;
814 int ty2 = ty + height;
817 int sx2 = sx + width;
818 int sy2 = sy + height;
828 unsigned int *spixels;
829 unsigned int *tpixels;
830 DPSOFTRAST_Texture *texture;
831 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
832 if (mip < 0 || mip >= texture->mipmaps) return;
833 spixels = dpsoftrast.fb_colorpixels[0];
834 swidth = dpsoftrast.fb_width;
835 sheight = dpsoftrast.fb_height;
836 tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
837 twidth = texture->mipmap[mip][2];
838 theight = texture->mipmap[mip][3];
839 if (tx1 < 0) tx1 = 0;
840 if (ty1 < 0) ty1 = 0;
841 if (tx2 > twidth) tx2 = twidth;
842 if (ty2 > theight) ty2 = theight;
843 if (sx1 < 0) sx1 = 0;
844 if (sy1 < 0) sy1 = 0;
845 if (sx2 > swidth) sx2 = swidth;
846 if (sy2 > sheight) sy2 = sheight;
851 if (tw > sw) tw = sw;
852 if (th > sh) th = sh;
853 if (tw < 1 || th < 1)
855 for (y = 0;y < th;y++)
856 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
857 if (texture->mipmaps > 1)
858 DPSOFTRAST_Texture_CalculateMipmaps(index);
860 void DPSOFTRAST_SetTexture(int unitnum, int index)
862 DPSOFTRAST_Texture *texture;
863 if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
865 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
868 texture = DPSOFTRAST_Texture_GetByIndex(index);
869 if (index && !texture)
871 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
874 dpsoftrast.texbound[unitnum] = texture;
877 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
879 dpsoftrast.pointer_vertex3f = vertex3f;
880 dpsoftrast.stride_vertex = stride;
882 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
884 dpsoftrast.pointer_color4f = color4f;
885 dpsoftrast.pointer_color4ub = NULL;
886 dpsoftrast.stride_color = stride;
888 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
890 dpsoftrast.pointer_color4f = NULL;
891 dpsoftrast.pointer_color4ub = color4ub;
892 dpsoftrast.stride_color = stride;
894 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
896 dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
897 dpsoftrast.components_texcoord[unitnum] = numcomponents;
898 dpsoftrast.stride_texcoord[unitnum] = stride;
901 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
903 dpsoftrast.shader_mode = mode;
904 dpsoftrast.shader_permutation = permutation;
906 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
908 dpsoftrast.uniform4f[index*4+0] = v0;
909 dpsoftrast.uniform4f[index*4+1] = v1;
910 dpsoftrast.uniform4f[index*4+2] = v2;
911 dpsoftrast.uniform4f[index*4+3] = v3;
913 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, 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];
920 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v)
922 dpsoftrast.uniform4f[index*4+0] = v[0];
923 dpsoftrast.uniform4f[index*4+1] = v[1];
924 dpsoftrast.uniform4f[index*4+2] = v[2];
925 dpsoftrast.uniform4f[index*4+3] = v[3];
926 dpsoftrast.uniform4f[index*4+4] = v[4];
927 dpsoftrast.uniform4f[index*4+5] = v[5];
928 dpsoftrast.uniform4f[index*4+6] = v[6];
929 dpsoftrast.uniform4f[index*4+7] = v[7];
930 dpsoftrast.uniform4f[index*4+8] = v[8];
931 dpsoftrast.uniform4f[index*4+9] = v[9];
932 dpsoftrast.uniform4f[index*4+10] = v[10];
933 dpsoftrast.uniform4f[index*4+11] = v[11];
934 dpsoftrast.uniform4f[index*4+12] = v[12];
935 dpsoftrast.uniform4f[index*4+13] = v[13];
936 dpsoftrast.uniform4f[index*4+14] = v[14];
937 dpsoftrast.uniform4f[index*4+15] = v[15];
939 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
941 dpsoftrast.uniform1i[index] = i0;
944 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors)
952 const unsigned char *b;
953 dpsoftrast.draw.numvertices = numvertices;
954 if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
956 if (dpsoftrast.draw.maxvertices < 4096)
957 dpsoftrast.draw.maxvertices = 4096;
958 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
959 dpsoftrast.draw.maxvertices *= 2;
960 if (dpsoftrast.draw.in_array4f[0])
961 free(dpsoftrast.draw.in_array4f[0]);
962 data = (float *)calloc(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
963 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
964 dpsoftrast.draw.in_array4f[i] = data;
965 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
966 dpsoftrast.draw.post_array4f[i] = data;
967 dpsoftrast.draw.screencoord4f = data;
968 data += dpsoftrast.draw.maxvertices * 4;
970 stride = dpsoftrast.stride_vertex;
971 v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
972 p = dpsoftrast.draw.in_array4f[0];
973 for (i = 0;i < numvertices;i++)
980 v = (const float *)((const unsigned char *)v + stride);
984 if (dpsoftrast.pointer_color4f)
986 stride = dpsoftrast.stride_color;
987 v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
988 p = dpsoftrast.draw.in_array4f[1];
989 for (i = 0;i < numvertices;i++)
996 v = (const float *)((const unsigned char *)v + stride);
999 else if (dpsoftrast.pointer_color4ub)
1001 stride = dpsoftrast.stride_color;
1002 b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
1003 p = dpsoftrast.draw.in_array4f[1];
1004 for (i = 0;i < numvertices;i++)
1006 p[0] = b[0] * (1.0f / 255.0f);
1007 p[1] = b[1] * (1.0f / 255.0f);
1008 p[2] = b[2] * (1.0f / 255.0f);
1009 p[3] = b[3] * (1.0f / 255.0f);
1011 b = (const unsigned char *)((const unsigned char *)b + stride);
1016 v = dpsoftrast.user.color;
1017 p = dpsoftrast.draw.in_array4f[1];
1018 for (i = 0;i < numvertices;i++)
1028 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL-2;j++)
1030 if (dpsoftrast.pointer_texcoordf[j])
1032 stride = dpsoftrast.stride_texcoord[j];
1033 v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1034 p = dpsoftrast.draw.in_array4f[j+2];
1035 switch(dpsoftrast.components_texcoord[j])
1038 for (i = 0;i < numvertices;i++)
1045 v = (const float *)((const unsigned char *)v + stride);
1049 for (i = 0;i < numvertices;i++)
1056 v = (const float *)((const unsigned char *)v + stride);
1060 for (i = 0;i < numvertices;i++)
1067 v = (const float *)((const unsigned char *)v + stride);
1075 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1077 static const float identitymatrix[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
1081 memcpy(matrix, inmatrix16f, sizeof(float[16]));
1082 if (!memcmp(identitymatrix, matrix, sizeof(float[16])))
1084 // fast case for identity matrix
1085 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1088 for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1090 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1091 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1092 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1093 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1097 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1099 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1102 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1104 // NOTE: this is used both as a whole mesh transform function and a
1105 // per-triangle transform function (for clipped triangles), accordingly
1106 // it should not crash on divide by 0 but the result of divide by 0 is
1111 float viewportcenter[4];
1112 float viewportscale[4];
1113 viewportscale[0] = dpsoftrast.fb_viewportscale[0];
1114 viewportscale[1] = dpsoftrast.fb_viewportscale[1];
1115 viewportscale[2] = 0.5f;
1116 viewportscale[3] = 0.0f;
1117 viewportcenter[0] = dpsoftrast.fb_viewportcenter[0];
1118 viewportcenter[1] = dpsoftrast.fb_viewportcenter[1];
1119 viewportcenter[2] = 0.5f;
1120 viewportcenter[3] = 0.0f;
1121 for (i = 0;i < numitems;i++)
1132 out4f[0] = viewportcenter[0] + viewportscale[0] * in4f[0] * w;
1133 out4f[1] = viewportcenter[1] + viewportscale[1] * in4f[1] * w;
1134 out4f[2] = viewportcenter[2] + viewportscale[2] * in4f[2] * w;
1135 out4f[3] = viewportcenter[3] + viewportscale[3] * in4f[3] * w;
1142 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1147 int w = dpsoftrast.fb_width;
1150 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1152 bounds[0] = dpsoftrast.fb_viewportscissor[0];
1153 bounds[1] = dpsoftrast.fb_viewportscissor[1];
1154 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1155 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1160 for (i = 0;i <= 128;i++)
1163 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1165 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1166 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1167 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1169 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1170 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1171 pixels[y*w+x] = 0xFFFFFFFF;
1175 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *zf)
1178 int startx = span->startx;
1179 int endx = span->endx;
1180 float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1181 float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1182 for (x = startx;x < endx;)
1184 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1185 float z = 1.0f / (w + wslope * x), dz;
1189 dz = endsub > x ? (1.0f / (w + wslope * endsub) - z) / (endsub - x) : 0.0f;
1193 dz = (1.0f / (w + wslope * endsub) - z) * (1.0f / (DPSOFTRAST_MAXSUBSPAN-1));
1195 for (; x <= endsub; x++, z += dz)
1200 void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const float * RESTRICT in4f)
1203 int startx = span->startx;
1204 int endx = span->endx;
1207 unsigned char * RESTRICT pixelmask = span->pixelmask;
1208 unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1211 pixel += span->start * 4;
1212 // handle alphatest now (this affects depth writes too)
1213 if (dpsoftrast.user.alphatest)
1214 for (x = startx;x < endx;x++)
1215 if (in4f[x*4+3] < 0.5f)
1216 pixelmask[x] = false;
1217 // FIXME: this does not handle bigendian
1218 switch(dpsoftrast.fb_blendmode)
1220 case DPSOFTRAST_BLENDMODE_OPAQUE:
1221 for (x = startx;x < endx;x++)
1225 d[0] = (int)(in4f[x*4+2]*255.0f);if (d[0] > 255) d[0] = 255;
1226 d[1] = (int)(in4f[x*4+1]*255.0f);if (d[1] > 255) d[1] = 255;
1227 d[2] = (int)(in4f[x*4+0]*255.0f);if (d[2] > 255) d[2] = 255;
1228 d[3] = (int)(in4f[x*4+3]*255.0f);if (d[3] > 255) d[3] = 255;
1229 pixel[x*4+0] = d[0];
1230 pixel[x*4+1] = d[1];
1231 pixel[x*4+2] = d[2];
1232 pixel[x*4+3] = d[3];
1235 case DPSOFTRAST_BLENDMODE_ALPHA:
1236 for (x = startx;x < endx;x++)
1240 a = in4f[x*4+3] * 255.0f;
1241 b = 1.0f - in4f[x*4+3];
1242 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1243 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1244 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1245 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);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_ADDALPHA:
1253 for (x = startx;x < endx;x++)
1257 a = in4f[x*4+3] * 255.0f;
1258 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1259 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1260 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1261 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1262 pixel[x*4+0] = d[0];
1263 pixel[x*4+1] = d[1];
1264 pixel[x*4+2] = d[2];
1265 pixel[x*4+3] = d[3];
1268 case DPSOFTRAST_BLENDMODE_ADD:
1269 for (x = startx;x < endx;x++)
1273 d[0] = (int)(in4f[x*4+2]*255.0f+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1274 d[1] = (int)(in4f[x*4+1]*255.0f+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1275 d[2] = (int)(in4f[x*4+0]*255.0f+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1276 d[3] = (int)(in4f[x*4+3]*255.0f+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1277 pixel[x*4+0] = d[0];
1278 pixel[x*4+1] = d[1];
1279 pixel[x*4+2] = d[2];
1280 pixel[x*4+3] = d[3];
1283 case DPSOFTRAST_BLENDMODE_INVMOD:
1284 for (x = startx;x < endx;x++)
1288 d[0] = (int)((1.0f-in4f[x*4+2])*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1289 d[1] = (int)((1.0f-in4f[x*4+1])*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1290 d[2] = (int)((1.0f-in4f[x*4+0])*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1291 d[3] = (int)((1.0f-in4f[x*4+3])*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1292 pixel[x*4+0] = d[0];
1293 pixel[x*4+1] = d[1];
1294 pixel[x*4+2] = d[2];
1295 pixel[x*4+3] = d[3];
1298 case DPSOFTRAST_BLENDMODE_MUL:
1299 for (x = startx;x < endx;x++)
1303 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1304 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1305 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1306 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1307 pixel[x*4+0] = d[0];
1308 pixel[x*4+1] = d[1];
1309 pixel[x*4+2] = d[2];
1310 pixel[x*4+3] = d[3];
1313 case DPSOFTRAST_BLENDMODE_MUL2:
1314 for (x = startx;x < endx;x++)
1318 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]*2.0f);if (d[0] > 255) d[0] = 255;
1319 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]*2.0f);if (d[1] > 255) d[1] = 255;
1320 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]*2.0f);if (d[2] > 255) d[2] = 255;
1321 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]*2.0f);if (d[3] > 255) d[3] = 255;
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];
1328 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1329 for (x = startx;x < endx;x++)
1333 a = in4f[x*4+3] * -255.0f;
1334 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;
1335 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;
1336 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;
1337 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;
1338 pixel[x*4+0] = d[0];
1339 pixel[x*4+1] = d[1];
1340 pixel[x*4+2] = d[2];
1341 pixel[x*4+3] = d[3];
1344 case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1345 for (x = startx;x < endx;x++)
1350 b = 1.0f - in4f[x*4+3];
1351 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1352 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1353 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1354 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1355 pixel[x*4+0] = d[0];
1356 pixel[x*4+1] = d[1];
1357 pixel[x*4+2] = d[2];
1358 pixel[x*4+3] = d[3];
1364 void DPSOFTRAST_Draw_Span_FinishBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const unsigned char* RESTRICT in4ub)
1367 int startx = span->startx;
1368 int endx = span->endx;
1370 const unsigned int * RESTRICT ini = (const unsigned int *)in4ub;
1372 unsigned char * RESTRICT pixelmask = span->pixelmask;
1373 unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1374 unsigned int * RESTRICT pixeli = (unsigned int *)dpsoftrast.fb_colorpixels[0];
1377 pixel += span->start * 4;
1378 pixeli += span->start;
1379 // handle alphatest now (this affects depth writes too)
1380 if (dpsoftrast.user.alphatest)
1381 for (x = startx;x < endx;x++)
1382 if (in4ub[x*4+3] < 0.5f)
1383 pixelmask[x] = false;
1384 // FIXME: this does not handle bigendian
1385 switch(dpsoftrast.fb_blendmode)
1387 case DPSOFTRAST_BLENDMODE_OPAQUE:
1388 for (x = startx;x < endx;x++)
1392 case DPSOFTRAST_BLENDMODE_ALPHA:
1393 for (x = startx;x < endx;x++)
1398 b = 256 - in4ub[x*4+3];
1399 pixel[x*4+0] = (in4ub[x*4+0]*a+pixel[x*4+0]*b) >> 8;
1400 pixel[x*4+1] = (in4ub[x*4+1]*a+pixel[x*4+1]*b) >> 8;
1401 pixel[x*4+2] = (in4ub[x*4+2]*a+pixel[x*4+2]*b) >> 8;
1402 pixel[x*4+3] = (in4ub[x*4+3]*a+pixel[x*4+3]*b) >> 8;
1405 case DPSOFTRAST_BLENDMODE_ADDALPHA:
1406 for (x = startx;x < endx;x++)
1411 d[0] = (((in4ub[x*4+0]*a)>>8)+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1412 d[1] = (((in4ub[x*4+1]*a)>>8)+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1413 d[2] = (((in4ub[x*4+2]*a)>>8)+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1414 d[3] = (((in4ub[x*4+3]*a)>>8)+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1415 pixel[x*4+0] = d[0];
1416 pixel[x*4+1] = d[1];
1417 pixel[x*4+2] = d[2];
1418 pixel[x*4+3] = d[3];
1421 case DPSOFTRAST_BLENDMODE_ADD:
1422 for (x = startx;x < endx;x++)
1426 d[0] = (in4ub[x*4+0]+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1427 d[1] = (in4ub[x*4+1]+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1428 d[2] = (in4ub[x*4+2]+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1429 d[3] = (in4ub[x*4+3]+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1430 pixel[x*4+0] = d[0];
1431 pixel[x*4+1] = d[1];
1432 pixel[x*4+2] = d[2];
1433 pixel[x*4+3] = d[3];
1436 case DPSOFTRAST_BLENDMODE_INVMOD:
1437 for (x = startx;x < endx;x++)
1441 pixel[x*4+0] = ((255-in4ub[x*4+0])*pixel[x*4+0])>>8;
1442 pixel[x*4+1] = ((255-in4ub[x*4+1])*pixel[x*4+1])>>8;
1443 pixel[x*4+2] = ((255-in4ub[x*4+2])*pixel[x*4+2])>>8;
1444 pixel[x*4+3] = ((255-in4ub[x*4+3])*pixel[x*4+3])>>8;
1447 case DPSOFTRAST_BLENDMODE_MUL:
1448 for (x = startx;x < endx;x++)
1452 pixel[x*4+0] = (in4ub[x*4+0]*pixel[x*4+0])>>8;
1453 pixel[x*4+1] = (in4ub[x*4+1]*pixel[x*4+1])>>8;
1454 pixel[x*4+2] = (in4ub[x*4+2]*pixel[x*4+2])>>8;
1455 pixel[x*4+3] = (in4ub[x*4+3]*pixel[x*4+3])>>8;
1458 case DPSOFTRAST_BLENDMODE_MUL2:
1459 for (x = startx;x < endx;x++)
1463 d[0] = (in4ub[x*4+0]*pixel[x*4+0])>>7;if (d[0] > 255) d[0] = 255;
1464 d[1] = (in4ub[x*4+1]*pixel[x*4+1])>>7;if (d[1] > 255) d[1] = 255;
1465 d[2] = (in4ub[x*4+2]*pixel[x*4+2])>>7;if (d[2] > 255) d[2] = 255;
1466 d[3] = (in4ub[x*4+3]*pixel[x*4+3])>>7;if (d[3] > 255) d[3] = 255;
1467 pixel[x*4+0] = d[0];
1468 pixel[x*4+1] = d[1];
1469 pixel[x*4+2] = d[2];
1470 pixel[x*4+3] = d[3];
1473 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1474 for (x = startx;x < endx;x++)
1479 d[0] = pixel[x*4+0]-((in4ub[x*4+0]*a)>>8);if (d[0] < 0) d[0] = 0;
1480 d[1] = pixel[x*4+1]-((in4ub[x*4+1]*a)>>8);if (d[1] < 0) d[1] = 0;
1481 d[2] = pixel[x*4+2]-((in4ub[x*4+2]*a)>>8);if (d[2] < 0) d[2] = 0;
1482 d[3] = pixel[x*4+3]-((in4ub[x*4+3]*a)>>8);if (d[3] < 0) d[3] = 0;
1483 pixel[x*4+0] = d[0];
1484 pixel[x*4+1] = d[1];
1485 pixel[x*4+2] = d[2];
1486 pixel[x*4+3] = d[3];
1489 case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1490 for (x = startx;x < endx;x++)
1494 b = 255 - in4ub[x*4+3];
1495 d[0] = in4ub[x*4+0]+((pixel[x*4+0]*b)>>8);if (d[0] > 255) d[0] = 255;
1496 d[1] = in4ub[x*4+1]+((pixel[x*4+1]*b)>>8);if (d[1] > 255) d[1] = 255;
1497 d[2] = in4ub[x*4+2]+((pixel[x*4+2]*b)>>8);if (d[2] > 255) d[2] = 255;
1498 d[3] = in4ub[x*4+3]+((pixel[x*4+3]*b)>>8);if (d[3] > 255) d[3] = 255;
1499 pixel[x*4+0] = d[0];
1500 pixel[x*4+1] = d[1];
1501 pixel[x*4+2] = d[2];
1502 pixel[x*4+3] = d[3];
1508 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float * RESTRICT out4f, int texunitindex, int arrayindex, const float * RESTRICT zf)
1511 int startx = span->startx;
1512 int endx = span->endx;
1519 unsigned int tci[2];
1520 unsigned int tci1[2];
1521 unsigned int tcimin[2];
1522 unsigned int tcimax[2];
1527 const unsigned char * RESTRICT pixelbase;
1528 const unsigned char * RESTRICT pixel[4];
1529 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1530 // if no texture is bound, just fill it with white
1533 for (x = startx;x < endx;x++)
1535 out4f[x*4+0] = 1.0f;
1536 out4f[x*4+1] = 1.0f;
1537 out4f[x*4+2] = 1.0f;
1538 out4f[x*4+3] = 1.0f;
1542 mip = span->mip[texunitindex];
1543 // if this mipmap of the texture is 1 pixel, just fill it with that color
1544 if (texture->mipmap[mip][1] == 4)
1546 c[0] = texture->bytes[2] * (1.0f/255.0f);
1547 c[1] = texture->bytes[1] * (1.0f/255.0f);
1548 c[2] = texture->bytes[0] * (1.0f/255.0f);
1549 c[3] = texture->bytes[3] * (1.0f/255.0f);
1550 for (x = startx;x < endx;x++)
1552 out4f[x*4+0] = c[0];
1553 out4f[x*4+1] = c[1];
1554 out4f[x*4+2] = c[2];
1555 out4f[x*4+3] = c[3];
1559 filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1560 data[0] = span->data[0][arrayindex][0];
1561 data[1] = span->data[0][arrayindex][1];
1562 data[2] = span->data[0][arrayindex][2];
1563 data[3] = span->data[0][arrayindex][3];
1564 slope[0] = span->data[1][arrayindex][0];
1565 slope[1] = span->data[1][arrayindex][1];
1566 slope[2] = span->data[1][arrayindex][2];
1567 slope[3] = span->data[1][arrayindex][3];
1568 flags = texture->flags;
1569 pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
1570 tcscale[0] = texture->mipmap[mip][2];
1571 tcscale[1] = texture->mipmap[mip][3];
1572 tciwidth = texture->mipmap[mip][2];
1575 tcimax[0] = texture->mipmap[mip][2]-1;
1576 tcimax[1] = texture->mipmap[mip][3]-1;
1577 tciwrapmask[0] = texture->mipmap[mip][2]-1;
1578 tciwrapmask[1] = texture->mipmap[mip][3]-1;
1579 for (x = startx;x < endx;)
1582 unsigned int subtc[2];
1583 unsigned int substep[2];
1584 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1585 float subscale = 4096.0f/(DPSOFTRAST_MAXSUBSPAN-1);
1589 subscale = endsub > x ? 4096.0f / (endsub - x) : 1.0f;
1591 tc[0] = (data[0] + slope[0]*x) * zf[x] * tcscale[0] - 0.5f;
1592 tc[1] = (data[1] + slope[1]*x) * zf[x] * tcscale[1] - 0.5f;
1593 endtc[0] = (data[0] + slope[0]*endsub) * zf[endsub] * tcscale[0] - 0.5f;
1594 endtc[1] = (data[1] + slope[1]*endsub) * zf[endsub] * tcscale[1] - 0.5f;
1595 substep[0] = (endtc[0] - tc[0]) * subscale;
1596 substep[1] = (endtc[1] - tc[1]) * subscale;
1597 subtc[0] = tc[0] * (1<<12);
1598 subtc[1] = tc[1] * (1<<12);
1599 if (!(flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE))
1601 subtc[0] &= (tciwrapmask[0]<<12)|0xFFF;
1602 subtc[1] &= (tciwrapmask[1]<<12)|0xFFF;
1606 tci[0] = (subtc[0]>>12) - tcimin[0];
1607 tci[1] = (subtc[1]>>12) - tcimin[0];
1608 tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12) + 1;
1609 tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12) + 1;
1610 if (tci[0] <= tcimax[0] && tci[1] <= tcimax[1] && tci1[0] <= tcimax[0] && tci1[1] <= tcimax[1])
1612 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1614 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1615 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1616 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1617 tci[0] = subtc[0]>>12;
1618 tci[1] = subtc[1]>>12;
1619 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1620 pixel[1] = pixel[0] + 4 * tciwidth;
1621 c[0] = (pixel[0][2]*lerp[0]+pixel[0][4+2]*lerp[1]+pixel[1][2]*lerp[2]+pixel[1][4+2]*lerp[3]) * (1.0f / 0xFF000000);
1622 c[1] = (pixel[0][1]*lerp[0]+pixel[0][4+1]*lerp[1]+pixel[1][1]*lerp[2]+pixel[1][4+1]*lerp[3]) * (1.0f / 0xFF000000);
1623 c[2] = (pixel[0][0]*lerp[0]+pixel[0][4+0]*lerp[1]+pixel[1][0]*lerp[2]+pixel[1][4+0]*lerp[3]) * (1.0f / 0xFF000000);
1624 c[3] = (pixel[0][3]*lerp[0]+pixel[0][4+3]*lerp[1]+pixel[1][3]*lerp[2]+pixel[1][4+3]*lerp[3]) * (1.0f / 0xFF000000);
1625 out4f[x*4+0] = c[0];
1626 out4f[x*4+1] = c[1];
1627 out4f[x*4+2] = c[2];
1628 out4f[x*4+3] = c[3];
1631 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1633 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1635 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1636 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1637 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1638 tci[0] = subtc[0]>>12;
1639 tci[1] = subtc[1]>>12;
1640 tci1[0] = tci[0] + 1;
1641 tci1[1] = tci[1] + 1;
1642 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1643 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1644 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1645 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1646 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1647 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1648 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1649 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1650 c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF000000);
1651 c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF000000);
1652 c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF000000);
1653 c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF000000);
1654 out4f[x*4+0] = c[0];
1655 out4f[x*4+1] = c[1];
1656 out4f[x*4+2] = c[2];
1657 out4f[x*4+3] = c[3];
1662 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1664 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1665 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1666 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1667 tci[0] = subtc[0]>>12;
1668 tci[1] = subtc[1]>>12;
1669 tci1[0] = tci[0] + 1;
1670 tci1[1] = tci[1] + 1;
1671 tci[0] &= tciwrapmask[0];
1672 tci[1] &= tciwrapmask[1];
1673 tci1[0] &= tciwrapmask[0];
1674 tci1[1] &= tciwrapmask[1];
1675 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1676 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1677 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1678 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1679 c[0] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF000000);
1680 c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF000000);
1681 c[2] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF000000);
1682 c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF000000);
1683 out4f[x*4+0] = c[0];
1684 out4f[x*4+1] = c[1];
1685 out4f[x*4+2] = c[2];
1686 out4f[x*4+3] = c[3];
1690 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1692 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1694 tci[0] = subtc[0]>>12;
1695 tci[1] = subtc[1]>>12;
1696 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1697 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1698 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1699 c[0] = pixel[0][2] * (1.0f / 255.0f);
1700 c[1] = pixel[0][1] * (1.0f / 255.0f);
1701 c[2] = pixel[0][0] * (1.0f / 255.0f);
1702 c[3] = pixel[0][3] * (1.0f / 255.0f);
1703 out4f[x*4+0] = c[0];
1704 out4f[x*4+1] = c[1];
1705 out4f[x*4+2] = c[2];
1706 out4f[x*4+3] = c[3];
1711 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1713 tci[0] = subtc[0]>>12;
1714 tci[1] = subtc[1]>>12;
1715 tci[0] &= tciwrapmask[0];
1716 tci[1] &= tciwrapmask[1];
1717 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1718 c[0] = pixel[0][2] * (1.0f / 255.0f);
1719 c[1] = pixel[0][1] * (1.0f / 255.0f);
1720 c[2] = pixel[0][0] * (1.0f / 255.0f);
1721 c[3] = pixel[0][3] * (1.0f / 255.0f);
1722 out4f[x*4+0] = c[0];
1723 out4f[x*4+1] = c[1];
1724 out4f[x*4+2] = c[2];
1725 out4f[x*4+3] = c[3];
1731 void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf)
1734 int startx = span->startx;
1735 int endx = span->endx;
1741 unsigned int tci[2];
1742 unsigned int tci1[2];
1743 unsigned int tcimin[2];
1744 unsigned int tcimax[2];
1750 unsigned int *outi = (unsigned int *)out4ub;
1751 const unsigned char * RESTRICT pixelbase;
1752 const unsigned int * RESTRICT pixelbasei;
1753 const unsigned char * RESTRICT pixel[4];
1754 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1755 // if no texture is bound, just fill it with white
1758 memset(out4ub, 255, span->length*4);
1761 mip = span->mip[texunitindex];
1762 // if this mipmap of the texture is 1 pixel, just fill it with that color
1763 if (texture->mipmap[mip][1] == 4)
1765 k = *((const unsigned int *)texture->bytes);
1766 for (x = startx;x < endx;x++)
1770 filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1771 data[0] = span->data[0][arrayindex][0];
1772 data[1] = span->data[0][arrayindex][1];
1773 data[2] = span->data[0][arrayindex][2];
1774 data[3] = span->data[0][arrayindex][3];
1775 slope[0] = span->data[1][arrayindex][0];
1776 slope[1] = span->data[1][arrayindex][1];
1777 slope[2] = span->data[1][arrayindex][2];
1778 slope[3] = span->data[1][arrayindex][3];
1779 flags = texture->flags;
1780 pixelbase = (const unsigned char *)texture->bytes + texture->mipmap[mip][0];
1781 pixelbasei = (const unsigned int *)pixelbase;
1782 tcscale[0] = texture->mipmap[mip][2];
1783 tcscale[1] = texture->mipmap[mip][3];
1784 tciwidth = texture->mipmap[mip][2];
1787 tcimax[0] = texture->mipmap[mip][2]-1;
1788 tcimax[1] = texture->mipmap[mip][3]-1;
1789 tciwrapmask[0] = texture->mipmap[mip][2]-1;
1790 tciwrapmask[1] = texture->mipmap[mip][3]-1;
1791 for (x = startx;x < endx;)
1794 unsigned int subtc[2];
1795 unsigned int substep[2];
1796 int endsub = x + DPSOFTRAST_MAXSUBSPAN-1;
1797 float subscale = 4096.0f/(DPSOFTRAST_MAXSUBSPAN-1);
1801 subscale = endsub > x ? 4096.0f / (endsub - x) : 1.0f;
1803 tc[0] = (data[0] + slope[0]*x) * zf[x] * tcscale[0] - 0.5f;
1804 tc[1] = (data[1] + slope[1]*x) * zf[x] * tcscale[1] - 0.5f;
1805 endtc[0] = (data[0] + slope[0]*endsub) * zf[endsub] * tcscale[0] - 0.5f;
1806 endtc[1] = (data[1] + slope[1]*endsub) * zf[endsub] * tcscale[1] - 0.5f;
1807 substep[0] = (endtc[0] - tc[0]) * subscale;
1808 substep[1] = (endtc[1] - tc[1]) * subscale;
1809 subtc[0] = tc[0] * (1<<12);
1810 subtc[1] = tc[1] * (1<<12);
1811 if (!(flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE))
1813 subtc[0] &= (tciwrapmask[0]<<12)|0xFFF;
1814 subtc[1] &= (tciwrapmask[1]<<12)|0xFFF;
1818 tci[0] = (subtc[0]>>12) - tcimin[0];
1819 tci[1] = (subtc[1]>>12) - tcimin[0];
1820 tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12) + 1;
1821 tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12) + 1;
1822 if (tci[0] <= tcimax[0] && tci[1] <= tcimax[1] && tci1[0] <= tcimax[0] && tci1[1] <= tcimax[1])
1824 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1826 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1827 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1828 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1829 tci[0] = subtc[0]>>12;
1830 tci[1] = subtc[1]>>12;
1831 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1832 pixel[1] = pixel[0] + 4 * tciwidth;
1833 out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[0][4+0]*lerp[1]+pixel[1][0]*lerp[2]+pixel[1][4+0]*lerp[3]) >> 24;
1834 out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[0][4+1]*lerp[1]+pixel[1][1]*lerp[2]+pixel[1][4+1]*lerp[3]) >> 24;
1835 out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[0][4+2]*lerp[1]+pixel[1][2]*lerp[2]+pixel[1][4+2]*lerp[3]) >> 24;
1836 out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[0][4+3]*lerp[1]+pixel[1][3]*lerp[2]+pixel[1][4+3]*lerp[3]) >> 24;
1839 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1841 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1843 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1844 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1845 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1846 tci[0] = subtc[0]>>12;
1847 tci[1] = subtc[1]>>12;
1848 tci1[0] = tci[0] + 1;
1849 tci1[1] = tci[1] + 1;
1850 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1851 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1852 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1853 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1854 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1855 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1856 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1857 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1858 out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) >> 24;
1859 out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) >> 24;
1860 out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) >> 24;
1861 out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) >> 24;
1866 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1868 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1869 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1870 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1871 tci[0] = subtc[0]>>12;
1872 tci[1] = subtc[1]>>12;
1873 tci1[0] = tci[0] + 1;
1874 tci1[1] = tci[1] + 1;
1875 tci[0] &= tciwrapmask[0];
1876 tci[1] &= tciwrapmask[1];
1877 tci1[0] &= tciwrapmask[0];
1878 tci1[1] &= tciwrapmask[1];
1879 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1880 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1881 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1882 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1883 out4ub[x*4+0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) >> 24;
1884 out4ub[x*4+1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) >> 24;
1885 out4ub[x*4+2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) >> 24;
1886 out4ub[x*4+3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) >> 24;
1892 tci[0] = (subtc[0]>>12) - tcimin[0];
1893 tci[1] = (subtc[1]>>12) - tcimin[0];
1894 tci1[0] = ((subtc[0] + (endsub - x)*substep[0])>>12);
1895 tci1[1] = ((subtc[1] + (endsub - x)*substep[1])>>12);
1896 if (tci[0] <= tcimax[0] && tci[1] <= tcimax[1] && tci1[0] <= tcimax[0] && tci1[1] <= tcimax[1])
1898 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1900 tci[0] = subtc[0]>>12;
1901 tci[1] = subtc[1]>>12;
1902 outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
1905 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1907 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1909 tci[0] = subtc[0]>>12;
1910 tci[1] = subtc[1]>>12;
1911 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1912 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1913 outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
1918 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1920 tci[0] = subtc[0]>>12;
1921 tci[1] = subtc[1]>>12;
1922 tci[0] &= tciwrapmask[0];
1923 tci[1] &= tciwrapmask[1];
1924 outi[x] = pixelbasei[(tci[1]*tciwidth+tci[0])];
1931 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, int arrayindex, const float *zf)
1934 int startx = span->startx;
1935 int endx = span->endx;
1940 data[0] = span->data[0][arrayindex][0];
1941 data[1] = span->data[0][arrayindex][1];
1942 data[2] = span->data[0][arrayindex][2];
1943 data[3] = span->data[0][arrayindex][3];
1944 slope[0] = span->data[1][arrayindex][0];
1945 slope[1] = span->data[1][arrayindex][1];
1946 slope[2] = span->data[1][arrayindex][2];
1947 slope[3] = span->data[1][arrayindex][3];
1948 for (x = startx;x < endx;x++)
1951 c[0] = (data[0] + slope[0]*x) * z;
1952 c[1] = (data[1] + slope[1]*x) * z;
1953 c[2] = (data[2] + slope[2]*x) * z;
1954 c[3] = (data[3] + slope[3]*x) * z;
1955 out4f[x*4+0] = in4f[x*4+0] * c[0];
1956 out4f[x*4+1] = in4f[x*4+1] * c[1];
1957 out4f[x*4+2] = in4f[x*4+2] * c[2];
1958 out4f[x*4+3] = in4f[x*4+3] * c[3];
1962 void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, int arrayindex, const float *zf)
1965 int startx = span->startx;
1966 int endx = span->endx;
1971 data[0] = span->data[0][arrayindex][0];
1972 data[1] = span->data[0][arrayindex][1];
1973 data[2] = span->data[0][arrayindex][2];
1974 data[3] = span->data[0][arrayindex][3];
1975 slope[0] = span->data[1][arrayindex][0];
1976 slope[1] = span->data[1][arrayindex][1];
1977 slope[2] = span->data[1][arrayindex][2];
1978 slope[3] = span->data[1][arrayindex][3];
1979 for (x = startx;x < endx;x++)
1982 c[0] = (data[0] + slope[0]*x) * z;
1983 c[1] = (data[1] + slope[1]*x) * z;
1984 c[2] = (data[2] + slope[2]*x) * z;
1985 c[3] = (data[3] + slope[3]*x) * z;
1986 out4f[x*4+0] = c[0];
1987 out4f[x*4+1] = c[1];
1988 out4f[x*4+2] = c[2];
1989 out4f[x*4+3] = c[3];
1993 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
1995 int x, startx = span->startx, endx = span->endx;
1996 float c[4], localcolor[4];
1997 localcolor[0] = subcolor[0];
1998 localcolor[1] = subcolor[1];
1999 localcolor[2] = subcolor[2];
2000 localcolor[3] = subcolor[3];
2001 for (x = startx;x < endx;x++)
2003 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
2004 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
2005 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
2006 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
2007 out4f[x*4+0] = ina4f[x*4+0] + c[0];
2008 out4f[x*4+1] = ina4f[x*4+1] + c[1];
2009 out4f[x*4+2] = ina4f[x*4+2] + c[2];
2010 out4f[x*4+3] = ina4f[x*4+3] + c[3];
2014 void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2016 int x, startx = span->startx, endx = span->endx;
2017 for (x = startx;x < endx;x++)
2019 out4f[x*4+0] = ina4f[x*4+0] * inb4f[x*4+0];
2020 out4f[x*4+1] = ina4f[x*4+1] * inb4f[x*4+1];
2021 out4f[x*4+2] = ina4f[x*4+2] * inb4f[x*4+2];
2022 out4f[x*4+3] = ina4f[x*4+3] * inb4f[x*4+3];
2026 void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2028 int x, startx = span->startx, endx = span->endx;
2029 for (x = startx;x < endx;x++)
2031 out4f[x*4+0] = ina4f[x*4+0] + inb4f[x*4+0];
2032 out4f[x*4+1] = ina4f[x*4+1] + inb4f[x*4+1];
2033 out4f[x*4+2] = ina4f[x*4+2] + inb4f[x*4+2];
2034 out4f[x*4+3] = ina4f[x*4+3] + inb4f[x*4+3];
2038 void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2040 int x, startx = span->startx, endx = span->endx;
2042 for (x = startx;x < endx;x++)
2044 a = 1.0f - inb4f[x*4+3];
2046 out4f[x*4+0] = ina4f[x*4+0] * a + inb4f[x*4+0] * b;
2047 out4f[x*4+1] = ina4f[x*4+1] * a + inb4f[x*4+1] * b;
2048 out4f[x*4+2] = ina4f[x*4+2] * a + inb4f[x*4+2] * b;
2049 out4f[x*4+3] = ina4f[x*4+3] * a + inb4f[x*4+3] * b;
2053 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, const float *color)
2055 int x, startx = span->startx, endx = span->endx;
2056 float localcolor[4], ilerp, lerp;
2057 localcolor[0] = color[0];
2058 localcolor[1] = color[1];
2059 localcolor[2] = color[2];
2060 localcolor[3] = color[3];
2061 ilerp = 1.0f - localcolor[3];
2062 lerp = localcolor[3];
2063 for (x = startx;x < endx;x++)
2065 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
2066 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
2067 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
2068 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
2074 void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, int arrayindex, const float *zf)
2077 int startx = span->startx;
2078 int endx = span->endx;
2082 data[2] = span->data[0][arrayindex][0];
2083 data[1] = span->data[0][arrayindex][1];
2084 data[0] = span->data[0][arrayindex][2];
2085 data[3] = span->data[0][arrayindex][3];
2086 slope[2] = span->data[1][arrayindex][0];
2087 slope[1] = span->data[1][arrayindex][1];
2088 slope[0] = span->data[1][arrayindex][2];
2089 slope[3] = span->data[1][arrayindex][3];
2090 for (x = startx;x < endx;x++)
2093 out4ub[x*4+0] = (int)(in4ub[x*4+0] * (data[0] + slope[0]*x) * z);
2094 out4ub[x*4+1] = (int)(in4ub[x*4+1] * (data[1] + slope[1]*x) * z);
2095 out4ub[x*4+2] = (int)(in4ub[x*4+2] * (data[2] + slope[2]*x) * z);
2096 out4ub[x*4+3] = (int)(in4ub[x*4+3] * (data[3] + slope[3]*x) * z);
2100 void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, int arrayindex, const float *zf)
2103 int startx = span->startx;
2104 int endx = span->endx;
2108 data[2] = span->data[0][arrayindex][0]*255.0f;
2109 data[1] = span->data[0][arrayindex][1]*255.0f;
2110 data[0] = span->data[0][arrayindex][2]*255.0f;
2111 data[3] = span->data[0][arrayindex][3]*255.0f;
2112 slope[2] = span->data[1][arrayindex][0]*255.0f;
2113 slope[1] = span->data[1][arrayindex][1]*255.0f;
2114 slope[0] = span->data[1][arrayindex][2]*255.0f;
2115 slope[3] = span->data[1][arrayindex][3]*255.0f;
2116 for (x = startx;x < endx;x++)
2119 out4ub[x*4+0] = (int)((data[0] + slope[0]*x) * z);
2120 out4ub[x*4+1] = (int)((data[1] + slope[1]*x) * z);
2121 out4ub[x*4+2] = (int)((data[2] + slope[2]*x) * z);
2122 out4ub[x*4+3] = (int)((data[3] + slope[3]*x) * z);
2126 void DPSOFTRAST_Draw_Span_AddBloomBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub, const float *subcolor)
2128 int x, startx = span->startx, endx = span->endx;
2129 int c[4], localcolor[4];
2130 localcolor[2] = (int)(subcolor[0] * 255.0f);
2131 localcolor[1] = (int)(subcolor[1] * 255.0f);
2132 localcolor[0] = (int)(subcolor[2] * 255.0f);
2133 localcolor[3] = (int)(subcolor[3] * 255.0f);
2134 for (x = startx;x < endx;x++)
2136 c[0] = inb4ub[x*4+0] - localcolor[0];if (c[0] < 0) c[0] = 0;
2137 c[1] = inb4ub[x*4+1] - localcolor[1];if (c[1] < 0) c[1] = 0;
2138 c[2] = inb4ub[x*4+2] - localcolor[2];if (c[2] < 0) c[2] = 0;
2139 c[3] = inb4ub[x*4+3] - localcolor[3];if (c[3] < 0) c[3] = 0;
2140 c[0] += ina4ub[x*4+0];if (c[0] > 255) c[0] = 255;
2141 c[1] += ina4ub[x*4+1];if (c[1] > 255) c[1] = 255;
2142 c[2] += ina4ub[x*4+2];if (c[2] > 255) c[2] = 255;
2143 c[3] += ina4ub[x*4+3];if (c[3] > 255) c[3] = 255;
2144 out4ub[x*4+0] = c[0];
2145 out4ub[x*4+1] = c[1];
2146 out4ub[x*4+2] = c[2];
2147 out4ub[x*4+3] = c[3];
2151 void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2153 int x, startx = span->startx, endx = span->endx;
2154 for (x = startx;x < endx;x++)
2156 out4ub[x*4+0] = (ina4ub[x*4+0] * inb4ub[x*4+0])>>8;
2157 out4ub[x*4+1] = (ina4ub[x*4+1] * inb4ub[x*4+1])>>8;
2158 out4ub[x*4+2] = (ina4ub[x*4+2] * inb4ub[x*4+2])>>8;
2159 out4ub[x*4+3] = (ina4ub[x*4+3] * inb4ub[x*4+3])>>8;
2163 void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2165 int x, startx = span->startx, endx = span->endx;
2167 for (x = startx;x < endx;x++)
2169 d[0] = ina4ub[x*4+0] + inb4ub[x*4+0];if (d[0] > 255) d[0] = 255;
2170 d[1] = ina4ub[x*4+1] + inb4ub[x*4+1];if (d[1] > 255) d[1] = 255;
2171 d[2] = ina4ub[x*4+2] + inb4ub[x*4+2];if (d[2] > 255) d[2] = 255;
2172 d[3] = ina4ub[x*4+3] + inb4ub[x*4+3];if (d[3] > 255) d[3] = 255;
2173 out4ub[x*4+0] = d[0];
2174 out4ub[x*4+1] = d[1];
2175 out4ub[x*4+2] = d[2];
2176 out4ub[x*4+3] = d[3];
2180 void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2182 int x, startx = span->startx, endx = span->endx;
2184 for (x = startx;x < endx;x++)
2186 a = 256 - inb4ub[x*4+3];
2188 out4ub[x*4+0] = (ina4ub[x*4+0] * a + inb4ub[x*4+0] * b)>>8;
2189 out4ub[x*4+1] = (ina4ub[x*4+1] * a + inb4ub[x*4+1] * b)>>8;
2190 out4ub[x*4+2] = (ina4ub[x*4+2] * a + inb4ub[x*4+2] * b)>>8;
2191 out4ub[x*4+3] = (ina4ub[x*4+3] * a + inb4ub[x*4+3] * b)>>8;
2195 void DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, const float *color)
2197 int x, startx = span->startx, endx = span->endx;
2198 int localcolor[4], ilerp, lerp;
2199 localcolor[2] = (int)(color[0]*255.0f);
2200 localcolor[1] = (int)(color[1]*255.0f);
2201 localcolor[0] = (int)(color[2]*255.0f);
2202 localcolor[3] = (int)(color[3]*255.0f);
2203 ilerp = 256 - localcolor[3];
2204 lerp = localcolor[3];
2205 for (x = startx;x < endx;x++)
2207 out4ub[x*4+0] = (in4ub[x*4+0] * ilerp + localcolor[0] * lerp)>>8;
2208 out4ub[x*4+1] = (in4ub[x*4+1] * ilerp + localcolor[1] * lerp)>>8;
2209 out4ub[x*4+2] = (in4ub[x*4+2] * ilerp + localcolor[2] * lerp)>>8;
2210 out4ub[x*4+3] = (in4ub[x*4+3] * ilerp + localcolor[3] * lerp)>>8;
2216 extern int dpsoftrast_test;
2218 void DPSOFTRAST_VertexShader_Generic(void)
2220 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);
2221 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2222 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2223 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2224 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2227 void DPSOFTRAST_PixelShader_Generic(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2229 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2230 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2231 unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2232 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2233 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2234 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
2236 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_FIRST, 2, buffer_z);
2237 DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(span, buffer_FragColorbgra8, buffer_texture_colorbgra8, 1, buffer_z);
2238 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2240 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_SECOND, 2, buffer_z);
2241 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2244 DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2246 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2249 DPSOFTRAST_Draw_Span_AddBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2251 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND)
2254 DPSOFTRAST_Draw_Span_MixBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2259 DPSOFTRAST_Draw_Span_VaryingBGRA8(span, buffer_FragColorbgra8, 1, buffer_z);
2260 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2265 void DPSOFTRAST_VertexShader_PostProcess(void)
2267 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);
2268 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2269 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2272 void DPSOFTRAST_PixelShader_PostProcess(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2274 // TODO: optimize!! at the very least there is no reason to use texture sampling on the frame texture
2275 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2276 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2277 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2278 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2279 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_FragColorbgra8, GL20TU_FIRST, 2, buffer_z);
2280 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
2282 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_SECOND, 3, buffer_z);
2283 DPSOFTRAST_Draw_Span_AddBloomBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_colorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
2285 DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
2286 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
2288 // TODO: implement saturation
2290 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
2292 // TODO: implement gammaramps
2294 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2299 void DPSOFTRAST_VertexShader_Depth_Or_Shadow(void)
2301 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);
2304 void DPSOFTRAST_PixelShader_Depth_Or_Shadow(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2306 // this is never called (because colormask is off when this shader is used)
2307 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2308 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2309 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2310 memset(buffer_FragColorbgra8, 0, span->length*4);
2311 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2316 void DPSOFTRAST_VertexShader_FlatColor(void)
2318 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);
2319 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1);
2322 void DPSOFTRAST_PixelShader_FlatColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2324 int x, startx = span->startx, endx = span->endx;
2325 int Color_Ambienti[4];
2326 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2327 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2328 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2329 Color_Ambienti[2] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]*256.0f);
2330 Color_Ambienti[1] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]*256.0f);
2331 Color_Ambienti[0] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]*256.0f);
2332 Color_Ambienti[3] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0] *256.0f);
2333 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2334 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2335 for (x = startx;x < endx;x++)
2337 buffer_FragColorbgra8[x*4+0] = (buffer_texture_colorbgra8[x*4+0] * Color_Ambienti[0])>>8;
2338 buffer_FragColorbgra8[x*4+1] = (buffer_texture_colorbgra8[x*4+1] * Color_Ambienti[1])>>8;
2339 buffer_FragColorbgra8[x*4+2] = (buffer_texture_colorbgra8[x*4+2] * Color_Ambienti[2])>>8;
2340 buffer_FragColorbgra8[x*4+3] = (buffer_texture_colorbgra8[x*4+3] * Color_Ambienti[3])>>8;
2342 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2347 void DPSOFTRAST_VertexShader_VertexColor(void)
2349 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);
2350 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2351 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1);
2354 void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2356 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2357 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2358 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2359 int x, startx = span->startx, endx = span->endx;
2360 float Color_Ambient[4], Color_Diffuse[4];
2364 int arrayindex = DPSOFTRAST_ARRAY_COLOR;
2365 data[2] = span->data[0][arrayindex][0];
2366 data[1] = span->data[0][arrayindex][1];
2367 data[0] = span->data[0][arrayindex][2];
2368 data[3] = span->data[0][arrayindex][3];
2369 slope[2] = span->data[1][arrayindex][0];
2370 slope[1] = span->data[1][arrayindex][1];
2371 slope[0] = span->data[1][arrayindex][2];
2372 slope[3] = span->data[1][arrayindex][3];
2373 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2374 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2375 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2376 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2377 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2378 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2379 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2380 Color_Diffuse[3] = 0.0f;
2381 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2382 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2383 for (x = startx;x < endx;x++)
2386 buffer_FragColorbgra8[x*4+0] = (int)(buffer_texture_colorbgra8[x*4+0] * (Color_Ambient[0] + ((data[0] + slope[0]*x) * z) * Color_Diffuse[0]));
2387 buffer_FragColorbgra8[x*4+1] = (int)(buffer_texture_colorbgra8[x*4+1] * (Color_Ambient[1] + ((data[1] + slope[1]*x) * z) * Color_Diffuse[1]));
2388 buffer_FragColorbgra8[x*4+2] = (int)(buffer_texture_colorbgra8[x*4+2] * (Color_Ambient[2] + ((data[2] + slope[2]*x) * z) * Color_Diffuse[2]));
2389 buffer_FragColorbgra8[x*4+3] = (int)(buffer_texture_colorbgra8[x*4+3] * (Color_Ambient[3] + ((data[3] + slope[3]*x) * z) * Color_Diffuse[3]));
2391 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2396 void DPSOFTRAST_VertexShader_Lightmap(void)
2398 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);
2399 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1);
2400 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
2403 void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2405 int x, startx = span->startx, endx = span->endx;
2406 float Color_Ambient[4], Color_Diffuse[4];
2407 int Color_Ambienti[4];
2408 int Color_Diffusei[4];
2409 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2410 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2411 unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2412 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2414 //unsigned char * RESTRICT pixelmask = span->pixelmask;
2415 //unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + span->start * 4;
2416 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2417 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2418 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2419 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2420 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2421 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2422 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2423 Color_Diffuse[3] = 0.0f;
2424 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2425 Color_Ambienti[2] = (int)(Color_Ambient[0] * 65536.0f);
2426 Color_Ambienti[1] = (int)(Color_Ambient[1] * 65536.0f);
2427 Color_Ambienti[0] = (int)(Color_Ambient[2] * 65536.0f);
2428 Color_Ambienti[3] = (int)(Color_Ambient[3] * 65536.0f);
2429 Color_Diffusei[2] = (int)(Color_Diffuse[0] * 256.0f);
2430 Color_Diffusei[1] = (int)(Color_Diffuse[1] * 256.0f);
2431 Color_Diffusei[0] = (int)(Color_Diffuse[2] * 256.0f);
2432 Color_Diffusei[3] = (int)(Color_Diffuse[3] * 256.0f);
2433 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2434 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_LIGHTMAP, 6, buffer_z);
2435 for (x = startx;x < endx;x++)
2437 d[0] = (buffer_texture_colorbgra8[x*4+0] * (Color_Ambienti[0] + buffer_texture_lightmapbgra8[x*4+0] * Color_Diffusei[0])) >> 16;if (d[0] > 255) d[0] = 255;
2438 d[1] = (buffer_texture_colorbgra8[x*4+1] * (Color_Ambienti[1] + buffer_texture_lightmapbgra8[x*4+1] * Color_Diffusei[1])) >> 16;if (d[1] > 255) d[1] = 255;
2439 d[2] = (buffer_texture_colorbgra8[x*4+2] * (Color_Ambienti[2] + buffer_texture_lightmapbgra8[x*4+2] * Color_Diffusei[2])) >> 16;if (d[2] > 255) d[2] = 255;
2440 d[3] = (buffer_texture_colorbgra8[x*4+3] * (Color_Ambienti[3] + buffer_texture_lightmapbgra8[x*4+3] * Color_Diffusei[3])) >> 16;if (d[3] > 255) d[3] = 255;
2441 buffer_FragColorbgra8[x*4+0] = d[0];
2442 buffer_FragColorbgra8[x*4+1] = d[1];
2443 buffer_FragColorbgra8[x*4+2] = d[2];
2444 buffer_FragColorbgra8[x*4+3] = d[3];
2446 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2451 void DPSOFTRAST_VertexShader_FakeLight(void)
2453 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);
2456 void DPSOFTRAST_PixelShader_FakeLight(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2459 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2460 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2461 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2462 memset(buffer_FragColorbgra8, 0, span->length*4);
2463 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2468 void DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace(void)
2470 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);
2473 void DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2476 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2477 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2478 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2479 memset(buffer_FragColorbgra8, 0, span->length*4);
2480 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2485 void DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace(void)
2487 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);
2490 void DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2493 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2494 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2495 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2496 memset(buffer_FragColorbgra8, 0, span->length*4);
2497 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2502 void DPSOFTRAST_VertexShader_LightDirection(void)
2504 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);
2505 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_TexMatrixM1);
2508 void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2511 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2512 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2513 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2514 memset(buffer_FragColorbgra8, 0, span->length*4);
2515 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2520 void DPSOFTRAST_VertexShader_LightSource(void)
2522 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);
2525 void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2528 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2529 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2530 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2531 memset(buffer_FragColorbgra8, 0, span->length*4);
2532 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2537 void DPSOFTRAST_VertexShader_Refraction(void)
2539 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);
2542 void DPSOFTRAST_PixelShader_Refraction(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2545 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2546 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2547 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2548 memset(buffer_FragColorbgra8, 0, span->length*4);
2549 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2554 void DPSOFTRAST_VertexShader_Water(void)
2556 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);
2560 void DPSOFTRAST_PixelShader_Water(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2563 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2564 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2565 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2566 memset(buffer_FragColorbgra8, 0, span->length*4);
2567 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2572 void DPSOFTRAST_VertexShader_ShowDepth(void)
2574 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);
2577 void DPSOFTRAST_PixelShader_ShowDepth(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2580 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2581 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2582 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2583 memset(buffer_FragColorbgra8, 0, span->length*4);
2584 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2589 void DPSOFTRAST_VertexShader_DeferredGeometry(void)
2591 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);
2594 void DPSOFTRAST_PixelShader_DeferredGeometry(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2597 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2598 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2599 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2600 memset(buffer_FragColorbgra8, 0, span->length*4);
2601 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2606 void DPSOFTRAST_VertexShader_DeferredLightSource(void)
2608 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);
2611 void DPSOFTRAST_PixelShader_DeferredLightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2614 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2615 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2616 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2617 memset(buffer_FragColorbgra8, 0, span->length*4);
2618 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2623 typedef struct DPSOFTRAST_ShaderModeInfo_s
2626 void (*Vertex)(void);
2627 void (*Span)(const DPSOFTRAST_State_Draw_Span * RESTRICT span);
2629 DPSOFTRAST_ShaderModeInfo;
2631 DPSOFTRAST_ShaderModeInfo DPSOFTRAST_ShaderModeTable[SHADERMODE_COUNT] =
2633 {2, DPSOFTRAST_VertexShader_Generic, DPSOFTRAST_PixelShader_Generic, },
2634 {2, DPSOFTRAST_VertexShader_PostProcess, DPSOFTRAST_PixelShader_PostProcess, },
2635 {2, DPSOFTRAST_VertexShader_Depth_Or_Shadow, DPSOFTRAST_PixelShader_Depth_Or_Shadow, },
2636 {2, DPSOFTRAST_VertexShader_FlatColor, DPSOFTRAST_PixelShader_FlatColor, },
2637 {2, DPSOFTRAST_VertexShader_VertexColor, DPSOFTRAST_PixelShader_VertexColor, },
2638 {2, DPSOFTRAST_VertexShader_Lightmap, DPSOFTRAST_PixelShader_Lightmap, },
2639 {2, DPSOFTRAST_VertexShader_FakeLight, DPSOFTRAST_PixelShader_FakeLight, },
2640 {2, DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace, DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace, },
2641 {2, DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace, DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace},
2642 {2, DPSOFTRAST_VertexShader_LightDirection, DPSOFTRAST_PixelShader_LightDirection, },
2643 {2, DPSOFTRAST_VertexShader_LightSource, DPSOFTRAST_PixelShader_LightSource, },
2644 {2, DPSOFTRAST_VertexShader_Refraction, DPSOFTRAST_PixelShader_Refraction, },
2645 {2, DPSOFTRAST_VertexShader_Water, DPSOFTRAST_PixelShader_Water, },
2646 {2, DPSOFTRAST_VertexShader_ShowDepth, DPSOFTRAST_PixelShader_ShowDepth, },
2647 {2, DPSOFTRAST_VertexShader_DeferredGeometry, DPSOFTRAST_PixelShader_DeferredGeometry, },
2648 {2, DPSOFTRAST_VertexShader_DeferredLightSource, DPSOFTRAST_PixelShader_DeferredLightSource, }
2653 void DPSOFTRAST_Draw_ProcessSpans(void)
2659 int numspans = dpsoftrast.draw.numspans;
2661 // unsigned int *colorpixel;
2662 unsigned int *depthpixel;
2668 DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
2669 unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2670 for (i = 0;i < numspans;i++, span++)
2672 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
2673 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
2674 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
2676 depth = (int)(w*DPSOFTRAST_DEPTHSCALE);
2677 depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
2678 depthpixel = dpsoftrast.fb_depthpixels + span->start;
2679 switch(dpsoftrast.fb_depthfunc)
2682 case GL_ALWAYS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
2683 case GL_LESS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
2684 case GL_LEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
2685 case GL_EQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
2686 case GL_GEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
2687 case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
2688 case GL_NEVER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
2690 //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
2691 //for (x = 0;x < span->length;x++)
2692 // colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
2693 // if there is no color buffer, skip pixel shader
2695 endx = span->length;
2696 while (startx < endx && !pixelmask[startx])
2698 while (endx > startx && !pixelmask[endx-1])
2701 continue; // no pixels to fill
2702 span->pixelmask = pixelmask;
2703 span->startx = startx;
2705 // run pixel shader if appropriate
2706 // do this before running depthmask code, to allow the pixelshader
2707 // to clear pixelmask values for alpha testing
2708 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
2709 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
2710 if (dpsoftrast.user.depthmask)
2711 for (x = 0, d = depth;x < span->length;x++, d += depthslope)
2717 // no depth testing means we're just dealing with color...
2718 // if there is no color buffer, skip pixel shader
2719 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
2721 memset(pixelmask, 1, span->length);
2722 span->pixelmask = pixelmask;
2724 span->endx = span->length;
2725 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
2731 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask)
2733 int cullface = dpsoftrast.user.cullface;
2734 int width = dpsoftrast.fb_width;
2735 int height = dpsoftrast.fb_height;
2753 float mip_edge0tc[2];
2754 float mip_edge1tc[2];
2755 float mip_edge0xy[2];
2756 float mip_edge1xy[2];
2757 float mip_edge0xymul;
2758 float mip_edge1xymul;
2762 unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS];
2777 float trianglearea2;
2778 float triangleedge[2][4];
2779 float trianglenormal[4];
2781 float clipped[DPSOFTRAST_ARRAY_TOTAL][4][4];
2783 float proj[DPSOFTRAST_ARRAY_TOTAL][4][4];
2784 DPSOFTRAST_Texture *texture;
2785 DPSOFTRAST_State_Draw_Span *span;
2786 DPSOFTRAST_State_Draw_Span *oldspan;
2787 for (i = 0;i < numtriangles;i++)
2789 // generate the 3 edges of this triangle
2790 // generate spans for the triangle - switch based on left split or right split classification of triangle
2793 e[0] = element3i[i*3+0] - firstvertex;
2794 e[1] = element3i[i*3+1] - firstvertex;
2795 e[2] = element3i[i*3+2] - firstvertex;
2799 e[0] = element3s[i*3+0] - firstvertex;
2800 e[1] = element3s[i*3+1] - firstvertex;
2801 e[2] = element3s[i*3+2] - firstvertex;
2809 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];
2810 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];
2811 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];
2812 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];
2813 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];
2814 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];
2815 trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
2816 trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
2817 trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
2818 trianglearea2 = trianglenormal[0] * trianglenormal[0] + trianglenormal[1] * trianglenormal[1] + trianglenormal[2] * trianglenormal[2];
2819 // skip degenerate triangles, nothing good can come from them...
2820 if (trianglearea2 == 0.0f)
2822 // apply current cullface mode (this culls many triangles)
2826 if (trianglenormal[2] < 0)
2830 if (trianglenormal[2] > 0)
2834 // calculate distance from nearplane
2835 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + 1.0f;
2836 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + 1.0f;
2837 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + 1.0f;
2839 if (clipdist[0] < 0.0f)
2841 if (clipdist[1] < 0.0f)
2843 if (clipdist[2] < 0.0f)
2845 // clip triangle if necessary
2849 // triangle is entirely in front of nearplane
2851 // macros for clipping vertices
2852 #define CLIPPEDVERTEXLERP(k,p1,p2) \
2853 frac = clipdist[p1] / (clipdist[p1] - clipdist[p2]);\
2854 ifrac = 1.0f - frac;\
2855 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2859 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+0]*frac;\
2860 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+1]*frac;\
2861 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+2]*frac;\
2862 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3]*ifrac+dpsoftrast.draw.post_array4f[j][e[p2]*4+3]*frac;\
2865 DPSOFTRAST_Draw_ProjectVertices(screen[k], clipped[DPSOFTRAST_ARRAY_POSITION][k], 1)
2866 #define CLIPPEDVERTEXCOPY(k,p1) \
2867 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
2871 clipped[j][k][0] = dpsoftrast.draw.post_array4f[j][e[p1]*4+0];\
2872 clipped[j][k][1] = dpsoftrast.draw.post_array4f[j][e[p1]*4+1];\
2873 clipped[j][k][2] = dpsoftrast.draw.post_array4f[j][e[p1]*4+2];\
2874 clipped[j][k][3] = dpsoftrast.draw.post_array4f[j][e[p1]*4+3];\
2877 screen[k][0] = dpsoftrast.draw.screencoord4f[e[p1]*4+0];\
2878 screen[k][1] = dpsoftrast.draw.screencoord4f[e[p1]*4+1];\
2879 screen[k][2] = dpsoftrast.draw.screencoord4f[e[p1]*4+2];\
2880 screen[k][3] = dpsoftrast.draw.screencoord4f[e[p1]*4+3];
2882 CLIPPEDVERTEXCOPY(0,0);
2883 CLIPPEDVERTEXCOPY(1,1);
2884 CLIPPEDVERTEXCOPY(2,2);
2888 CLIPPEDVERTEXLERP(0,0,1);
2889 CLIPPEDVERTEXCOPY(1,1);
2890 CLIPPEDVERTEXCOPY(2,2);
2891 CLIPPEDVERTEXLERP(3,2,0);
2895 CLIPPEDVERTEXCOPY(0,0);
2896 CLIPPEDVERTEXLERP(1,0,1);
2897 CLIPPEDVERTEXLERP(2,1,2);
2898 CLIPPEDVERTEXCOPY(3,2);
2902 CLIPPEDVERTEXLERP(0,1,2);
2903 CLIPPEDVERTEXCOPY(1,2);
2904 CLIPPEDVERTEXLERP(2,2,0);
2908 CLIPPEDVERTEXCOPY(0,0);
2909 CLIPPEDVERTEXCOPY(1,1);
2910 CLIPPEDVERTEXLERP(2,1,2);
2911 CLIPPEDVERTEXLERP(3,2,0);
2915 CLIPPEDVERTEXLERP(0,0,1);
2916 CLIPPEDVERTEXCOPY(1,1);
2917 CLIPPEDVERTEXLERP(2,1,2);
2921 CLIPPEDVERTEXCOPY(0,0);
2922 CLIPPEDVERTEXLERP(1,0,1);
2923 CLIPPEDVERTEXLERP(2,2,0);
2927 // triangle is entirely behind nearplane
2930 // calculate integer y coords for triangle points
2931 screenx[0] = (int)(screen[0][0]);
2932 screeny[0] = (int)(screen[0][1]);
2933 screenx[1] = (int)(screen[1][0]);
2934 screeny[1] = (int)(screen[1][1]);
2935 screenx[2] = (int)(screen[2][0]);
2936 screeny[2] = (int)(screen[2][1]);
2937 screenx[3] = (int)(screen[3][0]);
2938 screeny[3] = (int)(screen[3][1]);
2939 // figure out the extents (bounding box) of the triangle
2940 extent[0] = screenx[0];
2941 extent[1] = screeny[0];
2942 extent[2] = screenx[0];
2943 extent[3] = screeny[0];
2944 for (j = 1;j < numpoints;j++)
2946 if (extent[0] > screenx[j]) extent[0] = screenx[j];
2947 if (extent[1] > screeny[j]) extent[1] = screeny[j];
2948 if (extent[2] < screenx[j]) extent[2] = screenx[j];
2949 if (extent[3] < screeny[j]) extent[3] = screeny[j];
2959 if (extent[2] > width)
2961 if (extent[3] > height)
2963 // skip offscreen triangles
2964 if (extent[2] <= extent[0] || extent[3] <= extent[1])
2966 // okay, this triangle is going to produce spans, we'd better project
2967 // the interpolants now (this is what gives perspective texturing),
2968 // this consists of simply multiplying all arrays by the W coord
2969 // (which is basically 1/Z), which will be undone per-pixel
2970 // (multiplying by Z again) to get the perspective-correct array
2972 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
2976 for (k = 0;k < numpoints;k++)
2979 proj[j][k][0] = clipped[j][k][0] * w;
2980 proj[j][k][1] = clipped[j][k][1] * w;
2981 proj[j][k][2] = clipped[j][k][2] * w;
2982 proj[j][k][3] = clipped[j][k][3] * w;
2986 // adjust texture LOD by texture density, in the simplest way possible...
2987 mip_edge0xy[0] = screen[0][0] - screen[1][0];
2988 mip_edge0xy[1] = screen[0][1] - screen[1][1];
2989 mip_edge1xy[0] = screen[2][0] - screen[1][0];
2990 mip_edge1xy[1] = screen[2][1] - screen[1][1];
2991 mip_edge0xymul = 1.0f / (mip_edge0xy[0]*mip_edge0xy[0]+mip_edge0xy[1]*mip_edge0xy[1]);
2992 mip_edge1xymul = 1.0f / (mip_edge1xy[0]*mip_edge1xy[0]+mip_edge1xy[1]*mip_edge1xy[1]);
2993 for (j = 0;j < DPSOFTRAST_MAXTEXTUREUNITS;j++)
2995 texture = dpsoftrast.texbound[j];
2998 if (texture->filter <= DPSOFTRAST_TEXTURE_FILTER_LINEAR)
3003 k = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].lodarrayindex;
3004 mip_edge0tc[0] = (clipped[k][0][0] - clipped[k][1][0]) * texture->mipmap[0][2];
3005 mip_edge0tc[1] = (clipped[k][0][1] - clipped[k][1][1]) * texture->mipmap[0][3];
3006 mip_edge1tc[0] = (clipped[k][2][0] - clipped[k][1][0]) * texture->mipmap[0][2];
3007 mip_edge1tc[1] = (clipped[k][2][1] - clipped[k][1][1]) * texture->mipmap[0][3];
3008 mip_edge0mip = (mip_edge0tc[0]*mip_edge0tc[0]+mip_edge0tc[1]*mip_edge0tc[1]) * mip_edge0xymul;
3009 mip_edge1mip = (mip_edge1tc[0]*mip_edge1tc[0]+mip_edge1tc[1]*mip_edge1tc[1]) * mip_edge1xymul;
3010 // this will be multiplied in the texturing routine by the texture resolution
3011 mipdensity = mip_edge0mip < mip_edge1mip ? mip_edge0mip : mip_edge1mip;
3012 y = (int)(log(mipdensity)/log(2.0f));
3015 if (y > texture->mipmaps - 1)
3016 y = texture->mipmaps - 1;
3020 // iterate potential spans
3021 // TODO: optimize? if we figured out the edge order beforehand, this
3022 // could do loops over the edges in the proper order rather than
3023 // selecting them for each span
3024 // TODO: optimize? the edges could have data slopes calculated
3025 // TODO: optimize? the data slopes could be calculated as a plane
3026 // (2D slopes) to avoid any interpolation along edges at all
3027 for (y = extent[1];y < extent[3];y++)
3029 // get center of pixel y
3031 // do the compares all at once
3032 screenyless[0] = y <= screeny[0];
3033 screenyless[1] = y <= screeny[1];
3034 screenyless[2] = y <= screeny[2];
3035 screenyless[3] = y <= screeny[3];
3038 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4 + screenyless[3] * 8)
3040 case 0: /*0000*/ continue;
3041 case 1: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 0;edge1n = 1;break;
3042 case 2: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
3043 case 3: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 2;break;
3044 case 4: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break;
3045 case 5: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 3;break; // concave - nonsense
3046 case 6: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 3;break;
3047 case 7: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 3;break;
3048 case 8: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 3;edge1n = 0;break;
3049 case 9: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 1;break;
3050 case 10: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
3051 case 11: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break;
3052 case 12: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 0;break;
3053 case 13: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
3054 case 14: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 0;break;
3055 case 15: /*1111*/ continue;
3060 switch(screenyless[0] + screenyless[1] * 2 + screenyless[2] * 4)
3062 case 0: /*000*/ continue;
3063 case 1: /*100*/ edge0p = 2;edge0n = 0;edge1p = 0;edge1n = 1;break;
3064 case 2: /*010*/ edge0p = 0;edge0n = 1;edge1p = 1;edge1n = 2;break;
3065 case 3: /*110*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 2;break;
3066 case 4: /*001*/ edge0p = 1;edge0n = 2;edge1p = 2;edge1n = 0;break;
3067 case 5: /*101*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 1;break;
3068 case 6: /*011*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 0;break;
3069 case 7: /*111*/ continue;
3079 for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
3081 if (screenyless[k] && !screenyless[j])
3087 else if (screenyless[j] && !screenyless[k])
3094 if (foundedges != 3)
3096 if (cedge0p != edge0p || cedge0n != edge0n || cedge1p != edge1p || cedge1n != edge1n)
3099 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);
3101 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);
3105 edge0ylerp = (yc - screen[edge0p][1]) / (screen[edge0n][1] - screen[edge0p][1]);
3106 edge1ylerp = (yc - screen[edge1p][1]) / (screen[edge1n][1] - screen[edge1p][1]);
3107 if (edge0ylerp < 0 || edge0ylerp > 1 || edge1ylerp < 0 || edge1ylerp > 1)
3109 edge0yilerp = 1.0f - edge0ylerp;
3110 edge1yilerp = 1.0f - edge1ylerp;
3111 edge0xf = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3112 edge1xf = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3113 if (edge0xf < edge1xf)
3123 startx = (int)ceil(startxf);
3124 endx = (int)ceil(endxf);
3131 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); }
3132 spanilength = 1.0f / (endxf - startxf);
3133 startxlerp = startx - startxf;
3134 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3135 memcpy(span->mip, mip, sizeof(span->mip));
3136 span->start = y * width + startx;
3137 span->length = endx - startx;
3138 j = DPSOFTRAST_ARRAY_TOTAL;
3139 if (edge0xf < edge1xf)
3141 span->data[0][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3142 span->data[0][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
3143 span->data[0][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
3144 span->data[0][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
3145 span->data[1][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3146 span->data[1][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
3147 span->data[1][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
3148 span->data[1][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
3149 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3153 span->data[0][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
3154 span->data[0][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
3155 span->data[0][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
3156 span->data[0][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
3157 span->data[1][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
3158 span->data[1][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
3159 span->data[1][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
3160 span->data[1][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
3166 span->data[0][j][0] = screen[edge1p][0] * edge1yilerp + screen[edge1n][0] * edge1ylerp;
3167 span->data[0][j][1] = screen[edge1p][1] * edge1yilerp + screen[edge1n][1] * edge1ylerp;
3168 span->data[0][j][2] = screen[edge1p][2] * edge1yilerp + screen[edge1n][2] * edge1ylerp;
3169 span->data[0][j][3] = screen[edge1p][3] * edge1yilerp + screen[edge1n][3] * edge1ylerp;
3170 span->data[1][j][0] = screen[edge0p][0] * edge0yilerp + screen[edge0n][0] * edge0ylerp;
3171 span->data[1][j][1] = screen[edge0p][1] * edge0yilerp + screen[edge0n][1] * edge0ylerp;
3172 span->data[1][j][2] = screen[edge0p][2] * edge0yilerp + screen[edge0n][2] * edge0ylerp;
3173 span->data[1][j][3] = screen[edge0p][3] * edge0yilerp + screen[edge0n][3] * edge0ylerp;
3174 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3178 span->data[0][j][0] = proj[j][edge1p][0] * edge1yilerp + proj[j][edge1n][0] * edge1ylerp;
3179 span->data[0][j][1] = proj[j][edge1p][1] * edge1yilerp + proj[j][edge1n][1] * edge1ylerp;
3180 span->data[0][j][2] = proj[j][edge1p][2] * edge1yilerp + proj[j][edge1n][2] * edge1ylerp;
3181 span->data[0][j][3] = proj[j][edge1p][3] * edge1yilerp + proj[j][edge1n][3] * edge1ylerp;
3182 span->data[1][j][0] = proj[j][edge0p][0] * edge0yilerp + proj[j][edge0n][0] * edge0ylerp;
3183 span->data[1][j][1] = proj[j][edge0p][1] * edge0yilerp + proj[j][edge0n][1] * edge0ylerp;
3184 span->data[1][j][2] = proj[j][edge0p][2] * edge0yilerp + proj[j][edge0n][2] * edge0ylerp;
3185 span->data[1][j][3] = proj[j][edge0p][3] * edge0yilerp + proj[j][edge0n][3] * edge0ylerp;
3189 // change data[1][n][] to be a data slope
3190 j = DPSOFTRAST_ARRAY_TOTAL;
3191 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
3192 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
3193 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
3194 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
3195 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3199 span->data[1][j][0] = (span->data[1][j][0] - span->data[0][j][0]) * spanilength;
3200 span->data[1][j][1] = (span->data[1][j][1] - span->data[0][j][1]) * spanilength;
3201 span->data[1][j][2] = (span->data[1][j][2] - span->data[0][j][2]) * spanilength;
3202 span->data[1][j][3] = (span->data[1][j][3] - span->data[0][j][3]) * spanilength;
3205 // adjust the data[0][n][] to be correct for the pixel centers
3206 // this also handles horizontal clipping where a major part of the
3207 // span may be off the left side of the screen
3208 j = DPSOFTRAST_ARRAY_TOTAL;
3209 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
3210 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
3211 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
3212 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
3213 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3217 span->data[0][j][0] += span->data[1][j][0] * startxlerp;
3218 span->data[0][j][1] += span->data[1][j][1] * startxlerp;
3219 span->data[0][j][2] += span->data[1][j][2] * startxlerp;
3220 span->data[0][j][3] += span->data[1][j][3] * startxlerp;
3223 // to keep the shader routines from needing more than a small
3224 // buffer for pixel intermediate data, we split long spans...
3225 while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
3227 span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
3228 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3230 DPSOFTRAST_Draw_ProcessSpans();
3231 dpsoftrast.draw.numspans = 0;
3234 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3236 startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
3237 span->start = y * width + startx;
3238 span->length = endx - startx;
3239 j = DPSOFTRAST_ARRAY_TOTAL;
3240 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3241 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3242 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3243 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3244 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3248 span->data[0][j][0] += span->data[1][j][0] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3249 span->data[0][j][1] += span->data[1][j][1] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3250 span->data[0][j][2] += span->data[1][j][2] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3251 span->data[0][j][3] += span->data[1][j][3] * DPSOFTRAST_DRAW_MAXSPANLENGTH;
3255 // after all that, we have a span suitable for the pixel shader...
3256 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3258 DPSOFTRAST_Draw_ProcessSpans();
3259 dpsoftrast.draw.numspans = 0;
3262 // draw outlines over triangle for debugging
3263 // for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
3264 // DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
3266 if (dpsoftrast.draw.numspans)
3268 DPSOFTRAST_Draw_ProcessSpans();
3269 dpsoftrast.draw.numspans = 0;
3273 void DPSOFTRAST_Draw_DebugPoints(void)
3278 int numvertices = dpsoftrast.draw.numvertices;
3279 int w = dpsoftrast.fb_width;
3281 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
3283 bounds[0] = dpsoftrast.fb_viewportscissor[0];
3284 bounds[1] = dpsoftrast.fb_viewportscissor[1];
3285 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
3286 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
3287 for (i = 0;i < numvertices;i++)
3290 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
3292 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
3293 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
3294 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
3295 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
3296 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
3297 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
3298 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
3300 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
3301 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
3305 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
3307 unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL];
3308 arraymask[0] = true;
3309 arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode)
3310 arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL;
3311 arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL;
3312 arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL;
3313 arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL;
3314 arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL;
3315 arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL;
3316 arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL;
3317 arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL;
3318 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
3319 DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true);
3320 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Vertex();
3321 DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
3322 DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numvertices, numtriangles, element3i, element3s, arraymask);
3325 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
3334 memset(&dpsoftrast, 0, sizeof(dpsoftrast));
3335 dpsoftrast.bigendian = u.b[3];
3336 dpsoftrast.fb_width = width;
3337 dpsoftrast.fb_height = height;
3338 dpsoftrast.fb_depthpixels = depthpixels;
3339 dpsoftrast.fb_colorpixels[0] = colorpixels;
3340 dpsoftrast.fb_colorpixels[1] = NULL;
3341 dpsoftrast.fb_colorpixels[1] = NULL;
3342 dpsoftrast.fb_colorpixels[1] = NULL;
3343 dpsoftrast.texture_firstfree = 1;
3344 dpsoftrast.texture_end = 1;
3345 dpsoftrast.texture_max = 0;
3346 dpsoftrast.user.colormask[0] = 1;
3347 dpsoftrast.user.colormask[1] = 1;
3348 dpsoftrast.user.colormask[2] = 1;
3349 dpsoftrast.user.colormask[3] = 1;
3350 dpsoftrast.user.blendfunc[0] = GL_ONE;
3351 dpsoftrast.user.blendfunc[1] = GL_ZERO;
3352 dpsoftrast.user.depthmask = true;
3353 dpsoftrast.user.depthtest = true;
3354 dpsoftrast.user.depthfunc = GL_LEQUAL;
3355 dpsoftrast.user.scissortest = false;
3356 dpsoftrast.user.cullface = GL_BACK;
3357 dpsoftrast.user.alphatest = false;
3358 dpsoftrast.user.alphafunc = GL_GREATER;
3359 dpsoftrast.user.alphavalue = 0.5f;
3360 dpsoftrast.user.scissor[0] = 0;
3361 dpsoftrast.user.scissor[1] = 0;
3362 dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
3363 dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
3364 dpsoftrast.user.viewport[0] = 0;
3365 dpsoftrast.user.viewport[1] = 0;
3366 dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
3367 dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
3368 dpsoftrast.user.depthrange[0] = 0;
3369 dpsoftrast.user.depthrange[1] = 1;
3370 dpsoftrast.user.polygonoffset[0] = 0;
3371 dpsoftrast.user.polygonoffset[1] = 0;
3372 dpsoftrast.user.color[0] = 1;
3373 dpsoftrast.user.color[1] = 1;
3374 dpsoftrast.user.color[2] = 1;
3375 dpsoftrast.user.color[3] = 1;
3376 dpsoftrast.validate = -1;
3377 DPSOFTRAST_Validate(-1);
3378 dpsoftrast.validate = 0;
3381 void DPSOFTRAST_Shutdown(void)
3384 for (i = 0;i < dpsoftrast.texture_end;i++)
3385 if (dpsoftrast.texture[i].bytes)
3386 free(dpsoftrast.texture[i].bytes);
3387 if (dpsoftrast.texture)
3388 free(dpsoftrast.texture);
3389 memset(&dpsoftrast, 0, sizeof(dpsoftrast));