游戏开发论坛

 找回密码
 立即注册
搜索
12
返回列表 发新帖
楼主: 敢问2007

如何让CPU与GPU同步?

[复制链接]

2

主题

141

帖子

141

积分

注册会员

Rank: 2

积分
141
发表于 2007-5-18 10:56:00 | 显示全部楼层

Re: 如何让CPU与GPU同步?

并不是Lock愈小愈好,应当根据不同的硬件测试获得一个最佳的buffer大小,具体可以进一步参考Nvidia上相关性能的讨论部分,
某位兄弟提问要什么样的机器才能支持大规模的比如万级别,十万级别粒子数?
说明他并没有真正了解为什么要采用这种渲染的内在意义,
在很普通的vs 1.0 ps1.1的显卡上我都能渲染好几万级别的粒子那
下面的代码出至Introduction to 3D Game Programming with DirectX 9.0
供大家学习
_vbSize—The number of particles that our vertex buffer can hold
at a given time. This value is independent of the number of particles
in the actual particle system.
_vbOffset—This variable marks the offset (measured in particles,
not bytes) into the vertex buffer into which we should begin
copying the next batch of particles. For instance, if batch one
resides in entries 0 to 499 of the vertex buffer, the offset to start
copying batch two would be 500.
_vbBatchSize—The number of particles that we define to be in a
batch.
void PSystem::render()
{
if( !_particles.empty() )
{
// set render states
preRender();
device->SetTexture(0, _tex);
_device->SetFVF(Particle::FVF);
_device->SetStreamSource(0, _vb, 0, sizeof(Particle));
// start at beginning if we're at the end of the vb
if(_vbOffset >= _vbSize)
_vbOffset = 0;
Particle* v = 0;
_vb->Lock(
_vbOffset * sizeof( Particle ),
_vbBatchSize * sizeof( Particle ),
(void**)&v,
_vbOffset ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD);
DWORD numParticlesInBatch = 0;
//
// Until all particles have been rendered.
//
std::list<Attribute>::iterator i;
for(i = _particles.begin(); i != _particles.end(); i++)
{
if( i->_isAlive )
{
//
// Copy a batch of the living particles to the
// next vertex buffer segment
//
v->_position = i->_position;
v->_color = (D3DCOLOR)i->_color;
v++; // next element;
numParticlesInBatch++; //increase batch counter
// is this batch full?
if(numParticlesInBatch == _vbBatchSize)
{
//
// Draw the last batch of particles that was
// copied to the vertex buffer.
//
_vb->Unlock();
_device->DrawPrimitive(
D3DPT_POINTLIST,
_vbOffset,
_vbBatchSize);
//
// While that batch is drawing, start filling the
// next batch with particles.
//
// move the offset to the start of the next batch
_vbOffset += _vbBatchSize;
// don't offset into memory thats outside the vb's
// range. If we're at the end, start at the beginning.
if(_vbOffset >= _vbSize)
_vbOffset = 0;
_vb->Lock(
_vbOffset * sizeof( Particle ),
_vbBatchSize * sizeof( Particle ),
(void**)&v,
_vbOffset ? D3DLOCK_NOOVERWRITE :
D3DLOCK_DISCARD);
numParticlesInBatch = 0; // reset for new batch
}//end if
}//end if
}//end for
_vb->Unlock();
// it’s possible that the LAST batch being filled never
// got rendered because the condition
// (numParticlesInBatch == _vbBatchSize) would not have
// been satisfied. We draw the last partially filled batch now.
if( numParticlesInBatch )
{
_device->DrawPrimitive(
D3DPT_POINTLIST,
_vbOffset,
numParticlesInBatch);
}
// next block
_vbOffset += _vbBatchSize;
postRender();
}//end if
}// end render()

1

主题

51

帖子

51

积分

注册会员

Rank: 2

积分
51
发表于 2007-5-18 14:01:00 | 显示全部楼层

Re:如何让CPU与GPU同步?

最后一个跑题贴,对不住LZ了
  楼上的楼上,你的说法显然有问题,分批LOCK的总时间一定大于一次LOCK的总时间,这点应该没什么疑问,按你的说法,则任何情况都不再需要分批LOCK,看看你楼下的贴
  楼上的你最好理解别人的意思再贴,很明显我的第一点是说你的小规模VB的,第二点是想说明游戏中粒子都是你所谓的小规模,大部分百十来个,小部分可以过千,过万的基本没有,我还不至于弱智到要问别人什么样的机器才能支持大规模的比如万级别,十万级别粒子数.  至于这段书上的代码,也别以为放之四海皆正确,你把数目设为100、1000试试一次渲染出来,要是不比多次渲染快,我把它吃了, 当然,这是有个前提条件的,就是这些粒子是些一般的系统,如果BT到每个粒子要执行个千8百次的运算另当别论

106

主题

743

帖子

745

积分

高级会员

Rank: 4

积分
745
QQ
发表于 2007-5-18 15:00:00 | 显示全部楼层

Re:如何让CPU与GPU同步?

一次BeginScene()→EndScene()提交N个批,有点误解了,^_^。

45

主题

1163

帖子

1165

积分

金牌会员

Rank: 6Rank: 6

积分
1165
发表于 2007-5-18 18:47:00 | 显示全部楼层

Re:如何让CPU与GPU同步?

不知道D3DLOCK_NOWAIT,  D3DLOCK_NOOVERWRITE 对LOCK有什么影响呢?

30

主题

357

帖子

388

积分

中级会员

Rank: 3Rank: 3

积分
388
QQ
发表于 2007-5-19 13:59:00 | 显示全部楼层

Re:如何让CPU与GPU同步?

晕倒!Nvidia网站上有个关于资源缓冲大小的文章BatchBatchBatch!,说的很清楚。LZ如果能看书看到内在实质上,就不会发这个问题了。如何渲染取决与你的配置。我很同意chesskillerboss的说法,看代码也要看出思想啊。LZ的那个小地图应该不要去Lock,只在初始化的时候Lock一遍就行了。你的CPU大多数时间都是消耗在Lock上,而不是计算上。对每个程序要找出瓶颈。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-26 10:40

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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