此篇文章主要讲一些小技巧,针对前面转载的D3D9 GPU Hacks,我们可以做的一些优化。

在做延迟渲染或者其它需要深度的地方使用INTZ格式的纹理,这样可以直接对纹理进行操作,节省了显存和带宽,这样即使在前向渲染的时候也可以获取深度,有了深度信息我们就可以做很多效果,如水的柔边,水边泡沫,景深等效果。

注:以下示例代码均摘自http://developer.amd.com/wordpress/media/2012/10/Advanced-DX9-Capabilities-for-ATI-Radeon-Cards_v2.pdf

INTZ 纹理示例代码:

Depth Texture Format: INTZ
Description: An additional texture format called INTZ is exposed that allows a 24-bit depth
buffer previously used for rendering to be bound as a texture. Any fetches from this texture
will return the depth values stored. Shadow mapping applications relying on PercentageCloser
Filtering should prefer the use of DX9 Depth Stencil Textures instead. INTZ
additionally exposes an 8-bit stencil buffer when used for depth buffering, allowing stencil
operation to be carried out when an INTZ surface is bound as the active depth stencil buffer.
However the contents of the stencil buffer will not be available as texture data when binding
the surface as a texture.
Note that an INTZ depth buffer may be used as a texture concurrently to the same INTZ
surface being used for depth buffering, as long as depth writes are disabled.
Supported hardware: ATI Radeon 4000 series and above.
Implementation details
A FourCC depth texture format is exposed:
#define FOURCC_INTZ ((D3DFORMAT)(MAKEFOURCC(‘I’,’N’,’T’,’Z’)))
To check support for this feature:
// Determine if INTZ is supported
HRESULT hr;
hr =pd3d->CheckDeviceFormat(AdapterOrdinal, DeviceType, AdapterFormat,
D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE,
FOURCC_INTZ);
BOOL bINTZDepthStencilTexturesSupported = (hr == D3D_OK);
To create an INTZ depth stencil texture:
// Create an INTZ depth stencil texture
IDirect3DTexture9 *pINTZDST;
pd3dDevice->CreateTexture(dwWidth, dwHeight, 1,
D3DUSAGE_DEPTHSTENCIL, FOURCC_INTZ,
D3DPOOL_DEFAULT, &pINTZDST,
NULL);
To bind the depth stencil texture as an active depth buffer:
// Retrieve depth buffer surface from texture interface
IDirect3DSurface9 *pINTZDSTSurface;
pINTZDST->GetSurfaceLevel(0, &pINTZDSTSurface);
// Bind depth buffer
pd3dDevice->SetDepthStencilSurface(pINTZDSTSurface);
Note that calling GetSurfaceLevel() increases the reference count of pINTZDST so you will
need to Release() it when no longer needed.
To bind an INTZ depth buffer texture as a texture:
// Bind depth buffer texture
pd3dDevice->SetTexture(0, pINTZDST);

注意:此处千万不能使用 D3DXCreateTexture 来创建这样的纹理,因为D3DX并没有对此提供支持,创建的返回的纹理类型并不是你需要的INTZ等格式,下面所讲也同等适用。

在渲染阴影的时候我们一般使用D16或者D32,使用tex2dproj函数来利用硬件特性来采样纹理,这样直接返回的是一个阴影系数。渲染阴影的时候我们并不需要一张颜色纹理,但是D3D9必须要绑定一张颜色纹理。我们可以通过上面提到的NULL纹理来达到节省显存的目的,当然使用之前也需要查询当前硬件是否支持NULL纹理。

示例代码如下:

Render Target Format: NULL
Description: A render target format called NULL is exposed. Render targets created with
this format will not have any memory allocated internally, but will satisfy validation
requirements imposed by the DX9 runtime. This functionality is useful when performing
rendering onto a depth buffer without wishing to update an actual color render target since
the DX9 runtime enforces the use of a valid color render target for all rendering operations.
Therefore common depth-only rendering applications like shadow mapping or depth pre-pass
can benefit from using this “dummy” format to save memory that would otherwise be
required to bind a valid color render target for this operation.
Supported hardware: ATI Radeon 4000 series and above.
Implementation details
A FourCC color render target format is exposed:
#define FOURCC_NULL ((D3DFORMAT)(MAKEFOURCC(‘N’,’U’,’L’,’L’)))
To check support for this feature:
// Determine if NULL is supported
HRESULT hr;
hr = pd3d->CheckDeviceFormat(AdapterOrdinal, DeviceType, AdapterFormat,
D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE,
FOURCC_NULL);
BOOL bNULLRenderTargetSupported = (hr == D3D_OK);
To create a NULL render target surface:
// Create a NULL render target with 4x multisampling
IDirect3DSurface9* pDummyRenderTarget;
pd3dDevice->CreateRenderTarget(dwWidth, dwHeight, FOURCC_NULL,
D3DMULTISAMPLE_4_SAMPLES, 0,
FALSE, &pDummyRenderTarget, NULL);
To bind a NULL render target surface:
// Bind a NULL render target at slot 0 so that we don’t have to bind
// a real color buffer; this allows memory savings
pd3dDevice->SetRenderTarget(0, pDummyRenderTarget);

