title: DirectX Render to a texture
body: <p>I'm trying to implement a rendering to a texture but without success. I've created a class that will(should) take care about all these stuff:</p>
  
  <pre><code>class TextureRenderer
  {
  public:
      TextureRenderer() = delete;
      TextureRenderer(ID3D11Device *pDevice, int width, int height);
      ~TextureRenderer();
  
  private:
      ID3D11ShaderResourceView *pResView;
      ID3D11RenderTargetView *pRenderTargetView;
      ID3D11Texture2D *pTexture;
      ID3D11RenderTargetView *pTarget;
      Texture *pCTexture;
  
  public:
      void BeginRender(ID3D11DeviceContext* pDeviceContext);
      void EndRender(ID3D11DeviceContext *pDeviceContext);
      void Clear(ID3D11DeviceContext *pDeviceContext, ID3D11DepthStencilView *pDepth,
          Vector4 clearColor);
  
      Texture *GetTexture()   { return pCTexture; }
  };
  </code></pre>
  
  <p><strong>Declarations:</strong></p>
  
  <pre><code>TextureRenderer::TextureRenderer(ID3D11Device *pDevice, int width, int height)
  {
      D3D11_TEXTURE2D_DESC textureDesc;
      D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
      D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
  
  
      ZeroMemory(&amp;textureDesc, sizeof(textureDesc));
      textureDesc.Width = width;
      textureDesc.Height = height;
      textureDesc.MipLevels = 1;
      textureDesc.ArraySize = 1;
      textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
      textureDesc.SampleDesc.Count = 1;
      textureDesc.Usage = D3D11_USAGE_DEFAULT;
      textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
      textureDesc.CPUAccessFlags = 0;
      textureDesc.MiscFlags = 0;
  
      HRESULT hr = pDevice-&gt;CreateTexture2D(&amp;textureDesc, nullptr, &amp;pTexture);
      if (FAILED(hr))
          throw directx_init_error(hr, "Texture2D");
  
      // Setup the description of the render target view.
      renderTargetViewDesc.Format = textureDesc.Format;
      renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
      renderTargetViewDesc.Texture2D.MipSlice = 0;
  
      hr = pDevice-&gt;CreateRenderTargetView(pTexture, &amp;renderTargetViewDesc, &amp;pRenderTargetView);
      if (FAILED(hr))
          throw(directx_init_error(hr, "RTV"));
  
      shaderResourceViewDesc.Format = textureDesc.Format;
      shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
      shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
      shaderResourceViewDesc.Texture2D.MipLevels = 1;
  
      hr = pDevice-&gt;CreateShaderResourceView(pTexture, &amp;shaderResourceViewDesc, &amp;pResView);
      if (FAILED(hr))
          throw directx_init_error(hr, "shader resource view");
  
      pCTexture = new Texture(pResView);
  }
  
  TextureRenderer::~TextureRenderer()
  {
      if (pCTexture)
      {
          delete pCTexture;
          pCTexture = nullptr;
      }
  
      if (pResView)
      {
          pResView-&gt;Release();
          pResView = 0;
      }
  
      if (pRenderTargetView)
      {
          pRenderTargetView-&gt;Release();
          pRenderTargetView = 0;
      }
  
      if (pTexture)
      {
          pTexture-&gt;Release();
          pTexture = 0;
      }
  }
  
  void TextureRenderer::Clear(ID3D11DeviceContext *pDeviceContext, ID3D11DepthStencilView *pDepth,
      Vector4 clearColor)
  {
      float color[] = {
          clearColor.x, clearColor.y, clearColor.z, clearColor.w
      };
  
      pDeviceContext-&gt;ClearRenderTargetView(pRenderTargetView, color);
      if (pDepth)
          pDeviceContext-&gt;ClearDepthStencilView(pDepth, D3D11_CLEAR_DEPTH, 1.0f, 0);
  }
  
  void TextureRenderer::BeginRender(ID3D11DeviceContext *pDeviceContext)
  {
      ID3D11DepthStencilView *pDepthStencil = nullptr;
      pDeviceContext-&gt;OMGetRenderTargets(1, &amp;pTarget, &amp;pDepthStencil);
  
      pDeviceContext-&gt;OMSetRenderTargets(1, &amp;pRenderTargetView, pDepthStencil);
  }
  
  void TextureRenderer::EndRender(ID3D11DeviceContext *pDeviceContext)
  {
      ID3D11DepthStencilView *pDepthStencil = nullptr;
      pDeviceContext-&gt;OMGetRenderTargets(0, nullptr, &amp;pDepthStencil);
  
      pDeviceContext-&gt;OMSetRenderTargets(1, &amp;pTarget, pDepthStencil);
  }
  </code></pre>
  
  <p><strong>Usage:</strong></p>
  
  <pre><code>pTextureRenderer-&gt;Clear(pDeviceContext, nullptr, Vector4(0, 0.25f, 0.333f, 0));
  pTextureRenderer-&gt;BeginRender(pDeviceContext);
  ....
  pTextureRenderer-&gt;EndRender(pDeviceContext);
  
  p2DObject-&gt;SetTexture(pTextureRenderer-&gt;GetTexture());
  p2DObject-&gt;Render(pDeviceContext);
  </code></pre>
  
  <p>Texture class just hold a pointer to <code>ID3D11ShaderResourceView</code>.<br>
  When I'll try to render this texture, nothing happens. Texture and D2DObject classes are ok(everything else is rendered correctly)</p>