Vertex Buffer Performance ( Locking/UnLocking)

Started by
2 comments, last by circlesoft 19 years, 6 months ago
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 ?
Advertisement
Vertex buffer locking is very expensive, even if you specify D3DLOCK_DISCARD. Locking vertex buffers, index buffers, and textures will cause Windows to switch from user mode to kernel mode (and back again). This takes a lot of time (check this thread for more time-intensive operations). And yes, the buffer must be unlocked before it's used again.

It looks like the operations you are doing on the grass are pretty simple. Perhaps you could do all the calculations in a vertex shader. It would require no locking, so it would be very fast. You may have to re-adjust your algorithm a little bit, but it would be a big improvement.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
I never though of using vertex shaders. Something I have never done before. A steep learning curve I think. Things do seem to be improving however. Thanks for the reply. :)
If you want to learn the basics of shader programming, the HLSL Workshop that is included in the SDK is a good start (DirectX Graphics->Tutorials and Samples->Tutorials->HLSL Workshop). There is even a lesson specifically on vertex animation, using the trig functions.

If you want to go farther than that, I recommend checking out the Shader X2 series. Also, Shader X3 should be coming out soon, so I bet that'll be a good read.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )

This topic is closed to new replies.

Advertisement