<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>waiting for vsync uses up too much CPU</title>
		<link>http://www.allegro.cc/forums/view/590675</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 24 Mar 2007 04:49:44 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, my monitor refresh rate: 60Hz<br />Game logic: 60Hz</p><p>Therefor, waiting for vsync uses up most extra free time there might be. A quick glance at the allegro source reveals that the code just loops within itself until theres a vertical blank, like this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> GFX_DRIVER <span class="k2">{</span>
     <a href="http://www.allegro.cc/manual/vsync" target="_blank"><span class="a">vsync</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>

...

GFX_DRIVER<span class="k3">*</span> gfx_driver<span class="k2">;</span>
</pre></div></div><p>

then when you call vsync()</p><p>it does this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> <a href="http://www.allegro.cc/manual/vsync" target="_blank"><span class="a">vsync</span></a><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="k2">{</span>
   <a href="http://www.allegro.cc/manual/ASSERT" target="_blank"><span class="a">ASSERT</span></a><span class="k2">(</span>gfx_driver<span class="k2">)</span><span class="k2">;</span>

   <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>_dispsw_status <span class="k3">&amp;</span><span class="k3">&amp;</span> gfx_driver-&gt;vsync<span class="k2">)</span>
      gfx_driver-&gt;vsync<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

So pretty much, if we&#39;re not at a vertical blank, run the function again. <br />Does that mean, i could slip in a rest(0) here, and get some CPU back, or would that mess everything up? Or have i just got lost in the source and completely missed something?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (The Unknown)</author>
		<pubDate>Fri, 23 Mar 2007 05:33:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why not slip in a rest(1) and give even more time back to the CPU?</p><p>This is what I had to do awhile back to get around some I/O issues I had been having with Allegro 4.2.0, Windows 98, and particular hardware configurations. Actually, I&#39;m using the Windows command Sleep() directly instead of having Allegro call it since I&#39;ve stopped using the Allegro timer system by default. In either case, I call it once per logic loop, so if the game is running 60 FPS, it will give up 60 of 1000 ms back to the CPU per second. (And thus, the lower the framerate, the less time the game gives up.)</p><p>You should <i>ALWAYS</i> call at least rest(0) in a game loop so that background tasks don&#39;t stall-out. I recommend rest(1) to further reduce CPU usage and to get around the problem I mentioned above.</p><p>The point in your code where you call it is pretty much up to you, but <i>never</i> call it immediately after vsync(). (You should always blit the screen immediately after a call to vsync().)</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Fri, 23 Mar 2007 09:15:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Implementation of vsync() in allegro should not busy-wait if timer has been installed. Try calling install_timer() first and check if it happend again. If it does, tell us which GFX_dirver are you using.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Sat, 24 Mar 2007 01:41:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Milan, any call to install_int* will call install_timer() if it hasn&#39;t already been called. The mouse subsystem uses that fact as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 24 Mar 2007 02:20:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hm right, I missunderstood the code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Sat, 24 Mar 2007 02:48:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> GFX_DRIVER <span class="k2">{</span>
     <a href="http://www.allegro.cc/manual/vsync" target="_blank"><span class="a">vsync</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
</p></div></div><p>Where do you see that? I see this:</p><div class="source-code snippet"><div class="inner"><pre>GFX_DRIVER gfx_directx_accel <span class="k3">=</span>
<span class="k2">{</span>
   gfx_directx_sync,
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>And that function is here:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> gfx_directx_sync<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="k2">{</span>
   IDirectDraw2_WaitForVerticalBlank<span class="k2">(</span>directdraw, DDWAITVB_BLOCKBEGIN, NULL<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>Alternatively, if you are using GDI:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">static</span> <span class="k1">void</span> gfx_gdi_vsync<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="k2">{</span>
   WaitForSingleObject<span class="k2">(</span>vsync_event, INFINITE<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>In fact, your code doesn&#39;t even make sense, because it would recurse as quickly as possible, which would crash almost instantly.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Sat, 24 Mar 2007 03:11:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I simplified it a little, but the code i found was rather different. <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /><br />i think i found some of it in gfx.h and gfx.c, but im not sure where the rest is now... oh well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (The Unknown)</author>
		<pubDate>Sat, 24 Mar 2007 04:32:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, you&#39;re going to need to post a program with a few lines as possible that uses extensive CPU and vsync.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Sat, 24 Mar 2007 04:49:44 +0000</pubDate>
	</item>
</rss>
