游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3107|回复: 9

DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

[复制链接]

52

主题

155

帖子

160

积分

注册会员

Rank: 2

积分
160
发表于 2009-7-3 22:55:00 | 显示全部楼层 |阅读模式
DrawIndexedPrimitive 做 batch 一次调用,每次render前动态lock VB->更新粒子的->unlock,
5000个粒子 只有 50fps
10000个粒子 只有 20fps
50000个粒子 只有 3 fps

我是9600GT的显卡这个fps值是否正常? 还是我程序有问题?


另外我试了 Hardware Instance , 50000个粒子 也只有 3 fps ?

13

主题

312

帖子

312

积分

中级会员

Rank: 3Rank: 3

积分
312
发表于 2009-7-4 01:13:00 | 显示全部楼层

Re:DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

换成Traingle试验一下,ATI有的GPU对Point支持不好。NV不知道有没有这种毛病



--------------------------------------------------------------------------------------
OpenGPU.org(http://www.opengpu.org/bbs/index.php)主要聚焦图形硬件(包括GPU体系结构)
以及各种图形算法

59

主题

984

帖子

1200

积分

金牌会员

Rank: 6Rank: 6

积分
1200
发表于 2009-7-4 01:47:00 | 显示全部楼层

Re:DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

显然非常不正常
这种问题lz应该用工具找到瓶颈,很难一下子说出问题在哪里
如果瓶颈在GPU,那么有可能是填充率的问题,你每个粒子多大?

8

主题

390

帖子

390

积分

中级会员

Rank: 3Rank: 3

积分
390
发表于 2009-7-4 11:18:00 | 显示全部楼层

Re:DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

啥硬件,啥分辨率,粒子多大,lock,unlock之间做了什么,贴代码

39

主题

170

帖子

170

积分

注册会员

Rank: 2

积分
170
发表于 2009-7-4 17:52:00 | 显示全部楼层

Re: DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

你看一下 你的lock 和 unlock 之间的东西是不是计算量太大了

34

主题

629

帖子

629

积分

高级会员

Rank: 4

积分
629
发表于 2009-7-4 19:21:00 | 显示全部楼层

Re:DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

粒子太大了

52

主题

155

帖子

160

积分

注册会员

Rank: 2

积分
160
 楼主| 发表于 2009-7-5 00:18:00 | 显示全部楼层

Re: DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

全屏分别率1024×768 硬件 9600GT 粒子纹理 128×128 ,绘制出来估计 64×64


//每一帧都动态lockbuffer,将矩阵传递给shader,形状就是正方形
void Spice::CreateBuffer()
{

        ParticleVertex* pIPos;
        hr = g_pVBBox->Lock( 0, NULL, ( void** )&pIPos, 0 );

        if( SUCCEEDED( hr ) )
        {
                for( int i= 0; i < g_NumSpices;i++)
                {
                        ParticleVertex instanceBox;
                        D3DXMATRIX matrix = vecLeaf.mat;
                        // since no projection, encode the 4x3 part in a 3x4(shader will decode)                       
                        instanceBox.r1 = D3DXVECTOR4(matrix._11,matrix._21,matrix._31,matrix._41);
                        instanceBox.r2 = D3DXVECTOR4(matrix._12,matrix._22,matrix._32,matrix._42);
                        instanceBox.r3 = D3DXVECTOR4(matrix._13,matrix._23,matrix._33,matrix._43);
                        instanceBox.c1 = D3DXVECTOR4(vecLeaf.color_index,0,0,0);

                        instanceBox.position = D3DXVECTOR3(-1,-1,0);
                        instanceBox.uv = D3DXVECTOR2(0,1);
                        *pIPos = instanceBox, pIPos++;

                        instanceBox.position = D3DXVECTOR3(1,-1,0);
                        instanceBox.uv = D3DXVECTOR2(1,1);
                        *pIPos = instanceBox, pIPos++;

                        instanceBox.position = D3DXVECTOR3(1,1,0);
                        instanceBox.uv = D3DXVECTOR2(1,0);
                        *pIPos = instanceBox, pIPos++;

                        instanceBox.position = D3DXVECTOR3(-1,1,0);
                        instanceBox.uv = D3DXVECTOR2(0,0);
                        *pIPos = instanceBox, pIPos++;
                }
                g_pVBBox->Unlock();
        }
        else
        {
                DXUT_ERR( L"Could not lock box intra-pile-position VB", hr );
        }

    WORD *pVertices = NULL;
        WORD dwIndex[] = {0,2,1,0,3,2};//如果背面剔除功能打开,必须按指定的时针顺序摆放顶点
        int nLength = sizeof(dwIndex);
        int base_index = 0;

    V(g_pIBBox->Lock(0, 0, (void **)&pVertices, 0));
        for( int i= 0; i < g_NumSpices;i++)
        {
                for(int j=0;j<6;j++) pVertices[base_index+j] = dwIndex[j]+i*4;
                base_index+=6;
        }
        V(g_pIBBox->Unlock());

}


void Spice::draw_Spice()
{
        CreateBuffer();

        g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
        g_pd3dDevice->SetRenderState( D3DRS_CULLMODE , D3DCULL_NONE);

        g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
        g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA );
       
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_ADD );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

        D3DXMATRIXA16 matView,matProj,matViewProj;

        V(g_pd3dDevice->GetTransform(D3DTS_VIEW,&matView));
        V(g_pd3dDevice->GetTransform(D3DTS_PROJECTION,&matProj));       
        matViewProj = matView*matProj;
       
        D3DXMATRIXA16 world;
        //D3DXMatrixTranslation(&world,6,4,0);
        D3DXMatrixIdentity(&world);

        V(g_pEffect9->SetMatrix( "world", &world  ));
        V(g_pEffect9->SetMatrix( "viewProjection", &matViewProj  ));
        V(g_pEffect9->SetTexture( "t1",g_pleavesTexture[0]));
        V(g_pEffect9->SetTexture( "t2",g_pleavesTexture[1]));
        V(g_pEffect9->SetTexture( "t3",g_pleavesTexture[2]));
        V(g_pEffect9->SetTexture( "t4",g_pleavesTexture[3]));
        V(g_pEffect9->SetTechnique("textured"));

        HRESULT hr;
        UINT iPass, cPasses;
        V( g_pd3dDevice->SetVertexDeclaration( g_pVertexDeclHardware ) );

        // Stream zero is our model, and its frequency is how we communicate the number of instances required,
        // which in this case is the total number of boxes

        V( g_pd3dDevice->SetStreamSource( 0, g_pVBBox, 0, sizeof( ParticleVertex ) ) );       
        V(g_pd3dDevice->SetIndices(g_pIBBox));


        V( g_pEffect9->Begin( &cPasses, 0 ) );
        for( iPass = 0; iPass < cPasses; iPass++ )
        {
                V( g_pEffect9->BeginPass( iPass ) );
                //V( g_pEffect9->CommitChanges() );

                V( g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4*g_NumSpices,0,g_NumSpices*2));
                V( g_pEffect9->EndPass() );
        }
        V( g_pEffect9->End() );

        V( g_pd3dDevice->SetStreamSourceFreq( 0, 1 ) );
        V( g_pd3dDevice->SetStreamSourceFreq( 1, 1 ) );


}

