Original Post
After revisiting a very old algorithm, I have made some performance optimizations of my old grass algo. For drawing a bunch of grass, I am drawing a patch of grass and then moving that patch around a redrawing it by changing the vertex buffer according to a patch map.
for ( DWORD Patch_Count = 0 ;
Patch_Count < Patch_Size ; Patch_Count ++ )
{
px = Patch_Map [ Patch_Count ].x;
pz = Patch_Map [ Patch_Count ].z;
// Move the Patch
if( FAILED( Grass_VB->Lock( 0, bv_size, (BYTE**)pVertices, D3DLOCK_DISCARD ) ) )
return E_FAIL;
//copy the template to the vertex buffer
if ( START_PFLAG == TRUE )
{ memcpy( pVertices, Patch_VBT, bv_size );
START_PFLAG = FALSE;
}
// update the verticies
do {
pVertices -> x += px;
if ( pz > 0.0f )
pVertices -> z += pz;
pVertices++;
} while( pVertices < end_verts );
Grass_VB->Unlock();
// end move the patch
g_pd3dDevice -> DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0, v_num,
0, p_num );
}
With a field of 36 pathches I am locking/unlocking 36 times, each time drawing a patch. I also do a first copy of a patch template before altering the patch. Each patch contains 4,400 vertices ( 11 verticies per blade and 400 blades per patch ) that are indexed triangles with each being moved 36 times. A rediculous number of calculations per frame. How expensive is Locking and Unlocking the vertex buffer ? Does the Vertex Buffer have to be unlocked before drawing the primitive ?