rayzor

diff src/main.cc @ 10:235c8b764c0b

optimized swap_buffers
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 10 Apr 2014 08:03:52 +0300
parents 70e332156d02
children fe94d9e986ae
line diff
     1.1 --- a/src/main.cc	Thu Apr 10 02:31:31 2014 +0300
     1.2 +++ b/src/main.cc	Thu Apr 10 08:03:52 2014 +0300
     1.3 @@ -1,7 +1,6 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <string.h>
     1.7 -#include <math.h>
     1.8  #include <signal.h>
     1.9  #include "inttypes.h"
    1.10  #include "gfx.h"
    1.11 @@ -14,6 +13,16 @@
    1.12  #include "modeller.h"
    1.13  #include "renderer.h"
    1.14  #include "scrman.h"
    1.15 +#include "timer.h"
    1.16 +
    1.17 +#ifdef __DOS__
    1.18 +#undef USE_ASM_SWAPBUF
    1.19 +#endif
    1.20 +
    1.21 +#ifdef USE_ASM_SWAPBUF
    1.22 +// defined in swapbuf.asm
    1.23 +extern "C" void swap_buffers_asm(void *dest, void *src, int xsz, int ysz, int bpp);
    1.24 +#endif
    1.25  
    1.26  static bool init();
    1.27  static void cleanup();
    1.28 @@ -31,19 +40,22 @@
    1.29  int fb_bpp = 32;
    1.30  Scene *scene;
    1.31  
    1.32 -static int bytespp;
    1.33  static bool novideo;
    1.34  static void *fb;
    1.35  static int rbits, gbits, bbits;
    1.36  static int rshift, gshift, bshift;
    1.37  static unsigned int rmask, gmask, bmask;
    1.38  
    1.39 +static bool use_asm_swap = true;
    1.40  static bool use_mouse;
    1.41  static int mouse_x, mouse_y;
    1.42  static bool quit;
    1.43  
    1.44  int main(int argc, char **argv)
    1.45  {
    1.46 +	unsigned long start_msec, msec;
    1.47 +	unsigned long nframes = 0;
    1.48 +
    1.49  	if(!parse_args(argc, argv)) {
    1.50  		return 1;
    1.51  	}
    1.52 @@ -51,6 +63,8 @@
    1.53  		return 1;
    1.54  	}
    1.55  
    1.56 +	start_msec = get_msec();
    1.57 +
    1.58  	// main loop
    1.59  	for(;;) {
    1.60  		handle_keyboard();
    1.61 @@ -58,11 +72,16 @@
    1.62  		if(quit) break;
    1.63  
    1.64  		display();
    1.65 +		++nframes;
    1.66  
    1.67  		if(novideo) break;
    1.68  	}
    1.69  
    1.70 +	msec = get_msec() - start_msec;
    1.71 +
    1.72  	cleanup();
    1.73 +
    1.74 +	printf("Average framerate: %g\n", (float)nframes / ((float)msec / 1000.0f));
    1.75  	printf("Thank you for using Rayzor!\n");
    1.76  	return 0;
    1.77  }
    1.78 @@ -79,6 +98,8 @@
    1.79  	signal(SIGILL, sig);
    1.80  	signal(SIGFPE, sig);
    1.81  
    1.82 +	init_timer(128);
    1.83 +
    1.84  	if(!novideo) {
    1.85  		if(kb_init(32) == -1) {
    1.86  			fprintf(stderr, "failed to initialize keyboard driver\n");
    1.87 @@ -94,8 +115,8 @@
    1.88  		get_color_bits(&rbits, &gbits, &bbits);
    1.89  		get_color_shift(&rshift, &gshift, &bshift);
    1.90  		get_color_mask(&rmask, &gmask, &bmask);
    1.91 -		bytespp = (int)ceil(fb_bpp / 8.0);
    1.92  
    1.93 +		printlog("video resolution: %dx%d\n", fb_width, fb_height);
    1.94  		printlog("bpp: %d (%d %d %d)\n", fb_bpp, rbits, gbits, bbits);
    1.95  		printlog("shift: %d %d %d\n", rshift, gshift, bshift);
    1.96  		printlog("mask: %x %x %x\n", rmask, gmask, bmask);
    1.97 @@ -107,9 +128,8 @@
    1.98  	} else {
    1.99  		logger_output(stdout);
   1.100  		printlog("novideo (debug) mode\n");
   1.101 -		fb_bpp = 32;
   1.102 +		fb_bpp = 24;
   1.103  		rbits = gbits = bbits = 8;
   1.104 -		bytespp = 3;
   1.105  	}
   1.106  
   1.107  	fb_pixels = new uint32_t[fb_width * fb_height * 4];
   1.108 @@ -165,8 +185,12 @@
   1.109  	}
   1.110  
   1.111  	if(!novideo) {
   1.112 +		wait_vsync();
   1.113 +#ifdef USE_ASM_SWAPBUF
   1.114 +		swap_buffers_asm(fb, fb_pixels, fb_width, fb_height, fb_bpp);
   1.115 +#else
   1.116  		swap_buffers();
   1.117 -		wait_vsync();
   1.118 +#endif
   1.119  	}
   1.120  }
   1.121  
   1.122 @@ -175,9 +199,9 @@
   1.123  	 (((g) << gshift) & gmask) | \
   1.124  	 (((b) << bshift) & bmask))
   1.125  
   1.126 -#define UNPACK_RED(c)	(((c) >> 16) & 0xff)
   1.127 +#define UNPACK_RED(c)	((c) & 0xff)
   1.128  #define UNPACK_GREEN(c)	(((c) >> 8) & 0xff)
   1.129 -#define UNPACK_BLUE(c)	((c) & 0xff)
   1.130 +#define UNPACK_BLUE(c)	(((c) >> 16) & 0xff)
   1.131  
   1.132  static void swap_buffers()
   1.133  {
   1.134 @@ -192,12 +216,13 @@
   1.135  	case 24:
   1.136  		{
   1.137  			unsigned char *dest = (unsigned char*)fb;
   1.138 -			for(int i=0; i<num_pixels; i++) {
   1.139 -				uint32_t c = *src++;
   1.140 -				*dest++ = UNPACK_RED(c);
   1.141 -				*dest++ = UNPACK_GREEN(c);
   1.142 -				*dest++ = UNPACK_BLUE(c);
   1.143 +			for(int i=0; i<num_pixels-1; i++) {
   1.144 +				*((uint32_t*)dest) = *src++;
   1.145 +				dest += 3;
   1.146  			}
   1.147 +			*dest++ = UNPACK_RED(*src);
   1.148 +			*dest++ = UNPACK_GREEN(*src);
   1.149 +			*dest++ = UNPACK_BLUE(*src);
   1.150  		}
   1.151  		break;
   1.152  
   1.153 @@ -257,6 +282,14 @@
   1.154  	if(novideo) return;
   1.155  
   1.156  	while((key = kb_getkey()) != -1) {
   1.157 +		switch(key) {
   1.158 +		case '`':
   1.159 +			use_asm_swap = !use_asm_swap;
   1.160 +			break;
   1.161 +
   1.162 +		default:
   1.163 +			break;
   1.164 +		}
   1.165  		scr->handle_keyboard(key, true);	// TODO also generate release events...
   1.166  	}
   1.167  }
   1.168 @@ -285,6 +318,9 @@
   1.169  	}
   1.170  	prev_mx = mx;
   1.171  	prev_my = my;
   1.172 +
   1.173 +	mouse_x = mx;
   1.174 +	mouse_y = my;
   1.175  }
   1.176  
   1.177