////////////////////////Shader
//------------------------------------
vertexOutput VS_TransformAndTexture(vertexInput IN)
{
    vertexOutput OUT = (vertexOutput)0;
   
   
           float4 row1 = float4(IN.vInstanceMatrix1.x,IN.vInstanceMatrix2.x,IN.vInstanceMatrix3.x,0);
        float4 row2 = float4(IN.vInstanceMatrix1.y,IN.vInstanceMatrix2.y,IN.vInstanceMatrix3.y,0);
        float4 row3 = float4(IN.vInstanceMatrix1.z,IN.vInstanceMatrix2.z,IN.vInstanceMatrix3.z,0);
        float4 row4 = float4(IN.vInstanceMatrix1.w,IN.vInstanceMatrix2.w,IN.vInstanceMatrix3.w,1);
        float4x4 World2 = float4x4(row1,row2,row3,row4);
   
   
    //World2 =  world  ;

    float4 Pos = mul(float4(IN.position,1),World2);   
    Pos = mul(Pos,viewProjection);
    OUT.hPosition  = Pos;     
    OUT.texCoordDiffuse = float4(IN.texCoordDiffuse,IN.particledata.x,0);   
   
    return OUT;
}





//-----------------------------------
float4 PS_Textured( vertexOutput IN): COLOR
{

float4 diffuseTexture;
int index = IN.texCoordDiffuse.z+1;
if(index==1)
   diffuseTexture = tex2D(t1s,IN.texCoordDiffuse.xy );
else if(index==2)
   diffuseTexture = tex2D(t2s,IN.texCoordDiffuse.xy );
else if(index==3)
   diffuseTexture = tex2D(t3s,IN.texCoordDiffuse.xy );
else if(index==4)
   diffuseTexture = tex2D(t4s,IN.texCoordDiffuse.xy );   
  return diffuseTexture;
}

