SDL_triangle.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. a->x <<= FP_BITS;
  101. a->y <<= FP_BITS;
  102. }
  103. /* bounding rect of three points */
  104. static void bounding_rect(const SDL_Point *a, const SDL_Point *b, const SDL_Point *c, SDL_Rect *r)
  105. {
  106. int min_x = SDL_min(a->x, SDL_min(b->x, c->x));
  107. int max_x = SDL_max(a->x, SDL_max(b->x, c->x));
  108. int min_y = SDL_min(a->y, SDL_min(b->y, c->y));
  109. int max_y = SDL_max(a->y, SDL_max(b->y, c->y));
  110. /* points are in fixed point, shift back */
  111. r->x = min_x >> FP_BITS;
  112. r->y = min_y >> FP_BITS;
  113. r->w = (max_x - min_x) >> FP_BITS;
  114. r->h = (max_y - min_y) >> FP_BITS;
  115. }
  116. /* Triangle rendering, using Barycentric coordinates (w0, w1, w2)
  117. *
  118. * The cross product isn't computed from scratch at each iteration,
  119. * but optimized using constant step increments
  120. *
  121. */
  122. #define TRIANGLE_BEGIN_LOOP \
  123. { \
  124. int x, y; \
  125. for (y = 0; y < dstrect.h; y++) { \
  126. /* y start */ \
  127. int w0 = w0_row; \
  128. int w1 = w1_row; \
  129. int w2 = w2_row; \
  130. for (x = 0; x < dstrect.w; x++) { \
  131. /* In triangle */ \
  132. if (w0 + bias_w0 >= 0 && w1 + bias_w1 >= 0 && w2 + bias_w2 >= 0) { \
  133. Uint8 *dptr = (Uint8 *) dst_ptr + x * dstbpp; \
  134. /* Use 64 bits precision to prevent overflow when interpolating color / texture with wide triangles */
  135. #define TRIANGLE_GET_TEXTCOORD \
  136. int srcx = ((Sint64)w0 * s2s0_x + (Sint64)w1 * s2s1_x + s2_x_area.x) / area; \
  137. int srcy = ((Sint64)w0 * s2s0_y + (Sint64)w1 * s2s1_y + s2_x_area.y) / area; \
  138. #define TRIANGLE_GET_MAPPED_COLOR \
  139. int r = ((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area; \
  140. int g = ((Sint64)w0 * c0.g + (Sint64)w1 * c1.g + (Sint64)w2 * c2.g) / area; \
  141. int b = ((Sint64)w0 * c0.b + (Sint64)w1 * c1.b + (Sint64)w2 * c2.b) / area; \
  142. int a = ((Sint64)w0 * c0.a + (Sint64)w1 * c1.a + (Sint64)w2 * c2.a) / area; \
  143. int color = SDL_MapRGBA(format, r, g, b, a); \
  144. #define TRIANGLE_GET_COLOR \
  145. int r = ((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area; \
  146. int g = ((Sint64)w0 * c0.g + (Sint64)w1 * c1.g + (Sint64)w2 * c2.g) / area; \
  147. int b = ((Sint64)w0 * c0.b + (Sint64)w1 * c1.b + (Sint64)w2 * c2.b) / area; \
  148. int a = ((Sint64)w0 * c0.a + (Sint64)w1 * c1.a + (Sint64)w2 * c2.a) / area; \
  149. #define TRIANGLE_END_LOOP \
  150. } \
  151. /* x += 1 */ \
  152. w0 += d2d1_y; \
  153. w1 += d0d2_y; \
  154. w2 += d1d0_y; \
  155. } \
  156. /* y += 1 */ \
  157. w0_row += d1d2_x; \
  158. w1_row += d2d0_x; \
  159. w2_row += d0d1_x; \
  160. dst_ptr += dst_pitch; \
  161. } \
  162. } \
  163. 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)
  164. {
  165. int ret = 0;
  166. int dst_locked = 0;
  167. SDL_Rect dstrect;
  168. int dstbpp;
  169. Uint8 *dst_ptr;
  170. int dst_pitch;
  171. int area, is_clockwise;
  172. int d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x;
  173. int w0_row, w1_row, w2_row;
  174. int bias_w0, bias_w1, bias_w2;
  175. int is_uniform;
  176. SDL_Surface *tmp = NULL;
  177. if (dst == NULL) {
  178. return -1;
  179. }
  180. area = cross_product(d0, d1, d2->x, d2->y);
  181. is_uniform = COLOR_EQ(c0, c1) && COLOR_EQ(c1, c2);
  182. /* Flat triangle */
  183. if (area == 0) {
  184. return 0;
  185. }
  186. /* Lock the destination, if needed */
  187. if (SDL_MUSTLOCK(dst)) {
  188. if (SDL_LockSurface(dst) < 0) {
  189. ret = -1;
  190. goto end;
  191. } else {
  192. dst_locked = 1;
  193. }
  194. }
  195. bounding_rect(d0, d1, d2, &dstrect);
  196. {
  197. /* Clip triangle rect with surface rect */
  198. SDL_Rect rect;
  199. rect.x = 0;
  200. rect.y = 0;
  201. rect.w = dst->w;
  202. rect.h = dst->h;
  203. SDL_IntersectRect(&dstrect, &rect, &dstrect);
  204. }
  205. {
  206. /* Clip triangle with surface clip rect */
  207. SDL_Rect rect;
  208. SDL_GetClipRect(dst, &rect);
  209. SDL_IntersectRect(&dstrect, &rect, &dstrect);
  210. }
  211. if (blend != SDL_BLENDMODE_NONE) {
  212. int format = dst->format->format;
  213. /* need an alpha format */
  214. if (! dst->format->Amask) {
  215. format = SDL_PIXELFORMAT_ARGB8888;
  216. }
  217. /* Use an intermediate surface */
  218. tmp = SDL_CreateRGBSurfaceWithFormat(0, dstrect.w, dstrect.h, 0, format);
  219. if (tmp == NULL) {
  220. ret = -1;
  221. goto end;
  222. }
  223. if (blend == SDL_BLENDMODE_MOD) {
  224. Uint32 c = SDL_MapRGBA(tmp->format, 255, 255, 255, 255);
  225. SDL_FillRect(tmp, NULL, c);
  226. }
  227. SDL_SetSurfaceBlendMode(tmp, blend);
  228. dstbpp = tmp->format->BytesPerPixel;
  229. dst_ptr = tmp->pixels;
  230. dst_pitch = tmp->pitch;
  231. } else {
  232. /* Write directly to destination surface */
  233. dstbpp = dst->format->BytesPerPixel;
  234. dst_ptr = (Uint8 *)dst->pixels + dstrect.x * dstbpp + dstrect.y * dst->pitch;
  235. dst_pitch = dst->pitch;
  236. }
  237. is_clockwise = area > 0;
  238. area = SDL_abs(area);
  239. d2d1_y = (d1->y - d2->y) << FP_BITS;
  240. d0d2_y = (d2->y - d0->y) << FP_BITS;
  241. d1d0_y = (d0->y - d1->y) << FP_BITS;
  242. d1d2_x = (d2->x - d1->x) << FP_BITS;
  243. d2d0_x = (d0->x - d2->x) << FP_BITS;
  244. d0d1_x = (d1->x - d0->x) << FP_BITS;
  245. /* Starting point for rendering, at the middle of a pixel */
  246. {
  247. SDL_Point p;
  248. p.x = dstrect.x;
  249. p.y = dstrect.y;
  250. trianglepoint_2_fixedpoint(&p);
  251. p.x += (1 << FP_BITS) / 2;
  252. p.y += (1 << FP_BITS) / 2;
  253. w0_row = cross_product(d1, d2, p.x, p.y);
  254. w1_row = cross_product(d2, d0, p.x, p.y);
  255. w2_row = cross_product(d0, d1, p.x, p.y);
  256. }
  257. /* Handle anti-clockwise triangles */
  258. if (! is_clockwise) {
  259. d2d1_y *= -1;
  260. d0d2_y *= -1;
  261. d1d0_y *= -1;
  262. d1d2_x *= -1;
  263. d2d0_x *= -1;
  264. d0d1_x *= -1;
  265. w0_row *= -1;
  266. w1_row *= -1;
  267. w2_row *= -1;
  268. }
  269. /* Add a bias to respect top-left rasterization rule */
  270. bias_w0 = (is_top_left(d1, d2, is_clockwise) ? 0 : -1);
  271. bias_w1 = (is_top_left(d2, d0, is_clockwise) ? 0 : -1);
  272. bias_w2 = (is_top_left(d0, d1, is_clockwise) ? 0 : -1);
  273. if (is_uniform) {
  274. Uint32 color;
  275. if (tmp) {
  276. if (dst->format->Amask) {
  277. color = SDL_MapRGBA(tmp->format, c0.r, c0.g, c0.b, c0.a);
  278. } else {
  279. //color = SDL_MapRGB(tmp->format, c0.r, c0.g, c0.b);
  280. color = SDL_MapRGBA(tmp->format, c0.r, c0.g, c0.b, c0.a);
  281. }
  282. } else {
  283. color = SDL_MapRGBA(dst->format, c0.r, c0.g, c0.b, c0.a);
  284. }
  285. if (dstbpp == 4) {
  286. TRIANGLE_BEGIN_LOOP
  287. {
  288. *(Uint32 *)dptr = color;
  289. }
  290. TRIANGLE_END_LOOP
  291. } else if (dstbpp == 3) {
  292. TRIANGLE_BEGIN_LOOP
  293. {
  294. Uint8 *s = (Uint8*)&color;
  295. dptr[0] = s[0];
  296. dptr[1] = s[1];
  297. dptr[2] = s[2];
  298. }
  299. TRIANGLE_END_LOOP
  300. } else if (dstbpp == 2) {
  301. TRIANGLE_BEGIN_LOOP
  302. {
  303. *(Uint16 *)dptr = color;
  304. }
  305. TRIANGLE_END_LOOP
  306. } else if (dstbpp == 1) {
  307. TRIANGLE_BEGIN_LOOP
  308. {
  309. *dptr = color;
  310. }
  311. TRIANGLE_END_LOOP
  312. }
  313. } else {
  314. SDL_PixelFormat *format = dst->format;
  315. if (tmp) {
  316. format = tmp->format;
  317. }
  318. if (dstbpp == 4) {
  319. TRIANGLE_BEGIN_LOOP
  320. {
  321. TRIANGLE_GET_MAPPED_COLOR
  322. *(Uint32 *)dptr = color;
  323. }
  324. TRIANGLE_END_LOOP
  325. } else if (dstbpp == 3) {
  326. TRIANGLE_BEGIN_LOOP
  327. {
  328. TRIANGLE_GET_MAPPED_COLOR
  329. Uint8 *s = (Uint8*)&color;
  330. dptr[0] = s[0];
  331. dptr[1] = s[1];
  332. dptr[2] = s[2];
  333. }
  334. TRIANGLE_END_LOOP
  335. } else if (dstbpp == 2) {
  336. TRIANGLE_BEGIN_LOOP
  337. {
  338. TRIANGLE_GET_MAPPED_COLOR
  339. *(Uint16 *)dptr = color;
  340. }
  341. TRIANGLE_END_LOOP
  342. } else if (dstbpp == 1) {
  343. TRIANGLE_BEGIN_LOOP
  344. {
  345. TRIANGLE_GET_MAPPED_COLOR
  346. *dptr = color;
  347. }
  348. TRIANGLE_END_LOOP
  349. }
  350. }
  351. if (tmp) {
  352. SDL_BlitSurface(tmp, NULL, dst, &dstrect);
  353. SDL_FreeSurface(tmp);
  354. }
  355. end:
  356. if (dst_locked) {
  357. SDL_UnlockSurface(dst);
  358. }
  359. return ret;
  360. }
  361. int SDL_SW_BlitTriangle(
  362. SDL_Surface *src,
  363. SDL_Point *s0, SDL_Point *s1, SDL_Point *s2,
  364. SDL_Surface *dst,
  365. SDL_Point *d0, SDL_Point *d1, SDL_Point *d2,
  366. SDL_Color c0, SDL_Color c1, SDL_Color c2)
  367. {
  368. int ret = 0;
  369. int src_locked = 0;
  370. int dst_locked = 0;
  371. SDL_BlendMode blend;
  372. SDL_Rect srcrect;
  373. SDL_Rect dstrect;
  374. SDL_Point s2_x_area;
  375. int dstbpp;
  376. Uint8 *dst_ptr;
  377. int dst_pitch;
  378. int *src_ptr;
  379. int src_pitch;
  380. int area, is_clockwise;
  381. int d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x;
  382. int s2s0_x, s2s1_x, s2s0_y, s2s1_y;
  383. int w0_row, w1_row, w2_row;
  384. int bias_w0, bias_w1, bias_w2;
  385. int is_uniform;
  386. int has_modulation;
  387. if (src == NULL || dst == NULL) {
  388. return -1;
  389. }
  390. area = cross_product(d0, d1, d2->x, d2->y);
  391. /* Flat triangle */
  392. if (area == 0) {
  393. return 0;
  394. }
  395. /* Lock the destination, if needed */
  396. if (SDL_MUSTLOCK(dst)) {
  397. if (SDL_LockSurface(dst) < 0) {
  398. ret = -1;
  399. goto end;
  400. } else {
  401. dst_locked = 1;
  402. }
  403. }
  404. /* Lock the source, if needed */
  405. if (SDL_MUSTLOCK(src)) {
  406. if (SDL_LockSurface(src) < 0) {
  407. ret = -1;
  408. goto end;
  409. } else {
  410. src_locked = 1;
  411. }
  412. }
  413. is_uniform = COLOR_EQ(c0, c1) && COLOR_EQ(c1, c2);
  414. bounding_rect(s0, s1, s2, &srcrect);
  415. bounding_rect(d0, d1, d2, &dstrect);
  416. SDL_GetSurfaceBlendMode(src, &blend);
  417. if (is_uniform) {
  418. // SDL_GetSurfaceColorMod(src, &r, &g, &b);
  419. has_modulation = c0.r != 255 || c0.g != 255 || c0.b != 255 || c0.a != 255;;
  420. } else {
  421. has_modulation = SDL_TRUE;
  422. }
  423. {
  424. /* Clip triangle rect with surface rect */
  425. SDL_Rect rect;
  426. rect.x = 0;
  427. rect.y = 0;
  428. rect.w = dst->w;
  429. rect.h = dst->h;
  430. SDL_IntersectRect(&dstrect, &rect, &dstrect);
  431. }
  432. {
  433. /* Clip triangle with surface clip rect */
  434. SDL_Rect rect;
  435. SDL_GetClipRect(dst, &rect);
  436. SDL_IntersectRect(&dstrect, &rect, &dstrect);
  437. }
  438. /* Set destination pointer */
  439. dstbpp = dst->format->BytesPerPixel;
  440. dst_ptr = (Uint8 *)dst->pixels + dstrect.x * dstbpp + dstrect.y * dst->pitch;
  441. dst_pitch = dst->pitch;
  442. /* Set source pointer */
  443. src_ptr = src->pixels;
  444. src_pitch = src->pitch;
  445. is_clockwise = area > 0;
  446. area = SDL_abs(area);
  447. d2d1_y = (d1->y - d2->y) << FP_BITS;
  448. d0d2_y = (d2->y - d0->y) << FP_BITS;
  449. d1d0_y = (d0->y - d1->y) << FP_BITS;
  450. d1d2_x = (d2->x - d1->x) << FP_BITS;
  451. d2d0_x = (d0->x - d2->x) << FP_BITS;
  452. d0d1_x = (d1->x - d0->x) << FP_BITS;
  453. s2s0_x = s0->x - s2->x;
  454. s2s1_x = s1->x - s2->x;
  455. s2s0_y = s0->y - s2->y;
  456. s2s1_y = s1->y - s2->y;
  457. /* Starting point for rendering, at the middle of a pixel */
  458. {
  459. SDL_Point p;
  460. p.x = dstrect.x;
  461. p.y = dstrect.y;
  462. trianglepoint_2_fixedpoint(&p);
  463. p.x += (1 << FP_BITS) / 2;
  464. p.y += (1 << FP_BITS) / 2;
  465. w0_row = cross_product(d1, d2, p.x, p.y);
  466. w1_row = cross_product(d2, d0, p.x, p.y);
  467. w2_row = cross_product(d0, d1, p.x, p.y);
  468. }
  469. /* Handle anti-clockwise triangles */
  470. if (! is_clockwise) {
  471. d2d1_y *= -1;
  472. d0d2_y *= -1;
  473. d1d0_y *= -1;
  474. d1d2_x *= -1;
  475. d2d0_x *= -1;
  476. d0d1_x *= -1;
  477. w0_row *= -1;
  478. w1_row *= -1;
  479. w2_row *= -1;
  480. }
  481. /* Add a bias to respect top-left rasterization rule */
  482. bias_w0 = (is_top_left(d1, d2, is_clockwise) ? 0 : -1);
  483. bias_w1 = (is_top_left(d2, d0, is_clockwise) ? 0 : -1);
  484. bias_w2 = (is_top_left(d0, d1, is_clockwise) ? 0 : -1);
  485. /* precompute constant 's2->x * area' used in TRIANGLE_GET_TEXTCOORD */
  486. s2_x_area.x = s2->x * area;
  487. s2_x_area.y = s2->y * area;
  488. if (blend != SDL_BLENDMODE_NONE || src->format->format != dst->format->format || has_modulation || ! is_uniform) {
  489. /* Use SDL_BlitTriangle_Slow */
  490. SDL_BlitInfo *info = &src->map->info;
  491. SDL_BlitInfo tmp_info;
  492. SDL_zero(tmp_info);
  493. tmp_info.src_fmt = src->format;
  494. tmp_info.dst_fmt = dst->format;
  495. tmp_info.flags = info->flags;
  496. /*
  497. tmp_info.r = info->r;
  498. tmp_info.g = info->g;
  499. tmp_info.b = info->b;
  500. tmp_info.a = info->a;
  501. */
  502. tmp_info.r = c0.r;
  503. tmp_info.g = c0.g;
  504. tmp_info.b = c0.b;
  505. tmp_info.a = c0.a;
  506. tmp_info.flags &= ~(SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA);
  507. if (c0.r != 255 || c1.r != 255 || c2.r != 255 ||
  508. c0.g != 255 || c1.g != 255 || c2.g != 255 ||
  509. c0.b != 255 || c1.b != 255 || c2.b != 255) {
  510. tmp_info.flags |= SDL_COPY_MODULATE_COLOR;
  511. }
  512. if (c0.a != 255 || c1.a != 255 || c2.a != 255) {
  513. tmp_info.flags |= SDL_COPY_MODULATE_ALPHA;
  514. }
  515. tmp_info.colorkey = info->colorkey;
  516. /* src */
  517. tmp_info.src = (Uint8 *) src_ptr;
  518. tmp_info.src_pitch = src_pitch;
  519. /* dst */
  520. tmp_info.dst = (Uint8 *) dst_ptr;
  521. tmp_info.dst_pitch = dst_pitch;
  522. SDL_BlitTriangle_Slow(&tmp_info, s2_x_area, dstrect, area, bias_w0, bias_w1, bias_w2,
  523. d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x,
  524. s2s0_x, s2s1_x, s2s0_y, s2s1_y, w0_row, w1_row, w2_row,
  525. c0, c1, c2, is_uniform);
  526. goto end;
  527. }
  528. if (dstbpp == 4) {
  529. TRIANGLE_BEGIN_LOOP
  530. {
  531. TRIANGLE_GET_TEXTCOORD
  532. Uint32 *sptr = (Uint32 *)((Uint8 *) src_ptr + srcy * src_pitch);
  533. *(Uint32 *)dptr = sptr[srcx];
  534. }
  535. TRIANGLE_END_LOOP
  536. } else if (dstbpp == 3) {
  537. TRIANGLE_BEGIN_LOOP
  538. {
  539. TRIANGLE_GET_TEXTCOORD
  540. Uint8 *sptr = (Uint8 *)((Uint8 *) src_ptr + srcy * src_pitch);
  541. dptr[0] = sptr[3 * srcx];
  542. dptr[1] = sptr[3 * srcx + 1];
  543. dptr[2] = sptr[3 * srcx + 2];
  544. }
  545. TRIANGLE_END_LOOP
  546. } else if (dstbpp == 2) {
  547. TRIANGLE_BEGIN_LOOP
  548. {
  549. TRIANGLE_GET_TEXTCOORD
  550. Uint16 *sptr = (Uint16 *)((Uint8 *) src_ptr + srcy * src_pitch);
  551. *(Uint16 *)dptr = sptr[srcx];
  552. }
  553. TRIANGLE_END_LOOP
  554. } else if (dstbpp == 1) {
  555. TRIANGLE_BEGIN_LOOP
  556. {
  557. TRIANGLE_GET_TEXTCOORD
  558. Uint8 *sptr = (Uint8 *)((Uint8 *) src_ptr + srcy * src_pitch);
  559. *dptr = sptr[srcx];
  560. }
  561. TRIANGLE_END_LOOP
  562. }
  563. end:
  564. if (dst_locked) {
  565. SDL_UnlockSurface(dst);
  566. }
  567. if (src_locked) {
  568. SDL_UnlockSurface(src);
  569. }
  570. return ret;
  571. }
  572. static void
  573. SDL_BlitTriangle_Slow(SDL_BlitInfo *info,
  574. SDL_Point s2_x_area, SDL_Rect dstrect, int area, int bias_w0, int bias_w1, int bias_w2,
  575. int d2d1_y, int d1d2_x, int d0d2_y, int d2d0_x, int d1d0_y, int d0d1_x,
  576. int s2s0_x, int s2s1_x, int s2s0_y, int s2s1_y, int w0_row, int w1_row, int w2_row,
  577. SDL_Color c0, SDL_Color c1, SDL_Color c2, int is_uniform)
  578. {
  579. const int flags = info->flags;
  580. Uint32 modulateR = info->r;
  581. Uint32 modulateG = info->g;
  582. Uint32 modulateB = info->b;
  583. Uint32 modulateA = info->a;
  584. Uint32 srcpixel;
  585. Uint32 srcR, srcG, srcB, srcA;
  586. Uint32 dstpixel;
  587. Uint32 dstR, dstG, dstB, dstA;
  588. SDL_PixelFormat *src_fmt = info->src_fmt;
  589. SDL_PixelFormat *dst_fmt = info->dst_fmt;
  590. int srcbpp = src_fmt->BytesPerPixel;
  591. int dstbpp = dst_fmt->BytesPerPixel;
  592. Uint32 rgbmask = ~src_fmt->Amask;
  593. Uint32 ckey = info->colorkey & rgbmask;
  594. Uint8 *dst_ptr = info->dst;
  595. int dst_pitch = info->dst_pitch;;
  596. TRIANGLE_BEGIN_LOOP
  597. {
  598. Uint8 *src;
  599. Uint8 *dst = dptr;
  600. TRIANGLE_GET_TEXTCOORD
  601. src = (info->src + (srcy * info->src_pitch) + (srcx * srcbpp));
  602. if (src_fmt->Amask) {
  603. DISEMBLE_RGBA(src, srcbpp, src_fmt, srcpixel, srcR, srcG,
  604. srcB, srcA);
  605. } else {
  606. DISEMBLE_RGB(src, srcbpp, src_fmt, srcpixel, srcR, srcG,
  607. srcB);
  608. srcA = 0xFF;
  609. }
  610. if (flags & SDL_COPY_COLORKEY) {
  611. /* srcpixel isn't set for 24 bpp */
  612. if (srcbpp == 3) {
  613. srcpixel = (srcR << src_fmt->Rshift) |
  614. (srcG << src_fmt->Gshift) | (srcB << src_fmt->Bshift);
  615. }
  616. if ((srcpixel & rgbmask) == ckey) {
  617. continue;
  618. }
  619. }
  620. if (dst_fmt->Amask) {
  621. DISEMBLE_RGBA(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG,
  622. dstB, dstA);
  623. } else {
  624. DISEMBLE_RGB(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG,
  625. dstB);
  626. dstA = 0xFF;
  627. }
  628. if (! is_uniform) {
  629. TRIANGLE_GET_COLOR
  630. modulateR = r;
  631. modulateG = g;
  632. modulateB = b;
  633. modulateA = a;
  634. }
  635. if (flags & SDL_COPY_MODULATE_COLOR) {
  636. srcR = (srcR * modulateR) / 255;
  637. srcG = (srcG * modulateG) / 255;
  638. srcB = (srcB * modulateB) / 255;
  639. }
  640. if (flags & SDL_COPY_MODULATE_ALPHA) {
  641. srcA = (srcA * modulateA) / 255;
  642. }
  643. if (flags & (SDL_COPY_BLEND | SDL_COPY_ADD)) {
  644. /* This goes away if we ever use premultiplied alpha */
  645. if (srcA < 255) {
  646. srcR = (srcR * srcA) / 255;
  647. srcG = (srcG * srcA) / 255;
  648. srcB = (srcB * srcA) / 255;
  649. }
  650. }
  651. switch (flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL)) {
  652. case 0:
  653. dstR = srcR;
  654. dstG = srcG;
  655. dstB = srcB;
  656. dstA = srcA;
  657. break;
  658. case SDL_COPY_BLEND:
  659. dstR = srcR + ((255 - srcA) * dstR) / 255;
  660. dstG = srcG + ((255 - srcA) * dstG) / 255;
  661. dstB = srcB + ((255 - srcA) * dstB) / 255;
  662. dstA = srcA + ((255 - srcA) * dstA) / 255;
  663. break;
  664. case SDL_COPY_ADD:
  665. dstR = srcR + dstR;
  666. if (dstR > 255)
  667. dstR = 255;
  668. dstG = srcG + dstG;
  669. if (dstG > 255)
  670. dstG = 255;
  671. dstB = srcB + dstB;
  672. if (dstB > 255)
  673. dstB = 255;
  674. break;
  675. case SDL_COPY_MOD:
  676. dstR = (srcR * dstR) / 255;
  677. dstG = (srcG * dstG) / 255;
  678. dstB = (srcB * dstB) / 255;
  679. break;
  680. case SDL_COPY_MUL:
  681. dstR = ((srcR * dstR) + (dstR * (255 - srcA))) / 255;
  682. if (dstR > 255)
  683. dstR = 255;
  684. dstG = ((srcG * dstG) + (dstG * (255 - srcA))) / 255;
  685. if (dstG > 255)
  686. dstG = 255;
  687. dstB = ((srcB * dstB) + (dstB * (255 - srcA))) / 255;
  688. if (dstB > 255)
  689. dstB = 255;
  690. dstA = ((srcA * dstA) + (dstA * (255 - srcA))) / 255;
  691. if (dstA > 255)
  692. dstA = 255;
  693. break;
  694. }
  695. if (dst_fmt->Amask) {
  696. ASSEMBLE_RGBA(dst, dstbpp, dst_fmt, dstR, dstG, dstB, dstA);
  697. } else {
  698. ASSEMBLE_RGB(dst, dstbpp, dst_fmt, dstR, dstG, dstB);
  699. }
  700. }
  701. TRIANGLE_END_LOOP
  702. }
  703. #endif /* SDL_VIDEO_RENDER_SW && !SDL_RENDER_DISABLED */
  704. /* vi: set ts=4 sw=4 expandtab: */