<?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</title>
	<atom:link href="http://www.gamerendering.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gamerendering.com</link>
	<description></description>
	<lastBuildDate>Mon, 06 May 2013 18:26:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Great free graphics programming books!</title>
		<link>http://www.gamerendering.com/2009/12/10/great-free-graphics-programming-books/</link>
		<comments>http://www.gamerendering.com/2009/12/10/great-free-graphics-programming-books/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 12:25:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Geometry Shader]]></category>
		<category><![CDATA[GPU]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Pixel Shader]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Vertex Shader]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=769</guid>
		<description><![CDATA[Here is a list of good books that all graphics programmer&#8217;s should read. They are a little dated now, but FREE!                      Feel free to mention in the comments if you know other free books online.]]></description>
				<content:encoded><![CDATA[<p>Here is a list of good books that all graphics programmer&#8217;s should read. They are a little dated now, but <strong>FREE</strong>!</p>
<p><a href="http://wiki.gamedev.net/index.php/D3DBook:Book_Cover"></a><a href="http://wiki.gamedev.net/index.php/D3DBook:Book_Cover"><img class="alignnone size-medium wp-image-785" title="Programming Vertex, Geometry, and Pixel Shaders" src="http://www.gamerendering.com/wp-content/uploads/programmingVGPS2-317x400.jpg" alt="Programming Vertex, Geometry, and Pixel Shaders" width="222" height="280" /></a>     <a href="http://tog.acm.org/resources/shaderx/Direct3D.ShaderX.Vertex.and.Pixel.Shader.Tips.and.Tricks_Wolfgang.F.Engel_Wordware.Pub_2002.pdf"><img class="size-medium wp-image-770" title="ShaderX1" src="http://www.gamerendering.com/wp-content/uploads/ShaderX1-320x400.jpg" alt="ShaderX1" width="205" height="256" /></a></p>
<div class="mceTemp"><a href="http://tog.acm.org/resources/shaderx/Direct3D.ShaderX.Vertex.and.Pixel.Shader.Tips.and.Tricks_Wolfgang.F.Engel_Wordware.Pub_2002.pdf"></a></div>
<div class="mceTemp"><a href="http://tog.acm.org/resources/shaderx/Introductions_and_Tutorials_with_DirectX_9.pdf"><img class="size-medium wp-image-771" title="ShaderX2" src="http://www.gamerendering.com/wp-content/uploads/ShaderX2-264x400.jpg" alt="ShaderX2" width="214" height="324" /></a>       <a href="http://tog.acm.org/resources/shaderx/Tips_and_Tricks_with_DirectX_9.pdf"><img class="alignnone size-full wp-image-772" title="ShaderX2" src="http://www.gamerendering.com/wp-content/uploads/ShaderX2b.jpg" alt="ShaderX2" width="200" height="301" /></a></div>
<div class="mceTemp"><a href="http://tog.acm.org/resources/shaderx/Tips_and_Tricks_with_DirectX_9.pdf"></a></div>
<div class="mceTemp"><a href="http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html"><img class="alignnone size-medium wp-image-777" title="CgTutorial" src="http://www.gamerendering.com/wp-content/uploads/CgTutorial-319x400.jpg" alt="CgTutorial" width="230" height="288" /></a>     <a href="http://http.developer.nvidia.com/GPUGems/gpugems_part01.html"><img class="alignnone size-medium wp-image-778" title="GPU Gems" src="http://www.gamerendering.com/wp-content/uploads/GPUGems1-319x400.jpg" alt="GPU Gems" width="223" height="280" /></a></div>
<div class="mceTemp"><a href="http://http.developer.nvidia.com/GPUGems/gpugems_part01.html"></a></div>
<div class="mceTemp"><a href="http://http.developer.nvidia.com/GPUGems2/gpugems2_part01.html"><img class="alignnone size-medium wp-image-779" title="GPU Gems 2" src="http://www.gamerendering.com/wp-content/uploads/GPUGems2-313x400.jpg" alt="GPU Gems 2" width="200" height="256" /></a>    <a href="http://http.developer.nvidia.com/GPUGems3/gpugems3_part01.html"><img class="alignnone size-medium wp-image-780" title="GPU Gems 3" src="http://www.gamerendering.com/wp-content/uploads/GPUGems3-317x400.jpg" alt="GPU Gems 3" width="222" height="280" /></a></div>
<div class="mceTemp"><a href="http://http.developer.nvidia.com/GPUGems3/gpugems3_part01.html"></a></div>
<div class="mceTemp"><a href="http://www.glprogramming.com/red/"></a></div>
<div class="mceTemp"><em>Feel free to mention in the comments if you know other free books online.</em></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2009/12/10/great-free-graphics-programming-books/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Texture Atlas</title>
		<link>http://www.gamerendering.com/2009/12/08/texture-atlas/</link>
		<comments>http://www.gamerendering.com/2009/12/08/texture-atlas/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 11:07:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Optimizations]]></category>
		<category><![CDATA[batching]]></category>
		<category><![CDATA[mipmaps]]></category>
		<category><![CDATA[Nvidia]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Texture Atlas]]></category>
		<category><![CDATA[Texturing]]></category>
		<category><![CDATA[tiling]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=742</guid>
		<description><![CDATA[Texture atlas [1][2] is a technique to group smaller textures into a larger texture. This decreases the number of state switches [3] a renderer needs to do and therefore often increases performance. Texture atlases have been used for a long time in the video game industry for sprite animations. When using texture atlases, the uv-coordinates of [...]]]></description>
				<content:encoded><![CDATA[<p style="MARGIN-BOTTOM: 0cm; FONT-WEIGHT: normal">Texture atlas [1][2] is a technique to group smaller textures into a larger texture. This decreases the number of state switches [3] a renderer needs to do and therefore often increases performance. Texture atlases have been used for a long time in the video game industry for sprite animations. When using texture atlases, the uv-coordinates of the models have to be changed so the original 0..1 map to the textures tile in the atlas. Grouping of textures can be done manually by texture artists or with tools. The texture coordinate system can be changed to map the new texture in a tool, or in the shader at run-time.</p>
<div class="mceTemp">
<dl id="attachment_743" class="wp-caption " style="width: 290px;">
<dt class="wp-caption-dt"><a href="http://www.gamerendering.com/wp-content/uploads/textureatlas.jpg"><img class="size-full wp-image-743" title="An example texture atlas" src="http://www.gamerendering.com/wp-content/uploads/textureatlas.jpg" alt="An example texture atlas" width="280" height="280" /></a></dt>
</dl>
</div>
<div class="mceTemp"><em>Image from article </em><a href="http://www.gamasutra.com/features/20060126/ivanov_01.shtml"><em>[2]</em></a></div>
<p style="MARGIN-BOTTOM: 0cm; FONT-WEIGHT: normal">There are some limitations with using texture atlases compared to normal textures. First of all, all texture coordinates must initially be within 0..1 range. So for example, no “free” tiling can be used. The other problem is bleeding between tiles in the atlas when doing filtering, for example when using mipmaps.</p>
<p style="MARGIN-BOTTOM: 0cm; FONT-WEIGHT: normal">Some additional information from Ivan-Assen Ivanov, author of article [2].</p>
<blockquote>
<p style="MARGIN-BOTTOM: 0cm; FONT-WEIGHT: normal"><em>&#8221; &#8211; separate textures hurts not only batching (in facts, it hurts batching less than years ago), but also memory – as there is a certain per-texture overhead. This is especially painful on consoles – on PCs, the overhead is still there, I guess, but the driver hides it from you. The exact numbers are under NDA, of course, but on an old, unreleased project, we saved about 9 MB by atlas-sing a category of textures we didn’t atlas before.</em></p>
<p><em>- vertex interpolators are expensive! make sure you measure the remapping from 0..1 to the actual UVs in the atlas both in the vertex and in the pixel shader. Sounds counterintuitive, but on modern GPUs and with dense geometry, pixel shader is actually faster.&#8221;</em> </p></blockquote>
<p style="MARGIN-BOTTOM: 0cm; FONT-WEIGHT: normal">[1] “Improve Batching Using Texture Atlases” <a href="http://http.download.nvidia.com/developer/NVTextureSuite/Atlas_Tools/Texture_Atlas_Whitepaper.pdf">http://http.download.nvidia.com/developer/NVTextureSuite/Atlas_Tools/Texture_Atlas_Whitepaper.pdf</a></p>
<p style="MARGIN-BOTTOM: 0cm; FONT-WEIGHT: normal">[2] &#8220;Practical Texture Atlases&#8221; (borrowed image from this page)<br />
<a href="http://www.gamasutra.com/features/20060126/ivanov_01.shtml">http://www.gamasutra.com/features/20060126/ivanov_01.shtml</a></p>
<p style="MARGIN-BOTTOM: 0cm; FONT-WEIGHT: normal">[3] &#8220;Batch, Batch, Batch: What Does It Really Mean?&#8221;<br />
<a href="http://developer.nvidia.com/docs/io/8230/batchbatchbatch.pdf">http://developer.nvidia.com/docs/io/8230/batchbatchbatch.pdf</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2009/12/08/texture-atlas/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<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>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2009/12/07/vpos/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Position Reconstruction</title>
		<link>http://www.gamerendering.com/2009/12/07/position-reconstruction/</link>
		<comments>http://www.gamerendering.com/2009/12/07/position-reconstruction/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 15:34:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cameras]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Deferred Rendering]]></category>
		<category><![CDATA[Deferred Shading]]></category>
		<category><![CDATA[HLSL]]></category>
		<category><![CDATA[Position]]></category>
		<category><![CDATA[Position Reconstruction]]></category>
		<category><![CDATA[Screen Space]]></category>
		<category><![CDATA[View Space]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=751</guid>
		<description><![CDATA[There are many occasions when the fragment position in world space needs to be reconstructed from a texture holding the scene depth (depth texture). One example of use is in deferred rendering when trying to decrease memory usage by not saving the position but instead only the depth. This will result in one channel of [...]]]></description>
				<content:encoded><![CDATA[<p style="MARGIN-BOTTOM: 0cm">There are many occasions when the fragment position in world space needs to be reconstructed from a texture holding the scene depth (depth texture). One example of use is in deferred rendering when trying to decrease memory usage by not saving the position but instead only the depth. This will result in one channel of data, instead of three channels needed when saving the whole position.</p>
<p style="MARGIN-BOTTOM: 0cm"> </p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/scenedepth.jpg"><img class="size-medium wp-image-755" title="Viewspace scene depth texture" src="http://www.gamerendering.com/wp-content/uploads/scenedepth-400x300.jpg" alt="Viewspace scene depth" width="400" height="300" /></a></div>
<p style="MARGIN-BOTTOM: 0cm">There are different ways to save the depth. The most popular are view space depth and screen space depth. Saving depth in view space instead of screen space gives two advantages. It&#8217;s faster, and it gives better precision because it&#8217;s linear in view space.</p>
<p style="MARGIN-BOTTOM: 0cm">This is how <strong>screen space depth</strong> can be rendered in HLSL:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p75111"><td class="code" id="p751code11"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> VS_OUTPUT
<span style="color: #009900;">&#123;</span>
   float4 Pos<span style="color: #339933;">:</span> POSITION<span style="color: #339933;">;</span>
   float4 posInProjectedSpace<span style="color: #339933;">:</span> TEXCOORD0<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// vertex shader</span>
VS_OUTPUT vs_main<span style="color: #009900;">&#40;</span> float4 Pos<span style="color: #339933;">:</span> POSITION <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   VS_OUTPUT Out <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>VS_OUTPUT<span style="color: #009900;">&#41;</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
   Out.<span style="color: #202020;">Pos</span> <span style="color: #339933;">=</span> mul<span style="color: #009900;">&#40;</span>Pos<span style="color: #339933;">,</span>matWorldViewProjection<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Out.<span style="color: #202020;">posInProjectedSpace</span> <span style="color: #339933;">=</span> Out.<span style="color: #202020;">Pos</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> Out<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// pixel shader</span>
float4 ps_main<span style="color: #009900;">&#40;</span> VS_OUTPUT Input <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> COLOR
<span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">float</span> depth <span style="color: #339933;">=</span> Input.<span style="color: #202020;">posInProjectedSpace</span>.<span style="color: #202020;">z</span> <span style="color: #339933;">/</span> Input.<span style="color: #202020;">posInProjectedSpace</span>.<span style="color: #202020;">w</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> depth<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The HLSL pixel shader below shows how the position can be reconstructed from the depth map stored with the code above. Although this is one of the slowest ways of doing position reconstruction since it requires a matrix multiplication.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p75112"><td class="code" id="p751code12"><pre class="c" style="font-family:monospace;">float4 ps_main<span style="color: #009900;">&#40;</span>float2 vPos <span style="color: #339933;">:</span> VPOS<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> COLOR0
<span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">float</span> depth <span style="color: #339933;">=</span> tex2D<span style="color: #009900;">&#40;</span>depthTexture<span style="color: #339933;">,</span>vPos<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: #009900;">&#41;</span>.<span style="color: #202020;">r</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// scale it to -1..1 (screen coordinates)</span>
   float2 projectedXY <span style="color: #339933;">=</span> vPos<span style="color: #339933;">*</span>fInverseViewportDimensions<span style="color: #339933;">*</span><span style="color: #0000dd;">2</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
   projectedXY.<span style="color: #202020;">y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>projectedXY.<span style="color: #202020;">y</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// create the position in screen space</span>
   float4 pos <span style="color: #339933;">=</span> float4<span style="color: #009900;">&#40;</span>projectedXY<span style="color: #339933;">,</span>depth<span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// transform position into world space by multiplication with the inverse view projection matrix</span>
   pos <span style="color: #339933;">=</span> mul<span style="color: #009900;">&#40;</span>pos<span style="color: #339933;">,</span>matViewProjectionInverse<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
   <span style="color: #666666; font-style: italic;">// make it homogeneous</span>
   pos <span style="color: #339933;">/=</span> pos.<span style="color: #202020;">w</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// result will be (x,y,z,1) in world space</span>
&nbsp;
   <span style="color: #b1b100;">return</span> pos<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// for now, just render it out</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p style="MARGIN-BOTTOM: 0cm">To reconstruct<strong> depth from view space</strong>, a ray from the camera position to the frustum far plane is needed. For a full screen quad, this ray can be precalculated for the four corners and passed to the shader. This is how the computer game Crysis did it [1] . But for arbitrary geometry, as needed in deferred rendering, the ray must be calculated in the shaders [2] .</p>
<p style="MARGIN-BOTTOM: 0cm">[1] &#8220;Finding next gen: CryEngine 2&#8243;<br />
<a href="http://ati.amd.com/developer/gdc/2007/mittring-finding_nextgen_cryengine2(siggraph07).pdf">http://ati.amd.com/developer/gdc/2007/mittring-finding_nextgen_cryengine2(siggraph07).pdf</a></p>
<p style="MARGIN-BOTTOM: 0cm">[2] “Reconstructing Position From Depth, Continued”<br />
<a href="http://mynameismjp.wordpress.com/2009/05/05/reconstructing-position-from-depth-continued/">http://mynameismjp.wordpress.com/2009/05/05/reconstructing-position-from-depth-continued/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2009/12/07/position-reconstruction/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
