[转]硬件遮挡查询(Hardware Occlusion Queries)
2013-03-26 16:33
357 查看
http://blog.csdn.net/skybreaker/article/details/1828104
本节将详细介绍硬件遮挡查询的使用以及优化策略。
如同上面提到的,通过硬件遮挡查询,我们能够直接获得所提交的物体是否被绘制到场景中。从而得知物体是否被遮挡(被遮挡的物体是不会出现在场景中的)。如果直接提交整个物体mesh,遮挡查询的开销显然太大了。我们只要提交物体的AABB(轴向包围盒)即可,这样仍然可以保证算法是“保守”的。包围盒越接近于物体效率就会越高。另外,使用一个小的渲染目标(如320*240)可以明显加快速度,同时基本不会产生任何副面效果。但是,仅仅是使用AABB和小渲染目标,遮挡查询的开销仍然十分巨大。为此我们还需要进一步对遮挡查询的执行序列进行优化:
下图是优化前CPU和GPU的执行情况:
从上图我们可以看出:虽然硬件遮挡查询操作本身十分耗时,但是进行遮挡查询时只有GPU在高负荷工作,CPU在提交查询之后就处于闲置状态。因此需要将提交查询和获得查询结果之间的时间间隔尽可能的充分利用。
下图是经过优化后的执行序列:
从上图我们可以看出:
原先用于等待查询结果的时间被用于提交其他查询,在GPU进行遮挡运算的同时CPU并没有空闲等待,而是进行其他工作。
为了进一步提高遮挡查询效率,还可以在提交查询之后进行各类其他运算,如AI,物理等等。
Simple hardware occlusion query usage
An occlusion query is fundamentally simple. The GPU tells us how many pixels should have ended up visible on-screen.
These pixels successfully passed various tests at the end of the pipeline-such as the frustum visibility test, the scissor test, the alpha test, the stencil test, and the depth test.
The step:
1.Create a query
2.Disable rendering to screen(set the color mask of all chanels to False).
3.Disable writing to depth buffer(just test against, but don't update, the depthe buffer).
4.Issue query begin(which resets the counter of visible pixels).
5."Render" the object's bounding box(it will only do depth testing; pixels that pass depth testing will not be rendered on-screen because rendering and depth writing were disabled).
6.End query(stop counting visible pixels).
7.Enable rendering to screen.
8.Enable depth writing(if required).
9.Get query result(the number of "visible" pixels).
10. If the number of visible pixels is greater than 0(or some threshold), render the complete object.
LPDIRECT3DDEVICE9 g_pd3dDevice;
LPDIRECT3DQUERY9 g_pSphereQuery;
g_pd3dDevice->CreateQuery( D3DQUERYTYPE_OCCLUSION, &g_pSphereQuery );
g_pSphereQuery->Issue( D3DISSUE_BEGIN );
g_pSphereMesh->DrawSubset(0);
g_pSphereQuery->Issue( D3DISSUE_END );
DWORD dwSphereData;
while( g_pSphereQuery->GetData( (void*)&dwSphereData, sizeof(DWORD), 0) == S_FALSE )
{
// GetData is asynchronous so, if possible, try to find some extra work
// to do here if the hardware is still rendering our scene.
}
Ogre Smaple_Lighting demonstrates usage of occlusion queries for lens flare.
硬件遮挡查询存在的两个主要问题:
1. 调用遮挡查询本身的开销。因为每次查询都增加了一次另外的绘制调用。
2. 等待查询结果引起的延迟。
遮挡查询可用来做遮挡剔除。适用场景:在城市中漫步,在这种视角下,有大量建筑模型被遮挡,可以提前根据遮挡查询剔除掉。
本节将详细介绍硬件遮挡查询的使用以及优化策略。
如同上面提到的,通过硬件遮挡查询,我们能够直接获得所提交的物体是否被绘制到场景中。从而得知物体是否被遮挡(被遮挡的物体是不会出现在场景中的)。如果直接提交整个物体mesh,遮挡查询的开销显然太大了。我们只要提交物体的AABB(轴向包围盒)即可,这样仍然可以保证算法是“保守”的。包围盒越接近于物体效率就会越高。另外,使用一个小的渲染目标(如320*240)可以明显加快速度,同时基本不会产生任何副面效果。但是,仅仅是使用AABB和小渲染目标,遮挡查询的开销仍然十分巨大。为此我们还需要进一步对遮挡查询的执行序列进行优化:
下图是优化前CPU和GPU的执行情况:
从上图我们可以看出:虽然硬件遮挡查询操作本身十分耗时,但是进行遮挡查询时只有GPU在高负荷工作,CPU在提交查询之后就处于闲置状态。因此需要将提交查询和获得查询结果之间的时间间隔尽可能的充分利用。
下图是经过优化后的执行序列:
从上图我们可以看出:
原先用于等待查询结果的时间被用于提交其他查询,在GPU进行遮挡运算的同时CPU并没有空闲等待,而是进行其他工作。
为了进一步提高遮挡查询效率,还可以在提交查询之后进行各类其他运算,如AI,物理等等。
Simple hardware occlusion query usage
An occlusion query is fundamentally simple. The GPU tells us how many pixels should have ended up visible on-screen.
These pixels successfully passed various tests at the end of the pipeline-such as the frustum visibility test, the scissor test, the alpha test, the stencil test, and the depth test.
The step:
1.Create a query
2.Disable rendering to screen(set the color mask of all chanels to False).
3.Disable writing to depth buffer(just test against, but don't update, the depthe buffer).
4.Issue query begin(which resets the counter of visible pixels).
5."Render" the object's bounding box(it will only do depth testing; pixels that pass depth testing will not be rendered on-screen because rendering and depth writing were disabled).
6.End query(stop counting visible pixels).
7.Enable rendering to screen.
8.Enable depth writing(if required).
9.Get query result(the number of "visible" pixels).
10. If the number of visible pixels is greater than 0(or some threshold), render the complete object.
LPDIRECT3DDEVICE9 g_pd3dDevice;
LPDIRECT3DQUERY9 g_pSphereQuery;
g_pd3dDevice->CreateQuery( D3DQUERYTYPE_OCCLUSION, &g_pSphereQuery );
g_pSphereQuery->Issue( D3DISSUE_BEGIN );
g_pSphereMesh->DrawSubset(0);
g_pSphereQuery->Issue( D3DISSUE_END );
DWORD dwSphereData;
while( g_pSphereQuery->GetData( (void*)&dwSphereData, sizeof(DWORD), 0) == S_FALSE )
{
// GetData is asynchronous so, if possible, try to find some extra work
// to do here if the hardware is still rendering our scene.
}
Ogre Smaple_Lighting demonstrates usage of occlusion queries for lens flare.
硬件遮挡查询存在的两个主要问题:
1. 调用遮挡查询本身的开销。因为每次查询都增加了一次另外的绘制调用。
2. 等待查询结果引起的延迟。
遮挡查询可用来做遮挡剔除。适用场景:在城市中漫步,在这种视角下,有大量建筑模型被遮挡,可以提前根据遮挡查询剔除掉。
相关文章推荐
- [Engine][GPUGems]Chapter 6. Hardware Occlusion Queries Made Useful
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
- MySQL开启慢查询日志log-slow-queries
- 硬件预取(Hardware Prefetch)
- 遮挡剔除(仅专业版) Occlusion Culling (Pro only)
- ubuntu14.04 硬件型号查询
- 即将来到: CSS Feature Queries (CSS特性查询)
- bat命令查询硬件信息
- 软硬件划分论文阅读系列 2 -- A Configurable Logic Architecture for Dynamic Hardware Software Partitioning
- Real-Time Rendering (10) - 图形硬件及学习小结(Graphics Hardware)
- Real-Rime Rendering (10) - 图形硬件及学习小结(Graphics Hardware)
- 【转载】三星A8 S5pV210 硬件设计指南S5PV210_Hardware Design Guide_Rev1.0
- 查看linux硬件信息 get linux hardware detailed info
- TYPE Directive in FOR XML Queries(FOR XML查询中的TYPE指令)
- 遮挡查询
- D3D10/11中的遮挡查询的使用
- Linux 查询硬件命令
- 查询Ubuntu对硬件的支持情况列表
- CentOS7下实践查询版本/CPU/内存/硬盘容量等硬件信息
- 查询苹果硬件产品保修情况: