您的位置:首页 > 其它

(转)Direct3D的浮点精度问题

2009-08-26 10:01 141 查看
(http://software.intel.com/zh-cn/blogs/2009/06/08/direct3d/?cid=sw:prccsdn695)



Direct3D的浮点精度问题

作者: Wu Xiaochang 吴晓昶 (Intel) (6 篇文章) 日期: 六月 8, 2009 在 11:49 上午

Direct3D的浮点精度问题

最近编写一个Firefox的小插件,里面利用D3D进行Render,结果导致Firefox的UI渲染错误(插件后面的主窗口背景变成黑色),检查了好几天发现程序逻辑并没有问题,窗口函数和消息处理都正常。后来偶然发现同样用OpenGL渲染没有问题,再三查找发现问题出在D3D的浮点精度上面。从D3D的文档D3DCREATE条目中查到的:

D3DCREATE_FPU_PRESERVE Set the precision for Direct3D floating-point calculations to the precision used by the calling thread. If you do not specify this flag, Direct3D defaults to single-precision round-to-nearest mode for two reasons:

- Double-precision mode will reduce Direct3D performance.

- Portions of Direct3D assume floating-point unit exceptions are masked; unmasking these exceptions may result in undefined behavior

在调用IDirect3D9::CreateDevice的时候,D3D默认改变了程序的浮点精度(出于性能的考虑),导致Firefox的UI出错,加上了这个选项就正常了,而实际上性能并没有比原来低多少。

在开发实践中,很多不起眼的细节都能影响程序的结果!而很多不相关的事情其实都是有千丝万缕的联系,编码的时候总有在设计时想不到的事情发生,这就是理论和实践的区别所在!印证了一句名言:
In theory, there is no difference between theory and practice. In practice, there is。

我们平时对整数比较熟悉,但是对浮点数比较陌生,这里有篇文章比较深入地探讨了FPU:Know Your FPU http://www.stereopsis.com/FPU.html,和它的更新版.http://www.stereopsis.com/sree/fpu2006.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: