GameAcademe

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

常用链接

留言簿(5)

我参与的团队

搜索

  •  

最新评论

前面说过可以通过map函数来用cpu来读写gpu的计算数据。这几天又看了些文章后,感觉自己前面的理解有点肤浅。再来补充总结下。首先如果想更新GPU中的resource,除了可以通过map()函数来让CPU读写GPU的数据之外,其实还可以通过ID3D11DeviceContext::UpdateSubresource()等来直接让GPU写更新它自己的resource。


对于上面几个方法的异同呢,可以参考SDK如下:

Each usage dictates a tradeoff between functionality and performance. In general, resource accessing is accomplished with the following APIs. CPU access is done with ID3D11DeviceContext::Map. GPU access is done with ID3D11DeviceContext::CopySubresourceRegion, ID3D11DeviceContext::CopyResource, or ID3D11DeviceContext::UpdateSubresource.


从上简单看出Map就是通过CPU来access resource,剩下的方法都是通过GPU来access resource。 为了将让我们理解的数据(int,float之类)输出到屏幕控制台之类的(gpu没有传统的i/o功能),我们还是要通过cpu来读写gpu中的数据。当然如果只是GPU内部的计算,就让GPU来读写就可以了。

至于用哪种呢,也不是随便用的。在创建一个resource时,比如buffer。我们都会先创建一个D3D11_BUFFER_DESC来描述我们的buffer。在D3D11_BUFFER_DESC里面,有个D3D11_USAGE Usage,它是来描述我们的resource将来要被怎样使用(比如只可以被GPU读写?可以被GPU读,CPU写?之类的)。然后改Usage分为下面四种:

    
D3D11_USAGE_DEFAULT

    A resource that requires read and write access by the GPU. This is likely to be the most common usage choice. 

D3D11_USAGE_IMMUTABLE

    A resource that can only be read by the GPU. It cannot be written by the GPU, and cannot be accessed at all by the CPU. This type of resource must be initialized when it is created, since it cannot be changed after creation.

D3D11_USAGE_DYNAMIC

    A resource that is accessible by both the GPU (read only) and the CPU (write only). A dynamic resource is a good choice for a resource that will be updated by the CPU at least once per frame. To update a dynamic resource, use a Map method.

D3D11_USAGE_STAGING

    A resource that supports data transfer (copy) from the GPU to the CPU。

   
一般来说我们用的最多就是D3D11_USAGE_DEFAULT了,它支持也仅给GPU读写,但CPU不行。 要注意的一点是D3D11_USAGE_DEFAULTD3D11_USAGE_IMMUTABLE不能用map()函数的,他们都是仅给GPU来access的。所以一般来说为了让CPU来读D3D11_USAGE_DEFAULT的resource,我们就只能采取前面博文讲的迂回的方法:新创建一个D3D11_USAGE_STAGING的resource,简称staResource。然后把D3D11_USAGE_DEFAULT的resource,简称defResource的数据通过ID3D11DeviceContext::CopyResource()拷贝到刚新创建的staResource中,最后再通过Map()函数来对刚得到的staResource来读写。

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

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