此篇文章主要讲一些小技巧,针对前面转载的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. Apache Spark源码走读之9 -- Spark源码编译

    欢迎转载,转载请注明出处,徽沪一郎. 概要 本来源码编译没有什么可说的,对于java项目来说,只要会点maven或ant的简单命令,依葫芦画瓢,一下子就ok了.但到了Spark上面,事情似乎不这么简单 ...

  2. HTML: margin重疊現象的說明

    2句話: ①相鄰的兩個普通元素,上下邊距,不是簡單的相加,而是取邊距較大的元素(若相邻的两个普通兄弟元素,它们的margin 值是一样的,则各取两个元素的margin 值的一半.)②关系为父子的两个d ...

  3. vbaexcel

    Sub WordTest() Dim objwordApp As Word.Application Dim objword As Word.Document Dim objSeheet As Stri ...

  4. a compromise between lock overhead and data safety

    High Performance My SQL  THIRD EDITION A locking strategy is a compromise between lock overhead and ...

  5. SpringMVC 基于注解的Controller详解

    本文出处 http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spri ...

  6. C 判断路径存在

    1   用   int   access(const   char   *pathname,   int   mode);   判断有没有此文件或目录 --它区别不出这是文件还是目录2   用   i ...

  7. trunc sysdate

    select *  from per_all_people_f papf where trunc(sysdate) between trunc(papf.effective_start_date) a ...

  8. hdc cdc

    CWindowDC dc(this); HDC hdc=dc.GetSafeHdc(); using namespace Gdiplus; Graphics graphics(hdc); graphi ...

  9. MyBatis-Generator 最佳实践

    为数据库中的表A生成A.java, A.java, A.xml 由于该插件生成的A.java, A.xml会带有example, 不希望生成example 数据库中的字段写有注释, 希望注释能自动生成 ...

  10. Android源码剖析之Framework层升级版(窗口、系统启动)

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 看本篇文章之前,建议先查看: Android源码剖析之Framework层基础版 前面讲了frame ...