<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[glsl]Using pixel shader with backbuffer</title>
		<link>http://www.allegro.cc/forums/view/607466</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 30 May 2011 00:56:43 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I started with the ex_opengl_pixelshader and added a timer to regulate fps. I&#39;m now trying to get it to use the backbuffer rather than shading a memory bitmap like the example.</p><p>I first changed the bitmap for a call to al_get_backbuffer, this, obviously resulted in crazy flicker because of double buffer. So in the render loop I added:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>   glUseProgramObjectARB<span class="k2">(</span>tinter<span class="k2">)</span><span class="k2">;</span>
<span class="number">  2</span>      loc <span class="k3">=</span> glGetUniformLocationARB<span class="k2">(</span>tinter, <span class="s">"ratio"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  3</span>      glUniform1fARB<span class="k2">(</span>loc, ratio<span class="k2">)</span><span class="k2">;</span>
<span class="number">  4</span>      loc <span class="k3">=</span> glGetUniformLocationARB<span class="k2">(</span>tinter, <span class="s">"r"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span>      glUniform1fARB<span class="k2">(</span>loc, r<span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>      loc <span class="k3">=</span> glGetUniformLocationARB<span class="k2">(</span>tinter, <span class="s">"g"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>      glUniform1fARB<span class="k2">(</span>loc, g<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>      loc <span class="k3">=</span> glGetUniformLocationARB<span class="k2">(</span>tinter, <span class="s">"b"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span>      glUniform1fARB<span class="k2">(</span>loc, b<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>
<span class="number"> 11</span>    loc <span class="k3">=</span> glGetUniformLocationARB<span class="k2">(</span>tinter, <span class="s">"backBuffer"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span>    glUniform1iARB<span class="k2">(</span>loc, <a href="http://www.allegro.cc/manual/al_get_opengl_texture"><span class="a">al_get_opengl_texture</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_backbuffer"><span class="a">al_get_backbuffer</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_current_display"><span class="a">al_get_current_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>      glUseProgramObjectARB<span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span>
<span class="number"> 15</span>      <a href="http://www.allegro.cc/manual/al_flip_display"><span class="a">al_flip_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>

But this still resulted in massive flicker. What am I doing wrong?</p><p>Thanks
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Sun, 29 May 2011 03:00:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Here&#39;s a brief lesson on how pixel shaders work. First you want to set the target bitmap (your backbuffer in this case). That does not get passed into the shader (ex_opengl_shader might be wrong, I didn&#39;t look at it). The sampler(s) you set are the textures you want to draw to the target. Now what happens is for each destination pixel, your fragment shader function gets called. You use the texture coordinates (gl_TexCoord or pass them as varying&#39;s into the fragment shader from the vertex shader) to look up pixels in the texture your drawing. Then you do whatever you want with those pixels/colors and when you&#39;re done you set gl_FragColor to the result. You have no control over which pixel gets written, you simply process &quot;whatever&quot; destination pixel you&#39;re given (You can &quot;find out&quot; which pixel you&#39;re drawing with gl_FragCoord and that is sometimes useful for some algorithms too).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 29 May 2011 03:05:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah Thanks for the excellent explanation, it really put things into perspective for me <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Sun, 29 May 2011 03:26:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is it possible to get pixel information about the target bitmap when drawing to it?  Or would you have to do something, like, copy the target bitmap to another bitmap and use it along with the image you&#39;re drawing?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Sun, 29 May 2011 08:54:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You cannot, in a pixel shader, use the same texture for both destination and source (if I understood your question correctly, Mark). This is because of the concurrent nature of pixel shaders. It&#39;s similar to threading, in that the source may be different between reads.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Erin Maus)</author>
		<pubDate>Sun, 29 May 2011 09:04:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think the question was can you read the destination regardless of its also the source or not. Well Aaron may have hinted at that anyway as the only way it would seem remotely possible is to also pass it in as a sampler, but alas, that does not work.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 29 May 2011 09:46:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is it possible with Allegro to set the fragment shader target to the front buffer because with the backbuffer it does each fragment written to it, but in my case I want to do post processing to the final result as a whole without blitting to an off screen bitmap first.</p><p>Thanks
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Sun, 29 May 2011 18:48:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What is the problem with using an off-screen buffer?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sun, 29 May 2011 19:12:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well there&#39;s no need to waste memory and sync during resize if it is not needed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Sun, 29 May 2011 19:15:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Consult the First Rule of Program Optimization. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Sun, 29 May 2011 19:23:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s not that I&#39;m trying to do premature optimization, I just thought it was possible to do this. If the proper way is to blit to another buffer first then I will by all means do so <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />, unless you mean don&#39;t use shaders?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Sun, 29 May 2011 19:27:22 +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/607466/919368#target">gnolam</a> said:</div><div class="quote"><p>Consult the First Rule of Program Optimization. </p></div></div><p>Do not talk about Fi^H^HProgram Optimization?</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/607466/919365#target">jmasterx</a> said:</div><div class="quote"><p>Is it possible with Allegro to set the fragment shader target to the front buffer because with the backbuffer it does each fragment written to it, but in my case I want to do post processing to the final result as a whole without blitting to an off screen bitmap first.</p></div></div><p>The hardware provides accelerated drawing, and accelerated page-flipping. By default every context includes at least 2 pages of vram for tear free drawing.</p><p>If you were drawing direct to the front buffer (which isn&#39;t always possible iirc), you&#39;d get a crazy amount of tearing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 29 May 2011 23:10: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/607466/919385#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>If you were drawing direct to the front buffer (which isn&#39;t always possible iirc), you&#39;d get a crazy amount of tearing.
</p></div></div><p>

Oh of course, but I wouldn&#39;t be doing that, my goal would be to somehow blit everything to the backbuffer then when the page gets flipped it goes through my shader, but it seems like blitting to a bitmap first, rendering to texture essentially, is the ideal solution.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Mon, 30 May 2011 00:06:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, you can&#39;t do that AFAIA because though Allegro treats the backbuffer and bitmaps the same, the backbuffer is not a texture, just a dummy &quot;fake&quot; bitmap.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Mon, 30 May 2011 00:12:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Cool, by rendering into another buffer first, resizing on windows is somehow dynamic and does not stretch!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Mon, 30 May 2011 00:56:43 +0000</pubDate>
	</item>
</rss>
