• <tfoot id='A3fWu'></tfoot>

          <bdo id='A3fWu'></bdo><ul id='A3fWu'></ul>
      1. <i id='A3fWu'><tr id='A3fWu'><dt id='A3fWu'><q id='A3fWu'><span id='A3fWu'><b id='A3fWu'><form id='A3fWu'><ins id='A3fWu'></ins><ul id='A3fWu'></ul><sub id='A3fWu'></sub></form><legend id='A3fWu'></legend><bdo id='A3fWu'><pre id='A3fWu'><center id='A3fWu'></center></pre></bdo></b><th id='A3fWu'></th></span></q></dt></tr></i><div id='A3fWu'><tfoot id='A3fWu'></tfoot><dl id='A3fWu'><fieldset id='A3fWu'></fieldset></dl></div>

        <small id='A3fWu'></small><noframes id='A3fWu'>

      2. <legend id='A3fWu'><style id='A3fWu'><dir id='A3fWu'><q id='A3fWu'></q></dir></style></legend>

        DirectX 11 帧缓冲区捕获(C++,无 Win32 或 D3DX)

        DirectX 11 framebuffer capture (C++, no Win32 or D3DX)(DirectX 11 帧缓冲区捕获(C++,无 Win32 或 D3DX))
              <tbody id='WzR3O'></tbody>

            <legend id='WzR3O'><style id='WzR3O'><dir id='WzR3O'><q id='WzR3O'></q></dir></style></legend>
          • <small id='WzR3O'></small><noframes id='WzR3O'>

                <tfoot id='WzR3O'></tfoot>
                  <bdo id='WzR3O'></bdo><ul id='WzR3O'></ul>
                • <i id='WzR3O'><tr id='WzR3O'><dt id='WzR3O'><q id='WzR3O'><span id='WzR3O'><b id='WzR3O'><form id='WzR3O'><ins id='WzR3O'></ins><ul id='WzR3O'></ul><sub id='WzR3O'></sub></form><legend id='WzR3O'></legend><bdo id='WzR3O'><pre id='WzR3O'><center id='WzR3O'></center></pre></bdo></b><th id='WzR3O'></th></span></q></dt></tr></i><div id='WzR3O'><tfoot id='WzR3O'></tfoot><dl id='WzR3O'><fieldset id='WzR3O'></fieldset></dl></div>
                  本文介绍了DirectX 11 帧缓冲区捕获(C++,无 Win32 或 D3DX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 DirectX 11 将我的前端或后端缓冲区的内容捕获到一个字节数组中,然后我可以将其用作纹理或用作创建文件的源.到目前为止,我有一个交换链设置、大量渲染和以下代码 - 我确保在调用 Present 后调用它们.

                  I would like to capture the contents of my front or back buffer using DirectX 11 into an array of bytes which I can then use as a texture or as a source for creating a file. I have a swap chain setup, lots of rendering happening and the following code so far - which I make sure to call after the call to Present.

                  ID3D11Texture2D* pSurface;
                  HRESULT hr = m_swapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast< void** >( &pSurface ) );
                  if( pSurface )
                  {
                      const int width = static_cast<int>(m_window->Bounds.Width * m_dpi / 96.0f);
                      const int height = static_cast<int>(m_window->Bounds.Height * m_dpi / 96.0f);
                      unsigned int size = width * height;
                      if( m_captureData )
                      {
                          freeFramebufferData( m_captureData );
                      }
                      m_captureData = new unsigned char[ width * height * 4 ];
                  
                      ID3D11Texture2D* pNewTexture = NULL;
                  
                      D3D11_TEXTURE2D_DESC description =
                      {
                          width, height, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM,
                          { 1, 0 }, // DXGI_SAMPLE_DESC
                          D3D11_USAGE_STAGING,
                          0, D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE, 0
                      };
                  
                      HRESULT hr = m_d3dDevice->CreateTexture2D( &description, NULL, &pNewTexture );
                      if( pNewTexture )
                      {
                          m_d3dContext->CopyResource( pNewTexture, pSurface );
                          D3D11_MAPPED_SUBRESOURCE resource;
                          unsigned int subresource = D3D11CalcSubresource( 0, 0, 0 );
                          HRESULT hr = m_d3dContext->Map( pNewTexture, subresource, D3D11_MAP_READ, 0, &resource );
                          //resource.pData; // TEXTURE DATA IS HERE
                  
                          const int pitch = width << 2;
                          const unsigned char* source = static_cast< const unsigned char* >( resource.pData );
                          unsigned char* dest = m_captureData;
                          for( int i = 0; i < height; ++i )
                          {
                              memcpy( dest, source, width * 4 );
                              source += pitch;
                              dest += pitch;
                          }
                  
                          m_captureSize = size;
                          m_captureWidth = width;
                          m_captureHeight = height;
                  
                          return;
                      }
                  
                      freeFramebufferData( m_captureData );
                  }
                  

                  它总是给我零 alphas 的黑色.

                  It always gives me black with zero alphas.

                  我通常可以选择 GDI 互操作来使用 BitBlt 从交换链中复制位图 - 但是我有一些限制,这意味着这不是一个有效的解决方案.

                  I would normally have the option of GDI interop to use BitBlt to copy a bitmap out of the swap chain - however I have restrictions which means this is not a valid solution.

                  还有 D3DX 库,其中包含执行这些操作的功能.

                  Also the D3DX library, which contains functionality for doing bits of this is also out of the question.

                  推荐答案

                  所以.更多的实验揭示了问题".通过获取帧缓冲区纹理的描述并将其用作创建新纹理的基础,问题得以解决...

                  So. A little more experimentation revealed the "problem". By getting the description of the framebuffer texture and using that as the basis to create the new texture the problem was resolved...

                  ID3D11Texture2D* pSurface;
                  HRESULT hr = m_swapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast< void** >( &pSurface ) );
                  if( pSurface )
                  {
                      const int width = static_cast<int>(m_window->Bounds.Width * m_dpi / 96.0f);
                      const int height = static_cast<int>(m_window->Bounds.Height * m_dpi / 96.0f);
                      unsigned int size = width * height;
                      if( m_captureData )
                      {
                          freeFramebufferData( m_captureData );
                      }
                      m_captureData = new unsigned char[ width * height * 4 ];
                  
                      ID3D11Texture2D* pNewTexture = NULL;
                  
                      D3D11_TEXTURE2D_DESC description;
                      pSurface->GetDesc( &description );
                      description.BindFlags = 0;
                      description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
                      description.Usage = D3D11_USAGE_STAGING;
                  
                      HRESULT hr = m_d3dDevice->CreateTexture2D( &description, NULL, &pNewTexture );
                      if( pNewTexture )
                      {
                          m_d3dContext->CopyResource( pNewTexture, pSurface );
                          D3D11_MAPPED_SUBRESOURCE resource;
                          unsigned int subresource = D3D11CalcSubresource( 0, 0, 0 );
                          HRESULT hr = m_d3dContext->Map( pNewTexture, subresource, D3D11_MAP_READ_WRITE, 0, &resource );
                          //resource.pData; // TEXTURE DATA IS HERE
                  
                          const int pitch = width << 2;
                          const unsigned char* source = static_cast< const unsigned char* >( resource.pData );
                          unsigned char* dest = m_captureData;
                          for( int i = 0; i < height; ++i )
                          {
                              memcpy( dest, source, width * 4 );
                              source += pitch;
                              dest += pitch;
                          }
                  
                          m_captureSize = size;
                          m_captureWidth = width;
                          m_captureHeight = height;
                  
                          return;
                      }
                  
                      freeFramebufferData( m_captureData );
                  }
                  

                  这篇关于DirectX 11 帧缓冲区捕获(C++,无 Win32 或 D3DX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                  <tfoot id='tXR7x'></tfoot>

                        <i id='tXR7x'><tr id='tXR7x'><dt id='tXR7x'><q id='tXR7x'><span id='tXR7x'><b id='tXR7x'><form id='tXR7x'><ins id='tXR7x'></ins><ul id='tXR7x'></ul><sub id='tXR7x'></sub></form><legend id='tXR7x'></legend><bdo id='tXR7x'><pre id='tXR7x'><center id='tXR7x'></center></pre></bdo></b><th id='tXR7x'></th></span></q></dt></tr></i><div id='tXR7x'><tfoot id='tXR7x'></tfoot><dl id='tXR7x'><fieldset id='tXR7x'></fieldset></dl></div>

                        <small id='tXR7x'></small><noframes id='tXR7x'>

                          <tbody id='tXR7x'></tbody>
                        • <bdo id='tXR7x'></bdo><ul id='tXR7x'></ul>

                        • <legend id='tXR7x'><style id='tXR7x'><dir id='tXR7x'><q id='tXR7x'></q></dir></style></legend>