|
|

楼主 |
发表于 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;
}
|
|