gba-trycatch

annotate src/polyfill.c @ 15:b755fb002f17

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 25 Jun 2014 17:02:48 +0300
parents c398d834d64a
children
rev   line source
nuclear@5 1 #include "config.h"
nuclear@5 2 #include <string.h>
nuclear@6 3 #include <assert.h>
nuclear@5 4 #include "polyfill.h"
nuclear@5 5 #include "fixed.h"
nuclear@5 6 #include "gbasys.h"
nuclear@5 7
nuclear@15 8 /* TODO: constant interpolant optimization */
nuclear@15 9
nuclear@9 10 #define VNEXT(x, n) (((x) + 1) % (n))
nuclear@9 11 #define VPREV(x, n) ((x) > 0 ? (x) - 1 : (n) - 1)
nuclear@9 12
nuclear@15 13 static void fill_scanline_pal(int y, int32_t x0, int32_t x1, int32_t u0, int32_t u1,
nuclear@15 14 int32_t v0, int32_t v1, uint8_t color);
nuclear@15 15 static void fill_scanline_rgb(int y, int32_t x0, int32_t x1, int32_t u0, int32_t u1,
nuclear@15 16 int32_t v0, int32_t v1, uint16_t color);
nuclear@14 17 static int winding(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
nuclear@5 18
nuclear@15 19 void draw_poly(int num, const pvec3 *verts, const pvec2 *tex, uint16_t color)
nuclear@5 20 {
nuclear@5 21 int i, topidx = 0, botidx = 0;
nuclear@9 22 int lidx[2] = {-1, -1}, ridx[2] = {-1, -1};
nuclear@5 23 int32_t y, topy, boty;
nuclear@9 24 int32_t ldy = 0, rdy = 0, ldxdy, rdxdy;
nuclear@5 25 int32_t lx, rx;
nuclear@5 26 int start, end;
nuclear@14 27 pvec3 v0, v1;
nuclear@14 28
nuclear@15 29 int32_t lu, lv, ru, rv, ldudy, ldvdy, rdudy, rdvdy; /* texture interpolants */
nuclear@15 30
nuclear@14 31 v0.x = verts[1].x - verts[0].x;
nuclear@14 32 v0.y = verts[1].y - verts[0].y;
nuclear@14 33
nuclear@14 34 v1.x = verts[2].x - verts[0].x;
nuclear@14 35 v1.y = verts[2].y - verts[0].y;
nuclear@14 36
nuclear@14 37 if(winding(v0.x, v0.y, v1.x, v1.y) < 0) {
nuclear@14 38 return; /* backface */
nuclear@14 39 }
nuclear@5 40
nuclear@5 41 topy = boty = verts[0].y;
nuclear@5 42 for(i=1; i<num; i++) {
nuclear@5 43 int32_t y = verts[i].y;
nuclear@5 44 if(y < topy) {
nuclear@5 45 topy = y;
nuclear@5 46 topidx = i;
nuclear@5 47 }
nuclear@5 48 if(y > boty) {
nuclear@5 49 boty = y;
nuclear@5 50 botidx = i;
nuclear@5 51 }
nuclear@5 52 }
nuclear@5 53
nuclear@5 54 lidx[0] = ridx[0] = topidx;
nuclear@9 55
nuclear@9 56 /* find starting left edge */
nuclear@9 57 lidx[1] = VPREV(lidx[0], num);
nuclear@9 58 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
nuclear@9 59
nuclear@9 60 while(ldy == 0) {
nuclear@9 61 lidx[0] = lidx[1];
nuclear@9 62 lidx[1] = VPREV(lidx[1], num);
nuclear@9 63
nuclear@9 64 if(lidx[1] == topidx) {
nuclear@9 65 return; /* degenerate */
nuclear@9 66 }
nuclear@9 67
nuclear@9 68 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
nuclear@9 69 }
nuclear@9 70 lx = verts[lidx[0]].x;
nuclear@9 71 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
nuclear@15 72 lu = tex[lidx[0]].x;
nuclear@15 73 ldudy = x16div(tex[lidx[1]].x - lu, ldy);
nuclear@15 74 lv = tex[lidx[0]].y;
nuclear@15 75 ldvdy = x16div(tex[lidx[1]].y - lv, ldy);
nuclear@9 76
nuclear@9 77 /* find starting right edge */
nuclear@9 78 ridx[1] = VNEXT(ridx[0], num);
nuclear@9 79 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
nuclear@9 80
nuclear@9 81 while(rdy == 0) {
nuclear@9 82 ridx[0] = ridx[1];
nuclear@9 83 ridx[1] = VNEXT(ridx[1], num);
nuclear@9 84
nuclear@9 85 if(ridx[1] == topidx) {
nuclear@9 86 return; /* degenerate */
nuclear@9 87 }
nuclear@9 88
nuclear@9 89 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
nuclear@9 90 }
nuclear@9 91 rx = verts[ridx[0]].x;
nuclear@9 92 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
nuclear@15 93 ru = tex[ridx[0]].x;
nuclear@15 94 rdudy = x16div(tex[ridx[1]].x - ru, rdy);
nuclear@15 95 rv = tex[ridx[0]].y;
nuclear@15 96 rdvdy = x16div(tex[ridx[1]].y - rv, rdy);
nuclear@9 97
nuclear@5 98 start = topy >> 16;
nuclear@5 99 end = boty >> 16;
nuclear@5 100
nuclear@6 101 if(end >= HEIGHT) end = HEIGHT - 1;
nuclear@6 102
nuclear@5 103 y = topy;
nuclear@14 104 for(i=start; i<=end; i++) {
nuclear@5 105
nuclear@5 106 if(y >= verts[lidx[1]].y) {
nuclear@9 107 lx = verts[lidx[1]].x;
nuclear@5 108 lidx[0] = lidx[1];
nuclear@9 109 lidx[1] = VPREV(lidx[1], num);
nuclear@5 110 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
nuclear@5 111 if(ldy < 0) {
nuclear@5 112 break;
nuclear@5 113 }
nuclear@15 114
nuclear@15 115 lu = tex[lidx[0]].x;
nuclear@15 116 lv = tex[lidx[0]].y;
nuclear@15 117
nuclear@14 118 if(ldy) {
nuclear@14 119 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
nuclear@15 120 ldudy = x16div(tex[lidx[1]].x - lu, ldy);
nuclear@15 121 ldvdy = x16div(tex[lidx[1]].y - lv, ldy);
nuclear@14 122 } else {
nuclear@14 123 ldxdy = verts[lidx[1]].x - lx;
nuclear@15 124 ldudy = tex[lidx[1]].x - lu;
nuclear@15 125 ldvdy = tex[lidx[1]].y - lv;
nuclear@14 126 }
nuclear@5 127 }
nuclear@5 128 if(y >= verts[ridx[1]].y) {
nuclear@9 129 rx = verts[ridx[1]].x;
nuclear@5 130 ridx[0] = ridx[1];
nuclear@9 131 ridx[1] = VNEXT(ridx[1], num);
nuclear@5 132 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
nuclear@5 133 if(rdy < 0) {
nuclear@5 134 break;
nuclear@5 135 }
nuclear@15 136
nuclear@15 137 ru = tex[ridx[0]].x;
nuclear@15 138 rv = tex[ridx[0]].y;
nuclear@15 139
nuclear@14 140 if(rdy) {
nuclear@14 141 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
nuclear@15 142 rdudy = x16div(tex[ridx[1]].x - ru, rdy);
nuclear@15 143 rdvdy = x16div(tex[ridx[1]].y - rv, rdy);
nuclear@14 144 } else {
nuclear@14 145 rdxdy = verts[ridx[1]].x - rx;
nuclear@15 146 rdudy = tex[ridx[1]].x - ru;
nuclear@15 147 rdvdy = tex[ridx[1]].y - rv;
nuclear@14 148 }
nuclear@5 149 }
nuclear@5 150
nuclear@15 151 if(i >= 0) {
nuclear@9 152 #ifdef PALMODE
nuclear@15 153 fill_scanline_pal(i, lx, rx, lu, ru, lv, rv, (uint8_t)color);
nuclear@9 154 #else
nuclear@15 155 fill_scanline_rgb(i, lx, rx, lu, ru, lv, rv, color);
nuclear@9 156 #endif
nuclear@9 157 }
nuclear@9 158
nuclear@5 159 lx += ldxdy;
nuclear@5 160 rx += rdxdy;
nuclear@6 161 y += 65536;
nuclear@15 162
nuclear@15 163 lu += ldudy;
nuclear@15 164 lv += ldvdy;
nuclear@15 165 ru += rdudy;
nuclear@15 166 rv += rdvdy;
nuclear@5 167 }
nuclear@5 168 }
nuclear@5 169
nuclear@9 170
nuclear@15 171 static void fill_scanline_pal(int y, int32_t x0, int32_t x1, int32_t u0, int32_t u1,
nuclear@15 172 int32_t v0, int32_t v1, uint8_t color)
nuclear@9 173 {
nuclear@15 174 int ix0, ix1;
nuclear@15 175 int32_t dx;
nuclear@15 176 int32_t u, v, dudx, dvdx;
nuclear@15 177
nuclear@15 178 int i, num_pairs, num_pix;
nuclear@15 179 uint16_t *pixels;
nuclear@9 180 uint16_t colpair = (uint16_t)color | ((uint16_t)color << 8);
nuclear@9 181
nuclear@15 182 if(x0 > x1) {
nuclear@15 183 int32_t tmp = x0;
nuclear@15 184 x0 = x1;
nuclear@15 185 x1 = tmp;
nuclear@15 186 }
nuclear@15 187
nuclear@15 188 dx = x1 - x0;
nuclear@15 189
nuclear@15 190 u = u0;
nuclear@15 191 v = v0;
nuclear@15 192 if(dx) {
nuclear@15 193 dudx = x16div(u1 - u0, dx);
nuclear@15 194 dvdx = x16div(v1 - v0, dx);
nuclear@15 195 } else {
nuclear@15 196 dudx = u1 - u0;
nuclear@15 197 dvdx = v1 - v0;
nuclear@15 198 }
nuclear@15 199
nuclear@15 200 ix0 = (x0 + 32768) >> 16;
nuclear@15 201 ix1 = (x1 + 32768) >> 16;
nuclear@15 202
nuclear@15 203 if(ix0 < 0) ix0 = 0;
nuclear@15 204 if(ix1 >= WIDTH - 1) ix1 = WIDTH - 1;
nuclear@15 205
nuclear@15 206 num_pix = ix1 - ix0;
nuclear@15 207 pixels = (uint16_t*)back_buffer->pixels + (y * WIDTH + ix0) / 2;
nuclear@15 208
nuclear@15 209 if(ix0 & 1) {
nuclear@9 210 uint16_t pix = *pixels & 0xff;
nuclear@9 211 *pixels++ = pix | ((uint16_t)color << 8);
nuclear@9 212 --num_pix;
nuclear@15 213 u += dudx;
nuclear@15 214 v += dvdx;
nuclear@9 215 }
nuclear@9 216
nuclear@9 217 num_pairs = (num_pix & 0xfffe) / 2;
nuclear@9 218
nuclear@9 219 for(i=0; i<num_pairs; i++) {
nuclear@9 220 *pixels++ = colpair;
nuclear@15 221 u += dudx * 2;
nuclear@15 222 v += dvdx * 2;
nuclear@9 223 }
nuclear@9 224
nuclear@9 225 if(num_pix & 1) {
nuclear@9 226 uint16_t pix = *pixels & 0xff00;
nuclear@9 227 *pixels = pix | color;
nuclear@9 228 }
nuclear@9 229 }
nuclear@9 230
nuclear@15 231 static void fill_scanline_rgb(int y, int32_t x0, int32_t x1, int32_t u0, int32_t u1,
nuclear@15 232 int32_t v0, int32_t v1, uint16_t color)
nuclear@5 233 {
nuclear@15 234 int i, ix0, ix1;
nuclear@14 235 uint16_t *pixels;
nuclear@15 236 int32_t dx;
nuclear@15 237 int32_t u, v, dudx, dvdx;
nuclear@5 238
nuclear@14 239 if(x0 > x1) {
nuclear@15 240 int32_t tmp = x0;
nuclear@14 241 x0 = x1;
nuclear@15 242 x1 = tmp;
nuclear@14 243 }
nuclear@14 244
nuclear@15 245 dx = x1 - x0;
nuclear@14 246
nuclear@15 247 u = u0;
nuclear@15 248 v = v0;
nuclear@15 249 if(dx) {
nuclear@15 250 dudx = x16div(u1 - u0, dx);
nuclear@15 251 dvdx = x16div(v1 - v0, dx);
nuclear@15 252 } else {
nuclear@15 253 dudx = u1 - u0;
nuclear@15 254 dvdx = v1 - v0;
nuclear@15 255 }
nuclear@15 256
nuclear@15 257 ix0 = (x0 + 32768) >> 16;
nuclear@15 258 ix1 = (x1 + 32768) >> 16;
nuclear@15 259
nuclear@15 260 if(ix0 < 0) ix0 = 0;
nuclear@15 261 if(ix1 >= WIDTH - 1) ix1 = WIDTH - 1;
nuclear@15 262
nuclear@15 263 pixels = (uint16_t*)back_buffer->pixels + y * WIDTH + ix0;
nuclear@15 264 for(i=ix0; i<ix1; i++) {
nuclear@15 265 /**pixels++ = color;*/
nuclear@15 266 int cr = u >> 8;
nuclear@15 267 int cg = v >> 8;
nuclear@15 268 if(cr > 255) cr = 255;
nuclear@15 269 if(cg > 255) cg = 255;
nuclear@15 270
nuclear@15 271 *pixels++ = RGB(cr, cg, 0);
nuclear@15 272
nuclear@15 273 u += dudx;
nuclear@15 274 v += dvdx;
nuclear@5 275 }
nuclear@5 276 }
nuclear@8 277
nuclear@8 278
nuclear@8 279 void draw_point(const pvec3 *v, uint16_t color)
nuclear@8 280 {
nuclear@8 281 int x = v->x >> 16;
nuclear@8 282 int y = v->y >> 16;
nuclear@8 283 uint16_t *pixels = (uint16_t*)back_buffer->pixels;
nuclear@8 284
nuclear@8 285 if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
nuclear@8 286 return;
nuclear@8 287 }
nuclear@8 288
nuclear@9 289 #ifdef PALMODE
nuclear@9 290 pixels += (y * WIDTH + x) / 2;
nuclear@9 291 if(x & 1) {
nuclear@9 292 *pixels = (*pixels & 0xff) | (color << 8);
nuclear@9 293 } else {
nuclear@9 294 *pixels = (*pixels & 0xff00) | color;
nuclear@9 295 }
nuclear@9 296 #else
nuclear@8 297 pixels[y * WIDTH + x] = color;
nuclear@9 298 #endif
nuclear@8 299 }
nuclear@14 300
nuclear@14 301 static int winding(int32_t x0, int32_t y0, int32_t x1, int32_t y1)
nuclear@14 302 {
nuclear@14 303 return x16mul(x0, y1) - x16mul(y0, x1);
nuclear@14 304 }