Add triangle prim support Improve line support Add initial texture support
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
diff --git a/src/render/ps2/SDL_render_ps2.c b/src/render/ps2/SDL_render_ps2.c
index 5541bec..0455f1b 100644
--- a/src/render/ps2/SDL_render_ps2.c
+++ b/src/render/ps2/SDL_render_ps2.c
@@ -144,7 +144,7 @@ PS2_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
gsKit_prim_line(gsGlobal, x1, y1, x2, y2, 1, color);
}
if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
- SDL_DrawPoint(dst, points[count-1].x, points[count-1].y, color);
+ PS2_DrawPoints(dst, points, 1, color);
}
return 0;
}
@@ -266,33 +266,76 @@ PS2_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
}
static int
+PixelFormatToPS2PSM(Uint32 format)
+{
+ switch (format) {
+ case SDL_PIXELFORMAT_ABGR1555:
+ return GS_PSM_CT16S;
+ case SDL_PIXELFORMAT_BGR888:
+ return GS_PSM_CT24;
+ case SDL_PIXELFORMAT_ABGR8888:
+ return GS_PSM_CT32;
+ default:
+ return GS_PSM_CT32;
+ }
+}
+
+static int
PS2_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
+ GSTEXTURE* ps2_tex = (GSTEXTURE*) SDL_calloc(1, sizeof(GSTEXTURE));
- if (!SDL_PixelFormatEnumToMasks
- (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
- return SDL_SetError("Unknown texture format");
- }
+ if(!ps2_tex)
+ return SDL_OutOfMemory();
- texture->driverdata =
- SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
- Bmask, Amask);
- SDL_SetSurfaceColorMod(texture->driverdata, texture->color.r, texture->color.g, texture->color.b);
- SDL_SetSurfaceAlphaMod(texture->driverdata, texture->color.a);
- SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
+ ps2_tex->Delayed = true;
+ ps2_tex->Width = texture->w;
+ ps2_tex->Height = texture->h;
+ ps2_tex->PSM = PixelFormatToPS2PSM(texture->format);
+ ps2_tex->Mem = memalign(128, gsKit_texture_size_ee(ps2_tex->Width, ps2_tex->Height, ps2_tex->PSM));
- /* Only RLE encode textures without an alpha channel since the RLE coder
- * discards the color values of pixels with an alpha value of zero.
- */
- if (texture->access == SDL_TEXTUREACCESS_STATIC && !Amask) {
- SDL_SetSurfaceRLE(texture->driverdata, 1);
+ if(!ps2_tex->Mem)
+ {
+ SDL_free(ps2_tex);
+ return SDL_OutOfMemory();
}
- if (!texture->driverdata) {
- return -1;
- }
+ if(!ps2_tex->Delayed)
+ {
+ ps2_tex->Vram = gsKit_vram_alloc(gsGlobal, gsKit_texture_size(ps2_tex->Width, ps2_tex->Height, ps2_tex->PSM), GSKIT_ALLOC_USERBUFFER);
+ if(ps2_tex->Vram == GSKIT_ALLOC_ERROR) {
+ printf("VRAM Allocation Failed. Will not upload texture.\n");
+ return -1;
+ }
+
+ if(ps2_tex->Clut != NULL) {
+ if(ps2_tex->PSM == GS_PSM_T4)
+ ps2_tex->VramClut = gsKit_vram_alloc(gsGlobal, gsKit_texture_size(8, 2, GS_PSM_CT32), GSKIT_ALLOC_USERBUFFER);
+ else
+ ps2_tex->VramClut = gsKit_vram_alloc(gsGlobal, gsKit_texture_size(16, 16, GS_PSM_CT32), GSKIT_ALLOC_USERBUFFER);
+
+ if(ps2_tex->VramClut == GSKIT_ALLOC_ERROR)
+ {
+ printf("VRAM CLUT Allocation Failed. Will not upload texture.\n");
+ return -1;
+ }
+ }
+
+ gsKit_texture_upload(gsGlobal, ps2_tex);
+ free(ps2_tex->Mem);
+ ps2_tex->Mem = NULL;
+ if(ps2_tex->Clut != NULL) {
+ free(ps2_tex->Clut);
+ ps2_tex->Clut = NULL;
+ }
+ } else {
+ gsKit_setup_tbw(ps2_tex);
+ }
+
+ texture->driverdata = ps2_tex;
+
return 0;
}
@@ -326,12 +369,12 @@ static int
PS2_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
- SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
+ GSTEXTURE *surface = (GSTEXTURE *) texture->driverdata;
*pixels =
- (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
- rect->x * surface->format->BytesPerPixel);
- *pitch = surface->pitch;
+ (void *) ((Uint8 *) surface->Mem +
+ gsKit_texture_size_ee(surface->Width, surface->Height, surface->PSM));
+ //*pitch = surface->pitch;
return 0;
}
@@ -1064,13 +1107,27 @@ PS2_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *verti
}
}
+ GSTEXTURE* tex = cmd->data.draw.texture->driverdata;
+
for (i = 0; i < count; i += 3, ptr += 3) {
- SDL_PS2_BlitTriangle(
- src,
- &(ptr[0].src), &(ptr[1].src), &(ptr[2].src),
- surface,
- &(ptr[0].dst), &(ptr[1].dst), &(ptr[2].dst),
- ptr[0].color, ptr[1].color, ptr[2].color);
+ float x1 = ptr[0].dst.x;
+ float y1 = ptr[0].dst.y;
+
+ float x2 = ptr[1].dst.x;
+ float y2 = ptr[1].dst.y;
+
+ float x3 = ptr[2].dst.x;
+ float y3 = ptr[2].dst.y;
+
+ Uint32 c1 = (ptr[0].color.r | (ptr[0].color.g << 8) | (ptr[0].color.b << 16) | (ptr[0].color.a << 24));
+ Uint32 c2 = (ptr[1].color.r | (ptr[1].color.g << 8) | (ptr[1].color.b << 16) | (ptr[1].color.a << 24));
+ Uint32 c3 = (ptr[0].color.r | (ptr[2].color.g << 8) | (ptr[2].color.b << 16) | (ptr[2].color.a << 24));
+
+ if (tex->Delayed == true) {
+ gsKit_TexManager_bind(gsGlobal, tex);
+ }
+ //It still need some works to make texture render on-screen
+ gsKit_prim_triangle_goraud_texture(gsGlobal, tex, x1, y1, 0, 0, x2, y2, 0, 1, x3, y3, 1, 0, 1, c1, c2, c3);
}
} else {
GeometryFillData *ptr = (GeometryFillData *) verts;
@@ -1087,8 +1144,21 @@ PS2_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *verti
}
}
- for (i = 0; i < count; i += 3, ptr += 3) {
- SDL_PS2_FillTriangle(surface, &(ptr[0].dst), &(ptr[1].dst), &(ptr[2].dst), blend, ptr[0].color, ptr[1].color, ptr[2].color);
+ for (i = 0; i < count; i += 3, ptr += 3) { //These for loops need to be in a separated function
+ float x1 = ptr[0].dst.x;
+ float y1 = ptr[0].dst.y;
+
+ float x2 = ptr[1].dst.x;
+ float y2 = ptr[1].dst.y;
+
+ float x3 = ptr[2].dst.x;
+ float y3 = ptr[2].dst.y;
+
+ Uint32 c1 = (ptr[0].color.r | (ptr[0].color.g << 8) | (ptr[0].color.b << 16) | (ptr[0].color.a << 24));
+ Uint32 c2 = (ptr[1].color.r | (ptr[1].color.g << 8) | (ptr[1].color.b << 16) | (ptr[1].color.a << 24));
+ Uint32 c3 = (ptr[0].color.r | (ptr[2].color.g << 8) | (ptr[2].color.b << 16) | (ptr[2].color.a << 24));
+
+ gsKit_prim_triangle_gouraud(gsGlobal, x1, y1, x2, y2, x3, y3, 1, c1, c2, c3);
}
}
break;
diff --git a/src/render/ps2/SDL_triangle.c b/src/render/ps2/SDL_triangle.c
index 2e1b7df..1dc073c 100644
--- a/src/render/ps2/SDL_triangle.c
+++ b/src/render/ps2/SDL_triangle.c
@@ -36,84 +36,6 @@
#define FP_BITS 1
-#define COLOR_EQ(c1, c2) ((c1).r == (c2).r && (c1).g == (c2).g && (c1).b == (c2).b && (c1).a == (c2).a)
-
-static void SDL_BlitTriangle_Slow(SDL_BlitInfo * info,
- SDL_Point s2_x_area, SDL_Rect dstrect, int area, int bias_w0, int bias_w1, int bias_w2,
- int d2d1_y, int d1d2_x, int d0d2_y, int d2d0_x, int d1d0_y, int d0d1_x,
- int s2s0_x, int s2s1_x, int s2s0_y, int s2s1_y, int w0_row, int w1_row, int w2_row,
- SDL_Color c0, SDL_Color c1, SDL_Color c2, int is_uniform);
-
-#if 0
-int SDL_BlitTriangle(SDL_Surface *src, const SDL_Point srcpoints[3], SDL_Surface *dst, const SDL_Point dstpoints[3])
-{
- int i;
- SDL_Point points[6];
-
- if (src == NULL || dst == NULL) {
- return -1;
- }
-
- for (i = 0; i < 3; i++) {
- if (srcpoints[i].x < 0 || srcpoints[i].y < 0 || srcpoints[i].x >= src->w || srcpoints[i].y >= src->h) {
- return SDL_SetError("Values of 'srcpoints' out of bounds");
- }
- }
-
- points[0] = srcpoints[0];
- points[1] = dstpoints[0];
- points[2] = srcpoints[1];
- points[3] = dstpoints[1];
- points[4] = srcpoints[2];
- points[5] = dstpoints[2];
- for (i = 0; i < 3; i++) {
- PS2_trianglepoint_2_fixedpoint(&points[2 * i + 1]);
- }
- return SDL_PS2_BlitTriangle(src, dst, points);
-}
-
-int SDL_FillTriangle(SDL_Surface *dst, const SDL_Point points[3], Uint32 color)
-{
- int i;
- SDL_Point points_tmp[3];
- if (dst == NULL) {
- return -1;
- }
- for (i = 0; i < 3; i++) {
- points_tmp[i] = points[i];
- PS2_trianglepoint_2_fixedpoint(&points_tmp[i]);
- }
- return SDL_PS2_FillTriangle(dst, points_tmp, SDL_BLENDMODE_NONE, color);
-}
-#endif
-
-/* cross product AB x AC */
-static int cross_product(const SDL_Point *a, const SDL_Point *b, int c_x, int c_y)
-{
- return (b->x - a->x) * (c_y - a->y) - (b->y - a->y) * (c_x - a->x);
-}
-
-/* check for top left rules */
-static int is_top_left(const SDL_Point *a, const SDL_Point *b, int is_clockwise)
-{
- if (is_clockwise) {
- if (a->y == b->y && a->x < b->x) {
- return 1;
- }
- if (b->y < a->y) {
- return 1;
- }
- } else {
- if (a->y == b->y && b->x < a->x) {
- return 1;
- }
- if (a->y < b->y) {
- return 1;
- }
- }
- return 0;
-}
-
void PS2_trianglepoint_2_fixedpoint(SDL_Point *a) {
a->x <<= FP_BITS;
a->y <<= FP_BITS;
@@ -202,687 +124,6 @@ static void bounding_rect(const SDL_Point *a, const SDL_Point *b, const SDL_Poin
} \
} \
-int SDL_PS2_FillTriangle(SDL_Surface *dst, SDL_Point *d0, SDL_Point *d1, SDL_Point *d2, SDL_BlendMode blend, SDL_Color c0, SDL_Color c1, SDL_Color c2)
-{
- int ret = 0;
- int dst_locked = 0;
-
- SDL_Rect dstrect;
-
- int dstbpp;
- Uint8 *dst_ptr;
- int dst_pitch;
-
- int area, is_clockwise;
-
- int d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x;
- int w0_row, w1_row, w2_row;
- int bias_w0, bias_w1, bias_w2;
-
- int is_uniform;
-
- SDL_Surface *tmp = NULL;
-
- if (dst == NULL) {
- return -1;
- }
-
- area = cross_product(d0, d1, d2->x, d2->y);
-
- is_uniform = COLOR_EQ(c0, c1) && COLOR_EQ(c1, c2);
-
- /* Flat triangle */
- if (area == 0) {
- return 0;
- }
-
- /* Lock the destination, if needed */
- if (SDL_MUSTLOCK(dst)) {
- if (SDL_LockSurface(dst) < 0) {
- ret = -1;
- goto end;
- } else {
- dst_locked = 1;
- }
- }
-
- bounding_rect_fixedpoint(d0, d1, d2, &dstrect);
-
- {
- /* Clip triangle rect with surface rect */
- SDL_Rect rect;
- rect.x = 0;
- rect.y = 0;
- rect.w = dst->w;
- rect.h = dst->h;
- SDL_IntersectRect(&dstrect, &rect, &dstrect);
- }
-
- {
- /* Clip triangle with surface clip rect */
- SDL_Rect rect;
- SDL_GetClipRect(dst, &rect);
- SDL_IntersectRect(&dstrect, &rect, &dstrect);
- }
-
-
- if (blend != SDL_BLENDMODE_NONE) {
- int format = dst->format->format;
-
- /* need an alpha format */
- if (! dst->format->Amask) {
- format = SDL_PIXELFORMAT_ARGB8888;
- }
-
- /* Use an intermediate surface */
- tmp = SDL_CreateRGBSurfaceWithFormat(0, dstrect.w, dstrect.h, 0, format);
- if (tmp == NULL) {
- ret = -1;
- goto end;
- }
-
- if (blend == SDL_BLENDMODE_MOD) {
- Uint32 c = SDL_MapRGBA(tmp->format, 255, 255, 255, 255);
- SDL_FillRect(tmp, NULL, c);
- }
-
- SDL_SetSurfaceBlendMode(tmp, blend);
-
- dstbpp = tmp->format->BytesPerPixel;
- dst_ptr = tmp->pixels;
- dst_pitch = tmp->pitch;
-
- } else {
- /* Write directly to destination surface */
- dstbpp = dst->format->BytesPerPixel;
- dst_ptr = (Uint8 *)dst->pixels + dstrect.x * dstbpp + dstrect.y * dst->pitch;
- dst_pitch = dst->pitch;
- }
-
- is_clockwise = area > 0;
- area = SDL_abs(area);
-
- d2d1_y = (d1->y - d2->y) << FP_BITS;
- d0d2_y = (d2->y - d0->y) << FP_BITS;
- d1d0_y = (d0->y - d1->y) << FP_BITS;
- d1d2_x = (d2->x - d1->x) << FP_BITS;
- d2d0_x = (d0->x - d2->x) << FP_BITS;
- d0d1_x = (d1->x - d0->x) << FP_BITS;
-
- /* Starting point for rendering, at the middle of a pixel */
- {
- SDL_Point p;
- p.x = dstrect.x;
- p.y = dstrect.y;
- PS2_trianglepoint_2_fixedpoint(&p);
- p.x += (1 << FP_BITS) / 2;
- p.y += (1 << FP_BITS) / 2;
- w0_row = cross_product(d1, d2, p.x, p.y);
- w1_row = cross_product(d2, d0, p.x, p.y);
- w2_row = cross_product(d0, d1, p.x, p.y);
- }
-
- /* Handle anti-clockwise triangles */
- if (! is_clockwise) {
- d2d1_y *= -1;
- d0d2_y *= -1;
- d1d0_y *= -1;
- d1d2_x *= -1;
- d2d0_x *= -1;
- d0d1_x *= -1;
- w0_row *= -1;
- w1_row *= -1;
- w2_row *= -1;
- }
-
- /* Add a bias to respect top-left rasterization rule */
- bias_w0 = (is_top_left(d1, d2, is_clockwise) ? 0 : -1);
- bias_w1 = (is_top_left(d2, d0, is_clockwise) ? 0 : -1);
- bias_w2 = (is_top_left(d0, d1, is_clockwise) ? 0 : -1);
-
- if (is_uniform) {
- Uint32 color;
- if (tmp) {
- if (dst->format->Amask) {
- color = SDL_MapRGBA(tmp->format, c0.r, c0.g, c0.b, c0.a);
- } else {
- //color = SDL_MapRGB(tmp->format, c0.r, c0.g, c0.b);
- color = SDL_MapRGBA(tmp->format, c0.r, c0.g, c0.b, c0.a);
- }
- } else {
- color = SDL_MapRGBA(dst->format, c0.r, c0.g, c0.b, c0.a);
- }
-
- if (dstbpp == 4) {
- TRIANGLE_BEGIN_LOOP
- {
- *(Uint32 *)dptr = color;
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 3) {
- TRIANGLE_BEGIN_LOOP
- {
- Uint8 *s = (Uint8*)&color;
- dptr[0] = s[0];
- dptr[1] = s[1];
- dptr[2] = s[2];
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 2) {
- TRIANGLE_BEGIN_LOOP
- {
- *(Uint16 *)dptr = color;
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 1) {
- TRIANGLE_BEGIN_LOOP
- {
- *dptr = color;
- }
- TRIANGLE_END_LOOP
- }
- } else {
- SDL_PixelFormat *format = dst->format;
- if (tmp) {
- format = tmp->format;
- }
- if (dstbpp == 4) {
- TRIANGLE_BEGIN_LOOP
- {
- TRIANGLE_GET_MAPPED_COLOR
- *(Uint32 *)dptr = color;
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 3) {
- TRIANGLE_BEGIN_LOOP
- {
- TRIANGLE_GET_MAPPED_COLOR
- Uint8 *s = (Uint8*)&color;
- dptr[0] = s[0];
- dptr[1] = s[1];
- dptr[2] = s[2];
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 2) {
- TRIANGLE_BEGIN_LOOP
- {
- TRIANGLE_GET_MAPPED_COLOR
- *(Uint16 *)dptr = color;
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 1) {
- TRIANGLE_BEGIN_LOOP
- {
- TRIANGLE_GET_MAPPED_COLOR
- *dptr = color;
- }
- TRIANGLE_END_LOOP
- }
- }
-
- if (tmp) {
- SDL_BlitSurface(tmp, NULL, dst, &dstrect);
- SDL_FreeSurface(tmp);
- }
-
-end:
- if (dst_locked) {
- SDL_UnlockSurface(dst);
- }
-
- return ret;
-}
-
-
-
-
-
-
-
-int SDL_PS2_BlitTriangle(
- SDL_Surface *src,
- SDL_Point *s0, SDL_Point *s1, SDL_Point *s2,
- SDL_Surface *dst,
- SDL_Point *d0, SDL_Point *d1, SDL_Point *d2,
- SDL_Color c0, SDL_Color c1, SDL_Color c2)
-{
- int ret = 0;
- int src_locked = 0;
- int dst_locked = 0;
-
- SDL_BlendMode blend;
-
- SDL_Rect dstrect;
-
- SDL_Point s2_x_area;
-
- int dstbpp;
- Uint8 *dst_ptr;
- int dst_pitch;
-
- int *src_ptr;
- int src_pitch;
-
- int area, is_clockwise;
-
- int d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x;
- int s2s0_x, s2s1_x, s2s0_y, s2s1_y;
-
- int w0_row, w1_row, w2_row;
- int bias_w0, bias_w1, bias_w2;
-
- int is_uniform;
-
- int has_modulation;
-
- if (src == NULL || dst == NULL) {
- return -1;
- }
-
- area = cross_product(d0, d1, d2->x, d2->y);
-
- /* Flat triangle */
- if (area == 0) {
- return 0;
- }
-
- /* Lock the destination, if needed */
- if (SDL_MUSTLOCK(dst)) {
- if (SDL_LockSurface(dst) < 0) {
- ret = -1;
- goto end;
- } else {
- dst_locked = 1;
- }
- }
-
- /* Lock the source, if needed */
- if (SDL_MUSTLOCK(src)) {
- if (SDL_LockSurface(src) < 0) {
- ret = -1;
- goto end;
- } else {
- src_locked = 1;
- }
- }
-
- is_uniform = COLOR_EQ(c0, c1) && COLOR_EQ(c1, c2);
-
- bounding_rect_fixedpoint(d0, d1, d2, &dstrect);
-
- SDL_GetSurfaceBlendMode(src, &blend);
-
- /* TRIANGLE_GET_TEXTCOORD interpolates up to the max values included, so reduce by 1 */
- {
- SDL_Rect srcrect;
- int maxx, maxy;
- bounding_rect(s0, s1, s2, &srcrect);
- maxx = srcrect.x + srcrect.w;
- maxy = srcrect.y + srcrect.h;
- if (srcrect.w > 0) {
- if (s0->x == maxx) s0->x--;
- if (s1->x == maxx) s1->x--;
- if (s2->x == maxx) s2->x--;
- }
- if (srcrect.h > 0) {
- if (s0->y == maxy) s0->y--;
- if (s1->y == maxy) s1->y--;
- if (s2->y == maxy) s2->y--;
- }
- }
-
- if (is_uniform) {
- // SDL_GetSurfaceColorMod(src, &r, &g, &b);
- has_modulation = c0.r != 255 || c0.g != 255 || c0.b != 255 || c0.a != 255;;
- } else {
- has_modulation = SDL_TRUE;
- }
-
- {
- /* Clip triangle rect with surface rect */
- SDL_Rect rect;
- rect.x = 0;
- rect.y = 0;
- rect.w = dst->w;
- rect.h = dst->h;
-
- SDL_IntersectRect(&dstrect, &rect, &dstrect);
- }
-
- {
- /* Clip triangle with surface clip rect */
- SDL_Rect rect;
- SDL_GetClipRect(dst, &rect);
- SDL_IntersectRect(&dstrect, &rect, &dstrect);
- }
-
- /* Set destination pointer */
- dstbpp = dst->format->BytesPerPixel;
- dst_ptr = (Uint8 *)dst->pixels + dstrect.x * dstbpp + dstrect.y * dst->pitch;
- dst_pitch = dst->pitch;
-
- /* Set source pointer */
- src_ptr = src->pixels;
- src_pitch = src->pitch;
-
- is_clockwise = area > 0;
- area = SDL_abs(area);
-
- d2d1_y = (d1->y - d2->y) << FP_BITS;
- d0d2_y = (d2->y - d0->y) << FP_BITS;
- d1d0_y = (d0->y - d1->y) << FP_BITS;
-
-
- d1d2_x = (d2->x - d1->x) << FP_BITS;
- d2d0_x = (d0->x - d2->x) << FP_BITS;
- d0d1_x = (d1->x - d0->x) << FP_BITS;
-
- s2s0_x = s0->x - s2->x;
- s2s1_x = s1->x - s2->x;
- s2s0_y = s0->y - s2->y;
- s2s1_y = s1->y - s2->y;
-
- /* Starting point for rendering, at the middle of a pixel */
- {
- SDL_Point p;
- p.x = dstrect.x;
- p.y = dstrect.y;
- PS2_trianglepoint_2_fixedpoint(&p);
- p.x += (1 << FP_BITS) / 2;
- p.y += (1 << FP_BITS) / 2;
- w0_row = cross_product(d1, d2, p.x, p.y);
- w1_row = cross_product(d2, d0, p.x, p.y);
- w2_row = cross_product(d0, d1, p.x, p.y);
- }
-
- /* Handle anti-clockwise triangles */
- if (! is_clockwise) {
- d2d1_y *= -1;
- d0d2_y *= -1;
- d1d0_y *= -1;
- d1d2_x *= -1;
- d2d0_x *= -1;
- d0d1_x *= -1;
- w0_row *= -1;
- w1_row *= -1;
- w2_row *= -1;
- }
-
- /* Add a bias to respect top-left rasterization rule */
- bias_w0 = (is_top_left(d1, d2, is_clockwise) ? 0 : -1);
- bias_w1 = (is_top_left(d2, d0, is_clockwise) ? 0 : -1);
- bias_w2 = (is_top_left(d0, d1, is_clockwise) ? 0 : -1);
-
- /* precompute constant 's2->x * area' used in TRIANGLE_GET_TEXTCOORD */
- s2_x_area.x = s2->x * area;
- s2_x_area.y = s2->y * area;
-
- if (blend != SDL_BLENDMODE_NONE || src->format->format != dst->format->format || has_modulation || ! is_uniform) {
- /* Use SDL_BlitTriangle_Slow */
-
- SDL_BlitInfo *info = &src->map->info;
- SDL_BlitInfo tmp_info;
-
- SDL_zero(tmp_info);
-
- tmp_info.src_fmt = src->format;
- tmp_info.dst_fmt = dst->format;
- tmp_info.flags = info->flags;
- /*
- tmp_info.r = info->r;
- tmp_info.g = info->g;
- tmp_info.b = info->b;
- tmp_info.a = info->a;
- */
- tmp_info.r = c0.r;
- tmp_info.g = c0.g;
- tmp_info.b = c0.b;
- tmp_info.a = c0.a;
-
-
- tmp_info.flags &= ~(SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA);
-
- if (c0.r != 255 || c1.r != 255 || c2.r != 255 ||
- c0.g != 255 || c1.g != 255 || c2.g != 255 ||
- c0.b != 255 || c1.b != 255 || c2.b != 255) {
- tmp_info.flags |= SDL_COPY_MODULATE_COLOR;
- }
-
- if (c0.a != 255 || c1.a != 255 || c2.a != 255) {
- tmp_info.flags |= SDL_COPY_MODULATE_ALPHA;
- }
-
- tmp_info.colorkey = info->colorkey;
-
- /* src */
- tmp_info.src = (Uint8 *) src_ptr;
- tmp_info.src_pitch = src_pitch;
-
- /* dst */
- tmp_info.dst = (Uint8 *) dst_ptr;
- tmp_info.dst_pitch = dst_pitch;
-
- SDL_BlitTriangle_Slow(&tmp_info, s2_x_area, dstrect, area, bias_w0, bias_w1, bias_w2,
- d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x,
- s2s0_x, s2s1_x, s2s0_y, s2s1_y, w0_row, w1_row, w2_row,
- c0, c1, c2, is_uniform);
-
- goto end;
- }
-
- if (dstbpp == 4) {
- TRIANGLE_BEGIN_LOOP
- {
- TRIANGLE_GET_TEXTCOORD
- Uint32 *sptr = (Uint32 *)((Uint8 *) src_ptr + srcy * src_pitch);
- *(Uint32 *)dptr = sptr[srcx];
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 3) {
- TRIANGLE_BEGIN_LOOP
- {
- TRIANGLE_GET_TEXTCOORD
- Uint8 *sptr = (Uint8 *)((Uint8 *) src_ptr + srcy * src_pitch);
- dptr[0] = sptr[3 * srcx];
- dptr[1] = sptr[3 * srcx + 1];
- dptr[2] = sptr[3 * srcx + 2];
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 2) {
- TRIANGLE_BEGIN_LOOP
- {
- TRIANGLE_GET_TEXTCOORD
- Uint16 *sptr = (Uint16 *)((Uint8 *) src_ptr + srcy * src_pitch);
- *(Uint16 *)dptr = sptr[srcx];
- }
- TRIANGLE_END_LOOP
- } else if (dstbpp == 1) {
- TRIANGLE_BEGIN_LOOP
- {
- TRIANGLE_GET_TEXTCOORD
- Uint8 *sptr = (Uint8 *)((Uint8 *) src_ptr + srcy * src_pitch);
- *dptr = sptr[srcx];
- }
- TRIANGLE_END_LOOP
- }
-
-end:
- if (dst_locked) {
- SDL_UnlockSurface(dst);
- }
- if (src_locked) {
- SDL_UnlockSurface(src);
- }
-
- return ret;
-}
-
-
-#define FORMAT_ALPHA 0
-#define FORMAT_NO_ALPHA -1
-#define FORMAT_2101010 1
-#define FORMAT_HAS_ALPHA(format) format == 0
-#define FORMAT_HAS_NO_ALPHA(format) format < 0
-static int SDL_INLINE detect_format(SDL_PixelFormat *pf) {
- if (pf->format == SDL_PIXELFORMAT_ARGB2101010) {
- return FORMAT_2101010;
- } else if (pf->Amask) {
- return FORMAT_ALPHA;
- } else {
- return FORMAT_NO_ALPHA;
- }
-}
-
-static void
-SDL_BlitTriangle_Slow(SDL_BlitInfo *info,
- SDL_Point s2_x_area, SDL_Rect dstrect, int area, int bias_w0, int bias_w1, int bias_w2,
- int d2d1_y, int d1d2_x, int d0d2_y, int d2d0_x, int d1d0_y, int d0d1_x,
- int s2s0_x, int s2s1_x, int s2s0_y, int s2s1_y, int w0_row, int w1_row, int w2_row,
- SDL_Color c0, SDL_Color c1, SDL_Color c2, int is_uniform)
-{
- const int flags = info->flags;
- Uint32 modulateR = info->r;
- Uint32 modulateG = info->g;
- Uint32 modulateB = info->b;
- Uint32 modulateA = info->a;
- Uint32 srcpixel;
- Uint32 srcR, srcG, srcB, srcA;
- Uint32 dstpixel;
- Uint32 dstR, dstG, dstB, dstA;
- SDL_PixelFormat *src_fmt = info->src_fmt;
- SDL_PixelFormat *dst_fmt = info->dst_fmt;
- int srcbpp = src_fmt->BytesPerPixel;
- int dstbpp = dst_fmt->BytesPerPixel;
- int srcfmt_val;
- int dstfmt_val;
- Uint32 rgbmask = ~src_fmt->Amask;
- Uint32 ckey = info->colorkey & rgbmask;
-
- Uint8 *dst_ptr = info->dst;
- int dst_pitch = info->dst_pitch;;
-
- srcfmt_val = detect_format(src_fmt);
- dstfmt_val = detect_format(dst_fmt);
-
- TRIANGLE_BEGIN_LOOP
- {
- Uint8 *src;
- Uint8 *dst = dptr;
- TRIANGLE_GET_TEXTCOORD
- src = (info->src + (srcy * info->src_pitch) + (srcx * srcbpp));
- if (FORMAT_HAS_ALPHA(srcfmt_val)) {
- DISEMBLE_RGBA(src, srcbpp, src_fmt, srcpixel, srcR, srcG, srcB, srcA);
- } else if (FORMAT_HAS_NO_ALPHA(srcfmt_val)) {
- DISEMBLE_RGB(src, srcbpp, src_fmt, srcpixel, srcR, srcG, srcB);
- srcA = 0xFF;
- } else {
- /* SDL_PIXELFORMAT_ARGB2101010 */
- srcpixel = *((Uint32 *)(src));
- RGBA_FROM_ARGB2101010(srcpixel, srcR, srcG, srcB, srcA);
- }
- if (flags & SDL_COPY_COLORKEY) {
- /* srcpixel isn't set for 24 bpp */
- if (srcbpp == 3) {
- srcpixel = (srcR << src_fmt->Rshift) |
- (srcG << src_fmt->Gshift) | (srcB << src_fmt->Bshift);
- }
- if ((srcpixel & rgbmask) == ckey) {
- continue;
- }
- }
- if (FORMAT_HAS_ALPHA(dstfmt_val)) {
- DISEMBLE_RGBA(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG, dstB, dstA);
- } else if (FORMAT_HAS_NO_ALPHA(dstfmt_val)) {
- DISEMBLE_RGB(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG, dstB);
- dstA = 0xFF;
- } else {
- /* SDL_PIXELFORMAT_ARGB2101010 */
- dstpixel = *((Uint32 *)(dst));
- RGBA_FROM_ARGB2101010(dstpixel, dstR, dstG, dstB, dstA);
- }
-
- if (! is_uniform) {
- TRIANGLE_GET_COLOR
- modulateR = r;
- modulateG = g;
- modulateB = b;
- modulateA = a;
- }
-
- if (flags & SDL_COPY_MODULATE_COLOR) {
- srcR = (srcR * modulateR) / 255;
- srcG = (srcG * modulateG) / 255;
- srcB = (srcB * modulateB) / 255;
- }
- if (flags & SDL_COPY_MODULATE_ALPHA) {
- srcA = (srcA * modulateA) / 255;
- }
- if (flags & (SDL_COPY_BLEND | SDL_COPY_ADD)) {
- /* This goes away if we ever use premultiplied alpha */
- if (srcA < 255) {
- srcR = (srcR * srcA) / 255;
- srcG = (srcG * srcA) / 255;
- srcB = (srcB * srcA) / 255;
- }
- }
- switch (flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL)) {
- case 0:
- dstR = srcR;
- dstG = srcG;
- dstB = srcB;
- dstA = srcA;
- break;
- case SDL_COPY_BLEND:
- dstR = srcR + ((255 - srcA) * dstR) / 255;
- dstG = srcG + ((255 - srcA) * dstG) / 255;
- dstB = srcB + ((255 - srcA) * dstB) / 255;
- dstA = srcA + ((255 - srcA) * dstA) / 255;
- break;
- case SDL_COPY_ADD:
- dstR = srcR + dstR;
- if (dstR > 255)
- dstR = 255;
- dstG = srcG + dstG;
- if (dstG > 255)
- dstG = 255;
- dstB = srcB + dstB;
- if (dstB > 255)
- dstB = 255;
- break;
- case SDL_COPY_MOD:
- dstR = (srcR * dstR) / 255;
- dstG = (srcG * dstG) / 255;
- dstB = (srcB * dstB) / 255;
- break;
- case SDL_COPY_MUL:
- dstR = ((srcR * dstR) + (dstR * (255 - srcA))) / 255;
- if (dstR > 255)
- dstR = 255;
- dstG = ((srcG * dstG) + (dstG * (255 - srcA))) / 255;
- if (dstG > 255)
- dstG = 255;
- dstB = ((srcB * dstB) + (dstB * (255 - srcA))) / 255;
- if (dstB > 255)
- dstB = 255;
- dstA = ((srcA * dstA) + (dstA * (255 - srcA))) / 255;
- if (dstA > 255)
- dstA = 255;
- break;
- }
- if (FORMAT_HAS_ALPHA(dstfmt_val)) {
- ASSEMBLE_RGBA(dst, dstbpp, dst_fmt, dstR, dstG, dstB, dstA);
- } else if (FORMAT_HAS_NO_ALPHA(dstfmt_val)) {
- ASSEMBLE_RGB(dst, dstbpp, dst_fmt, dstR, dstG, dstB);
- } else {
- /* SDL_PIXELFORMAT_ARGB2101010 */
- Uint32 pixel;
- ARGB2101010_FROM_RGBA(pixel, dstR, dstG, dstB, dstA);
- *(Uint32 *)dst = pixel;
- }
- }
- TRIANGLE_END_LOOP
-}
-
#endif /* SDL_VIDEO_RENDER_PS2 && !SDL_RENDER_DISABLED */
/* vi: set ts=4 sw=4 expandtab: */