optimizations
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 18 Oct 2022 01:05:41 +0000 (04:05 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 18 Oct 2022 01:05:41 +0000 (04:05 +0300)
Makefile
src/debug.h
src/gamescr.c
src/pc/main.c
src/util.c
src/util.h
src/voxscape.c

index 025fa8e..b5de0c7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,7 @@ OBJCOPY = $(TCPREFIX)objcopy
 OBJDUMP = $(TCPREFIX)objdump
 
 def = -DBUILD_GBA
-opt = -O0 -fomit-frame-pointer -mcpu=arm7tdmi -mtune=arm7tdmi -mthumb -mthumb-interwork
+opt = -O3 -fomit-frame-pointer -mcpu=arm7tdmi -mtune=arm7tdmi -mthumb -mthumb-interwork
 dbg = -g
 inc = -I. -Isrc -Isrc/gba -Ilibs/maxmod
 warn = -pedantic -Wall
index 921fd12..80a7826 100644 (file)
@@ -12,7 +12,7 @@ volatile int vblperf_count;
 
 void vblperf_setcolor(int palidx);
 
-#ifdef VBLBAR
+#if defined(BUILD_GBA) && defined(VBLBAR)
 #define vblperf_begin()        \
        do { \
                *vblperf_palptr = 0; \
index 4f9f754..0e5c6f8 100644 (file)
@@ -52,14 +52,14 @@ static int gamescr_start(void)
 
        gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
 
-       vblperf_setcolor(1);
+       vblperf_setcolor(0);
 
        pos[0] = pos[1] = 256 << 16;
 
        if(!(vox = vox_create(512, 512, height_pixels, color_pixels))) {
                panic(get_pc(), "vox_create");
        }
-       vox_proj(vox, 45, 1, 250);
+       vox_proj(vox, 45, 10, 100);
 
        /* setup color image palette */
        for(i=0; i<192; i++) {
@@ -79,8 +79,6 @@ static int gamescr_start(void)
                gba_bgpal[cidx] = ((b << 7) & 0x7c00) | ((g << 2) & 0x3e0) | (r >> 3);
        }
 
-       /*select_input(BN_DPAD | BN_LT | BN_RT);*/
-
        nframes = 0;
        return 0;
 }
@@ -101,10 +99,10 @@ static void gamescr_frame(void)
        update();
        draw();
 
-       //vblperf_end();
+       vblperf_end();
        wait_vblank();
        present(backbuf);
-       //vblperf_begin();
+       vblperf_begin();
 }
 
 #define WALK_SPEED     0x40000
@@ -115,41 +113,42 @@ static void update(void)
        int32_t fwd[2], right[2];
        uint16_t input;
 
-       input = read_input();
-
-       if(input & BN_LT) angle += TURN_SPEED;
-       if(input & BN_RT) angle -= TURN_SPEED;
-
-       fwd[0] = -SIN(angle);
-       fwd[1] = COS(angle);
-       right[0] = fwd[1];
-       right[1] = -fwd[0];
-
-       if(input & BN_UP) {
-               pos[0] += fwd[0];
-               pos[1] += fwd[1];
+       if((input = read_input())) {
+
+               if(input & BN_LT) angle += TURN_SPEED;
+               if(input & BN_RT) angle -= TURN_SPEED;
+
+               fwd[0] = -SIN(angle);
+               fwd[1] = COS(angle);
+               right[0] = fwd[1];
+               right[1] = -fwd[0];
+
+               if(input & BN_UP) {
+                       pos[0] += fwd[0];
+                       pos[1] += fwd[1];
+               }
+               if(input & BN_DOWN) {
+                       pos[0] -= fwd[0];
+                       pos[1] -= fwd[1];
+               }
+               if(input & BN_RIGHT) {
+                       pos[0] += right[0];
+                       pos[1] += right[1];
+               }
+               if(input & BN_LEFT) {
+                       pos[0] -= right[0];
+                       pos[1] -= right[1];
+               }
+
+               vox_view(vox, pos[0], pos[1], -30, angle);
        }
-       if(input & BN_DOWN) {
-               pos[0] -= fwd[0];
-               pos[1] -= fwd[1];
-       }
-       if(input & BN_RIGHT) {
-               pos[0] += right[0];
-               pos[1] += right[1];
-       }
-       if(input & BN_LEFT) {
-               pos[0] -= right[0];
-               pos[1] -= right[1];
-       }
-
-       vox_view(vox, pos[0], pos[1], -30, angle);
 }
 
 static void draw(void)
 {
        vox_render(vox);
-       vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
-       //vox_sky_solid(vox, COLOR_ZENITH);
+       //vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
+       vox_sky_solid(vox, COLOR_ZENITH);
 }
 
 #ifdef BUILD_GBA
