<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Drawing large numbers of primitives</title>
		<link>http://www.allegro.cc/forums/view/609524</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 12 Feb 2012 21:57:10 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Essentially I want to draw a matrix to the screen, and quickly.</p><p>The image will always be composed of squares at some resolution. I have been using primitives to draw them up to now, and this has performed well for my test cases of a 600x600px display and drawing a 100x100 matrix (ie each square is 6x6 px). Now that I&#39;m moving to trying to draw larger examples, it seems that it isn&#39;t running nearly as well. The 100x100 matrix is able to be drawn at hundreds of fps easily, but trying to draw a 600x600 matrix to the same 600x600 px display as before has the framerate tank to about 0.5fps.</p><p>I&#39;m confused as to why the same quantity of drawing takes much more resources when done in more steps. It seems that it is not possible to lock a bitmap and then draw with primitives, is this actually the case or have I made a mistake there? (I assume that there isn&#39;t the issue of loading/unloading the bitmap from the video card repeatedly by default using primitives, so locking would at best be redundant)</p><p>I could go to drawing pixel by pixel manually with locking, but I&#39;m not sure if that would actually be faster and I don&#39;t want to charge in and implement it if there is something obvious I&#39;m missing. What is the best way to handle this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (R S)</author>
		<pubDate>Sun, 12 Feb 2012 02:00:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In my calculations you do 36 times as much drawing, not the same amount <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> The only things that cause a slowdown I can think of are: you&#39;re using a weird way of calculating the the rectangle coordinates; or the so-called &#39;matrix&#39; is actually coupled with a large data-structure that forces memory paging when allocating 360,000.<br />I am so smart.<br />The only other ways I can think of to draw a grid of squares are:<br />- Use a transformation on a single square. (Read the manual)<br />- Use the primitives low-level drawing routines. (Read the manual)<br />- Use putpixel on a bitmap and scale it up.<br />I&#39;d go with the third, because that&#39;s the only one I understand =_=
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Sun, 12 Feb 2012 04:09:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Nowadays I never know whether questions are referring to old-school Allegro (memory/video bitmaps), or the latest version of Allegro built on OpenGL, but I&#39;m going to assume the latter...</p><p>By &quot;primitives&quot; do you mean that you&#39;re using the some Allegro primitives library? If so then the issue is probably that you&#39;re introducing all sorts of unnecessary overhead.  Drawing 360,000 quads using a plain OpenGL quadstrip should be lightning fast on any vaguely decent machine even if you&#39;re doing it straight from immediate mode. However, calling some crappy primitives library method call 360,000 times (which at the very least is going to include a GL.Begin / GL.End, and may even include modelview transformations) is not going to be fast.</p><p>Basically, if you want to do things properly learn OpenGL instead of relying on libraries built on top of it. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Sun, 12 Feb 2012 04:23:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I meant the same amount of drawing as in the same number of pixels get drawn, just in more parts. In both cases a 600x600 pixel display was completely redrawn, one in 6x6 squares, the other in 1x1 squares.</p><p>The actual accessing of the data from the matrix isn&#39;t the slow part here, as I can read it all to a standard array in preparation to dump that to a text file in vastly less time than this is now taking.</p><p>By &quot;Use the primitives low-level drawing routines&quot; thats what I&#39;m already doing as far as I know, using al_draw_filled_rectangle(...).</p><p>I&#39;ve tried using the primitives drawing both directly onto the display backbuffer and also onto an intermediary bitmap and both gave the same speed results.</p><p>I&#39;ll have a go on doing putpixel on a locked bitmap and scaling up. This may well be the most efficient solution if there isn&#39;t a way to do it directly with the primitives.</p><p>EDIT: James, if there isn&#39;t a way to solve this issue within allegro, is it possible to incorporate some OpenGL directly into an allegro project for something as simple as drawing a shedload of squares to an existing allegro display? If so, how?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (R S)</author>
		<pubDate>Sun, 12 Feb 2012 04:35:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/609524/947126#target">R S</a> said:</div><div class="quote"><p>is it possible to incorporate some OpenGL directly into an allegro project for something as simple as drawing a shedload of squares to an existing allegro display? If so, how?</p></div></div><p>

