<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Fastest forms of putpixel()/getpixel()</title>
		<link>http://www.allegro.cc/forums/view/589221</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 23 Dec 2006 20:34:09 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, just a quick question: What are the <i>fastest</i> possible funcitons that work like <tt>putpixel()</tt> and <tt>getpixel()</tt>?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Fri, 22 Dec 2006 02:54:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/_putpixel" target="_blank"><span class="a">_putpixel</span></a>
<a href="http://www.allegro.cc/manual/_putpixel15" target="_blank"><span class="a">_putpixel15</span></a>
<a href="http://www.allegro.cc/manual/_putpixel16" target="_blank"><span class="a">_putpixel16</span></a>
<a href="http://www.allegro.cc/manual/_putpixel24" target="_blank"><span class="a">_putpixel24</span></a>
<a href="http://www.allegro.cc/manual/_putpixel32" target="_blank"><span class="a">_putpixel32</span></a>

<a href="http://www.allegro.cc/manual/_getpixel" target="_blank"><span class="a">_getpixel</span></a>
<a href="http://www.allegro.cc/manual/_getpixel15" target="_blank"><span class="a">_getpixel15</span></a>
<a href="http://www.allegro.cc/manual/_getpixel16" target="_blank"><span class="a">_getpixel16</span></a>
<a href="http://www.allegro.cc/manual/_getpixel24" target="_blank"><span class="a">_getpixel24</span></a>
<a href="http://www.allegro.cc/manual/_getpixel32" target="_blank"><span class="a">_getpixel32</span></a>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kazzmir)</author>
		<pubDate>Fri, 22 Dec 2006 02:59:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><s>I thought there was like a <tt>memory_getpixel()</tt> function that was faster... Anyone know about that?</s></p><p>Nevermind, nevermind, found it... Well, thanks! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Fri, 22 Dec 2006 03:53:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>direct memory access through the lines[] pointer would is the fastest but you&#39;ll have to make sure you won&#39;t go out of bitmap bounds.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 22 Dec 2006 05:37:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>lines[] also only works on memory bitmaps. Depending on what you&#39;re doing, theo fastest may be:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Once per line</span>
<a href="http://www.allegro.cc/manual/bmp_write_line" target="_blank"><span class="a">bmp_write_line</span></a> <a href="http://www.allegro.cc/manual/bmp_read_line" target="_blank"><span class="a">bmp_read_line</span></a>
<span class="c">// Once per pixel</span>
bmp_write8<span class="k3">/</span><span class="n">16</span><span class="k3">/</span><span class="n">24</span><span class="k3">/</span><span class="n">32</span> bmp_read8<span class="k3">/</span><span class="n">16</span><span class="k3">/</span><span class="n">24</span><span class="k3">/</span><span class="n">32</span>
<span class="c">// Once when done</span>
<a href="http://www.allegro.cc/manual/bmp_unwrite_line" target="_blank"><span class="a">bmp_unwrite_line</span></a>
</pre></div></div><p>
That will work on any and all valid bitmaps, and for memory bitmaps resolves to little more than direct line[] access. You may have to manually acquire/release the bitmap though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Fri, 22 Dec 2006 06:46:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If he wants to play directly with pixels then won&#39;t there be a severe performance loss unless the bitmap is memory bitmap?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 22 Dec 2006 14:37:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
direct memory access through the lines[] pointer would is the fastest but you&#39;ll have to make sure you won&#39;t go out of bitmap bounds.f
</p></div></div><p>

