title: Wrong depth buffer (to texture) output?
body: <p>For the SSAO effect I have to generate two textures: normals (in view space) and depth.</p>
  
  <p>I decided to use depth buffer as texture according to <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb205074(v=vs.85).aspx" rel="nofollow noreferrer">Microsoft tutorial</a> (the <em>Reading the Depth-Stencil Buffer as a Texture</em> chapter).</p>
  
  <p>Unfortunately, after rendering I got none information from the depth buffer (the lower image):</p>
  
  <p><a href="https://i.stack.imgur.com/BPlK4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BPlK4.jpg" alt="enter image description here"></a></p>
  
  <p>I guess it's not right. And what is strange, the depth buffer seems to work (I get the right order of faces etc.).</p>
  
  <p>The depth buffer code:</p>
  
  <pre><code>//create depth stencil texture (depth buffer)
  D3D11_TEXTURE2D_DESC descDepth;
  ZeroMemory(&amp;descDepth, sizeof(descDepth));
  descDepth.Width = width;
  descDepth.Height = height;
  descDepth.MipLevels = 1;
  descDepth.ArraySize = 1;
  descDepth.Format = DXGI_FORMAT_R24G8_TYPELESS;
  descDepth.SampleDesc.Count = antiAliasing.getCount();
  descDepth.SampleDesc.Quality = antiAliasing.getQuality();
  descDepth.Usage = D3D11_USAGE_DEFAULT;
  descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
  descDepth.CPUAccessFlags = 0;
  descDepth.MiscFlags = 0;
  
  ID3D11Texture2D* depthStencil = NULL;
  result = device-&gt;CreateTexture2D(&amp;descDepth, NULL, &amp;depthStencil);
  ERROR_HANDLE(SUCCEEDED(result), L"Could not create depth stencil texture.", MOD_GRAPHIC);
  
  D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
  //setup the description of the shader resource view
  shaderResourceViewDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
  shaderResourceViewDesc.ViewDimension = antiAliasing.isOn() ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D;
  shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
  shaderResourceViewDesc.Texture2D.MipLevels = 1;
  
  //create the shader resource view.
  ERROR_HANDLE(SUCCEEDED(device-&gt;CreateShaderResourceView(depthStencil, &amp;shaderResourceViewDesc, &amp;depthStencilShaderResourceView)),
      L"Could not create shader resource view for depth buffer.", MOD_GRAPHIC);
  
  createDepthStencilStates();
  //set the depth stencil state.
  context-&gt;OMSetDepthStencilState(depthStencilState3D, 1);
  
  D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  // Initialize the depth stencil view.
  ZeroMemory(&amp;depthStencilViewDesc, sizeof(depthStencilViewDesc));
  
  // Set up the depth stencil view description.
  depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  depthStencilViewDesc.ViewDimension = antiAliasing.isOn() ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D;
  depthStencilViewDesc.Texture2D.MipSlice = 0;
  //depthStencilViewDesc.Flags = D3D11_DSV_READ_ONLY_DEPTH;
  
  // Create the depth stencil view.
  result = device-&gt;CreateDepthStencilView(depthStencil, &amp;depthStencilViewDesc, &amp;depthStencilView);
  ERROR_HANDLE(SUCCEEDED(result), L"Could not create depth stencil view.", MOD_GRAPHIC);
  </code></pre>
  
  <p>After rendering with first pass, I set the depth stencil as texture resource along with other render targets (color, normals), appending it to array:</p>
  
  <pre><code>ID3D11ShaderResourceView ** textures = new ID3D11ShaderResourceView *[targets.size()+1];
  for (unsigned i = 0; i &lt; targets.size(); i++) {
      textures[i] = targets[i]-&gt;getShaderResourceView();
  }
  textures[targets.size()] = depthStencilShaderResourceView;
  context-&gt;PSSetShaderResources(0, targets.size()+1, textures);
  </code></pre>
  
  <p>Before second pass I call <code>context-&gt;OMSetRenderTargets(1, &amp;myRenderTargetView, NULL);</code> to unbind depth buffer (so I can use it as texture).</p>
  
  <p>Then, I render my textures (render targets from first pass + depth buffer) with trivial post-process shader, just for debug purpose (second pass):</p>
  
  <pre><code>Texture2D ColorTexture[3];
  SamplerState ObjSamplerState;
  
  float4 main(VS_OUTPUT input) : SV_TARGET0{
      float4 Color;
      Color = float4(0, 1, 1, 1);
      float2 textureCoordinates = input.textureCoordinates.xy * 2;
      if (input.textureCoordinates.x &lt; 0.5f &amp;&amp; input.textureCoordinates.y &lt; 0.5f) {
          Color = ColorTexture[0].Sample(ObjSamplerState, textureCoordinates);
      }
      if (input.textureCoordinates.x &gt; 0.5f &amp;&amp; input.textureCoordinates.y &lt; 0.5f) {
          textureCoordinates.x -= 0.5f;
          Color = ColorTexture[1].Sample(ObjSamplerState, textureCoordinates);
      }
      if (input.textureCoordinates.x &lt; 0.5f &amp;&amp; input.textureCoordinates.y &gt; 0.5f) { //depth texture
          textureCoordinates.y -= 0.5f;
          Color = ColorTexture[2].Sample(ObjSamplerState, textureCoordinates);
      }
  ...
  </code></pre>
  
  <p>It works fine for normals texture. Why it doesn't for depth buffer (as shader resource view)?</p>