52

主题

155

帖子

160

积分

注册会员

Rank: 2

积分
160
 楼主| 发表于 2009-7-5 00:20:00 | 显示全部楼层

Re:DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

//顶点声明如下
D3DVERTEXELEMENT9 g_VertexElemHardware[] =
        {
                { 0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},               
                { 0, 12, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
                { 0, 20, D3DDECLTYPE_FLOAT4,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
                { 0, 36, D3DDECLTYPE_FLOAT4,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
                { 0, 52, D3DDECLTYPE_FLOAT4,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
                { 0, 68, D3DDECLTYPE_FLOAT4,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
                D3DDECL_END()
        };

52

主题

155

帖子

160

积分

注册会员

Rank: 2

积分
160
 楼主| 发表于 2009-7-5 00:41:00 | 显示全部楼层

Re: DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

我用PIX载入分析了下用了50000个粒子,发现最大的是第一步 Clear ,花掉了 420ms
Lock & unlock花掉了 20ms,BeginPass花掉了60ms , DrawIndexedPrimitive 非常快0.2ms.为什么啊?

1611        Frame 45        23087878144        45        495740928                        2.0
2506        <0x04F6A668> IDirect3DDevice9::Clear(0x00000000, NULL, 0x00000003, D3DCOLOR_ARGB(0x00,0x00,0x00,0x00), 1.000f, 0x00000000)        23102062592                                62491       
2507        <0x04F6A668> IDirect3DDevice9::BeginScene()        23521943552                                       
2508        <0x04FAF478> IDirect3DVertexBuffer9:ock(0, 0, 0x0012F80C, D3DLOCK_DISCARD)        23521949696                                       
2509        <0x04FAF478> IDirect3DVertexBuffer9::Unlock()        23540727808                                       
2510        <0x04FB2098> IDirect3DIndexBuffer9::Lock(0, 0, 0x0012F770, D3DLOCK_DISCARD)        23559385088                                       
2511        <0x04FB2098> IDirect3DIndexBuffer9::Unlock()        23560861696                                       
2512        <0x04F6A668> IDirect3DDevice9::SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)        23561373696                                       
2513        <0x04F6A668> IDirect3DDevice9::SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)        23561379840                                       
2514        <0x04F6A668> IDirect3DDevice9::SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)        23561381888                                       
2515        <0x04F6A668> IDirect3DDevice9::SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)        23561383936                                       
2516        <0x04F6A668> IDirect3DDevice9::SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_ADD)        23561385984                                       
2517        <0x04F6A668> IDirect3DDevice9::SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1)        23561390080                                       
2518        <0x04F6A668> IDirect3DDevice9::SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE)        23561392128                                       
2519        <0x04F6A668> IDirect3DDevice9::SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE)        23561394176                                       
2520        <0x04F6A668> IDirect3DDevice9::GetTransform(D3DTS_VIEW, 0x0012F980)        23561398272                                       
2521        <0x04F6A668> IDirect3DDevice9::GetTransform(D3DTS_PROJECTION, 0x0012F940)        23561400320                                       
2522        <0x04FAF028> ID3DXEffect::SetMatrix(0x007691D4, 0x0012F8C0)        23561406464                                       
2523        <0x04FAF028> ID3DXEffect::SetMatrix(0x00767358, 0x0012F900)        23561412608                                       
2524        <0x04FAF028> ID3DXEffect::SetTexture(0x00769168, 0x04F674A0)        23561414656                                       
2525        <0x04FAF028> ID3DXEffect::SetTexture(0x007690E8, 0x05006F20)        23561416704                                       
2526        <0x04FAF028> ID3DXEffect::SetTexture(0x00769068, 0x05008338)        23561418752                                       
2527        <0x04FAF028> ID3DXEffect::SetTexture(0x00768FE8, 0x04F64228)        23561420800                                       
2528        <0x04FAF028> ID3DXEffect::SetTechnique(0x00767238)        23561422848                                       
2529        <0x04F6A668> IDirect3DDevice9::SetVertexDeclaration(0x04FAF078)        23561426944                                       
2530        <0x04F6A668> IDirect3DDevice9::SetStreamSource(0, 0x04FAF478, 0, 84)        23561431040                                       
2531        <0x04F6A668> IDirect3DDevice9::SetIndices(0x04FB2098)        23561435136                                       
2532        <0x04FAF028> ID3DXEffect::Begin(0x0012F8B4, 0x00000000)        23561439232                                       
2620        <0x04FAF028> ID3DXEffect::BeginPass(0)        23561586688                                       
2858        <0x04F6A668> IDirect3DDevice9:rawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 200000, 0, 100000)        23567333376                                562800586       
2859        <0x04FAF028> ID3DXEffect::EndPass()        23567351808                                       
2860        <0x04FAF028> ID3DXEffect::End()        23567353856                                       
2866        <0x04F6A668> IDirect3DDevice9::SetStreamSourceFreq(0, 1)        23567386624                                       
2867        <0x04F6A668> IDirect3DDevice9::SetStreamSourceFreq(1, 1)        23567388672                                       
2868        <0x04F6A668> IDirect3DDevice9::EndScene()        23567392768                                       
2869        <0x04F6A668> IDirect3DDevice9::BeginScene()        23567396864                                       
2870        <0x04FB28B0> ID3DXSprite::Begin(0x00000030)        23567400960                                       
3012        <0x04F69FA8> ID3DXFont::DrawTextW(0x04FB28B0, 0x04E3640C, -1, 0x0012F9B4, 0x00000100, D3DCOLOR_ARGB(0xff,0xff,0xff,0xff))        23567630336                                       
3111        <0x04F69FA8> ID3DXFont::DrawTextW(0x04FB28B0, 0x0076A880, -1, 0x0012F9B4, 0x00000100, D3DCOLOR_ARGB(0xff,0xff,0xff,0xff))        23568832512                                       
3136        <0x04FB28B0> ID3DXSprite::End()        23568891904                                       
3217        <0x04F6A668> IDirect3DDevice9::EndScene()        23575332864                                       
3218        <0x04F6A668> IDirect3DDevice9:resent(NULL, NULL, NULL, NULL)        23575435264                                       

52

主题

155

帖子

160

积分

注册会员

Rank: 2

积分
160
 楼主| 发表于 2009-7-8 22:55:00 | 显示全部楼层

Re:DrawIndexedPrimitive 做 batch 10000个粒子 只有 20fps

顶下,有没有人帮我看看
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-19 19:54

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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