c++ - Avoid waiting on SwapBuffers -


i have discovered swapbuffers in opengl busy-wait long graphics card isn't done rendering or if it's waiting on v-sync.

this problem me because don't want waste 100% of cpu core while waiting card finished. i'm not writing game, can not use cpu cycles more productive, want yield them other process in operating system.

i've found callback-functions such gluttimerfunc , glutidlefunc work me, don't want use glut. still, glut must in way use normal gl functions this, right?

is there function such "glreadytoswap" or similar? in case check every millisecond or , determine if should wait while longer or swap. imagine perhaps skip swapbuffers , write own similar function doesn't busy-wait if point me in right direction.

swapbuffers not busy waiting, blocks thread in driver context, makes windows calculating cpu usage wrongly: windows calculates cpu usage determining how cpu time idle process gets + how time programs don't spend in driver context. swapbuffers block in driver context , program takes away cpu time idle process. cpu doing literally nothing in time, scheduler happily waiting pass time other processes. idle process otoh nothing else yield time rest of system, scheduler jumps right process, blocks in driver windows counts "is clogging cpu". if you'd measure actual power consumption or heat output, simple opengl program stay rather low.

this irritating behaviour opengl faq!

just create additional threads parallel data processing. keep opengl in 1 thread, data processing in other. if want down reported cpu usage, adding sleep(0) or sleep(1) after swapbuffers trick. sleep(1) make process spend blocking little time in user context, idle process gets more time, out numbers. if don't want sleep, may following:

const float time_margin = ... // margin float display_refresh_period; // 1./60. or so.  void render(){      float rendertime_start = get_time();      render_scene();     glfinish();      float rendertime_finish = get_time();     float time_to_finish = rendertime_finish - rendertime_start;      float time_rest = fmod(render_finish - time_margin, display_refresh_period);     sleep(time_rest);     swapbuffers();  } 

in programs use kind of timing reason: let swapbuffers block without helper sleeps, give other worker threads time stuff on gpu through shared context (like updating textures) , have garbage collector running. it's not neccesary time it, worker threads being finished before swapbuffers returns allows 1 start rendering next frame since mutexes unlocked then.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -