From 15fa7c58b4dd399d2dc3dc0eacbf504e357d66ca Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Wed, 19 May 2021 05:19:33 +0300 Subject: [PATCH] no-thread option, coalesced tunnel lut, more accurate fps counter --- Makefile | 8 +++++++- src/main.c | 6 ++++-- src/tpool.c | 3 +++ src/tunnel.c | 65 +++++++++++++++++++++++++++++++--------------------------- 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index b1ba5c1..7ead672 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,13 @@ obj = $(src:.c=.o) bin = fbgfx CFLAGS = -pedantic -Wall -g -O3 -LDFLAGS = -limago -lm -lpthread +LDFLAGS = -limago -lm $(ldflags_pthread) + +ifeq ($(NOTHREADS), 1) + CFLAGS += -DNO_THREADS +else + ldflags_pthread = -lpthread +endif $(bin): $(obj) $(CC) -o $@ $(obj) $(LDFLAGS) diff --git a/src/main.c b/src/main.c index 4d63726..7c41e2e 100644 --- a/src/main.c +++ b/src/main.c @@ -61,7 +61,9 @@ int main(void) scr_update(); scr_draw(); - ++num_frames; + if(time_msec >= 4000) { + ++num_frames; + } } time_msec = get_time_msec() - start_msec; @@ -70,7 +72,7 @@ end: fbev_shutdown(); fbgfx_restore_video_mode(); if(num_frames && time_msec) { - printf("\ravg framerate: %.1f\n", (float)num_frames / ((float)time_msec / 1000.0)); + printf("\ravg framerate: %.1f\n", (float)num_frames / ((float)(time_msec - 4000) / 1000.0)); } return 0; } diff --git a/src/tpool.c b/src/tpool.c index 9e73f44..4d06570 100644 --- a/src/tpool.c +++ b/src/tpool.c @@ -1,3 +1,4 @@ +#ifndef NO_THREADS /* worker thread pool based on POSIX threads * author: John Tsiombikas * This code is public domain. @@ -311,3 +312,5 @@ int tpool_num_processors(void) return info.dwNumberOfProcessors; #endif } + +#endif /* !def NO_THREADS */ diff --git a/src/tunnel.c b/src/tunnel.c index 221fadc..e6a53d5 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -3,7 +3,9 @@ #include #include #include +#ifndef NO_THREADS #include "tpool.h" +#endif #include "demo.h" #include "screen.h" @@ -44,17 +46,23 @@ static struct screen scr = { draw }; +struct tunmap { + int u, v; + unsigned char fog; +}; + static int xsz, ysz, vxsz, vysz; static int pan_width, pan_height; -static unsigned int *tunnel_map; -static unsigned char *tunnel_fog; +static struct tunmap *tunnel_map; static int tex_xsz, tex_ysz; static unsigned int *tex_pixels; static int tex_xshift, tex_yshift; static unsigned int tex_xmask, tex_ymask; +#ifndef NO_THREADS static struct thread_pool *tpool; +#endif static long trans_start, trans_dur; static int trans_dir; @@ -69,8 +77,7 @@ struct screen *tunnel_screen(void) static int init(void) { int i, j, n; - unsigned int *tmap; - unsigned char *fog; + struct tunmap *tmap; float aspect = (float)fb_width / (float)fb_height; switch(fb_depth) { @@ -97,13 +104,8 @@ static int init(void) fprintf(stderr, "failed to allocate tunnel map\n"); return -1; } - if(!(tunnel_fog = malloc(vxsz * vysz))) { - fprintf(stderr, "failed to allocate tunnel fog map\n"); - return -1; - } tmap = tunnel_map; - fog = tunnel_fog; for(i=0; i 255 ? 255 : f; + tmap->u = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff; + tmap->v = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff; + tmap->fog = fog > 255 ? 255 : fog; + tmap++; } } @@ -144,10 +145,12 @@ static int init(void) } tex_yshift = n; +#ifndef NO_THREADS if(!(tpool = tpool_create(0))) { fprintf(stderr, "failed to create thread pool\n"); return -1; } +#endif /* initialize the constant part of all work items */ for(i=0; i> 16) & 0xffff) << tex_xshift) >> 16; - unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16; + unsigned int tx = (tmap->u << tex_xshift) >> 16; + unsigned int ty = (tmap->v << tex_yshift) >> 16; tx += toffs; ty += toffs << 1; @@ -243,9 +252,9 @@ static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpa g = (col >> 8) & 0xff; b = (col >> 16) & 0xff; - *rp = (r * fog) >> 8; - *gp = (g * fog) >> 8; - *bp = (b * fog) >> 8; + *rp = (r * tmap->fog) >> 8; + *gp = (g * tmap->fog) >> 8; + *bp = (b * tmap->fog) >> 8; } #define PACK_RGB16(r, g, b) \ @@ -256,8 +265,7 @@ static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpa static void draw_tunnel_range16(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm) { int i, j; - unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs; - unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs; + struct tunmap *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs; long toffs = tm / 4; unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1); @@ -267,20 +275,18 @@ static void draw_tunnel_range16(void *pix, int xoffs, int yoffs, int starty, int unsigned int col; int r, g, b; - tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]); + tunnel_color(&r, &g, &b, toffs, tmap + j); col = PACK_RGB16(r, g, b); *pixels++ = (col << 16) | col; } tmap += vxsz; - fog += vxsz; } } static void draw_tunnel_range32(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm) { int i, j; - unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs; - unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs; + struct tunmap *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs; long toffs = tm / 4; unsigned int *pixels = (unsigned int*)pix + starty * fb_width; @@ -290,14 +296,13 @@ static void draw_tunnel_range32(void *pix, int xoffs, int yoffs, int starty, int unsigned int col; int r, g, b; - tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]); + tunnel_color(&r, &g, &b, toffs, tmap + j); col = PACK_RGB32(r, g, b); *pixels++ = col; *pixels++ = col; } tmap += vxsz; - fog += vxsz; } } -- 1.7.10.4