SDL_triangle.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_RENDER_SW && !SDL_RENDER_DISABLED
  20. #include "SDL_surface.h"
  21. #include "SDL_triangle.h"
  22. #include "../../video/SDL_blit.h"
  23. /* fixed points bits precision
  24. * Set to 1, so that it can start rendering wth middle of a pixel precision.
  25. * It doesn't need to be increased.
  26. * But, if increased too much, it overflows (srcx, srcy) coordinates used for filling with texture.
  27. * (which could be turned to int64).
  28. */
  29. #define FP_BITS 1
  30. #define COLOR_EQ(c1, c2) ((c1).r == (c2).r && (c1).g == (c2).g && (c1).b == (c2).b && (c1).a == (c2).a)
  31. static void SDL_BlitTriangle_Slow(SDL_BlitInfo *info,
  32. SDL_Point s2_x_area, SDL_Rect dstrect, int area, int bias_w0, int bias_w1, int bias_w2,
  33. int d2d1_y, int d1d2_x, int d0d2_y, int d2d0_x, int d1d0_y, int d0d1_x,
  34. int s2s0_x, int s2s1_x, int s2s0_y, int s2s1_y, int w0_row, int w1_row, int w2_row,
  35. SDL_Color c0, SDL_Color c1, SDL_Color c2, int is_uniform);
  36. #if 0
  37. int SDL_BlitTriangle(SDL_Surface *src, const SDL_Point srcpoints[3], SDL_Surface *dst, const SDL_Point dstpoints[3])
  38. {
  39. int i;
  40. SDL_Point points[6];
  41. if (src == NULL || dst == NULL) {
  42. return -1;
  43. }
  44. for (i = 0; i < 3; i++) {
  45. if (srcpoints[i].x < 0 || srcpoints[i].y < 0 || srcpoints[i].x >= src->w || srcpoints[i].y >= src->h) {
  46. return SDL_SetError("Values of 'srcpoints' out of bounds");
  47. }
  48. }
  49. points[0] = srcpoints[0];
  50. points[1] = dstpoints[0];
  51. points[2] = srcpoints[1];
  52. points[3] = dstpoints[1];
  53. points[4] = srcpoints[2];
  54. points[5] = dstpoints[2];
  55. for (i = 0; i < 3; i++) {
  56. trianglepoint_2_fixedpoint(&points[2 * i + 1]);
  57. }
  58. return SDL_SW_BlitTriangle(src, dst, points);
  59. }
  60. int SDL_FillTriangle(SDL_Surface *dst, const SDL_Point points[3], Uint32 color)
  61. {
  62. int i;
  63. SDL_Point points_tmp[3];
  64. if (dst == NULL) {
  65. return -1;
  66. }
  67. for (i = 0; i < 3; i++) {
  68. points_tmp[i] = points[i];
  69. trianglepoint_2_fixedpoint(&points_tmp[i]);
  70. }
  71. return SDL_SW_FillTriangle(dst, points_tmp, SDL_BLENDMODE_NONE, color);
  72. }
  73. #endif
  74. /* cross product AB x AC */
  75. static int cross_product(const SDL_Point *a, const SDL_Point *b, int c_x, int c_y)
  76. {
  77. return (b->x - a->x) * (c_y - a->y) - (b->y - a->y) * (c_x - a->x);
  78. }
  79. /* check for top left rules */
  80. static int is_top_left(const SDL_Point *a, const SDL_Point *b, int is_clockwise)
  81. {
  82. if (is_clockwise) {
  83. if (a->y == b->y && a->x < b->x) {
  84. return 1;
  85. }
  86. if (b->y < a->y) {
  87. return 1;
  88. }
  89. } else {
  90. if (a->y == b->y && b->x < a->x) {
  91. return 1;
  92. }
  93. if (a->y < b->y) {
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }
  99. void trianglepoint_2_fixedpoint(SDL_Point *a)
  100. {
  101. a->x <<= FP_BITS;
  102. a->y <<= FP_BITS;
  103. }
  104. /* bounding rect of three points (in fixed point) */
  105. static void bounding_rect_fixedpoint(const SDL_Point *a, const SDL_Point *b, const SDL_Point *c, SDL_Rect *r)
  106. {
  107. int min_x = SDL_min(a->x, SDL_min(b->x, c->x));
  108. int max_x = SDL_max(a->x, SDL_max(b->x, c->x));
  109. int min_y = SDL_min(a->y, SDL_min(b->y, c->y));
  110. int max_y = SDL_max(a->y, SDL_max(b->y, c->y));
  111. /* points are in fixed point, shift back */
  112. r->x = min_x >> FP_BITS;
  113. r->y = min_y >> FP_BITS;
  114. r->w = (max_x - min_x) >> FP_BITS;
  115. r->h = (max_y - min_y) >> FP_BITS;
  116. }
  117. /* bounding rect of three points */
  118. static void bounding_rect(const SDL_Point *a, const SDL_Point *b, const SDL_Point *c, SDL_Rect *r)
  119. {
  120. int min_x = SDL_min(a->x, SDL_min(b->x, c->x));
  121. int max_x = SDL_max(a->x, SDL_max(b->x, c->x));
  122. int min_y = SDL_min(a->y, SDL_min(b->y, c->y));
  123. int max_y = SDL_max(a->y, SDL_max(b->y, c->y));
  124. r->x = min_x;
  125. r->y = min_y;
  126. r->w = (max_x - min_x);
  127. r->h = (max_y - min_y);
  128. }
  129. /* Triangle rendering, using Barycentric coordinates (w0, w1, w2)
  130. *
  131. * The cross product isn't computed from scratch at each iteration,
  132. * but optimized using constant step increments
  133. *
  134. */
  135. #define TRIANGLE_BEGIN_LOOP \
  136. { \
  137. int x, y; \
  138. for (y = 0; y < dstrect.h; y++) { \
  139. /* y start */ \
  140. int w0 = w0_row; \
  141. int w1 = w1_row; \
  142. int w2 = w2_row; \
  143. for (x = 0; x < dstrect.w; x++) { \
  144. /* In triangle */ \
  145. if (w0 + bias_w0 >= 0 && w1 + bias_w1 >= 0 && w2 + bias_w2 >= 0) { \
  146. Uint8 *dptr = (Uint8 *)dst_ptr + x * dstbpp;
  147. /* Use 64 bits precision to prevent overflow when interpolating color / texture with wide triangles */
  148. #define TRIANGLE_GET_TEXTCOORD \
  149. int srcx = (int)(((Sint64)w0 * s2s0_x + (Sint64)w1 * s2s1_x + s2_x_area.x) / area); \
  150. int srcy = (int)(((Sint64)w0 * s2s0_y + (Sint64)w1 * s2s1_y + s2_x_area.y) / area);
  151. #define TRIANGLE_GET_MAPPED_COLOR \
  152. int r = (int)(((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area); \
  153. int g = (int)(((Sint64)w0 * c0.g + (Sint64)w1 * c1.g + (Sint64)w2 * c2.g) / area); \
  154. int b = (int)(((Sint64)w0 * c0.b + (Sint64)w1 * c1.b + (Sint64)w2 * c2.b) / area); \
  155. int a = (int)(((Sint64)w0 * c0.a + (Sint64)w1 * c1.a + (Sint64)w2 * c2.a) / area); \
  156. int color = SDL_MapRGBA(format, r, g, b, a);
  157. #define TRIANGLE_GET_COLOR \
  158. int r = (int)(((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area); \
  159. int g = (int)(((Sint64)w0 * c0.g + (Sint64)w1 * c1.g + (Sint64)w2 * c2.g) / area); \
  160. int b = (int)(((Sint64)w0 * c0.b + (Sint64)w1 * c1.b + (Sint64)w2 * c2.b) / area); \
  161. int a = (int)(((Sint64)w0 * c0.a + (Sint64)w1 * c1.a + (Sint64)w2 * c2.a) / area);
  162. #define TRIANGLE_END_LOOP \
  163. } \
  164. /* x += 1 */ \
  165. w0 += d2d1_y; \
  166. w1 += d0d2_y; \
  167. w2 += d1d0_y; \
  168. } \
  169. /* y += 1 */ \
  170. w0_row += d1d2_x; \
  171. w1_row += d2d0_x; \
  172. w2_row += d0d1_x; \
  173. dst_ptr += dst_pitch; \
  174. } \
  175. }
  176. int SDL_SW_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)
  177. {
  178. int ret = 0;
  179. int dst_locked = 0;
  180. SDL_Rect dstrect;
  181. int dstbpp;
  182. Uint8 *dst_ptr;
  183. int dst_pitch;
  184. int area, is_clockwise;
  185. int d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x;
  186. int w0_row, w1_row, w2_row;
  187. int bias_w0, bias_w1, bias_w2;
  188. int is_uniform;
  189. SDL_Surface *tmp = NULL;
  190. if (dst == NULL) {
  191. return -1;
  192. }
  193. area = cross_product(d0, d1, d2->x, d2->y);
  194. is_uniform = COLOR_EQ(c0, c1) && COLOR_EQ(c1, c2);
  195. /* Flat triangle */
  196. if (area == 0) {
  197. return 0;
  198. }
  199. /* Lock the destination, if needed */
  200. if (SDL_MUSTLOCK(dst)) {
  201. if (SDL_LockSurface(dst) < 0) {
  202. ret = -1;
  203. goto end;
  204. } else {
  205. dst_locked = 1;
  206. }
  207. }
  208. bounding_rect_fixedpoint(d0, d1, d2, &dstrect);
  209. {
  210. /* Clip triangle rect with surface rect */
  211. SDL_Rect rect;
  212. rect.x = 0;
  213. rect.y = 0;
  214. rect.w = dst->w;
  215. rect.h = dst->h;
  216. SDL_IntersectRect(&dstrect, &rect, &dstrect);
  217. }
  218. {
  219. /* Clip triangle with surface clip rect */
  220. SDL_Rect rect;
  221. SDL_GetClipRect(dst, &rect);
  222. SDL_IntersectRect(&dstrect, &rect, &dstrect);
  223. }
  224. if (blend != SDL_BLENDMODE_NONE) {
  225. int format = dst->format->format;
  226. /* need an alpha format */
  227. if (!dst->format->Amask) {
  228. format = SDL_PIXELFORMAT_ARGB8888;
  229. }
  230. /* Use an intermediate surface */
  231. tmp = SDL_CreateRGBSurfaceWithFormat(0, dstrect.w, dstrect.h, 0, format);
  232. if (tmp == NULL) {
  233. ret = -1;
  234. goto end;
  235. }
  236. if (blend == SDL_BLENDMODE_MOD) {
  237. Uint32 c = SDL_MapRGBA(tmp->format, 255, 255, 255, 255);
  238. SDL_FillRect(tmp, NULL, c);
  239. }
  240. SDL_SetSurfaceBlendMode(tmp, blend);
  241. dstbpp = tmp->format->BytesPerPixel;
  242. dst_ptr = tmp->pixels;
  243. dst_pitch = tmp->pitch;
  244. } else {
  245. /* Write directly to destination surface */
  246. dstbpp = dst->format->BytesPerPixel;
  247. dst_ptr = (Uint8 *)dst->pixels + dstrect.x * dstbpp + dstrect.y * dst->pitch;
  248. dst_pitch = dst->pitch;
  249. }
  250. is_clockwise = area > 0;
  251. area = SDL_abs(area);
  252. d2d1_y = (d1->y - d2->y) << FP_BITS;
  253. d0d2_y = (d2->y - d0->y) << FP_BITS;
  254. d1d0_y = (d0->y - d1->y) << FP_BITS;
  255. d1d2_x = (d2->x - d1->x) << FP_BITS;
  256. d2d0_x = (d0->x - d2->x) << FP_BITS;
  257. d0d1_x = (d1->x - d0->x) << FP_BITS;
  258. /* Starting point for rendering, at the middle of a pixel */
  259. {
  260. SDL_Point p;
  261. p.x = dstrect.x;
  262. p.y = dstrect.y;
  263. trianglepoint_2_fixedpoint(&p);
  264. p.x += (1 << FP_BITS) / 2;
  265. p.y += (1 << FP_BITS) / 2;
  266. w0_row = cross_product(d1, d2, p.x, p.y);
  267. w1_row = cross_product(d2, d0, p.x, p.y);
  268. w2_row = cross_product(d0, d1, p.x, p.y);
  269. }
  270. /* Handle anti-clockwise triangles */
  271. if (!is_clockwise) {
  272. d2d1_y *= -1;
  273. d0d2_y *= -1;
  274. d1d0_y *= -1;
  275. d1d2_x *= -1;
  276. d2d0_x *= -1;
  277. d0d1_x *= -1;
  278. w0_row *= -1;
  279. w1_row *= -1;
  280. w2_row *= -1;
  281. }
  282. /* Add a bias to respect top-left rasterization rule */
  283. bias_w0 = (is_top_left(d1, d2, is_clockwise) ? 0 : -1);
  284. bias_w1 = (is_top_left(d2, d0, is_clockwise) ? 0 : -1);
  285. bias_w2 = (is_top_left(d0, d1, is_clockwise) ? 0 : -1);
  286. if (is_uniform) {
  287. Uint32 color;
  288. if (tmp) {
  289. if (dst->format->Amask) {
  290. color = SDL_MapRGBA(tmp->format, c0.r, c0.g, c0.b, c0.a);
  291. } else {
  292. // color = SDL_MapRGB(tmp->format, c0.r, c0.g, c0.b);
  293. color = SDL_MapRGBA(tmp->format, c0.r, c0.g, c0.b, c0.a);
  294. }
  295. } else {
  296. color = SDL_MapRGBA(dst->format, c0.r, c0.g, c0.b, c0.a);
  297. }
  298. if (dstbpp == 4) {
  299. TRIANGLE_BEGIN_LOOP
  300. {
  301. *(Uint32 *)dptr = color;
  302. }
  303. TRIANGLE_END_LOOP
  304. } else if (dstbpp == 3) {
  305. TRIANGLE_BEGIN_LOOP
  306. {
  307. Uint8 *s = (Uint8 *)&color;
  308. dptr[0] = s[0];
  309. dptr[1] = s[1];
  310. dptr[2] = s[2];
  311. }
  312. TRIANGLE_END_LOOP
  313. } else if (dstbpp == 2) {
  314. TRIANGLE_BEGIN_LOOP
  315. {
  316. *(Uint16 *)dptr = color;
  317. }
  318. TRIANGLE_END_LOOP
  319. } else if (dstbpp == 1) {
  320. TRIANGLE_BEGIN_LOOP
  321. {
  322. *dptr = color;
  323. }
  324. TRIANGLE_END_LOOP
  325. }
  326. } else {
  327. SDL_PixelFormat *format = dst->format;
  328. if (tmp) {
  329. format = tmp->format;
  330. }
  331. if (dstbpp == 4) {
  332. TRIANGLE_BEGIN_LOOP
  333. {
  334. TRIANGLE_GET_MAPPED_COLOR
  335. *(Uint32 *)dptr = color;
  336. }
  337. TRIANGLE_END_LOOP
  338. } else if (dstbpp == 3) {
  339. TRIANGLE_BEGIN_LOOP
  340. {
  341. TRIANGLE_GET_MAPPED_COLOR
  342. Uint8 *s = (Uint8 *)&color;
  343. dptr[0] = s[0];
  344. dptr[1] = s[1];
  345. dptr[2] = s[2];
  346. }
  347. TRIANGLE_END_LOOP
  348. } else if (dstbpp == 2) {
  349. TRIANGLE_BEGIN_LOOP
  350. {
  351. TRIANGLE_GET_MAPPED_COLOR
  352. *(Uint16 *)dptr = color;
  353. }
  354. TRIANGLE_END_LOOP
  355. } else if (dstbpp == 1) {
  356. TRIANGLE_BEGIN_LOOP
  357. {
  358. TRIANGLE_GET_MAPPED_COLOR
  359. *dptr = color;
  360. }
  361. TRIANGLE_END_LOOP
  362. }
  363. }
  364. if (tmp) {
  365. SDL_BlitSurface(tmp, NULL, dst, &dstrect);
  366. SDL_FreeSurface(tmp);
  367. }
  368. end:
  369. if (dst_locked) {
  370. SDL_UnlockSurface(dst);
  371. }
  372. return ret;
  373. }
  374. int SDL_SW_BlitTriangle(
  375. SDL_Surface *src,
  376. SDL_Point *s0, SDL_Point *s1, SDL_Point *s2,
  377. SDL_Surface *dst,
  378. SDL_Point *d0, SDL_Point *d1, SDL_Point *d2,
  379. SDL_Color c0, SDL_Color c1, SDL_Color c2)
  380. {
  381. int ret = 0;
  382. int src_locked = 0;
  383. int dst_locked = 0;
  384. SDL_BlendMode blend;
  385. SDL_Rect dstrect;
  386. SDL_Point s2_x_area;
  387. int dstbpp;
  388. Uint8 *dst_ptr;
  389. int dst_pitch;
  390. int *src_ptr;
  391. int src_pitch;
  392. int area, is_clockwise;
  393. int d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x;
  394. int s2s0_x, s2s1_x, s2s0_y, s2s1_y;
  395. int w0_row, w1_row, w2_row;
  396. int bias_w0, bias_w1, bias_w2;
  397. int is_uniform;
  398. int has_modulation;
  399. if (src == NULL || dst == NULL) {
  400. return -1;
  401. }
  402. area = cross_product(d0, d1, d2->x, d2->y);
  403. /* Flat triangle */
  404. if (area == 0) {
  405. return 0;
  406. }
  407. /* Lock the destination, if needed */
  408. if (SDL_MUSTLOCK(dst)) {
  409. if (SDL_LockSurface(dst) < 0) {
  410. ret = -1;
  411. goto end;
  412. } else {
  413. dst_locked = 1;
  414. }
  415. }
  416. /* Lock the source, if needed */
  417. if (SDL_MUSTLOCK(src)) {
  418. if (SDL_LockSurface(src) < 0) {
  419. ret = -1;
  420. goto end;
  421. } else {
  422. src_locked = 1;
  423. }
  424. }
  425. is_uniform = COLOR_EQ(c0, c1) && COLOR_EQ(c1, c2);
  426. bounding_rect_fixedpoint(d0, d1, d2, &dstrect);
  427. SDL_GetSurfaceBlendMode(src, &blend);
  428. /* TRIANGLE_GET_TEXTCOORD interpolates up to the max values included, so reduce by 1 */
  429. {
  430. SDL_Rect srcrect;
  431. int maxx, maxy;
  432. bounding_rect(s0, s1, s2, &srcrect);
  433. maxx = srcrect.x + srcrect.w;
  434. maxy = srcrect.y + srcrect.h;
  435. if (srcrect.w > 0) {
  436. if (s0->x == maxx) {
  437. s0->x--;
  438. }
  439. if (s1->x == maxx) {
  440. s1->x--;
  441. }
  442. if (s2->x == maxx) {
  443. s2->x--;
  444. }
  445. }
  446. if (srcrect.h > 0) {
  447. if (s0->y == maxy) {
  448. s0->y--;
  449. }
  450. if (s1->y == maxy) {
  451. s1->y--;
  452. }
  453. if (s2->y == maxy) {
  454. s2->y--;
  455. }
  456. }
  457. }
  458. if (is_uniform) {
  459. // SDL_GetSurfaceColorMod(src, &r, &g, &b);
  460. has_modulation = c0.r != 255 || c0.g != 255 || c0.b != 255 || c0.a != 255;
  461. } else {
  462. has_modulation = SDL_TRUE;
  463. }
  464. {
  465. /* Clip triangle rect with surface rect */
  466. SDL_Rect rect;
  467. rect.x = 0;
  468. rect.y = 0;
  469. rect.w = dst->w;
  470. rect.h = dst->h;
  471. SDL_IntersectRect(&dstrect, &rect, &dstrect);
  472. }
  473. {
  474. /* Clip triangle with surface clip rect */
  475. SDL_Rect rect;
  476. SDL_GetClipRect(dst, &rect);
  477. SDL_IntersectRect(&dstrect, &rect, &dstrect);
  478. }
  479. /* Set destination pointer */
  480. dstbpp = dst->format->BytesPerPixel;
  481. dst_ptr = (Uint8 *)dst->pixels + dstrect.x * dstbpp + dstrect.y * dst->pitch;
  482. dst_pitch = dst->pitch;
  483. /* Set source pointer */
  484. src_ptr = src->pixels;
  485. src_pitch = src->pitch;
  486. is_clockwise = area > 0;
  487. area = SDL_abs(area);
  488. d2d1_y = (d1->y - d2->y) << FP_BITS;
  489. d0d2_y = (d2->y - d0->y) << FP_BITS;
  490. d1d0_y = (d0->y - d1->y) << FP_BITS;
  491. d1d2_x = (d2->x - d1->x) << FP_BITS;
  492. d2d0_x = (d0->x - d2->x) << FP_BITS;
  493. d0d1_x = (d1->x - d0->x) << FP_BITS;
  494. s2s0_x = s0->x - s2->x;
  495. s2s1_x = s1->x - s2->x;
  496. s2s0_y = s0->y - s2->y;
  497. s2s1_y = s1->y - s2->y;
  498. /* Starting point for rendering, at the middle of a pixel */
  499. {
  500. SDL_Point p;
  501. p.x = dstrect.x;
  502. p.y = dstrect.y;
  503. trianglepoint_2_fixedpoint(&p);
  504. p.x += (1 << FP_BITS) / 2;
  505. p.y += (1 << FP_BITS) / 2;
  506. w0_row = cross_product(d1, d2, p.x, p.y);
  507. w1_row = cross_product(d2, d0, p.x, p.y);
  508. w2_row = cross_product(d0, d1, p.x, p.y);
  509. }
  510. /* Handle anti-clockwise triangles */
  511. if (!is_clockwise) {
  512. d2d1_y *= -1;
  513. d0d2_y *= -1;
  514. d1d0_y *= -1;
  515. d1d2_x *= -1;
  516. d2d0_x *= -1;
  517. d0d1_x *= -1;
  518. w0_row *= -1;
  519. w1_row *= -1;
  520. w2_row *= -1;
  521. }
  522. /* Add a bias to respect top-left rasterization rule */
  523. bias_w0 = (is_top_left(d1, d2, is_clockwise) ? 0 : -1);
  524. bias_w1 = (is_top_left(d2, d0, is_clockwise) ? 0 : -1);
  525. bias_w2 = (is_top_left(d0, d1, is_clockwise) ? 0 : -1);
  526. /* precompute constant 's2->x * area' used in TRIANGLE_GET_TEXTCOORD */
  527. s2_x_area.x = s2->x * area;
  528. s2_x_area.y = s2->y * area;
  529. if (blend != SDL_BLENDMODE_NONE || src->format->format != dst->format->format || has_modulation || !is_uniform) {
  530. /* Use SDL_BlitTriangle_Slow */
  531. SDL_BlitInfo *info = &src->map->info;
  532. SDL_BlitInfo tmp_info;
  533. SDL_zero(tmp_info);
  534. tmp_info.src_fmt = src->format;
  535. tmp_info.dst_fmt = dst->format;
  536. tmp_info.flags = info->flags;
  537. /*
  538. tmp_info.r = info->r;
  539. tmp_info.g = info->g;
  540. tmp_info.b = info->b;
  541. tmp_info.a = info->a;
  542. */
  543. tmp_info.r = c0.r;
  544. tmp_info.g = c0.g;
  545. tmp_info.b = c0.b;
  546. tmp_info.a = c0.a;
  547. tmp_info.flags &= ~(SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA);
  548. if (c0.r != 255 || c1.r != 255 || c2.r != 255 ||
  549. c0.g != 255 || c1.g != 255 || c2.g != 255 ||
  550. c0.b != 255 || c1.b != 255 || c2.b != 255) {
  551. tmp_info.flags |= SDL_COPY_MODULATE_COLOR;
  552. }
  553. if (c0.a != 255 || c1.a != 255 || c2.a != 255) {
  554. tmp_info.flags |= SDL_COPY_MODULATE_ALPHA;
  555. }
  556. tmp_info.colorkey = info->colorkey;
  557. /* src */
  558. tmp_info.src = (Uint8 *)src_ptr;
  559. tmp_info.src_pitch = src_pitch;
  560. /* dst */
  561. tmp_info.dst = dst_ptr;
  562. tmp_info.dst_pitch = dst_pitch;
  563. SDL_BlitTriangle_Slow(&tmp_info, s2_x_area, dstrect, area, bias_w0, bias_w1, bias_w2,
  564. d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x,
  565. s2s0_x, s2s1_x, s2s0_y, s2s1_y, w0_row, w1_row, w2_row,
  566. c0, c1, c2, is_uniform);
  567. goto end;
  568. }
  569. if (dstbpp == 4) {
  570. TRIANGLE_BEGIN_LOOP
  571. {
  572. TRIANGLE_GET_TEXTCOORD
  573. Uint32 *sptr = (Uint32 *)((Uint8 *)src_ptr + srcy * src_pitch);
  574. *(Uint32 *)dptr = sptr[srcx];
  575. }
  576. TRIANGLE_END_LOOP
  577. } else if (dstbpp == 3) {
  578. TRIANGLE_BEGIN_LOOP
  579. {
  580. TRIANGLE_GET_TEXTCOORD
  581. Uint8 *sptr = (Uint8 *)src_ptr + srcy * src_pitch;
  582. dptr[0] = sptr[3 * srcx];
  583. dptr[1] = sptr[3 * srcx + 1];
  584. dptr[2] = sptr[3 * srcx + 2];
  585. }
  586. TRIANGLE_END_LOOP
  587. } else if (dstbpp == 2) {
  588. TRIANGLE_BEGIN_LOOP
  589. {
  590. TRIANGLE_GET_TEXTCOORD
  591. Uint16 *sptr = (Uint16 *)((Uint8 *)src_ptr + srcy * src_pitch);
  592. *(Uint16 *)dptr = sptr[srcx];
  593. }
  594. TRIANGLE_END_LOOP
  595. } else if (dstbpp == 1) {
  596. TRIANGLE_BEGIN_LOOP
  597. {
  598. TRIANGLE_GET_TEXTCOORD
  599. Uint8 *sptr = (Uint8 *)src_ptr + srcy * src_pitch;
  600. *dptr = sptr[srcx];
  601. }
  602. TRIANGLE_END_LOOP
  603. }
  604. end:
  605. if (dst_locked) {
  606. SDL_UnlockSurface(dst);
  607. }
  608. if (src_locked) {
  609. SDL_UnlockSurface(src);
  610. }
  611. return ret;
  612. }
  613. #define FORMAT_ALPHA 0
  614. #define FORMAT_NO_ALPHA -1
  615. #define FORMAT_2101010 1
  616. #define FORMAT_HAS_ALPHA(format) format == 0
  617. #define FORMAT_HAS_NO_ALPHA(format) format < 0
  618. static int SDL_INLINE detect_format(SDL_PixelFormat *pf)
  619. {
  620. if (pf->format == SDL_PIXELFORMAT_ARGB2101010) {
  621. return FORMAT_2101010;
  622. } else if (pf->Amask) {
  623. return FORMAT_ALPHA;
  624. } else {
  625. return FORMAT_NO_ALPHA;
  626. }
  627. }
  628. static void SDL_BlitTriangle_Slow(SDL_BlitInfo *info,
  629. SDL_Point s2_x_area, SDL_Rect dstrect, int area, int bias_w0, int bias_w1, int bias_w2,
  630. int d2d1_y, int d1d2_x, int d0d2_y, int d2d0_x, int d1d0_y, int d0d1_x,
  631. int s2s0_x, int s2s1_x, int s2s0_y, int s2s1_y, int w0_row, int w1_row, int w2_row,
  632. SDL_Color c0, SDL_Color c1, SDL_Color c2, int is_uniform)
  633. {
  634. const int flags = info->flags;
  635. Uint32 modulateR = info->r;
  636. Uint32 modulateG = info->g;
  637. Uint32 modulateB = info->b;
  638. Uint32 modulateA = info->a;
  639. Uint32 srcpixel;
  640. Uint32 srcR, srcG, srcB, srcA;
  641. Uint32 dstpixel;
  642. Uint32 dstR, dstG, dstB, dstA;
  643. SDL_PixelFormat *src_fmt = info->src_fmt;
  644. SDL_PixelFormat *dst_fmt = info->dst_fmt;
  645. int srcbpp = src_fmt->BytesPerPixel;
  646. int dstbpp = dst_fmt->BytesPerPixel;
  647. int srcfmt_val;
  648. int dstfmt_val;
  649. Uint32 rgbmask = ~src_fmt->Amask;
  650. Uint32 ckey = info->colorkey & rgbmask;
  651. Uint8 *dst_ptr = info->dst;
  652. int dst_pitch = info->dst_pitch;
  653. srcfmt_val = detect_format(src_fmt);
  654. dstfmt_val = detect_format(dst_fmt);
  655. TRIANGLE_BEGIN_LOOP
  656. {
  657. Uint8 *src;
  658. Uint8 *dst = dptr;
  659. TRIANGLE_GET_TEXTCOORD
  660. src = (info->src + (srcy * info->src_pitch) + (srcx * srcbpp));
  661. if (FORMAT_HAS_ALPHA(srcfmt_val)) {
  662. DISEMBLE_RGBA(src, srcbpp, src_fmt, srcpixel, srcR, srcG, srcB, srcA);
  663. } else if (FORMAT_HAS_NO_ALPHA(srcfmt_val)) {
  664. DISEMBLE_RGB(src, srcbpp, src_fmt, srcpixel, srcR, srcG, srcB);
  665. srcA = 0xFF;
  666. } else {
  667. /* SDL_PIXELFORMAT_ARGB2101010 */
  668. srcpixel = *((Uint32 *)(src));
  669. RGBA_FROM_ARGB2101010(srcpixel, srcR, srcG, srcB, srcA);
  670. }
  671. if (flags & SDL_COPY_COLORKEY) {
  672. /* srcpixel isn't set for 24 bpp */
  673. if (srcbpp == 3) {
  674. srcpixel = (srcR << src_fmt->Rshift) |
  675. (srcG << src_fmt->Gshift) | (srcB << src_fmt->Bshift);
  676. }
  677. if ((srcpixel & rgbmask) == ckey) {
  678. continue;
  679. }
  680. }
  681. if (FORMAT_HAS_ALPHA(dstfmt_val)) {
  682. DISEMBLE_RGBA(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG, dstB, dstA);
  683. } else if (FORMAT_HAS_NO_ALPHA(dstfmt_val)) {
  684. DISEMBLE_RGB(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG, dstB);
  685. dstA = 0xFF;
  686. } else {
  687. /* SDL_PIXELFORMAT_ARGB2101010 */
  688. dstpixel = *((Uint32 *)(dst));
  689. RGBA_FROM_ARGB2101010(dstpixel, dstR, dstG, dstB, dstA);
  690. }
  691. if (!is_uniform) {
  692. TRIANGLE_GET_COLOR
  693. modulateR = r;
  694. modulateG = g;
  695. modulateB = b;
  696. modulateA = a;
  697. }
  698. if (flags & SDL_COPY_MODULATE_COLOR) {
  699. srcR = (srcR * modulateR) / 255;
  700. srcG = (srcG * modulateG) / 255;
  701. srcB = (srcB * modulateB) / 255;
  702. }
  703. if (flags & SDL_COPY_MODULATE_ALPHA) {
  704. srcA = (srcA * modulateA) / 255;
  705. }
  706. if (flags & (SDL_COPY_BLEND | SDL_COPY_ADD)) {
  707. /* This goes away if we ever use premultiplied alpha */
  708. if (srcA < 255) {
  709. srcR = (srcR * srcA) / 255;
  710. srcG = (srcG * srcA) / 255;
  711. srcB = (srcB * srcA) / 255;
  712. }
  713. }
  714. switch (flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL)) {
  715. case 0:
  716. dstR = srcR;
  717. dstG = srcG;
  718. dstB = srcB;
  719. dstA = srcA;
  720. break;
  721. case SDL_COPY_BLEND:
  722. dstR = srcR + ((255 - srcA) * dstR) / 255;
  723. dstG = srcG + ((255 - srcA) * dstG) / 255;
  724. dstB = srcB + ((255 - srcA) * dstB) / 255;
  725. dstA = srcA + ((255 - srcA) * dstA) / 255;
  726. break;
  727. case SDL_COPY_ADD:
  728. dstR = srcR + dstR;
  729. if (dstR > 255) {
  730. dstR = 255;
  731. }
  732. dstG = srcG + dstG;
  733. if (dstG > 255) {
  734. dstG = 255;
  735. }
  736. dstB = srcB + dstB;
  737. if (dstB > 255) {
  738. dstB = 255;
  739. }
  740. break;
  741. case SDL_COPY_MOD:
  742. dstR = (srcR * dstR) / 255;
  743. dstG = (srcG * dstG) / 255;
  744. dstB = (srcB * dstB) / 255;
  745. break;
  746. case SDL_COPY_MUL:
  747. dstR = ((srcR * dstR) + (dstR * (255 - srcA))) / 255;
  748. if (dstR > 255) {
  749. dstR = 255;
  750. }
  751. dstG = ((srcG * dstG) + (dstG * (255 - srcA))) / 255;
  752. if (dstG > 255) {
  753. dstG = 255;
  754. }
  755. dstB = ((srcB * dstB) + (dstB * (255 - srcA))) / 255;
  756. if (dstB > 255) {
  757. dstB = 255;
  758. }
  759. dstA = ((srcA * dstA) + (dstA * (255 - srcA))) / 255;
  760. if (dstA > 255) {
  761. dstA = 255;
  762. }
  763. break;
  764. }
  765. if (FORMAT_HAS_ALPHA(dstfmt_val)) {
  766. ASSEMBLE_RGBA(dst, dstbpp, dst_fmt, dstR, dstG, dstB, dstA);
  767. } else if (FORMAT_HAS_NO_ALPHA(dstfmt_val)) {
  768. ASSEMBLE_RGB(dst, dstbpp, dst_fmt, dstR, dstG, dstB);
  769. } else {
  770. /* SDL_PIXELFORMAT_ARGB2101010 */
  771. Uint32 pixel;
  772. ARGB2101010_FROM_RGBA(pixel, dstR, dstG, dstB, dstA);
  773. *(Uint32 *)dst = pixel;
  774. }
  775. }
  776. TRIANGLE_END_LOOP
  777. }
  778. #endif /* SDL_VIDEO_RENDER_SW && !SDL_RENDER_DISABLED */
  779. /* vi: set ts=4 sw=4 expandtab: */