You have to care about that when using _putpixel too. _putpixel is not any faster then using line[] pointer, just cleaner.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Fri, 22 Dec 2006 15:58:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think direct memory access could still have some benefits. E.g you could take the line pointer, saving a bit on pointer calculations (line [X] instead of bmp-&gt;line[X]). Also I think using full pixels (int32_t for 32bit) instead of separate RGB channels could give a bit of speed. When getting maximum possible speed very important you could always use MMX/SSE intrinsics for some SIMD action <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 22 Dec 2006 16:48:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I seem to remember _putpixel* being slower then line[] for some reason.  Even if thats not so and forgetting about ASM tricks, you could process multiple pixels at a time via line[].
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Fri, 22 Dec 2006 18:03:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I seem to remember _putpixel* being slower then line[] for some reason.
</p></div></div><p>
It is, because it has to lock the bitmap, select the line you&#39;re writing to, then write the pixel, then deselect the line, and finally unlock the bitmap. Doing all that yourself to ensure they aren&#39;t called more than necessary will be quicker.</p><p>Using line is a bit faster, but will only work on memory bitmaps.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If he wants to play directly with pixels then won&#39;t there be a severe performance loss unless the bitmap is memory bitmap?
</p></div></div><p>
Depends. The driver can have it set so you write to system RAM, then will push it all out to the card in one shot when you read/unwrite. In such cases, it won&#39;t be much slower as long as you keep read/write switches to a minimal (there&#39;ll also be additional gains from having no intermediary bitmap (page flipping or dirty rectangles on the screen), or by having accelerated vram-&gt;vram blits).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Fri, 22 Dec 2006 22:00:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I am not drawing to the screen, and I am already checking to see if it&#39;s inside.  Should I use direct access? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><p>[EDIT]<br />Is it possible to just access the pixels like this:
</p><div class="source-code snippet"><div class="inner"><pre>bmp-&gt;line<span class="k2">[</span>y<span class="k2">]</span><span class="k2">[</span>x<span class="k2">]</span> <span class="k3">=</span> color<span class="k2">;</span>
</pre></div></div><p>?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Fri, 22 Dec 2006 22:14:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Kikaru:<br />If the BITMAP is a memory bitmap, and it&#39;s an 8 bit per pixel BITMAP, that&#39;s all you need.  If it&#39;s not an 8 bit per pixel bitmap, it&#39;s almost that simple, but you need to add a cast (like: ((short*)(bmp-&gt;line[y]))[x] = color; for 15 or 16 bit per pixel modes).  Any bitmap created with create_bitmap is a memory bitmap.  The screen usually is not a memory bitmap though.  However, the screen usually is a linear bitmap, which can be used almost as fast via a slightly different technique.  </p><p>This is all in the docs, under &quot;Direct access to video memory&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (orz)</author>
		<pubDate>Sat, 23 Dec 2006 08:09:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I can definitely recommend the line[] method. When I began writing my ProSonic game engine, I was still learning about Allegro. I tried many methods of drawing to the screen, but that is by far the fastest way! It did wonders for the performance in my game engine. It is indeed faster than _putpixel.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Damian Grove)</author>
		<pubDate>Sat, 23 Dec 2006 10:46:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><span class="source-code"><span class="k2">(</span><span class="k2">(</span>uintN_t<span class="k3">*</span><span class="k2">)</span><span class="k2">(</span>bmp-&gt;line<span class="k2">[</span>y<span class="k2">]</span><span class="k2">)</span><span class="k2">)</span><span class="k2">[</span>x<span class="k2">]</span> <span class="k3">=</span> color<span class="k2">;</span></span>where N is the color depth.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sat, 23 Dec 2006 10:50:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
<span class="source-code"><span class="k2">(</span><span class="k2">(</span>uintN_t<span class="k3">*</span><span class="k2">)</span><span class="k2">(</span>bmp-&gt;line<span class="k2">[</span>y<span class="k2">]</span><span class="k2">)</span><span class="k2">)</span><span class="k2">[</span>x<span class="k2">]</span> <span class="k3">=</span> color<span class="k2">;</span></span><br />where N is the color depth.
</p></div></div><p>
That won&#39;t work for 24-bit..
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 23 Dec 2006 11:20:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If I recall correctly, there was an Allegro example, &#39;exflame.c&#39;, which compared three methods of writing pixels.<br />First using &#39;getpixel()&#39; and &#39;putpixel()&#39;, which was slow, then using direct memory access, which was faster, then using block memory copy operations, which was by far faster than both.<br />Perhaps you should check that out?</p><p>If you&#39;re going to use direct memory access, there&#39;s a chapter devoted to it in the manual, called &#39;Direct access to video memory&#39;. You could also try the following optimizations:</p><p>Instead of &#39;for(x = 0; x &lt; SCREEN_W; x++)&#39; do &#39;for(x = SCREEN_W; x--; )&#39;.<br />This will save the processor the need of subtracting &#39;x&#39; from &#39;SCREEN_W&#39; every time.</p><p>Try unrolling the loops in blocks of 8. (I think every resolution can be divided by 8.)<br />For a resolution of 800x600, it could save you 472,500 operations of incrementing the counters and testing for loop conditions per frame.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mr. Big)</author>
		<pubDate>Sat, 23 Dec 2006 17:47:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I read part of the manual, but it tries to give me some functions that are not defined in my version of allegro (4.2) <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>Do I need to call <tt>lock_bitmap()</tt> or something before I do that?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Sat, 23 Dec 2006 20:34:09 +0000</pubDate>
	</item>
</rss>