There&#39;s probably nothing stopping you calling OpenGL directly; however, if you want to maintain compatibility with other rendering backends (i.e. software and DirectX), it&#39;s going to be a bit more work.</p><p>Either way, what you are doing sounds very basic and should be trivial in whatever.</p><p>[edit]</p><p>Just had a look at the primitives code, and yeah: looooads of overhead. You certainly don&#39;t want to be using it if you&#39;re drawing more than a few thousand primitives.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Sun, 12 Feb 2012 05:47:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/609524/947126#target">R S</a> said:</div><div class="quote"><p> By &quot;Use the primitives low-level drawing routines&quot; thats what I&#39;m already doing as far as I know, using al_draw_filled_rectangle(...).</p></div></div><p>No, low level routines are <span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_prim"><span class="a">al_draw_prim</span></a></span> and <span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_indexed_prim"><span class="a">al_draw_indexed_prim</span></a></span>. The others are high level routines. In your case you&#39;d use <span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_prim"><span class="a">al_draw_prim</span></a></span> with a ALLEGRO_PRIM_TRIANGLE_LIST primitive type. See ex_prim for usage.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Sun, 12 Feb 2012 05:50:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/609524/947135#target">James Lohr</a> said:</div><div class="quote"><p>
Just had a look at the primitives code, and yeah: looooads of overhead. You certainly don&#39;t want to be using it if you&#39;re drawing more than a few thousand primitives.
</p></div></div><p>

What do you mean? What part of the primitives addon? You can use al_draw_prim with ALLEGRO_PRIM_POINT_LIST and it boils down to a single glDrawArrays call...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 12 Feb 2012 05:52:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/609524/947137#target">Trent Gamblin</a> said:</div><div class="quote"><p>You can use al_draw_prim with ALLEGRO_PRIM_POINT_LIST and it <b><i>boils down</i></b> to a single glDrawArrays call..</p></div></div><p>

You said it. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>The point is that not all of the setup (texture binding, glEnables/Disables and transformations) is always necessary. I&#39;m not saying that there is anything wrong with the primitives addon by the way. This is more of a case of it being misused, but for drawing a large number of very small primitives, it is going to be very inefficient, even using the &quot;low-level&quot; routines like al_draw_prim.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Sun, 12 Feb 2012 16:56:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The performance advantage may be negligible though. Basically something like:</p><p><span class="source-code">lots of <a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a> calls <span class="k3">&lt;</span><span class="k3">&lt;</span> <a href="http://www.allegro.cc/manual/al_draw_prim"><span class="a">al_draw_prim</span></a> <span class="k3">&lt;</span><span class="k3">=</span> glDrawArrays</span></p><p>Of course if with OpenGL you would try to keep all the vertices on the GPU in a VBO or things like that, using it directly will be an advantage.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sun, 12 Feb 2012 19:08:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/609524/947164#target">James Lohr</a> said:</div><div class="quote"><p>
The point is that not all of the setup (texture binding, glEnables/Disables and transformations) is always necessary.
</p></div></div><p>

It&#39;s done once per call. You seem to be saying that for larger numbers of primitives it&#39;s less efficient, but it gets more efficient the more primitives you draw. The only thing better is a vbo like Elias said, but you can&#39;t always use vbos.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 12 Feb 2012 19:38:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m saying that calling al_draw_prim repeatedly is much slowing that calling the OpenGL directly for the particular problem at hand.</p><p>For example, assuming what the OP is trying to do is to draw a matrix of solid squares, then this could be done with a single call to GL_QUADS, setting the colour every 4th vertex.  This is going to be much much faster than calling al_draw_prim individually for every quad.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/609524/947165#target">Elias</a> said:</div><div class="quote"><p>The performance advantage may be negligible though.</p></div></div><p>

It&#39;s not though, hence this thread.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Sun, 12 Feb 2012 21:04:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, but you&#39;re comparing apples and oranges... drawing a bunch of GL_QUADS directly with gl calls <i>is</i> going to be negligibly different from al_draw_prim with ALLEGRO_PRIM_TRIANGLE_LIST.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 12 Feb 2012 21:08:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sure, drawing it all using a vertex array, an ALLEGRO_PRIM_TRIANGLE_LIST and a single call to al_draw_prim is going to do the job: it would appear that al_draw_prim is a little more flexible than I gave it credit.<br /> <br />I had glanced at the code and in the absence of GL_QUADS, I had made a few unfair assumptions of what was there.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Sun, 12 Feb 2012 21:51:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well we don&#39;t support GL_QUADS simply because not all of the platforms we target (OpenGL ES) support quads.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 12 Feb 2012 21:57:10 +0000</pubDate>
	</item>
</rss>
