gba-x3dtest
changeset 20:2e903e27e35a tip
fixed x3d_disable_texture
added runtime teture checks in the rasterizer
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 01 Jul 2014 23:23:37 +0300 |
parents | 62390f9cc93e |
children | |
files | src/polyfill.c src/x3d.c test.png |
diffstat | 3 files changed, 57 insertions(+), 113 deletions(-) [+] |
line diff
1.1 --- a/src/polyfill.c Mon Jun 30 09:07:41 2014 +0300 1.2 +++ b/src/polyfill.c Tue Jul 01 23:23:37 2014 +0300 1.3 @@ -19,21 +19,6 @@ 1.4 int32_t v0, int32_t v1, uint16_t color, struct texture *tex); 1.5 static int winding(int32_t x0, int32_t y0, int32_t x1, int32_t y1); 1.6 1.7 -#ifdef USE_TEX 1.8 -static uint16_t tex_lookup(struct texture *tex, int tx, int ty); 1.9 - 1.10 -/* TODO currently the linkscript will put statics in iwram. make this explicit */ 1.11 -#define TCACHE_XSZ 16 1.12 -#define TCACHE_YSZ 16 1.13 - 1.14 -static struct { 1.15 - uint16_t pixels[TCACHE_XSZ * TCACHE_YSZ]; 1.16 - struct texture *tex; 1.17 - int orig_x, orig_y; 1.18 -} tcache; 1.19 -#endif /* USE_TEX */ 1.20 - 1.21 - 1.22 void draw_poly(int num, const pvec3 *verts, const pvec2 *texcoords, uint16_t color, 1.23 struct texture *tex) 1.24 { 1.25 @@ -94,10 +79,12 @@ 1.26 lx = verts[lidx[0]].x; 1.27 ldxdy = x16div(verts[lidx[1]].x - lx, ldy); 1.28 #ifdef USE_TEX 1.29 - lu = texcoords[lidx[0]].x; 1.30 - ldudy = x16div(texcoords[lidx[1]].x - lu, ldy); 1.31 - lv = texcoords[lidx[0]].y; 1.32 - ldvdy = x16div(texcoords[lidx[1]].y - lv, ldy); 1.33 + if(tex) { 1.34 + lu = texcoords[lidx[0]].x; 1.35 + ldudy = x16div(texcoords[lidx[1]].x - lu, ldy); 1.36 + lv = texcoords[lidx[0]].y; 1.37 + ldvdy = x16div(texcoords[lidx[1]].y - lv, ldy); 1.38 + } 1.39 #endif 1.40 1.41 /* find starting right edge */ 1.42 @@ -117,14 +104,16 @@ 1.43 rx = verts[ridx[0]].x; 1.44 rdxdy = x16div(verts[ridx[1]].x - rx, rdy); 1.45 #ifdef USE_TEX 1.46 - ru = texcoords[ridx[0]].x; 1.47 - rdudy = x16div(texcoords[ridx[1]].x - ru, rdy); 1.48 - rv = texcoords[ridx[0]].y; 1.49 - rdvdy = x16div(texcoords[ridx[1]].y - rv, rdy); 1.50 + if(tex) { 1.51 + ru = texcoords[ridx[0]].x; 1.52 + rdudy = x16div(texcoords[ridx[1]].x - ru, rdy); 1.53 + rv = texcoords[ridx[0]].y; 1.54 + rdvdy = x16div(texcoords[ridx[1]].y - rv, rdy); 1.55 + } 1.56 #endif 1.57 1.58 - start = topy >> 16; 1.59 - end = boty >> 16; 1.60 + start = (topy + 32768) >> 16; 1.61 + end = (boty + 32768) >> 16; 1.62 1.63 if(end >= HEIGHT) end = HEIGHT - 1; 1.64 1.65 @@ -147,14 +136,16 @@ 1.66 } 1.67 1.68 #ifdef USE_TEX 1.69 - lu = texcoords[lidx[0]].x; 1.70 - lv = texcoords[lidx[0]].y; 1.71 - if(ldy) { 1.72 - ldudy = x16div(texcoords[lidx[1]].x - lu, ldy); 1.73 - ldvdy = x16div(texcoords[lidx[1]].y - lv, ldy); 1.74 - } else { 1.75 - ldudy = texcoords[lidx[1]].x - lu; 1.76 - ldvdy = texcoords[lidx[1]].y - lv; 1.77 + if(tex) { 1.78 + lu = texcoords[lidx[0]].x; 1.79 + lv = texcoords[lidx[0]].y; 1.80 + if(ldy) { 1.81 + ldudy = x16div(texcoords[lidx[1]].x - lu, ldy); 1.82 + ldvdy = x16div(texcoords[lidx[1]].y - lv, ldy); 1.83 + } else { 1.84 + ldudy = texcoords[lidx[1]].x - lu; 1.85 + ldvdy = texcoords[lidx[1]].y - lv; 1.86 + } 1.87 } 1.88 #endif /* USE_TEX */ 1.89 } 1.90 @@ -174,14 +165,16 @@ 1.91 } 1.92 1.93 #ifdef USE_TEX 1.94 - ru = texcoords[ridx[0]].x; 1.95 - rv = texcoords[ridx[0]].y; 1.96 - if(rdy) { 1.97 - rdudy = x16div(texcoords[ridx[1]].x - ru, rdy); 1.98 - rdvdy = x16div(texcoords[ridx[1]].y - rv, rdy); 1.99 - } else { 1.100 - rdudy = texcoords[ridx[1]].x - ru; 1.101 - rdvdy = texcoords[ridx[1]].y - rv; 1.102 + if(tex) { 1.103 + ru = texcoords[ridx[0]].x; 1.104 + rv = texcoords[ridx[0]].y; 1.105 + if(rdy) { 1.106 + rdudy = x16div(texcoords[ridx[1]].x - ru, rdy); 1.107 + rdvdy = x16div(texcoords[ridx[1]].y - rv, rdy); 1.108 + } else { 1.109 + rdudy = texcoords[ridx[1]].x - ru; 1.110 + rdvdy = texcoords[ridx[1]].y - rv; 1.111 + } 1.112 } 1.113 #endif /* USE_TEX */ 1.114 } 1.115 @@ -199,10 +192,12 @@ 1.116 y += 65536; 1.117 1.118 #ifdef USE_TEX 1.119 - lu += ldudy; 1.120 - lv += ldvdy; 1.121 - ru += rdudy; 1.122 - rv += rdvdy; 1.123 + if(tex) { 1.124 + lu += ldudy; 1.125 + lv += ldvdy; 1.126 + ru += rdudy; 1.127 + rv += rdvdy; 1.128 + } 1.129 #endif 1.130 } 1.131 } 1.132 @@ -293,16 +288,18 @@ 1.133 } 1.134 1.135 #ifdef USE_TEX 1.136 - dx = x1 - x0; 1.137 + if(tex) { 1.138 + dx = x1 - x0; 1.139 1.140 - u = u0; 1.141 - v = v0; 1.142 - if(dx) { 1.143 - dudx = x16div(u1 - u0, dx); 1.144 - dvdx = x16div(v1 - v0, dx); 1.145 - } else { 1.146 - dudx = u1 - u0; 1.147 - dvdx = v1 - v0; 1.148 + u = u0; 1.149 + v = v0; 1.150 + if(dx) { 1.151 + dudx = x16div(u1 - u0, dx); 1.152 + dvdx = x16div(v1 - v0, dx); 1.153 + } else { 1.154 + dudx = u1 - u0; 1.155 + dvdx = v1 - v0; 1.156 + } 1.157 } 1.158 #endif 1.159 1.160 @@ -318,8 +315,7 @@ 1.161 for(i=ix0; i<ix1; i++) { 1.162 int tx = (u >> (16 - tex->ushift)) & tex->umask; 1.163 int ty = (v >> (16 - tex->vshift)) & tex->vmask; 1.164 - *pixels++ = tex_lookup(tex, tx, ty); 1.165 - //*pixels++ = ((uint16_t*)tex->pixels)[ty * tex->xsz + yx]; 1.166 + *pixels++ = ((uint16_t*)tex->pixels)[ty * tex->xsz + tx]; 1.167 1.168 u += dudx; 1.169 v += dvdx; 1.170 @@ -336,8 +332,8 @@ 1.171 1.172 void draw_point(const pvec3 *v, uint16_t color) 1.173 { 1.174 - int x = v->x >> 16; 1.175 - int y = v->y >> 16; 1.176 + int x = (v->x + 32768) >> 16; 1.177 + int y = (v->y + 32768) >> 16; 1.178 uint16_t *pixels = (uint16_t*)back_buffer->pixels; 1.179 1.180 if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) { 1.181 @@ -360,53 +356,3 @@ 1.182 { 1.183 return x16mul(x0, y1) - x16mul(y0, x1); 1.184 } 1.185 - 1.186 -#ifdef USE_TEX 1.187 -#define MIN(a, b) ((a) < (b) ? (a) : (b)) 1.188 -static uint16_t tex_lookup(struct texture *tex, int tx, int ty) 1.189 -{ 1.190 - int x, y, i, j; 1.191 - int width, height; 1.192 - uint16_t *ptr; 1.193 - int cx = tx - tcache.orig_x; 1.194 - int cy = ty - tcache.orig_y; 1.195 - 1.196 - if(tcache.tex == tex && cx >= 0 && cx < TCACHE_XSZ && cy >= 0 && cy < TCACHE_YSZ) { 1.197 - return tcache.pixels[cy * TCACHE_XSZ + cx]; 1.198 - } 1.199 - 1.200 - tcache.tex = tex; 1.201 - x = tx - TCACHE_XSZ / 2; 1.202 - y = ty - TCACHE_YSZ / 2; 1.203 - 1.204 - if(x + TCACHE_XSZ > tex->xsz) { 1.205 - x = tex->xsz - TCACHE_XSZ; 1.206 - } 1.207 - if(y + TCACHE_YSZ > tex->ysz) { 1.208 - y = tex->ysz - TCACHE_YSZ; 1.209 - } 1.210 - 1.211 - if(x < 0) x = 0; 1.212 - if(y < 0) y = 0; 1.213 - 1.214 - width = MIN(TCACHE_XSZ, tex->xsz - x); 1.215 - height = MIN(TCACHE_YSZ, tex->ysz - y); 1.216 - 1.217 - tcache.orig_x = x; 1.218 - tcache.orig_y = y; 1.219 - 1.220 - /*logmsg(LOG_DBG, "lookup(%d, %d): loading %dx%d+%d+%d into cache %p\n", tx, ty, 1.221 - width, height, x, y, tcache.pixels); 1.222 - */ 1.223 - 1.224 - ptr = (uint16_t*)tex->pixels + y * tex->xsz + x; 1.225 - for(i=0; i<height; i++) { 1.226 - memcpy(tcache.pixels + i * TCACHE_XSZ, ptr, width * 2); 1.227 - ptr += tex->xsz; 1.228 - } 1.229 - 1.230 - cx = tx - tcache.orig_x; 1.231 - cy = ty - tcache.orig_y; 1.232 - return tcache.pixels[cy * TCACHE_XSZ + cx]; 1.233 -} 1.234 -#endif /* USE_TEX */
2.1 --- a/src/x3d.c Mon Jun 30 09:07:41 2014 +0300 2.2 +++ b/src/x3d.c Tue Jul 01 23:23:37 2014 +0300 2.3 @@ -352,9 +352,7 @@ 2.4 2.5 for(i=0; i<MAX_TEXTURES; i++) { 2.6 if(!textures[i].pixels) { 2.7 - /*textures[i].pixels = pixels;*/ 2.8 - textures[i].pixels = malloc(xsz * ysz * 2); 2.9 - memcpy(textures[i].pixels, pixels, xsz * ysz * 2); 2.10 + textures[i].pixels = pixels; 2.11 textures[i].xsz = xsz; 2.12 textures[i].ysz = ysz; 2.13 textures[i].umask = xsz - 1; 2.14 @@ -369,7 +367,7 @@ 2.15 } 2.16 } 2.17 2.18 - logmsg(LOG_DBG, "create texture %dx%d: %p\n", xsz, ysz, pixels); 2.19 + logmsg(LOG_DBG, "create texture %dx%d: %p\n", xsz, ysz, textures[i].pixels); 2.20 2.21 return i; 2.22 } 2.23 @@ -384,7 +382,7 @@ 2.24 2.25 void x3d_disable_texture(void) 2.26 { 2.27 - cur_tex = 0; 2.28 + cur_tex = -1; 2.29 } 2.30 2.31 int x3d_get_active_texture(void)
3.1 Binary file test.png has changed