6 #include "dpsoftrast.h"
13 #define ALIGN(var) var __attribute__((__aligned__(16)))
14 #elif defined(_MSC_VER)
15 #define ALIGN(var) __declspec(align(16)) var
17 #define ALIGN(var) var
21 #include <emmintrin.h>
23 #define MM_MALLOC(size) _mm_malloc(size, 16)
25 static void *MM_CALLOC(size_t nmemb, size_t size)
27 void *ptr = _mm_malloc(nmemb*size, 16);
28 if(ptr != NULL) memset(ptr, 0, nmemb*size);
32 #define MM_FREE _mm_free
34 #define MM_MALLOC(size) malloc(size)
35 #define MM_CALLOC(nmemb, size) calloc(nmemb, size)
39 typedef enum DPSOFTRAST_ARRAY_e
41 DPSOFTRAST_ARRAY_POSITION,
42 DPSOFTRAST_ARRAY_COLOR,
43 DPSOFTRAST_ARRAY_TEXCOORD0,
44 DPSOFTRAST_ARRAY_TEXCOORD1,
45 DPSOFTRAST_ARRAY_TEXCOORD2,
46 DPSOFTRAST_ARRAY_TEXCOORD3,
47 DPSOFTRAST_ARRAY_TEXCOORD4,
48 DPSOFTRAST_ARRAY_TEXCOORD5,
49 DPSOFTRAST_ARRAY_TEXCOORD6,
50 DPSOFTRAST_ARRAY_TEXCOORD7,
51 DPSOFTRAST_ARRAY_TOTAL
55 typedef struct DPSOFTRAST_Texture_s
62 DPSOFTRAST_TEXTURE_FILTER filter;
66 int mipmap[DPSOFTRAST_MAXMIPMAPS][5];
70 typedef struct DPSOFTRAST_State_User_s
86 float polygonoffset[2];
89 DPSOFTRAST_State_User;
91 #define DPSOFTRAST_MAXSUBSPAN 16
93 typedef ALIGN(struct DPSOFTRAST_State_Draw_Span_s
95 int start; // pixel index
96 int length; // pixel count
97 int startx; // usable range (according to pixelmask)
98 int endx; // usable range (according to pixelmask)
99 unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; // texcoord to screen space density values (for picking mipmap of textures)
100 unsigned char *pixelmask; // true for pixels that passed depth test, false for others
101 // [0][n][] is start interpolant values (projected)
102 // [1][n][] is end interpolant values (projected)
103 // [0][DPSOFTRAST_ARRAY_TOTAL][] is start screencoord4f
104 // [1][DPSOFTRAST_ARRAY_TOTAL][] is end screencoord4f
105 // NOTE: screencoord4f[3] is W (basically 1/Z), useful for depthbuffer
106 ALIGN(float data[2][DPSOFTRAST_ARRAY_TOTAL+1][4]);
108 DPSOFTRAST_State_Draw_Span);
110 #define DPSOFTRAST_DRAW_MAXSPANQUEUE 1024
112 typedef struct DPSOFTRAST_State_Draw_s
116 float *in_array4f[DPSOFTRAST_ARRAY_TOTAL];
117 float *post_array4f[DPSOFTRAST_ARRAY_TOTAL];
118 float *screencoord4f;
120 // spans are queued in this structure for dispatch to the pixel shader,
121 // partly to improve cache locality, partly for batching purposes, spans
122 // are flushed before DrawTriangles returns to caller
124 DPSOFTRAST_State_Draw_Span spanqueue[DPSOFTRAST_DRAW_MAXSPANQUEUE];
126 DPSOFTRAST_State_Draw;
128 #define DPSOFTRAST_VALIDATE_FB 1
129 #define DPSOFTRAST_VALIDATE_DEPTHFUNC 2
130 #define DPSOFTRAST_VALIDATE_BLENDFUNC 4
131 #define DPSOFTRAST_VALIDATE_DRAW (DPSOFTRAST_VALIDATE_FB | DPSOFTRAST_VALIDATE_DEPTHFUNC | DPSOFTRAST_VALIDATE_BLENDFUNC)
133 typedef enum DPSOFTRAST_BLENDMODE_e
135 DPSOFTRAST_BLENDMODE_OPAQUE,
136 DPSOFTRAST_BLENDMODE_ALPHA,
137 DPSOFTRAST_BLENDMODE_ADDALPHA,
138 DPSOFTRAST_BLENDMODE_ADD,
139 DPSOFTRAST_BLENDMODE_INVMOD,
140 DPSOFTRAST_BLENDMODE_MUL,
141 DPSOFTRAST_BLENDMODE_MUL2,
142 DPSOFTRAST_BLENDMODE_SUBALPHA,
143 DPSOFTRAST_BLENDMODE_PSEUDOALPHA,
144 DPSOFTRAST_BLENDMODE_TOTAL
146 DPSOFTRAST_BLENDMODE;
148 typedef ALIGN(struct DPSOFTRAST_State_s
150 // DPSOFTRAST_VALIDATE_ flags
156 unsigned int *fb_depthpixels;
157 unsigned int *fb_colorpixels[4];
159 const float *pointer_vertex3f;
160 const float *pointer_color4f;
161 const unsigned char *pointer_color4ub;
162 const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
165 int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
166 int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
167 DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
170 int shader_permutation;
171 ALIGN(float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4]);
172 int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
174 // derived values (DPSOFTRAST_VALIDATE_FB)
175 int fb_clearscissor[4];
177 int fb_viewportscissor[4];
178 ALIGN(float fb_viewportcenter[4]);
179 ALIGN(float fb_viewportscale[4]);
181 // derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
184 // derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
189 int texture_firstfree;
190 DPSOFTRAST_Texture *texture;
195 const char *errorstring;
197 DPSOFTRAST_State_User user;
199 DPSOFTRAST_State_Draw draw;
203 DPSOFTRAST_State dpsoftrast;
205 extern int dpsoftrast_test;
207 #define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
208 #define DPSOFTRAST_DEPTHOFFSET (128.0f)
209 #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))
210 #define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
211 #define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
213 void DPSOFTRAST_RecalcFB(void)
215 // calculate framebuffer scissor, viewport, viewport clipped by scissor,
216 // and viewport projection values
217 int x1, x2, x3, x4, x5, x6;
218 int y1, y2, y3, y4, y5, y6;
219 x1 = dpsoftrast.user.scissor[0];
220 x2 = dpsoftrast.user.scissor[0] + dpsoftrast.user.scissor[2];
221 x3 = dpsoftrast.user.viewport[0];
222 x4 = dpsoftrast.user.viewport[0] + dpsoftrast.user.viewport[2];
223 y1 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1] - dpsoftrast.user.scissor[3];
224 y2 = dpsoftrast.fb_height - dpsoftrast.user.scissor[1];
225 y3 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - dpsoftrast.user.viewport[3];
226 y4 = dpsoftrast.fb_height - dpsoftrast.user.viewport[1];
227 if (!dpsoftrast.user.scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
229 if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
231 if (x4 > dpsoftrast.fb_width) x4 = dpsoftrast.fb_width;
233 if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
235 if (y4 > dpsoftrast.fb_height) y4 = dpsoftrast.fb_height;
236 x5 = x1;if (x5 < x3) x5 = x3;
237 x6 = x2;if (x6 > x4) x4 = x4;
238 y5 = y1;if (y5 < y3) y5 = y3;
239 y6 = y2;if (y6 > y4) y6 = y4;
240 dpsoftrast.fb_clearscissor[0] = x1;
241 dpsoftrast.fb_clearscissor[1] = y1;
242 dpsoftrast.fb_clearscissor[2] = x2 - x1;
243 dpsoftrast.fb_clearscissor[3] = y2 - y1;
244 dpsoftrast.fb_viewport[0] = x3;
245 dpsoftrast.fb_viewport[1] = y3;
246 dpsoftrast.fb_viewport[2] = x4 - x3;
247 dpsoftrast.fb_viewport[3] = y4 - y3;
248 dpsoftrast.fb_viewportscissor[0] = x5;
249 dpsoftrast.fb_viewportscissor[1] = y5;
250 dpsoftrast.fb_viewportscissor[2] = x6 - x5;
251 dpsoftrast.fb_viewportscissor[3] = y6 - y5;
252 dpsoftrast.fb_viewportcenter[1] = dpsoftrast.user.viewport[0] + 0.5f * dpsoftrast.user.viewport[2] - 0.5f;
253 dpsoftrast.fb_viewportcenter[2] = dpsoftrast.fb_height - dpsoftrast.user.viewport[1] - 0.5f * dpsoftrast.user.viewport[3] - 0.5f;
254 dpsoftrast.fb_viewportcenter[3] = 0.5f;
255 dpsoftrast.fb_viewportcenter[0] = 0.0f;
256 dpsoftrast.fb_viewportscale[1] = 0.5f * dpsoftrast.user.viewport[2];
257 dpsoftrast.fb_viewportscale[2] = -0.5f * dpsoftrast.user.viewport[3];
258 dpsoftrast.fb_viewportscale[3] = 0.5f;
259 dpsoftrast.fb_viewportscale[0] = 1.0f;
262 void DPSOFTRAST_RecalcDepthFunc(void)
264 dpsoftrast.fb_depthfunc = dpsoftrast.user.depthtest ? dpsoftrast.user.depthfunc : GL_ALWAYS;
267 int blendmodetable[][4] =
269 {DPSOFTRAST_BLENDMODE_OPAQUE, GL_ONE, GL_ZERO, false},
270 {DPSOFTRAST_BLENDMODE_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false},
271 {DPSOFTRAST_BLENDMODE_ADDALPHA, GL_SRC_ALPHA, GL_ONE, false},
272 {DPSOFTRAST_BLENDMODE_ADD, GL_ONE, GL_ONE, false},
273 {DPSOFTRAST_BLENDMODE_INVMOD, GL_ZERO, GL_ONE_MINUS_SRC_COLOR, false},
274 {DPSOFTRAST_BLENDMODE_MUL, GL_ZERO, GL_SRC_COLOR, false},
275 {DPSOFTRAST_BLENDMODE_MUL, GL_DST_COLOR, GL_ZERO, false},
276 {DPSOFTRAST_BLENDMODE_MUL2, GL_DST_COLOR, GL_SRC_COLOR, false},
277 {DPSOFTRAST_BLENDMODE_PSEUDOALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, false},
278 {DPSOFTRAST_BLENDMODE_SUBALPHA, GL_SRC_COLOR, GL_ONE, true}
281 void DPSOFTRAST_RecalcBlendFunc(void)
284 dpsoftrast.fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE;
285 for (i = 0;i < (int)(sizeof(blendmodetable) / sizeof(blendmodetable[0]));i++)
287 if (dpsoftrast.user.blendfunc[0] == blendmodetable[i][1] && dpsoftrast.user.blendfunc[1] == blendmodetable[i][2] && dpsoftrast.user.blendsubtract == blendmodetable[i][3])
289 dpsoftrast.fb_blendmode = blendmodetable[i][0];
295 #define DPSOFTRAST_ValidateQuick(f) ((dpsoftrast.validate & (f)) ? (DPSOFTRAST_Validate(f), 0) : 0)
297 void DPSOFTRAST_Validate(int mask)
299 mask &= dpsoftrast.validate;
302 if (mask & DPSOFTRAST_VALIDATE_FB)
304 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_FB;
305 DPSOFTRAST_RecalcFB();
307 if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
309 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
310 DPSOFTRAST_RecalcDepthFunc();
312 if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
314 dpsoftrast.validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
315 DPSOFTRAST_RecalcBlendFunc();
319 DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
321 if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
322 return &dpsoftrast.texture[index];
326 int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
335 int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
336 int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
337 DPSOFTRAST_Texture *texture;
338 if (width*height*depth < 1)
340 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
343 if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
345 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
350 case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
351 case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
352 case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
354 case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
355 if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
357 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
362 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
365 if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
367 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
372 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
374 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
377 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
379 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
382 if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
384 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
387 if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
389 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
392 if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
394 dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
397 // find first empty slot in texture array
398 for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
399 if (!dpsoftrast.texture[texnum].bytes)
401 dpsoftrast.texture_firstfree = texnum + 1;
402 if (dpsoftrast.texture_max <= texnum)
404 // expand texture array as needed
405 if (dpsoftrast.texture_max < 1024)
406 dpsoftrast.texture_max = 1024;
408 dpsoftrast.texture_max *= 2;
409 dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
411 if (dpsoftrast.texture_end <= texnum)
412 dpsoftrast.texture_end = texnum + 1;
413 texture = &dpsoftrast.texture[texnum];
414 memset(texture, 0, sizeof(*texture));
415 texture->flags = flags;
416 texture->width = width;
417 texture->height = height;
418 texture->depth = depth;
419 texture->sides = sides;
430 s = w * h * d * sides * 4;
431 texture->mipmap[mipmaps][0] = size;
432 texture->mipmap[mipmaps][1] = s;
433 texture->mipmap[mipmaps][2] = w;
434 texture->mipmap[mipmaps][3] = h;
435 texture->mipmap[mipmaps][4] = d;
438 if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
444 texture->mipmaps = mipmaps;
445 texture->size = size;
447 // allocate the pixels now
448 texture->bytes = (unsigned char *)MM_CALLOC(1, size);
452 void DPSOFTRAST_Texture_Free(int index)
454 DPSOFTRAST_Texture *texture;
455 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
457 MM_FREE(texture->bytes);
458 texture->bytes = NULL;
459 memset(texture, 0, sizeof(*texture));
460 // adjust the free range and used range
461 if (dpsoftrast.texture_firstfree > index)
462 dpsoftrast.texture_firstfree = index;
463 while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
464 dpsoftrast.texture_end--;
466 void DPSOFTRAST_Texture_CalculateMipmaps(int index)
468 int i, x, y, z, w, layer0, layer1, row0, row1;
469 unsigned char *o, *i0, *i1, *i2, *i3;
470 DPSOFTRAST_Texture *texture;
471 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
472 if (texture->mipmaps <= 1)
474 for (i = 1;i < texture->mipmaps;i++)
476 for (z = 0;z < texture->mipmap[i][4];z++)
480 if (layer1 >= texture->mipmap[i-1][4])
481 layer1 = texture->mipmap[i-1][4]-1;
482 for (y = 0;y < texture->mipmap[i][3];y++)
486 if (row1 >= texture->mipmap[i-1][3])
487 row1 = texture->mipmap[i-1][3]-1;
488 o = texture->bytes + texture->mipmap[i ][0] + 4*((texture->mipmap[i ][3] * z + y ) * texture->mipmap[i ][2]);
489 i0 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row0) * texture->mipmap[i-1][2]);
490 i1 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row1) * texture->mipmap[i-1][2]);
491 i2 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row0) * texture->mipmap[i-1][2]);
492 i3 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row1) * texture->mipmap[i-1][2]);
493 w = texture->mipmap[i][2];
496 if (texture->mipmap[i-1][2] > 1)
498 // average 3D texture
499 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8, i2 += 8, i3 += 8)
501 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
502 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
503 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
504 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
509 // average 3D mipmap with parent width == 1
510 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
512 o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
513 o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
514 o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
515 o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
521 if (texture->mipmap[i-1][2] > 1)
523 // average 2D texture (common case)
524 for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
526 o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
527 o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
528 o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
529 o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
534 // 2D texture with parent width == 1
535 o[0] = (i0[0] + i1[0] + 1) >> 1;
536 o[1] = (i0[1] + i1[1] + 1) >> 1;
537 o[2] = (i0[2] + i1[2] + 1) >> 1;
538 o[3] = (i0[3] + i1[3] + 1) >> 1;
545 void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
547 DPSOFTRAST_Texture *texture;
548 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
552 dpsoftrast.errorstring = "DPSOFTRAST_Texture_UpdatePartial: Not implemented.";
554 void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
556 DPSOFTRAST_Texture *texture;
557 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
559 memcpy(texture->bytes, pixels, texture->mipmap[0][1]);
560 DPSOFTRAST_Texture_CalculateMipmaps(index);
562 int DPSOFTRAST_Texture_GetWidth(int index, int mip)
564 DPSOFTRAST_Texture *texture;
565 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
566 return texture->mipmap[mip][2];
568 int DPSOFTRAST_Texture_GetHeight(int index, int mip)
570 DPSOFTRAST_Texture *texture;
571 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
572 return texture->mipmap[mip][3];
574 int DPSOFTRAST_Texture_GetDepth(int index, int mip)
576 DPSOFTRAST_Texture *texture;
577 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
578 return texture->mipmap[mip][4];
580 unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
582 DPSOFTRAST_Texture *texture;
583 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
584 return texture->bytes + texture->mipmap[mip][0];
586 void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
588 DPSOFTRAST_Texture *texture;
589 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
590 if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
592 dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
595 texture->filter = filter;
598 void DPSOFTRAST_SetRenderTargets(int width, int height, unsigned int *depthpixels, unsigned int *colorpixels0, unsigned int *colorpixels1, unsigned int *colorpixels2, unsigned int *colorpixels3)
600 dpsoftrast.fb_width = width;
601 dpsoftrast.fb_height = height;
602 dpsoftrast.fb_depthpixels = depthpixels;
603 dpsoftrast.fb_colorpixels[0] = colorpixels0;
604 dpsoftrast.fb_colorpixels[1] = colorpixels1;
605 dpsoftrast.fb_colorpixels[2] = colorpixels2;
606 dpsoftrast.fb_colorpixels[3] = colorpixels3;
608 void DPSOFTRAST_Viewport(int x, int y, int width, int height)
610 dpsoftrast.user.viewport[0] = x;
611 dpsoftrast.user.viewport[1] = y;
612 dpsoftrast.user.viewport[2] = width;
613 dpsoftrast.user.viewport[3] = height;
614 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
616 void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
618 int i, x1, y1, x2, y2, w, h, x, y;
621 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
622 x1 = dpsoftrast.fb_clearscissor[0];
623 y1 = dpsoftrast.fb_clearscissor[1];
624 x2 = dpsoftrast.fb_clearscissor[2];
625 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
630 // FIXME: honor dpsoftrast.fb_colormask?
631 c = DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a);
632 for (i = 0;i < 4;i++)
634 if (!dpsoftrast.fb_colorpixels[i])
636 for (y = y1;y < y2;y++)
638 p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
639 for (x = x1;x < x2;x++)
644 void DPSOFTRAST_ClearDepth(float d)
646 int x1, y1, x2, y2, w, h, x, y;
649 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_FB);
650 x1 = dpsoftrast.fb_clearscissor[0];
651 y1 = dpsoftrast.fb_clearscissor[1];
652 x2 = dpsoftrast.fb_clearscissor[2];
653 y2 = dpsoftrast.fb_clearscissor[1] + dpsoftrast.fb_clearscissor[3];
658 c = DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d);
659 for (y = y1;y < y2;y++)
661 p = dpsoftrast.fb_depthpixels + y * dpsoftrast.fb_width;
662 for (x = x1;x < x2;x++)
666 void DPSOFTRAST_ColorMask(int r, int g, int b, int a)
668 dpsoftrast.user.colormask[0] = r != 0;
669 dpsoftrast.user.colormask[1] = g != 0;
670 dpsoftrast.user.colormask[2] = b != 0;
671 dpsoftrast.user.colormask[3] = a != 0;
672 dpsoftrast.fb_colormask = ((-dpsoftrast.user.colormask[0]) & 0x00FF0000) | ((-dpsoftrast.user.colormask[1]) & 0x0000FF00) | ((-dpsoftrast.user.colormask[2]) & 0x000000FF) | ((-dpsoftrast.user.colormask[3]) & 0xFF000000);
674 void DPSOFTRAST_DepthTest(int enable)
676 dpsoftrast.user.depthtest = enable;
677 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_DEPTHFUNC;
679 void DPSOFTRAST_ScissorTest(int enable)
681 dpsoftrast.user.scissortest = enable;
682 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
684 void DPSOFTRAST_Scissor(float x, float y, float width, float height)
686 dpsoftrast.user.scissor[0] = x;
687 dpsoftrast.user.scissor[1] = y;
688 dpsoftrast.user.scissor[2] = width;
689 dpsoftrast.user.scissor[3] = height;
690 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_FB;
693 void DPSOFTRAST_BlendFunc(int smodulate, int dmodulate)
696 dpsoftrast.user.blendfunc[0] = smodulate;
697 dpsoftrast.user.blendfunc[1] = dmodulate;
698 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
700 void DPSOFTRAST_BlendSubtract(int enable)
702 dpsoftrast.user.blendsubtract = enable != 0;
703 dpsoftrast.validate |= DPSOFTRAST_VALIDATE_BLENDFUNC;
705 void DPSOFTRAST_DepthMask(int enable)
707 dpsoftrast.user.depthmask = enable;
709 void DPSOFTRAST_DepthFunc(int comparemode)
712 dpsoftrast.user.depthfunc = comparemode;
714 void DPSOFTRAST_DepthRange(float range0, float range1)
716 dpsoftrast.user.depthrange[0] = range0;
717 dpsoftrast.user.depthrange[1] = range1;
719 void DPSOFTRAST_PolygonOffset(float alongnormal, float intoview)
721 dpsoftrast.user.polygonoffset[0] = alongnormal;
722 dpsoftrast.user.polygonoffset[1] = intoview;
724 void DPSOFTRAST_CullFace(int mode)
727 dpsoftrast.user.cullface = mode;
729 void DPSOFTRAST_AlphaTest(float enable)
731 dpsoftrast.user.alphatest = enable;
733 void DPSOFTRAST_AlphaFunc(int alphafunc, float alphavalue)
736 dpsoftrast.user.alphafunc = alphafunc;
737 dpsoftrast.user.alphavalue = alphavalue;
739 void DPSOFTRAST_Color4f(float r, float g, float b, float a)
741 dpsoftrast.user.color[0] = r;
742 dpsoftrast.user.color[1] = g;
743 dpsoftrast.user.color[2] = b;
744 dpsoftrast.user.color[3] = a;
746 void DPSOFTRAST_GetPixelsBGRA(int blockx, int blocky, int blockwidth, int blockheight, unsigned char *outpixels)
748 int outstride = blockwidth * 4;
749 int instride = dpsoftrast.fb_width * 4;
752 int bx2 = blockx + blockwidth;
753 int by2 = blocky + blockheight;
758 unsigned char *inpixels;
761 if (bx1 < 0) bx1 = 0;
762 if (by1 < 0) by1 = 0;
763 if (bx2 > dpsoftrast.fb_width) bx2 = dpsoftrast.fb_width;
764 if (by2 > dpsoftrast.fb_height) by2 = dpsoftrast.fb_height;
767 inpixels = (unsigned char *)dpsoftrast.fb_colorpixels[0];
768 if (dpsoftrast.bigendian)
770 for (y = by1;y < by2;y++)
772 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
773 o = (unsigned char *)outpixels + (y - by1) * outstride;
774 for (x = bx1;x < bx2;x++)
787 for (y = by1;y < by2;y++)
789 b = (unsigned char *)inpixels + (dpsoftrast.fb_height - 1 - y) * instride + 4 * bx1;
790 o = (unsigned char *)outpixels + (y - by1) * outstride;
796 void DPSOFTRAST_CopyRectangleToTexture(int index, int mip, int tx, int ty, int sx, int sy, int width, int height)
800 int tx2 = tx + width;
801 int ty2 = ty + height;
804 int sx2 = sx + width;
805 int sy2 = sy + height;
815 unsigned int *spixels;
816 unsigned int *tpixels;
817 DPSOFTRAST_Texture *texture;
818 texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
819 if (mip < 0 || mip >= texture->mipmaps) return;
820 spixels = dpsoftrast.fb_colorpixels[0];
821 swidth = dpsoftrast.fb_width;
822 sheight = dpsoftrast.fb_height;
823 tpixels = (unsigned int *)(texture->bytes + texture->mipmap[mip][0]);
824 twidth = texture->mipmap[mip][2];
825 theight = texture->mipmap[mip][3];
826 if (tx1 < 0) tx1 = 0;
827 if (ty1 < 0) ty1 = 0;
828 if (tx2 > twidth) tx2 = twidth;
829 if (ty2 > theight) ty2 = theight;
830 if (sx1 < 0) sx1 = 0;
831 if (sy1 < 0) sy1 = 0;
832 if (sx2 > swidth) sx2 = swidth;
833 if (sy2 > sheight) sy2 = sheight;
838 if (tw > sw) tw = sw;
839 if (th > sh) th = sh;
840 if (tw < 1 || th < 1)
842 for (y = 0;y < th;y++)
843 memcpy(tpixels + ((ty1 + y) * twidth + tx1), spixels + ((sy1 + y) * swidth + sx1), tw*4);
844 if (texture->mipmaps > 1)
845 DPSOFTRAST_Texture_CalculateMipmaps(index);
847 void DPSOFTRAST_SetTexture(int unitnum, int index)
849 DPSOFTRAST_Texture *texture;
850 if (unitnum < 0 || unitnum >= DPSOFTRAST_MAXTEXTUREUNITS)
852 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid unit number";
855 texture = DPSOFTRAST_Texture_GetByIndex(index);
856 if (index && !texture)
858 dpsoftrast.errorstring = "DPSOFTRAST_SetTexture: invalid texture handle";
861 dpsoftrast.texbound[unitnum] = texture;
864 void DPSOFTRAST_SetVertexPointer(const float *vertex3f, size_t stride)
866 dpsoftrast.pointer_vertex3f = vertex3f;
867 dpsoftrast.stride_vertex = stride;
869 void DPSOFTRAST_SetColorPointer(const float *color4f, size_t stride)
871 dpsoftrast.pointer_color4f = color4f;
872 dpsoftrast.pointer_color4ub = NULL;
873 dpsoftrast.stride_color = stride;
875 void DPSOFTRAST_SetColorPointer4ub(const unsigned char *color4ub, size_t stride)
877 dpsoftrast.pointer_color4f = NULL;
878 dpsoftrast.pointer_color4ub = color4ub;
879 dpsoftrast.stride_color = stride;
881 void DPSOFTRAST_SetTexCoordPointer(int unitnum, int numcomponents, size_t stride, const float *texcoordf)
883 dpsoftrast.pointer_texcoordf[unitnum] = texcoordf;
884 dpsoftrast.components_texcoord[unitnum] = numcomponents;
885 dpsoftrast.stride_texcoord[unitnum] = stride;
888 void DPSOFTRAST_SetShader(unsigned int mode, unsigned int permutation)
890 dpsoftrast.shader_mode = mode;
891 dpsoftrast.shader_permutation = permutation;
893 void DPSOFTRAST_Uniform4fARB(DPSOFTRAST_UNIFORM index, float v0, float v1, float v2, float v3)
895 dpsoftrast.uniform4f[index*4+0] = v0;
896 dpsoftrast.uniform4f[index*4+1] = v1;
897 dpsoftrast.uniform4f[index*4+2] = v2;
898 dpsoftrast.uniform4f[index*4+3] = v3;
900 void DPSOFTRAST_Uniform4fvARB(DPSOFTRAST_UNIFORM index, const float *v)
902 dpsoftrast.uniform4f[index*4+0] = v[0];
903 dpsoftrast.uniform4f[index*4+1] = v[1];
904 dpsoftrast.uniform4f[index*4+2] = v[2];
905 dpsoftrast.uniform4f[index*4+3] = v[3];
907 void DPSOFTRAST_UniformMatrix4fvARB(DPSOFTRAST_UNIFORM index, int arraysize, int transpose, const float *v)
910 for (i = 0;i < arraysize;i++, index += 4, v += 16)
914 dpsoftrast.uniform4f[index*4+0] = v[0];
915 dpsoftrast.uniform4f[index*4+1] = v[4];
916 dpsoftrast.uniform4f[index*4+2] = v[8];
917 dpsoftrast.uniform4f[index*4+3] = v[12];
918 dpsoftrast.uniform4f[index*4+4] = v[1];
919 dpsoftrast.uniform4f[index*4+5] = v[5];
920 dpsoftrast.uniform4f[index*4+6] = v[9];
921 dpsoftrast.uniform4f[index*4+7] = v[13];
922 dpsoftrast.uniform4f[index*4+8] = v[2];
923 dpsoftrast.uniform4f[index*4+9] = v[6];
924 dpsoftrast.uniform4f[index*4+10] = v[10];
925 dpsoftrast.uniform4f[index*4+11] = v[14];
926 dpsoftrast.uniform4f[index*4+12] = v[3];
927 dpsoftrast.uniform4f[index*4+13] = v[7];
928 dpsoftrast.uniform4f[index*4+14] = v[11];
929 dpsoftrast.uniform4f[index*4+15] = v[15];
933 dpsoftrast.uniform4f[index*4+0] = v[0];
934 dpsoftrast.uniform4f[index*4+1] = v[1];
935 dpsoftrast.uniform4f[index*4+2] = v[2];
936 dpsoftrast.uniform4f[index*4+3] = v[3];
937 dpsoftrast.uniform4f[index*4+4] = v[4];
938 dpsoftrast.uniform4f[index*4+5] = v[5];
939 dpsoftrast.uniform4f[index*4+6] = v[6];
940 dpsoftrast.uniform4f[index*4+7] = v[7];
941 dpsoftrast.uniform4f[index*4+8] = v[8];
942 dpsoftrast.uniform4f[index*4+9] = v[9];
943 dpsoftrast.uniform4f[index*4+10] = v[10];
944 dpsoftrast.uniform4f[index*4+11] = v[11];
945 dpsoftrast.uniform4f[index*4+12] = v[12];
946 dpsoftrast.uniform4f[index*4+13] = v[13];
947 dpsoftrast.uniform4f[index*4+14] = v[14];
948 dpsoftrast.uniform4f[index*4+15] = v[15];
952 void DPSOFTRAST_Uniform1iARB(DPSOFTRAST_UNIFORM index, int i0)
954 dpsoftrast.uniform1i[index] = i0;
957 void DPSOFTRAST_Draw_LoadVertices(int firstvertex, int numvertices, bool needcolors)
965 const unsigned char *b;
966 dpsoftrast.draw.numvertices = numvertices;
967 if (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
969 if (dpsoftrast.draw.maxvertices < 4096)
970 dpsoftrast.draw.maxvertices = 4096;
971 while (dpsoftrast.draw.maxvertices < dpsoftrast.draw.numvertices)
972 dpsoftrast.draw.maxvertices *= 2;
973 if (dpsoftrast.draw.in_array4f[0])
974 MM_FREE(dpsoftrast.draw.in_array4f[0]);
975 data = (float *)MM_CALLOC(1, dpsoftrast.draw.maxvertices * sizeof(float[4])*(DPSOFTRAST_ARRAY_TOTAL*2 + 1));
976 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
977 dpsoftrast.draw.in_array4f[i] = data;
978 for (i = 0;i < DPSOFTRAST_ARRAY_TOTAL;i++, data += dpsoftrast.draw.maxvertices * 4)
979 dpsoftrast.draw.post_array4f[i] = data;
980 dpsoftrast.draw.screencoord4f = data;
981 data += dpsoftrast.draw.maxvertices * 4;
983 stride = dpsoftrast.stride_vertex;
984 v = (const float *)((unsigned char *)dpsoftrast.pointer_vertex3f + firstvertex * stride);
985 p = dpsoftrast.draw.in_array4f[0];
986 for (i = 0;i < numvertices;i++)
993 v = (const float *)((const unsigned char *)v + stride);
997 if (dpsoftrast.pointer_color4f)
999 stride = dpsoftrast.stride_color;
1000 v = (const float *)((const unsigned char *)dpsoftrast.pointer_color4f + firstvertex * stride);
1001 p = dpsoftrast.draw.in_array4f[1];
1002 for (i = 0;i < numvertices;i++)
1009 v = (const float *)((const unsigned char *)v + stride);
1012 else if (dpsoftrast.pointer_color4ub)
1014 stride = dpsoftrast.stride_color;
1015 b = (const unsigned char *)((const unsigned char *)dpsoftrast.pointer_color4ub + firstvertex * stride);
1016 p = dpsoftrast.draw.in_array4f[1];
1017 for (i = 0;i < numvertices;i++)
1019 p[0] = b[0] * (1.0f / 255.0f);
1020 p[1] = b[1] * (1.0f / 255.0f);
1021 p[2] = b[2] * (1.0f / 255.0f);
1022 p[3] = b[3] * (1.0f / 255.0f);
1024 b = (const unsigned char *)((const unsigned char *)b + stride);
1029 v = dpsoftrast.user.color;
1030 p = dpsoftrast.draw.in_array4f[1];
1031 for (i = 0;i < numvertices;i++)
1041 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL-2;j++)
1043 if (dpsoftrast.pointer_texcoordf[j])
1045 stride = dpsoftrast.stride_texcoord[j];
1046 v = (const float *)((const unsigned char *)dpsoftrast.pointer_texcoordf[j] + firstvertex * stride);
1047 p = dpsoftrast.draw.in_array4f[j+2];
1048 switch(dpsoftrast.components_texcoord[j])
1051 for (i = 0;i < numvertices;i++)
1058 v = (const float *)((const unsigned char *)v + stride);
1062 for (i = 0;i < numvertices;i++)
1069 v = (const float *)((const unsigned char *)v + stride);
1073 for (i = 0;i < numvertices;i++)
1080 v = (const float *)((const unsigned char *)v + stride);
1088 void DPSOFTRAST_Array_Transform(float *out4f, const float *in4f, int numitems, const float *inmatrix16f)
1090 static const float identitymatrix[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
1094 memcpy(matrix, inmatrix16f, sizeof(float[16]));
1095 if (!memcmp(identitymatrix, matrix, sizeof(float[16])))
1097 // fast case for identity matrix
1098 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1101 for (i = 0;i < numitems;i++, out4f += 4, in4f += 4)
1103 out4f[0] = in4f[0] * matrix[0][0] + in4f[1] * matrix[1][0] + in4f[2] * matrix[2][0] + in4f[3] * matrix[3][0];
1104 out4f[1] = in4f[0] * matrix[0][1] + in4f[1] * matrix[1][1] + in4f[2] * matrix[2][1] + in4f[3] * matrix[3][1];
1105 out4f[2] = in4f[0] * matrix[0][2] + in4f[1] * matrix[1][2] + in4f[2] * matrix[2][2] + in4f[3] * matrix[3][2];
1106 out4f[3] = in4f[0] * matrix[0][3] + in4f[1] * matrix[1][3] + in4f[2] * matrix[2][3] + in4f[3] * matrix[3][3];
1110 void DPSOFTRAST_Array_Copy(float *out4f, const float *in4f, int numitems)
1112 memcpy(out4f, in4f, numitems * sizeof(float[4]));
1116 static __m128 DPSOFTRAST_Draw_ProjectVertex(__m128 v)
1118 __m128 viewportcenter = _mm_load_ps(dpsoftrast.fb_viewportcenter), viewportscale = _mm_load_ps(dpsoftrast.fb_viewportscale);
1119 __m128 w = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3));
1120 v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set1_ps(1.0f));
1121 v = _mm_add_ps(viewportcenter, _mm_div_ps(_mm_mul_ps(viewportscale, v), w));
1122 v = _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1));
1127 void DPSOFTRAST_Draw_ProjectVertices(float *out4f, const float *in4f, int numitems)
1130 // NOTE: this is used both as a whole mesh transform function and a
1131 // per-triangle transform function (for clipped triangles), accordingly
1132 // it should not crash on divide by 0 but the result of divide by 0 is
1136 __m128 viewportcenter = _mm_load_ps(dpsoftrast.fb_viewportcenter), viewportscale = _mm_load_ps(dpsoftrast.fb_viewportscale);
1137 for (i = 0;i < numitems;i++)
1139 __m128 v = _mm_load_ps(in4f), w = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 3, 3, 3));
1140 v = _mm_move_ss(_mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 1, 0, 3)), _mm_set1_ps(1.0f));
1141 v = _mm_add_ps(viewportcenter, _mm_div_ps(_mm_mul_ps(viewportscale, v), w));
1142 _mm_store_ps(out4f, _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1)));
1149 void DPSOFTRAST_Draw_DebugEdgePoints(const float *screen0, const float *screen1)
1154 int w = dpsoftrast.fb_width;
1157 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
1159 bounds[0] = dpsoftrast.fb_viewportscissor[0];
1160 bounds[1] = dpsoftrast.fb_viewportscissor[1];
1161 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
1162 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
1167 for (i = 0;i <= 128;i++)
1170 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
1172 x = (int)(v0[0] + (v1[0] - v0[0]) * (i/128.0f));
1173 y = (int)(v0[1] + (v1[1] - v0[1]) * (i/128.0f));
1174 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
1176 //c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + element0*4;
1177 //pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
1178 pixels[y*w+x] = 0xFFFFFFFF;
1182 void DPSOFTRAST_Draw_Span_Begin(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *zf)
1185 int startx = span->startx;
1186 int endx = span->endx;
1187 float w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
1188 float wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
1189 float endz = 1.0f / (w + wslope * startx);
1190 for (x = startx;x < endx;)
1192 int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1;
1194 if(nextsub >= endx) nextsub = endsub = endx-1;
1195 endz = 1.0f / (w + wslope * nextsub);
1196 dz = x < nextsub ? (endz - z) / (nextsub - x) : 0.0f;
1197 for (; x <= endsub; x++, z += dz)
1202 void DPSOFTRAST_Draw_Span_Finish(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const float * RESTRICT in4f)
1205 int startx = span->startx;
1206 int endx = span->endx;
1209 unsigned char * RESTRICT pixelmask = span->pixelmask;
1210 unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1213 pixel += span->start * 4;
1214 // handle alphatest now (this affects depth writes too)
1215 if (dpsoftrast.user.alphatest)
1216 for (x = startx;x < endx;x++)
1217 if (in4f[x*4+3] < 0.5f)
1218 pixelmask[x] = false;
1219 // FIXME: this does not handle bigendian
1220 switch(dpsoftrast.fb_blendmode)
1222 case DPSOFTRAST_BLENDMODE_OPAQUE:
1223 for (x = startx;x < endx;x++)
1227 d[0] = (int)(in4f[x*4+2]*255.0f);if (d[0] > 255) d[0] = 255;
1228 d[1] = (int)(in4f[x*4+1]*255.0f);if (d[1] > 255) d[1] = 255;
1229 d[2] = (int)(in4f[x*4+0]*255.0f);if (d[2] > 255) d[2] = 255;
1230 d[3] = (int)(in4f[x*4+3]*255.0f);if (d[3] > 255) d[3] = 255;
1231 pixel[x*4+0] = d[0];
1232 pixel[x*4+1] = d[1];
1233 pixel[x*4+2] = d[2];
1234 pixel[x*4+3] = d[3];
1237 case DPSOFTRAST_BLENDMODE_ALPHA:
1238 for (x = startx;x < endx;x++)
1242 a = in4f[x*4+3] * 255.0f;
1243 b = 1.0f - in4f[x*4+3];
1244 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1245 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1246 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1247 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1248 pixel[x*4+0] = d[0];
1249 pixel[x*4+1] = d[1];
1250 pixel[x*4+2] = d[2];
1251 pixel[x*4+3] = d[3];
1254 case DPSOFTRAST_BLENDMODE_ADDALPHA:
1255 for (x = startx;x < endx;x++)
1259 a = in4f[x*4+3] * 255.0f;
1260 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1261 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1262 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1263 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1264 pixel[x*4+0] = d[0];
1265 pixel[x*4+1] = d[1];
1266 pixel[x*4+2] = d[2];
1267 pixel[x*4+3] = d[3];
1270 case DPSOFTRAST_BLENDMODE_ADD:
1271 for (x = startx;x < endx;x++)
1275 d[0] = (int)(in4f[x*4+2]*255.0f+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1276 d[1] = (int)(in4f[x*4+1]*255.0f+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1277 d[2] = (int)(in4f[x*4+0]*255.0f+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1278 d[3] = (int)(in4f[x*4+3]*255.0f+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1279 pixel[x*4+0] = d[0];
1280 pixel[x*4+1] = d[1];
1281 pixel[x*4+2] = d[2];
1282 pixel[x*4+3] = d[3];
1285 case DPSOFTRAST_BLENDMODE_INVMOD:
1286 for (x = startx;x < endx;x++)
1290 d[0] = (int)((1.0f-in4f[x*4+2])*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1291 d[1] = (int)((1.0f-in4f[x*4+1])*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1292 d[2] = (int)((1.0f-in4f[x*4+0])*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1293 d[3] = (int)((1.0f-in4f[x*4+3])*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1294 pixel[x*4+0] = d[0];
1295 pixel[x*4+1] = d[1];
1296 pixel[x*4+2] = d[2];
1297 pixel[x*4+3] = d[3];
1300 case DPSOFTRAST_BLENDMODE_MUL:
1301 for (x = startx;x < endx;x++)
1305 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]);if (d[0] > 255) d[0] = 255;
1306 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]);if (d[1] > 255) d[1] = 255;
1307 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]);if (d[2] > 255) d[2] = 255;
1308 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]);if (d[3] > 255) d[3] = 255;
1309 pixel[x*4+0] = d[0];
1310 pixel[x*4+1] = d[1];
1311 pixel[x*4+2] = d[2];
1312 pixel[x*4+3] = d[3];
1315 case DPSOFTRAST_BLENDMODE_MUL2:
1316 for (x = startx;x < endx;x++)
1320 d[0] = (int)(in4f[x*4+2]*pixel[x*4+0]*2.0f);if (d[0] > 255) d[0] = 255;
1321 d[1] = (int)(in4f[x*4+1]*pixel[x*4+1]*2.0f);if (d[1] > 255) d[1] = 255;
1322 d[2] = (int)(in4f[x*4+0]*pixel[x*4+2]*2.0f);if (d[2] > 255) d[2] = 255;
1323 d[3] = (int)(in4f[x*4+3]*pixel[x*4+3]*2.0f);if (d[3] > 255) d[3] = 255;
1324 pixel[x*4+0] = d[0];
1325 pixel[x*4+1] = d[1];
1326 pixel[x*4+2] = d[2];
1327 pixel[x*4+3] = d[3];
1330 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1331 for (x = startx;x < endx;x++)
1335 a = in4f[x*4+3] * -255.0f;
1336 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]);if (d[0] > 255) d[0] = 255;if (d[0] < 0) d[0] = 0;
1337 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]);if (d[1] > 255) d[1] = 255;if (d[1] < 0) d[1] = 0;
1338 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]);if (d[2] > 255) d[2] = 255;if (d[2] < 0) d[2] = 0;
1339 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]);if (d[3] > 255) d[3] = 255;if (d[3] < 0) d[3] = 0;
1340 pixel[x*4+0] = d[0];
1341 pixel[x*4+1] = d[1];
1342 pixel[x*4+2] = d[2];
1343 pixel[x*4+3] = d[3];
1346 case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1347 for (x = startx;x < endx;x++)
1352 b = 1.0f - in4f[x*4+3];
1353 d[0] = (int)(in4f[x*4+2]*a+pixel[x*4+0]*b);if (d[0] > 255) d[0] = 255;
1354 d[1] = (int)(in4f[x*4+1]*a+pixel[x*4+1]*b);if (d[1] > 255) d[1] = 255;
1355 d[2] = (int)(in4f[x*4+0]*a+pixel[x*4+2]*b);if (d[2] > 255) d[2] = 255;
1356 d[3] = (int)(in4f[x*4+3]*a+pixel[x*4+3]*b);if (d[3] > 255) d[3] = 255;
1357 pixel[x*4+0] = d[0];
1358 pixel[x*4+1] = d[1];
1359 pixel[x*4+2] = d[2];
1360 pixel[x*4+3] = d[3];
1366 void DPSOFTRAST_Draw_Span_FinishBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, const unsigned char* RESTRICT in4ub)
1370 int startx = span->startx;
1371 int endx = span->endx;
1372 const unsigned int * RESTRICT ini = (const unsigned int *)in4ub;
1373 unsigned char * RESTRICT pixelmask = span->pixelmask;
1374 unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0];
1375 unsigned int * RESTRICT pixeli = (unsigned int *)dpsoftrast.fb_colorpixels[0];
1378 pixel += span->start * 4;
1379 pixeli += span->start;
1380 // handle alphatest now (this affects depth writes too)
1381 if (dpsoftrast.user.alphatest)
1382 for (x = startx;x < endx;x++)
1383 if (in4ub[x*4+3] < 0.5f)
1384 pixelmask[x] = false;
1385 // FIXME: this does not handle bigendian
1386 switch(dpsoftrast.fb_blendmode)
1388 case DPSOFTRAST_BLENDMODE_OPAQUE:
1389 for (x = startx;x + 4 <= endx;)
1391 if (*(const unsigned int *)&pixelmask[x] == 0x01010101)
1393 _mm_storeu_si128((__m128i *)&pixeli[x], _mm_loadu_si128((const __m128i *)&ini[x]));
1407 case DPSOFTRAST_BLENDMODE_ALPHA:
1408 #define FINISHBLEND(blend2, blend1) \
1409 for (x = startx;x + 2 <= endx;x += 2) \
1412 switch (*(const unsigned short*)&pixelmask[x]) \
1415 src = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ini[x]), _mm_setzero_si128()); \
1416 dst = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixeli[x]), _mm_setzero_si128()); \
1418 _mm_storel_epi64((__m128i *)&pixeli[x], _mm_packus_epi16(dst, dst)); \
1421 src = _mm_unpacklo_epi8(_mm_cvtsi32_si128(ini[x+1]), _mm_setzero_si128()); \
1422 dst = _mm_unpacklo_epi8(_mm_cvtsi32_si128(pixeli[x+1]), _mm_setzero_si128()); \
1424 pixeli[x+1] = _mm_cvtsi128_si32(_mm_packus_epi16(dst, dst)); \
1427 src = _mm_unpacklo_epi8(_mm_cvtsi32_si128(ini[x]), _mm_setzero_si128()); \
1428 dst = _mm_unpacklo_epi8(_mm_cvtsi32_si128(pixeli[x]), _mm_setzero_si128()); \
1430 pixeli[x] = _mm_cvtsi128_si32(_mm_packus_epi16(dst, dst)); \
1435 for(;x < endx; x++) \
1438 if (!pixelmask[x]) \
1440 src = _mm_unpacklo_epi8(_mm_cvtsi32_si128(ini[x]), _mm_setzero_si128()); \
1441 dst = _mm_unpacklo_epi8(_mm_cvtsi32_si128(pixeli[x]), _mm_setzero_si128()); \
1443 pixeli[x] = _mm_cvtsi128_si32(_mm_packus_epi16(dst, dst)); \
1447 __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
1448 dst = _mm_add_epi16(dst, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(src, dst), 4), _mm_slli_epi16(blend, 4)));
1450 __m128i blend = _mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3));
1451 dst = _mm_add_epi16(dst, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(src, dst), 4), _mm_slli_epi16(blend, 4)));
1454 case DPSOFTRAST_BLENDMODE_ADDALPHA:
1456 __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
1457 dst = _mm_add_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(src, blend), 8));
1459 __m128i blend = _mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3));
1460 dst = _mm_add_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(src, blend), 8));
1463 case DPSOFTRAST_BLENDMODE_ADD:
1464 FINISHBLEND({ dst = _mm_add_epi16(src, dst); }, { dst = _mm_add_epi16(src, dst); });
1466 case DPSOFTRAST_BLENDMODE_INVMOD:
1468 dst = _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(dst, src), 8));
1470 dst = _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(dst, src), 8));
1473 case DPSOFTRAST_BLENDMODE_MUL:
1474 FINISHBLEND({ dst = _mm_srli_epi16(_mm_mullo_epi16(src, dst), 8); }, { dst = _mm_srli_epi16(_mm_mullo_epi16(src, dst), 8); });
1476 case DPSOFTRAST_BLENDMODE_MUL2:
1477 FINISHBLEND({ dst = _mm_srli_epi16(_mm_mullo_epi16(src, dst), 7); }, { dst = _mm_srli_epi16(_mm_mullo_epi16(src, dst), 7); });
1479 case DPSOFTRAST_BLENDMODE_SUBALPHA:
1481 __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
1482 dst = _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(src, blend), 8));
1484 __m128i blend = _mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3));
1485 dst = _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(src, blend), 8));
1488 case DPSOFTRAST_BLENDMODE_PSEUDOALPHA:
1490 __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
1491 dst = _mm_add_epi16(src, _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(dst, blend), 8)));
1493 __m128i blend = _mm_shufflelo_epi16(src, _MM_SHUFFLE(3, 3, 3, 3));
1494 dst = _mm_add_epi16(src, _mm_sub_epi16(dst, _mm_srli_epi16(_mm_mullo_epi16(dst, blend), 8)));
1501 void DPSOFTRAST_Draw_Span_Texture2DVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float * RESTRICT out4f, int texunitindex, int arrayindex, const float * RESTRICT zf)
1504 int startx = span->startx;
1505 int endx = span->endx;
1510 float tc[2], endtc[2];
1512 unsigned int tci[2];
1513 unsigned int tci1[2];
1514 unsigned int tcimin[2];
1515 unsigned int tcimax[2];
1520 const unsigned char * RESTRICT pixelbase;
1521 const unsigned char * RESTRICT pixel[4];
1522 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1523 // if no texture is bound, just fill it with white
1526 for (x = startx;x < endx;x++)
1528 out4f[x*4+0] = 1.0f;
1529 out4f[x*4+1] = 1.0f;
1530 out4f[x*4+2] = 1.0f;
1531 out4f[x*4+3] = 1.0f;
1535 mip = span->mip[texunitindex];
1536 pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
1537 // if this mipmap of the texture is 1 pixel, just fill it with that color
1538 if (texture->mipmap[mip][1] == 4)
1540 c[0] = texture->bytes[2] * (1.0f/255.0f);
1541 c[1] = texture->bytes[1] * (1.0f/255.0f);
1542 c[2] = texture->bytes[0] * (1.0f/255.0f);
1543 c[3] = texture->bytes[3] * (1.0f/255.0f);
1544 for (x = startx;x < endx;x++)
1546 out4f[x*4+0] = c[0];
1547 out4f[x*4+1] = c[1];
1548 out4f[x*4+2] = c[2];
1549 out4f[x*4+3] = c[3];
1553 filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1554 data[0] = span->data[0][arrayindex][0];
1555 data[1] = span->data[0][arrayindex][1];
1556 data[2] = span->data[0][arrayindex][2];
1557 data[3] = span->data[0][arrayindex][3];
1558 slope[0] = span->data[1][arrayindex][0];
1559 slope[1] = span->data[1][arrayindex][1];
1560 slope[2] = span->data[1][arrayindex][2];
1561 slope[3] = span->data[1][arrayindex][3];
1562 flags = texture->flags;
1563 tcscale[0] = texture->mipmap[mip][2];
1564 tcscale[1] = texture->mipmap[mip][3];
1565 tciwidth = texture->mipmap[mip][2];
1568 tcimax[0] = texture->mipmap[mip][2]-1;
1569 tcimax[1] = texture->mipmap[mip][3]-1;
1570 tciwrapmask[0] = texture->mipmap[mip][2]-1;
1571 tciwrapmask[1] = texture->mipmap[mip][3]-1;
1572 endtc[0] = (data[0] + slope[0]*startx) * zf[startx] * tcscale[0] - 0.5f;
1573 endtc[1] = (data[1] + slope[1]*startx) * zf[startx] * tcscale[1] - 0.5f;
1574 for (x = startx;x < endx;)
1576 unsigned int subtc[2];
1577 unsigned int substep[2];
1578 float subscale = 65536.0f/DPSOFTRAST_MAXSUBSPAN;
1579 int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1;
1582 nextsub = endsub = endx-1;
1583 if(x < nextsub) subscale = 65536.0f / (nextsub - x);
1587 endtc[0] = (data[0] + slope[0]*nextsub) * zf[nextsub] * tcscale[0] - 0.5f;
1588 endtc[1] = (data[1] + slope[1]*nextsub) * zf[nextsub] * tcscale[1] - 0.5f;
1589 substep[0] = (endtc[0] - tc[0]) * subscale;
1590 substep[1] = (endtc[1] - tc[1]) * subscale;
1591 subtc[0] = tc[0] * (1<<16);
1592 subtc[1] = tc[1] * (1<<16);
1595 if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1597 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1599 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1600 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1601 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1602 tci[0] = subtc[0]>>16;
1603 tci[1] = subtc[1]>>16;
1604 tci1[0] = tci[0] + 1;
1605 tci1[1] = tci[1] + 1;
1606 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1607 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1608 tci1[0] = tci1[0] >= tcimin[0] ? (tci1[0] <= tcimax[0] ? tci1[0] : tcimax[0]) : tcimin[0];
1609 tci1[1] = tci1[1] >= tcimin[1] ? (tci1[1] <= tcimax[1] ? tci1[1] : tcimax[1]) : tcimin[1];
1610 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1611 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1612 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1613 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1614 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);
1615 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);
1616 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);
1617 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);
1618 out4f[x*4+0] = c[0];
1619 out4f[x*4+1] = c[1];
1620 out4f[x*4+2] = c[2];
1621 out4f[x*4+3] = c[3];
1626 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1628 unsigned int frac[2] = { subtc[0]&0xFFF, subtc[1]&0xFFF };
1629 unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
1630 unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
1631 tci[0] = subtc[0]>>16;
1632 tci[1] = subtc[1]>>16;
1633 tci1[0] = tci[0] + 1;
1634 tci1[1] = tci[1] + 1;
1635 tci[0] &= tciwrapmask[0];
1636 tci[1] &= tciwrapmask[1];
1637 tci1[0] &= tciwrapmask[0];
1638 tci1[1] &= tciwrapmask[1];
1639 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1640 pixel[1] = pixelbase + 4 * (tci[1]*tciwidth+tci1[0]);
1641 pixel[2] = pixelbase + 4 * (tci1[1]*tciwidth+tci[0]);
1642 pixel[3] = pixelbase + 4 * (tci1[1]*tciwidth+tci1[0]);
1643 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);
1644 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);
1645 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);
1646 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);
1647 out4f[x*4+0] = c[0];
1648 out4f[x*4+1] = c[1];
1649 out4f[x*4+2] = c[2];
1650 out4f[x*4+3] = c[3];
1654 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1656 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1658 tci[0] = subtc[0]>>16;
1659 tci[1] = subtc[1]>>16;
1660 tci[0] = tci[0] >= tcimin[0] ? (tci[0] <= tcimax[0] ? tci[0] : tcimax[0]) : tcimin[0];
1661 tci[1] = tci[1] >= tcimin[1] ? (tci[1] <= tcimax[1] ? tci[1] : tcimax[1]) : tcimin[1];
1662 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1663 c[0] = pixel[0][2] * (1.0f / 255.0f);
1664 c[1] = pixel[0][1] * (1.0f / 255.0f);
1665 c[2] = pixel[0][0] * (1.0f / 255.0f);
1666 c[3] = pixel[0][3] * (1.0f / 255.0f);
1667 out4f[x*4+0] = c[0];
1668 out4f[x*4+1] = c[1];
1669 out4f[x*4+2] = c[2];
1670 out4f[x*4+3] = c[3];
1675 for (; x <= endsub; x++, subtc[0] += substep[0], subtc[1] += substep[1])
1677 tci[0] = subtc[0]>>16;
1678 tci[1] = subtc[1]>>16;
1679 tci[0] &= tciwrapmask[0];
1680 tci[1] &= tciwrapmask[1];
1681 pixel[0] = pixelbase + 4 * (tci[1]*tciwidth+tci[0]);
1682 c[0] = pixel[0][2] * (1.0f / 255.0f);
1683 c[1] = pixel[0][1] * (1.0f / 255.0f);
1684 c[2] = pixel[0][0] * (1.0f / 255.0f);
1685 c[3] = pixel[0][3] * (1.0f / 255.0f);
1686 out4f[x*4+0] = c[0];
1687 out4f[x*4+1] = c[1];
1688 out4f[x*4+2] = c[2];
1689 out4f[x*4+3] = c[3];
1695 void DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf)
1699 int startx = span->startx;
1700 int endx = span->endx;
1702 __m128 data, slope, tcscale;
1703 __m128i tcsize, tcmask, tcoffset, tcmax;
1705 __m128i subtc, substep, endsubtc;
1708 unsigned int *outi = (unsigned int *)out4ub;
1709 const unsigned char * RESTRICT pixelbase;
1710 DPSOFTRAST_Texture *texture = dpsoftrast.texbound[texunitindex];
1711 // if no texture is bound, just fill it with white
1714 memset(out4ub + startx*4, 255, span->length*4);
1717 mip = span->mip[texunitindex];
1718 pixelbase = (const unsigned char *)texture->bytes + texture->mipmap[mip][0];
1719 // if this mipmap of the texture is 1 pixel, just fill it with that color
1720 if (texture->mipmap[mip][1] == 4)
1722 unsigned int k = *((const unsigned int *)pixelbase);
1723 for (x = startx;x < endx;x++)
1727 filter = texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR;
1728 data = _mm_load_ps(span->data[0][arrayindex]);
1729 slope = _mm_load_ps(span->data[1][arrayindex]);
1730 flags = texture->flags;
1731 tcsize = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i *)&texture->mipmap[mip][0]), _MM_SHUFFLE(3, 2, 3, 2));
1732 tcmask = _mm_sub_epi32(tcsize, _mm_set1_epi32(1));
1733 tcscale = _mm_cvtepi32_ps(tcsize);
1734 data = _mm_mul_ps(_mm_shuffle_ps(data, data, _MM_SHUFFLE(1, 0, 1, 0)), tcscale);
1735 slope = _mm_mul_ps(_mm_shuffle_ps(slope, slope, _MM_SHUFFLE(1, 0, 1, 0)), tcscale);
1736 endtc = _mm_sub_ps(_mm_mul_ps(_mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx))), _mm_load1_ps(&zf[startx])), _mm_set1_ps(0.5f));
1737 endsubtc = _mm_cvtps_epi32(_mm_mul_ps(endtc, _mm_set1_ps(65536.0f)));
1738 tcoffset = _mm_add_epi32(_mm_slli_epi32(_mm_shuffle_epi32(tcsize, _MM_SHUFFLE(0, 0, 0, 0)), 18), _mm_set1_epi32(4));
1739 tcmax = filter ? _mm_packs_epi32(tcmask, tcmask) : _mm_slli_epi32(tcmask, 16);
1740 for (x = startx;x < endx;)
1742 int nextsub = x + DPSOFTRAST_MAXSUBSPAN, endsub = nextsub - 1;
1743 __m128 subscale = _mm_set1_ps(65536.0f/DPSOFTRAST_MAXSUBSPAN);
1746 nextsub = endsub = endx-1;
1747 if(x < nextsub) subscale = _mm_set1_ps(65536.0f / (nextsub - x));
1751 endtc = _mm_sub_ps(_mm_mul_ps(_mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(nextsub))), _mm_load1_ps(&zf[nextsub])), _mm_set1_ps(0.5f));
1752 substep = _mm_cvtps_epi32(_mm_mul_ps(_mm_sub_ps(endtc, tc), subscale));
1753 endsubtc = _mm_cvtps_epi32(_mm_mul_ps(endtc, _mm_set1_ps(65536.0f)));
1754 subtc = _mm_unpacklo_epi64(subtc, _mm_add_epi32(subtc, substep));
1755 substep = _mm_slli_epi32(substep, 1);
1758 __m128i tcrange = _mm_srai_epi32(_mm_unpacklo_epi64(subtc, _mm_add_epi32(endsubtc, substep)), 16);
1759 if (_mm_movemask_epi8(_mm_andnot_si128(_mm_cmplt_epi32(tcrange, _mm_setzero_si128()), _mm_cmplt_epi32(tcrange, tcmask))) == 0xFFFF)
1761 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1763 __m128i tci = _mm_shufflehi_epi16(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 1, 3, 1)), pix1, pix2, pix3, pix4, fracm;
1764 tci = _mm_madd_epi16(_mm_add_epi16(tci, _mm_setr_epi32(0, 0x10000, 0, 0x10000)), tcoffset);
1765 pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(tci)]), _mm_setzero_si128());
1766 pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))]), _mm_setzero_si128());
1767 pix3 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]), _mm_setzero_si128());
1768 pix4 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))]), _mm_setzero_si128());
1769 fracm = _mm_srli_epi16(subtc, 1);
1770 pix1 = _mm_add_epi16(pix1,
1771 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1772 _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1773 pix3 = _mm_add_epi16(pix3,
1774 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix3), 1),
1775 _mm_shuffle_epi32(_mm_shufflehi_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(3, 2, 3, 2))));
1776 pix2 = _mm_unpacklo_epi64(pix1, pix3);
1777 pix4 = _mm_unpackhi_epi64(pix1, pix3);
1778 pix2 = _mm_add_epi16(pix2,
1779 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix2), 1),
1780 _mm_shufflehi_epi16(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0)), _MM_SHUFFLE(0, 0, 0, 0))));
1781 _mm_storel_epi64((__m128i *)&outi[x], _mm_packus_epi16(pix2, _mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 2, 3, 2))));
1785 __m128i tci = _mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), pix1, pix2, fracm;
1786 tci = _mm_madd_epi16(_mm_add_epi16(tci, _mm_setr_epi32(0, 0x10000, 0, 0)), tcoffset);
1787 pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(tci)]), _mm_setzero_si128());
1788 pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))]), _mm_setzero_si128());
1789 fracm = _mm_srli_epi16(subtc, 1);
1790 pix1 = _mm_add_epi16(pix1,
1791 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1792 _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1793 pix2 = _mm_shuffle_epi32(pix1, _MM_SHUFFLE(3, 2, 3, 2));
1794 pix1 = _mm_add_epi16(pix1,
1795 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1796 _mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0))));
1797 outi[x] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
1801 else if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1803 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1805 __m128i tci = _mm_shuffle_epi32(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(1, 0, 1, 0)), pix1, pix2, pix3, pix4, fracm;
1806 tci = _mm_min_epi16(_mm_max_epi16(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), _mm_setzero_si128()), tcmax);
1807 tci = _mm_madd_epi16(tci, tcoffset);
1808 pix1 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1809 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1810 _mm_setzero_si128());
1811 pix2 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1812 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1813 _mm_setzero_si128());
1814 tci = _mm_shuffle_epi32(_mm_shufflehi_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 2, 3, 2));
1815 tci = _mm_and_si128(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), tcmax);
1816 tci = _mm_madd_epi16(tci, tcoffset);
1817 pix3 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1818 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1819 _mm_setzero_si128());
1820 pix4 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1821 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1822 _mm_setzero_si128());
1823 fracm = _mm_srli_epi16(subtc, 1);
1824 pix1 = _mm_add_epi16(pix1,
1825 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1826 _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1827 pix3 = _mm_add_epi16(pix3,
1828 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix3), 1),
1829 _mm_shuffle_epi32(_mm_shufflehi_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(3, 2, 3, 2))));
1830 pix2 = _mm_unpacklo_epi64(pix1, pix3);
1831 pix4 = _mm_unpackhi_epi64(pix1, pix3);
1832 pix2 = _mm_add_epi16(pix2,
1833 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix2), 1),
1834 _mm_shufflehi_epi16(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0)), _MM_SHUFFLE(0, 0, 0, 0))));
1835 _mm_storel_epi64((__m128i *)&outi[x], _mm_packus_epi16(pix2, _mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 2, 3, 2))));
1839 __m128i tci = _mm_shuffle_epi32(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(1, 0, 1, 0)), pix1, pix2, fracm;
1840 tci = _mm_min_epi16(_mm_max_epi16(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), _mm_setzero_si128()), tcmax);
1841 tci = _mm_madd_epi16(tci, tcoffset);
1842 pix1 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1843 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1844 _mm_setzero_si128());
1845 pix2 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1846 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1847 _mm_setzero_si128());
1848 fracm = _mm_srli_epi16(subtc, 1);
1849 pix1 = _mm_add_epi16(pix1,
1850 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1851 _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1852 pix2 = _mm_shuffle_epi32(pix1, _MM_SHUFFLE(3, 2, 3, 2));
1853 pix1 = _mm_add_epi16(pix1,
1854 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1855 _mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0))));
1856 outi[x] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
1862 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1864 __m128i tci = _mm_shuffle_epi32(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(1, 0, 1, 0)), pix1, pix2, pix3, pix4, fracm;
1865 tci = _mm_and_si128(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), tcmax);
1866 tci = _mm_madd_epi16(tci, tcoffset);
1867 pix1 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1868 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1869 _mm_setzero_si128());
1870 pix2 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1871 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1872 _mm_setzero_si128());
1873 tci = _mm_shuffle_epi32(_mm_shufflehi_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 2, 3, 2));
1874 tci = _mm_and_si128(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), tcmax);
1875 tci = _mm_madd_epi16(tci, tcoffset);
1876 pix3 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1877 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1878 _mm_setzero_si128());
1879 pix4 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1880 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1881 _mm_setzero_si128());
1882 fracm = _mm_srli_epi16(subtc, 1);
1883 pix1 = _mm_add_epi16(pix1,
1884 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1885 _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1886 pix3 = _mm_add_epi16(pix3,
1887 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix3), 1),
1888 _mm_shuffle_epi32(_mm_shufflehi_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(3, 2, 3, 2))));
1889 pix2 = _mm_unpacklo_epi64(pix1, pix3);
1890 pix4 = _mm_unpackhi_epi64(pix1, pix3);
1891 pix2 = _mm_add_epi16(pix2,
1892 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix4, pix2), 1),
1893 _mm_shufflehi_epi16(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0)), _MM_SHUFFLE(0, 0, 0, 0))));
1894 _mm_storel_epi64((__m128i *)&outi[x], _mm_packus_epi16(pix2, _mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 2, 3, 2))));
1898 __m128i tci = _mm_shuffle_epi32(_mm_shufflelo_epi16(subtc, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(1, 0, 1, 0)), pix1, pix2, fracm;
1899 tci = _mm_and_si128(_mm_add_epi16(tci, _mm_setr_epi32(0, 1, 0x10000, 0x10001)), tcmax);
1900 tci = _mm_madd_epi16(tci, tcoffset);
1901 pix1 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(tci)]),
1902 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))])),
1903 _mm_setzero_si128());
1904 pix2 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(_mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(2, 2, 2, 2)))]),
1905 _mm_cvtsi32_si128(*(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(3, 3, 3, 3)))])),
1906 _mm_setzero_si128());
1907 fracm = _mm_srli_epi16(subtc, 1);
1908 pix1 = _mm_add_epi16(pix1,
1909 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1910 _mm_shuffle_epi32(_mm_shufflelo_epi16(fracm, _MM_SHUFFLE(2, 2, 2, 2)), _MM_SHUFFLE(1, 0, 1, 0))));
1911 pix2 = _mm_shuffle_epi32(pix1, _MM_SHUFFLE(3, 2, 3, 2));
1912 pix1 = _mm_add_epi16(pix1,
1913 _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 1),
1914 _mm_shufflelo_epi16(fracm, _MM_SHUFFLE(0, 0, 0, 0))));
1915 outi[x] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
1922 if (flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
1924 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1926 __m128i tci = _mm_min_epi16(_mm_max_epi16(subtc, _mm_setzero_si128()), tcmax);
1927 tci = _mm_shufflehi_epi16(_mm_shufflelo_epi16(tci, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 1, 3, 1));
1928 tci = _mm_madd_epi16(tci, tcoffset);
1929 outi[x] = *(const int *)&pixelbase[_mm_cvtsi128_si32(tci)];
1930 outi[x+1] = *(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))];
1934 __m128i tci = _mm_min_epi16(_mm_max_epi16(subtc, _mm_setzero_si128()), tcmax);
1935 tci = _mm_shufflelo_epi16(tci, _MM_SHUFFLE(3, 1, 3, 1));
1936 tci = _mm_madd_epi16(tci, tcoffset);
1937 outi[x] = *(const int *)&pixelbase[_mm_cvtsi128_si32(tci)];
1943 for (; x + 1 <= endsub; x += 2, subtc = _mm_add_epi32(subtc, substep))
1945 __m128i tci = _mm_and_si128(subtc, tcmax);
1946 tci = _mm_shufflehi_epi16(_mm_shufflelo_epi16(tci, _MM_SHUFFLE(3, 1, 3, 1)), _MM_SHUFFLE(3, 1, 3, 1));
1947 tci = _mm_madd_epi16(tci, tcoffset);
1948 outi[x] = *(const int *)&pixelbase[_mm_cvtsi128_si32(tci)];
1949 outi[x+1] = *(const int *)&pixelbase[_mm_cvtsi128_si32(_mm_shuffle_epi32(tci, _MM_SHUFFLE(1, 1, 1, 1)))];
1953 __m128i tci = _mm_and_si128(subtc, tcmax);
1954 tci = _mm_shufflelo_epi16(tci, _MM_SHUFFLE(3, 1, 3, 1));
1955 tci = _mm_madd_epi16(tci, tcoffset);
1956 outi[x] = *(const int *)&pixelbase[_mm_cvtsi128_si32(tci)];
1965 void DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char * RESTRICT out4ub, int texunitindex, int arrayindex, const float * RESTRICT zf)
1968 memset(out4ub, 255, span->length*4);
1971 float DPSOFTRAST_SampleShadowmap(const float *vector)
1977 void DPSOFTRAST_Draw_Span_MultiplyVarying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, int arrayindex, const float *zf)
1980 int startx = span->startx;
1981 int endx = span->endx;
1986 data[0] = span->data[0][arrayindex][0];
1987 data[1] = span->data[0][arrayindex][1];
1988 data[2] = span->data[0][arrayindex][2];
1989 data[3] = span->data[0][arrayindex][3];
1990 slope[0] = span->data[1][arrayindex][0];
1991 slope[1] = span->data[1][arrayindex][1];
1992 slope[2] = span->data[1][arrayindex][2];
1993 slope[3] = span->data[1][arrayindex][3];
1994 for (x = startx;x < endx;x++)
1997 c[0] = (data[0] + slope[0]*x) * z;
1998 c[1] = (data[1] + slope[1]*x) * z;
1999 c[2] = (data[2] + slope[2]*x) * z;
2000 c[3] = (data[3] + slope[3]*x) * z;
2001 out4f[x*4+0] = in4f[x*4+0] * c[0];
2002 out4f[x*4+1] = in4f[x*4+1] * c[1];
2003 out4f[x*4+2] = in4f[x*4+2] * c[2];
2004 out4f[x*4+3] = in4f[x*4+3] * c[3];
2008 void DPSOFTRAST_Draw_Span_Varying(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, int arrayindex, const float *zf)
2011 int startx = span->startx;
2012 int endx = span->endx;
2017 data[0] = span->data[0][arrayindex][0];
2018 data[1] = span->data[0][arrayindex][1];
2019 data[2] = span->data[0][arrayindex][2];
2020 data[3] = span->data[0][arrayindex][3];
2021 slope[0] = span->data[1][arrayindex][0];
2022 slope[1] = span->data[1][arrayindex][1];
2023 slope[2] = span->data[1][arrayindex][2];
2024 slope[3] = span->data[1][arrayindex][3];
2025 for (x = startx;x < endx;x++)
2028 c[0] = (data[0] + slope[0]*x) * z;
2029 c[1] = (data[1] + slope[1]*x) * z;
2030 c[2] = (data[2] + slope[2]*x) * z;
2031 c[3] = (data[3] + slope[3]*x) * z;
2032 out4f[x*4+0] = c[0];
2033 out4f[x*4+1] = c[1];
2034 out4f[x*4+2] = c[2];
2035 out4f[x*4+3] = c[3];
2039 void DPSOFTRAST_Draw_Span_AddBloom(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f, const float *subcolor)
2041 int x, startx = span->startx, endx = span->endx;
2042 float c[4], localcolor[4];
2043 localcolor[0] = subcolor[0];
2044 localcolor[1] = subcolor[1];
2045 localcolor[2] = subcolor[2];
2046 localcolor[3] = subcolor[3];
2047 for (x = startx;x < endx;x++)
2049 c[0] = inb4f[x*4+0] - localcolor[0];if (c[0] < 0.0f) c[0] = 0.0f;
2050 c[1] = inb4f[x*4+1] - localcolor[1];if (c[1] < 0.0f) c[1] = 0.0f;
2051 c[2] = inb4f[x*4+2] - localcolor[2];if (c[2] < 0.0f) c[2] = 0.0f;
2052 c[3] = inb4f[x*4+3] - localcolor[3];if (c[3] < 0.0f) c[3] = 0.0f;
2053 out4f[x*4+0] = ina4f[x*4+0] + c[0];
2054 out4f[x*4+1] = ina4f[x*4+1] + c[1];
2055 out4f[x*4+2] = ina4f[x*4+2] + c[2];
2056 out4f[x*4+3] = ina4f[x*4+3] + c[3];
2060 void DPSOFTRAST_Draw_Span_MultiplyBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2062 int x, startx = span->startx, endx = span->endx;
2063 for (x = startx;x < endx;x++)
2065 out4f[x*4+0] = ina4f[x*4+0] * inb4f[x*4+0];
2066 out4f[x*4+1] = ina4f[x*4+1] * inb4f[x*4+1];
2067 out4f[x*4+2] = ina4f[x*4+2] * inb4f[x*4+2];
2068 out4f[x*4+3] = ina4f[x*4+3] * inb4f[x*4+3];
2072 void DPSOFTRAST_Draw_Span_AddBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2074 int x, startx = span->startx, endx = span->endx;
2075 for (x = startx;x < endx;x++)
2077 out4f[x*4+0] = ina4f[x*4+0] + inb4f[x*4+0];
2078 out4f[x*4+1] = ina4f[x*4+1] + inb4f[x*4+1];
2079 out4f[x*4+2] = ina4f[x*4+2] + inb4f[x*4+2];
2080 out4f[x*4+3] = ina4f[x*4+3] + inb4f[x*4+3];
2084 void DPSOFTRAST_Draw_Span_MixBuffers(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *ina4f, const float *inb4f)
2086 int x, startx = span->startx, endx = span->endx;
2088 for (x = startx;x < endx;x++)
2090 a = 1.0f - inb4f[x*4+3];
2092 out4f[x*4+0] = ina4f[x*4+0] * a + inb4f[x*4+0] * b;
2093 out4f[x*4+1] = ina4f[x*4+1] * a + inb4f[x*4+1] * b;
2094 out4f[x*4+2] = ina4f[x*4+2] * a + inb4f[x*4+2] * b;
2095 out4f[x*4+3] = ina4f[x*4+3] * a + inb4f[x*4+3] * b;
2099 void DPSOFTRAST_Draw_Span_MixUniformColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span, float *out4f, const float *in4f, const float *color)
2101 int x, startx = span->startx, endx = span->endx;
2102 float localcolor[4], ilerp, lerp;
2103 localcolor[0] = color[0];
2104 localcolor[1] = color[1];
2105 localcolor[2] = color[2];
2106 localcolor[3] = color[3];
2107 ilerp = 1.0f - localcolor[3];
2108 lerp = localcolor[3];
2109 for (x = startx;x < endx;x++)
2111 out4f[x*4+0] = in4f[x*4+0] * ilerp + localcolor[0] * lerp;
2112 out4f[x*4+1] = in4f[x*4+1] * ilerp + localcolor[1] * lerp;
2113 out4f[x*4+2] = in4f[x*4+2] * ilerp + localcolor[2] * lerp;
2114 out4f[x*4+3] = in4f[x*4+3] * ilerp + localcolor[3] * lerp;
2120 void DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, int arrayindex, const float *zf)
2124 int startx = span->startx;
2125 int endx = span->endx;
2126 __m128 data = _mm_load_ps(span->data[0][arrayindex]), slope = _mm_load_ps(span->data[1][arrayindex]);
2127 data = _mm_shuffle_ps(data, data, _MM_SHUFFLE(3, 0, 1, 2));
2128 slope = _mm_shuffle_ps(slope, slope, _MM_SHUFFLE(3, 0, 1, 2));
2129 data = _mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx)));
2130 data = _mm_mul_ps(data, _mm_set1_ps(256.0f));
2131 slope = _mm_mul_ps(slope, _mm_set1_ps(256.0f));
2132 for (x = startx;x+2 <= endx;x += 2, data = _mm_add_ps(data, slope))
2134 __m128i pix = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_loadl_epi64((const __m128i *)&in4ub[x*4]));
2135 __m128i mod = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x]))), mod2;
2136 data = _mm_add_ps(data, slope);
2137 mod2 = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x+1])));
2138 mod = _mm_unpacklo_epi64(_mm_packs_epi32(mod, mod), _mm_packs_epi32(mod2, mod2));
2139 pix = _mm_mulhi_epu16(pix, mod);
2140 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix, pix));
2142 for (;x < endx;x++, data = _mm_add_ps(data, slope))
2144 __m128i pix = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&in4ub[x*4]));
2145 __m128i mod = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x])));
2146 mod = _mm_packs_epi32(mod, mod);
2147 pix = _mm_mulhi_epu16(pix, mod);
2148 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2153 void DPSOFTRAST_Draw_Span_VaryingBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, int arrayindex, const float *zf)
2157 int startx = span->startx;
2158 int endx = span->endx;
2159 __m128 data = _mm_load_ps(span->data[0][arrayindex]), slope = _mm_load_ps(span->data[1][arrayindex]);
2160 data = _mm_shuffle_ps(data, data, _MM_SHUFFLE(3, 0, 1, 2));
2161 slope = _mm_shuffle_ps(slope, slope, _MM_SHUFFLE(3, 0, 1, 2));
2162 data = _mm_add_ps(data, _mm_mul_ps(slope, _mm_set1_ps(startx)));
2163 data = _mm_mul_ps(data, _mm_set1_ps(255.0f));
2164 slope = _mm_mul_ps(slope, _mm_set1_ps(255.0f));
2165 for (x = startx;x+2 <= endx;x += 2, data = _mm_add_ps(data, slope))
2167 __m128i pix = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x]))), pix2;
2168 data = _mm_add_ps(data, slope);
2169 pix2 = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x+1])));
2170 pix = _mm_unpacklo_epi64(_mm_packs_epi32(pix, pix), _mm_packs_epi32(pix2, pix2));
2171 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix, pix));
2173 for (;x < endx;x++, data = _mm_add_ps(data, slope))
2175 __m128i pix = _mm_cvtps_epi32(_mm_mul_ps(data, _mm_load1_ps(&zf[x])));
2176 pix = _mm_packs_epi32(pix, pix);
2177 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2182 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)
2185 int x, startx = span->startx, endx = span->endx;
2186 __m128i localcolor = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(subcolor), _mm_set1_ps(255.0f))), _MM_SHUFFLE(3, 0, 1, 2));
2187 localcolor = _mm_shuffle_epi32(_mm_packs_epi32(localcolor, localcolor), _MM_SHUFFLE(1, 0, 1, 0));
2188 for (x = startx;x+2 <= endx;x+=2)
2190 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2191 __m128i pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&inb4ub[x*4]), _mm_setzero_si128());
2192 pix1 = _mm_add_epi16(pix1, _mm_sub_epi16(pix2, localcolor));
2193 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2197 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2198 __m128i pix2 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]), _mm_setzero_si128());
2199 pix1 = _mm_add_epi16(pix1, _mm_sub_epi16(pix2, localcolor));
2200 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2205 void DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2208 int x, startx = span->startx, endx = span->endx;
2209 for (x = startx;x+2 <= endx;x+=2)
2211 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2212 __m128i pix2 = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_loadl_epi64((const __m128i *)&inb4ub[x*4]));
2213 pix1 = _mm_mulhi_epu16(pix1, pix2);
2214 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2218 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2219 __m128i pix2 = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]));
2220 pix1 = _mm_mulhi_epu16(pix1, pix2);
2221 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2226 void DPSOFTRAST_Draw_Span_AddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2229 int x, startx = span->startx, endx = span->endx;
2230 for (x = startx;x+2 <= endx;x+=2)
2232 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2233 __m128i pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&inb4ub[x*4]), _mm_setzero_si128());
2234 pix1 = _mm_add_epi16(pix1, pix2);
2235 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2239 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2240 __m128i pix2 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]), _mm_setzero_si128());
2241 pix1 = _mm_add_epi16(pix1, pix2);
2242 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2247 void DPSOFTRAST_Draw_Span_TintedAddBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub, const float *inbtintbgra)
2250 int x, startx = span->startx, endx = span->endx;
2251 __m128i tint = _mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(inbtintbgra), _mm_set1_ps(256.0f)));
2252 tint = _mm_shuffle_epi32(_mm_packs_epi32(tint, tint), _MM_SHUFFLE(1, 0, 1, 0));
2253 for (x = startx;x+2 <= endx;x+=2)
2255 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2256 __m128i pix2 = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_loadl_epi64((const __m128i *)&inb4ub[x*4]));
2257 pix1 = _mm_add_epi16(pix1, _mm_mulhi_epu16(tint, pix2));
2258 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2262 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2263 __m128i pix2 = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]));
2264 pix1 = _mm_add_epi16(pix1, _mm_mulhi_epu16(tint, pix2));
2265 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2270 void DPSOFTRAST_Draw_Span_MixBuffersBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *ina4ub, const unsigned char *inb4ub)
2273 int x, startx = span->startx, endx = span->endx;
2274 for (x = startx;x+2 <= endx;x+=2)
2276 __m128i pix1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&ina4ub[x*4]), _mm_setzero_si128());
2277 __m128i pix2 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&inb4ub[x*4]), _mm_setzero_si128());
2278 __m128i blend = _mm_shufflehi_epi16(_mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
2279 pix1 = _mm_add_epi16(pix1, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 4), _mm_slli_epi16(blend, 4)));
2280 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix1, pix1));
2284 __m128i pix1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&ina4ub[x*4]), _mm_setzero_si128());
2285 __m128i pix2 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&inb4ub[x*4]), _mm_setzero_si128());
2286 __m128i blend = _mm_shufflelo_epi16(pix2, _MM_SHUFFLE(3, 3, 3, 3));
2287 pix1 = _mm_add_epi16(pix1, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(pix2, pix1), 4), _mm_slli_epi16(blend, 4)));
2288 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix1, pix1));
2293 void DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(const DPSOFTRAST_State_Draw_Span * RESTRICT span, unsigned char *out4ub, const unsigned char *in4ub, const float *color)
2296 int x, startx = span->startx, endx = span->endx;
2297 __m128i localcolor = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_loadu_ps(color), _mm_set1_ps(255.0f))), _MM_SHUFFLE(3, 0, 1, 2)), blend;
2298 localcolor = _mm_shuffle_epi32(_mm_packs_epi32(localcolor, localcolor), _MM_SHUFFLE(1, 0, 1, 0));
2299 blend = _mm_slli_epi16(_mm_shufflehi_epi16(_mm_shufflelo_epi16(localcolor, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3)), 4);
2300 for (x = startx;x+2 <= endx;x+=2)
2302 __m128i pix = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)&in4ub[x*4]), _mm_setzero_si128());
2303 pix = _mm_add_epi16(pix, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(localcolor, pix), 4), blend));
2304 _mm_storel_epi64((__m128i *)&out4ub[x*4], _mm_packus_epi16(pix, pix));
2308 __m128i pix = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(const int *)&in4ub[x*4]), _mm_setzero_si128());
2309 pix = _mm_add_epi16(pix, _mm_mulhi_epi16(_mm_slli_epi16(_mm_sub_epi16(localcolor, pix), 4), blend));
2310 *(int *)&out4ub[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2317 void DPSOFTRAST_VertexShader_Generic(void)
2319 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);
2320 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2321 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2322 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2323 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2326 void DPSOFTRAST_PixelShader_Generic(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2328 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2329 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2330 unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2331 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2332 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2333 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
2335 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_FIRST, 2, buffer_z);
2336 DPSOFTRAST_Draw_Span_MultiplyVaryingBGRA8(span, buffer_FragColorbgra8, buffer_texture_colorbgra8, 1, buffer_z);
2337 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2339 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_SECOND, 2, buffer_z);
2340 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2343 DPSOFTRAST_Draw_Span_MultiplyBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2345 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2348 DPSOFTRAST_Draw_Span_AddBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2350 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND)
2353 DPSOFTRAST_Draw_Span_MixBuffersBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_lightmapbgra8);
2358 DPSOFTRAST_Draw_Span_VaryingBGRA8(span, buffer_FragColorbgra8, 1, buffer_z);
2359 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2364 void DPSOFTRAST_VertexShader_PostProcess(void)
2366 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);
2367 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD0], dpsoftrast.draw.numvertices);
2368 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1], dpsoftrast.draw.numvertices);
2371 void DPSOFTRAST_PixelShader_PostProcess(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2373 // TODO: optimize!! at the very least there is no reason to use texture sampling on the frame texture
2374 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2375 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2376 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2377 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2378 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_FragColorbgra8, GL20TU_FIRST, 2, buffer_z);
2379 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_BLOOM)
2381 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_SECOND, 3, buffer_z);
2382 DPSOFTRAST_Draw_Span_AddBloomBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, buffer_texture_colorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_BloomColorSubtract * 4);
2384 DPSOFTRAST_Draw_Span_MixUniformColorBGRA8(span, buffer_FragColorbgra8, buffer_FragColorbgra8, dpsoftrast.uniform4f + DPSOFTRAST_UNIFORM_ViewTintColor * 4);
2385 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SATURATION)
2387 // TODO: implement saturation
2389 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GAMMARAMPS)
2391 // TODO: implement gammaramps
2393 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2398 void DPSOFTRAST_VertexShader_Depth_Or_Shadow(void)
2400 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);
2403 void DPSOFTRAST_PixelShader_Depth_Or_Shadow(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2405 // this is never called (because colormask is off when this shader is used)
2406 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2407 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2408 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2409 memset(buffer_FragColorbgra8, 0, span->length*4);
2410 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2415 void DPSOFTRAST_VertexShader_FlatColor(void)
2417 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);
2418 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);
2421 void DPSOFTRAST_PixelShader_FlatColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2423 int x, startx = span->startx, endx = span->endx;
2424 int Color_Ambienti[4];
2425 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2426 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2427 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2428 Color_Ambienti[2] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0]*256.0f);
2429 Color_Ambienti[1] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1]*256.0f);
2430 Color_Ambienti[0] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2]*256.0f);
2431 Color_Ambienti[3] = (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0] *256.0f);
2432 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2433 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2434 for (x = startx;x < endx;x++)
2436 buffer_FragColorbgra8[x*4+0] = (buffer_texture_colorbgra8[x*4+0] * Color_Ambienti[0])>>8;
2437 buffer_FragColorbgra8[x*4+1] = (buffer_texture_colorbgra8[x*4+1] * Color_Ambienti[1])>>8;
2438 buffer_FragColorbgra8[x*4+2] = (buffer_texture_colorbgra8[x*4+2] * Color_Ambienti[2])>>8;
2439 buffer_FragColorbgra8[x*4+3] = (buffer_texture_colorbgra8[x*4+3] * Color_Ambienti[3])>>8;
2441 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2446 void DPSOFTRAST_VertexShader_VertexColor(void)
2448 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);
2449 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_COLOR], dpsoftrast.draw.numvertices);
2450 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);
2453 void DPSOFTRAST_PixelShader_VertexColor(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2455 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2456 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2457 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2458 int x, startx = span->startx, endx = span->endx;
2459 float Color_Ambient[4], Color_Diffuse[4];
2463 int arrayindex = DPSOFTRAST_ARRAY_COLOR;
2464 data[2] = span->data[0][arrayindex][0];
2465 data[1] = span->data[0][arrayindex][1];
2466 data[0] = span->data[0][arrayindex][2];
2467 data[3] = span->data[0][arrayindex][3];
2468 slope[2] = span->data[1][arrayindex][0];
2469 slope[1] = span->data[1][arrayindex][1];
2470 slope[0] = span->data[1][arrayindex][2];
2471 slope[3] = span->data[1][arrayindex][3];
2472 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2473 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2474 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2475 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2476 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2477 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2478 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2479 Color_Diffuse[3] = 0.0f;
2480 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2481 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, 2, buffer_z);
2482 for (x = startx;x < endx;x++)
2485 buffer_FragColorbgra8[x*4+0] = (int)(buffer_texture_colorbgra8[x*4+0] * (Color_Ambient[0] + ((data[0] + slope[0]*x) * z) * Color_Diffuse[0]));
2486 buffer_FragColorbgra8[x*4+1] = (int)(buffer_texture_colorbgra8[x*4+1] * (Color_Ambient[1] + ((data[1] + slope[1]*x) * z) * Color_Diffuse[1]));
2487 buffer_FragColorbgra8[x*4+2] = (int)(buffer_texture_colorbgra8[x*4+2] * (Color_Ambient[2] + ((data[2] + slope[2]*x) * z) * Color_Diffuse[2]));
2488 buffer_FragColorbgra8[x*4+3] = (int)(buffer_texture_colorbgra8[x*4+3] * (Color_Ambient[3] + ((data[3] + slope[3]*x) * z) * Color_Diffuse[3]));
2490 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2495 void DPSOFTRAST_VertexShader_Lightmap(void)
2497 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);
2498 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);
2499 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
2502 void DPSOFTRAST_PixelShader_Lightmap(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2505 unsigned char * RESTRICT pixelmask = span->pixelmask;
2506 unsigned char * RESTRICT pixel = (unsigned char *)dpsoftrast.fb_colorpixels[0] + span->start * 4;
2507 int x, startx = span->startx, endx = span->endx;
2508 __m128i Color_Ambientm, Color_Diffusem, Color_Glowm, Color_AmbientGlowm;
2509 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2510 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2511 unsigned char buffer_texture_lightmapbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2512 unsigned char buffer_texture_glowbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2513 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2514 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2515 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2516 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_lightmapbgra8, GL20TU_LIGHTMAP, DPSOFTRAST_ARRAY_TEXCOORD4, buffer_z);
2517 if (dpsoftrast.user.alphatest || dpsoftrast.fb_blendmode != DPSOFTRAST_BLENDMODE_OPAQUE)
2518 pixel = buffer_FragColorbgra8;
2519 Color_Ambientm = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2));
2520 Color_Ambientm = _mm_and_si128(Color_Ambientm, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0));
2521 Color_Ambientm = _mm_or_si128(Color_Ambientm, _mm_setr_epi32(0, 0, 0, (int)(dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0]*255.0f)));
2522 Color_Ambientm = _mm_packs_epi32(Color_Ambientm, Color_Ambientm);
2523 Color_Diffusem = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2));
2524 Color_Diffusem = _mm_and_si128(Color_Diffusem, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0));
2525 Color_Diffusem = _mm_packs_epi32(Color_Diffusem, Color_Diffusem);
2526 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2528 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glowbgra8, GL20TU_GLOW, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2529 Color_Glowm = _mm_shuffle_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(&dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4]), _mm_set1_ps(256.0f))), _MM_SHUFFLE(3, 0, 1, 2));
2530 Color_Glowm = _mm_and_si128(Color_Glowm, _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0));
2531 Color_Glowm = _mm_packs_epi32(Color_Glowm, Color_Glowm);
2532 Color_AmbientGlowm = _mm_unpacklo_epi64(Color_Ambientm, Color_Glowm);
2533 for (x = startx;x < endx;x++)
2535 __m128i color, lightmap, glow, pix;
2536 if (x + 4 <= endx && *(const unsigned int *)&pixelmask[x] == 0x01010101)
2539 color = _mm_loadu_si128((const __m128i *)&buffer_texture_colorbgra8[x*4]);
2540 lightmap = _mm_loadu_si128((const __m128i *)&buffer_texture_lightmapbgra8[x*4]);
2541 glow = _mm_loadu_si128((const __m128i *)&buffer_texture_glowbgra8[x*4]);
2542 pix = _mm_add_epi16(_mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, _mm_unpacklo_epi8(_mm_setzero_si128(), lightmap)), Color_Ambientm),
2543 _mm_unpacklo_epi8(_mm_setzero_si128(), color)),
2544 _mm_mulhi_epu16(Color_Glowm, _mm_unpacklo_epi8(_mm_setzero_si128(), glow)));
2545 pix2 = _mm_add_epi16(_mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, _mm_unpackhi_epi8(_mm_setzero_si128(), lightmap)), Color_Ambientm),
2546 _mm_unpackhi_epi8(_mm_setzero_si128(), color)),
2547 _mm_mulhi_epu16(Color_Glowm, _mm_unpackhi_epi8(_mm_setzero_si128(), glow)));
2548 _mm_storeu_si128((__m128i *)&pixel[x*4], _mm_packus_epi16(pix, pix2));
2554 color = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_colorbgra8[x*4]));
2555 lightmap = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_lightmapbgra8[x*4]));
2556 glow = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_glowbgra8[x*4]));
2557 pix = _mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, lightmap), Color_AmbientGlowm), _mm_unpacklo_epi64(color, glow));
2558 pix = _mm_add_epi16(pix, _mm_shuffle_epi32(pix, _MM_SHUFFLE(3, 2, 3, 2)));
2559 *(int *)&pixel[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2564 for (x = startx;x < endx;x++)
2566 __m128i color, lightmap, pix;
2567 if (x + 4 <= endx && *(const unsigned int *)&pixelmask[x] == 0x01010101)
2570 color = _mm_loadu_si128((const __m128i *)&buffer_texture_colorbgra8[x*4]);
2571 lightmap = _mm_loadu_si128((const __m128i *)&buffer_texture_lightmapbgra8[x*4]);
2572 pix = _mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, _mm_unpacklo_epi8(_mm_setzero_si128(), lightmap)), Color_Ambientm),
2573 _mm_unpacklo_epi8(_mm_setzero_si128(), color));
2574 pix2 = _mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(Color_Diffusem, _mm_unpackhi_epi8(_mm_setzero_si128(), lightmap)), Color_Ambientm),
2575 _mm_unpackhi_epi8(_mm_setzero_si128(), color));
2576 _mm_storeu_si128((__m128i *)&pixel[x*4], _mm_packus_epi16(pix, pix2));
2582 color = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_colorbgra8[x*4]));
2583 lightmap = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_cvtsi32_si128(*(const int *)&buffer_texture_lightmapbgra8[x*4]));
2584 pix = _mm_mulhi_epu16(_mm_add_epi16(_mm_mulhi_epu16(lightmap, Color_Diffusem), Color_Ambientm), color);
2585 *(int *)&pixel[x*4] = _mm_cvtsi128_si32(_mm_packus_epi16(pix, pix));
2588 if(pixel == buffer_FragColorbgra8)
2589 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2595 void DPSOFTRAST_VertexShader_FakeLight(void)
2597 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);
2600 void DPSOFTRAST_PixelShader_FakeLight(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2603 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2604 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2605 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2606 memset(buffer_FragColorbgra8, 0, span->length*4);
2607 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2612 void DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace(void)
2614 DPSOFTRAST_VertexShader_Lightmap();
2617 void DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2619 DPSOFTRAST_PixelShader_Lightmap(span);
2625 void DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace(void)
2627 DPSOFTRAST_VertexShader_Lightmap();
2630 void DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2632 DPSOFTRAST_PixelShader_Lightmap(span);
2638 void DPSOFTRAST_VertexShader_LightDirection(void)
2641 int numvertices = dpsoftrast.draw.numvertices;
2643 float LightVector[4];
2644 float EyePosition[4];
2645 float EyeVectorModelSpace[4];
2651 LightDir[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightDir*4+0];
2652 LightDir[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightDir*4+1];
2653 LightDir[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightDir*4+2];
2654 LightDir[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightDir*4+3];
2655 EyePosition[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+0];
2656 EyePosition[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+1];
2657 EyePosition[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+2];
2658 EyePosition[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+3];
2659 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);
2660 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);
2661 for (i = 0;i < numvertices;i++)
2663 position[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0];
2664 position[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1];
2665 position[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2];
2666 svector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0];
2667 svector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1];
2668 svector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2];
2669 tvector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0];
2670 tvector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1];
2671 tvector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2];
2672 normal[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0];
2673 normal[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1];
2674 normal[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2];
2675 LightVector[0] = svector[0] * LightDir[0] + svector[1] * LightDir[1] + svector[2] * LightDir[2];
2676 LightVector[1] = tvector[0] * LightDir[0] + tvector[1] * LightDir[1] + tvector[2] * LightDir[2];
2677 LightVector[2] = normal[0] * LightDir[0] + normal[1] * LightDir[1] + normal[2] * LightDir[2];
2678 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0] = LightVector[0];
2679 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1] = LightVector[1];
2680 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2] = LightVector[2];
2681 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+3] = 0.0f;
2682 EyeVectorModelSpace[0] = EyePosition[0] - position[0];
2683 EyeVectorModelSpace[1] = EyePosition[1] - position[1];
2684 EyeVectorModelSpace[2] = EyePosition[2] - position[2];
2685 EyeVector[0] = svector[0] * EyeVectorModelSpace[0] + svector[1] * EyeVectorModelSpace[1] + svector[2] * EyeVectorModelSpace[2];
2686 EyeVector[1] = tvector[0] * EyeVectorModelSpace[0] + tvector[1] * EyeVectorModelSpace[1] + tvector[2] * EyeVectorModelSpace[2];
2687 EyeVector[2] = normal[0] * EyeVectorModelSpace[0] + normal[1] * EyeVectorModelSpace[1] + normal[2] * EyeVectorModelSpace[2];
2688 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0] = EyeVector[0];
2689 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1] = EyeVector[1];
2690 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2] = EyeVector[2];
2691 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+3] = 0.0f;
2695 #define DPSOFTRAST_Min(a,b) ((a) < (b) ? (a) : (b))
2696 #define DPSOFTRAST_Max(a,b) ((a) > (b) ? (a) : (b))
2697 #define DPSOFTRAST_Vector3Dot(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
2698 #define DPSOFTRAST_Vector3LengthSquared(v) (DPSOFTRAST_Vector3Dot((v),(v)))
2699 #define DPSOFTRAST_Vector3Length(v) (sqrt(DPSOFTRAST_Vector3LengthSquared(v)))
2700 #define DPSOFTRAST_Vector3Normalize(v)\
2703 float len = sqrt(DPSOFTRAST_Vector3Dot(v,v));\
2714 void DPSOFTRAST_PixelShader_LightDirection(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
2716 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
2717 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2718 unsigned char buffer_texture_normalbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2719 unsigned char buffer_texture_glossbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2720 unsigned char buffer_texture_glowbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2721 unsigned char buffer_texture_pantsbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2722 unsigned char buffer_texture_shirtbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2723 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
2724 int x, startx = span->startx, endx = span->endx;
2725 float Color_Ambient[4], Color_Diffuse[4], Color_Specular[4], Color_Glow[4], Color_Pants[4], Color_Shirt[4], LightColor[4];
2726 float LightVectordata[4];
2727 float LightVectorslope[4];
2728 float EyeVectordata[4];
2729 float EyeVectorslope[4];
2731 float diffusetex[4];
2733 float surfacenormal[4];
2734 float lightnormal[4];
2736 float specularnormal[4];
2739 float SpecularPower;
2741 Color_Glow[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0];
2742 Color_Glow[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1];
2743 Color_Glow[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2];
2744 Color_Glow[3] = 0.0f;
2745 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
2746 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
2747 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
2748 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
2749 Color_Pants[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0];
2750 Color_Pants[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1];
2751 Color_Pants[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2];
2752 Color_Pants[3] = 0.0f;
2753 Color_Shirt[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0];
2754 Color_Shirt[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1];
2755 Color_Shirt[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2];
2756 Color_Shirt[3] = 0.0f;
2757 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
2758 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2759 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2761 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2762 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2764 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2766 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glowbgra8, GL20TU_GLOW, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2768 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
2770 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2771 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2772 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2773 Color_Diffuse[3] = 0.0f;
2774 LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0];
2775 LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1];
2776 LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2];
2777 LightColor[3] = 0.0f;
2778 LightVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0];
2779 LightVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1];
2780 LightVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2];
2781 LightVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3];
2782 LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0];
2783 LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1];
2784 LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2];
2785 LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3];
2786 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2787 Color_Specular[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0];
2788 Color_Specular[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1];
2789 Color_Specular[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2];
2790 Color_Specular[3] = 0.0f;
2791 SpecularPower = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f);
2792 EyeVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][0];
2793 EyeVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][1];
2794 EyeVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][2];
2795 EyeVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][3];
2796 EyeVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][0];
2797 EyeVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][1];
2798 EyeVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][2];
2799 EyeVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][3];
2800 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2801 for (x = startx;x < endx;x++)
2804 diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
2805 diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
2806 diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
2807 diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
2808 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
2810 diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
2811 diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
2812 diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
2813 diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
2815 glosstex[0] = buffer_texture_glossbgra8[x*4+0];
2816 glosstex[1] = buffer_texture_glossbgra8[x*4+1];
2817 glosstex[2] = buffer_texture_glossbgra8[x*4+2];
2818 glosstex[3] = buffer_texture_glossbgra8[x*4+3];
2819 surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
2820 surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
2821 surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
2822 DPSOFTRAST_Vector3Normalize(surfacenormal);
2824 lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
2825 lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
2826 lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
2827 DPSOFTRAST_Vector3Normalize(lightnormal);
2829 eyenormal[0] = (EyeVectordata[0] + EyeVectorslope[0]*x) * z;
2830 eyenormal[1] = (EyeVectordata[1] + EyeVectorslope[1]*x) * z;
2831 eyenormal[2] = (EyeVectordata[2] + EyeVectorslope[2]*x) * z;
2832 DPSOFTRAST_Vector3Normalize(eyenormal);
2834 specularnormal[0] = lightnormal[0] + eyenormal[0];
2835 specularnormal[1] = lightnormal[1] + eyenormal[1];
2836 specularnormal[2] = lightnormal[2] + eyenormal[2];
2837 DPSOFTRAST_Vector3Normalize(specularnormal);
2839 diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
2840 specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f;
2841 specular = pow(specular, SpecularPower * glosstex[3]);
2842 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2844 d[0] = (int)(buffer_texture_glowbgra8[x*4+0] * Color_Glow[0] + diffusetex[0] * Color_Ambient[0] + (diffusetex[0] * Color_Diffuse[0] * diffuse + glosstex[0] * Color_Specular[0] * specular) * LightColor[0]);if (d[0] > 255) d[0] = 255;
2845 d[1] = (int)(buffer_texture_glowbgra8[x*4+1] * Color_Glow[1] + diffusetex[1] * Color_Ambient[1] + (diffusetex[1] * Color_Diffuse[1] * diffuse + glosstex[1] * Color_Specular[1] * specular) * LightColor[1]);if (d[1] > 255) d[1] = 255;
2846 d[2] = (int)(buffer_texture_glowbgra8[x*4+2] * Color_Glow[2] + diffusetex[2] * Color_Ambient[2] + (diffusetex[2] * Color_Diffuse[2] * diffuse + glosstex[2] * Color_Specular[2] * specular) * LightColor[2]);if (d[2] > 255) d[2] = 255;
2847 d[3] = (int)( diffusetex[3] * Color_Ambient[3]);if (d[3] > 255) d[3] = 255;
2851 d[0] = (int)( diffusetex[0] * Color_Ambient[0] + (diffusetex[0] * Color_Diffuse[0] * diffuse + glosstex[0] * Color_Specular[0] * specular) * LightColor[0]);if (d[0] > 255) d[0] = 255;
2852 d[1] = (int)( diffusetex[1] * Color_Ambient[1] + (diffusetex[1] * Color_Diffuse[1] * diffuse + glosstex[1] * Color_Specular[1] * specular) * LightColor[1]);if (d[1] > 255) d[1] = 255;
2853 d[2] = (int)( diffusetex[2] * Color_Ambient[2] + (diffusetex[2] * Color_Diffuse[2] * diffuse + glosstex[2] * Color_Specular[2] * specular) * LightColor[2]);if (d[2] > 255) d[2] = 255;
2854 d[3] = (int)( diffusetex[3] * Color_Ambient[3]);if (d[3] > 255) d[3] = 255;
2856 buffer_FragColorbgra8[x*4+0] = d[0];
2857 buffer_FragColorbgra8[x*4+1] = d[1];
2858 buffer_FragColorbgra8[x*4+2] = d[2];
2859 buffer_FragColorbgra8[x*4+3] = d[3];
2862 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
2864 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
2865 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
2866 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
2867 Color_Diffuse[3] = 0.0f;
2868 LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0];
2869 LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1];
2870 LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2];
2871 LightColor[3] = 0.0f;
2872 LightVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0];
2873 LightVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1];
2874 LightVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2];
2875 LightVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3];
2876 LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0];
2877 LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1];
2878 LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2];
2879 LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3];
2880 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
2881 for (x = startx;x < endx;x++)
2884 diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
2885 diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
2886 diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
2887 diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
2888 surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
2889 surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
2890 surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
2891 DPSOFTRAST_Vector3Normalize(surfacenormal);
2893 lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
2894 lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
2895 lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
2896 DPSOFTRAST_Vector3Normalize(lightnormal);
2898 diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
2899 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2901 d[0] = (int)(buffer_texture_glowbgra8[x*4+0] * Color_Glow[0] + diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse * LightColor[0]));if (d[0] > 255) d[0] = 255;
2902 d[1] = (int)(buffer_texture_glowbgra8[x*4+1] * Color_Glow[1] + diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse * LightColor[1]));if (d[1] > 255) d[1] = 255;
2903 d[2] = (int)(buffer_texture_glowbgra8[x*4+2] * Color_Glow[2] + diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse * LightColor[2]));if (d[2] > 255) d[2] = 255;
2904 d[3] = (int)( diffusetex[3] * (Color_Ambient[3] ));if (d[3] > 255) d[3] = 255;
2908 d[0] = (int)( + diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse * LightColor[0]));if (d[0] > 255) d[0] = 255;
2909 d[1] = (int)( + diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse * LightColor[1]));if (d[1] > 255) d[1] = 255;
2910 d[2] = (int)( + diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse * LightColor[2]));if (d[2] > 255) d[2] = 255;
2911 d[3] = (int)( diffusetex[3] * (Color_Ambient[3] ));if (d[3] > 255) d[3] = 255;
2913 buffer_FragColorbgra8[x*4+0] = d[0];
2914 buffer_FragColorbgra8[x*4+1] = d[1];
2915 buffer_FragColorbgra8[x*4+2] = d[2];
2916 buffer_FragColorbgra8[x*4+3] = d[3];
2921 for (x = startx;x < endx;x++)
2924 diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
2925 diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
2926 diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
2927 diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
2929 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_GLOW)
2931 d[0] = (int)(buffer_texture_glowbgra8[x*4+0] * Color_Glow[0] + diffusetex[0] * Color_Ambient[0]);if (d[0] > 255) d[0] = 255;
2932 d[1] = (int)(buffer_texture_glowbgra8[x*4+1] * Color_Glow[1] + diffusetex[1] * Color_Ambient[1]);if (d[1] > 255) d[1] = 255;
2933 d[2] = (int)(buffer_texture_glowbgra8[x*4+2] * Color_Glow[2] + diffusetex[2] * Color_Ambient[2]);if (d[2] > 255) d[2] = 255;
2934 d[3] = (int)( diffusetex[3] * Color_Ambient[3]);if (d[3] > 255) d[3] = 255;
2938 d[0] = (int)( diffusetex[0] * Color_Ambient[0]);if (d[0] > 255) d[0] = 255;
2939 d[1] = (int)( diffusetex[1] * Color_Ambient[1]);if (d[1] > 255) d[1] = 255;
2940 d[2] = (int)( diffusetex[2] * Color_Ambient[2]);if (d[2] > 255) d[2] = 255;
2941 d[3] = (int)( diffusetex[3] * Color_Ambient[3]);if (d[3] > 255) d[3] = 255;
2943 buffer_FragColorbgra8[x*4+0] = d[0];
2944 buffer_FragColorbgra8[x*4+1] = d[1];
2945 buffer_FragColorbgra8[x*4+2] = d[2];
2946 buffer_FragColorbgra8[x*4+3] = d[3];
2949 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
2954 void DPSOFTRAST_VertexShader_LightSource(void)
2957 int numvertices = dpsoftrast.draw.numvertices;
2958 float LightPosition[4];
2959 float LightVector[4];
2960 float LightVectorModelSpace[4];
2961 float EyePosition[4];
2962 float EyeVectorModelSpace[4];
2968 LightPosition[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightPosition*4+0];
2969 LightPosition[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightPosition*4+1];
2970 LightPosition[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightPosition*4+2];
2971 LightPosition[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightPosition*4+3];
2972 EyePosition[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+0];
2973 EyePosition[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+1];
2974 EyePosition[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+2];
2975 EyePosition[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_EyePosition*4+3];
2976 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);
2977 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);
2978 DPSOFTRAST_Array_Transform(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD3], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION], dpsoftrast.draw.numvertices, dpsoftrast.uniform4f + 4*DPSOFTRAST_UNIFORM_ModelToLightM1);
2979 DPSOFTRAST_Array_Copy(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD4], dpsoftrast.draw.numvertices);
2980 for (i = 0;i < numvertices;i++)
2982 position[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0];
2983 position[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1];
2984 position[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+2];
2985 svector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0];
2986 svector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1];
2987 svector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2];
2988 tvector[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0];
2989 tvector[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1];
2990 tvector[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2];
2991 normal[0] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+0];
2992 normal[1] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+1];
2993 normal[2] = dpsoftrast.draw.in_array4f[DPSOFTRAST_ARRAY_TEXCOORD3][i*4+2];
2994 LightVectorModelSpace[0] = LightPosition[0] - position[0];
2995 LightVectorModelSpace[1] = LightPosition[1] - position[1];
2996 LightVectorModelSpace[2] = LightPosition[2] - position[2];
2997 LightVector[0] = svector[0] * LightVectorModelSpace[0] + svector[1] * LightVectorModelSpace[1] + svector[2] * LightVectorModelSpace[2];
2998 LightVector[1] = tvector[0] * LightVectorModelSpace[0] + tvector[1] * LightVectorModelSpace[1] + tvector[2] * LightVectorModelSpace[2];
2999 LightVector[2] = normal[0] * LightVectorModelSpace[0] + normal[1] * LightVectorModelSpace[1] + normal[2] * LightVectorModelSpace[2];
3000 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+0] = LightVector[0];
3001 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+1] = LightVector[1];
3002 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+2] = LightVector[2];
3003 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD1][i*4+3] = 0.0f;
3004 EyeVectorModelSpace[0] = EyePosition[0] - position[0];
3005 EyeVectorModelSpace[1] = EyePosition[1] - position[1];
3006 EyeVectorModelSpace[2] = EyePosition[2] - position[2];
3007 EyeVector[0] = svector[0] * EyeVectorModelSpace[0] + svector[1] * EyeVectorModelSpace[1] + svector[2] * EyeVectorModelSpace[2];
3008 EyeVector[1] = tvector[0] * EyeVectorModelSpace[0] + tvector[1] * EyeVectorModelSpace[1] + tvector[2] * EyeVectorModelSpace[2];
3009 EyeVector[2] = normal[0] * EyeVectorModelSpace[0] + normal[1] * EyeVectorModelSpace[1] + normal[2] * EyeVectorModelSpace[2];
3010 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+0] = EyeVector[0];
3011 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+1] = EyeVector[1];
3012 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+2] = EyeVector[2];
3013 dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_TEXCOORD2][i*4+3] = 0.0f;
3017 void DPSOFTRAST_PixelShader_LightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3019 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3020 unsigned char buffer_texture_colorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3021 unsigned char buffer_texture_normalbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3022 unsigned char buffer_texture_glossbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3023 unsigned char buffer_texture_cubebgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3024 unsigned char buffer_texture_pantsbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3025 unsigned char buffer_texture_shirtbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3026 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3027 int x, startx = span->startx, endx = span->endx;
3028 float Color_Ambient[4], Color_Diffuse[4], Color_Specular[4], Color_Glow[4], Color_Pants[4], Color_Shirt[4], LightColor[4];
3029 float CubeVectordata[4];
3030 float CubeVectorslope[4];
3031 float LightVectordata[4];
3032 float LightVectorslope[4];
3033 float EyeVectordata[4];
3034 float EyeVectorslope[4];
3036 float diffusetex[4];
3038 float surfacenormal[4];
3039 float lightnormal[4];
3041 float specularnormal[4];
3044 float SpecularPower;
3045 float CubeVector[4];
3048 Color_Glow[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0];
3049 Color_Glow[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1];
3050 Color_Glow[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2];
3051 Color_Glow[3] = 0.0f;
3052 Color_Ambient[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
3053 Color_Ambient[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
3054 Color_Ambient[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
3055 Color_Ambient[3] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
3056 Color_Diffuse[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
3057 Color_Diffuse[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
3058 Color_Diffuse[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
3059 Color_Diffuse[3] = 0.0f;
3060 Color_Specular[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0];
3061 Color_Specular[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1];
3062 Color_Specular[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2];
3063 Color_Specular[3] = 0.0f;
3064 Color_Pants[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0];
3065 Color_Pants[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1];
3066 Color_Pants[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2];
3067 Color_Pants[3] = 0.0f;
3068 Color_Shirt[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0];
3069 Color_Shirt[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1];
3070 Color_Shirt[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2];
3071 Color_Shirt[3] = 0.0f;
3072 LightColor[2] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0];
3073 LightColor[1] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1];
3074 LightColor[0] = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2];
3075 LightColor[3] = 0.0f;
3076 SpecularPower = dpsoftrast.uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f);
3077 EyeVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][0];
3078 EyeVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][1];
3079 EyeVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][2];
3080 EyeVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD2][3];
3081 EyeVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][0];
3082 EyeVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][1];
3083 EyeVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][2];
3084 EyeVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD2][3];
3085 LightVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][0];
3086 LightVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][1];
3087 LightVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][2];
3088 LightVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD1][3];
3089 LightVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][0];
3090 LightVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][1];
3091 LightVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][2];
3092 LightVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD1][3];
3093 CubeVectordata[0] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][0];
3094 CubeVectordata[1] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][1];
3095 CubeVectordata[2] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][2];
3096 CubeVectordata[3] = span->data[0][DPSOFTRAST_ARRAY_TEXCOORD3][3];
3097 CubeVectorslope[0] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][0];
3098 CubeVectorslope[1] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][1];
3099 CubeVectorslope[2] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][2];
3100 CubeVectorslope[3] = span->data[1][DPSOFTRAST_ARRAY_TEXCOORD3][3];
3101 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3102 memset(buffer_FragColorbgra8 + startx*4, 0, (endx-startx)*4); // clear first, because we skip writing black pixels, and there are a LOT of them...
3103 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3104 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
3106 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3107 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3109 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER)
3110 DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(span, buffer_texture_cubebgra8, GL20TU_CUBE, DPSOFTRAST_ARRAY_TEXCOORD3, buffer_z);
3111 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SPECULAR)
3113 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3114 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3115 for (x = startx;x < endx;x++)
3118 CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
3119 CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
3120 CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
3121 attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
3122 if (attenuation < 0.01f)
3124 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
3126 attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
3127 if (attenuation < 0.01f)
3131 diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
3132 diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
3133 diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
3134 diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
3135 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
3137 diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
3138 diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
3139 diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
3140 diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
3142 glosstex[0] = buffer_texture_glossbgra8[x*4+0];
3143 glosstex[1] = buffer_texture_glossbgra8[x*4+1];
3144 glosstex[2] = buffer_texture_glossbgra8[x*4+2];
3145 glosstex[3] = buffer_texture_glossbgra8[x*4+3];
3146 surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
3147 surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
3148 surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
3149 DPSOFTRAST_Vector3Normalize(surfacenormal);
3151 lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
3152 lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
3153 lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
3154 DPSOFTRAST_Vector3Normalize(lightnormal);
3156 eyenormal[0] = (EyeVectordata[0] + EyeVectorslope[0]*x) * z;
3157 eyenormal[1] = (EyeVectordata[1] + EyeVectorslope[1]*x) * z;
3158 eyenormal[2] = (EyeVectordata[2] + EyeVectorslope[2]*x) * z;
3159 DPSOFTRAST_Vector3Normalize(eyenormal);
3161 specularnormal[0] = lightnormal[0] + eyenormal[0];
3162 specularnormal[1] = lightnormal[1] + eyenormal[1];
3163 specularnormal[2] = lightnormal[2] + eyenormal[2];
3164 DPSOFTRAST_Vector3Normalize(specularnormal);
3166 diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
3167 specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f;
3168 specular = pow(specular, SpecularPower * glosstex[3]);
3169 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER)
3171 // scale down the attenuation to account for the cubefilter multiplying everything by 255
3172 attenuation *= (1.0f / 255.0f);
3173 d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse) + glosstex[0] * Color_Specular[0] * specular) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
3174 d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse) + glosstex[1] * Color_Specular[1] * specular) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
3175 d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse) + glosstex[2] * Color_Specular[2] * specular) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
3176 d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
3180 d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse) + glosstex[0] * Color_Specular[0] * specular) * LightColor[0] * attenuation);if (d[0] > 255) d[0] = 255;
3181 d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse) + glosstex[1] * Color_Specular[1] * specular) * LightColor[1] * attenuation);if (d[1] > 255) d[1] = 255;
3182 d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse) + glosstex[2] * Color_Specular[2] * specular) * LightColor[2] * attenuation);if (d[2] > 255) d[2] = 255;
3183 d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
3185 buffer_FragColorbgra8[x*4+0] = d[0];
3186 buffer_FragColorbgra8[x*4+1] = d[1];
3187 buffer_FragColorbgra8[x*4+2] = d[2];
3188 buffer_FragColorbgra8[x*4+3] = d[3];
3191 else if (dpsoftrast.shader_permutation & SHADERPERMUTATION_DIFFUSE)
3193 DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
3194 for (x = startx;x < endx;x++)
3197 CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
3198 CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
3199 CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
3200 attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
3201 if (attenuation < 0.01f)
3203 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
3205 attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
3206 if (attenuation < 0.01f)
3210 diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
3211 diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
3212 diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
3213 diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
3214 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
3216 diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
3217 diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
3218 diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
3219 diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
3221 surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
3222 surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
3223 surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
3224 DPSOFTRAST_Vector3Normalize(surfacenormal);
3226 lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
3227 lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
3228 lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
3229 DPSOFTRAST_Vector3Normalize(lightnormal);
3231 diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
3232 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER)
3234 // scale down the attenuation to account for the cubefilter multiplying everything by 255
3235 attenuation *= (1.0f / 255.0f);
3236 d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse)) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
3237 d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse)) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
3238 d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse)) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
3239 d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
3243 d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse)) * LightColor[0] * attenuation);if (d[0] > 255) d[0] = 255;
3244 d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse)) * LightColor[1] * attenuation);if (d[1] > 255) d[1] = 255;
3245 d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse)) * LightColor[2] * attenuation);if (d[2] > 255) d[2] = 255;
3246 d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
3248 buffer_FragColorbgra8[x*4+0] = d[0];
3249 buffer_FragColorbgra8[x*4+1] = d[1];
3250 buffer_FragColorbgra8[x*4+2] = d[2];
3251 buffer_FragColorbgra8[x*4+3] = d[3];
3256 for (x = startx;x < endx;x++)
3259 CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
3260 CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
3261 CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
3262 attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
3263 if (attenuation < 0.01f)
3265 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
3267 attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
3268 if (attenuation < 0.01f)
3272 diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
3273 diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
3274 diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
3275 diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
3276 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_COLORMAPPING)
3278 diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
3279 diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
3280 diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
3281 diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
3283 if (dpsoftrast.shader_permutation & SHADERPERMUTATION_CUBEFILTER)
3285 // scale down the attenuation to account for the cubefilter multiplying everything by 255
3286 attenuation *= (1.0f / 255.0f);
3287 d[0] = (int)((diffusetex[0] * (Color_Ambient[0])) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
3288 d[1] = (int)((diffusetex[1] * (Color_Ambient[1])) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
3289 d[2] = (int)((diffusetex[2] * (Color_Ambient[2])) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
3290 d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
3294 d[0] = (int)((diffusetex[0] * (Color_Ambient[0])) * LightColor[0] * attenuation);if (d[0] > 255) d[0] = 255;
3295 d[1] = (int)((diffusetex[1] * (Color_Ambient[1])) * LightColor[1] * attenuation);if (d[1] > 255) d[1] = 255;
3296 d[2] = (int)((diffusetex[2] * (Color_Ambient[2])) * LightColor[2] * attenuation);if (d[2] > 255) d[2] = 255;
3297 d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
3299 buffer_FragColorbgra8[x*4+0] = d[0];
3300 buffer_FragColorbgra8[x*4+1] = d[1];
3301 buffer_FragColorbgra8[x*4+2] = d[2];
3302 buffer_FragColorbgra8[x*4+3] = d[3];
3305 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3310 void DPSOFTRAST_VertexShader_Refraction(void)
3312 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);
3315 void DPSOFTRAST_PixelShader_Refraction(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3318 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3319 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3320 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3321 memset(buffer_FragColorbgra8, 0, span->length*4);
3322 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3327 void DPSOFTRAST_VertexShader_Water(void)
3329 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);
3333 void DPSOFTRAST_PixelShader_Water(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3336 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3337 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3338 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3339 memset(buffer_FragColorbgra8, 0, span->length*4);
3340 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3345 void DPSOFTRAST_VertexShader_ShowDepth(void)
3347 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);
3350 void DPSOFTRAST_PixelShader_ShowDepth(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3353 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3354 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3355 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3356 memset(buffer_FragColorbgra8, 0, span->length*4);
3357 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3362 void DPSOFTRAST_VertexShader_DeferredGeometry(void)
3364 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);
3367 void DPSOFTRAST_PixelShader_DeferredGeometry(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3370 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3371 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3372 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3373 memset(buffer_FragColorbgra8, 0, span->length*4);
3374 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3379 void DPSOFTRAST_VertexShader_DeferredLightSource(void)
3381 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);
3384 void DPSOFTRAST_PixelShader_DeferredLightSource(const DPSOFTRAST_State_Draw_Span * RESTRICT span)
3387 float buffer_z[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3388 unsigned char buffer_FragColorbgra8[DPSOFTRAST_DRAW_MAXSPANLENGTH*4];
3389 DPSOFTRAST_Draw_Span_Begin(span, buffer_z);
3390 memset(buffer_FragColorbgra8, 0, span->length*4);
3391 DPSOFTRAST_Draw_Span_FinishBGRA8(span, buffer_FragColorbgra8);
3396 typedef struct DPSOFTRAST_ShaderModeInfo_s
3399 void (*Vertex)(void);
3400 void (*Span)(const DPSOFTRAST_State_Draw_Span * RESTRICT span);
3402 DPSOFTRAST_ShaderModeInfo;
3404 DPSOFTRAST_ShaderModeInfo DPSOFTRAST_ShaderModeTable[SHADERMODE_COUNT] =
3406 {2, DPSOFTRAST_VertexShader_Generic, DPSOFTRAST_PixelShader_Generic, },
3407 {2, DPSOFTRAST_VertexShader_PostProcess, DPSOFTRAST_PixelShader_PostProcess, },
3408 {2, DPSOFTRAST_VertexShader_Depth_Or_Shadow, DPSOFTRAST_PixelShader_Depth_Or_Shadow, },
3409 {2, DPSOFTRAST_VertexShader_FlatColor, DPSOFTRAST_PixelShader_FlatColor, },
3410 {2, DPSOFTRAST_VertexShader_VertexColor, DPSOFTRAST_PixelShader_VertexColor, },
3411 {2, DPSOFTRAST_VertexShader_Lightmap, DPSOFTRAST_PixelShader_Lightmap, },
3412 {2, DPSOFTRAST_VertexShader_FakeLight, DPSOFTRAST_PixelShader_FakeLight, },
3413 {2, DPSOFTRAST_VertexShader_LightDirectionMap_ModelSpace, DPSOFTRAST_PixelShader_LightDirectionMap_ModelSpace, },
3414 {2, DPSOFTRAST_VertexShader_LightDirectionMap_TangentSpace, DPSOFTRAST_PixelShader_LightDirectionMap_TangentSpace},
3415 {2, DPSOFTRAST_VertexShader_LightDirection, DPSOFTRAST_PixelShader_LightDirection, },
3416 {2, DPSOFTRAST_VertexShader_LightSource, DPSOFTRAST_PixelShader_LightSource, },
3417 {2, DPSOFTRAST_VertexShader_Refraction, DPSOFTRAST_PixelShader_Refraction, },
3418 {2, DPSOFTRAST_VertexShader_Water, DPSOFTRAST_PixelShader_Water, },
3419 {2, DPSOFTRAST_VertexShader_ShowDepth, DPSOFTRAST_PixelShader_ShowDepth, },
3420 {2, DPSOFTRAST_VertexShader_DeferredGeometry, DPSOFTRAST_PixelShader_DeferredGeometry, },
3421 {2, DPSOFTRAST_VertexShader_DeferredLightSource, DPSOFTRAST_PixelShader_DeferredLightSource, }
3426 void DPSOFTRAST_Draw_ProcessSpans(void)
3432 int numspans = dpsoftrast.draw.numspans;
3434 // unsigned int *colorpixel;
3435 unsigned int *depthpixel;
3441 DPSOFTRAST_State_Draw_Span *span = dpsoftrast.draw.spanqueue;
3442 unsigned char pixelmask[DPSOFTRAST_DRAW_MAXSPANLENGTH];
3443 for (i = 0;i < numspans;i++, span++)
3445 w = span->data[0][DPSOFTRAST_ARRAY_TOTAL][3];
3446 wslope = span->data[1][DPSOFTRAST_ARRAY_TOTAL][3];
3447 if (dpsoftrast.user.depthtest && dpsoftrast.fb_depthpixels)
3449 depthslope = (int)(wslope*DPSOFTRAST_DEPTHSCALE);
3450 depth = (int)(w*DPSOFTRAST_DEPTHSCALE - DPSOFTRAST_DEPTHOFFSET*(dpsoftrast.user.polygonoffset[1] + fabs(wslope)*dpsoftrast.user.polygonoffset[0]));
3451 depthpixel = dpsoftrast.fb_depthpixels + span->start;
3452 switch(dpsoftrast.fb_depthfunc)
3455 case GL_ALWAYS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = true; break;
3456 case GL_LESS: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] < d; break;
3457 case GL_LEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] <= d; break;
3458 case GL_EQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] == d; break;
3459 case GL_GEQUAL: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] >= d; break;
3460 case GL_GREATER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = depthpixel[x] > d; break;
3461 case GL_NEVER: for (x = 0, d = depth;x < span->length;x++, d += depthslope) pixelmask[x] = false; break;
3463 //colorpixel = dpsoftrast.fb_colorpixels[0] + span->start;
3464 //for (x = 0;x < span->length;x++)
3465 // colorpixel[x] = (depthpixel[x] & 0xFF000000) ? (0x00FF0000) : (depthpixel[x] & 0x00FF0000);
3466 // if there is no color buffer, skip pixel shader
3468 endx = span->length;
3469 while (startx < endx && !pixelmask[startx])
3471 while (endx > startx && !pixelmask[endx-1])
3474 continue; // no pixels to fill
3475 span->pixelmask = pixelmask;
3476 span->startx = startx;
3478 // run pixel shader if appropriate
3479 // do this before running depthmask code, to allow the pixelshader
3480 // to clear pixelmask values for alpha testing
3481 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
3482 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
3483 if (dpsoftrast.user.depthmask)
3484 for (x = startx, d = depth + depthslope*startx;x < endx;x++, d += depthslope)
3490 // no depth testing means we're just dealing with color...
3491 // if there is no color buffer, skip pixel shader
3492 if (dpsoftrast.fb_colorpixels[0] && dpsoftrast.fb_colormask)
3494 memset(pixelmask, 1, span->length);
3495 span->pixelmask = pixelmask;
3497 span->endx = span->length;
3498 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Span(span);
3504 void DPSOFTRAST_Draw_ProcessTriangles(int firstvertex, int numtriangles, const int *element3i, const unsigned short *element3s, unsigned char *arraymask)
3507 int cullface = dpsoftrast.user.cullface;
3508 int width = dpsoftrast.fb_width;
3509 int height = dpsoftrast.fb_height;
3510 __m128i fbmax = _mm_sub_epi16(_mm_setr_epi16(width, height, width, height, width, height, width, height), _mm_set1_epi16(1));
3525 unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS];
3526 __m128 mipedgescale;
3529 __m128 clipped[DPSOFTRAST_ARRAY_TOTAL][4];
3531 __m128 proj[DPSOFTRAST_ARRAY_TOTAL][4];
3532 DPSOFTRAST_Texture *texture;
3533 DPSOFTRAST_State_Draw_Span *span;
3534 DPSOFTRAST_State_Draw_Span *oldspan;
3535 for (i = 0;i < numtriangles;i++)
3537 // generate the 3 edges of this triangle
3538 // generate spans for the triangle - switch based on left split or right split classification of triangle
3541 e[0] = element3i[i*3+0] - firstvertex;
3542 e[1] = element3i[i*3+1] - firstvertex;
3543 e[2] = element3i[i*3+2] - firstvertex;
3547 e[0] = element3s[i*3+0] - firstvertex;
3548 e[1] = element3s[i*3+1] - firstvertex;
3549 e[2] = element3s[i*3+2] - firstvertex;
3558 #define SKIPBACKFACE \
3559 if(cullface != GL_NONE) \
3561 __m128 triangleedge[2] = { _mm_sub_ps(screen[0], screen[1]), _mm_sub_ps(screen[2], screen[1]) }; \
3562 /* store normal in 2, 0, 1 order instead of 0, 1, 2 as it requires fewer shuffles and leaves z component accessible as scalar */ \
3563 __m128 trianglenormal = _mm_sub_ss(_mm_mul_ss(triangleedge[0], _mm_shuffle_ps(triangleedge[1], triangleedge[1], _MM_SHUFFLE(3, 0, 2, 1))), \
3564 _mm_mul_ss(_mm_shuffle_ps(triangleedge[0], triangleedge[0], _MM_SHUFFLE(3, 0, 2, 1)), triangleedge[1])); \
3565 /* apply current cullface mode (this culls many triangles) */ \
3569 if (_mm_ucomilt_ss(trianglenormal, _mm_setzero_ps())) \
3573 if (_mm_ucomigt_ss(trianglenormal, _mm_setzero_ps())) \
3578 //trianglenormal = _mm_sub_ps(_mm_mul_ps(triangleedge[0], _mm_shuffle_ps(triangleedge[1], triangleedge[1], _MM_SHUFFLE(3, 0, 2, 1))),
3579 // _mm_mul_ps(_mm_shuffle_ps(triangleedge[0], triangleedge[0], _MM_SHUFFLE(3, 0, 2, 1)), triangleedge[1]));
3580 //trianglenormal[2] = triangleedge[0][0] * triangleedge[1][1] - triangleedge[0][1] * triangleedge[1][0];
3581 //trianglenormal[0] = triangleedge[0][1] * triangleedge[1][2] - triangleedge[0][2] * triangleedge[1][1];
3582 //trianglenormal[1] = triangleedge[0][2] * triangleedge[1][0] - triangleedge[0][0] * triangleedge[1][2];
3584 // macros for clipping vertices
3585 #define CLIPPEDVERTEXLERP(k,p1, p2) \
3586 clipfrac[k] = _mm_set1_ps(clipdist[p1] / (clipdist[p1] - clipdist[p2])); \
3588 __m128 v1 = _mm_load_ps(&dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[p1]*4]), v2 = _mm_load_ps(&dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[p2]*4]); \
3589 clipped[DPSOFTRAST_ARRAY_POSITION][k] = _mm_add_ps(v1, _mm_mul_ps(_mm_sub_ps(v2, v1), clipfrac[k])); \
3591 screen[k] = DPSOFTRAST_Draw_ProjectVertex(clipped[DPSOFTRAST_ARRAY_POSITION][k]);
3592 #define CLIPPEDATTRIBSLERP(k,p1,p2) \
3593 for (j = DPSOFTRAST_ARRAY_POSITION+1;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
3595 /*if (arraymask[j])*/\
3597 __m128 v1 = _mm_load_ps(&dpsoftrast.draw.post_array4f[j][e[p1]*4]), v2 = _mm_load_ps(&dpsoftrast.draw.post_array4f[j][e[p2]*4]); \
3598 clipped[j][k] = _mm_add_ps(v1, _mm_mul_ps(_mm_sub_ps(v2, v1), clipfrac[k])); \
3601 #define CLIPPEDVERTEXCOPY(k,p1) \
3602 screen[k] = _mm_load_ps(&dpsoftrast.draw.screencoord4f[e[p1]*4]);
3603 #define CLIPPEDATTRIBSCOPY(k,p1) \
3604 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)\
3606 /*if (arraymask[j])*/\
3608 clipped[j][k] = _mm_load_ps(&dpsoftrast.draw.post_array4f[j][e[p1]*4]); \
3612 // calculate distance from nearplane
3613 clipdist[0] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[0]*4+3];
3614 clipdist[1] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[1]*4+3];
3615 clipdist[2] = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+2] + dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][e[2]*4+3];
3616 if (clipdist[0] >= 0.0f)
3618 if (clipdist[1] >= 0.0f)
3620 if (clipdist[2] >= 0.0f)
3622 // triangle is entirely in front of nearplane
3623 CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXCOPY(2,2);
3626 CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSCOPY(2,2);
3630 CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXLERP(2,1,2); CLIPPEDVERTEXLERP(3,2,0);
3633 CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSLERP(2,1,2); CLIPPEDATTRIBSLERP(3,2,0);
3638 if (clipdist[2] >= 0.0f)
3640 CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXLERP(1,0,1); CLIPPEDVERTEXLERP(2,1,2); CLIPPEDVERTEXCOPY(3,2);
3643 CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSLERP(1,0,1); CLIPPEDATTRIBSLERP(2,1,2); CLIPPEDATTRIBSCOPY(3,2);
3647 CLIPPEDVERTEXCOPY(0,0); CLIPPEDVERTEXLERP(1,0,1); CLIPPEDVERTEXLERP(2,2,0);
3650 CLIPPEDATTRIBSCOPY(0,0); CLIPPEDATTRIBSLERP(1,0,1); CLIPPEDATTRIBSLERP(2,2,0);
3654 else if (clipdist[1] >= 0.0f)
3656 if (clipdist[2] >= 0.0f)
3658 CLIPPEDVERTEXLERP(0,0,1); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXCOPY(2,2); CLIPPEDVERTEXLERP(3,2,0);
3661 CLIPPEDATTRIBSLERP(0,0,1); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSCOPY(2,2); CLIPPEDATTRIBSLERP(3,2,0);
3665 CLIPPEDVERTEXLERP(0,0,1); CLIPPEDVERTEXCOPY(1,1); CLIPPEDVERTEXLERP(2,1,2);
3668 CLIPPEDATTRIBSLERP(0,0,1); CLIPPEDATTRIBSCOPY(1,1); CLIPPEDATTRIBSLERP(2,1,2);
3671 else if (clipdist[2] >= 0.0f)
3673 CLIPPEDVERTEXLERP(0,1,2); CLIPPEDVERTEXCOPY(1,2); CLIPPEDVERTEXLERP(2,2,0);
3676 CLIPPEDATTRIBSLERP(0,1,2); CLIPPEDATTRIBSCOPY(1,2); CLIPPEDATTRIBSLERP(2,2,0);
3678 else continue; // triangle is entirely behind nearplane
3681 // calculate integer y coords for triangle points
3682 __m128i screeni = _mm_packs_epi32(_mm_cvttps_epi32(_mm_shuffle_ps(screen[0], screen[1], _MM_SHUFFLE(1, 0, 1, 0))),
3683 _mm_cvttps_epi32(_mm_shuffle_ps(screen[2], numpoints <= 3 ? screen[2] : screen[3], _MM_SHUFFLE(1, 0, 1, 0)))),
3684 screenir = _mm_shuffle_epi32(screeni, _MM_SHUFFLE(1, 0, 3, 2)),
3685 screenmin = _mm_min_epi16(screeni, screenir),
3686 screenmax = _mm_max_epi16(screeni, screenir);
3687 screenmin = _mm_min_epi16(screenmin, _mm_shufflelo_epi16(screenmin, _MM_SHUFFLE(1, 0, 3, 2)));
3688 screenmax = _mm_max_epi16(screenmax, _mm_shufflelo_epi16(screenmax, _MM_SHUFFLE(1, 0, 3, 2)));
3689 screenmin = _mm_max_epi16(screenmin, _mm_setzero_si128());
3690 screenmax = _mm_min_epi16(screenmax, fbmax);
3691 // skip offscreen triangles
3692 if (_mm_cvtsi128_si32(_mm_cmplt_epi16(screenmax, screenmin)))
3694 starty = _mm_extract_epi16(screenmin, 1);
3695 endy = _mm_extract_epi16(screenmax, 1)+1;
3696 screeny = _mm_srai_epi32(screeni, 16);
3699 // okay, this triangle is going to produce spans, we'd better project
3700 // the interpolants now (this is what gives perspective texturing),
3701 // this consists of simply multiplying all arrays by the W coord
3702 // (which is basically 1/Z), which will be undone per-pixel
3703 // (multiplying by Z again) to get the perspective-correct array
3705 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3709 for (k = 0;k < numpoints;k++)
3711 proj[j][k] = _mm_mul_ps(clipped[j][k], _mm_shuffle_ps(screen[k], screen[k], _MM_SHUFFLE(3, 3, 3, 3)));
3715 // adjust texture LOD by texture density, in the simplest way possible...
3716 mipedgescale = _mm_sub_ps(_mm_shuffle_ps(screen[0], screen[2], _MM_SHUFFLE(1, 0, 1, 0)), _mm_shuffle_ps(screen[1], screen[1], _MM_SHUFFLE(1, 0, 1, 0)));
3717 mipedgescale = _mm_mul_ps(mipedgescale, mipedgescale);
3718 mipedgescale = _mm_div_ps(_mm_set1_ps(1.0f), _mm_add_ps(mipedgescale, _mm_shuffle_ps(mipedgescale, mipedgescale, _MM_SHUFFLE(2, 3, 0, 1))));
3719 for (j = 0;j < DPSOFTRAST_MAXTEXTUREUNITS;j++)
3721 texture = dpsoftrast.texbound[j];
3725 if (texture->filter <= DPSOFTRAST_TEXTURE_FILTER_LINEAR)
3730 k = DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].lodarrayindex;
3731 mipedgetc = _mm_sub_ps(_mm_shuffle_ps(clipped[k][0], clipped[k][2], _MM_SHUFFLE(1, 0, 1, 0)),
3732 _mm_shuffle_ps(clipped[k][1], clipped[k][1], _MM_SHUFFLE(1, 0, 1, 0)));
3733 mipedgetc = _mm_mul_ps(mipedgetc, _mm_cvtepi32_ps(_mm_shuffle_epi32(_mm_loadl_epi64((const __m128i *)&texture->mipmap[0][2]), _MM_SHUFFLE(1, 0, 1, 0))));
3734 mipedgetc = _mm_mul_ps(mipedgetc, mipedgetc);
3735 mipedgetc = _mm_add_ps(mipedgetc, _mm_shuffle_ps(mipedgetc, mipedgetc, _MM_SHUFFLE(2, 3, 0, 1)));
3736 mipedgetc = _mm_mul_ps(mipedgetc, mipedgescale);
3737 mipedgetc = _mm_min_ss(mipedgetc, _mm_shuffle_ps(mipedgetc, mipedgetc, _MM_SHUFFLE(2, 2, 2, 2)));
3738 // this will be multiplied in the texturing routine by the texture resolution
3739 y = _mm_cvtss_si32(mipedgetc);
3742 y = (int)(log(y)/M_LN2);
3743 if (y > texture->mipmaps - 1)
3744 y = texture->mipmaps - 1;
3750 // iterate potential spans
3751 // TODO: optimize? if we figured out the edge order beforehand, this
3752 // could do loops over the edges in the proper order rather than
3753 // selecting them for each span
3754 // TODO: optimize? the edges could have data slopes calculated
3755 // TODO: optimize? the data slopes could be calculated as a plane
3756 // (2D slopes) to avoid any interpolation along edges at all
3757 for (y = starty+1;y < endy;)
3760 __m128 edge0offset, edge1offset, edge0scale, edge1scale, data[DPSOFTRAST_ARRAY_TOTAL+1][2], slope[DPSOFTRAST_ARRAY_TOTAL+1][2];
3761 __m128i screenycc = _mm_cmpgt_epi32(_mm_set1_epi32(y), screeny);
3762 int screenymask = _mm_movemask_epi8(screenycc);
3768 case 0xFFFF: /*0000*/ y++; continue;
3769 case 0xFFF0: /*1000*/ edge0p = 3;edge0n = 0;edge1p = 1;edge1n = 0;break;
3770 case 0xFF0F: /*0100*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 1;break;
3771 case 0xFF00: /*1100*/ edge0p = 3;edge0n = 0;edge1p = 2;edge1n = 1;break;
3772 case 0xF0FF: /*0010*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 2;break;
3773 case 0xF0F0: /*1010*/ edge0p = 1;edge0n = 2;edge1p = 3;edge1n = 2;break; // concave - nonsense
3774 case 0xF00F: /*0110*/ edge0p = 0;edge0n = 1;edge1p = 3;edge1n = 2;break;
3775 case 0xF000: /*1110*/ edge0p = 3;edge0n = 0;edge1p = 3;edge1n = 2;break;
3776 case 0x0FFF: /*0001*/ edge0p = 2;edge0n = 3;edge1p = 0;edge1n = 3;break;
3777 case 0x0FF0: /*1001*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 0;break;
3778 case 0x0F0F: /*0101*/ edge0p = 2;edge0n = 3;edge1p = 1;edge1n = 2;break; // concave - nonsense
3779 case 0x0F00: /*1101*/ edge0p = 2;edge0n = 3;edge1p = 2;edge1n = 1;break;
3780 case 0x00FF: /*0011*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 3;break;
3781 case 0x00F0: /*1011*/ edge0p = 1;edge0n = 2;edge1p = 1;edge1n = 0;break;
3782 case 0x000F: /*0111*/ edge0p = 0;edge0n = 1;edge1p = 0;edge1n = 3;break;
3783 case 0x0000: /*1111*/ y++; continue;
3791 case 0xFFFF: /*000*/ y++; continue;
3792 case 0xFFF0: /*100*/ edge0p = 2;edge0n = 0;edge1p = 1;edge1n = 0;break;
3793 case 0xFF0F: /*010*/ edge0p = 0;edge0n = 1;edge1p = 2;edge1n = 1;break;
3794 case 0xFF00: /*110*/ edge0p = 2;edge0n = 0;edge1p = 2;edge1n = 1;break;
3795 case 0x00FF: /*001*/ edge0p = 1;edge0n = 2;edge1p = 0;edge1n = 2;break;
3796 case 0x00F0: /*101*/ edge0p = 1;edge0n = 2;edge1p = 1;edge1n = 0;break;
3797 case 0x000F: /*011*/ edge0p = 0;edge0n = 1;edge1p = 0;edge1n = 2;break;
3798 case 0x0000: /*111*/ y++; continue;
3801 screenycc = _mm_max_epi16(_mm_srli_epi16(screenycc, 1), screeny);
3802 screenycc = _mm_min_epi16(screenycc, _mm_shuffle_epi32(screenycc, _MM_SHUFFLE(1, 0, 3, 2)));
3803 screenycc = _mm_min_epi16(screenycc, _mm_shuffle_epi32(screenycc, _MM_SHUFFLE(2, 3, 0, 1)));
3804 nexty = _mm_extract_epi16(screenycc, 0);
3805 if(nexty >= endy) nexty = endy-1;
3806 if (_mm_ucomigt_ss(_mm_max_ss(screen[edge0n], screen[edge0p]), _mm_min_ss(screen[edge1n], screen[edge1p])))
3815 edge0offset = _mm_shuffle_ps(screen[edge0p], screen[edge0p], _MM_SHUFFLE(1, 1, 1, 1));
3816 edge0scale = _mm_div_ss(_mm_set1_ps(1.0f), _mm_sub_ss(_mm_shuffle_ps(screen[edge0n], screen[edge0n], _MM_SHUFFLE(1, 1, 1, 1)), edge0offset));
3817 edge0scale = _mm_shuffle_ps(edge0scale, edge0scale, _MM_SHUFFLE(0, 0, 0, 0));
3818 edge0offset = _mm_sub_ps(_mm_set1_ps(y), edge0offset);
3819 edge1offset = _mm_shuffle_ps(screen[edge1p], screen[edge1p], _MM_SHUFFLE(1, 1, 1, 1));
3820 edge1scale = _mm_div_ss(_mm_set1_ps(1.0f), _mm_sub_ss(_mm_shuffle_ps(screen[edge1n], screen[edge1n], _MM_SHUFFLE(1, 1, 1, 1)), edge1offset));
3821 edge1offset = _mm_sub_ps(_mm_set1_ps(y), edge1offset);
3822 edge1scale = _mm_shuffle_ps(edge1scale, edge1scale, _MM_SHUFFLE(0, 0, 0, 0));
3823 j = DPSOFTRAST_ARRAY_TOTAL;
3824 slope[j][0] = _mm_mul_ps(_mm_sub_ps(screen[edge0n], screen[edge0p]), edge0scale);
3825 slope[j][1] = _mm_mul_ps(_mm_sub_ps(screen[edge1n], screen[edge1p]), edge1scale);
3826 data[j][0] = _mm_add_ps(_mm_mul_ps(slope[j][0], edge0offset), screen[edge0p]);
3827 data[j][1] = _mm_add_ps(_mm_mul_ps(slope[j][1], edge1offset), screen[edge1p]);
3828 data[j][1] = _mm_sub_ps(data[j][1], data[j][0]);
3829 slope[j][1] = _mm_sub_ps(slope[j][1], slope[j][0]);
3830 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3834 slope[j][0] = _mm_mul_ps(_mm_sub_ps(proj[j][edge0n], proj[j][edge0p]), edge0scale);
3835 slope[j][1] = _mm_mul_ps(_mm_sub_ps(proj[j][edge1n], proj[j][edge1p]), edge1scale);
3836 data[j][0] = _mm_add_ps(_mm_mul_ps(slope[j][0], edge0offset), proj[j][edge0p]);
3837 data[j][1] = _mm_add_ps(_mm_mul_ps(slope[j][1], edge1offset), proj[j][edge1p]);
3838 data[j][1] = _mm_sub_ps(data[j][1], data[j][0]);
3839 slope[j][1] = _mm_sub_ps(slope[j][1], slope[j][0]);
3843 for(; y <= nexty; y++)
3845 __m128 data0, data1, spanilength, startxlerp;
3846 j = DPSOFTRAST_ARRAY_TOTAL;
3847 data[j][0] = _mm_add_ps(data[j][0], slope[j][0]);
3848 data[j][1] = _mm_add_ps(data[j][1], slope[j][1]);
3849 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3853 data[j][0] = _mm_add_ps(data[j][0], slope[j][0]);
3854 data[j][1] = _mm_add_ps(data[j][1], slope[j][1]);
3859 startx = _mm_cvtss_si32(_mm_add_ss(data[DPSOFTRAST_ARRAY_TOTAL][0], _mm_set1_ps(0.5f)));
3860 endx = _mm_cvtss_si32(_mm_add_ss(_mm_add_ss(data[DPSOFTRAST_ARRAY_TOTAL][0], data[DPSOFTRAST_ARRAY_TOTAL][1]), _mm_set1_ps(0.5f)));
3861 if (startx < 0) startx = 0;
3862 if (endx > width) endx = width;
3863 if (startx >= endx) continue;
3865 _mm_store_ss(&startxf, data0);
3866 _mm_store_ss(&endxf, data1);
3867 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); }
3869 spanilength = _mm_div_ss(_mm_set1_ps(1.0f), data[DPSOFTRAST_ARRAY_TOTAL][1]);
3870 spanilength = _mm_shuffle_ps(spanilength, spanilength, _MM_SHUFFLE(0, 0, 0, 0));
3871 startxlerp = _mm_sub_ss(_mm_cvtsi32_ss(_mm_setzero_ps(), startx), data[DPSOFTRAST_ARRAY_TOTAL][0]);
3872 startxlerp = _mm_shuffle_ps(startxlerp, startxlerp, _MM_SHUFFLE(0, 0, 0, 0));
3873 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3874 memcpy(span->mip, mip, sizeof(span->mip));
3875 span->start = y * width + startx;
3876 span->length = endx - startx;
3877 j = DPSOFTRAST_ARRAY_TOTAL;
3878 data1 = _mm_mul_ps(data[j][1], spanilength);
3879 data0 = _mm_add_ps(data[j][0], _mm_mul_ps(data1, startxlerp));
3880 _mm_store_ps(span->data[0][j], data0);
3881 _mm_store_ps(span->data[1][j], data1);
3882 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3886 data1 = _mm_mul_ps(data[j][1], spanilength);
3887 data0 = _mm_add_ps(data[j][0], _mm_mul_ps(data1, startxlerp));
3888 _mm_store_ps(span->data[0][j], data0);
3889 _mm_store_ps(span->data[1][j], data1);
3892 // to keep the shader routines from needing more than a small
3893 // buffer for pixel intermediate data, we split long spans...
3894 while (span->length > DPSOFTRAST_DRAW_MAXSPANLENGTH)
3896 span->length = DPSOFTRAST_DRAW_MAXSPANLENGTH;
3897 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3899 DPSOFTRAST_Draw_ProcessSpans();
3900 dpsoftrast.draw.numspans = 0;
3903 span = &dpsoftrast.draw.spanqueue[dpsoftrast.draw.numspans++];
3905 startx += DPSOFTRAST_DRAW_MAXSPANLENGTH;
3906 span->start = y * width + startx;
3907 span->length = endx - startx;
3908 j = DPSOFTRAST_ARRAY_TOTAL;
3909 _mm_store_ps(span->data[0][j], _mm_add_ps(_mm_load_ps(span->data[0][j]), _mm_mul_ps(_mm_load_ps(span->data[1][j]), _mm_set1_ps(DPSOFTRAST_DRAW_MAXSPANLENGTH))));
3910 for (j = 0;j < DPSOFTRAST_ARRAY_TOTAL;j++)
3914 _mm_store_ps(span->data[0][j], _mm_add_ps(_mm_load_ps(span->data[0][j]), _mm_mul_ps(_mm_load_ps(span->data[1][j]), _mm_set1_ps(DPSOFTRAST_DRAW_MAXSPANLENGTH))));
3918 // after all that, we have a span suitable for the pixel shader...
3919 if (dpsoftrast.draw.numspans >= DPSOFTRAST_DRAW_MAXSPANQUEUE)
3921 DPSOFTRAST_Draw_ProcessSpans();
3922 dpsoftrast.draw.numspans = 0;
3926 // draw outlines over triangle for debugging
3927 // for (j = 0, k = numpoints-1;j < numpoints;k = j, j++)
3928 // DPSOFTRAST_Draw_DebugEdgePoints(screen[k], screen[j]);
3930 if (dpsoftrast.draw.numspans)
3932 DPSOFTRAST_Draw_ProcessSpans();
3933 dpsoftrast.draw.numspans = 0;
3938 void DPSOFTRAST_Draw_DebugPoints(void)
3943 int numvertices = dpsoftrast.draw.numvertices;
3944 int w = dpsoftrast.fb_width;
3946 unsigned int *pixels = dpsoftrast.fb_colorpixels[0];
3948 bounds[0] = dpsoftrast.fb_viewportscissor[0];
3949 bounds[1] = dpsoftrast.fb_viewportscissor[1];
3950 bounds[2] = dpsoftrast.fb_viewportscissor[0] + dpsoftrast.fb_viewportscissor[2];
3951 bounds[3] = dpsoftrast.fb_viewportscissor[1] + dpsoftrast.fb_viewportscissor[3];
3952 for (i = 0;i < numvertices;i++)
3955 //if (dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+3] != 1.0f)
3957 x = (int)(dpsoftrast.draw.screencoord4f[i*4+0]);
3958 y = (int)(dpsoftrast.draw.screencoord4f[i*4+1]);
3959 //x = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 0.5f);
3960 //y = (int)(dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 0.5f);
3961 //x = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+0] + 1.0f) * dpsoftrast.fb_width * 0.5f + 0.5f);
3962 //y = (int)((dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION][i*4+1] + 1.0f) * dpsoftrast.fb_height * 0.5f + 0.5f);
3963 if (x < bounds[0] || y < bounds[1] || x >= bounds[2] || y >= bounds[3])
3965 c4f = dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_COLOR] + i*4;
3966 pixels[y*w+x] = DPSOFTRAST_BGRA8_FROM_RGBA32F(c4f[0], c4f[1], c4f[2], c4f[3]);
3970 void DPSOFTRAST_DrawTriangles(int firstvertex, int numvertices, int numtriangles, const int *element3i, const unsigned short *element3s)
3972 unsigned char arraymask[DPSOFTRAST_ARRAY_TOTAL];
3973 arraymask[0] = true;
3974 arraymask[1] = dpsoftrast.fb_colorpixels[0] != NULL; // TODO: optimize (decide based on shadermode)
3975 arraymask[2] = dpsoftrast.pointer_texcoordf[0] != NULL;
3976 arraymask[3] = dpsoftrast.pointer_texcoordf[1] != NULL;
3977 arraymask[4] = dpsoftrast.pointer_texcoordf[2] != NULL;
3978 arraymask[5] = dpsoftrast.pointer_texcoordf[3] != NULL;
3979 arraymask[6] = dpsoftrast.pointer_texcoordf[4] != NULL;
3980 arraymask[7] = dpsoftrast.pointer_texcoordf[5] != NULL;
3981 arraymask[8] = dpsoftrast.pointer_texcoordf[6] != NULL;
3982 arraymask[9] = dpsoftrast.pointer_texcoordf[7] != NULL;
3983 DPSOFTRAST_Validate(DPSOFTRAST_VALIDATE_DRAW);
3984 DPSOFTRAST_Draw_LoadVertices(firstvertex, numvertices, true);
3985 DPSOFTRAST_ShaderModeTable[dpsoftrast.shader_mode].Vertex();
3986 DPSOFTRAST_Draw_ProjectVertices(dpsoftrast.draw.screencoord4f, dpsoftrast.draw.post_array4f[DPSOFTRAST_ARRAY_POSITION], numvertices);
3987 DPSOFTRAST_Draw_ProcessTriangles(firstvertex, numtriangles, element3i, element3s, arraymask);
3990 void DPSOFTRAST_Init(int width, int height, unsigned int *colorpixels, unsigned int *depthpixels)
3999 memset(&dpsoftrast, 0, sizeof(dpsoftrast));
4000 dpsoftrast.bigendian = u.b[3];
4001 dpsoftrast.fb_width = width;
4002 dpsoftrast.fb_height = height;
4003 dpsoftrast.fb_depthpixels = depthpixels;
4004 dpsoftrast.fb_colorpixels[0] = colorpixels;
4005 dpsoftrast.fb_colorpixels[1] = NULL;
4006 dpsoftrast.fb_colorpixels[1] = NULL;
4007 dpsoftrast.fb_colorpixels[1] = NULL;
4008 dpsoftrast.texture_firstfree = 1;
4009 dpsoftrast.texture_end = 1;
4010 dpsoftrast.texture_max = 0;
4011 dpsoftrast.user.colormask[0] = 1;
4012 dpsoftrast.user.colormask[1] = 1;
4013 dpsoftrast.user.colormask[2] = 1;
4014 dpsoftrast.user.colormask[3] = 1;
4015 dpsoftrast.user.blendfunc[0] = GL_ONE;
4016 dpsoftrast.user.blendfunc[1] = GL_ZERO;
4017 dpsoftrast.user.depthmask = true;
4018 dpsoftrast.user.depthtest = true;
4019 dpsoftrast.user.depthfunc = GL_LEQUAL;
4020 dpsoftrast.user.scissortest = false;
4021 dpsoftrast.user.cullface = GL_BACK;
4022 dpsoftrast.user.alphatest = false;
4023 dpsoftrast.user.alphafunc = GL_GREATER;
4024 dpsoftrast.user.alphavalue = 0.5f;
4025 dpsoftrast.user.scissor[0] = 0;
4026 dpsoftrast.user.scissor[1] = 0;
4027 dpsoftrast.user.scissor[2] = dpsoftrast.fb_width;
4028 dpsoftrast.user.scissor[3] = dpsoftrast.fb_height;
4029 dpsoftrast.user.viewport[0] = 0;
4030 dpsoftrast.user.viewport[1] = 0;
4031 dpsoftrast.user.viewport[2] = dpsoftrast.fb_width;
4032 dpsoftrast.user.viewport[3] = dpsoftrast.fb_height;
4033 dpsoftrast.user.depthrange[0] = 0;
4034 dpsoftrast.user.depthrange[1] = 1;
4035 dpsoftrast.user.polygonoffset[0] = 0;
4036 dpsoftrast.user.polygonoffset[1] = 0;
4037 dpsoftrast.user.color[0] = 1;
4038 dpsoftrast.user.color[1] = 1;
4039 dpsoftrast.user.color[2] = 1;
4040 dpsoftrast.user.color[3] = 1;
4041 dpsoftrast.validate = -1;
4042 DPSOFTRAST_Validate(-1);
4043 dpsoftrast.validate = 0;
4046 void DPSOFTRAST_Shutdown(void)
4049 for (i = 0;i < dpsoftrast.texture_end;i++)
4050 if (dpsoftrast.texture[i].bytes)
4051 MM_FREE(dpsoftrast.texture[i].bytes);
4052 if (dpsoftrast.texture)
4053 free(dpsoftrast.texture);
4054 memset(&dpsoftrast, 0, sizeof(dpsoftrast));