Direct3D 9中的多线程问题(Multithreading Issues)

最近在封装Direct3D 9引擎时,更新设备调用IDirect3Device::Reset()函数总是失败,查询了各种资料总是无果。后来知道了怎么开启Direct3D的调试模式(如图1,2),知道了原来创建设备和重置设备不在同一个线程就会出错(错误信息如图3)!!!​

图1,Direct3D Control Panel所在路径
图2,对Direct3D 9进行的调试设置
图3,调试信息输出的错误提示

我在Direct3D的帮助文档中(如图4,5),找到了官网给予的解释(图6):​

图4,Windows DirectX Graphics主界面
    图5,官方在编程小贴士中的位置
图6,多线程问题的讨论版块

Content:​

Full-screen Direct3D applications provide a window handle to the Direct3D run time. The window is hooked by the run time. This means that all messages passed to the application's window message procedure have first been examined by the Direct3D run time's own message-handling procedure.​​

Display mode changes are affected by support routines built into the underlying operating system. When mode changes occur, the system broadcasts several messages to all applications. In Direct3D applications, the messages are received on the window procedure thread, which is not necessarily the same thread that called IDirect3DDevice9::Reset or IDirect3D9::CreateDevice (or the final Release of IDirect3DDevice9, which can cause a display mode change). The Direct3D run time maintains several critical sections internally. Because at least one of these critical sections is held across the mode switch caused by IDirect3DDevice9::Reset or IDirect3D9::CreateDevice, these critical sections are still held when the application receives the mode-change related window messages.​

This design has some implications for multithreaded applications. In particular, an application must be sure to strongly segregate its window message handling threads from its Direct3D threads. An application that causes a mode change on one thread but makes Direct3D calls on a different thread in its window procedure is in danger of deadlock.​

For these reasons, Direct3D is designed so that the methods IDirect3DDevice9::Reset, IDirect3D9::CreateDevice, IDirect3DDevice9::TestCooperativeLevel, or the final Release of IDirect3DDevice9 can only be called from the same thread that handles window messages.​

我在程序中的主线程(主线程主要相应上层系统调用)中创建的设备,然后在另一个线程(这个线程可以叫做“渲染线程”,它不断地执行循环调用,来保证每一帧的正常显示)中重置修改设备,这样会出错。现在我把创建设备也放置在渲染线程中,解决了这个问题。

 

转载于:https://www.cnblogs.com/yooyoo/p/4717874.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值