游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2768|回复: 5

DXUT和多线程的问题

[复制链接]

7

主题

22

帖子

25

积分

注册会员

Rank: 2

积分
25
发表于 2009-4-17 13:59:00 | 显示全部楼层 |阅读模式
小弟正在做一个DEMO,用了DXUT框架,在游戏开始时开启了一个线程来加载游戏资源,主线程绘制LOADING画面。用的是_beginthread()这个函数,程序的主线程有时候会自己停止,调试的时候停在atiumdag.dll的加载时候,显卡是ATI HD3470。程序在NV系列的显卡上则是出现内存报错。经常会读取到0x0000001x的内存。
不知道各位兄弟有没有遇到这种问题?或者有没有什么其他办法读取资源的时候做LOADING

伪代码:

void loadRes(void* xx)
{
  //创建资源
  mesh* ms = new mesh();
  ms->dosomething();
  g_bFinish = true;
  
}

//创建设备
onCreateDevice()
{
_beginthread(loadRes,NULL,0);
}
//更新画面
onFrameRender()
{
  loadingBar->onFrameRender();
}
//onFrameMove
onFrameMove()
{
  if(g_bFinish )
  {
     gameState = playing;//读取完毕进入游戏状态
  }
}

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2009-4-17 14:30:00 | 显示全部楼层

Re:DXUT和多线程的问题

upp

8

主题

125

帖子

125

积分

注册会员

Rank: 2

积分
125
发表于 2009-4-21 03:33:00 | 显示全部楼层

Re:DXUT和多线程的问题

我用的就是N卡啊,_BEGINTHREAD没问题
问题出在别的代码上,你贴出的这些是看不出问题的

8

主题

125

帖子

125

积分

注册会员

Rank: 2

积分
125
发表于 2009-4-21 03:34:00 | 显示全部楼层

Re:DXUT和多线程的问题

也可能是多线程没做同步

7

主题

22

帖子

25

积分

注册会员

Rank: 2

积分
25
 楼主| 发表于 2009-5-6 13:38:00 | 显示全部楼层

Re:DXUT和多线程的问题

做了线程同步后,出错的机会小了,但是还是会内存错误,看过网上一些帖子说DXUT的多线程调用createTexture会失败,不知道是不是这个原因。。。

59

主题

984

帖子

1200

积分

金牌会员

Rank: 6Rank: 6

积分
1200
发表于 2009-5-6 14:52:00 | 显示全部楼层

Re:DXUT和多线程的问题

loading的时候要降低渲染频率
比如每load n%才重新渲染一次loading画面
否则你的load过程会非常非常慢

    while (!finishedLoading)
    {
        DrawFunkyLoadingAnimation();
        GraphicsDevice.Present();
    }
    Here be dragons! The above code will work, but is liable to make your loading hundreds of times slower.

The reason is that every time you touch the graphics device from a different thread, the framework takes out a critical section, in order to avoid heisenbadness. If the animation is constantly redrawing, the animation thread will pretty much always own this lock, so any time your load function wants to create a graphics resource such as a texture, vertex buffer, or shader, it must wait until the animation thread releases it. This might take a while if the animation thread is just constantly looping over the same piece of draw code!

The solution is to slow down your animation thread by inserting a sleep call, which stops it hogging the graphics device.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-20 12:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表