这样我们可以通过这些小技巧来达到节省显存、带宽等目的。还有一些小的技巧,比如Alpha to coverage, depth bound test等,我也会陆续把它们添加进来。

参考文章:

1. D3D9 GPU Hacks

2. Advanced DX9 Capabilities for ATI Radeon Cards

D3D9 优化小技巧的更多相关文章

  1. 嵌入式C语言优化小技巧

    嵌入式C语言优化小技巧 1 概述 嵌入式系统是指完成一种或几种特定功能的计算机系统,具有自动化程度高,响应速度快等优点,目前已广泛应用于消费电子,工业控制等领域.嵌入式系统受其使用的硬件以及运行环境的 ...

  2. .NET性能优化小技巧

    .NET 性能优化小技巧 Intro 之前做了短信发送速度的提升,在大师的指导下,发送短信的速度有了极大的提升,学到了一些提升 .NET 性能的一些小技巧 HttpClient 优化 关于使用 Htt ...

  3. IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题

    IT咨询顾问:一次吐血的项目救火   年后的一个合作公司上线了一个子业务系统,对接公司内部的单点系统.我收到该公司的技术咨询:项目启动后没有规律的突然无法登录了,重新启动后,登录一断时间后又无法重新登 ...

  4. OI常用的常数优化小技巧

    注意:本文所介绍的优化并不是算法上的优化,那个就非常复杂了,不同题目有不同的优化.笔者要说的只是一些实用的常数优化小技巧,很简单,虽然效果可能不那么明显,但在对时间复杂度要求十分苛刻的时候,这些小的优 ...

  5. mysql优化小技巧

    对mysql优化时一个综合性的技术,主要包括 a: 表的设计合理化(符合3NF) b: 添加适当索引(index) [四种: 普通索引.主键索引.唯一索引unique.全文索引] c: 分表技术(水平 ...

  6. How Javascript works (Javascript工作原理) (十一) 渲染引擎及性能优化小技巧

    个人总结:读完这篇文章需要20分钟,这篇文章主要讲解了浏览器中引擎的渲染机制. DOMtree       ----|   |---->  RenderTree CSSOMtree  ----| ...

  7. JavaScript 工作原理之十一-渲染引擎及性能优化小技巧

    原文请查阅这里,略有删减,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第十一章. 迄 ...

  8. Spring Cloud OpenFeign 的 5 个优化小技巧!

    OpenFeign 是 Spring 官方推出的一种声明式服务调用和负载均衡组件.它的出现就是为了替代已经进入停更维护状态的 Feign(Netflix Feign),同时它也是 Spring 官方的 ...

  9. angularjs的一些优化小技巧

    尽可能少调用 ng-repeat ng-repeat默认会创建很多监听器,所以在数据量很大的时候,这个非常消耗页面性能,我觉的只有在当需要经常更新数据列表的时候才需要用ng-repeat,要不然放那么 ...

随机推荐

  1. ExtJS笔记3 MVC Architecture

    MVC Architecture   MVC架构 Contents File Structure Creating the application in app.js Defining a Contr ...

  2. 动态样式语言Less学习笔记

    介绍资料参见:http://www.bootcss.com/p/lesscss/ LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数. LESS 既可以在 客户端 上运行 (支 ...

  3. varnish中忽略cookie进行缓存

    varnish不缓存cookie的页面,如果html页面中带有cookie以下代码为接收到结尾的文件,自动去除掉cookiesub vcl_recv {    if (req.request == ” ...

  4. mysql备份恢复

    备份命令: mysqldump -u root -p --opt 数据库名 > /data/数据库文件名.sql 恢复命令: mysql -u root -p 数据库名</data/恢复的 ...

  5. 使用Xpath对XML进行模糊查询

    如果要对XML文件进行模糊查找的话是一个比较麻烦的事情,Xpath表达式中没有像文件系统中的“*”或"?" 或者有像SQL表达式中的"%",这样的模糊查找的通配 ...

  6. Andrew Ng机器学习公开课笔记 – Factor Analysis

    网易公开课,第13,14课 notes,9 本质上因子分析是一种降维算法 参考,http://www.douban.com/note/225942377/,浅谈主成分分析和因子分析 把大量的原始变量, ...

  7. ubuntu挂载其他分区到/home下,将当前分区内容替换

    有时候,我们装系统时,可能因为没注意,把某一个分区分小了,导致到最后,我们的那个盘容不下了, 这时,面临的两个选择就是:要么卸载一些软件,要么重新分区,重装系统,其实,还可以这样,去把其他 多余的盘分 ...

  8. Myeclipse配置 项目编码格式

    修改MyEclipse 工作空间的编码 修改之后,在该工作空间下创建的任何项目,编码都是UTF-8,既,该项目下所有文件都是utf-8 格式,工作空间影响项目,项目影响文件 General --> ...

  9. Bootstrap 进度条媒体对象和条组

    列表组组件 列表组组件用于显示一组列表的组件. //基本实例 <ul class="list-group"> <li class="list-group ...

  10. 蓝牙的AVCTP协议笔记

    1.概述     AVCTP协议描述了蓝牙设备间Audio/Video的控制信号交换的格式和机制,它是一个总体的协议,具体的控制信息由其指定的协议(如AVRCP)实现,AVCTP本身只指定控制comm ...