<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Performance issues (Windows)</title>
		<link>http://www.allegro.cc/forums/view/590785</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 03 Apr 2007 03:53:52 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m just starting out w/Allegro, and have a very basic prototype for a shooter.  It runs acceptably on my computer (for an I&#39;m-still-learning-as-I-go prototype), but runs terribly on Windows (I use OS X).  The difference is striking -- on my Mac, it starts to slow down with ~50 objects on screen.  Not great, but not too bad for a first shot.  But it starts to slow down ~13 objects in Windows, even on a computer with much better specs than my own.  </p><p>I don&#39;t have any experience programming for Windows, so I&#39;m not even sure where to begin looking for the problem.  I don&#39;t do anything Mac-specific anywhere in the code.</p><p>gprof says it&#39;s spending a huge amount of time in gfx_directx_unlock_win.</p><p>Anyways, the source and Windows executable are <a href="http://greay.phpwebhosting.com/shooter/shooter.html">here</a>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (.greay)</author>
		<pubDate>Fri, 30 Mar 2007 01:09:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Are you drawing onto a video bitmap? If yes then describe the operations you do with it (what kind of bitmaps, blending, transparency, per-pixel effects, ...)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 30 Mar 2007 01:40:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It runs slow in my P4 2400MHz! but this problem is not Allegro related.<br />Post the code you use to blit your buffer bitmap to screen.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Fri, 30 Mar 2007 01:52:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I read your code a bit and found this thing:
</p><div class="source-code snippet"><div class="inner"><pre>  <span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>game_end_flag<span class="k2">)</span> <span class="k2">{</span>

    <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>t<span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>inc_timer<span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>inc_timer, BPS_TO_TIMER<span class="k2">(</span><span class="n">120</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>    

    <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>e<span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>inc_enemy<span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>inc_enemy, BPS_TO_TIMER<span class="k2">(</span><span class="n">120</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>    

    input_update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="c">// lots more stuff here that isn't interesting ATM</span>
</pre></div></div><p>How often are those things executed? I think it is not once per frame but less often. If it is more than once per application startup then it isn&#39;t a good thing, you should lock and install stuff once at initialization time, unless you uninstall timers some where else.</p><p>Also as I thought you draw things to video bitmap. What is worse is that it seems like you are doing per-pixel stuff there (rectangles, triangles). Try using regular memory bitmap for backbuffer, it should give considerable speedboost.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 30 Mar 2007 02:14:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Paul whoknows said:</div><div class="quote"><p>
It runs slow in my P4 2400MHz! but this problem is not Allegro related.<br />Post the code you use to blit your buffer bitmap to screen.
</p></div></div><p>

Yeah, I figured it was my fault, not Allegro&#39;s <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />  Here&#39;s pretty much the whole display routine, cut &amp; paste from a couple different files.  There&#39;s really nothing complicated going on.</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">static</span> <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>sandbox, <span class="k3">*</span>bkg<span class="k2">;</span></td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td>  sandbox <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_sub_bitmap" target="_blank"><span class="a">create_sub_bitmap</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, screen_x, screen_y, screen_width, screen_height<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>  bkg <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_bitmap" target="_blank"><span class="a">create_bitmap</span></a><span class="k2">(</span>screen_width, screen_height<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="k1">void</span> display_update<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">7</td><td>  Particle <span class="k3">*</span>ship <span class="k3">=</span> get_first_object<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>  <span class="k1">while</span> <span class="k2">(</span>ship<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">9</td><td>    <span class="k2">[</span>ship erase: sandbox to: bkg<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>    <span class="k2">[</span>ship draw: sandbox<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>    ship <span class="k3">=</span> <span class="k2">[</span>ship next<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">13</td><td><span class="k2">}</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td><span class="k3">-</span><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>erase: <span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span><span class="k2">)</span>front to: <span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span><span class="k2">)</span>back</td></tr><tr><td class="number">16</td><td><span class="k2">{</span></td></tr><tr><td class="number">17</td><td>  <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>back, front, drawn_x, drawn_y, drawn_x, drawn_y, drawn_l, drawn_h<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td><span class="k2">}</span></td></tr><tr><td class="number">19</td><td>&#160;</td></tr><tr><td class="number">20</td><td><span class="k3">-</span><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>draw: <span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span><span class="k2">)</span>bmp <span class="k2">{</span></td></tr><tr><td class="number">21</td><td>  <a href="http://www.allegro.cc/manual/triangle" target="_blank"><span class="a">triangle</span></a><span class="k2">(</span>bmp, x, y <span class="k3">+</span> sizey, x <span class="k3">+</span> <span class="n">10</span>, y <span class="k3">+</span> sizey, x <span class="k3">+</span> <span class="n">5</span>, y, color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>  <span class="k2">[</span>self save_state<span class="k2">]</span><span class="k2">;</span>  </td></tr><tr><td class="number">23</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

</p><div class="quote_container"><div class="title">HoHo said:</div><div class="quote"><p>
How often are those things executed? I think it is not once per frame but less often. If it is more than once per application startup then it isn&#39;t a good thing, you should lock and install stuff once at initialization time, unless you uninstall timers some where else.</p><p>Also as I thought you draw things to video bitmap. What is worse is that it seems like you are doing per-pixel stuff there (rectangles, triangles). Try using regular memory bitmap for backbuffer, it should give considerable speedboost.
</p></div></div><p>

Yeah, the timer initialization there was a mistake.  It should be in the game initialization fn.  Those are the only two timers I have, at least thus far.  <br />So I&#39;m basically drawing directly to the screen, yes?  Since I draw to the sandbox &amp; that&#39;s a sub-bitmap of the screen.  Are you saying I should draw to a system-memory bitmap first, and then blit that to the screen?</p><p>Ultimately, most of these are going to be pre-rendered bitmaps anyways, but are the drawing functions really that expensive?  Or is it just because how I&#39;m going about it?</p><p>Alright, I have some stuff to try.  Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (.greay)</author>
		<pubDate>Fri, 30 Mar 2007 21:04:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
So I&#39;m basically drawing directly to the screen, yes? Since I draw to the sandbox &amp; that&#39;s a sub-bitmap of the screen
</p></div></div><p>yes</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Are you saying I should draw to a system-memory bitmap first, and then blit that to the screen?
</p></div></div><p>yes</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Ultimately, most of these are going to be pre-rendered bitmaps anyways, but are the drawing functions really that expensive?
</p></div></div><p>They are if you perform them on video bitmaps. Reason is that to get a per-pixel access to video bitmaps you first have to lock them. That probably means the bitmap is downloaded from video RAM to system RAM, updated and uploaded back. That download-modify-upload is done once for every drawing command. The drawing itself should be quite cheap. That is also the reason why gprof showed that most of the time is spent in gfx_directx_unlock_win</p><p>[edit]</p><p>One simple thing you can do is to make the sandbox a regular system/memory bitmap and see how that works. It should be considerably faster than your current method
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 30 Mar 2007 21:21:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hmm.  I tried it, and the performance change is negligible in OS X (i.e., it&#39;s acceptable).  Sadly, it&#39;s also negligible in Windows (at least on the laptop I&#39;m using).  It still slows down ~ 13 objects or so, and gprof says pretty much the same thing:</p><pre>
27.72   1.12   1.12   gfx_directx_unlock_win
21.53   1.99   0.87   _linear_blit32
 7.18   2.28   0.29   blit
</pre><p>

...etc.</p><p>here&#39;s what I&#39;ve changed:</p><div class="source-code snippet"><div class="inner"><pre>  sandbox <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_bitmap" target="_blank"><span class="a">create_bitmap</span></a><span class="k2">(</span>screen_width, screen_height<span class="k2">)</span><span class="k2">;</span>
  bkg <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_bitmap" target="_blank"><span class="a">create_bitmap</span></a><span class="k2">(</span>screen_width, screen_height<span class="k2">)</span><span class="k2">;</span> <span class="c">// this line's still the same</span>

<span class="k3">-</span><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>erase: <span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span><span class="k2">)</span>front to: <span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span><span class="k2">)</span>back
<span class="k2">{</span>
  <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>back, front, drawn_x, drawn_y, drawn_x, drawn_y, drawn_l, drawn_h<span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>back, <a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, drawn_x, drawn_y, drawn_x, drawn_y, drawn_l, drawn_h<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>

<span class="k3">-</span><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>draw: <span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span><span class="k2">)</span>bmp <span class="k2">{</span>
  <a href="http://www.allegro.cc/manual/triangle" target="_blank"><span class="a">triangle</span></a><span class="k2">(</span>bmp, x, y, x <span class="k3">+</span> sizex, y, x <span class="k3">+</span> sizex <span class="k3">/</span> <span class="n">2</span>, y <span class="k3">+</span> sizey, color<span class="k2">)</span><span class="k2">;</span>
  <span class="k2">[</span>self save_state<span class="k2">]</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>bmp, <a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, drawn_x, drawn_y, drawn_x, drawn_y, drawn_l, drawn_h<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Oh yeah, and I moved the timer initializations to their proper place, i.e. game_init().</p><p>I&#39;ll try making the bitmaps expicitly system memory bmps by creating them with create_system_bitmap() instead of simply create_bitmap(), I guess.  But I&#39;ve got to be doing something <b>seriously</b> wrong somewhere.  What I don&#39;t get is why the disparity in performance between OS X and Windows is so large.</p><p>I know I could get a performance boost by using pre-rendered bitmaps instead of drawing the objects, but that doesn&#39;t look like it would actually solve my problem, so I&#39;m not going to worry about that yet.</p><p><b>**EDIT**</b></p><p>Ok.  Reading some of the docs, it sounded like using acquire_screen() and release_screen() might help.  I bookended the display loop with those.  The performance is pretty much the same, but now gprof reads thus:</p><pre>
18.18   0.70   0.70   blit
10.13   1.09   0.39   _linear_hline32
 9.35   1.45   0.36   _soft_polygon
 8.57   1.78   0.33   objc_msg_lookup
 5.71   2.00   0.22   ddraw_blit_to_self
</pre><p>

... and so on.  Pretty much the same, except w/the first two lines gone.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (.greay)</author>
		<pubDate>Mon, 02 Apr 2007 22:16:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t quite get what you&#39;ve changed. Do you blit stuff to both, front (backbufffe?) and screen? You should blit your backbuffer to screen only once per frame, not a single time more.</p><p>Usually things go like this:<br />Clear backbuffer<br />Blit your images to bacbuffer<br />blit backbuffer to screen<br />repeat</p><p>Backbuffer and images are all regular memory or system bitmaps, there are no video bitmaps besides screen that Allegro creates for you.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Pretty much the same, except w/the first two lines gone.
</p></div></div><p>That&#39;s odd. Together those function timings are no where near 100% usage. Are you sure you didn&#39;t miss something?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Tue, 03 Apr 2007 01:25:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ohmygod.</p><p>I think I found the problem.</p><p><b>BANGS HEAD ON DESK</b></p><p>I&#39;ll be with you again in a moment.</p><p>...</p><p>so yeah.  I had the display_update() function called INSIDE the game logic loop.  Which means it would loop through all the objects &amp; draw them &amp; draw to the screen ONCE FOR EACH OBJECT.  No wonder it was slowing down the more objects there were onscreen.  I was drawing the screen objects^2 times each time-step.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (.greay)</author>
		<pubDate>Tue, 03 Apr 2007 02:48:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">.greay said:</div><div class="quote"><p>

so yeah. I had the display_update() function called INSIDE the game logic loop. Which means it would loop through all the objects &amp; draw them &amp; draw to the screen ONCE FOR EACH OBJECT. No wonder it was slowing down the more objects there were onscreen. I was drawing the screen objects^2 times each time-step.
</p></div></div><p>
But even so, the OSX version was considerably faster thasn the Windows version.</p><p>One thing I suggest you read up on is the Allegro functions for locking and unlocking bitmaps
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/acquire_bitmap" target="_blank"><span class="a">acquire_bitmap</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/release_bitmap" target="_blank"><span class="a">release_bitmap</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>


AE.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Andrei Ellman)</author>
		<pubDate>Tue, 03 Apr 2007 03:49:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I always suspected there was some major problem with something. Things shouldn&#39;t slow down with so little objects on screen. Still, glad you&#39;ve found the problem <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Tue, 03 Apr 2007 03:53:52 +0000</pubDate>
	</item>
</rss>
