<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Game Rendering &#187; Render-to-Texture</title>
	<atom:link href="http://www.gamerendering.com/tag/render-to-texture/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gamerendering.com</link>
	<description></description>
	<lastBuildDate>Thu, 21 Jan 2010 01:32:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>VPOS</title>
		<link>http://www.gamerendering.com/2009/12/07/vpos/</link>
		<comments>http://www.gamerendering.com/2009/12/07/vpos/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 15:35:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Shaders]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Pixel Shader]]></category>
		<category><![CDATA[Render-to-Texture]]></category>
		<category><![CDATA[Rendering]]></category>
		<category><![CDATA[SM3]]></category>
		<category><![CDATA[VPOS]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=735</guid>
		<description><![CDATA[Starting with DirectX Pixel Shader Model 3.0 there exist an input type called VPOS. It&#8217;s the current pixels position on the screen and it&#8217;s automatically generated. This can be useful when sampling from a previously rendered texture when rendering an arbitrarily shaped mesh to the screen. To do this, we need uv-coords that represents where [...]]]></description>
			<content:encoded><![CDATA[<p>Starting with DirectX Pixel Shader Model 3.0 there exist an input type called VPOS. It&#8217;s the current pixels position on the screen and it&#8217;s automatically generated. This can be useful when sampling from a previously rendered texture when rendering an arbitrarily shaped mesh to the screen. To do this, we need uv-coords that represents where to sample on the texture. These coordinates can be gained by simply dividing VPOS with the screen dimensions.<br />
When working with older hardware, that doesn&#8217;t support shader model 3.0, there is a need to manually create the VPOS in the vertex shader and pass it to the fragment shader as a TEXCOORD. This is the way to do so ( including the scaling to uv-range which manually has to be done for VPOS if you&#8217;re using it).</p>
<p><strong>Vertex Shader:</strong></p>

<div class="wp_codebox"><table width="100%" ><tr id="p7355"><td class="code" id="p735code5"><pre class="c" style="font-family:monospace;">float4x4 matWorldViewProjection<span style="color: #339933;">;</span>
float2 fInverseViewportDimensions<span style="color: #339933;">;</span>
<span style="color: #993333;">struct</span> VS_INPUT
<span style="color: #009900;">&#123;</span>
   float4 Position <span style="color: #339933;">:</span> POSITION0<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">struct</span> VS_OUTPUT
<span style="color: #009900;">&#123;</span>
   float4 Position <span style="color: #339933;">:</span> POSITION0<span style="color: #339933;">;</span>
   float4 calculatedVPos <span style="color: #339933;">:</span> TEXCOORD0<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
float4 ConvertToVPos<span style="color: #009900;">&#40;</span> float4 p <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> float4<span style="color: #009900;">&#40;</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span> float2<span style="color: #009900;">&#40;</span>p.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> p.<span style="color: #202020;">w</span><span style="color: #339933;">,</span> p.<span style="color: #202020;">w</span> <span style="color: #339933;">-</span> p.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> p.<span style="color: #202020;">w</span><span style="color: #339933;">*</span>fInverseViewportDimensions.<span style="color: #202020;">xy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> p.<span style="color: #202020;">zw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
VS_OUTPUT vs_main<span style="color: #009900;">&#40;</span> VS_INPUT Input <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   VS_OUTPUT Output<span style="color: #339933;">;</span>
   Output.<span style="color: #202020;">Position</span> <span style="color: #339933;">=</span> mul<span style="color: #009900;">&#40;</span> Input.<span style="color: #202020;">Position</span><span style="color: #339933;">,</span> matWorldViewProjection <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Output.<span style="color: #202020;">calculatedVPos</span> <span style="color: #339933;">=</span> ConvertToVPos<span style="color: #009900;">&#40;</span>Output.<span style="color: #202020;">Position</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> Output <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Pixel Shader:<br />
</strong></p>

<div class="wp_codebox"><table width="100%" ><tr id="p7356"><td class="code" id="p735code6"><pre class="c" style="font-family:monospace;">float4 ps_main<span style="color: #009900;">&#40;</span>VS_OUTPUT Input<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> COLOR0
<span style="color: #009900;">&#123;</span>
   Input.<span style="color: #202020;">calculatedVPos</span> <span style="color: #339933;">/=</span> Input.<span style="color: #202020;">calculatedVPos</span>.<span style="color: #202020;">w</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> float4<span style="color: #009900;">&#40;</span>Input.<span style="color: #202020;">calculatedVPos</span>.<span style="color: #202020;">xy</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// test render it to the screen</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The image below shows an elephant model rendered with the shader above. As can be seen, the color (red and green channels) correctly represents the uv-coords for a fullscreen quad. Since 0,0,0 = black, 1,0,0 = red, 0,1,0 = green, 1, 1,0 = yellow.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/vPosElephant.JPG"><img class="size-full wp-image-736" title="VPOS Elephant" src="http://www.gamerendering.com/wp-content/uploads/vPosElephant.JPG" alt="VPOS Elephant" width="177" height="162" /></a></div>
<div class="mceTemp">This is how the pixel shader would have looked like if VPOS were used instead (note: no special vertex shader needed in this case).</div>

<div class="wp_codebox"><table width="100%" ><tr id="p7357"><td class="code" id="p735code7"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> PS_INPUT
<span style="color: #009900;">&#123;</span>
   float2 vPos <span style="color: #339933;">:</span> VPOS<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table width="100%" ><tr id="p7358"><td class="code" id="p735code8"><pre class="c" style="font-family:monospace;">float4 ps_main<span style="color: #009900;">&#40;</span>PS_INPUT Input<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> COLOR0
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> float4<span style="color: #009900;">&#40;</span>Input.<span style="color: #202020;">vPos</span><span style="color: #339933;">*</span>fInverseViewportDimensions <span style="color: #339933;">+</span> fInverseViewportDimensions<span style="color: #339933;">*</span><span style="color:#800080;">0.5</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// test render it to the screen</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The original code, more info and proof can be found here:<br />
<a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=506573">http://www.gamedev.net/community/forums/topic.asp?topic_id=506573</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS%26amp%3Bbodytext%3DStarting%2520with%2520DirectX%2520Pixel%2520Shader%2520Model%25203.0%2520there%2520exist%2520an%2520input%2520type%2520called%2520VPOS.%2520It%2527s%2520the%2520current%2520pixels%2520position%2520on%2520the%2520screen%2520and%2520it%2527s%2520automatically%2520generated.%2520This%2520can%2520be%2520useful%2520when%2520sampling%2520from%2520a%2520previously%2520rendered%2520texture%2520when%2520rendering%2520an';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS%26amp%3Bnotes%3DStarting%2520with%2520DirectX%2520Pixel%2520Shader%2520Model%25203.0%2520there%2520exist%2520an%2520input%2520type%2520called%2520VPOS.%2520It%2527s%2520the%2520current%2520pixels%2520position%2520on%2520the%2520screen%2520and%2520it%2527s%2520automatically%2520generated.%2520This%2520can%2520be%2520useful%2520when%2520sampling%2520from%2520a%2520previously%2520rendered%2520texture%2520when%2520rendering%2520an';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Bt%3DVPOS';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS%26amp%3Bannotation%3DStarting%2520with%2520DirectX%2520Pixel%2520Shader%2520Model%25203.0%2520there%2520exist%2520an%2520input%2520type%2520called%2520VPOS.%2520It%2527s%2520the%2520current%2520pixels%2520position%2520on%2520the%2520screen%2520and%2520it%2527s%2520automatically%2520generated.%2520This%2520can%2520be%2520useful%2520when%2520sampling%2520from%2520a%2520previously%2520rendered%2520texture%2520when%2520rendering%2520an';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DStarting%2520with%2520DirectX%2520Pixel%2520Shader%2520Model%25203.0%2520there%2520exist%2520an%2520input%2520type%2520called%2520VPOS.%2520It%2527s%2520the%2520current%2520pixels%2520position%2520on%2520the%2520screen%2520and%2520it%2527s%2520automatically%2520generated.%2520This%2520can%2520be%2520useful%2520when%2520sampling%2520from%2520a%2520previously%2520rendered%2520texture%2520when%2520rendering%2520an';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Bt%3DVPOS';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DVPOS%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DVPOS%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Btitle%3DVPOS';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F12%252F07%252Fvpos%252F%26amp%3Bt%3DVPOS%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DStarting%2520with%2520DirectX%2520Pixel%2520Shader%2520Model%25203.0%2520there%2520exist%2520an%2520input%2520type%2520called%2520VPOS.%2520It%2527s%2520the%2520current%2520pixels%2520position%2520on%2520the%2520screen%2520and%2520it%2527s%2520automatically%2520generated.%2520This%2520can%2520be%2520useful%2520when%2520sampling%2520from%2520a%2520previously%2520rendered%2520texture%2520when%2520rendering%2520an';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2009/12/07/vpos/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Render Thickness</title>
		<link>http://www.gamerendering.com/2009/09/25/render-thickness/</link>
		<comments>http://www.gamerendering.com/2009/09/25/render-thickness/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 14:24:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rendering Methods]]></category>
		<category><![CDATA[Billboards]]></category>
		<category><![CDATA[Clouds]]></category>
		<category><![CDATA[Hebe]]></category>
		<category><![CDATA[Render-to-Texture]]></category>
		<category><![CDATA[Rendering]]></category>
		<category><![CDATA[Thickness]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=710</guid>
		<description><![CDATA[In [1] they describe a clever way of rendering the thickness of an object in a single pass. The method only correctly works for convex objects but this limitation isn&#8217;t that bad, the method can often be used to get the approximated thickness of concave objects as well. For example, [1] uses it to fake [...]]]></description>
			<content:encoded><![CDATA[<p style="margin-bottom: 0cm;">In [1] they describe a clever way of rendering the thickness of an object in a single pass. The method only correctly works for convex objects but this limitation isn&#8217;t that bad, the method can often be used to get the approximated thickness of concave objects as well. For example, [1] uses it to fake the light scattering in clouds rendered as billboards. The methods works like this:</p>
<p style="margin-bottom: 0cm;">The object is rendered and the distance from the near plane is saved in a color channel R. Also, the distance to the far plane is saved in channel G. By rendering with the blend color mode MIN, one will get the minimum distance from the near plane in R, and the minimum distance to the far plane in G. By using these two distances, one can easily calculate the thickness of the rendered object with the following formula (1-G) – R (if distance is scaled so one is the the distance between the clip planes). Alpha can be saved as well in the same render pass, by outputting it to the A channel. And selecting blend alpha mode ADD (color and alpha can have different modes). This will add up the alpha.</p>
<p style="margin-bottom: 0cm;">All this is done in only one pass. Just remember to clear to white before rendering.</p>
<p>The image below shows the thickness of the popular Hebe mesh rendered with this method. This model is not convex, and the problem areas are for example the arm holding the bowl. As one can see, the algorithm believes that the bowl and the shoulder are connected, and therefore believes that part of the object is the thickest.</p>
<p><a href="http://www.gamerendering.com/wp-content/uploads/Hebe.JPG"><img class="size-full wp-image-711" title="Hebe" src="http://www.gamerendering.com/wp-content/uploads/Hebe.JPG" alt="Hebe" width="328" height="493" /></a></p>
<p>[1] The Art and Technology of Whiteout<br />
<a href="http://ati.amd.com/developer/gdc/2007/ArtAndTechnologyOfWhiteout(Siggraph07).pdf">http://ati.amd.com/developer/gdc/2007/ArtAndTechnologyOfWhiteout(Siggraph07).pdf</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness%26amp%3Bbodytext%3DIn%2520%255B1%255D%2520they%2520describe%2520a%2520clever%2520way%2520of%2520rendering%2520the%2520thickness%2520of%2520an%2520object%2520in%2520a%2520single%2520pass.%2520The%2520method%2520only%2520correctly%2520works%2520for%2520convex%2520objects%2520but%2520this%2520limitation%2520isn%2527t%2520that%2520bad%252C%2520the%2520method%2520can%2520often%2520be%2520used%2520to%2520get%2520the%2520approximated%2520thickness%2520of%2520conca';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness%26amp%3Bnotes%3DIn%2520%255B1%255D%2520they%2520describe%2520a%2520clever%2520way%2520of%2520rendering%2520the%2520thickness%2520of%2520an%2520object%2520in%2520a%2520single%2520pass.%2520The%2520method%2520only%2520correctly%2520works%2520for%2520convex%2520objects%2520but%2520this%2520limitation%2520isn%2527t%2520that%2520bad%252C%2520the%2520method%2520can%2520often%2520be%2520used%2520to%2520get%2520the%2520approximated%2520thickness%2520of%2520conca';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Bt%3DRender%2520Thickness';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness%26amp%3Bannotation%3DIn%2520%255B1%255D%2520they%2520describe%2520a%2520clever%2520way%2520of%2520rendering%2520the%2520thickness%2520of%2520an%2520object%2520in%2520a%2520single%2520pass.%2520The%2520method%2520only%2520correctly%2520works%2520for%2520convex%2520objects%2520but%2520this%2520limitation%2520isn%2527t%2520that%2520bad%252C%2520the%2520method%2520can%2520often%2520be%2520used%2520to%2520get%2520the%2520approximated%2520thickness%2520of%2520conca';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DIn%2520%255B1%255D%2520they%2520describe%2520a%2520clever%2520way%2520of%2520rendering%2520the%2520thickness%2520of%2520an%2520object%2520in%2520a%2520single%2520pass.%2520The%2520method%2520only%2520correctly%2520works%2520for%2520convex%2520objects%2520but%2520this%2520limitation%2520isn%2527t%2520that%2520bad%252C%2520the%2520method%2520can%2520often%2520be%2520used%2520to%2520get%2520the%2520approximated%2520thickness%2520of%2520conca';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Bt%3DRender%2520Thickness';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DRender%2520Thickness%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DRender%2520Thickness%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Btitle%3DRender%2520Thickness';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F09%252F25%252Frender-thickness%252F%26amp%3Bt%3DRender%2520Thickness%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DIn%2520%255B1%255D%2520they%2520describe%2520a%2520clever%2520way%2520of%2520rendering%2520the%2520thickness%2520of%2520an%2520object%2520in%2520a%2520single%2520pass.%2520The%2520method%2520only%2520correctly%2520works%2520for%2520convex%2520objects%2520but%2520this%2520limitation%2520isn%2527t%2520that%2520bad%252C%2520the%2520method%2520can%2520often%2520be%2520used%2520to%2520get%2520the%2520approximated%2520thickness%2520of%2520conca';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2009/09/25/render-thickness/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Deferred Lighting</title>
		<link>http://www.gamerendering.com/2008/11/01/deferred-lightning/</link>
		<comments>http://www.gamerendering.com/2008/11/01/deferred-lightning/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 23:39:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rendering Methods]]></category>
		<category><![CDATA[Deferred Lighting]]></category>
		<category><![CDATA[Deferred Lightning]]></category>
		<category><![CDATA[Deferred Rendering]]></category>
		<category><![CDATA[Deferred Shading]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Lighting]]></category>
		<category><![CDATA[Render-to-Texture]]></category>
		<category><![CDATA[Rendering]]></category>
		<category><![CDATA[Shading]]></category>
		<category><![CDATA[SSAO]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=434</guid>
		<description><![CDATA[This is a lighting technique that lately has increased a lot in popularity. The normal way of shading is to perform the lighting calculations on a fragment when it is rasterized to the screen. This is often good but requires a lot of calculations if there are many lights. And the bad thing is that [...]]]></description>
			<content:encoded><![CDATA[<p>This is a lighting technique that lately has increased a lot in popularity. The normal way of shading is to perform the lighting calculations on a fragment when it is rasterized to the screen. This is often good but requires a lot of calculations if there are many lights. And the bad thing is that this fragment might later on be overwritten by some other fragment so the calculations might be a waste.</p>
<p>In deferred lighting (or deferred shading, or deferred rendering), you save the information about the fragment that is necessary to perform the shading (lighting) by rendering them to textures instead of doing the actual lighting calculation. When all geometry is rendered, the lighting will now be calculated only once per pixel on the screen. So no calculations will be wasted. You can perhaps say that it is some sort of a lazy evaluator.</p>
<p>The information saved per fragment is often:</p>
<ul>
<li>position ( or just depth )</li>
<li>albedo ( the diffuse texture )</li>
<li>normal</li>
<li>specular</li>
</ul>
<p>And these are sometimes also used:</p>
<ul>
<li>shininess</li>
<li>material ID (for selecting material behaviour)</li>
</ul>
<p>When all geometry has been rendered and it&#8217;s time to perform the lighting, the lights needs to be represented as something when sent to rasterization. Point lights can be drawn either as spheres or just square billboards. Directional light should be drawn as a full screen rectangle. And spotlights will be cones. Note that this shading technique allows for lights shaped in any form, not just these traditional ones.</p>
<p>The big reason for using deferred rendering is how well it scales with more lights. Another reason that it has increased in popularity lately is how nice it works with new rendering methods like SSAO and depth of field. The problem areas with deferred lighting is transparent objects and multisampling (antialiasing). If the original scene didn&#8217;t have per pixel lighting (but instead maybe vertex lightning) on the whole scene then the deferred rendering might be slower than traditional rendering.</p>
<div id="attachment_436" class="wp-caption alignnone" style="width: 314px"><a href="http://www.gamerendering.com/wp-content/uploads/1.jpg"><img class="size-medium wp-image-436" title="Deferred Lightning example" src="http://www.gamerendering.com/wp-content/uploads/1.jpg" alt="Deferred Lightning example" width="304" height="214" /></a><p class="wp-caption-text">Deferred Lighting example</p></div>
<p>Explanation of deferred lighting (and source code)<br />
<a href="http://www.beyond3d.com/content/articles/19/">http://www.beyond3d.com/content/articles/19/</a></p>
<div id="attachment_437" class="wp-caption alignnone" style="width: 338px"><a href="http://www.gamerendering.com/wp-content/uploads/3.jpg"><img class="size-medium wp-image-437" title="Deferred Rendering in S.T.A.L.K.E.R." src="http://www.gamerendering.com/wp-content/uploads/3.jpg" alt="Deferred Rendering in S.T.A.L.K.E.R." width="328" height="248" /></a><p class="wp-caption-text">Deferred Rendering in S.T.A.L.K.E.R.</p></div>
<p>Explanation of how deferred lighting was used in the game S.T.A.L.K.E.R.<br />
<a href="http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter09.html">http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter09.html</a> </p>
<div id="attachment_438" class="wp-caption alignnone" style="width: 324px"><a href="http://www.gamerendering.com/wp-content/uploads/4.jpg"><img class="size-medium wp-image-438" title="The result of the deferred rendering XNA tutorial" src="http://www.gamerendering.com/wp-content/uploads/4.jpg" alt="The result of the deferred rendering XNA tutorial" width="314" height="237" /></a><p class="wp-caption-text">The result of the deferred rendering XNA tutorial</p></div>
<p>A very good tutorial of how to implement deferred lighting in XNA 2.0. This is good reading even when you are rendering in an other API.<br />
<a href="http://www.ziggyware.com/readarticle.php?article_id=155">http://www.ziggyware.com/readarticle.php?article_id=155</a></p>
<p>A long discussion on the gamedev.net forum of pros and cons of using deferred rendering compared to traditional forward rendering.<br />
<a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=424979">http://www.gamedev.net/community/forums/topic.asp?topic_id=424979</a></p>
<p>DirectX9 implementation if deferred shading, and some optimization talk<br />
<a href="http://www.gamedev.net/reference/programming/features/shaderx2/Tips_and_Tricks_with_DirectX_9.pdf">http://www.gamedev.net/reference/programming/features/shaderx2/Tips_and_Tricks_with_ DirectX_9.pdf</a></p>
<div id="attachment_435" class="wp-caption alignnone" style="width: 248px"><a href="http://www.gamerendering.com/wp-content/uploads/2.jpg"><img class="size-medium wp-image-435" title="Deferred Lightning in Leadwerk Engine" src="http://www.gamerendering.com/wp-content/uploads/2.jpg" alt="Deferred Lightning in Leadwerk Engine" width="238" height="180" /></a><p class="wp-caption-text">Deferred Lightning in Leadwerk Engine</p></div>
<p>Info about the implementation of deferred shading in the Leadwerks Engine.<br />
<a href="http://www.leadwerks.com/files/Deferred_Rendering_in_Leadwerks_Engine.pdf">http://www.leadwerks.com/files/Deferred_Rendering_in_Leadwerks_Engine.pdf</a></p>
<div id="attachment_483" class="wp-caption alignnone" style="width: 410px"><a href="http://www.gamerendering.com/wp-content/uploads/killzone.jpg"><img class="size-medium wp-image-483" title="Deferred Lighting in Killzone 2" src="http://www.gamerendering.com/wp-content/uploads/killzone-400x253.jpg" alt="Deferred Lighting in Killzone 2" width="400" height="253" /></a><p class="wp-caption-text">Deferred Lighting in Killzone 2</p></div>
<p>A presentation about deferred lighting in the game Killzone 2:<br />
<a href="http://www.guerrilla-games.com/publications/dr_kz2_rsx_dev07.pdf">http://www.guerrilla-games.com/publications/dr_kz2_rsx_dev07.pdf</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting%26amp%3Bbodytext%3DThis%2520is%2520a%2520lighting%2520technique%2520that%2520lately%2520has%2520increased%2520a%2520lot%2520in%2520popularity.%2520The%2520normal%2520way%2520of%2520shading%2520is%2520to%2520perform%2520the%2520lighting%2520calculations%2520on%2520a%2520fragment%2520when%2520it%2520is%2520rasterized%2520to%2520the%2520screen.%2520This%2520is%2520often%2520good%2520but%2520requires%2520a%2520lot%2520of%2520calculations%2520if%2520';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting%26amp%3Bnotes%3DThis%2520is%2520a%2520lighting%2520technique%2520that%2520lately%2520has%2520increased%2520a%2520lot%2520in%2520popularity.%2520The%2520normal%2520way%2520of%2520shading%2520is%2520to%2520perform%2520the%2520lighting%2520calculations%2520on%2520a%2520fragment%2520when%2520it%2520is%2520rasterized%2520to%2520the%2520screen.%2520This%2520is%2520often%2520good%2520but%2520requires%2520a%2520lot%2520of%2520calculations%2520if%2520';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Bt%3DDeferred%2520Lighting';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting%26amp%3Bannotation%3DThis%2520is%2520a%2520lighting%2520technique%2520that%2520lately%2520has%2520increased%2520a%2520lot%2520in%2520popularity.%2520The%2520normal%2520way%2520of%2520shading%2520is%2520to%2520perform%2520the%2520lighting%2520calculations%2520on%2520a%2520fragment%2520when%2520it%2520is%2520rasterized%2520to%2520the%2520screen.%2520This%2520is%2520often%2520good%2520but%2520requires%2520a%2520lot%2520of%2520calculations%2520if%2520';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DThis%2520is%2520a%2520lighting%2520technique%2520that%2520lately%2520has%2520increased%2520a%2520lot%2520in%2520popularity.%2520The%2520normal%2520way%2520of%2520shading%2520is%2520to%2520perform%2520the%2520lighting%2520calculations%2520on%2520a%2520fragment%2520when%2520it%2520is%2520rasterized%2520to%2520the%2520screen.%2520This%2520is%2520often%2520good%2520but%2520requires%2520a%2520lot%2520of%2520calculations%2520if%2520';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Bt%3DDeferred%2520Lighting';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DDeferred%2520Lighting%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DDeferred%2520Lighting%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Btitle%3DDeferred%2520Lighting';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F01%252Fdeferred-lightning%252F%26amp%3Bt%3DDeferred%2520Lighting%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DThis%2520is%2520a%2520lighting%2520technique%2520that%2520lately%2520has%2520increased%2520a%2520lot%2520in%2520popularity.%2520The%2520normal%2520way%2520of%2520shading%2520is%2520to%2520perform%2520the%2520lighting%2520calculations%2520on%2520a%2520fragment%2520when%2520it%2520is%2520rasterized%2520to%2520the%2520screen.%2520This%2520is%2520often%2520good%2520but%2520requires%2520a%2520lot%2520of%2520calculations%2520if%2520';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/11/01/deferred-lightning/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Gaussian Blur Filter Shader</title>
		<link>http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/</link>
		<comments>http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 19:01:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Image Enhancements]]></category>
		<category><![CDATA[Blur]]></category>
		<category><![CDATA[Blur Filter]]></category>
		<category><![CDATA[Bluring Scene]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Filtering]]></category>
		<category><![CDATA[Fragment Shader]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Gaussian Blur]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[Render-to-Texture]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Textures]]></category>
		<category><![CDATA[Vertex Shader]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=295</guid>
		<description><![CDATA[There are different ways to perform blur and this is one of the most common way to do it in a shader. It&#8217;s a two step method with first a horizontal blur and then a vertical blur. By splitting the work in two directions (two passes) you can save a lot of computation.
The method can [...]]]></description>
			<content:encoded><![CDATA[<p>There are different ways to perform blur and this is one of the most common way to do it in a shader. It&#8217;s a two step method with first a horizontal blur and then a vertical blur. By splitting the work in two directions (two passes) you can save a lot of computation.</p>
<p>The method can be divided in the following parts:</p>
<ol>
<li>Render the scene you want to blur to a texture (could be downsampled)</li>
<li>Render a screen aligned quad with the horizontal blur shader to a texture</li>
<li>Render a screen aligned quad with the vertical blur shader to the screen or texture depending on what you want to use it for</li>
</ol>
<p>The following image shows how the blur works when splitted up in two directions.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/image13-bluring.jpg"><img class="size-medium wp-image-298" title="Separable blur filter" src="http://www.gamerendering.com/wp-content/uploads/image13-bluring-390x400.jpg" alt="Separable blur filter" width="390" height="400" /></a></div>
<p>Here&#8217;s the horizontal blur shader.</p>
<p>Vertex Shader (GLSL) . This shader screen align a quad with width 1. Any method to render a screen aligned quad will work. So you&#8217;re free to use other shaders.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p29513"><td class="code" id="p295code13"><pre class="c" style="font-family:monospace;">varying vec2 vTexCoord<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// remember that you should draw a screen aligned quad</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   gl_Position <span style="color: #339933;">=</span> ftransform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;;</span>
  
   <span style="color: #666666; font-style: italic;">// Clean up inaccuracies</span>
   vec2 Pos<span style="color: #339933;">;</span>
   Pos <span style="color: #339933;">=</span> sign<span style="color: #009900;">&#40;</span>gl_Vertex.<span style="color: #202020;">xy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   gl_Position <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span>Pos<span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// Image-space</span>
   vTexCoord <span style="color: #339933;">=</span> Pos <span style="color: #339933;">*</span> <span style="color:#800080;">0.5</span> <span style="color: #339933;">+</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Fragment Shader (GLSL) </p>

<div class="wp_codebox"><table width="100%" ><tr id="p29514"><td class="code" id="p295code14"><pre class="c" style="font-family:monospace;">uniform sampler2D RTScene<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the texture with the scene you want to blur</span>
varying vec2 vTexCoord<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> blurSize <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">/</span><span style="color:#800080;">512.0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// I've chosen this size because this will result in that every step will be one pixel wide if the RTScene texture is of size 512x512</span>
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   vec4 sum <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// blur in y (vertical)</span>
   <span style="color: #666666; font-style: italic;">// take nine samples, with the distance blurSize between them</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.09</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> <span style="color:#800080;">2.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.12</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.15</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.16</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.15</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> <span style="color:#800080;">2.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.12</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.09</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
&nbsp;
   gl_FragColor <span style="color: #339933;">=</span> sum<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here&#8217;s the vertical blur shader.</p>
<p>Vertex Shader (GLSL) (the same as for the blur in horizontal direction)</p>

<div class="wp_codebox"><table width="100%" ><tr id="p29515"><td class="code" id="p295code15"><pre class="c" style="font-family:monospace;">varying vec2 vTexCoord<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// remember that you should draw a screen aligned quad</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   gl_Position <span style="color: #339933;">=</span> ftransform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;;</span>
  
   <span style="color: #666666; font-style: italic;">// Clean up inaccuracies</span>
   vec2 Pos<span style="color: #339933;">;</span>
   Pos <span style="color: #339933;">=</span> sign<span style="color: #009900;">&#40;</span>gl_Vertex.<span style="color: #202020;">xy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   gl_Position <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span>Pos<span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// Image-space</span>
   vTexCoord <span style="color: #339933;">=</span> Pos <span style="color: #339933;">*</span> <span style="color:#800080;">0.5</span> <span style="color: #339933;">+</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Fragment Shader (GLSL) </p>

<div class="wp_codebox"><table width="100%" ><tr id="p29516"><td class="code" id="p295code16"><pre class="c" style="font-family:monospace;">uniform sampler2D RTBlurH<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this should hold the texture rendered by the horizontal blur pass</span>
varying vec2 vTexCoord<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> blurSize <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">/</span><span style="color:#800080;">512.0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   vec4 sum <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// blur in y (vertical)</span>
   <span style="color: #666666; font-style: italic;">// take nine samples, with the distance blurSize between them</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.09</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> <span style="color:#800080;">2.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.12</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.15</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.16</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.15</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> <span style="color:#800080;">2.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.12</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.09</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
&nbsp;
   gl_FragColor <span style="color: #339933;">=</span> sum<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And this is a scene without blur.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/preblurshader.jpg"><img class="size-medium wp-image-297" title="Scene before bluring" src="http://www.gamerendering.com/wp-content/uploads/preblurshader-395x400.jpg" alt="Scene before bluring" width="357" height="385" /></a></div>
<p>And this is the same scene but with gaussian blur.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/postblurshader.jpg"><img class="size-medium wp-image-296" title="Blured Scene" src="http://www.gamerendering.com/wp-content/uploads/postblurshader.jpg" alt="Blured Scene" width="356" height="334" /></a></div>
<p>You can tweak the blur radius to change the size of the blur and change the number of samples in each direction.</p>
<p>Cost for separable blur shader : 9+9 = 18 (number of texture samples)<br />
Cost for shader if blured in one pass: 9*9 = 81 (number of texture samples)<br />
So splitting up in two directions saves a lot.</p>
<p>The gaussian weights are calculated accordingly to the gaussian function with standard deviation of 2.7. These calculations were done in the excel document found [2].</p>
<p>Here&#8217;s a description of blur shaders and other image processing shaders in DirectX:<br />
[1] <a href="http://ati.amd.com/developer/shaderx/ShaderX2_AdvancedImageProcessing.pdf">http://ati.amd.com/developer/shaderx/ShaderX2_AdvancedImageProcessing.pdf</a></p>
<p>More info about calculating weights for separable gaussian blur:<br />
[2] <a href="http://theinstructionlimit.com/?p=40">http://theinstructionlimit.com/?p=40</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Bbodytext%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Bnotes%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Bt%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Bannotation%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Bt%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DGaussian%2520Blur%2520Filter%2520Shader%2520%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Bt%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