index 5f54f35..c61066d 100644 (file)
@@ -138,7 +138,7 @@ void present(int buf)
 {
        int i, npix = 240 * 160;
        uint32_t *dptr = convbuf;
-       uint8_t *sptr = buf ? gba_vram_lfb1 : gba_vram_lfb0;
+       uint8_t *sptr = (uint8_t*)(buf ? gba_vram_lfb1 : gba_vram_lfb0);
 
        for(i=0; i<npix; i++) {
                int idx = *sptr++;
index b6b62e6..2bd0338 100644 (file)
@@ -107,6 +107,6 @@ void *get_pc(void)
 
 void *get_sp(void)
 {
-       return 0;       /* not useful on PC builds */
+       return (void*)-1;       /* not useful on PC builds */
 }
 #endif
index f719b17..2e5f013 100644 (file)
                REG_DISPCNT = DISPCNT_BG2 | DISPCNT_OBJ | 4 | ((x) << 4); \
        } while(0)
 
+#define ARM_IWRAM      __attribute__((noinline, target("arm"), section(".iwram")))
+
 #else  /* non-GBA build */
 #define wait_vblank()
 
 void present(int buf);         /* defined in src/pc/main.c */
+
+#define ARM_IWRAM
 #endif
 
 #define set_bg_color(idx, r, g, b) \
index ef11974..a9008ab 100644 (file)
@@ -7,8 +7,18 @@
 #include "voxscape.h"
 #include "debug.h"
 
+/* hardcoded dimensions for the GBA */
 #define FBWIDTH                240
 #define FBHEIGHT       160
+/* map size */
+#define XSZ                    512
+#define YSZ                    512
+#define XSHIFT         9
+#define XMASK          0x1ff
+#define YMASK          0x1ff
+
+
+#define NO_LERP
 
 #define XLERP(a, b, t, fp) \
        ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
@@ -108,10 +118,15 @@ void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
 }
 
 #define H(x, y)        \
-       vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
+       vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
 #define C(x, y) \
-       vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
+       vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
 
+#ifdef NO_LERP
+#define vox_height(vox, x, y)  H(x, y)
+#define vox_color(vox, x, y)   C(x, y)
+
+#else
 
 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
 {
@@ -156,6 +171,7 @@ int vox_color(struct voxscape *vox, int32_t x, int32_t y)
        c1 = XLERP(c10, c11, v, 16);
        return XLERP(c0, c1, u, 16);
 }
+#endif /* !NO_LERP */
 
 
 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
@@ -167,9 +183,8 @@ void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
 {
        if(xres != vox->fbwidth) {
-               free(vox->coltop);
-               if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
-                       fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
+               if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
+                       panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
                        return;
                }
        }
@@ -201,8 +216,8 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
 
        vox->nslices = vox->zfar - vox->znear;
        free(vox->slicelen);
-       if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
-               fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
+       if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
+               panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
                return;
        }
 
@@ -241,11 +256,12 @@ void vox_begin(struct voxscape *vox)
        }
 }
 
+ARM_IWRAM
 void vox_render_slice(struct voxscape *vox, int n)
 {
-       int i, j, hval, colstart, colheight, col, z;
+       int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
        int32_t x, y, len, xstep, ystep;
-       uint8_t color;
+       uint8_t color, last_col;
        uint16_t *fbptr;
 
        z = vox->znear + n;
@@ -257,13 +273,22 @@ void vox_render_slice(struct voxscape *vox, int n)
        x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 2);
        y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 2);
 
-       for(i=0; i<FBWIDTH / 2; i++) {
+       for(i=1; i<FBWIDTH / 2; i++) {
                col = i * 2;
-               hval = vox_height(vox, x, y) - vox->vheight;
-               hval = hval * 40 / (vox->znear + n) + vox->horizon;
-               if(hval > FBHEIGHT) hval = FBHEIGHT;
+               offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
+               if(offs == last_offs) {
+                       hval = last_hval;
+                       color = last_col;
+               } else {
+                       hval = vox->height[offs] - vox->vheight;
+                       hval = hval * 40 / (vox->znear + n) + vox->horizon;
+                       if(hval > FBHEIGHT) hval = FBHEIGHT;
+                       color = vox->color[offs];
+                       last_offs = offs;
+                       last_hval = hval;
+                       last_col = color;
+               }
                if(hval > vox->coltop[col]) {
-                       color = vox_color(vox, x, y);
                        colstart = FBHEIGHT - hval;
                        colheight = hval - vox->coltop[col];
                        fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
@@ -278,11 +303,20 @@ void vox_render_slice(struct voxscape *vox, int n)
                y += ystep;
 
                col++;
-               hval = vox_height(vox, x, y) - vox->vheight;
-               hval = hval * 40 / (vox->znear + n) + vox->horizon;
-               if(hval > FBHEIGHT) hval = FBHEIGHT;
+               offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
+               if(offs == last_offs) {
+                       hval = last_hval;
+                       color = last_col;
+               } else {
+                       hval = vox->height[offs] - vox->vheight;
+                       hval = hval * 40 / (vox->znear + n) + vox->horizon;
+                       if(hval > FBHEIGHT) hval = FBHEIGHT;
+                       color = vox->color[offs];
+                       last_offs = offs;
+                       last_hval = hval;
+                       last_col = color;
+               }
                if(hval > vox->coltop[col]) {
-                       color = vox_color(vox, x, y);
                        colstart = FBHEIGHT - hval;
                        colheight = hval - vox->coltop[col];
                        fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
@@ -298,12 +332,13 @@ void vox_render_slice(struct voxscape *vox, int n)
        }
 }
 
+ARM_IWRAM
 void vox_sky_solid(struct voxscape *vox, uint8_t color)
 {
        int i, j, colh0, colh1, colhboth;
        uint16_t *fbptr;
 
-       for(i=0; i<FBWIDTH / 2; i++) {
+       for(i=1; i<FBWIDTH / 2; i++) {
                fbptr = vox->fb + i;
                colh0 = FBHEIGHT - vox->coltop[i << 1];
                colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
@@ -343,7 +378,7 @@ void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
                grad[i] = chor;
        }
 
-       for(i=0; i<FBWIDTH / 2; i++) {
+       for(i=1; i<FBWIDTH / 2; i++) {
                fbptr = vox->fb + i;
                colh0 = FBHEIGHT - vox->coltop[i << 1];
                colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];