From 5d9ec41cc62ebddf5d406511714e561e88884987 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sun, 11 Sep 2016 07:47:44 +0300 Subject: [PATCH] added two new global pointers in demo.h: vmem_back and vmem_front, and now parts need to call swap_buffers manually. This allows the flexibility to have some parts write to video memory directly (vmem_back) and then do an extremely fast page-flip by calling swap_buffers(0). Other parts that prefer to write to a system-ram framebuffer can continue to use fb_pixels, but they now have to call swap_buffers(fb_pixels), to copy the result to vmem_front. vmem_back and vmem_front at the moment point to the front buffer, until I implement VBE page flipping, and in any case they may still both point to the front buffer if there isn't enough memory for two pages, so don't memcpy from one to the other manually. Use swap_buffers only. --- src/3dgfx.h | 2 ++ src/demo.c | 2 +- src/demo.h | 15 ++++++++++++++- src/dos/main.c | 20 ++++++++++++++------ src/fract.c | 4 +++- src/grise.c | 6 ++++-- src/polytest.c | 2 ++ src/sdl/main.c | 6 ++++++ src/tunnel.c | 10 ++++++---- 9 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/3dgfx.h b/src/3dgfx.h index 68d0783..c80ded1 100644 --- a/src/3dgfx.h +++ b/src/3dgfx.h @@ -1,6 +1,8 @@ #ifndef THREEDGFX_H_ #define THREEDGFX_H_ +#include "inttypes.h" + struct g3d_vertex { float x, y, z, w; float nx, ny, nz; diff --git a/src/demo.c b/src/demo.c index 64bb965..ce06a44 100644 --- a/src/demo.c +++ b/src/demo.c @@ -11,7 +11,7 @@ int fb_width = 320; int fb_height = 240; int fb_bpp = 16; -void *fb_pixels; +uint16_t *fb_pixels, *vmem_back, *vmem_front; unsigned long time_msec; int mouse_x, mouse_y; unsigned int mouse_bmask; diff --git a/src/demo.h b/src/demo.h index 530e3de..1b246ed 100644 --- a/src/demo.h +++ b/src/demo.h @@ -1,8 +1,14 @@ #ifndef DEMO_H_ #define DEMO_H_ +#include "inttypes.h" + extern int fb_width, fb_height, fb_bpp; -extern void *fb_pixels; +extern uint16_t *fb_pixels; /* system-RAM pixel buffer: use swap_buffers(fb_pixels) */ +/* video memory pointers. might both point to the front buffer if there is not + * enough memory for page flipping. use swap_buffers(0) to flip. */ +extern uint16_t *vmem_back, *vmem_front; + extern unsigned long time_msec; extern int mouse_x, mouse_y; extern unsigned int mouse_bmask; @@ -14,9 +20,16 @@ void demo_draw(void); void demo_keyboard(int key, int state); + /* defined in main_*.c */ void demo_quit(void); unsigned long get_msec(void); void set_palette(int idx, int r, int g, int b); +/* pass 0 to just swap vmem_back/vmem_front with page flipping + * pass a pointer to a system-ram pixel buffer to copy it to vmem_front, + * instead of flipping. + */ +void swap_buffers(void *pixels); + #endif /* DEMO_H_ */ diff --git a/src/dos/main.c b/src/dos/main.c index 06fd0bb..10492fe 100644 --- a/src/dos/main.c +++ b/src/dos/main.c @@ -10,11 +10,11 @@ static int quit; static int use_mouse; +static long fbsize; int main(int argc, char **argv) { - void *vmem; - long fbsize = fb_width * fb_height * fb_bpp / CHAR_BIT; + fbsize = fb_width * fb_height * fb_bpp / CHAR_BIT; init_timer(100); kb_init(32); @@ -29,9 +29,11 @@ int main(int argc, char **argv) return 1; } - if(!(vmem = set_video_mode(fb_width, fb_height, fb_bpp))) { + if(!(vmem_front = set_video_mode(fb_width, fb_height, fb_bpp))) { return 1; } + /* TODO implement multiple video memory pages for flipping */ + vmem_back = vmem_front; if(demo_init(argc, argv) == -1) { set_text_mode(); @@ -52,9 +54,6 @@ int main(int argc, char **argv) time_msec = get_msec(); demo_draw(); - - /*wait_vsync();*/ - memcpy(vmem, fb_pixels, fbsize); } break_evloop: @@ -68,3 +67,12 @@ void demo_quit(void) { quit = 1; } + +void swap_buffers(void *pixels) +{ + /* TODO implement page flipping */ + if(pixels) { + /*wait_vsync();*/ + memcpy(vmem_front, pixels, fbsize); + } +} diff --git a/src/fract.c b/src/fract.c index 0245b72..15da79d 100644 --- a/src/fract.c +++ b/src/fract.c @@ -22,7 +22,7 @@ static struct screen scr = { draw }; -static long aspect_24x8 = (long)(1.3333333 * 256.0); +/*static long aspect_24x8 = (long)(1.3333333 * 256.0);*/ static long xscale_24x8 = (long)(1.3333333 * 1.2 * 256.0); static long yscale_24x8 = (long)(1.2 * 256.0); static int cx, cy; @@ -84,6 +84,8 @@ static void draw(void) } pixels[mouse_y * fb_width + mouse_x] = 0xffe; + + swap_buffers(fb_pixels); } static long normalize_coord(long x, long range) diff --git a/src/grise.c b/src/grise.c index 50d80ce..2aaf925 100644 --- a/src/grise.c +++ b/src/grise.c @@ -42,8 +42,8 @@ static void updateScrollTables(float dt); static void rleEncode(unsigned char *pixels, unsigned int w, unsigned int h); static unsigned short *background = 0; -static unsigned int backgroundW = 0; -static unsigned int backgroundH = 0; +static int backgroundW = 0; +static int backgroundH = 0; static unsigned int lastFrameTime = 0; static float lastFrameDuration = 0.0f; @@ -178,6 +178,8 @@ static void draw(void) src += BB_SIZE; dst += fb_width; } + + swap_buffers(fb_pixels); } /* src and dst can be the same */ diff --git a/src/polytest.c b/src/polytest.c index 0217916..fc38c16 100644 --- a/src/polytest.c +++ b/src/polytest.c @@ -90,6 +90,8 @@ static void draw(void) zsort(&torus); draw_mesh(&torus); + + swap_buffers(fb_pixels); } static void draw_mesh(struct mesh *mesh) diff --git a/src/sdl/main.c b/src/sdl/main.c index b3ff905..e5eda65 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -32,6 +32,7 @@ int main(int argc, char **argv) fprintf(stderr, "failed to allocate virtual framebuffer\n"); return 1; } + vmem_front = vmem_back = fb_pixels; SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, sdl_flags))) { @@ -98,6 +99,11 @@ void demo_quit(void) quit = 1; } +void swap_buffers(void *pixels) +{ + /* do nothing, all pointers point to the same buffer */ +} + static void handle_event(SDL_Event *ev) { switch(ev->type) { diff --git a/src/tunnel.c b/src/tunnel.c index 13eea70..63f0f9f 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -27,8 +27,6 @@ static void draw_tunnel_range(unsigned short *pixels, int xoffs, int yoffs, int static int count_bits(unsigned int x); static int count_zeros(unsigned int x); -static unsigned int *gen_test_image(int *wptr, int *hptr); - static struct screen scr = { "tunnel", init, @@ -184,11 +182,13 @@ static void draw(void) int starty = i * num_lines; int resty = starty + draw_lines; int rest_lines = num_lines - draw_lines; - draw_tunnel_range((unsigned short*)fb_pixels, xoffs, yoffs, starty, draw_lines, time_msec); + draw_tunnel_range(vmem_back, xoffs, yoffs, starty, draw_lines, time_msec); if(rest_lines) { - memset((unsigned short*)fb_pixels + resty * fb_width, 0, rest_lines * fb_width * 2); + memset(vmem_back + resty * fb_width, 0, rest_lines * fb_width * 2); } } + + swap_buffers(0); } static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog) @@ -264,6 +264,7 @@ static int count_zeros(unsigned int x) return num; } +/* static unsigned int *gen_test_image(int *wptr, int *hptr) { int i, j; @@ -287,3 +288,4 @@ static unsigned int *gen_test_image(int *wptr, int *hptr) *hptr = ysz; return pixels; } +*/ -- 1.7.10.4