fixed 8bit writes to vmem, unaligned data buffers, and started on
[gba_blender] / src / polyfill.c
1 /*
2 blender for the Gameboy Advance
3 Copyright (C) 2021  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <string.h>
19 #include "polyfill.h"
20 #include "debug.h"
21
22 static unsigned char *fb;
23 static int fbwidth, fbheight;
24 static short scantab[2][160] __attribute__((section(".iwram")));
25
26 void polyfill_framebuffer(unsigned char *ptr, int w, int h)
27 {
28         fb = ptr;
29         fbwidth = w;
30         fbheight = h;
31 }
32
33 #define VNEXT(p)        (((p) == vlast) ? varr : (p) + 1)
34 #define VPREV(p)        ((p) == varr ? vlast : (p) - 1)
35 #define VSUCC(p, side)  ((side) == 0 ? VNEXT(p) : VPREV(p))
36
37 void polyfill_flat(struct pvertex *varr, int vnum, unsigned char col)
38 {
39         int i, line, top, bot;
40         struct pvertex *vlast, *v, *vn;
41         int32_t x, y0, y1, dx, dy, slope, fx, fy;
42         short *tab, start, len;
43         unsigned char *fbptr;
44         uint16_t *pptr, pcol = ((uint16_t)col << 8) | (uint16_t)col;
45
46         vlast = varr + vnum - 1;
47         top = fbheight;
48         bot = 0;
49
50         for(i=0; i<vnum; i++) {
51                 v = varr + i;
52                 vn = VNEXT(v);
53
54                 if(vn->y == v->y) continue;
55
56                 if(vn->y > v->y) {
57                         tab = scantab[0];
58                 } else {
59                         tab = scantab[1];
60                         v = vn;
61                         vn = varr + i;
62                 }
63
64                 dx = vn->x - v->x;
65                 dy = vn->y - v->y;
66                 slope = (dx << 8) / dy;
67
68                 y0 = (v->y + 0x100) & 0xffffff00;       /* start from the next scanline */
69                 fy = y0 - v->y;                                         /* fractional part before the next scanline */
70                 fx = (fy * slope) >> 8;                         /* X adjust for the step to the next scanline */
71                 x = v->x + fx;                                          /* adjust X */
72                 y1 = vn->y & 0xffffff00;                        /* last scanline of the edge <= vn->y */
73
74                 line = y0 >> 8;
75                 if(line < top) top = line;
76                 if((y1 >> 8) > bot) bot = y1 >> 8;
77
78                 if(line > 0) tab += line;
79
80                 while(line <= (y1 >> 8) && line < fbheight) {
81                         if(line >= 0) {
82                                 int val = x < 0 ? 0 : x >> 8;
83                                 *tab++ = val < fbwidth ? val : fbwidth - 1;
84                         }
85                         x += slope;
86                         line++;
87                 }
88         }
89
90         fbptr = fb + top * fbwidth;
91         for(i=top; i<=bot; i++) {
92                 start = scantab[0][i];
93                 len = scantab[1][i] - start;
94
95                 if(len > 0) {
96                         if(start & 1) {
97                                 pptr = (uint16_t*)(fbptr + (start & 0xfffe));
98                                 *pptr = (*pptr & 0xff) | ((uint16_t)col << 8);
99                                 len--;
100                                 start++;
101                         }
102                         pptr = (uint16_t*)(fbptr + start);
103                         while(len > 1) {
104                                 *pptr++ = pcol;
105                                 len -= 2;
106                         }
107                         if(len) {
108                                 *pptr = (*pptr & 0xff00) | col;
109                         }
110                 }
111                 fbptr += fbwidth;
112         }
113 }
114
115
116 /* ----- line drawing and clipping ------ */
117 enum {
118         IN              = 0,
119         LEFT    = 1,
120         RIGHT   = 2,
121         TOP             = 4,
122         BOTTOM  = 8
123 };
124
125 static int outcode(int x, int y, int xmin, int ymin, int xmax, int ymax)
126 {
127         int code = 0;
128
129         if(x < xmin) {
130                 code |= LEFT;
131         } else if(x > xmax) {
132                 code |= RIGHT;
133         }
134         if(y < ymin) {
135                 code |= TOP;
136         } else if(y > ymax) {
137                 code |= BOTTOM;
138         }
139         return code;
140 }
141
142 #define FIXMUL(a, b)    (((a) * (b)) >> 8)
143 #define FIXDIV(a, b)    (((a) << 8) / (b))
144
145 #define LERP(a, b, t)   ((a) + FIXMUL((b) - (a), (t)))
146
147 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax)
148 {
149         int oc_out;
150
151         int oc0 = outcode(*x0, *y0, xmin, ymin, xmax, ymax);
152         int oc1 = outcode(*x1, *y1, xmin, ymin, xmax, ymax);
153
154         long fx0, fy0, fx1, fy1, fxmin, fymin, fxmax, fymax;
155
156         if(!(oc0 | oc1)) return 1;      /* both points are inside */
157
158         fx0 = *x0 << 8;
159         fy0 = *y0 << 8;
160         fx1 = *x1 << 8;
161         fy1 = *y1 << 8;
162         fxmin = xmin << 8;
163         fymin = ymin << 8;
164         fxmax = xmax << 8;
165         fymax = ymax << 8;
166
167         for(;;) {
168                 long x, y, t;
169
170                 if(oc0 & oc1) return 0;         /* both have points with the same outbit, not visible */
171                 if(!(oc0 | oc1)) break;         /* both points are inside */
172
173                 oc_out = oc0 ? oc0 : oc1;
174
175                 if(oc_out & TOP) {
176                         t = FIXDIV(fymin - fy0, fy1 - fy0);
177                         x = LERP(fx0, fx1, t);
178                         y = fymin;
179                 } else if(oc_out & BOTTOM) {
180                         t = FIXDIV(fymax - fy0, fy1 - fy0);
181                         x = LERP(fx0, fx1, t);
182                         y = fymax;
183                 } else if(oc_out & LEFT) {
184                         t = FIXDIV(fxmin - fx0, fx1 - fx0);
185                         x = fxmin;
186                         y = LERP(fy0, fy1, t);
187                 } else {/*if(oc_out & RIGHT) {*/
188                         t = FIXDIV(fxmax - fx0, fx1 - fx0);
189                         x = fxmax;
190                         y = LERP(fy0, fy1, t);
191                 }
192
193                 if(oc_out == oc0) {
194                         fx0 = x;
195                         fy0 = y;
196                         oc0 = outcode(fx0 >> 8, fy0 >> 8, xmin, ymin, xmax, ymax);
197                 } else {
198                         fx1 = x;
199                         fy1 = y;
200                         oc1 = outcode(fx1 >> 8, fy1 >> 8, xmin, ymin, xmax, ymax);
201                 }
202         }
203
204         *x0 = fx0 >> 8;
205         *y0 = fy0 >> 8;
206         *x1 = fx1 >> 8;
207         *y1 = fy1 >> 8;
208         return 1;
209 }
210
211 #define PUTPIXEL(ptr) \
212         do { \
213                 uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
214                 if((uint32_t)ptr & 1) { \
215                         *pptr = (*pptr & 0xff) | (color << 8); \
216                 } else { \
217                         *pptr = (*pptr & 0xff00) | color; \
218                 } \
219         } while(0)
220
221 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
222 {
223         int i, dx, dy, x_inc, y_inc, error;
224         unsigned char *fbptr = fb;
225
226         fbptr += y0 * fbwidth + x0;
227
228         dx = x1 - x0;
229         dy = y1 - y0;
230
231         if(dx >= 0) {
232                 x_inc = 1;
233         } else {
234                 x_inc = -1;
235                 dx = -dx;
236         }
237         if(dy >= 0) {
238                 y_inc = fbwidth;
239         } else {
240                 y_inc = -fbwidth;
241                 dy = -dy;
242         }
243
244         if(dx > dy) {
245                 error = dy * 2 - dx;
246                 for(i=0; i<=dx; i++) {
247                         PUTPIXEL(fbptr);
248                         if(error >= 0) {
249                                 error -= dx * 2;
250                                 fbptr += y_inc;
251                         }
252                         error += dy * 2;
253                         fbptr += x_inc;
254                 }
255         } else {
256                 error = dx * 2 - dy;
257                 for(i=0; i<=dy; i++) {
258                         PUTPIXEL(fbptr);
259                         if(error >= 0) {
260                                 error -= dy * 2;
261                                 fbptr += x_inc;
262                         }
263                         error += dx * 2;
264                         fbptr += y_inc;
265                 }
266         }
267 }
268