Datasets:
Download verification/sdl_fixture.c from PureOne/ChronoSheaf-Input-Lag: direct link, hf CLI and curl.
- Browser
- Download file 4.49 kB
-
https://huggingface.co/datasets/PureOne/ChronoSheaf-Input-Lag/resolve/main/verification/sdl_fixture.c
- Command line
-
hf download hf://datasets/PureOne/ChronoSheaf-Input-Lag/verification/sdl_fixture.c
-
curl -L -o sdl_fixture.c https://huggingface.co/datasets/PureOne/ChronoSheaf-Input-Lag/resolve/main/verification/sdl_fixture.c
4.49 kB
| /* The public SDL2 C types used by this fixture. SDL2 event union is 56 | |
| bytes on the supported x86-64 ABI. The adapter itself only uses pointers. */ | |
| typedef union { | |
| uint64_t align; | |
| unsigned char bytes[56]; | |
| struct { uint32_t type, timestamp, window; int32_t code; void *one, *two; } user; | |
| } event; | |
| extern int SDL_Init(uint32_t), SDL_PollEvent(event *), SDL_PushEvent(event *); | |
| extern uint32_t SDL_RegisterEvents(int); | |
| extern void *SDL_CreateWindow(const char *,int,int,int,int,uint32_t); | |
| extern void *SDL_CreateRenderer(void *,int,uint32_t); | |
| extern int SDL_RenderSetVSync(void *,int); | |
| extern int SDL_SetRenderDrawColor(void *,uint8_t,uint8_t,uint8_t,uint8_t); | |
| extern int SDL_RenderClear(void *), SDL_RenderReadPixels(void *,const void *,uint32_t,void *,int); | |
| extern void SDL_RenderPresent(void *),SDL_DestroyRenderer(void *),SDL_DestroyWindow(void *),SDL_Quit(void); | |
| extern const char *SDL_GetCurrentVideoDriver(void),*SDL_GetError(void); | |
| static const unsigned WARM=54, FRAMES=165; | |
| static int64_t born[LIMIT], completed[LIMIT]; | |
| static int seen_frame[LIMIT]; | |
| static atomic_int stop; | |
| static unsigned produced, event_type; | |
| static uint32_t event_seed=3129; | |
| static int64_t now_us(void) { | |
| struct timespec ts; clock_gettime(CLOCK_MONOTONIC,&ts); | |
| return (int64_t)ts.tv_sec*1000000+ts.tv_nsec/1000; | |
| } | |
| static void sleep_us(long t) { | |
| struct timespec ts={t/1000000,(t%1000000)*1000}; | |
| nanosleep(&ts,NULL); | |
| } | |
| static void *events(void *unused) { | |
| (void)unused; | |
| uint32_t rng=event_seed; | |
| for(unsigned i=1;i<LIMIT && !atomic_load(&stop);++i) { | |
| rng=1664525u*rng+1013904223u; sleep_us(1900+(rng%3200)); | |
| if(atomic_load(&stop)) break; | |
| event e; memset(&e,0,sizeof(e)); e.user.type=event_type; e.user.code=(int)i; | |
| born[i]=now_us(); | |
| if(SDL_PushEvent(&e)!=1) abort(); | |
| produced=i; | |
| } | |
| return NULL; | |
| } | |
| static void require(int ok) { if (!ok) { fprintf(stderr,"SDL fixture: %s\n",SDL_GetError()); exit(1); } } | |
| int main(int argc,char **argv) { | |
| if(argc>2) event_seed=(uint32_t)strtoul(argv[2],NULL,10); | |
| long work=argc>3?strtol(argv[3],NULL,10):1250; | |
| const int paced=!(argc>1 && strcmp(argv[1],"unpaced")==0); | |
| require(SDL_Init(0x20|0x4000)==0); | |
| void *window=SDL_CreateWindow("Timing fixture",0,0,8,8,0); | |
| require(window!=NULL); | |
| void *renderer=SDL_CreateRenderer(window,-1,1|(paced?4:0)); | |
| require(renderer!=NULL); | |
| if(paced) require(SDL_RenderSetVSync(renderer,1)==0); | |
| int64_t (*underlying)(void); | |
| *(void **)(&underlying)=dlsym(RTLD_DEFAULT,"xr_last_underlying_us"); | |
| require(underlying!=NULL); | |
| event_type=SDL_RegisterEvents(1); | |
| require(event_type!=(unsigned)-1); | |
| int64_t last=0; unsigned consumed=0; | |
| pthread_t worker; | |
| if(pthread_create(&worker,NULL,events,NULL)) return 3; | |
| puts("kind,id,frame,t0_us,t1_us,application_return_us"); | |
| for(unsigned frame=0;frame<FRAMES;++frame) { | |
| unsigned prev=consumed; | |
| event e; | |
| while(SDL_PollEvent(&e)) if(e.user.type==event_type) { | |
| require((unsigned)e.user.code==consumed+1); consumed++; | |
| } | |
| unsigned char r=consumed&255u,g=(consumed>>8)&255u; | |
| require(SDL_SetRenderDrawColor(renderer,r,g,0,255)==0 && SDL_RenderClear(renderer)==0); | |
| uint32_t pixels[64]; | |
| require(SDL_RenderReadPixels(renderer,NULL,0x16362004u,pixels,8*4)==0); | |
| for(unsigned p=0;p<64;p++) require((pixels[p]&0xffffffu)==((unsigned)r<<16|((unsigned)g<<8))); | |
| sleep_us(work+(long)((frame*29)%137)); | |
| SDL_RenderPresent(renderer); | |
| int64_t app=now_us(), raw=underlying(); require(raw>last && app>=raw); | |
| printf("F,%u,%u,%lld,%lld,%lld\n",consumed,frame,(long long)last,(long long)raw,(long long)app); | |
| for(unsigned id=prev+1;id<=consumed;id++) {completed[id]=raw;seen_frame[id]=(int)frame;} | |
| last=raw; | |
| } | |
| atomic_store(&stop,1);pthread_join(worker,NULL); | |
| for(unsigned id=1;id<=consumed;id++) | |
| printf("E,%u,%d,%lld,%lld,0\n",id,seen_frame[id],(long long)born[id],(long long)completed[id]); | |
| fprintf(stderr,"driver=%s consumed=%u trailing=%u warmup=%u frames=%u\n",SDL_GetCurrentVideoDriver(),consumed,produced-consumed,WARM,FRAMES); | |
| SDL_DestroyRenderer(renderer);SDL_DestroyWindow(window);SDL_Quit();return 0; | |
| } | |