   [DX/HLSL]Shadow map from Z-Map
  Hi all;I've been implementing shadow map for my Deferred Shading demo. I want to fill my shadow-map texture using my existing depth-map texture.While filling z-map tex, I'm multiplying vertex local space position with World x View x Proj matrix, then writing z/w. I can reconstruct vertex World Space position with this (by using a Full-Screen Quad) :<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>float4 PS (float2 t : TEXCOORD0) : COLOR0{  …   half zw = tex2D (smpZMap, t);  half x = <span class="cpp-number">2</span> * t.x - <span class="cpp-number">1</span>;  half y = <span class="cpp-number">2</span> * (<span class="cpp-number">1</span> - t.y) - <span class="cpp-number">1</span>;  half posW = mul(half4(x, y, zw, <span class="cpp-number">1</span>), g_ViewProjInv);  posW.xyz /= posW.w;  …}</pre></div><!–ENDSCRIPT–>I can create shadow-map texture by rendering all occluder objects to shadow map. But, I want to get shadow map texture from my z-map tex. Because I don't want to render all objects again.I tried that code but didn't get I wanted. Also, shadow-map tex got depended &#111;n view position:<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>float4 ShadowPS (float2 t : TEXCOORD0) : COLOR0{  …  half zw = tex2D (smpZMap, t);  half x = <span class="cpp-number">2</span> * t.x - <span class="cpp-number">1</span>;  half y = <span class="cpp-number">2</span> * (<span class="cpp-number">1</span> - t.y) - <span class="cpp-number">1</span>;  half posW = mul(half4(x, y, zw, <span class="cpp-number">1</span>), g_ViewProjInv);  posW.xyz /= posW.w;  half4 posP = mul(posW, g_LightViewProj);  <span class="cpp-keyword">return</span> posP.z / posP.w;}</pre></div><!–ENDSCRIPT–>How can I create shadow-map with using existing Z-Map tex?Or, another question: Creating shadow-map texture by;a - Reconstructing position from z-mapb - Rendering all occluders.Which is better for performance?Thanks in advance.
  You can't generate a shadow map from your view-space depth buffer.  It quite simply won't have all the information you need to render all occluders.  Rendering out your shadow map should be very quick...usually your shaders are dead-simple and there's no texture sampling which means quite often you'll be limited by triangle setup rate.  Usually the only issue to worry about is CPU overhead from DrawPrimitive calls, but you can usually get better batching with your shadow map pass since there's no materials involved.Also...I wrote a bit about different methods of reconstructing position from depth, which you might be interested in reading.   