GameAcademe

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 14 Stories :: 5 Comments :: 0 Trackbacks

常用链接

留言簿(5)

我参与的团队

搜索

  •  

最新评论

将GPU中的数据读到CPU的方法总结。

1:先将仅GPU可读写的subresource拷贝到一份CPU也可以读写的subresource(核心调用ID3D11DeviceContext::CopyResource)。
2:然后通过Map函数,Get a pointer(*D3D11_MAPPED_SUBRESOURCE) to the data contained in a subresource, and deny the GPU access to that subresource。
3:将上面得到的(*D3D11_MAPPED_SUBRESOURCE).(void*)强制转换为CPU可以理解的struct或者class或者float等类型的指针。
4:对上面得到的CPU指针读写即可得到或者更改GPU中的数据。
5:最后Unmap()刚刚Map()的Resource。
/*示例:我们想将GPU中的计算结果ID3D11Buffer* m_pTestBuffer读出来。我们先通过调用函数
  ID3D11Buffer *CreateAndCopyToDebugBuffer(ID3D11Buffer* sourceBuffer)得到一份它的CPU可以读写的拷贝buffer。
*/


HRESULT hr
=S_OK;
ID3D11Buffer
* resultBuffer=NULL;
resultBuffer
=CreateAndCopyToDebugBuffer(m_pTestBuffer);
  
 
/*然后通过Map函数,Get a pointer ( D3D11_MAPPED_SUBRESOURCE *) to the data 
 contained in a subresource, and deny the GPU access to that subresource。
*/


D3D11_MAPPED_SUBRESOURCE resultResources;
ZeroMemory(
&resultResources,sizeof(D3D11_MAPPED_SUBRESOURCE));
IFR(m_pContext
->Map(resultBuffer,0,D3D11_MAP_READ,0,&resultResources));

 
/*将上面得到的D3D11_MAPPED_SUBRESOURCE::VOID*强制转换为CPU可以理解的struct或者class或者float等类型的指针。*/

TestBufType
* p=NULL;
p
=(TestBufType*)resultResources.pData;
  
/*对上面得到的CPU指针p读写即可得到或者更改GPU中ID3D11Buffer* m_pTestBuffer的数据。*/

m_pGPUTestResult
=new TestBufType[uNumSimpleElements];
for(int i=0;i<uNumSimpleElements;i++)
{
    m_pGPUTestResult[i].pos.x
=p[i].pos.x;
    m_pGPUTestResult[i].pos.y
=p[i].pos.y;
    m_pGPUTestResult[i].velocity.x
=p[i].velocity.x;
    m_pGPUTestResult[i].velocity.y
=p[i].velocity.y;
}

   
/*最后Unmap()刚刚Map()的Resource,使得这个resource的CPU指针无效,GPU重新获得对该resource的读写权限。*/
m_pContext
->Unmap(resultBuffer,0);
我们最后反过来看下最开始调用的将仅GPU读写的resource拷贝到CPU的resource的函数CreateAndCopyToDebugBuffer(),里面用来描述拷贝出来的resource的D3D11_BUFFER_DESC有两个要注意的点,CPUAccessFlags和Usage。其中CPUAccessFlags根据需要可以设置为D3D11_CPU_ACCESS_READ和D3D11_CPU_ACCESS_WRITE。然后Usage就一定要设置为D3D11_USAGE_STAGING,它表明这个resource是从GPU拷贝到CPU的。CreateAndCopyToDebugBuffer()的实现如下:
ID3D11Buffer* DemoApp::CreateAndCopyToDebugBuffer(ID3D11Buffer* pSrcBuffer)
{
    ID3D11Buffer
* debugBuffer;
    ZeroMemory(
&debugBuffer,sizeof(ID3D11Buffer));
    D3D11_BUFFER_DESC bfDESC;
    ZeroMemory(
&bfDESC,sizeof(D3D11_BUFFER_DESC));
    pSrcBuffer
->GetDesc(&bfDESC);
    bfDESC.BindFlags
=0;
    bfDESC.MiscFlags
=0;
    bfDESC.CPUAccessFlags
=D3D11_CPU_ACCESS_READ;
    bfDESC.Usage
=D3D11_USAGE_STAGING;

    
if(SUCCEEDED(m_pDevice->CreateBuffer(&bfDESC,NULL,&debugBuffer)))
    
{
        m_pContext
->CopyResource(debugBuffer,pSrcBuffer);
    }

    
    
return debugBuffer;
}





posted on 2011-06-30 20:32 游戏研究院 阅读(979) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理